-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Remove physical expr of NamedStructField, convert to get_field
function call
#9563
Changes from 14 commits
8301e30
dfb5027
2543d70
69a014e
d2873c7
3decd00
9b00171
0cf4936
3050a2e
c834597
f21f41f
686891c
3a39ec7
25c75e8
d4fe597
0680d2c
ff8d800
61186fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use arrow::datatypes::DataType; | ||
use arrow_array::{Scalar, StringArray}; | ||
use datafusion_common::cast::{as_map_array, as_struct_array}; | ||
use datafusion_common::{exec_err, ExprSchema, Result, ScalarValue}; | ||
use datafusion_expr::field_util::GetFieldAccessSchema; | ||
use datafusion_expr::{ColumnarValue, Expr, ExprSchemable}; | ||
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility}; | ||
use std::any::Any; | ||
|
||
#[derive(Debug)] | ||
pub struct GetFieldFunc { | ||
signature: Signature, | ||
} | ||
|
||
impl GetFieldFunc { | ||
pub fn new() -> Self { | ||
Self { | ||
signature: Signature::any(2, Volatility::Immutable), | ||
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. it seems the signature should be 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 can also be
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 we should keep it as any(2) for now. I forgot that it is better to introduce signature only if we need type coercion. |
||
} | ||
} | ||
} | ||
impl Default for GetFieldFunc { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
// get_field(struct_array, field_name) | ||
impl ScalarUDFImpl for GetFieldFunc { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
fn name(&self) -> &str { | ||
"get_field" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, _: &[DataType]) -> Result<DataType> { | ||
todo!() | ||
} | ||
|
||
fn return_type_from_exprs( | ||
&self, | ||
args: &[Expr], | ||
schema: &dyn ExprSchema, | ||
_arg_types: &[DataType], | ||
) -> Result<DataType> { | ||
if args.len() != 2 { | ||
return exec_err!( | ||
"get_field function requires 2 arguments, got {}", | ||
args.len() | ||
); | ||
} | ||
|
||
let name = match &args[1] { | ||
Expr::Literal(name) => name, | ||
_ => { | ||
return exec_err!( | ||
"get_field function requires the argument field_name to be a string" | ||
); | ||
} | ||
}; | ||
let access_schema = GetFieldAccessSchema::NamedStructField { name: name.clone() }; | ||
let arg_dt = args[0].get_type(schema)?; | ||
access_schema | ||
.get_accessed_field(&arg_dt) | ||
.map(|f| f.data_type().clone()) | ||
} | ||
|
||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
if args.len() != 2 { | ||
return exec_err!( | ||
"get_field function requires 2 arguments, got {}", | ||
args.len() | ||
); | ||
} | ||
|
||
let arr; | ||
let array = match &args[0] { | ||
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. maybe try |
||
ColumnarValue::Array(array) => array, | ||
ColumnarValue::Scalar(scalar) => { | ||
arr = scalar.clone().to_array()?; | ||
&arr | ||
} | ||
}; | ||
let name = match &args[1] { | ||
ColumnarValue::Scalar(name) => name, | ||
_ => { | ||
return exec_err!( | ||
"get_field function requires the argument field_name to be a string" | ||
); | ||
} | ||
}; | ||
match (array.data_type(), name) { | ||
(DataType::Map(_, _), ScalarValue::Utf8(Some(k))) => { | ||
let map_array = as_map_array(array.as_ref())?; | ||
let key_scalar = Scalar::new(StringArray::from(vec![k.clone()])); | ||
let keys = arrow::compute::kernels::cmp::eq(&key_scalar, map_array.keys())?; | ||
let entries = arrow::compute::filter(map_array.entries(), &keys)?; | ||
let entries_struct_array = as_struct_array(entries.as_ref())?; | ||
Ok(ColumnarValue::Array(entries_struct_array.column(1).clone())) | ||
} | ||
(DataType::Struct(_), ScalarValue::Utf8(Some(k))) => { | ||
let as_struct_array = as_struct_array(&array)?; | ||
match as_struct_array.column_by_name(k) { | ||
None => exec_err!( | ||
"get indexed field {k} not found in struct"), | ||
Some(col) => Ok(ColumnarValue::Array(col.clone())) | ||
} | ||
} | ||
(DataType::Struct(_), name) => exec_err!( | ||
"get indexed field is only possible on struct with utf8 indexes. \ | ||
Tried with {name:?} index"), | ||
(dt, name) => exec_err!( | ||
"get indexed field is only possible on lists with int64 indexes or struct \ | ||
with utf8 indexes. Tried {dt:?} with {name:?} index"), | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ fn struct_expr(args: &[ColumnarValue]) -> Result<ColumnarValue> { | |
Ok(ColumnarValue::Array(array_struct(arrays.as_slice())?)) | ||
} | ||
#[derive(Debug)] | ||
pub struct StructFunc { | ||
pub(super) struct StructFunc { | ||
signature: Signature, | ||
} | ||
|
||
|
@@ -73,12 +73,6 @@ impl StructFunc { | |
} | ||
} | ||
|
||
impl Default for StructFunc { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl ScalarUDFImpl for StructFunc { | ||
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. Last PR I make StructFunc pub so I add the Default trait, but since #9546 (comment) it will not be needed. |
||
fn as_any(&self) -> &dyn Any { | ||
self | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ async-trait = { workspace = true } | |
chrono = { workspace = true } | ||
datafusion-common = { workspace = true, default-features = true } | ||
datafusion-expr = { workspace = true } | ||
datafusion-functions = { workspace = true } | ||
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 very important to not add this dependency -- we are triyng to make the core not know about the functions I think #9583 will let us avoid it 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. Cool, I can wait on it and use that function. |
||
datafusion-functions-array = { workspace = true, optional = true } | ||
datafusion-physical-expr = { workspace = true } | ||
hashbrown = { version = "0.14", features = ["raw"] } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
|
||
//! Analyzer rule for to replace operators with function calls (e.g `||` to array_concat`) | ||
|
||
#[cfg(feature = "array_expressions")] | ||
use std::sync::Arc; | ||
|
||
use super::AnalyzerRule; | ||
|
@@ -30,11 +29,11 @@ use datafusion_common::{DFSchema, Result}; | |
use datafusion_expr::expr::ScalarFunction; | ||
use datafusion_expr::expr_rewriter::rewrite_preserving_name; | ||
use datafusion_expr::utils::merge_schema; | ||
use datafusion_expr::BuiltinScalarFunction; | ||
use datafusion_expr::GetFieldAccess; | ||
use datafusion_expr::GetIndexedField; | ||
#[cfg(feature = "array_expressions")] | ||
use datafusion_expr::{BinaryExpr, Operator, ScalarFunctionDefinition}; | ||
use datafusion_expr::{BuiltinScalarFunction, ScalarUDF}; | ||
use datafusion_expr::{Expr, LogicalPlan}; | ||
#[cfg(feature = "array_expressions")] | ||
use datafusion_functions_array::expr_fn::{array_append, array_concat, array_prepend}; | ||
|
@@ -137,6 +136,19 @@ impl TreeNodeRewriter for OperatorToFunctionRewriter { | |
}) = expr | ||
{ | ||
match field { | ||
GetFieldAccess::NamedStructField { name, .. } => { | ||
let expr = *expr.clone(); | ||
let name = name.clone(); | ||
let args = vec![expr, Expr::Literal(name)]; | ||
return Ok(Transformed::yes(Expr::ScalarFunction( | ||
ScalarFunction::new_udf( | ||
Arc::new(ScalarUDF::new_from_impl( | ||
datafusion_functions::core::getfield::GetFieldFunc::new(), | ||
)), | ||
args, | ||
), | ||
))); | ||
} | ||
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 know the method from #9546 (comment), but it seems that here it can't get the 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. only sql crate can get it directly, or any other crate that has 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 we could implement this rewrite more easily using the API proposed in #9583 (so you could use datafusion_functions directly) |
||
GetFieldAccess::ListIndex { ref key } => { | ||
let expr = *expr.clone(); | ||
let key = *key.clone(); | ||
|
@@ -159,7 +171,6 @@ impl TreeNodeRewriter for OperatorToFunctionRewriter { | |
ScalarFunction::new(BuiltinScalarFunction::ArraySlice, args), | ||
))); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
|
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.
👍