-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Summary of the issue and the root cause: ``` (1) SELECT 100, 100 -> success (2) SELECT ?, ? (with params: 100, 100) -> success (3) SELECT 100, 100 FROM test -> Unknown output attribute exception for the second 100 (4) SELECT ?, ? FROM test (params: 100, 100) -> Unknown output attribute exception for the second ? (5) SELECT field1 as "x", field1 as "x" FROM test -> Unknown output attribute exception for the second "x" ``` There are two separate issues at play here: 1. Construction of `AttributeMap`s keeps only one of the `Attribute`s with the same name even if the `id`s are different (see the `AttributeMapTests` in this PR). This should be fixed no matter what, we should not overwrite attributes with one another during the construction of the `AttributeMap`. 2. The `id` on the `Alias`es is not the same in case the `Alias`es have the same `name` and same `child` It was considered to simpy fix the second issue by just reassigning the same `id`s to the `Alias`es with the same name and child, but it would not solve the `unknown output attribute exception` (see notes below). This PR covers the fix for the first issue. Relates to #56013
- Loading branch information
Andras Palinkas
authored
Nov 5, 2020
1 parent
da0a969
commit 3d8e17f
Showing
7 changed files
with
236 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ParamLiteralTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.sql.parser; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xpack.ql.expression.Literal; | ||
import org.elasticsearch.xpack.ql.expression.NamedExpression; | ||
import org.elasticsearch.xpack.ql.expression.UnresolvedAlias; | ||
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.LessThan; | ||
import org.elasticsearch.xpack.ql.plan.logical.Filter; | ||
import org.elasticsearch.xpack.ql.plan.logical.LogicalPlan; | ||
import org.elasticsearch.xpack.ql.plan.logical.Project; | ||
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.elasticsearch.xpack.ql.type.DateUtils.UTC; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.everyItem; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.startsWith; | ||
|
||
public class ParamLiteralTests extends ESTestCase { | ||
|
||
private final SqlParser parser = new SqlParser(); | ||
|
||
private LogicalPlan parse(String sql, SqlTypedParamValue... parameters) { | ||
return parser.createStatement(sql, Arrays.asList(parameters), UTC); | ||
} | ||
|
||
public void testMultipleParamLiteralsWithUnresolvedAliases() { | ||
LogicalPlan logicalPlan = parse("SELECT ?, ? FROM test", | ||
new SqlTypedParamValue("integer", 100), | ||
new SqlTypedParamValue("integer", 200) | ||
); | ||
List<? extends NamedExpression> projections = ((Project) logicalPlan.children().get(0)).projections(); | ||
assertThat(projections, everyItem(instanceOf(UnresolvedAlias.class))); | ||
assertThat(projections.get(0).toString(), startsWith("100 AS ?")); | ||
assertThat(projections.get(1).toString(), startsWith("200 AS ?")); | ||
} | ||
|
||
public void testMultipleParamLiteralsWithUnresolvedAliasesAndWhereClause() { | ||
LogicalPlan logicalPlan = parse("SELECT ?, ?, (?) FROM test WHERE 1 < ?", | ||
new SqlTypedParamValue("integer", 100), | ||
new SqlTypedParamValue("integer", 100), | ||
new SqlTypedParamValue("integer", 200), | ||
new SqlTypedParamValue("integer", 300) | ||
); | ||
Project project = (Project) logicalPlan.children().get(0); | ||
List<? extends NamedExpression> projections = project.projections(); | ||
assertThat(projections, everyItem(instanceOf(UnresolvedAlias.class))); | ||
assertThat(projections.get(0).toString(), startsWith("100 AS ?")); | ||
assertThat(projections.get(1).toString(), startsWith("100 AS ?")); | ||
assertThat(projections.get(2).toString(), startsWith("200 AS ?")); | ||
assertThat(project.children().get(0), instanceOf(Filter.class)); | ||
Filter filter = (Filter) project.children().get(0); | ||
assertThat(filter.condition(), instanceOf(LessThan.class)); | ||
LessThan condition = (LessThan) filter.condition(); | ||
assertThat(condition.left(), instanceOf(Literal.class)); | ||
assertThat(condition.right(), instanceOf(Literal.class)); | ||
assertThat(((Literal)condition.right()).value(), equalTo(300)); | ||
} | ||
|
||
public void testParamLiteralsWithUnresolvedAliasesAndMixedTypes() { | ||
LogicalPlan logicalPlan = parse("SELECT ?, ? FROM test", | ||
new SqlTypedParamValue("integer", 100), | ||
new SqlTypedParamValue("text", "100") | ||
); | ||
List<? extends NamedExpression> projections = ((Project) logicalPlan.children().get(0)).projections(); | ||
assertThat(projections, everyItem(instanceOf(UnresolvedAlias.class))); | ||
assertThat(projections.get(0).toString(), startsWith("100 AS ?")); | ||
assertThat(projections.get(1).toString(), startsWith("100 AS ?")); | ||
} | ||
|
||
public void testParamLiteralsWithResolvedAndUnresolvedAliases() { | ||
LogicalPlan logicalPlan = parse("SELECT ?, ? as x, ? FROM test", | ||
new SqlTypedParamValue("integer", 100), | ||
new SqlTypedParamValue("integer", 200), | ||
new SqlTypedParamValue("integer", 300) | ||
); | ||
List<? extends NamedExpression> projections = ((Project) logicalPlan.children().get(0)).projections(); | ||
assertThat(projections.get(0).toString(), startsWith("100 AS ?")); | ||
assertThat(projections.get(1).toString(), startsWith("200 AS x#"));; | ||
assertThat(projections.get(2).toString(), startsWith("300 AS ?"));; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters