Skip to content

Commit d1ef34a

Browse files
committed
Add support for PartiQL queries in Redshift
1 parent 988cdbe commit d1ef34a

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

src/parser/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3087,7 +3087,7 @@ impl<'a> Parser<'a> {
30873087
}
30883088

30893089
debug_assert!(!path.is_empty());
3090-
Ok(JsonPath{ path })
3090+
Ok(JsonPath { path })
30913091
}
30923092

30933093
pub fn parse_map_access(&mut self, expr: Expr) -> Result<Expr, ParserError> {
@@ -10311,12 +10311,12 @@ impl<'a> Parser<'a> {
1031110311
self.parse_open_json_table_factor()
1031210312
} else {
1031310313
let name = self.parse_object_name(true)?;
10314-
10314+
1031510315
let partiql = match self.peek_token().token {
1031610316
Token::LBracket if self.dialect.supports_partiql() => Some(self.parse_json_path()?),
10317-
_ => None
10317+
_ => None,
1031810318
};
10319-
10319+
1032010320
let partitions: Vec<Ident> = if dialect_of!(self is MySqlDialect | GenericDialect)
1032110321
&& self.parse_keyword(Keyword::PARTITION)
1032210322
{

tests/sqlparser_redshift.rs

+45-19
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,20 @@ fn test_redshift_json_path() {
206206
let select = redshift().verified_only_select(sql);
207207

208208
assert_eq!(
209-
&Expr::JsonAccess{
209+
&Expr::JsonAccess {
210210
value: Box::new(Expr::CompoundIdentifier(vec![
211211
Ident::new("cust"),
212212
Ident::new("c_orders")
213213
])),
214-
path: JsonPath{
214+
path: JsonPath {
215215
path: vec![
216-
JsonPathElem::Bracket{key: Expr::Value(Value::Number("0".to_string(), false))},
217-
JsonPathElem::Dot{key: "o_orderkey".to_string(), quoted: false}
216+
JsonPathElem::Bracket {
217+
key: Expr::Value(Value::Number("0".parse().unwrap(), false))
218+
},
219+
JsonPathElem::Dot {
220+
key: "o_orderkey".to_string(),
221+
quoted: false
222+
}
218223
]
219224
}
220225
},
@@ -224,15 +229,19 @@ fn test_redshift_json_path() {
224229
let sql = "SELECT cust.c_orders[0]['id'] FROM customer_orders_lineitem";
225230
let select = redshift().verified_only_select(sql);
226231
assert_eq!(
227-
&Expr::JsonAccess{
232+
&Expr::JsonAccess {
228233
value: Box::new(Expr::CompoundIdentifier(vec![
229234
Ident::new("cust"),
230235
Ident::new("c_orders")
231236
])),
232-
path: JsonPath{
237+
path: JsonPath {
233238
path: vec![
234-
JsonPathElem::Bracket{key: Expr::Value(Value::Number("0".to_string(), false))},
235-
JsonPathElem::Bracket{key: Expr::Value(Value::SingleQuotedString("id".to_owned()))}
239+
JsonPathElem::Bracket {
240+
key: Expr::Value(Value::Number("0".parse().unwrap(), false))
241+
},
242+
JsonPathElem::Bracket {
243+
key: Expr::Value(Value::SingleQuotedString("id".to_owned()))
244+
}
236245
]
237246
}
238247
},
@@ -248,10 +257,15 @@ fn test_parse_json_path_from() {
248257
assert_eq!(name, &ObjectName(vec![Ident::new("src")]));
249258
assert_eq!(
250259
partiql,
251-
&Some(JsonPath{
260+
&Some(JsonPath {
252261
path: vec![
253-
JsonPathElem::Bracket{key: Expr::Value(Value::Number("0".to_string(), false))},
254-
JsonPathElem::Dot{key: "a".to_string(), quoted: false}
262+
JsonPathElem::Bracket {
263+
key: Expr::Value(Value::Number("0".parse().unwrap(), false))
264+
},
265+
JsonPathElem::Dot {
266+
key: "a".to_string(),
267+
quoted: false
268+
}
255269
]
256270
})
257271
);
@@ -265,12 +279,22 @@ fn test_parse_json_path_from() {
265279
assert_eq!(name, &ObjectName(vec![Ident::new("src")]));
266280
assert_eq!(
267281
partiql,
268-
&Some(JsonPath{
282+
&Some(JsonPath {
269283
path: vec![
270-
JsonPathElem::Bracket{key: Expr::Value(Value::Number("0".to_string(), false))},
271-
JsonPathElem::Dot{key: "a".to_string(), quoted: false},
272-
JsonPathElem::Bracket{key: Expr::Value(Value::Number("1".to_string(), false))},
273-
JsonPathElem::Dot{key: "b".to_string(), quoted: false},
284+
JsonPathElem::Bracket {
285+
key: Expr::Value(Value::Number("0".parse().unwrap(), false))
286+
},
287+
JsonPathElem::Dot {
288+
key: "a".to_string(),
289+
quoted: false
290+
},
291+
JsonPathElem::Bracket {
292+
key: Expr::Value(Value::Number("1".parse().unwrap(), false))
293+
},
294+
JsonPathElem::Dot {
295+
key: "b".to_string(),
296+
quoted: false
297+
},
274298
]
275299
})
276300
);
@@ -281,10 +305,12 @@ fn test_parse_json_path_from() {
281305
let select = redshift().verified_only_select("SELECT * FROM src.a.b");
282306
match &select.from[0].relation {
283307
TableFactor::Table { name, partiql, .. } => {
284-
assert_eq!(name, &ObjectName(vec![Ident::new("src"), Ident::new("a"), Ident::new("b")]));
308+
assert_eq!(
309+
name,
310+
&ObjectName(vec![Ident::new("src"), Ident::new("a"), Ident::new("b")])
311+
);
285312
assert_eq!(partiql, &None);
286313
}
287314
_ => panic!(),
288315
}
289-
290-
}
316+
}

0 commit comments

Comments
 (0)