Skip to content
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

Fix failure with repeated complex expressions in group by clause #4609

Merged
merged 2 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,15 @@ private GroupingSetsPlan planGroupingSets(PlanBuilder subPlan, QuerySpecificatio
groupingSetMappings.put(output, input);
}

ImmutableMap.Builder<Expression, Symbol> expressionsBuilder = ImmutableMap.builder();
Map<ScopeAware<Expression>, Symbol> complexExpressions = new HashMap<>();
for (Expression expression : groupingSetAnalysis.getComplexExpressions()) {
Symbol input = subPlan.translate(expression);
Symbol output = symbolAllocator.newSymbol(expression, analysis.getType(expression), "gid");
expressionsBuilder.put(expression, output);
groupingSetMappings.put(output, input);
if (!complexExpressions.containsKey(scopeAwareKey(expression, analysis, subPlan.getScope()))) {
Symbol input = subPlan.translate(expression);
Symbol output = symbolAllocator.newSymbol(expression, analysis.getType(expression), "gid");
complexExpressions.put(scopeAwareKey(expression, analysis, subPlan.getScope()), output);
groupingSetMappings.put(output, input);
}
}
Map<Expression, Symbol> complexExpressions = expressionsBuilder.build();

// For the purpose of "distinct", we need to canonicalize column references that may have varying
// syntactic forms (e.g., "t.a" vs "a"). Thus we need to enumerate grouping sets based on the underlying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Verify.verify;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static io.prestosql.sql.planner.ScopeAware.scopeAwareKey;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -110,12 +109,9 @@ public TranslationMap withScope(Scope scope, List<Symbol> fields)
return new TranslationMap(outerContext, scope, analysis, lambdaArguments, fields.toArray(new Symbol[0]), astToSymbols);
}

public TranslationMap withNewMappings(Map<Expression, Symbol> mappings, List<Symbol> fields)
public TranslationMap withNewMappings(Map<ScopeAware<Expression>, Symbol> mappings, List<Symbol> fields)
{
Map<ScopeAware<Expression>, Symbol> newMappings = mappings.entrySet().stream()
.collect(toImmutableMap(entry -> scopeAwareKey(entry.getKey(), analysis, scope), Map.Entry::getValue));

return new TranslationMap(outerContext, scope, analysis, lambdaArguments, fields, newMappings);
return new TranslationMap(outerContext, scope, analysis, lambdaArguments, fields, mappings);
}

public TranslationMap withAdditionalMappings(Map<ScopeAware<Expression>, Symbol> mappings)
Expand Down
80 changes: 80 additions & 0 deletions presto-main/src/test/java/io/prestosql/sql/query/TestGroupBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,84 @@ public void testCastDifferentCase()
"GROUP BY CAST(row(x) AS row(\"A\" bigint))"))
.hasMessage("line 1:8: 'CAST(ROW (x) AS ROW(\"a\" bigint))' must be an aggregate expression or appear in GROUP BY clause");
}

@Test
public void testDuplicateComplexExpressions()
{
assertThat(assertions.query(
"SELECT a + 1, a + 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY 1, 2"))
.matches("VALUES (2, 2)");

assertThat(assertions.query(
"SELECT 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY a + 1, a + 1"))
.matches("VALUES 1");

assertThat(assertions.query(
"SELECT 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY t.a + 1, a + 1"))
.matches("VALUES 1");

assertThat(assertions.query(
"SELECT 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY A + 1, a + 1"))
.matches("VALUES 1");

assertThat(assertions.query(
"SELECT 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY t.A + 1, a + 1"))
.matches("VALUES 1");

assertThat(assertions.query(
"SELECT a + 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY t.A + 1, 1"))
.matches("VALUES 2");
}

@Test
public void testReferenceWithMixedStyle()
{
assertThat(assertions.query(
"SELECT a + 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY A + 1"))
.matches("VALUES 2");

assertThat(assertions.query(
"SELECT a + 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY t.a + 1"))
.matches("VALUES 2");

assertThat(assertions.query(
"SELECT a + 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY t.A + 1"))
.matches("VALUES 2");

assertThat(assertions.query(
"SELECT t.a + 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY a + 1"))
.matches("VALUES 2");

assertThat(assertions.query(
"SELECT t.a + 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY A + 1"))
.matches("VALUES 2");

assertThat(assertions.query(
"SELECT t.a + 1 " +
"FROM (VALUES 1) t(a) " +
"GROUP BY t.A + 1"))
.matches("VALUES 2");
}
}