Skip to content
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

Support multi column IN lists #8590

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion datafusion/expr/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::Operator;
use arrow::array::{new_empty_array, Array};
use arrow::compute::can_cast_types;
use arrow::datatypes::{
DataType, Field, TimeUnit, DECIMAL128_MAX_PRECISION, DECIMAL128_MAX_SCALE,
DataType, Field, FieldRef, TimeUnit, DECIMAL128_MAX_PRECISION, DECIMAL128_MAX_SCALE,
DECIMAL256_MAX_PRECISION, DECIMAL256_MAX_SCALE,
};

Expand Down Expand Up @@ -298,6 +298,7 @@ pub fn comparison_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<D
.or_else(|| string_numeric_coercion(lhs_type, rhs_type))
.or_else(|| string_temporal_coercion(lhs_type, rhs_type))
.or_else(|| binary_coercion(lhs_type, rhs_type))
.or_else(|| struct_coercion(lhs_type, rhs_type))
}

/// Coerce `lhs_type` and `rhs_type` to a common type for the purposes of a comparison operation
Expand Down Expand Up @@ -745,6 +746,29 @@ fn binary_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType>
}
}

fn struct_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
use arrow::datatypes::DataType::*;
match (lhs_type, rhs_type) {
(Struct(lhs_fields), Struct(rhs_fields)) => {
let types = lhs_fields
.iter()
.map(|f| f.data_type())
.zip(rhs_fields.iter().map(|f| f.data_type()))
.map(|(lhs, rhs)| comparison_coercion(lhs, rhs))
.collect::<Option<Vec<DataType>>>()?;
let fields = types
.into_iter()
.enumerate()
.map(|(i, datatype)| {
Arc::new(Field::new(format!("c{}", i), datatype, true))
})
.collect::<Vec<FieldRef>>();
Some(Struct(fields.into()))
}
_ => None,
}
}

/// coercion rules for like operations.
/// This is a union of string coercion rules and dictionary coercion rules
pub fn like_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
Expand Down
24 changes: 24 additions & 0 deletions datafusion/sql/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ mod substring;
mod unary_op;
mod value;

use std::vec;

use crate::planner::{ContextProvider, PlannerContext, SqlToRel};

use arrow_schema::DataType;
use arrow_schema::TimeUnit;
use datafusion_common::{
Expand Down Expand Up @@ -513,6 +516,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
self.parse_struct(values, fields, schema, planner_context)
}

SQLExpr::Tuple(values) => self.parse_tuple(values, schema, planner_context),

_ => not_impl_err!("Unsupported ast node in sqltorel: {sql:?}"),
}
}
Expand Down Expand Up @@ -583,6 +588,25 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
)))
}

fn parse_tuple(
&self,
values: Vec<SQLExpr>,
input_schema: &DFSchema,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
if values.is_empty() {
return not_impl_err!("Empty tuple not supported yet");
}
match values.get(0).unwrap() {
SQLExpr::Identifier(_) | SQLExpr::Value(_) => {
self.parse_struct(values, vec![], input_schema, planner_context)
}
_ => {
not_impl_err!("Only identifiers and literals are supported in tuples")
}
}
}

fn sql_in_list_to_expr(
&self,
expr: SQLExpr,
Expand Down