Skip to content

Commit e6e3a8b

Browse files
committed
fix: reserved keywords in qualified column names
See: - apache#14141 - apache/datafusion-sqlparser-rs#1909
1 parent fcbde2f commit e6e3a8b

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

datafusion/sql/src/expr/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ use datafusion_expr::planner::{
2121
};
2222
use sqlparser::ast::{
2323
AccessExpr, BinaryOperator, CastFormat, CastKind, DataType as SQLDataType,
24-
DictionaryField, Expr as SQLExpr, ExprWithAlias as SQLExprWithAlias, MapEntry,
25-
StructField, Subscript, TrimWhereField, Value, ValueWithSpan,
24+
DictionaryField, Expr as SQLExpr, ExprWithAlias as SQLExprWithAlias,
25+
FunctionArguments, MapEntry, StructField, Subscript, TrimWhereField, Value,
26+
ValueWithSpan,
2627
};
2728

2829
use datafusion_common::{
@@ -476,7 +477,21 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
476477
),
477478

478479
SQLExpr::Function(function) => {
479-
self.sql_function_to_expr(function, schema, planner_context)
480+
// workaround for https://github.com/apache/datafusion-sqlparser-rs/issues/1909
481+
if matches!(function.args, FunctionArguments::None)
482+
&& function.name.0.len() > 1
483+
&& function.name.0.iter().all(|part| part.as_ident().is_some())
484+
{
485+
let ids = function
486+
.name
487+
.0
488+
.iter()
489+
.map(|part| part.as_ident().expect("just checked").clone())
490+
.collect();
491+
self.sql_compound_identifier_to_expr(ids, schema, planner_context)
492+
} else {
493+
self.sql_function_to_expr(function, schema, planner_context)
494+
}
480495
}
481496

482497
SQLExpr::Rollup(exprs) => {

datafusion/sqllogictest/test_files/select.slt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ VALUES (1,2,3,4,5,6,7,8,9,10,11,12,13,NULL,'F',3.5)
408408

409409
# Test non-literal expressions in VALUES
410410
query II
411-
VALUES (1, CASE WHEN RANDOM() > 0.5 THEN 1 ELSE 1 END),
411+
VALUES (1, CASE WHEN RANDOM() > 0.5 THEN 1 ELSE 1 END),
412412
(2, CASE WHEN RANDOM() > 0.5 THEN 2 ELSE 2 END);
413413
----
414414
1 1
@@ -558,7 +558,7 @@ EXPLAIN SELECT * FROM ((SELECT column1 FROM foo) "T1" CROSS JOIN (SELECT column2
558558
----
559559
logical_plan
560560
01)SubqueryAlias: F
561-
02)--Cross Join:
561+
02)--Cross Join:
562562
03)----SubqueryAlias: T1
563563
04)------TableScan: foo projection=[column1]
564564
05)----SubqueryAlias: T2
@@ -1641,7 +1641,7 @@ query II
16411641
SELECT
16421642
CASE WHEN B.x > 0 THEN A.x / B.x ELSE 0 END AS value1,
16431643
CASE WHEN B.x > 0 AND B.y > 0 THEN A.x / B.x ELSE 0 END AS value3
1644-
FROM t AS A, (SELECT * FROM t WHERE x = 0) AS B;
1644+
FROM t AS A, (SELECT * FROM t WHERE x = 0) AS B;
16451645
----
16461646
0 0
16471647
0 0
@@ -1871,3 +1871,14 @@ select *, count(*) over() as ta from t;
18711871

18721872
statement count 0
18731873
drop table t;
1874+
1875+
# test "user" column
1876+
# See https://github.com/apache/datafusion/issues/14141
1877+
statement count 0
1878+
create table t_with_user(a int, user text) as values (1,'test'), (2,null);
1879+
1880+
query T
1881+
select t_with_user.user from t_with_user;
1882+
----
1883+
test
1884+
NULL

0 commit comments

Comments
 (0)