Skip to content

Commit e6d873c

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

File tree

3 files changed

+385
-7
lines changed

3 files changed

+385
-7
lines changed

src/ast/mod.rs

Lines changed: 350 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ pub use self::query::{
9494
ForJson, ForXml, FormatClause, GroupByExpr, GroupByWithModifier, IdentWithAlias,
9595
IlikeSelectItem, InputFormatClause, Interpolate, InterpolateExpr, Join, JoinConstraint,
9696
JoinOperator, JsonTableColumn, JsonTableColumnErrorHandling, JsonTableNamedColumn,
97-
JsonTableNestedColumn, LateralView, LimitClause, LockClause, LockType, MatchRecognizePattern,
98-
MatchRecognizeSymbol, Measure, NamedWindowDefinition, NamedWindowExpr, NonBlock, Offset,
99-
OffsetRows, OpenJsonTableColumn, OrderBy, OrderByExpr, OrderByKind, OrderByOptions,
100-
PipeOperator, PivotValueSource, ProjectionSelect, Query, RenameSelectItem,
101-
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
102-
SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind, SelectModifiers,
97+
JsonTableNestedColumn, JsonTableOnErrorHandling, LateralView, LimitClause, LockClause,
98+
LockType, MatchRecognizePattern, MatchRecognizeSymbol, Measure, NamedWindowDefinition,
99+
NamedWindowExpr, NonBlock, Offset, OffsetRows, OpenJsonTableColumn, OrderBy, OrderByExpr,
100+
OrderByKind, OrderByOptions, PipeOperator, PivotValueSource, ProjectionSelect, Query,
101+
RenameSelectItem, RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch,
102+
Select, SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind, SelectModifiers,
103103
SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table, TableAlias,
104104
TableAliasColumnDef, TableFactor, TableFunctionArgs, TableIndexHintForClause,
105105
TableIndexHintType, TableIndexHints, TableIndexType, TableSample, TableSampleBucket,
@@ -8004,6 +8004,20 @@ pub enum FunctionArgumentClause {
80048004
///
80058005
/// [`JSON_OBJECT`](https://www.postgresql.org/docs/current/functions-json.html#:~:text=json_object)
80068006
JsonReturningClause(JsonReturningClause),
8007+
/// The `PASSING` clause for SQL/JSON query functions in PostgreSQL.
8008+
JsonPassingClause(JsonPassingClause),
8009+
/// The SQL/JSON `... ON ERROR` clause for `JSON_EXISTS`.
8010+
JsonExistsOnErrorClause(JsonExistsOnErrorBehavior),
8011+
/// The SQL/JSON `... ON EMPTY`/`... ON ERROR` clause for `JSON_VALUE`.
8012+
JsonValueBehaviorClause(JsonValueBehaviorClause),
8013+
/// The SQL/JSON wrapper behavior clause for `JSON_QUERY`.
8014+
JsonQueryWrapperClause(JsonQueryWrapperClause),
8015+
/// The SQL/JSON quote handling clause for `JSON_QUERY`.
8016+
JsonQueryQuotesClause(JsonQueryQuotesClause),
8017+
/// The SQL/JSON `... ON EMPTY`/`... ON ERROR` clause for `JSON_QUERY`.
8018+
JsonQueryBehaviorClause(JsonQueryBehaviorClause),
8019+
/// The SQL/JSON format clause for JSON query functions.
8020+
JsonFormatClause(JsonFormatClause),
80078021
}
80088022

80098023
impl fmt::Display for FunctionArgumentClause {
@@ -8023,6 +8037,27 @@ impl fmt::Display for FunctionArgumentClause {
80238037
FunctionArgumentClause::JsonReturningClause(returning_clause) => {
80248038
write!(f, "{returning_clause}")
80258039
}
8040+
FunctionArgumentClause::JsonPassingClause(passing_clause) => {
8041+
write!(f, "{passing_clause}")
8042+
}
8043+
FunctionArgumentClause::JsonExistsOnErrorClause(on_error_clause) => {
8044+
write!(f, "{on_error_clause}")
8045+
}
8046+
FunctionArgumentClause::JsonValueBehaviorClause(behavior_clause) => {
8047+
write!(f, "{behavior_clause}")
8048+
}
8049+
FunctionArgumentClause::JsonQueryWrapperClause(wrapper_clause) => {
8050+
write!(f, "{wrapper_clause}")
8051+
}
8052+
FunctionArgumentClause::JsonQueryQuotesClause(quotes_clause) => {
8053+
write!(f, "{quotes_clause}")
8054+
}
8055+
FunctionArgumentClause::JsonQueryBehaviorClause(behavior_clause) => {
8056+
write!(f, "{behavior_clause}")
8057+
}
8058+
FunctionArgumentClause::JsonFormatClause(format_clause) => {
8059+
write!(f, "{format_clause}")
8060+
}
80268061
}
80278062
}
80288063
}
@@ -10700,6 +10735,315 @@ impl Display for JsonReturningClause {
1070010735
}
1070110736
}
1070210737

10738+
/// SQL/JSON PASSING clause.
10739+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10740+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10741+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10742+
pub struct JsonPassingClause {
10743+
/// Arguments passed in the `PASSING` clause.
10744+
pub args: Vec<JsonPassingArg>,
10745+
}
10746+
10747+
impl Display for JsonPassingClause {
10748+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10749+
write!(f, "PASSING {}", display_comma_separated(&self.args))
10750+
}
10751+
}
10752+
10753+
/// A single SQL/JSON `PASSING` argument.
10754+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10755+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10756+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10757+
pub struct JsonPassingArg {
10758+
/// Value expression to pass to the JSON path.
10759+
pub expr: Expr,
10760+
/// Variable name used in the path expression.
10761+
pub name: Ident,
10762+
}
10763+
10764+
impl Display for JsonPassingArg {
10765+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10766+
write!(f, "{} AS {}", self.expr, self.name)
10767+
}
10768+
}
10769+
10770+
/// SQL/JSON `... ON ERROR` behavior for `JSON_EXISTS`.
10771+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10772+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10773+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10774+
pub enum JsonExistsOnErrorBehavior {
10775+
/// `ERROR ON ERROR`
10776+
Error,
10777+
/// `TRUE ON ERROR`
10778+
True,
10779+
/// `FALSE ON ERROR`
10780+
False,
10781+
/// `UNKNOWN ON ERROR`
10782+
Unknown,
10783+
}
10784+
10785+
impl Display for JsonExistsOnErrorBehavior {
10786+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10787+
match self {
10788+
JsonExistsOnErrorBehavior::Error => write!(f, "ERROR ON ERROR"),
10789+
JsonExistsOnErrorBehavior::True => write!(f, "TRUE ON ERROR"),
10790+
JsonExistsOnErrorBehavior::False => write!(f, "FALSE ON ERROR"),
10791+
JsonExistsOnErrorBehavior::Unknown => write!(f, "UNKNOWN ON ERROR"),
10792+
}
10793+
}
10794+
}
10795+
10796+
/// SQL/JSON behavior target for `JSON_VALUE` and `JSON_QUERY`.
10797+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10798+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10799+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10800+
pub enum JsonBehaviorTarget {
10801+
/// `... ON EMPTY`
10802+
Empty,
10803+
/// `... ON ERROR`
10804+
Error,
10805+
}
10806+
10807+
impl Display for JsonBehaviorTarget {
10808+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10809+
match self {
10810+
JsonBehaviorTarget::Empty => write!(f, "EMPTY"),
10811+
JsonBehaviorTarget::Error => write!(f, "ERROR"),
10812+
}
10813+
}
10814+
}
10815+
10816+
/// SQL/JSON behavior variant for `JSON_VALUE`.
10817+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10818+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10819+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10820+
pub enum JsonValueBehavior {
10821+
/// `ERROR`
10822+
Error,
10823+
/// `NULL`
10824+
Null,
10825+
/// `DEFAULT <expr>`
10826+
Default(Expr),
10827+
}
10828+
10829+
impl Display for JsonValueBehavior {
10830+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10831+
match self {
10832+
JsonValueBehavior::Error => write!(f, "ERROR"),
10833+
JsonValueBehavior::Null => write!(f, "NULL"),
10834+
JsonValueBehavior::Default(expr) => write!(f, "DEFAULT {expr}"),
10835+
}
10836+
}
10837+
}
10838+
10839+
/// SQL/JSON behavior clause for `JSON_VALUE`.
10840+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10841+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10842+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10843+
pub struct JsonValueBehaviorClause {
10844+
/// Behavior applied on target condition.
10845+
pub behavior: JsonValueBehavior,
10846+
/// Target condition: `ON EMPTY` or `ON ERROR`.
10847+
pub target: JsonBehaviorTarget,
10848+
}
10849+
10850+
impl Display for JsonValueBehaviorClause {
10851+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10852+
write!(f, "{} ON {}", self.behavior, self.target)
10853+
}
10854+
}
10855+
10856+
/// SQL/JSON wrapper behavior clause for `JSON_QUERY`.
10857+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10858+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10859+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10860+
pub enum JsonQueryWrapperClause {
10861+
/// `WITHOUT WRAPPER`
10862+
WithoutWrapper,
10863+
/// `WITHOUT ARRAY WRAPPER`
10864+
WithoutArrayWrapper,
10865+
/// `WITH WRAPPER`
10866+
WithWrapper,
10867+
/// `WITH ARRAY WRAPPER`
10868+
WithArrayWrapper,
10869+
/// `WITH CONDITIONAL WRAPPER`
10870+
WithConditionalWrapper,
10871+
/// `WITH CONDITIONAL ARRAY WRAPPER`
10872+
WithConditionalArrayWrapper,
10873+
/// `WITH UNCONDITIONAL WRAPPER`
10874+
WithUnconditionalWrapper,
10875+
/// `WITH UNCONDITIONAL ARRAY WRAPPER`
10876+
WithUnconditionalArrayWrapper,
10877+
}
10878+
10879+
impl Display for JsonQueryWrapperClause {
10880+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10881+
match self {
10882+
JsonQueryWrapperClause::WithoutWrapper => write!(f, "WITHOUT WRAPPER"),
10883+
JsonQueryWrapperClause::WithoutArrayWrapper => write!(f, "WITHOUT ARRAY WRAPPER"),
10884+
JsonQueryWrapperClause::WithWrapper => write!(f, "WITH WRAPPER"),
10885+
JsonQueryWrapperClause::WithArrayWrapper => write!(f, "WITH ARRAY WRAPPER"),
10886+
JsonQueryWrapperClause::WithConditionalWrapper => {
10887+
write!(f, "WITH CONDITIONAL WRAPPER")
10888+
}
10889+
JsonQueryWrapperClause::WithConditionalArrayWrapper => {
10890+
write!(f, "WITH CONDITIONAL ARRAY WRAPPER")
10891+
}
10892+
JsonQueryWrapperClause::WithUnconditionalWrapper => {
10893+
write!(f, "WITH UNCONDITIONAL WRAPPER")
10894+
}
10895+
JsonQueryWrapperClause::WithUnconditionalArrayWrapper => {
10896+
write!(f, "WITH UNCONDITIONAL ARRAY WRAPPER")
10897+
}
10898+
}
10899+
}
10900+
}
10901+
10902+
/// SQL/JSON quote handling mode for `JSON_QUERY`.
10903+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10904+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10905+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10906+
pub enum JsonQueryQuotesMode {
10907+
/// `KEEP`
10908+
Keep,
10909+
/// `OMIT`
10910+
Omit,
10911+
}
10912+
10913+
impl Display for JsonQueryQuotesMode {
10914+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10915+
match self {
10916+
JsonQueryQuotesMode::Keep => write!(f, "KEEP"),
10917+
JsonQueryQuotesMode::Omit => write!(f, "OMIT"),
10918+
}
10919+
}
10920+
}
10921+
10922+
/// SQL/JSON quote handling clause for `JSON_QUERY`.
10923+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10924+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10925+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10926+
pub struct JsonQueryQuotesClause {
10927+
/// Whether to keep or omit quotes.
10928+
pub mode: JsonQueryQuotesMode,
10929+
/// Whether `ON SCALAR STRING` was specified.
10930+
pub on_scalar_string: bool,
10931+
}
10932+
10933+
impl Display for JsonQueryQuotesClause {
10934+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10935+
write!(f, "{} QUOTES", self.mode)?;
10936+
if self.on_scalar_string {
10937+
write!(f, " ON SCALAR STRING")?;
10938+
}
10939+
Ok(())
10940+
}
10941+
}
10942+
10943+
/// SQL/JSON behavior variant for `JSON_QUERY`.
10944+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10945+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10946+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10947+
pub enum JsonQueryBehavior {
10948+
/// `ERROR`
10949+
Error,
10950+
/// `NULL`
10951+
Null,
10952+
/// `EMPTY`
10953+
Empty,
10954+
/// `EMPTY ARRAY`
10955+
EmptyArray,
10956+
/// `EMPTY OBJECT`
10957+
EmptyObject,
10958+
/// `DEFAULT <expr>`
10959+
Default(Expr),
10960+
}
10961+
10962+
impl Display for JsonQueryBehavior {
10963+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10964+
match self {
10965+
JsonQueryBehavior::Error => write!(f, "ERROR"),
10966+
JsonQueryBehavior::Null => write!(f, "NULL"),
10967+
JsonQueryBehavior::Empty => write!(f, "EMPTY"),
10968+
JsonQueryBehavior::EmptyArray => write!(f, "EMPTY ARRAY"),
10969+
JsonQueryBehavior::EmptyObject => write!(f, "EMPTY OBJECT"),
10970+
JsonQueryBehavior::Default(expr) => write!(f, "DEFAULT {expr}"),
10971+
}
10972+
}
10973+
}
10974+
10975+
/// SQL/JSON behavior clause for `JSON_QUERY`.
10976+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10977+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10978+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10979+
pub struct JsonQueryBehaviorClause {
10980+
/// Behavior applied on target condition.
10981+
pub behavior: JsonQueryBehavior,
10982+
/// Target condition: `ON EMPTY` or `ON ERROR`.
10983+
pub target: JsonBehaviorTarget,
10984+
}
10985+
10986+
impl Display for JsonQueryBehaviorClause {
10987+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10988+
write!(f, "{} ON {}", self.behavior, self.target)
10989+
}
10990+
}
10991+
10992+
/// SQL/JSON `FORMAT` clause.
10993+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10994+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10995+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10996+
pub struct JsonFormatClause {
10997+
/// Format type.
10998+
pub format: JsonFormatType,
10999+
/// Optional encoding.
11000+
pub encoding: Option<JsonFormatEncoding>,
11001+
}
11002+
11003+
impl Display for JsonFormatClause {
11004+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11005+
write!(f, "FORMAT {}", self.format)?;
11006+
if let Some(encoding) = self.encoding {
11007+
write!(f, " ENCODING {encoding}")?;
11008+
}
11009+
Ok(())
11010+
}
11011+
}
11012+
11013+
/// SQL/JSON format type.
11014+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11015+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11016+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11017+
pub enum JsonFormatType {
11018+
/// `JSON`
11019+
Json,
11020+
}
11021+
11022+
impl Display for JsonFormatType {
11023+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11024+
match self {
11025+
JsonFormatType::Json => write!(f, "JSON"),
11026+
}
11027+
}
11028+
}
11029+
11030+
/// SQL/JSON format encoding.
11031+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11032+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11033+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11034+
pub enum JsonFormatEncoding {
11035+
/// `UTF8`
11036+
Utf8,
11037+
}
11038+
11039+
impl Display for JsonFormatEncoding {
11040+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11041+
match self {
11042+
JsonFormatEncoding::Utf8 => write!(f, "UTF8"),
11043+
}
11044+
}
11045+
}
11046+
1070311047
/// rename object definition
1070411048
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1070511049
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

0 commit comments

Comments
 (0)