diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java index 868a922d4f3..2cf91b6687f 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java @@ -867,7 +867,7 @@ public class Analyzer extends RuleExecutor { 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()))) { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java index e17b35c58b2..7b8582dcbc3 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java @@ -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; @@ -29,6 +28,7 @@ import org.elasticsearch.xpack.sql.stats.Metrics; 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; @@ -277,4 +277,27 @@ public class FieldAttributeTests extends ESTestCase { + "matches any of [line 1:37 [m], line 1:55 [m]]", ex.getMessage()); } + + public void testFunctionOverNonExistingFieldAsArgumentAndSameAlias() throws Exception { + Map 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 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()); + } + }