Skip to content

Commit

Permalink
SQL: Do not resolve self-referencing aliases (elastic#62382)
Browse files Browse the repository at this point in the history
Prevent the analyzer for trying to resolve aliases on expressions that
reference themselves (or fields within themselves) as that causes
infinite recursion.

Fix elastic#62296

(cherry picked from commit 021d278)
  • Loading branch information
costin committed Sep 15, 2020
1 parent 9ac4ee9 commit b2e85d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ private Expression replaceAliases(Expression condition, List<? extends NamedExpr
boolean qualified = u.qualifier() != null;
for (Alias alias : aliases) {
// don't replace field with their own aliases (it creates infinite cycles)
if (u != alias.child() &&
if (alias.anyMatch(e -> e == u) == false &&
(qualified ?
Objects.equals(alias.qualifiedName(), u.qualifiedName()) :
Objects.equals(alias.name(), u.name()))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package org.elasticsearch.xpack.sql.analysis.analyzer;

import java.util.stream.Collectors;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.ql.QlIllegalArgumentException;
import org.elasticsearch.xpack.ql.expression.Alias;
Expand All @@ -29,6 +28,7 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.elasticsearch.xpack.ql.type.DataTypes.BOOLEAN;
import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD;
Expand Down Expand Up @@ -277,4 +277,27 @@ public void testGroupByAmbiguity() {
+ "matches any of [line 1:37 [m], line 1:55 [m]]",
ex.getMessage());
}

public void testFunctionOverNonExistingFieldAsArgumentAndSameAlias() throws Exception {
Map<String, EsField> mapping = TypesTests.loadMapping("mapping-basic.json");
EsIndex index = new EsIndex("test", mapping);
getIndexResult = IndexResolution.valid(index);
analyzer = new Analyzer(SqlTestUtils.TEST_CFG, functionRegistry, getIndexResult, verifier);

VerificationException ex = expectThrows(VerificationException.class, () ->
plan("SELECT sum(missing) AS missing FROM test WHERE missing = 0"));
assertEquals("Found 1 problem\nline 1:12: Unknown column [missing]", ex.getMessage());
}

public void testFunctionWithExpressionOverNonExistingFieldAsArgumentAndSameAlias() throws Exception {
Map<String, EsField> mapping = TypesTests.loadMapping("mapping-basic.json");
EsIndex index = new EsIndex("test", mapping);
getIndexResult = IndexResolution.valid(index);
analyzer = new Analyzer(SqlTestUtils.TEST_CFG, functionRegistry, getIndexResult, verifier);

VerificationException ex = expectThrows(VerificationException.class, () ->
plan("SELECT LENGTH(CONCAT(missing, 'x')) + 1 AS missing FROM test WHERE missing = 0"));
assertEquals("Found 1 problem\nline 1:22: Unknown column [missing]", ex.getMessage());
}

}

0 comments on commit b2e85d5

Please sign in to comment.