Skip to content

Conversation

@bpintea
Copy link
Contributor

@bpintea bpintea commented Jul 11, 2020

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]

@bpintea bpintea added the >bug label Jul 11, 2020
bpintea added 3 commits July 11, 2020 16:57
- 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.
@bpintea bpintea marked this pull request as ready for review July 13, 2020 08:26
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-ql (:Query Languages/SQL)

@elasticmachine elasticmachine added the Team:QL (Deprecated) Meta label for query languages team label Jul 13, 2020
@bpintea bpintea requested review from astefan, costin and matriv July 13, 2020 08:26

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) {
Copy link
Contributor Author

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>?

Comment on lines +195 to +196
// 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);
Copy link
Contributor Author

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.

Comment on lines +200 to +204
.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()));
})
Copy link
Contributor Author

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.

Comment on lines +351 to +356
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
Copy link
Contributor Author

@bpintea bpintea Jul 13, 2020

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.

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bpintea bpintea merged commit 9ba70a3 into elastic:master Jul 20, 2020
@bpintea bpintea deleted the fix/fix_npe_on_ambiguous_group_by branch July 20, 2020 14:46
bpintea added a commit to bpintea/elasticsearch that referenced this pull request Jul 29, 2020
* 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)
bpintea added a commit to bpintea/elasticsearch that referenced this pull request Jul 29, 2020
* 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)
@bpintea bpintea added v7.9.1 and removed v7.9.0 labels Jul 29, 2020
bpintea added a commit that referenced this pull request Jul 29, 2020
* 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)
bpintea added a commit that referenced this pull request Jul 29, 2020
* 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL: NPE when using the same alias for a projection and an aggregate and GROUPed BY

5 participants