Skip to content

Commit

Permalink
Fix planner bug when mixing window functions and implicit coercions
Browse files Browse the repository at this point in the history
A query that has:
- window functions
- implicit coercions
- wildcard field references (e.g., SELECT *)

Such as:

SELECT *, 1.0 * count(*) OVER () FROM (VALUES 1);

Fails during planning with the following error:

java.lang.IllegalStateException: No mapping for field '0'
	at com.google.common.base.Preconditions.checkState(Preconditions.java:197)
	at com.facebook.presto.sql.planner.TranslationMap.rewrite(TranslationMap.java:114)
	at com.facebook.presto.sql.planner.PlanBuilder.rewrite(PlanBuilder.java:72)
	at com.facebook.presto.sql.planner.QueryPlanner.project(QueryPlanner.java:210)
	at com.facebook.presto.sql.planner.QueryPlanner.visitQuerySpecification(QueryPlanner.java:128)
	at com.facebook.presto.sql.planner.QueryPlanner.visitQuerySpecification(QueryPlanner.java:70)
	at com.facebook.presto.sql.tree.QuerySpecification.accept(QuerySpecification.java:98)
	at com.facebook.presto.sql.tree.AstVisitor.process(AstVisitor.java:22)
	at com.facebook.presto.sql.planner.RelationPlanner.visitQuerySpecification(RelationPlanner.java:416)
	at com.facebook.presto.sql.planner.RelationPlanner.visitQuerySpecification(RelationPlanner.java:97)
	at com.facebook.presto.sql.tree.QuerySpecification.accept(QuerySpecification.java:98)
	at com.facebook.presto.sql.tree.AstVisitor.process(AstVisitor.java:22)
	at com.facebook.presto.sql.planner.QueryPlanner.planQueryBody(QueryPlanner.java:141)
	at com.facebook.presto.sql.planner.QueryPlanner.visitQuery(QueryPlanner.java:97)
	at com.facebook.presto.sql.planner.QueryPlanner.visitQuery(QueryPlanner.java:70)
	at com.facebook.presto.sql.tree.Query.accept(Query.java:80)
	at com.facebook.presto.sql.tree.AstVisitor.process(AstVisitor.java:22)
	at com.facebook.presto.sql.planner.RelationPlanner.visitQuery(RelationPlanner.java:403)
	at com.facebook.presto.sql.planner.RelationPlanner.visitQuery(RelationPlanner.java:97)
	at com.facebook.presto.sql.tree.Query.accept(Query.java:80)
	at com.facebook.presto.sql.tree.AstVisitor.process(AstVisitor.java:22)
	at com.facebook.presto.sql.planner.LogicalPlanner.createRelationPlan(LogicalPlanner.java:176)
	at com.facebook.presto.sql.planner.LogicalPlanner.plan(LogicalPlanner.java:80)
  • Loading branch information
martint committed Mar 6, 2015
1 parent c82e643 commit 6a88a98
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ private PlanBuilder explicitCoercionFields(PlanBuilder subPlan, Iterable<FieldOr
private PlanBuilder explicitCoercionSymbols(PlanBuilder subPlan, Iterable<Symbol> alreadyCoerced, Iterable<? extends Expression> uncoerced)
{
TranslationMap translations = new TranslationMap(subPlan.getRelationPlan(), analysis);
translations.copyMappingsFrom(subPlan.getTranslations());
ImmutableMap.Builder<Symbol, Expression> projections = ImmutableMap.builder();

projections.putAll(coerce(uncoerced, subPlan, translations));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,13 @@ public void testSameInputToAggregates()
assertQuery("SELECT max(a), max(b) FROM (SELECT custkey a, custkey b FROM orders) x");
}

@Test
public void testWindowFunctionWithImplicitCoercion()
throws Exception
{
assertQuery("SELECT *, 1.0 * sum(x) OVER () FROM (VALUES 1) t(x)", "SELECT 1, 1.0");
}

@SuppressWarnings("PointlessArithmeticExpression")
@Test
public void testWindowFunctionsExpressions()
Expand Down

0 comments on commit 6a88a98

Please sign in to comment.