-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: Speed up struct and named_struct using invoke_with_args
#14276
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // 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::{ | ||
| array::{Float32Array, Float64Array}, | ||
| datatypes::{DataType, Field, Schema}, | ||
| record_batch::RecordBatch, | ||
| }; | ||
| use criterion::{criterion_group, criterion_main, Criterion}; | ||
| use datafusion::prelude::SessionContext; | ||
| use datafusion::{datasource::MemTable, error::Result}; | ||
| use futures::executor::block_on; | ||
| use std::sync::Arc; | ||
| use tokio::runtime::Runtime; | ||
|
|
||
| async fn query(ctx: &SessionContext, sql: &str) { | ||
| let rt = Runtime::new().unwrap(); | ||
|
|
||
| // execute the query | ||
| let df = rt.block_on(ctx.sql(sql)).unwrap(); | ||
| criterion::black_box(rt.block_on(df.collect()).unwrap()); | ||
| } | ||
|
|
||
| fn create_context(array_len: usize, batch_size: usize) -> Result<SessionContext> { | ||
| // define a schema. | ||
| let schema = Arc::new(Schema::new(vec![ | ||
| Field::new("f32", DataType::Float32, false), | ||
| Field::new("f64", DataType::Float64, false), | ||
| ])); | ||
|
|
||
| // define data. | ||
| let batches = (0..array_len / batch_size) | ||
| .map(|i| { | ||
| RecordBatch::try_new( | ||
| schema.clone(), | ||
| vec![ | ||
| Arc::new(Float32Array::from(vec![i as f32; batch_size])), | ||
| Arc::new(Float64Array::from(vec![i as f64; batch_size])), | ||
| ], | ||
| ) | ||
| .unwrap() | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let ctx = SessionContext::new(); | ||
|
|
||
| // declare a table in memory. In spark API, this corresponds to createDataFrame(...). | ||
| let provider = MemTable::try_new(schema, vec![batches])?; | ||
| ctx.register_table("t", Arc::new(provider))?; | ||
|
|
||
| Ok(ctx) | ||
| } | ||
|
|
||
| fn criterion_benchmark(c: &mut Criterion) { | ||
| let array_len = 524_288; // 2^19 | ||
| let batch_size = 4096; // 2^12 | ||
|
|
||
| c.bench_function("struct", |b| { | ||
| let ctx = create_context(array_len, batch_size).unwrap(); | ||
| b.iter(|| block_on(query(&ctx, "select struct(f32, f64) from t"))) | ||
| }); | ||
| } | ||
|
|
||
| criterion_group!(benches, criterion_benchmark); | ||
| criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,79 +17,15 @@ | |
|
|
||
| use arrow::array::StructArray; | ||
| use arrow::datatypes::{DataType, Field, Fields}; | ||
| use datafusion_common::{exec_err, internal_err, HashSet, Result, ScalarValue}; | ||
| use datafusion_expr::{ColumnarValue, Documentation, ReturnInfo, ReturnTypeArgs}; | ||
| use datafusion_common::{exec_err, internal_err, Result}; | ||
| use datafusion_expr::{ | ||
| ColumnarValue, Documentation, ReturnInfo, ReturnTypeArgs, ScalarFunctionArgs, | ||
| }; | ||
| use datafusion_expr::{ScalarUDFImpl, Signature, Volatility}; | ||
| use datafusion_macros::user_doc; | ||
| use std::any::Any; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Put values in a struct array. | ||
| fn named_struct_expr(args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
| // Do not accept 0 arguments. | ||
| if args.is_empty() { | ||
| return exec_err!( | ||
| "named_struct requires at least one pair of arguments, got 0 instead" | ||
| ); | ||
| } | ||
|
|
||
| if args.len() % 2 != 0 { | ||
| return exec_err!( | ||
| "named_struct requires an even number of arguments, got {} instead", | ||
| args.len() | ||
| ); | ||
| } | ||
|
|
||
| let (names, values): (Vec<_>, Vec<_>) = args | ||
| .chunks_exact(2) | ||
| .enumerate() | ||
| .map(|(i, chunk)| { | ||
| let name_column = &chunk[0]; | ||
| let name = match name_column { | ||
| ColumnarValue::Scalar(ScalarValue::Utf8(Some(name_scalar))) => { | ||
| name_scalar | ||
| } | ||
| // TODO: Implement Display for ColumnarValue | ||
| _ => { | ||
| return exec_err!( | ||
| "named_struct even arguments must be string literals at position {}", | ||
| i * 2 | ||
| ) | ||
| } | ||
| }; | ||
|
|
||
| Ok((name, chunk[1].clone())) | ||
| }) | ||
| .collect::<Result<Vec<_>>>()? | ||
| .into_iter() | ||
| .unzip(); | ||
|
|
||
| { | ||
| // Check to enforce the uniqueness of struct field name | ||
| let mut unique_field_names = HashSet::new(); | ||
| for name in names.iter() { | ||
| if unique_field_names.contains(name) { | ||
| return exec_err!( | ||
| "named_struct requires unique field names. Field {name} is used more than once." | ||
| ); | ||
| } | ||
| unique_field_names.insert(name); | ||
| } | ||
| } | ||
|
|
||
| let fields: Fields = names | ||
| .into_iter() | ||
| .zip(&values) | ||
| .map(|(name, value)| Arc::new(Field::new(name, value.data_type().clone(), true))) | ||
| .collect::<Vec<_>>() | ||
| .into(); | ||
|
|
||
| let arrays = ColumnarValue::values_to_arrays(&values)?; | ||
|
|
||
| let struct_array = StructArray::new(fields, arrays, None); | ||
| Ok(ColumnarValue::Array(Arc::new(struct_array))) | ||
| } | ||
|
|
||
| #[user_doc( | ||
| doc_section(label = "Struct Functions"), | ||
| description = "Returns an Arrow struct using the specified name and input expressions pairs.", | ||
|
|
@@ -203,12 +139,28 @@ impl ScalarUDFImpl for NamedStructFunc { | |
| )))) | ||
| } | ||
|
|
||
| fn invoke_batch( | ||
| &self, | ||
| args: &[ColumnarValue], | ||
| _number_rows: usize, | ||
| ) -> Result<ColumnarValue> { | ||
| named_struct_expr(args) | ||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| let DataType::Struct(fields) = args.return_type else { | ||
| return internal_err!("incorrect named_struct return type"); | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| fields.len(), | ||
| args.args.len() / 2, | ||
| "return type field count != argument count / 2" | ||
| ); | ||
|
|
||
| let values: Vec<ColumnarValue> = args | ||
|
Contributor
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. 😍 |
||
| .args | ||
| .chunks_exact(2) | ||
| .map(|chunk| chunk[1].clone()) | ||
| .collect(); | ||
| let arrays = ColumnarValue::values_to_arrays(&values)?; | ||
| Ok(ColumnarValue::Array(Arc::new(StructArray::new( | ||
| fields.clone(), | ||
| arrays, | ||
| None, | ||
| )))) | ||
| } | ||
|
|
||
| fn documentation(&self) -> Option<&Documentation> { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
If delete
invoke_batchfunction, the defaultinvoke_batchfunction will callinvokewhen args is not empty, butinvokefunction is left impl by Specific type, is it ok?Uh oh!
There was an error while loading. Please reload this page.
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.
I wasn't sure what the intention is for the deprecated functions from an API user point of view. I had interpreted ScalarUDF(Impl) as an extension point rather than as public API that library users would call directly. If that assumption is correct, then it doesn't matter if we remove the
invoke_batchimplementation sinceScalarFunctionExprnever calls this method. The only usages ofinvoke_batchare in benchmark and test code.Perhaps @alamb, who logged #13515, can provide some insight here.
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.
THe reason we kept around deprecated functions is to give downstream users of DataFusion time to adjust their code -- especially on upgrade having a deprecated function with guidance of what to change has been helpful
You can read more about this strategy here:
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.
I'm coming from the Java world so I'll use the terminology from there; not sure what the equivalent is in Rust lingo. It's not explicitly stated which parts of the library are Service Provider Interface vs Application Programming Interface. My assumption was that
ScalarUDFImplis SPI andinvoke_batchwas kept around to not break all existing implementations andScalarFunctionExpris the API side of things which doesn't exposeinvoke_batch. Is that a correct interpretation?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.
Yes, that is accurate in my understanding
Yes that is also my understanding
I would say
ScalarFunctionExpris an implementation detail of how functions are invoked in theExecutionPlan(aka the physical execution)The split between logical/physical plans is explained a bit in the API docs / intro videos in case you are interested:
https://docs.rs/datafusion/latest/datafusion/index.html#query-planning-and-execution-overview