Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Postgres array slice syntax #1290

Merged
merged 5 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
68 changes: 57 additions & 11 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ pub enum Expr {
},
/// Access a map-like object by field (e.g. `column['field']` or `column[4]`
/// Note that depending on the dialect, struct like accesses may be
/// parsed as [`ArrayIndex`](Self::ArrayIndex) or [`MapAccess`](Self::MapAccess)
/// parsed as [`Subscript`](Self::Subscript) or [`MapAccess`](Self::MapAccess)
/// <https://clickhouse.com/docs/en/sql-reference/data-types/map/>
MapAccess {
column: Box<Expr>,
Expand Down Expand Up @@ -745,10 +745,10 @@ pub enum Expr {
/// ```
/// [1]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
Dictionary(Vec<DictionaryField>),
/// An array index expression e.g. `(ARRAY[1, 2])[1]` or `(current_schemas(FALSE))[1]`
ArrayIndex {
obj: Box<Expr>,
indexes: Vec<Expr>,
/// An access of nested data using subscript syntax, for example `array[2]`.
Subscript {
expr: Box<Expr>,
subscript: Box<Subscript>,
},
Comment on lines +748 to 752
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the last comment, datafusion has a method:

    fn plan_indices(
        &self,
        expr: SQLExpr,
        schema: &DFSchema,
        planner_context: &mut PlannerContext,
    ) -> Result<GetFieldAccess> {

where takes only the subscript part. And:

    fn plan_indexed(
        &self,
        expr: Expr,
        mut keys: Vec<SQLExpr>,
        schema: &DFSchema,
        planner_context: &mut PlannerContext,
    ) -> Result<Expr> {

where the keys is the subscript part (vec![subscript]). The logic is shared with MapAccess so I don't think it's easy to refactor.

Copy link
Member

@tisonkun tisonkun May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may still have:

    ArrayIndex {
        obj: Box<Expr>,
        indexes: Vec<Expr>,
    },

with a new variant:

Subscript { ... } // [1] or [1:2] or [1:2:3]

that becomes the indexes part.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an idea how to fix this -- working on it

/// An array expression e.g. `ARRAY[1, 2]`
Array(Array),
Expand Down Expand Up @@ -804,6 +804,53 @@ pub enum Expr {
Lambda(LambdaFunction),
}

/// The contents inside the `[` and `]` in a subscript expression.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum Subscript {
/// Accesses the element of the array at the given index.
Index { index: Expr },

/// Accesses a slice of an array on PostgreSQL, e.g.
///
/// ```plaintext
/// => select (array[1,2,3,4,5,6])[2:5];
/// -----------
/// {2,3,4,5}
/// ```
///
/// The lower and/or upper bound can be omitted to slice from the start or
/// end of the array respectively.
///
/// See <https://www.postgresql.org/docs/current/arrays.html#ARRAYS-ACCESSING>.
Slice {
lower_bound: Option<Expr>,
upper_bound: Option<Expr>,
},
}

impl fmt::Display for Subscript {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Subscript::Index { index } => write!(f, "{index}"),
Subscript::Slice {
lower_bound,
upper_bound,
} => {
if let Some(lower) = lower_bound {
write!(f, "{lower}")?;
}
write!(f, ":")?;
if let Some(upper) = upper_bound {
write!(f, "{upper}")?;
}
Ok(())
}
}
}
}

/// A lambda function.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -1250,12 +1297,11 @@ impl fmt::Display for Expr {
Expr::Dictionary(fields) => {
write!(f, "{{{}}}", display_comma_separated(fields))
}
Expr::ArrayIndex { obj, indexes } => {
write!(f, "{obj}")?;
for i in indexes {
write!(f, "[{i}]")?;
}
Ok(())
Expr::Subscript {
expr,
subscript: key,
} => {
write!(f, "{expr}[{key}]")
}
Expr::Array(set) => {
write!(f, "{set}")
Expand Down
43 changes: 29 additions & 14 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2582,8 +2582,7 @@ impl<'a> Parser<'a> {
})
} else if Token::LBracket == tok {
if dialect_of!(self is PostgreSqlDialect | DuckDbDialect | GenericDialect) {
// parse index
self.parse_array_index(expr)
self.parse_subscript(expr)
} else if dialect_of!(self is SnowflakeDialect) {
self.prev_token();
self.parse_json_access(expr)
Expand Down Expand Up @@ -2611,18 +2610,34 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_array_index(&mut self, expr: Expr) -> Result<Expr, ParserError> {
let index = self.parse_expr()?;
pub fn parse_subscript(&mut self, expr: Expr) -> Result<Expr, ParserError> {
let parse_upper_bound = |p: &mut Parser<'a>| {
if let Token::RBracket = p.peek_token().token {
Ok(None)
} else {
p.parse_expr().map(Some)
}
};
let subscript = if self.consume_token(&Token::Colon) {
Subscript::Slice {
lower_bound: None,
upper_bound: parse_upper_bound(self)?,
}
} else {
let expr = self.parse_expr()?;
if self.consume_token(&Token::Colon) {
Subscript::Slice {
lower_bound: Some(expr),
upper_bound: parse_upper_bound(self)?,
}
} else {
Subscript::Index { index: expr }
}
};
self.expect_token(&Token::RBracket)?;
let mut indexes: Vec<Expr> = vec![index];
while self.consume_token(&Token::LBracket) {
let index = self.parse_expr()?;
self.expect_token(&Token::RBracket)?;
indexes.push(index);
}
Ok(Expr::ArrayIndex {
obj: Box::new(expr),
indexes,
Ok(Expr::Subscript {
expr: Box::new(expr),
subscript: Box::new(subscript),
})
}

Expand Down Expand Up @@ -2872,7 +2887,7 @@ impl<'a> Parser<'a> {
Ok(Self::MUL_DIV_MOD_OP_PREC)
}
Token::DoubleColon => Ok(50),
Token::Colon => Ok(50),
Token::Colon if dialect_of!(self is SnowflakeDialect) => Ok(50),
Token::ExclamationMark => Ok(50),
Token::LBracket | Token::Overlap | Token::CaretAt => Ok(50),
Token::Arrow
Expand Down
8 changes: 5 additions & 3 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,16 +528,18 @@ fn test_array_index() {
_ => panic!("Expected an expression with alias"),
};
assert_eq!(
&Expr::ArrayIndex {
obj: Box::new(Expr::Array(Array {
&Expr::Subscript {
expr: Box::new(Expr::Array(Array {
elem: vec![
Expr::Value(Value::SingleQuotedString("a".to_owned())),
Expr::Value(Value::SingleQuotedString("b".to_owned())),
Expr::Value(Value::SingleQuotedString("c".to_owned()))
],
named: false
})),
indexes: vec![Expr::Value(number("3"))]
subscript: Box::new(Subscript::Index {
index: Expr::Value(number("3"))
})
},
expr
);
Expand Down
159 changes: 126 additions & 33 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1873,66 +1873,90 @@ fn parse_array_index_expr() {
let sql = "SELECT foo[0] FROM foos";
let select = pg_and_generic().verified_only_select(sql);
assert_eq!(
&Expr::ArrayIndex {
obj: Box::new(Expr::Identifier(Ident::new("foo"))),
indexes: vec![num[0].clone()],
&Expr::Subscript {
expr: Box::new(Expr::Identifier(Ident::new("foo"))),
subscript: Box::new(Subscript::Index {
index: num[0].clone()
}),
},
expr_from_projection(only(&select.projection)),
);

let sql = "SELECT foo[0][0] FROM foos";
let select = pg_and_generic().verified_only_select(sql);
assert_eq!(
&Expr::ArrayIndex {
obj: Box::new(Expr::Identifier(Ident::new("foo"))),
indexes: vec![num[0].clone(), num[0].clone()],
&Expr::Subscript {
expr: Box::new(Expr::Subscript {
expr: Box::new(Expr::Identifier(Ident::new("foo"))),
subscript: Box::new(Subscript::Index {
index: num[0].clone()
}),
}),
subscript: Box::new(Subscript::Index {
index: num[0].clone()
}),
},
expr_from_projection(only(&select.projection)),
);

let sql = r#"SELECT bar[0]["baz"]["fooz"] FROM foos"#;
let select = pg_and_generic().verified_only_select(sql);
assert_eq!(
&Expr::ArrayIndex {
obj: Box::new(Expr::Identifier(Ident::new("bar"))),
indexes: vec![
num[0].clone(),
Expr::Identifier(Ident {
value: "baz".to_string(),
quote_style: Some('"')
&Expr::Subscript {
expr: Box::new(Expr::Subscript {
expr: Box::new(Expr::Subscript {
expr: Box::new(Expr::Identifier(Ident::new("bar"))),
subscript: Box::new(Subscript::Index {
index: num[0].clone()
})
}),
Expr::Identifier(Ident {
subscript: Box::new(Subscript::Index {
index: Expr::Identifier(Ident {
value: "baz".to_string(),
quote_style: Some('"')
})
})
}),
subscript: Box::new(Subscript::Index {
index: Expr::Identifier(Ident {
value: "fooz".to_string(),
quote_style: Some('"')
})
],
})
},
expr_from_projection(only(&select.projection)),
);

let sql = "SELECT (CAST(ARRAY[ARRAY[2, 3]] AS INT[][]))[1][2]";
let select = pg_and_generic().verified_only_select(sql);
assert_eq!(
&Expr::ArrayIndex {
obj: Box::new(Expr::Nested(Box::new(Expr::Cast {
kind: CastKind::Cast,
expr: Box::new(Expr::Array(Array {
elem: vec![Expr::Array(Array {
elem: vec![num[2].clone(), num[3].clone(),],
&Expr::Subscript {
expr: Box::new(Expr::Subscript {
expr: Box::new(Expr::Nested(Box::new(Expr::Cast {
kind: CastKind::Cast,
expr: Box::new(Expr::Array(Array {
elem: vec![Expr::Array(Array {
elem: vec![num[2].clone(), num[3].clone(),],
named: true,
})],
named: true,
})],
named: true,
})),
data_type: DataType::Array(ArrayElemTypeDef::SquareBracket(
Box::new(DataType::Array(ArrayElemTypeDef::SquareBracket(
Box::new(DataType::Int(None)),
})),
data_type: DataType::Array(ArrayElemTypeDef::SquareBracket(
Box::new(DataType::Array(ArrayElemTypeDef::SquareBracket(
Box::new(DataType::Int(None)),
None
))),
None
))),
None
)),
format: None,
}))),
indexes: vec![num[1].clone(), num[2].clone()],
)),
format: None,
}))),
subscript: Box::new(Subscript::Index {
index: num[1].clone()
}),
}),
subscript: Box::new(Subscript::Index {
index: num[2].clone()
}),
},
expr_from_projection(only(&select.projection)),
);
Expand All @@ -1948,6 +1972,75 @@ fn parse_array_index_expr() {
);
}

#[test]
fn parse_array_subscript() {
let tests = [
(
"(ARRAY[1, 2, 3, 4, 5, 6])[2]",
Subscript::Index {
index: Expr::Value(number("2")),
},
),
(
"(ARRAY[1, 2, 3, 4, 5, 6])[foo]",
Subscript::Index {
index: Expr::Identifier(Ident::new("foo")),
},
),
(
"(ARRAY[1, 2, 3, 4, 5, 6])[2:5]",
Subscript::Slice {
lower_bound: Some(Expr::Value(number("2"))),
upper_bound: Some(Expr::Value(number("5"))),
},
),
(
"arr[array_length(arr) - 3:array_length(arr) - 1]",
Subscript::Slice {
lower_bound: Some(Expr::BinaryOp {
left: Box::new(call("array_length", [Expr::Identifier(Ident::new("arr"))])),
op: BinaryOperator::Minus,
right: Box::new(Expr::Value(number("3"))),
}),
upper_bound: Some(Expr::BinaryOp {
left: Box::new(call("array_length", [Expr::Identifier(Ident::new("arr"))])),
op: BinaryOperator::Minus,
right: Box::new(Expr::Value(number("1"))),
}),
},
),
(
"(ARRAY[1, 2, 3, 4, 5, 6])[:5]",
Subscript::Slice {
lower_bound: None,
upper_bound: Some(Expr::Value(number("5"))),
},
),
(
"(ARRAY[1, 2, 3, 4, 5, 6])[2:]",
Subscript::Slice {
lower_bound: Some(Expr::Value(number("2"))),
upper_bound: None,
},
),
(
"(ARRAY[1, 2, 3, 4, 5, 6])[:]",
Subscript::Slice {
lower_bound: None,
upper_bound: None,
},
),
];
for (sql, expect) in tests {
let Expr::Subscript { subscript, .. } = pg_and_generic().verified_expr(sql) else {
panic!("expected subscript expr");
};
assert_eq!(expect, *subscript);
}

pg_and_generic().verified_expr("schedule[:2][2:]");
}

#[test]
fn parse_create_index() {
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2)";
Expand Down
Loading
Loading