-
Notifications
You must be signed in to change notification settings - Fork 25.6k
SQL: fix NPE on ambiguous GROUP BY #59370
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
SQL: fix NPE on ambiguous GROUP BY #59370
Conversation
This reverts commit ae8ea65
…rkhovin/elasticsearch into fix_npe_on_ambiguous_group_by
…ch into fix/fix_npe_on_ambiguous_group_by
- remove Comparable implementations from Attribute and Location; - add ad-hoc comparator for sorting locations in ambiguity message; - remove added AttributeAlias class with Touple; - add code comment to explain issue with Location overwriting.
- remove Comparable implementations from Attribute and Location; - add ad-hoc comparator for sorting locations in ambiguity message; - remove added AttributeAlias class with Touple; - add code comment to explain issue with Location overwriting.
Fix copy&paste error in dedicated comparator used for sorting ambiguity location references. Slightly increase its readability.
|
Pinging @elastic/es-ql (:Query Languages/SQL) |
|
|
||
| public static AttributeMap<Expression> aliases(List<? extends NamedExpression> named) { | ||
| Map<Attribute, Expression> aliasMap = new LinkedHashMap<>(); | ||
| public static List<Tuple<Attribute, Expression>> aliases(List<? extends NamedExpression> named) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please replace this with a Tuple<Attribute, Expression>?
AttributeAlias replaced with Tuple<Attribute, Expression>?
| // only add the location if the match is univocal; b/c otherwise adding the location will overwrite any preexisting one | ||
| return handleSpecialFields(u, matches.get(0).withLocation(u.source()), allowCompound); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why removing it from here and adding it only if matches.size() == 1?
Attribute#withLocation() will "overwrite" any pre-existing location, which is not desirable if the existing locations need to be kept in order to be reported in an error message.
| .sorted((a, b) -> { | ||
| int lineDiff = a.sourceLocation().getLineNumber() - b.sourceLocation().getLineNumber(); | ||
| int colDiff = a.sourceLocation().getColumnNumber() - b.sourceLocation().getColumnNumber(); | ||
| return lineDiff != 0 ? lineDiff : (colDiff != 0 ? colDiff : a.qualifiedName().compareTo(b.qualifiedName())); | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the comparison implementation from here and use a custom comparator to sort the ambiguous attributes.
Implementations of Comparable removed.
| grouping = resolvedAliases.stream() | ||
| .filter(t -> t.v1().equals(maybeResolved)) | ||
| // use the matched expression (not its attribute) | ||
| .map(Tuple::v2) | ||
| .findAny() | ||
| .get(); // there should always be exactly one match |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imho, it would be better to use a for loop to increase readability.
The stream construction seems a bit more compact and readable IMO and inline with other usages (like the current Analyzer#resolveAgainstList()), but I can still change it if desired.
matriv
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* fix npe on ambiguous group by * add tests for aggregates and group by, add quotes to error message * add more cases for Group By ambiguity test * change error messages for field ambiguity * change collection aliases approach * add locations of attributes for ambiguous grouping error * Adress review comments - remove Comparable implementations from Attribute and Location; - add ad-hoc comparator for sorting locations in ambiguity message; - remove added AttributeAlias class with Touple; - add code comment to explain issue with Location overwriting. * Fix c&p error in location ref generation comparator Fix copy&paste error in dedicated comparator used for sorting ambiguity location references. Slightly increase its readability. Co-authored-by: Nikita Verkhovin <[email protected]> (cherry picked from commit 9ba70a3)
* fix npe on ambiguous group by * add tests for aggregates and group by, add quotes to error message * add more cases for Group By ambiguity test * change error messages for field ambiguity * change collection aliases approach * add locations of attributes for ambiguous grouping error * Adress review comments - remove Comparable implementations from Attribute and Location; - add ad-hoc comparator for sorting locations in ambiguity message; - remove added AttributeAlias class with Touple; - add code comment to explain issue with Location overwriting. * Fix c&p error in location ref generation comparator Fix copy&paste error in dedicated comparator used for sorting ambiguity location references. Slightly increase its readability. Co-authored-by: Nikita Verkhovin <[email protected]> (cherry picked from commit 9ba70a3)
* fix npe on ambiguous group by * add tests for aggregates and group by, add quotes to error message * add more cases for Group By ambiguity test * change error messages for field ambiguity * change collection aliases approach * add locations of attributes for ambiguous grouping error * Adress review comments - remove Comparable implementations from Attribute and Location; - add ad-hoc comparator for sorting locations in ambiguity message; - remove added AttributeAlias class with Touple; - add code comment to explain issue with Location overwriting. * Fix c&p error in location ref generation comparator Fix copy&paste error in dedicated comparator used for sorting ambiguity location references. Slightly increase its readability. Co-authored-by: Nikita Verkhovin <[email protected]> (cherry picked from commit 9ba70a3)
* fix npe on ambiguous group by * add tests for aggregates and group by, add quotes to error message * add more cases for Group By ambiguity test * change error messages for field ambiguity * change collection aliases approach * add locations of attributes for ambiguous grouping error * Adress review comments - remove Comparable implementations from Attribute and Location; - add ad-hoc comparator for sorting locations in ambiguity message; - remove added AttributeAlias class with Touple; - add code comment to explain issue with Location overwriting. * Fix c&p error in location ref generation comparator Fix copy&paste error in dedicated comparator used for sorting ambiguity location references. Slightly increase its readability. Co-authored-by: Nikita Verkhovin <[email protected]> (cherry picked from commit 9ba70a3)
This PR fixes a NPE occurring when an ambiguous alias is used for grouping.
Continues #56489.
Fixes #46396.
Co-authored-by: Nikita Verkhovin [email protected]