-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[CALCITE-6109] Avoid extra loops when optimizing statements with ternary expressions #3518
Conversation
@@ -152,6 +154,25 @@ void checkAssignInConditionOptimizedOut(int modifiers, String s) { | |||
assertThat(Expressions.toString(builder.toBlock()), is(s)); | |||
} | |||
|
|||
@Test void testInlineTernaryNonOptimized() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's customary to add a Javadoc comment indicating the issue which is being solved. Please follow the format of the many examples existing, the tools are very particular about the format, including the final period.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's customary to add a Javadoc comment indicating the issue which is being solved. Please follow the format of the many examples existing, the tools are very particular about the format, including the final period.
Thanks for your suggestion, I have added Javadoc comment on the test case.
This seems to affect the output of at least one Druid test. |
Failed tests are not related to changes. Please rerun the CI. |
280d0fd
to
7a6db03
Compare
Thanks for helping to review the code. |
Oops! Druid Tests Failed again.
|
6fa88a3
to
70c3a62
Compare
I saw the same error in some other PRs, and after I rerun the CI, the tests passed. |
@asdfgh19 |
OK, I will try to working on it latter. |
Expressions.parameter(int.class, "c"))); | ||
b.add(originStatement); | ||
BlockStatement block = b.toBlock(); | ||
assertEquals(block.statements.size(), 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use assertThat
rather than assertEquals
and assertSame
. people get the args in the wrong order. as you did.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that assertSame and assertEquals easily make people confused which is expected and which is actual? So I need to change to assertThat, right?
b.add(originStatement); | ||
BlockStatement block = b.toBlock(); | ||
assertEquals(block.statements.size(), 1); | ||
assertSame(originStatement, block.statements.get(0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why you require assertSame
is rather subtle. and it's the whole point of the test. so, say why.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the Javadoc comment of the BlockBuilder#optimize method indicates that this method returns whether any optimizations were made, and the basis for its judgment is whether the original statement and the statement optimized by InlineVariableVisitor and OptimizeShuttle are the same instance.
Moreover, in the Shuttle class, all visit methods will check the corresponding statement. If the fields of the statement have not been modified, the original statement will be returned.
So I wrote this test case as simple as possible, which only includes one line of code that ensures the conventions of the BlockBuilder and Shuttle classes.
70c3a62
to
766c91b
Compare
Kudos, SonarCloud Quality Gate passed! |
766c91b
to
94f6819
Compare
… of TernaryExpression if it does not do any optimization Before this change, OptimizeShuttle might return a different Expression object that is equivalent to the original; this would cause the optimizer to loop, mistakenly believing that optimizations were occurring, which was inefficient. After this change, the OptimizeShuttle avoids extra loops when optimizing statements with ternary expressions. Close apache#3518
… of TernaryExpression if it does not do any optimization Before this change, OptimizeShuttle might return a different Expression object that is equivalent to the original; this would cause the optimizer to loop, mistakenly believing that optimizations were occurring, which was inefficient. After this change, the OptimizeShuttle avoids extra loops when optimizing statements with ternary expressions. Close apache#3518
… of TernaryExpression if it does not do any optimization Before this change, OptimizeShuttle might return a different Expression object that is equivalent to the original; this would cause the optimizer to loop, mistakenly believing that optimizations were occurring, which was inefficient. After this change, the OptimizeShuttle avoids extra loops when optimizing statements with ternary expressions. Close apache#3518
jira: CALCITE-6109
When there are statements with ternary expressions in BlockBuilder#statements, the optimize method always returns true, causing the loop in the toBlock method to always be executed 10 times.
The reason is that when OptimizeShuttle traverses the statement, a new instance of TernaryExpression will always be created, regardless of whether the optimization is actually performed.