Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,17 @@ pub enum Expr {
IsDistinctFrom(Box<Expr>, Box<Expr>),
/// `IS NOT DISTINCT FROM` operator
IsNotDistinctFrom(Box<Expr>, Box<Expr>),
/// `<expr> IS [NOT] JSON [VALUE|SCALAR|ARRAY|OBJECT] [WITH|WITHOUT UNIQUE [KEYS]]`
IsJson {
/// Expression being tested.
expr: Box<Expr>,
/// Optional JSON shape constraint.
kind: Option<JsonPredicateType>,
/// Optional duplicate-key handling constraint for JSON objects.
unique_keys: Option<JsonKeyUniqueness>,
/// `true` when `NOT` is present.
negated: bool,
},
/// `<expr> IS [ NOT ] [ form ] NORMALIZED`
IsNormalized {
/// Expression being tested.
Expand Down Expand Up @@ -1685,6 +1696,25 @@ impl fmt::Display for Expr {
Expr::IsNotNull(ast) => write!(f, "{ast} IS NOT NULL"),
Expr::IsUnknown(ast) => write!(f, "{ast} IS UNKNOWN"),
Expr::IsNotUnknown(ast) => write!(f, "{ast} IS NOT UNKNOWN"),
Expr::IsJson {
expr,
kind,
unique_keys,
negated,
} => {
write!(f, "{expr} IS ")?;
if *negated {
write!(f, "NOT ")?;
}
write!(f, "JSON")?;
if let Some(kind) = kind {
write!(f, " {kind}")?;
}
if let Some(unique_keys) = unique_keys {
write!(f, " {unique_keys}")?;
}
Ok(())
}
Expr::InList {
expr,
list,
Expand Down Expand Up @@ -8107,6 +8137,52 @@ pub enum AnalyzeFormat {
TREE,
}

/// Optional type constraint for `IS JSON`.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum JsonPredicateType {
/// `VALUE` form.
Value,
/// `SCALAR` form.
Scalar,
/// `ARRAY` form.
Array,
/// `OBJECT` form.
Object,
}

impl fmt::Display for JsonPredicateType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
JsonPredicateType::Value => write!(f, "VALUE"),
JsonPredicateType::Scalar => write!(f, "SCALAR"),
JsonPredicateType::Array => write!(f, "ARRAY"),
JsonPredicateType::Object => write!(f, "OBJECT"),
}
}
}

/// Optional duplicate-key handling for `IS JSON`.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum JsonKeyUniqueness {
/// `WITH UNIQUE KEYS` form.
WithUniqueKeys,
/// `WITHOUT UNIQUE KEYS` form.
WithoutUniqueKeys,
}

impl fmt::Display for JsonKeyUniqueness {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
JsonKeyUniqueness::WithUniqueKeys => write!(f, "WITH UNIQUE KEYS"),
JsonKeyUniqueness::WithoutUniqueKeys => write!(f, "WITHOUT UNIQUE KEYS"),
}
}
}

impl fmt::Display for AnalyzeFormat {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
Expand Down
6 changes: 6 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,12 @@ impl Spanned for Expr {
Expr::IsNotNull(expr) => expr.span(),
Expr::IsUnknown(expr) => expr.span(),
Expr::IsNotUnknown(expr) => expr.span(),
Expr::IsJson {
expr,
kind: _,
unique_keys: _,
negated: _,
} => expr.span(),
Expr::IsDistinctFrom(lhs, rhs) => lhs.span().union(&rhs.span()),
Expr::IsNotDistinctFrom(lhs, rhs) => lhs.span().union(&rhs.span()),
Expr::InList {
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ impl Dialect for AnsiDialect {
fn supports_nested_comments(&self) -> bool {
true
}

fn supports_is_json_predicate(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ impl Dialect for GenericDialect {
true
}

fn supports_is_json_predicate(&self) -> bool {
true
}

fn supports_comma_separated_trim(&self) -> bool {
true
}
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,11 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports the `IS [NOT] JSON` predicate.
fn supports_is_json_predicate(&self) -> bool {
false
}

/// Returns true if this dialect allows an optional `SIGNED` suffix after integer data types.
///
/// Example:
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,8 @@ impl Dialect for OracleDialect {
fn supports_insert_table_alias(&self) -> bool {
true
}

fn supports_is_json_predicate(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ impl Dialect for PostgreSqlDialect {
true
}

fn supports_is_json_predicate(&self) -> bool {
true
}

fn supports_comma_separated_trim(&self) -> bool {
true
}
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ define_keywords!(
SAFE_CAST,
SAMPLE,
SAVEPOINT,
SCALAR,
SCHEMA,
SCHEMAS,
SCOPE,
Expand Down
Loading