Skip to content

Commit 497a3b0

Browse files
authored
Fixed the bug that the access parsing is wrong when multiple query conditions contain arrays or map. (#433)
1 parent 6b55e51 commit 497a3b0

File tree

2 files changed

+71
-21
lines changed

2 files changed

+71
-21
lines changed

src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ impl<'a> Parser<'a> {
12851285
Token::Mul | Token::Div | Token::Mod | Token::StringConcat => Ok(40),
12861286
Token::DoubleColon => Ok(50),
12871287
Token::ExclamationMark => Ok(50),
1288-
Token::LBracket => Ok(10),
1288+
Token::LBracket => Ok(50),
12891289
_ => Ok(0),
12901290
}
12911291
}

tests/sqpparser_clickhouse.rs

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,86 @@ mod test_utils;
1818

1919
use test_utils::*;
2020

21-
use sqlparser::ast::Expr::{Identifier, MapAccess};
21+
use sqlparser::ast::Expr::{BinaryOp, Identifier, MapAccess};
22+
use sqlparser::ast::Ident;
23+
use sqlparser::ast::SelectItem::UnnamedExpr;
24+
use sqlparser::ast::TableFactor::Table;
2225
use sqlparser::ast::*;
2326

2427
use sqlparser::dialect::ClickHouseDialect;
2528

2629
#[test]
2730
fn parse_map_access_expr() {
28-
let sql = r#"SELECT string_values[indexOf(string_names, 'endpoint')] FROM foos"#;
31+
let sql = r#"SELECT string_values[indexOf(string_names, 'endpoint')] FROM foos WHERE id = 'test' AND string_value[indexOf(string_name, 'app')] <> 'foo'"#;
2932
let select = clickhouse().verified_only_select(sql);
3033
assert_eq!(
31-
&MapAccess {
32-
column: Box::new(Identifier(Ident {
33-
value: "string_values".to_string(),
34-
quote_style: None,
35-
})),
36-
keys: vec![Expr::Function(Function {
37-
name: ObjectName(vec!["indexOf".into()]),
38-
args: vec![
39-
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Identifier(Ident::new(
40-
"string_names"
41-
)))),
42-
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(
43-
Value::SingleQuotedString("endpoint".to_string())
44-
))),
45-
],
46-
over: None,
47-
distinct: false,
34+
Select {
35+
distinct: false,
36+
top: None,
37+
projection: vec![UnnamedExpr(MapAccess {
38+
column: Box::new(Identifier(Ident {
39+
value: "string_values".to_string(),
40+
quote_style: None,
41+
})),
42+
keys: vec![Expr::Function(Function {
43+
name: ObjectName(vec!["indexOf".into()]),
44+
args: vec![
45+
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Identifier(Ident::new(
46+
"string_names"
47+
)))),
48+
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(
49+
Value::SingleQuotedString("endpoint".to_string())
50+
))),
51+
],
52+
over: None,
53+
distinct: false,
54+
})],
4855
})],
56+
from: vec![TableWithJoins {
57+
relation: Table {
58+
name: ObjectName(vec![Ident::new("foos")]),
59+
alias: None,
60+
args: vec![],
61+
with_hints: vec![],
62+
},
63+
joins: vec![]
64+
}],
65+
lateral_views: vec![],
66+
selection: Some(BinaryOp {
67+
left: Box::new(BinaryOp {
68+
left: Box::new(Identifier(Ident::new("id"))),
69+
op: BinaryOperator::Eq,
70+
right: Box::new(Expr::Value(Value::SingleQuotedString("test".to_string())))
71+
}),
72+
op: BinaryOperator::And,
73+
right: Box::new(BinaryOp {
74+
left: Box::new(MapAccess {
75+
column: Box::new(Identifier(Ident::new("string_value"))),
76+
keys: vec![Expr::Function(Function {
77+
name: ObjectName(vec![Ident::new("indexOf")]),
78+
args: vec![
79+
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Identifier(
80+
Ident::new("string_name")
81+
))),
82+
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(
83+
Value::SingleQuotedString("app".to_string())
84+
))),
85+
],
86+
over: None,
87+
distinct: false
88+
})]
89+
}),
90+
op: BinaryOperator::NotEq,
91+
right: Box::new(Expr::Value(Value::SingleQuotedString("foo".to_string())))
92+
})
93+
}),
94+
group_by: vec![],
95+
cluster_by: vec![],
96+
distribute_by: vec![],
97+
sort_by: vec![],
98+
having: None
4999
},
50-
expr_from_projection(only(&select.projection)),
100+
select
51101
);
52102
}
53103

0 commit comments

Comments
 (0)