Skip to content

Commit f00ef9d

Browse files
authored
Fix: allow arbitrary exprs in VALUES clause (#5813)
* Fix: allow arbitrary exprs in VALUES clause * update test
1 parent e41711b commit f00ef9d

File tree

4 files changed

+34
-47
lines changed

4 files changed

+34
-47
lines changed

datafusion/core/tests/sqllogictests/test_files/ddl.slt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,19 @@ query I
565565
select * from foo;
566566
----
567567
NULL
568+
569+
statement ok
570+
drop table foo;
571+
572+
573+
## should allow any type of exprs as values
574+
statement ok
575+
create table t (i interval, x int) as values (interval '5 days 3 nanoseconds', CASE when true then 1 else 0 end);
576+
577+
query ?I
578+
select * from t;
579+
----
580+
0 years 0 mons 5 days 0 hours 0 mins 0.000000003 secs 1
581+
582+
statement ok
583+
drop table t;

datafusion/core/tests/sqllogictests/test_files/interval.slt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,20 @@ select
149149
Interval(MonthDayNano) Interval(MonthDayNano)
150150

151151

152-
# Should work tables with interval values
153-
# https://github.com/apache/arrow-datafusion/issues/5802
154-
statement error DataFusion error: This feature is not implemented: Unsupported value Interval \{ value: Value\(SingleQuotedString\("5 days 3 nanoseconds"\)\), leading_field: None, leading_precision: None, last_field: None, fractional_seconds_precision: None \} in a values list expression
152+
statement ok
155153
create table t (i interval) as values (interval '5 days 3 nanoseconds');
156154

155+
query ?T rowsort
156+
select
157+
i,
158+
arrow_typeof(i)
159+
from t;
160+
----
161+
0 years 0 mons 5 days 0 hours 0 mins 0.000000003 secs Interval(MonthDayNano)
162+
163+
164+
statement ok
165+
drop table t;
157166

158167
# Create tables with interval values
159168
statement ok

datafusion/sql/src/set_expr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
2828
) -> Result<LogicalPlan> {
2929
match set_expr {
3030
SetExpr::Select(s) => self.select_to_plan(*s, planner_context),
31-
SetExpr::Values(v) => {
32-
self.sql_values_to_plan(v, planner_context.prepare_param_data_types())
33-
}
31+
SetExpr::Values(v) => self.sql_values_to_plan(v, planner_context),
3432
SetExpr::SetOperation {
3533
op,
3634
left,

datafusion/sql/src/values.rs

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616
// under the License.
1717

1818
use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
19-
use arrow_schema::DataType;
20-
use datafusion_common::{DFSchema, DataFusionError, Result};
21-
use datafusion_expr::{lit, Cast, Expr, LogicalPlan, LogicalPlanBuilder};
22-
use sqlparser::ast::{Expr as SQLExpr, Values as SQLValues};
19+
use datafusion_common::{DFSchema, Result};
20+
use datafusion_expr::{LogicalPlan, LogicalPlanBuilder};
21+
use sqlparser::ast::Values as SQLValues;
2322

2423
impl<'a, S: ContextProvider> SqlToRel<'a, S> {
2524
pub(super) fn sql_values_to_plan(
2625
&self,
2726
values: SQLValues,
28-
param_data_types: &[DataType],
27+
planner_context: &mut PlannerContext,
2928
) -> Result<LogicalPlan> {
3029
let SQLValues {
3130
explicit_row: _,
@@ -38,42 +37,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
3837
.into_iter()
3938
.map(|row| {
4039
row.into_iter()
41-
.map(|v| match v {
42-
SQLExpr::Value(value) => {
43-
self.parse_value(value, param_data_types)
44-
}
45-
SQLExpr::UnaryOp { op, expr } => self.parse_sql_unary_op(
46-
op,
47-
*expr,
48-
&schema,
49-
&mut PlannerContext::new(),
50-
),
51-
SQLExpr::BinaryOp { left, op, right } => self
52-
.parse_sql_binary_op(
53-
*left,
54-
op,
55-
*right,
56-
&schema,
57-
&mut PlannerContext::new(),
58-
),
59-
SQLExpr::TypedString { data_type, value } => {
60-
Ok(Expr::Cast(Cast::new(
61-
Box::new(lit(value)),
62-
self.convert_data_type(&data_type)?,
63-
)))
64-
}
65-
SQLExpr::Cast { expr, data_type } => Ok(Expr::Cast(Cast::new(
66-
Box::new(self.sql_expr_to_logical_expr(
67-
*expr,
68-
&schema,
69-
&mut PlannerContext::new(),
70-
)?),
71-
self.convert_data_type(&data_type)?,
72-
))),
73-
other => Err(DataFusionError::NotImplemented(format!(
74-
"Unsupported value {other:?} in a values list expression"
75-
))),
76-
})
40+
.map(|v| self.sql_to_expr(v, &schema, planner_context))
7741
.collect::<Result<Vec<_>>>()
7842
})
7943
.collect::<Result<Vec<_>>>()?;

0 commit comments

Comments
 (0)