Skip to content

Commit 3d10874

Browse files
ast: model PostgreSQL SQL/JSON clauses and JSON_TABLE on_error
1 parent 7c78d13 commit 3d10874

3 files changed

Lines changed: 387 additions & 9 deletions

File tree

src/ast/mod.rs

Lines changed: 352 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ 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-
OrderBySort, PipeOperator, PivotValueSource, ProjectionSelect, Query, RenameSelectItem,
103-
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
104-
SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind, SelectModifiers,
105-
SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table, TableAlias,
106-
TableAliasColumnDef, TableFactor, TableFunctionArgs, TableIndexHintForClause,
99+
JsonTableNestedColumn, JsonTableOnErrorHandling, LateralView, LimitClause, LockClause,
100+
LockType, MatchRecognizePattern, MatchRecognizeSymbol, Measure, NamedWindowDefinition,
101+
NamedWindowExpr, NonBlock, Offset, OffsetRows, OpenJsonTableColumn, OrderBy, OrderByExpr,
102+
OrderByKind, OrderByOptions, OrderBySort, PipeOperator, PivotValueSource, ProjectionSelect,
103+
Query, RenameSelectItem, RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem,
104+
RowsPerMatch, Select, SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind,
105+
SelectModifiers, SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table,
106+
TableAlias, TableAliasColumnDef, TableFactor, TableFunctionArgs, TableIndexHintForClause,
107107
TableIndexHintType, TableIndexHints, TableIndexType, TableSample, TableSampleBucket,
108108
TableSampleKind, TableSampleMethod, TableSampleModifier, TableSampleQuantity, TableSampleSeed,
109109
TableSampleSeedModifier, TableSampleUnit, TableVersion, TableWithJoins, Top, TopQuantity,
@@ -8226,6 +8226,20 @@ pub enum FunctionArgumentClause {
82268226
///
82278227
/// [`JSON_OBJECT`](https://www.postgresql.org/docs/current/functions-json.html#:~:text=json_object)
82288228
JsonReturningClause(JsonReturningClause),
8229+
/// The `PASSING` clause for SQL/JSON query functions in PostgreSQL.
8230+
JsonPassingClause(JsonPassingClause),
8231+
/// The SQL/JSON `... ON ERROR` clause for `JSON_EXISTS`.
8232+
JsonExistsOnErrorClause(JsonExistsOnErrorBehavior),
8233+
/// The SQL/JSON `... ON EMPTY`/`... ON ERROR` clause for `JSON_VALUE`.
8234+
JsonValueBehaviorClause(JsonValueBehaviorClause),
8235+
/// The SQL/JSON wrapper behavior clause for `JSON_QUERY`.
8236+
JsonQueryWrapperClause(JsonQueryWrapperClause),
8237+
/// The SQL/JSON quote handling clause for `JSON_QUERY`.
8238+
JsonQueryQuotesClause(JsonQueryQuotesClause),
8239+
/// The SQL/JSON `... ON EMPTY`/`... ON ERROR` clause for `JSON_QUERY`.
8240+
JsonQueryBehaviorClause(JsonQueryBehaviorClause),
8241+
/// The SQL/JSON format clause for JSON query functions.
8242+
JsonFormatClause(JsonFormatClause),
82298243
}
82308244

82318245
impl fmt::Display for FunctionArgumentClause {
@@ -8245,6 +8259,27 @@ impl fmt::Display for FunctionArgumentClause {
82458259
FunctionArgumentClause::JsonReturningClause(returning_clause) => {
82468260
write!(f, "{returning_clause}")
82478261
}
8262+
FunctionArgumentClause::JsonPassingClause(passing_clause) => {
8263+
write!(f, "{passing_clause}")
8264+
}
8265+
FunctionArgumentClause::JsonExistsOnErrorClause(on_error_clause) => {
8266+
write!(f, "{on_error_clause}")
8267+
}
8268+
FunctionArgumentClause::JsonValueBehaviorClause(behavior_clause) => {
8269+
write!(f, "{behavior_clause}")
8270+
}
8271+
FunctionArgumentClause::JsonQueryWrapperClause(wrapper_clause) => {
8272+
write!(f, "{wrapper_clause}")
8273+
}
8274+
FunctionArgumentClause::JsonQueryQuotesClause(quotes_clause) => {
8275+
write!(f, "{quotes_clause}")
8276+
}
8277+
FunctionArgumentClause::JsonQueryBehaviorClause(behavior_clause) => {
8278+
write!(f, "{behavior_clause}")
8279+
}
8280+
FunctionArgumentClause::JsonFormatClause(format_clause) => {
8281+
write!(f, "{format_clause}")
8282+
}
82488283
}
82498284
}
82508285
}
@@ -10968,6 +11003,315 @@ impl Display for JsonReturningClause {
1096811003
}
1096911004
}
1097011005

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

0 commit comments

Comments
 (0)