Skip to content

Commit f846260

Browse files
ast: model PostgreSQL SQL/JSON clauses and JSON_TABLE on_error
1 parent a281171 commit f846260

3 files changed

Lines changed: 385 additions & 7 deletions

File tree

src/ast/mod.rs

Lines changed: 350 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ pub use self::query::{
9696
ForJson, ForXml, FormatClause, GroupByExpr, GroupByWithModifier, IdentWithAlias,
9797
IlikeSelectItem, InputFormatClause, Interpolate, InterpolateExpr, Join, JoinConstraint,
9898
JoinOperator, JsonTableColumn, JsonTableColumnErrorHandling, JsonTableNamedColumn,
99-
JsonTableNestedColumn, LateralView, LimitClause, LockClause, LockType, MatchRecognizePattern,
100-
MatchRecognizeSymbol, Measure, NamedWindowDefinition, NamedWindowExpr, NonBlock, Offset,
101-
OffsetRows, OpenJsonTableColumn, OrderBy, OrderByExpr, OrderByKind, OrderByOptions,
102-
PipeOperator, PivotValueSource, ProjectionSelect, Query, RenameSelectItem,
103-
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
104-
SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind, SelectModifiers,
99+
JsonTableNestedColumn, JsonTableOnErrorHandling, LateralView, LimitClause, LockClause,
100+
LockType, MatchRecognizePattern, MatchRecognizeSymbol, Measure, NamedWindowDefinition,
101+
NamedWindowExpr, NonBlock, Offset, OffsetRows, OpenJsonTableColumn, OrderBy, OrderByExpr,
102+
OrderByKind, OrderByOptions, PipeOperator, PivotValueSource, ProjectionSelect, Query,
103+
RenameSelectItem, RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch,
104+
Select, SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind, SelectModifiers,
105105
SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table, TableAlias,
106106
TableAliasColumnDef, TableFactor, TableFunctionArgs, TableIndexHintForClause,
107107
TableIndexHintType, TableIndexHints, TableIndexType, TableSample, TableSampleBucket,
@@ -8233,6 +8233,20 @@ pub enum FunctionArgumentClause {
82338233
///
82348234
/// [`JSON_OBJECT`](https://www.postgresql.org/docs/current/functions-json.html#:~:text=json_object)
82358235
JsonReturningClause(JsonReturningClause),
8236+
/// The `PASSING` clause for SQL/JSON query functions in PostgreSQL.
8237+
JsonPassingClause(JsonPassingClause),
8238+
/// The SQL/JSON `... ON ERROR` clause for `JSON_EXISTS`.
8239+
JsonExistsOnErrorClause(JsonExistsOnErrorBehavior),
8240+
/// The SQL/JSON `... ON EMPTY`/`... ON ERROR` clause for `JSON_VALUE`.
8241+
JsonValueBehaviorClause(JsonValueBehaviorClause),
8242+
/// The SQL/JSON wrapper behavior clause for `JSON_QUERY`.
8243+
JsonQueryWrapperClause(JsonQueryWrapperClause),
8244+
/// The SQL/JSON quote handling clause for `JSON_QUERY`.
8245+
JsonQueryQuotesClause(JsonQueryQuotesClause),
8246+
/// The SQL/JSON `... ON EMPTY`/`... ON ERROR` clause for `JSON_QUERY`.
8247+
JsonQueryBehaviorClause(JsonQueryBehaviorClause),
8248+
/// The SQL/JSON format clause for JSON query functions.
8249+
JsonFormatClause(JsonFormatClause),
82368250
}
82378251

82388252
impl fmt::Display for FunctionArgumentClause {
@@ -8252,6 +8266,27 @@ impl fmt::Display for FunctionArgumentClause {
82528266
FunctionArgumentClause::JsonReturningClause(returning_clause) => {
82538267
write!(f, "{returning_clause}")
82548268
}
8269+
FunctionArgumentClause::JsonPassingClause(passing_clause) => {
8270+
write!(f, "{passing_clause}")
8271+
}
8272+
FunctionArgumentClause::JsonExistsOnErrorClause(on_error_clause) => {
8273+
write!(f, "{on_error_clause}")
8274+
}
8275+
FunctionArgumentClause::JsonValueBehaviorClause(behavior_clause) => {
8276+
write!(f, "{behavior_clause}")
8277+
}
8278+
FunctionArgumentClause::JsonQueryWrapperClause(wrapper_clause) => {
8279+
write!(f, "{wrapper_clause}")
8280+
}
8281+
FunctionArgumentClause::JsonQueryQuotesClause(quotes_clause) => {
8282+
write!(f, "{quotes_clause}")
8283+
}
8284+
FunctionArgumentClause::JsonQueryBehaviorClause(behavior_clause) => {
8285+
write!(f, "{behavior_clause}")
8286+
}
8287+
FunctionArgumentClause::JsonFormatClause(format_clause) => {
8288+
write!(f, "{format_clause}")
8289+
}
82558290
}
82568291
}
82578292
}
@@ -10975,6 +11010,315 @@ impl Display for JsonReturningClause {
1097511010
}
1097611011
}
1097711012

11013+
/// SQL/JSON PASSING clause.
11014+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11015+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11016+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11017+
pub struct JsonPassingClause {
11018+
/// Arguments passed in the `PASSING` clause.
11019+
pub args: Vec<JsonPassingArg>,
11020+
}
11021+
11022+
impl Display for JsonPassingClause {
11023+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11024+
write!(f, "PASSING {}", display_comma_separated(&self.args))
11025+
}
11026+
}
11027+
11028+
/// A single SQL/JSON `PASSING` argument.
11029+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11030+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11031+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11032+
pub struct JsonPassingArg {
11033+
/// Value expression to pass to the JSON path.
11034+
pub expr: Expr,
11035+
/// Variable name used in the path expression.
11036+
pub name: Ident,
11037+
}
11038+
11039+
impl Display for JsonPassingArg {
11040+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11041+
write!(f, "{} AS {}", self.expr, self.name)
11042+
}
11043+
}
11044+
11045+
/// SQL/JSON `... ON ERROR` behavior for `JSON_EXISTS`.
11046+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11047+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11048+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11049+
pub enum JsonExistsOnErrorBehavior {
11050+
/// `ERROR ON ERROR`
11051+
Error,
11052+
/// `TRUE ON ERROR`
11053+
True,
11054+
/// `FALSE ON ERROR`
11055+
False,
11056+
/// `UNKNOWN ON ERROR`
11057+
Unknown,
11058+
}
11059+
11060+
impl Display for JsonExistsOnErrorBehavior {
11061+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11062+
match self {
11063+
JsonExistsOnErrorBehavior::Error => write!(f, "ERROR ON ERROR"),
11064+
JsonExistsOnErrorBehavior::True => write!(f, "TRUE ON ERROR"),
11065+
JsonExistsOnErrorBehavior::False => write!(f, "FALSE ON ERROR"),
11066+
JsonExistsOnErrorBehavior::Unknown => write!(f, "UNKNOWN ON ERROR"),
11067+
}
11068+
}
11069+
}
11070+
11071+
/// SQL/JSON behavior target for `JSON_VALUE` and `JSON_QUERY`.
11072+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11073+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11074+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11075+
pub enum JsonBehaviorTarget {
11076+
/// `... ON EMPTY`
11077+
Empty,
11078+
/// `... ON ERROR`
11079+
Error,
11080+
}
11081+
11082+
impl Display for JsonBehaviorTarget {
11083+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11084+
match self {
11085+
JsonBehaviorTarget::Empty => write!(f, "EMPTY"),
11086+
JsonBehaviorTarget::Error => write!(f, "ERROR"),
11087+
}
11088+
}
11089+
}
11090+
11091+
/// SQL/JSON behavior variant for `JSON_VALUE`.
11092+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11093+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11094+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11095+
pub enum JsonValueBehavior {
11096+
/// `ERROR`
11097+
Error,
11098+
/// `NULL`
11099+
Null,
11100+
/// `DEFAULT <expr>`
11101+
Default(Expr),
11102+
}
11103+
11104+
impl Display for JsonValueBehavior {
11105+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11106+
match self {
11107+
JsonValueBehavior::Error => write!(f, "ERROR"),
11108+
JsonValueBehavior::Null => write!(f, "NULL"),
11109+
JsonValueBehavior::Default(expr) => write!(f, "DEFAULT {expr}"),
11110+
}
11111+
}
11112+
}
11113+
11114+
/// SQL/JSON behavior clause for `JSON_VALUE`.
11115+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11116+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11117+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11118+
pub struct JsonValueBehaviorClause {
11119+
/// Behavior applied on target condition.
11120+
pub behavior: JsonValueBehavior,
11121+
/// Target condition: `ON EMPTY` or `ON ERROR`.
11122+
pub target: JsonBehaviorTarget,
11123+
}
11124+
11125+
impl Display for JsonValueBehaviorClause {
11126+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11127+
write!(f, "{} ON {}", self.behavior, self.target)
11128+
}
11129+
}
11130+
11131+
/// SQL/JSON wrapper behavior clause for `JSON_QUERY`.
11132+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11133+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11134+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11135+
pub enum JsonQueryWrapperClause {
11136+
/// `WITHOUT WRAPPER`
11137+
WithoutWrapper,
11138+
/// `WITHOUT ARRAY WRAPPER`
11139+
WithoutArrayWrapper,
11140+
/// `WITH WRAPPER`
11141+
WithWrapper,
11142+
/// `WITH ARRAY WRAPPER`
11143+
WithArrayWrapper,
11144+
/// `WITH CONDITIONAL WRAPPER`
11145+
WithConditionalWrapper,
11146+
/// `WITH CONDITIONAL ARRAY WRAPPER`
11147+
WithConditionalArrayWrapper,
11148+
/// `WITH UNCONDITIONAL WRAPPER`
11149+
WithUnconditionalWrapper,
11150+
/// `WITH UNCONDITIONAL ARRAY WRAPPER`
11151+
WithUnconditionalArrayWrapper,
11152+
}
11153+
11154+
impl Display for JsonQueryWrapperClause {
11155+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11156+
match self {
11157+
JsonQueryWrapperClause::WithoutWrapper => write!(f, "WITHOUT WRAPPER"),
11158+
JsonQueryWrapperClause::WithoutArrayWrapper => write!(f, "WITHOUT ARRAY WRAPPER"),
11159+
JsonQueryWrapperClause::WithWrapper => write!(f, "WITH WRAPPER"),
11160+
JsonQueryWrapperClause::WithArrayWrapper => write!(f, "WITH ARRAY WRAPPER"),
11161+
JsonQueryWrapperClause::WithConditionalWrapper => {
11162+
write!(f, "WITH CONDITIONAL WRAPPER")
11163+
}
11164+
JsonQueryWrapperClause::WithConditionalArrayWrapper => {
11165+
write!(f, "WITH CONDITIONAL ARRAY WRAPPER")
11166+
}
11167+
JsonQueryWrapperClause::WithUnconditionalWrapper => {
11168+
write!(f, "WITH UNCONDITIONAL WRAPPER")
11169+
}
11170+
JsonQueryWrapperClause::WithUnconditionalArrayWrapper => {
11171+
write!(f, "WITH UNCONDITIONAL ARRAY WRAPPER")
11172+
}
11173+
}
11174+
}
11175+
}
11176+
11177+
/// SQL/JSON quote handling mode for `JSON_QUERY`.
11178+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11179+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11180+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11181+
pub enum JsonQueryQuotesMode {
11182+
/// `KEEP`
11183+
Keep,
11184+
/// `OMIT`
11185+
Omit,
11186+
}
11187+
11188+
impl Display for JsonQueryQuotesMode {
11189+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11190+
match self {
11191+
JsonQueryQuotesMode::Keep => write!(f, "KEEP"),
11192+
JsonQueryQuotesMode::Omit => write!(f, "OMIT"),
11193+
}
11194+
}
11195+
}
11196+
11197+
/// SQL/JSON quote handling clause for `JSON_QUERY`.
11198+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11199+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11200+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11201+
pub struct JsonQueryQuotesClause {
11202+
/// Whether to keep or omit quotes.
11203+
pub mode: JsonQueryQuotesMode,
11204+
/// Whether `ON SCALAR STRING` was specified.
11205+
pub on_scalar_string: bool,
11206+
}
11207+
11208+
impl Display for JsonQueryQuotesClause {
11209+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11210+
write!(f, "{} QUOTES", self.mode)?;
11211+
if self.on_scalar_string {
11212+
write!(f, " ON SCALAR STRING")?;
11213+
}
11214+
Ok(())
11215+
}
11216+
}
11217+
11218+
/// SQL/JSON behavior variant for `JSON_QUERY`.
11219+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11220+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11221+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11222+
pub enum JsonQueryBehavior {
11223+
/// `ERROR`
11224+
Error,
11225+
/// `NULL`
11226+
Null,
11227+
/// `EMPTY`
11228+
Empty,
11229+
/// `EMPTY ARRAY`
11230+
EmptyArray,
11231+
/// `EMPTY OBJECT`
11232+
EmptyObject,
11233+
/// `DEFAULT <expr>`
11234+
Default(Expr),
11235+
}
11236+
11237+
impl Display for JsonQueryBehavior {
11238+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11239+
match self {
11240+
JsonQueryBehavior::Error => write!(f, "ERROR"),
11241+
JsonQueryBehavior::Null => write!(f, "NULL"),
11242+
JsonQueryBehavior::Empty => write!(f, "EMPTY"),
11243+
JsonQueryBehavior::EmptyArray => write!(f, "EMPTY ARRAY"),
11244+
JsonQueryBehavior::EmptyObject => write!(f, "EMPTY OBJECT"),
11245+
JsonQueryBehavior::Default(expr) => write!(f, "DEFAULT {expr}"),
11246+
}
11247+
}
11248+
}
11249+
11250+
/// SQL/JSON behavior clause for `JSON_QUERY`.
11251+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11252+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11253+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11254+
pub struct JsonQueryBehaviorClause {
11255+
/// Behavior applied on target condition.
11256+
pub behavior: JsonQueryBehavior,
11257+
/// Target condition: `ON EMPTY` or `ON ERROR`.
11258+
pub target: JsonBehaviorTarget,
11259+
}
11260+
11261+
impl Display for JsonQueryBehaviorClause {
11262+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11263+
write!(f, "{} ON {}", self.behavior, self.target)
11264+
}
11265+
}
11266+
11267+
/// SQL/JSON `FORMAT` clause.
11268+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11269+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11270+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11271+
pub struct JsonFormatClause {
11272+
/// Format type.
11273+
pub format: JsonFormatType,
11274+
/// Optional encoding.
11275+
pub encoding: Option<JsonFormatEncoding>,
11276+
}
11277+
11278+
impl Display for JsonFormatClause {
11279+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11280+
write!(f, "FORMAT {}", self.format)?;
11281+
if let Some(encoding) = self.encoding {
11282+
write!(f, " ENCODING {encoding}")?;
11283+
}
11284+
Ok(())
11285+
}
11286+
}
11287+
11288+
/// SQL/JSON format type.
11289+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11290+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11291+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11292+
pub enum JsonFormatType {
11293+
/// `JSON`
11294+
Json,
11295+
}
11296+
11297+
impl Display for JsonFormatType {
11298+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11299+
match self {
11300+
JsonFormatType::Json => write!(f, "JSON"),
11301+
}
11302+
}
11303+
}
11304+
11305+
/// SQL/JSON format encoding.
11306+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11307+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11308+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11309+
pub enum JsonFormatEncoding {
11310+
/// `UTF8`
11311+
Utf8,
11312+
}
11313+
11314+
impl Display for JsonFormatEncoding {
11315+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11316+
match self {
11317+
JsonFormatEncoding::Utf8 => write!(f, "UTF8"),
11318+
}
11319+
}
11320+
}
11321+
1097811322
/// rename object definition
1097911323
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1098011324
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

0 commit comments

Comments
 (0)