Enable map access for numbers, multiple nesting levels (#356)

* enable integer keys for map access

* enable map access for number keys

* Add tests for string based map access

* MapAccess: unbox single quoted strings to always display double quoted strings for map access

* cargo fmt

* cargo clippy

* Fix compilation with nostd by avoiding format!

* fix codestyle

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
Guillaume Balaine 2021-09-24 20:22:12 +02:00 committed by GitHub
parent 014b82f03d
commit d498887a5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 5 deletions

View file

@ -18,6 +18,9 @@
mod test_utils;
use test_utils::*;
#[cfg(feature = "bigdecimal")]
use bigdecimal::BigDecimal;
use sqlparser::ast::Expr::{Identifier, MapAccess};
use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
use sqlparser::parser::ParserError;
@ -669,6 +672,57 @@ fn parse_pg_regex_match_ops() {
}
}
#[test]
fn parse_map_access_expr() {
#[cfg(not(feature = "bigdecimal"))]
let zero = "0".to_string();
#[cfg(feature = "bigdecimal")]
let zero = BigDecimal::parse_bytes(b"0", 10).unwrap();
let sql = "SELECT foo[0] FROM foos";
let select = pg_and_generic().verified_only_select(sql);
assert_eq!(
&MapAccess {
column: Box::new(Identifier(Ident {
value: "foo".to_string(),
quote_style: None
})),
keys: vec![Value::Number(zero.clone(), false)]
},
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!(
&MapAccess {
column: Box::new(Identifier(Ident {
value: "foo".to_string(),
quote_style: None
})),
keys: vec![
Value::Number(zero.clone(), false),
Value::Number(zero.clone(), false)
]
},
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!(
&MapAccess {
column: Box::new(Identifier(Ident {
value: "bar".to_string(),
quote_style: None
})),
keys: vec![
Value::Number(zero, false),
Value::SingleQuotedString("baz".to_string()),
Value::SingleQuotedString("fooz".to_string())
]
},
expr_from_projection(only(&select.projection)),
);
}
fn pg() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(PostgreSqlDialect {})],