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 issue with folding of CASE/IIF #49449

Merged
merged 3 commits into from
Nov 22, 2019
Merged

Fix issue with folding of CASE/IIF #49449

merged 3 commits into from
Nov 22, 2019

Conversation

matriv
Copy link
Contributor

@matriv matriv commented Nov 21, 2019

Add extra checks to prevent ConstantFolding rule to try to fold
the CASE/IIF functions early before the SimplifyCase rule gets applied.

Fixes: #49387

Add extra checks to prevent ConstantFolding rule to try to fold
the CASE/IIF functions early before the SimplifyCase rule gets applied.

Fixes: elastic#49387
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (:Search/SQL)

Copy link
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@costin costin left a comment

Choose a reason for hiding this comment

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

I consider it a semantic leak when expression classes become aware of runtime rules.
It indicates that either the expression tries to do too much (in this case folding) or that the relationship between the rules is not correct (either the rules need to be simplified or the order between them changed).

The comment refers to a rule but it seems to me that the folding is eager?
Or maybe SimplifyCase be moved before ConstantFolding - I couldn't tell from the test case what is the current issue...

@matriv
Copy link
Contributor Author

matriv commented Nov 21, 2019

@costin The issue was that the Case was answering foldable() -> true even if the result of the condition (folded to true) was un-foldable. The tests test that the case is not foldable before the SimplifyCase rule takes place.

I thought of changing the order but I personally believe this approach can become easier a future confusion than my current solution. The current code just tests that the result or elseResult (depending on the TRUE/FALSE of the condition) is also foldable and (at least in my mind) doesn't appear that connected to the rules and their order. On the contrary the previous state seems to depend more on the ordering of the rules in the Optimizer, and the comment on the method (comes from a previous suggestion of yours) clearly states that it's based on the fact that the SimplifyCase rule has been applied.

If you think changing the orders of the rules in the Optimizer seems a better approach, I'm happy to proceed with that.

@matriv
Copy link
Contributor Author

matriv commented Nov 21, 2019

@costin
To give an example for supporting the current solution:

public void testSimplifyIif_ConditionFalse_NonFoldableResult() {
    SimplifyCase rule = new SimplifyCase();
    Iif iif = new Iif(EMPTY, new Equals(EMPTY, ONE, TWO), Literal.of(EMPTY, "foo"), getFieldAttribute("myField"));
    assertFalse(iif.foldable()); --> previously here it was TRUE which logically is wrong.

    Expression e = rule.rule(iif);
    assertEquals(Iif.class, e.getClass());
    iif = (Iif) e;
    assertEquals(0, iif.conditions().size());
    assertFalse(iif.foldable());
    assertEquals("myField", Expressions.name(iif.elseResult()));
}

(please check the comment)

Copy link
Member

@costin costin left a comment

Choose a reason for hiding this comment

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

LGTM - the comment about the rules threw me off hence why it should be removed.

return true;
}
if (conditions.size() == 1 && conditions.get(0).condition().foldable()) {
// Need to prevent ConstantFolding rule to get applied before SimplifyCase rule
Copy link
Member

Choose a reason for hiding this comment

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

I find the comment misleading as it implies the rules order is incorrect - called in a different order the rules might affect fold but the matter is, foldable is incorrect (which leads to the exception).

@matriv
Copy link
Contributor Author

matriv commented Nov 22, 2019

@elasticmachine run elasticsearch-ci/default-distro
@elasticmachine run elasticsearch-ci/bwc

@matriv
Copy link
Contributor Author

matriv commented Nov 22, 2019

@elasticmachine run elasticsearch-ci/packaging-sample-matrix

@matriv matriv merged commit f35c972 into elastic:master Nov 22, 2019
@matriv matriv deleted the fix-49387 branch November 22, 2019 14:28
@matriv matriv removed the v6.8.6 label Nov 22, 2019
matriv added a commit that referenced this pull request Nov 22, 2019
Add extra checks to prevent ConstantFolding rule to try to fold
the CASE/IIF functions early before the SimplifyCase rule gets applied.

Fixes: #49387

(cherry picked from commit f35c972)
matriv added a commit that referenced this pull request Nov 22, 2019
Add extra checks to prevent ConstantFolding rule to try to fold
the CASE/IIF functions early before the SimplifyCase rule gets applied.

Fixes: #49387

(cherry picked from commit f35c972)
matriv added a commit that referenced this pull request Nov 22, 2019
Add extra checks to prevent ConstantFolding rule to try to fold
the CASE/IIF functions early before the SimplifyCase rule gets applied.

Fixes: #49387

(cherry picked from commit f35c972)
@jpountz jpountz changed the title SQL: Fix issue with folding of CASE/IIF Fix issue with folding of CASE/IIF Dec 18, 2019
@jakelandis jakelandis removed the v8.0.0 label Jul 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SQL: "Should not fold expression" for conditional function
5 participants