-
Notifications
You must be signed in to change notification settings - Fork 25.2k
SQL: incorrect column resolution from parameter #56013
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
Labels
Comments
palesz
pushed a commit
to palesz/elasticsearch
that referenced
this issue
Oct 15, 2020
Parameters in the SQL query marked as `?` turn into an `UnresolvedAlias` in the parsed tree, aliasing (without a name) the `Literal` representing the passed in parameter value. For example in the query `SELECT ? FROM test` with a single integer (value = 100) parameter the ? turns into an `UnresolvedAlias (child=Literal(100))` or if we try to print it back into SQL, `SELECT 100 as ? FROM test`. A problem arises when multiple `?` is in the query, since the `SELECT ?, ? FROM test` query with integer parameters (value=100, 200) will turn into `SELECT 100 as ?, 200 as ? FROM test`. You guessed it correctly, name conflict is the underlying issue. The `UnresolvedAlias`es will be assigned the same name and are turned into `Alias(name="?")` objects by the `Analyzer`. The problem ultimately comes down to the fact that `Alias`es in the `QueryFolder`. The `Alias`es eventually put into an `AttributeMap`, but not directly. First the collection of `Alias`es gets turned into a `java.util.LinkedHashMap<Attribute, Expression>` which later get's turned an `AttributeMap`. The key in the map gets created using `Alias .toAttribute()`, but the `LinkedHashMap<>` uses the `Attribute.equals()` and `Attribute.hashCode()` methods which only take into the account the `name` field of the attribute vs `Attribute.semanticEquals()` and `Attribute.semanticHash()` that take into account the `name` and `id` fields (used by the `AttributeMap`). The name-based equality check in the `LinkedHashMap` essentially de-duplicates the `Alias`es based on the name and that is why one of the attributes cannot be resolved (resolution uses both `name` and `id`). This fix deprecates the `AttributeMap(Map)` constructor, makes sure that the `AttributeMap`s are populated from `Stream`s or `Iterable`s directly, so no deduplication happens based on the names of the `Attribute`s. Note: The `SELECT 100 as ?, 200 as ? FROM test` query will result in two columns named as `?` in the results. Alternatively I could have just generated different names for the `UnresolvedAlias` objects, but that would just hide the above mentioned bug in the code. Fix elastic#56013
palesz
pushed a commit
to palesz/elasticsearch
that referenced
this issue
Oct 27, 2020
For example in the query `SELECT ? FROM test` with a single integer (value = 100) parameter the ? turns into an `UnresolvedAlias (child=Literal(100))` or if we try to print it back into SQL, `SELECT 100 as ? FROM test`. A problem arises when multiple `?` is in the query with the same type. For example the `SELECT ?, ? FROM test` query with integer parameters (value=100, 200) will turn into `SELECT 100 as ?, 200 as ? FROM test`. This commit changes the way aliases are assigned: the first unaliased parameter will stay `?`, but from the 2nd unaliased parameter literal a counter will be added to the alias (`?2`, `?3`, ...). Callout: This will introduce a change for the users: a query with multiple unaliased parameters (but each parameter with different types) successfully executed before and returned the results with multiple `?` columns (although must have accessed via index from the result set instead of alias). Fix elastic#56013
palesz
pushed a commit
that referenced
this issue
Nov 4, 2020
* SQL: Fix incorrect parameter resolution 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. Fix #56013
palesz
pushed a commit
to palesz/elasticsearch
that referenced
this issue
Nov 4, 2020
* SQL: Fix incorrect parameter resolution 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. Fix elastic#56013
palesz
pushed a commit
to palesz/elasticsearch
that referenced
this issue
Nov 4, 2020
* SQL: Fix incorrect parameter resolution 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. Fix elastic#56013
palesz
pushed a commit
that referenced
this issue
Nov 5, 2020
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
palesz
pushed a commit
that referenced
this issue
Nov 5, 2020
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
Detailed description of the root cause can be found here: #63710 (comment) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
will fail with
Unknown output attribute ?{r}#9796
.Leaving the index out or changing the parameter type (i.e. no type repetition in the list) will work as expected.
Stacktrace:
The text was updated successfully, but these errors were encountered: