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

Keep output as scalar for scalar function if all inputs are scalar #7967

Merged
merged 2 commits into from
Oct 31, 2023
Merged
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
11 changes: 10 additions & 1 deletion datafusion/physical-expr/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ where
ColumnarValue::Array(a) => Some(a.len()),
});

let is_scalar = len.is_none();

let inferred_length = len.unwrap_or(1);
let args = args
.iter()
Expand All @@ -373,7 +375,14 @@ where
.collect::<Vec<ArrayRef>>();

let result = (inner)(&args);
result.map(ColumnarValue::Array)

if is_scalar {
// If all inputs are scalar, keeps output as scalar
let result = result.and_then(|arr| ScalarValue::try_from_array(&arr, 0));
result.map(ColumnarValue::Scalar)
} else {
result.map(ColumnarValue::Array)
}
})
}

Expand Down
34 changes: 34 additions & 0 deletions datafusion/physical-expr/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,37 @@ pub fn create_physical_expr(
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use arrow_array::{ArrayRef, BooleanArray, RecordBatch, StringArray};
use arrow_schema::{DataType, Field, Schema};
use datafusion_common::{DFSchema, Result};
use datafusion_expr::{col, left, Literal};

#[test]
fn test_create_physical_expr_scalar_input_output() -> Result<()> {
let expr = col("letter").eq(left("APACHE".lit(), 1i64.lit()));

let schema = Schema::new(vec![Field::new("letter", DataType::Utf8, false)]);
let df_schema = DFSchema::try_from_qualified_schema("data", &schema)?;
let p = create_physical_expr(&expr, &df_schema, &schema, &ExecutionProps::new())?;

let batch = RecordBatch::try_new(
Arc::new(schema),
vec![Arc::new(StringArray::from_iter_values(vec![
"A", "B", "C", "D",
]))],
)?;
let result = p.evaluate(&batch)?;
let result = result.into_array(4);

assert_eq!(
&result,
&(Arc::new(BooleanArray::from(vec![true, false, false, false,])) as ArrayRef)
);

Ok(())
}
}
48 changes: 48 additions & 0 deletions datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1878,3 +1878,51 @@ query T
SELECT CONCAT('Hello', 'World')
----
HelloWorld

statement ok
CREATE TABLE simple_string(
letter STRING,
letter2 STRING
) as VALUES
('A', 'APACHE'),
('B', 'APACHE'),
('C', 'APACHE'),
('D', 'APACHE')
;

query TT
EXPLAIN SELECT letter, letter = LEFT('APACHE', 1) FROM simple_string;
----
logical_plan
Projection: simple_string.letter, simple_string.letter = Utf8("A") AS simple_string.letter = left(Utf8("APACHE"),Int64(1))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end-to-end tests are not failed, but I think it is good to have it to be clear.

For end-to-end execution, LEFT('APACHE', 1) is a foldable literal. So expression optimizer optimizes it to a scalar literal.

But if developers use the API create_physical_expr directly, the reported error will happen.

--TableScan: simple_string projection=[letter]
physical_plan
ProjectionExec: expr=[letter@0 as letter, letter@0 = A as simple_string.letter = left(Utf8("APACHE"),Int64(1))]
--MemoryExec: partitions=1, partition_sizes=[1]

query TB
SELECT letter, letter = LEFT('APACHE', 1) FROM simple_string;
----
----
A true
B false
C false
D false

query TT
EXPLAIN SELECT letter, letter = LEFT(letter2, 1) FROM simple_string;
----
logical_plan
Projection: simple_string.letter, simple_string.letter = left(simple_string.letter2, Int64(1))
--TableScan: simple_string projection=[letter, letter2]
physical_plan
ProjectionExec: expr=[letter@0 as letter, letter@0 = left(letter2@1, 1) as simple_string.letter = left(simple_string.letter2,Int64(1))]
--MemoryExec: partitions=1, partition_sizes=[1]

query TB
SELECT letter, letter = LEFT(letter2, 1) FROM simple_string;
----
A true
B false
C false
D false