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

ExprSimplifier::coerce limits the applicability of containment PruningPredicates (LiteralGuarantee::analyze`) #13244

Closed
joseph-isaacs opened this issue Nov 4, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@joseph-isaacs
Copy link
Contributor

Is your feature request related to a problem or challenge?

I am trying to use PruningPredicates.

The expressions require coercion ExprSimplifier::coerce before it can be evaluated, so we run [simplify, coerce, simplify, create_physical_expr, PruningPredicate::try_new].

If the expression being considered (with schema) is: Col('y', Int32) = Lit(Int64(5)) then the resulting expression will be.

BinaryExpr {
    left: Cast(
        Cast {
            expr: Column(
                Column {
                    relation: None,
                    name: "y",
                },
            ),
            data_type: Int64,
        },
    ),
    op: Eq,
    right: Literal(
        Int64(5),
    ),
}

Then when PruningPredicate analysis is run the LiteralGuarantee::analyze will not find any LiteralGuarantees and therefore pruning will safely assume that nothing can be pruned.

However if the expression is Col('y', Int32) = Lit(Int32(5)), no cast will be included and pruning will work as indented.

Describe the solution you'd like

Either:

  • expand LiteralGuarantee::analyze to understand Casting
  • extend ExprSimplifier::coerce to safely downcast Literals (in Lit <op> Col) to the column type if possible Lit(Int64(2)) -> Lit(Int16(2)).
  • or add another pass to downcast all literals (where lossless) so that coercion is applied to the literal and therefore folded into the literal.

Describe alternatives you've considered

No response

Additional context

Relevant issues: #8302

@joseph-isaacs joseph-isaacs added the enhancement New feature or request label Nov 4, 2024
@eejbyfeldt
Copy link
Contributor

or add another pass to downcast all literals (where lossless) so that coercion is applied to the literal and therefore folded into the literal.

Maybe I am not fully understanding the ask here, but does this not already exists as the optimizer pass UnwrapCastInComparision?

@joseph-isaacs
Copy link
Contributor Author

This is great thanks.

We are only using expressions not plans. Do you think we could make struct UnwrapCastExprRewriter public?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants