-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Support identifiers with . in them
#1449
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
Changes from all commits
9cee7b1
712497f
3d96d82
44f49ad
d35c44a
3e4418c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1062,8 +1062,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| } | ||
|
|
||
| let field = schema.field(field_index - 1); | ||
| let col_ident = SQLExpr::Identifier(Ident::new(field.qualified_name())); | ||
| self.sql_expr_to_logical_expr(&col_ident, schema)? | ||
| Expr::Column(field.qualified_column()) | ||
| } | ||
| e => self.sql_expr_to_logical_expr(e, schema)?, | ||
| }; | ||
|
|
@@ -1323,9 +1322,14 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| let var_names = vec![id.value.clone()]; | ||
| Ok(Expr::ScalarVariable(var_names)) | ||
| } else { | ||
| // create a column expression based on raw user input, this column will be | ||
| // normalized with qualifer later by the SQL planner. | ||
| Ok(col(&id.value)) | ||
| // Don't use `col()` here because it will try to | ||
| // interpret names with '.' as if they were | ||
| // compound indenfiers, but this is not a compound | ||
| // identifier. (e.g. it is "foo.bar" not foo.bar) | ||
| Ok(Expr::Column(Column { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the core change -- |
||
| relation: None, | ||
| name: id.value.clone(), | ||
| })) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5426,6 +5426,86 @@ async fn qualified_table_references() -> Result<()> { | |
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn qualified_table_references_and_fields() -> Result<()> { | ||
| let mut ctx = ExecutionContext::new(); | ||
|
|
||
| let c1: StringArray = vec!["foofoo", "foobar", "foobaz"] | ||
| .into_iter() | ||
| .map(Some) | ||
| .collect(); | ||
| let c2: Int64Array = vec![1, 2, 3].into_iter().map(Some).collect(); | ||
| let c3: Int64Array = vec![10, 20, 30].into_iter().map(Some).collect(); | ||
|
|
||
| let batch = RecordBatch::try_from_iter(vec![ | ||
| ("f.c1", Arc::new(c1) as ArrayRef), | ||
| // evil -- use the same name as the table | ||
| ("test.c2", Arc::new(c2) as ArrayRef), | ||
| // more evil still | ||
| ("....", Arc::new(c3) as ArrayRef), | ||
| ])?; | ||
|
|
||
| let table = MemTable::try_new(batch.schema(), vec![vec![batch]])?; | ||
| ctx.register_table("test", Arc::new(table))?; | ||
|
|
||
| // referring to the unquoted column is an error | ||
| let sql = r#"SELECT f1.c1 from test"#; | ||
| let error = ctx.create_logical_plan(sql).unwrap_err(); | ||
| assert_contains!( | ||
| error.to_string(), | ||
| "No field named 'f1.c1'. Valid fields are 'test.f.c1', 'test.test.c2'" | ||
| ); | ||
|
|
||
| // however, enclosing it in double quotes is ok | ||
| let sql = r#"SELECT "f.c1" from test"#; | ||
| let actual = execute_to_batches(&mut ctx, sql).await; | ||
| let expected = vec![ | ||
| "+--------+", | ||
| "| f.c1 |", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i wonder if it is okay to create column
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is "ok" from the point of view of DataFusion should run the query. I don't think anyone should actually do it 😆 Added a test for this case in 3e4418c |
||
| "+--------+", | ||
| "| foofoo |", | ||
| "| foobar |", | ||
| "| foobaz |", | ||
| "+--------+", | ||
| ]; | ||
| assert_batches_eq!(expected, &actual); | ||
| // Works fully qualified too | ||
| let sql = r#"SELECT test."f.c1" from test"#; | ||
| let actual = execute_to_batches(&mut ctx, sql).await; | ||
| assert_batches_eq!(expected, &actual); | ||
|
|
||
| // check that duplicated table name and column name are ok | ||
| let sql = r#"SELECT "test.c2" as expr1, test."test.c2" as expr2 from test"#; | ||
| let actual = execute_to_batches(&mut ctx, sql).await; | ||
| let expected = vec![ | ||
| "+-------+-------+", | ||
| "| expr1 | expr2 |", | ||
| "+-------+-------+", | ||
| "| 1 | 1 |", | ||
| "| 2 | 2 |", | ||
| "| 3 | 3 |", | ||
| "+-------+-------+", | ||
| ]; | ||
| assert_batches_eq!(expected, &actual); | ||
|
|
||
| // check that '....' is also an ok column name (in the sense that | ||
| // datafusion should run the query, not that someone should write | ||
| // this | ||
| let sql = r#"SELECT "....", "...." as c3 from test order by "....""#; | ||
| let actual = execute_to_batches(&mut ctx, sql).await; | ||
| let expected = vec![ | ||
| "+------+----+", | ||
| "| .... | c3 |", | ||
| "+------+----+", | ||
| "| 10 | 10 |", | ||
| "| 20 | 20 |", | ||
| "| 30 | 30 |", | ||
| "+------+----+", | ||
| ]; | ||
| assert_batches_eq!(expected, &actual); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn invalid_qualified_table_references() -> Result<()> { | ||
| let mut ctx = ExecutionContext::new(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change avoids trying to reparse the string as a column name, and instead just creates the
Columnreference directlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍, FYI @hntd187
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is much better than what I had :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are all learning together!