You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "AMashenkov (via GitHub)" <gi...@apache.org> on 2024/04/01 17:00:07 UTC
[PR] IGNITE-21897 Sql. Invalid collation passthrough for sorted aggregates [ignite-3]
AMashenkov opened a new pull request, #3537:
URL: https://github.com/apache/ignite-3/pull/3537
Thank you for submitting the pull request.
To streamline the review process of the patch and ensure better code quality
we ask both an author and a reviewer to verify the following:
### The Review Checklist
- [ ] **Formal criteria:** TC status, codestyle, mandatory documentation. Also make sure to complete the following:
\- There is a single JIRA ticket related to the pull request.
\- The web-link to the pull request is attached to the JIRA ticket.
\- The JIRA ticket has the Patch Available state.
\- The description of the JIRA ticket explains WHAT was made, WHY and HOW.
\- The pull request title is treated as the final commit message. The following pattern must be used: IGNITE-XXXX Change summary where XXXX - number of JIRA issue.
- [ ] **Design:** new code conforms with the design principles of the components it is added to.
- [ ] **Patch quality:** patch cannot be split into smaller pieces, its size must be reasonable.
- [ ] **Code quality:** code is clean and readable, necessary developer documentation is added if needed.
- [ ] **Tests code quality:** test set covers positive/negative scenarios, happy/edge cases. Tests are effective in terms of execution time and resources.
### Notes
- [Apache Ignite Coding Guidelines](https://cwiki.apache.org/confluence/display/IGNITE/Java+Code+Style+Guide)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21897 Sql. Invalid collation passthrough for sorted aggregates [ignite-3]
Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #3537:
URL: https://github.com/apache/ignite-3/pull/3537#discussion_r1547317500
##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rel/agg/IgniteSortAggregateBase.java:
##########
@@ -77,22 +80,42 @@ default Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(
RelTraitSet nodeTraits, List<RelTraitSet> inputTraits
) {
RelCollation required = TraitUtils.collation(nodeTraits);
- ImmutableBitSet requiredKeys = ImmutableBitSet.of(required.getKeys());
- RelCollation collation;
- if (getGroupSet().contains(requiredKeys)) {
- List<RelFieldCollation> newCollationFields = new ArrayList<>(getGroupSet().cardinality());
- newCollationFields.addAll(required.getFieldCollations());
-
- ImmutableBitSet keysLeft = getGroupSet().except(requiredKeys);
+ if (getGroupSet().isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, RelCollations.EMPTY); // Erase collation for a single group.
+ } else if (required.getFieldCollations().isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, TraitUtils.createCollation(getGroupSet().asList())); // No match.
+ }
- keysLeft.forEach(fieldIdx -> newCollationFields.add(TraitUtils.createFieldCollation(fieldIdx)));
+ ImmutableBitSet groupingColumns = ImmutableBitSet.range(getGroupSet().cardinality());
+ IntList prefix = maxPrefix(required.getKeys(), groupingColumns.asSet());
- collation = RelCollations.of(newCollationFields);
- } else {
- collation = TraitUtils.createCollation(getGroupSet().toList());
+ if (prefix.isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, TraitUtils.createCollation(getGroupSet().asList())); // No match.
}
+ // Rearrange grouping columns to satisfy required collation (as much as possible).
+ List<RelFieldCollation> newCollationColumns = new ArrayList<>(getGroupSet().cardinality());
+ Mapping mapping = Commons.trimmingMapping(groupingColumns.cardinality(), groupingColumns);
+
+ // Add required columns first.
+ prefix.intStream().map(mapping::getTarget)
+ .forEach(fieldIdx -> newCollationColumns.add(TraitUtils.createFieldCollation(fieldIdx)));
+
+ // Then add missed grouping columns.
+ groupingColumns.asList().stream()
+ .filter(not(prefix::contains))
+ .map(mapping::getTarget)
+ .forEach(fieldIdx -> newCollationColumns.add(TraitUtils.createFieldCollation(fieldIdx)));
+
+ return passThroughCollation(nodeTraits, inputTraits, RelCollations.of(newCollationColumns));
+ }
+
+ private static Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(
+ RelTraitSet nodeTraits,
+ List<RelTraitSet> inputTraits,
+ RelCollation collation
+ ) {
return Pair.of(nodeTraits.replace(collation),
List.of(inputTraits.get(0).replace(collation)));
}
Review Comment:
in general, such propagation (when you use the same collation for input and output) is not correct. Here is an example:
assume, I have table `T` with tuple `(a, b, c, d)`. In case of query like `GROUP BY b, d ORDER BY b, d`, the input collation should be `[1, 3]`, while the node (or, in other words, output) collation will be `[0, 1]`. Such transformation caused by the fact, that aggregate node always puts the grouping column in the very beginning of the resulting tuple
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21897 Sql. Invalid collation passthrough for sorted aggregates [ignite-3]
Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #3537:
URL: https://github.com/apache/ignite-3/pull/3537#discussion_r1548121802
##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rel/agg/IgniteSortAggregateBase.java:
##########
@@ -77,22 +80,42 @@ default Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(
RelTraitSet nodeTraits, List<RelTraitSet> inputTraits
) {
RelCollation required = TraitUtils.collation(nodeTraits);
- ImmutableBitSet requiredKeys = ImmutableBitSet.of(required.getKeys());
- RelCollation collation;
- if (getGroupSet().contains(requiredKeys)) {
- List<RelFieldCollation> newCollationFields = new ArrayList<>(getGroupSet().cardinality());
- newCollationFields.addAll(required.getFieldCollations());
-
- ImmutableBitSet keysLeft = getGroupSet().except(requiredKeys);
+ if (getGroupSet().isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, RelCollations.EMPTY); // Erase collation for a single group.
+ } else if (required.getFieldCollations().isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, TraitUtils.createCollation(getGroupSet().asList())); // No match.
+ }
- keysLeft.forEach(fieldIdx -> newCollationFields.add(TraitUtils.createFieldCollation(fieldIdx)));
+ ImmutableBitSet groupingColumns = ImmutableBitSet.range(getGroupSet().cardinality());
+ IntList prefix = maxPrefix(required.getKeys(), groupingColumns.asSet());
- collation = RelCollations.of(newCollationFields);
- } else {
- collation = TraitUtils.createCollation(getGroupSet().toList());
+ if (prefix.isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, TraitUtils.createCollation(getGroupSet().asList())); // No match.
}
+ // Rearrange grouping columns to satisfy required collation (as much as possible).
+ List<RelFieldCollation> newCollationColumns = new ArrayList<>(getGroupSet().cardinality());
+ Mapping mapping = Commons.trimmingMapping(groupingColumns.cardinality(), groupingColumns);
+
+ // Add required columns first.
+ prefix.intStream().map(mapping::getTarget)
+ .forEach(fieldIdx -> newCollationColumns.add(TraitUtils.createFieldCollation(fieldIdx)));
+
+ // Then add missed grouping columns.
+ groupingColumns.asList().stream()
+ .filter(not(prefix::contains))
+ .map(mapping::getTarget)
+ .forEach(fieldIdx -> newCollationColumns.add(TraitUtils.createFieldCollation(fieldIdx)));
+
+ return passThroughCollation(nodeTraits, inputTraits, RelCollations.of(newCollationColumns));
+ }
+
+ private static Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(
+ RelTraitSet nodeTraits,
+ List<RelTraitSet> inputTraits,
+ RelCollation collation
+ ) {
return Pair.of(nodeTraits.replace(collation),
List.of(inputTraits.get(0).replace(collation)));
}
Review Comment:
I've added more tests.
Unfortunately, in case, when there is Sort over SortAggregate, it is impossible to supply partial order (collation) from the aggregate node to help the upper Sort node. Because Sort is enforcer and erase collations on pass-through.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21897 Sql. Invalid collation passthrough for sorted aggregates [ignite-3]
Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #3537:
URL: https://github.com/apache/ignite-3/pull/3537#discussion_r1547573774
##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rel/agg/IgniteSortAggregateBase.java:
##########
@@ -77,22 +80,42 @@ default Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(
RelTraitSet nodeTraits, List<RelTraitSet> inputTraits
) {
RelCollation required = TraitUtils.collation(nodeTraits);
- ImmutableBitSet requiredKeys = ImmutableBitSet.of(required.getKeys());
- RelCollation collation;
- if (getGroupSet().contains(requiredKeys)) {
- List<RelFieldCollation> newCollationFields = new ArrayList<>(getGroupSet().cardinality());
- newCollationFields.addAll(required.getFieldCollations());
-
- ImmutableBitSet keysLeft = getGroupSet().except(requiredKeys);
+ if (getGroupSet().isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, RelCollations.EMPTY); // Erase collation for a single group.
+ } else if (required.getFieldCollations().isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, TraitUtils.createCollation(getGroupSet().asList())); // No match.
+ }
- keysLeft.forEach(fieldIdx -> newCollationFields.add(TraitUtils.createFieldCollation(fieldIdx)));
+ ImmutableBitSet groupingColumns = ImmutableBitSet.range(getGroupSet().cardinality());
+ IntList prefix = maxPrefix(required.getKeys(), groupingColumns.asSet());
- collation = RelCollations.of(newCollationFields);
- } else {
- collation = TraitUtils.createCollation(getGroupSet().toList());
+ if (prefix.isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, TraitUtils.createCollation(getGroupSet().asList())); // No match.
}
+ // Rearrange grouping columns to satisfy required collation (as much as possible).
+ List<RelFieldCollation> newCollationColumns = new ArrayList<>(getGroupSet().cardinality());
+ Mapping mapping = Commons.trimmingMapping(groupingColumns.cardinality(), groupingColumns);
+
+ // Add required columns first.
+ prefix.intStream().map(mapping::getTarget)
+ .forEach(fieldIdx -> newCollationColumns.add(TraitUtils.createFieldCollation(fieldIdx)));
+
+ // Then add missed grouping columns.
+ groupingColumns.asList().stream()
+ .filter(not(prefix::contains))
+ .map(mapping::getTarget)
+ .forEach(fieldIdx -> newCollationColumns.add(TraitUtils.createFieldCollation(fieldIdx)));
+
+ return passThroughCollation(nodeTraits, inputTraits, RelCollations.of(newCollationColumns));
+ }
+
+ private static Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(
+ RelTraitSet nodeTraits,
+ List<RelTraitSet> inputTraits,
+ RelCollation collation
+ ) {
return Pair.of(nodeTraits.replace(collation),
List.of(inputTraits.get(0).replace(collation)));
}
Review Comment:
Got it. Fixed.
Also, I've found I used default order (asc/desc) for prefix columns instead of preserving desired collation.
Seems, we need more tests on collation propagation.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21897 Sql. Invalid collation passthrough for sorted aggregates [ignite-3]
Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #3537:
URL: https://github.com/apache/ignite-3/pull/3537#discussion_r1548121802
##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rel/agg/IgniteSortAggregateBase.java:
##########
@@ -77,22 +80,42 @@ default Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(
RelTraitSet nodeTraits, List<RelTraitSet> inputTraits
) {
RelCollation required = TraitUtils.collation(nodeTraits);
- ImmutableBitSet requiredKeys = ImmutableBitSet.of(required.getKeys());
- RelCollation collation;
- if (getGroupSet().contains(requiredKeys)) {
- List<RelFieldCollation> newCollationFields = new ArrayList<>(getGroupSet().cardinality());
- newCollationFields.addAll(required.getFieldCollations());
-
- ImmutableBitSet keysLeft = getGroupSet().except(requiredKeys);
+ if (getGroupSet().isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, RelCollations.EMPTY); // Erase collation for a single group.
+ } else if (required.getFieldCollations().isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, TraitUtils.createCollation(getGroupSet().asList())); // No match.
+ }
- keysLeft.forEach(fieldIdx -> newCollationFields.add(TraitUtils.createFieldCollation(fieldIdx)));
+ ImmutableBitSet groupingColumns = ImmutableBitSet.range(getGroupSet().cardinality());
+ IntList prefix = maxPrefix(required.getKeys(), groupingColumns.asSet());
- collation = RelCollations.of(newCollationFields);
- } else {
- collation = TraitUtils.createCollation(getGroupSet().toList());
+ if (prefix.isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, TraitUtils.createCollation(getGroupSet().asList())); // No match.
}
+ // Rearrange grouping columns to satisfy required collation (as much as possible).
+ List<RelFieldCollation> newCollationColumns = new ArrayList<>(getGroupSet().cardinality());
+ Mapping mapping = Commons.trimmingMapping(groupingColumns.cardinality(), groupingColumns);
+
+ // Add required columns first.
+ prefix.intStream().map(mapping::getTarget)
+ .forEach(fieldIdx -> newCollationColumns.add(TraitUtils.createFieldCollation(fieldIdx)));
+
+ // Then add missed grouping columns.
+ groupingColumns.asList().stream()
+ .filter(not(prefix::contains))
+ .map(mapping::getTarget)
+ .forEach(fieldIdx -> newCollationColumns.add(TraitUtils.createFieldCollation(fieldIdx)));
+
+ return passThroughCollation(nodeTraits, inputTraits, RelCollations.of(newCollationColumns));
+ }
+
+ private static Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(
+ RelTraitSet nodeTraits,
+ List<RelTraitSet> inputTraits,
+ RelCollation collation
+ ) {
return Pair.of(nodeTraits.replace(collation),
List.of(inputTraits.get(0).replace(collation)));
}
Review Comment:
I've added more tests.
Unfortunately, in case, when there is Sort over SortAggregate, it is impossible to supply partial order (collation) from the aggregate node to help upper Sort node. Because Sort is enforcer and erase collations on pass-through.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21897 Sql. Invalid collation passthrough for sorted aggregates [ignite-3]
Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov merged PR #3537:
URL: https://github.com/apache/ignite-3/pull/3537
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21897 Sql. Invalid collation passthrough for sorted aggregates [ignite-3]
Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #3537:
URL: https://github.com/apache/ignite-3/pull/3537#discussion_r1548121802
##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rel/agg/IgniteSortAggregateBase.java:
##########
@@ -77,22 +80,42 @@ default Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(
RelTraitSet nodeTraits, List<RelTraitSet> inputTraits
) {
RelCollation required = TraitUtils.collation(nodeTraits);
- ImmutableBitSet requiredKeys = ImmutableBitSet.of(required.getKeys());
- RelCollation collation;
- if (getGroupSet().contains(requiredKeys)) {
- List<RelFieldCollation> newCollationFields = new ArrayList<>(getGroupSet().cardinality());
- newCollationFields.addAll(required.getFieldCollations());
-
- ImmutableBitSet keysLeft = getGroupSet().except(requiredKeys);
+ if (getGroupSet().isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, RelCollations.EMPTY); // Erase collation for a single group.
+ } else if (required.getFieldCollations().isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, TraitUtils.createCollation(getGroupSet().asList())); // No match.
+ }
- keysLeft.forEach(fieldIdx -> newCollationFields.add(TraitUtils.createFieldCollation(fieldIdx)));
+ ImmutableBitSet groupingColumns = ImmutableBitSet.range(getGroupSet().cardinality());
+ IntList prefix = maxPrefix(required.getKeys(), groupingColumns.asSet());
- collation = RelCollations.of(newCollationFields);
- } else {
- collation = TraitUtils.createCollation(getGroupSet().toList());
+ if (prefix.isEmpty()) {
+ return passThroughCollation(nodeTraits, inputTraits, TraitUtils.createCollation(getGroupSet().asList())); // No match.
}
+ // Rearrange grouping columns to satisfy required collation (as much as possible).
+ List<RelFieldCollation> newCollationColumns = new ArrayList<>(getGroupSet().cardinality());
+ Mapping mapping = Commons.trimmingMapping(groupingColumns.cardinality(), groupingColumns);
+
+ // Add required columns first.
+ prefix.intStream().map(mapping::getTarget)
+ .forEach(fieldIdx -> newCollationColumns.add(TraitUtils.createFieldCollation(fieldIdx)));
+
+ // Then add missed grouping columns.
+ groupingColumns.asList().stream()
+ .filter(not(prefix::contains))
+ .map(mapping::getTarget)
+ .forEach(fieldIdx -> newCollationColumns.add(TraitUtils.createFieldCollation(fieldIdx)));
+
+ return passThroughCollation(nodeTraits, inputTraits, RelCollations.of(newCollationColumns));
+ }
+
+ private static Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(
+ RelTraitSet nodeTraits,
+ List<RelTraitSet> inputTraits,
+ RelCollation collation
+ ) {
return Pair.of(nodeTraits.replace(collation),
List.of(inputTraits.get(0).replace(collation)));
}
Review Comment:
I've added more tests.
Unfortunately, in case, when there is Sort over SortAggregate, it is impossible to supply partial sort from the aggregate node to help upper Sort node. Because Sort is enforcer and erase collations on pass-through.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org