Skip to content

Unwrap year in comparison also for IN predicate #18092

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

Merged
merged 1 commit into from
Jul 5, 2023
Merged

Unwrap year in comparison also for IN predicate #18092

merged 1 commit into from
Jul 5, 2023

Conversation

kabunchi
Copy link

@kabunchi kabunchi commented Jun 29, 2023

Description

Additional context and related issues

Release notes

(*) This is not user-visible or docs only and no release notes are required.
( ) Release notes are required, please propose a release note for me.
( ) Release notes are required, with the following suggested text:

# Section
* Improve query performance for queries involving `year` function within `IN` predicate.

@cla-bot cla-bot bot added the cla-signed label Jun 29, 2023
{
Expression value = node.getValue();
Expression valueList = node.getValueList();
Expression inPredicate = treeRewriter.defaultRewrite((Expression) node, null);
Copy link
Author

Choose a reason for hiding this comment

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

@ebyhr I am not sure I need this line. please advise.

Copy link
Member

Choose a reason for hiding this comment

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

i think it would be better to default-rewrite (recursive) first, then to extract the attributes, just in case they got rewritten or simplified (don't think it matters currently, matter of style/future-proofing)

 InPredicate inPredicate = treeRewriter.defaultRewrite(node, null);
            Expression value = inPredicate.getValue();
            Expression valueList = inPredicate.getValueList();

@ebyhr ebyhr requested review from findepi and ebyhr July 3, 2023 08:44
@@ -124,6 +129,36 @@ public Expression rewriteComparisonExpression(ComparisonExpression node, Void co
return unwrapYear(expression);
}

@Override
Copy link
Member

Choose a reason for hiding this comment

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

we probably want similar change for UnwrapDateTruncInComparison and UnwrapCastInComparison?

cc @martint @alexjo2144

Copy link
Contributor

Choose a reason for hiding this comment

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

Follow-up #18138

// Convert each value to a comparison expression and try to unwrap it.
// unwrap the InPredicate only in case we manage to unwrap the entire value list
List<Expression> expressionsList = inListExpression.getValues();
ArrayList<Expression> comparisonExpressionsList = new ArrayList<>(expressionsList.size());
Copy link
Member

Choose a reason for hiding this comment

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

use ImmutableList.Builder

(per style, but also to avoid copy when creating new LogicalExpression)

{
Expression value = node.getValue();
Expression valueList = node.getValueList();
Expression inPredicate = treeRewriter.defaultRewrite((Expression) node, null);
Copy link
Member

Choose a reason for hiding this comment

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

i think it would be better to default-rewrite (recursive) first, then to extract the attributes, just in case they got rewritten or simplified (don't think it matters currently, matter of style/future-proofing)

 InPredicate inPredicate = treeRewriter.defaultRewrite(node, null);
            Expression value = inPredicate.getValue();
            Expression valueList = inPredicate.getValueList();

@findepi
Copy link
Member

findepi commented Jul 4, 2023

force-pushed the unwrap-in branch from dc30ccf to 600dba1
8 minutes ago

please avoid mixing changes with rebase. it's hard to notice what chaned.

Comment on lines 136 to 137
Expression value = node.getValue();
Expression valueList = node.getValueList();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Expression value = node.getValue();
Expression valueList = node.getValueList();
Expression value = inPredicate.getValue();
Expression valueList = inPredicate.getValueList();

Copy link
Author

Choose a reason for hiding this comment

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

done. I wasn't sure that the returned expression from defaultRewriter must be an InPredicate so I also added a validation.

Comment on lines 148 to 150
List<Expression> expressionsList = inListExpression.getValues();
ImmutableList.Builder<Expression> comparisonExpressionsListBuilder = ImmutableList.builder();
for (Expression currExpression : expressionsList) {
Copy link
Member

Choose a reason for hiding this comment

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

  • avoid abbreviations ("curr")
  • comparisonExpressionsListBuilder is a long name and the fact it's listbuilder is known from its type
  • expressionsList can be inlined
Suggested change
List<Expression> expressionsList = inListExpression.getValues();
ImmutableList.Builder<Expression> comparisonExpressionsListBuilder = ImmutableList.builder();
for (Expression currExpression : expressionsList) {
ImmutableList.Builder<Expression> comparisonExpressions = ImmutableList.builderWithExpectedSize(inListExpression.getValues().size());
for (Expression rightExpression : inListExpression.getValues()) {
ComparisonExpression comparisonExpression = new ComparisonExpression(EQUAL, value, rightExpression);

Copy link
Author

Choose a reason for hiding this comment

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

done.

@findepi
Copy link
Member

findepi commented Jul 5, 2023

@kabunchi thanks, will merge when the build passes
can you please follow-up with https://github.com/trinodb/trino/pull/18092/files#r1252095563 ?

@findepi findepi merged commit d3f55cb into trinodb:master Jul 5, 2023
@github-actions github-actions bot added this to the 421 milestone Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants