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

fix: coalesce schema issues #12308

Merged
merged 1 commit into from
Sep 27, 2024
Merged

Conversation

mesejo
Copy link
Contributor

@mesejo mesejo commented Sep 3, 2024

Which issue does this PR close?

Closes #12307.

Are these changes tested?

Yes

Are there any user-facing changes?

No

@github-actions github-actions bot added core Core DataFusion crate functions labels Sep 3, 2024
@github-actions github-actions bot added logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt) labels Sep 4, 2024
@mesejo mesejo force-pushed the fix/coalesce-null branch 7 times, most recently from 6954677 to 01fab57 Compare September 9, 2024 18:36
@mesejo mesejo force-pushed the fix/coalesce-null branch 3 times, most recently from 4c7989e to 30a5c5d Compare September 10, 2024 18:36
@mesejo mesejo requested a review from jayzhan211 September 10, 2024 19:35
Copy link
Contributor

@jayzhan211 jayzhan211 left a comment

Choose a reason for hiding this comment

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

Thanks @mesejo, I think overall looks good to me

datafusion/sqllogictest/test_files/timestamps.slt Outdated Show resolved Hide resolved
@mesejo mesejo force-pushed the fix/coalesce-null branch 2 times, most recently from 5c57ac6 to 1cc4344 Compare September 13, 2024 10:50
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 @mesejo and @jayzhan211

cc @findepi who I believe is also working in this area / thinking about functions

Self {
signature: Signature::one_of(
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 to me that moving the signature from a data driven description (aka describe "what" is needed and letting some other code compute if the given arguments match that signature), this PR is moving many of the functions towards more functional (each function has to implement its own custom coercion, likely resulting in significant duplication).

What do you think (perhaps as a follow on PR) of adding DataType::Null support to the Signature calculations somehow rather than inlining / duplicating the coercion logic?

Maybe something like

Signature::allow_null(..)

that would support automatically coercing arguments from null?

Or maybe we should always support coercing Null to any type

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternative signature like Signature::String, similar to Signature::numeric that includes converting null to string too?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure -- I was just reacting that this "handle null" pattern seems common and it seems like this approach will require custom coerce logic for all functions 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Null to T coercion needs to be handled elsewhere anyway (eg when computing type of a UNION, etc.).
We can free functions from having to bother about coercions at all and let the engine calculate coercions when building the logical plan.

This is actually super fundamental for DataFusion vision as a composable query engine. Coercion rules are very implementation-specific. If we had functions spiced up with coercions inside them, that would make those functions non-reusable.

cc @wizardxz @sadboy

Copy link
Contributor

Choose a reason for hiding this comment

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

100%

It seems to me like Signature is supposed to communicate what types the function implementation has a native implementation for and the coercion of whatever the user provided doesn't match one of the supported types

Copy link
Contributor

Choose a reason for hiding this comment

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

@findepi Are you suggesting something like general coercion that is non-function specific? But what if we want different coercion rule for different function, we might need to do coercion function wise

Copy link
Member

Choose a reason for hiding this comment

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

Why would we want different coercion rules for different functions?

Copy link
Contributor

Choose a reason for hiding this comment

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

My idea is that it is more flexible to the user, although, without the real use case, it might be a premature optimization 🤔.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry to chip in late; this PR addresses other issues, such as #12307. I wonder if I could split it and leave the changes regarding the coercion of functions in this one (to keep the discussion in one place) and the others in a new PR. Would that be ok?

func.name(),
func.signature().clone(),
&arg_data_types,
let new_data_types = data_types_with_scalar_udf(&arg_data_types, func)
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 this is the root cause of the issue and to solve this other changes are necessary. Therefore, I think we should go with this change and maybe further optimize the coercion in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, so should I leave it as it is? Or change it back to how it was:

data_types_with_scalar_udf(&arg_data_types, func)

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ok with the current change, maybe wait for @findepi @alamb for how to move on with this PR

Copy link
Contributor

Choose a reason for hiding this comment

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

I defer to @jayzhan211 -- if he is good to merge this PR, let's get the conflicts resolved and merge it in.

If there is additional work we know is needed / could be cleaned up, let's try and file them as tickets

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Conflicts solved! 😄

@jayzhan211 jayzhan211 merged commit 1b3608d into apache:main Sep 27, 2024
24 checks passed
@jayzhan211
Copy link
Contributor

Thanks @mesejo and @alamb @findepi @andygrove for the review

@@ -1907,7 +1907,7 @@ select position('' in '')
1


query error DataFusion error: Execution error: The STRPOS/INSTR/POSITION function can only accept strings, but got Int64.
Copy link
Member

Choose a reason for hiding this comment

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

i agree in principle, but actually why didn't this work?

Looking at the code here, every function that accepts a string, should be invocable with a number or pretty much anything

// Any type can be coerced into strings
(Utf8 | LargeUtf8, _) => Some(type_into.clone()),

i am not convinced this is how it should be, but that's not the point here. My question is -- why this didn't work.

cc @alamb @jayzhan211

Copy link
Contributor

@jayzhan211 jayzhan211 Oct 2, 2024

Choose a reason for hiding this comment

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

coerce_types doesn't go to coerce_from. That is the reason. I hope to deprecate coerce_from some day but not yet 😢

Copy link
Member

Choose a reason for hiding this comment

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

thanks, but not sure i understand. It this explaining why the query fails before or after this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The query was already failing before this PR. The reason is in the function that computes the return type:

fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
        utf8_to_int_type(&arg_types[0], "strpos/instr/position")
    }

The source of the error is in these lines:

data_type => {
return datafusion_common::exec_err!(
"The {} function can only accept strings, but got {:?}.",
name.to_uppercase(),
data_type
);
}
})
}
};
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate functions logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Coalesce fails for query: `SELECT COALESCE(null, 5)
5 participants