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

ScalarUDF with zero arguments should be provided with one null array as parameter #9031

Merged
merged 6 commits into from
Jan 30, 2024

Conversation

viirya
Copy link
Member

@viirya viirya commented Jan 28, 2024

Which issue does this PR close?

Closes #9032.

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@viirya
Copy link
Member Author

viirya commented Jan 28, 2024

Fix clippy at #9034

@@ -578,8 +578,9 @@ fn roundtrip_builtin_scalar_function() -> Result<()> {
"acos",
fun_expr,
vec![col("a", &schema)?],
DataType::Int64,
DataType::Float64,
Copy link
Member Author

@viirya viirya Jan 29, 2024

Choose a reason for hiding this comment

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

The existing test is not correct at all. acos built-in scalar function's return type should be Float64.

Previously the roundtrip test passes because from_proto simply takes serde return type and uses it as parameter to ScalarFunctionExpr.

But in this PR, from_proto calls create_physical_expr which gets return type directly from BuiltinScalarFunction. So with the PR, this test issue is found.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thanks @viirya -- I think this is looking close. My only real concern is about adding the entire Signature on to ScalarFunctionExpr but if you feel differently I would be ok with this PR as written.

async fn test_user_defined_functions_zero_argument() -> Result<()> {
let ctx = SessionContext::new();

let schema = Arc::new(Schema::new(vec![
Copy link
Contributor

Choose a reason for hiding this comment

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

it doesn't hurt but I wonder if the example table needs 4 columns 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I just copied the reported test case. I think we can reduce the columns.

Copy link
Member Author

Choose a reason for hiding this comment

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

Reduced to one column.


assert_eq!(random_udf.len(), native_random.len());

let mut previous = 1.0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can could the random implementation ever actually make 1.0 (the range is 0..1.0). Maybe we could start at -1.0 or something just to be sure this won't ever flake

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the range 0..1.0 is exclusive on the end point?

Copy link
Member Author

Choose a reason for hiding this comment

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

But -1.0 is also good.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the range 0..1.0 is exclusive on the end point?

If so that this is fine!

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed to -1.0 to make it more clear.

@@ -58,6 +58,8 @@ pub struct ScalarFunctionExpr {
// and it specifies the effect of an increase or decrease in
// the corresponding `arg` to the function value.
monotonicity: Option<FuncMonotonicity>,
// Signature of the function
signature: Signature,
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like only one field is ever read. I wonder if it would be better to copy just this field rather than the entire signature (which is both larger with several allocations, but also might be misleading that this signature information was used somehow more in execution plans.

I worry that the signature information might start being referred to in physical planning

So perhaps something like

Suggested change
signature: Signature,
// Does this function need to be invoked with zero arguments ?
supports_zero_argument: bool,
self.signature.type_signature.supports_zero_argument

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok

@@ -149,6 +153,11 @@ impl PhysicalExpr for ScalarFunctionExpr {
{
vec![ColumnarValue::create_null_array(batch.num_rows())]
}
// If the function supports zero argument, we pass in a null array indicating the batch size.
Copy link
Contributor

Choose a reason for hiding this comment

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

I never fully understood why this didn't just check self.args.is_empty() 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. Changed to self.args.is_empty().

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

This looks great -- a really nice improvement. Thank you @viirya 🙏

@alamb alamb merged commit 85ceb9d into apache:main Jan 30, 2024
22 checks passed
@alamb
Copy link
Contributor

alamb commented Jan 30, 2024

Thanks again @viirya and @dadepo for the report and reproducer 🙏

@viirya
Copy link
Member Author

viirya commented Jan 30, 2024

Thanks @alamb and @dadepo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate physical-expr Physical Expressions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ScalarUDF with zero arguments should be provided with one null array as parameter
2 participants