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 UNION field nullability tracking #14356

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

findepi
Copy link
Member

@findepi findepi commented Jan 29, 2025

This commit fixes two bugs related to UNION handling

  • when constructing union plan nullability of the other union branch was
    ignored, thus resulting field could easily have incorrect nullability
  • when pruning/simplifying projects, in recompute_schema function
    there was similar logic, thus loosing nullability information even for
    correctly constructed Union plan node

As a result, other optimizer logic (e.g. expr_simplifier.rs) could
draw incorrect conclusions and thus lead to incorrect query results, as
demonstrated with the attached SLT test.

@github-actions github-actions bot added logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt) labels Jan 29, 2025
This commit fixes two bugs related to UNION handling

- when constructing union plan nullability of the other union branch was
  ignored, thus resulting field could easily have incorrect nullability
- when pruning/simplifying projects, in `recompute_schema` function
  there was similar logic, thus loosing nullability information even for
  correctly constructed Union plan node

As a result, other optimizer logic (e.g. `expr_simplifier.rs`) could
draw incorrect conclusions and thus lead to incorrect query results, as
demonstrated with the attached SLT test.
@findepi
Copy link
Member Author

findepi commented Jan 29, 2025

cc @felipecrv, @Omega359

@findepi findepi requested review from alamb and jonahgao January 29, 2025 14:07
@alamb
Copy link
Contributor

alamb commented Jan 29, 2025

@wiedld can you also please review this PR?

/// Constructs new Union instance deriving schema from inputs.
///
/// `loose_types` if true, inputs do not have to have matching types and produced schema will
/// take type from the first input. TODO this is not necessarily reasonable behavior.
Copy link
Contributor

Choose a reason for hiding this comment

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

Possible alternative: coerce type to common type? https://www.postgresql.org/docs/17/typeconv-union-case.html is what PG does

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 agree this is what should be happening.
There are two places where this code is run:

  1. plan building
  2. recompute_schema

For now in the (1) case I retained pre-existing logic, see current main

// Temporarily use the schema from the left input and later rely on the analyzer to
// coerce the two schemas into a common one.

For (2) we don't want any coercions at all.

@Omega359
Copy link
Contributor

I ran the sqlite tests against this branch with no changes so the tests in there did not cover this case.

@findepi
Copy link
Member Author

findepi commented Jan 29, 2025

Thanks @Omega359 for your review, addressed!

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.

Thank you @findepi -- given this PR fixes a bug it looks good to me 👍 🦾 for the assist @Omega359

It would be great to avoid using a second copy of union schema coercion if possible

/// Constructs new Union instance deriving schema from inputs.
/// Inputs do not have to have matching types and produced schema will
/// take type from the first input.
pub fn try_new_with_loose_types(inputs: Vec<Arc<LogicalPlan>>) -> Result<Self> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Another name for this one is try_new_with_coerce_types to emphasize it is coercing the types

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 believe this one should be coercing types, but it's not doing this. It just takes a type from first UNION branch.
This is not new logic though. It's just moved from plan builder over here, to avoid duplicating code.

I didn't know why this logic existed in this shape, but it felt intentional enough not to simply replace it in this PR. I would prefer it to be fixed later... Hence this function name to make the caller wonder what "loose types" mean.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, this was noted once before. Thank you for making it more explicit.

datafusion/expr/src/logical_plan/plan.rs Show resolved Hide resolved
inputs: &[Arc<LogicalPlan>],
loose_types: bool,
) -> Result<DFSchemaRef> {
if inputs.len() < 2 {
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 there is already code that computes the coerced schema in the analyzer:

pub fn coerce_union_schema(inputs: &[Arc<LogicalPlan>]) -> Result<DFSchema> {

Can we reuse the same logic? Maybe we can move the coercion code here

Copy link
Member Author

Choose a reason for hiding this comment

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

In the optimizer case, no coercion logic should be invoked, the types must match.
So we need a version of this code which does that. (#14296 (comment))

For the variant which constructs new schema from some uncoerced inputs -- currently called "loose types" and currently not doing coercion to maintain behavior as it was -- yes, i agree this one could use the coerce_union_schema from the analyzer (perhaps moving the logic into here)

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we can file a ticket describing what is desired and leave a comment in the code referring to that ticket

Copy link
Contributor

Choose a reason for hiding this comment

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

Making sure I understand the use case. If I want to construct a UNION logical plan with different types that are coercible (be it by current builtin rules or future user-defined rules), then I would use the Union::try_new_with_loose_types and have the analyzer pass handle coercion. Is this right?

Then what exactly is the use case for the Union::try_new? Since it's used in the schema recompute which can occur after the analyzer type coercion. Do we therefore need it to always perform proper coercion, including any future user-defined coercion rules?

datafusion/sqllogictest/test_files/union.slt Show resolved Hide resolved
inputs: &[Arc<LogicalPlan>],
loose_types: bool,
) -> Result<DFSchemaRef> {
if inputs.len() < 2 {
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we can file a ticket describing what is desired and leave a comment in the code referring to that ticket

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

Successfully merging this pull request may close these issues.

Incorrect result for IS NOT NULL predicate over UNION ALL query
4 participants