Refactor SimplifyExpression::rewrite for use outside of Rule
*/
package com.facebook.presto.sql.planner.iterative.rule;
+import com.facebook.presto.Session;
import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.sql.parser.SqlParser;
import com.facebook.presto.sql.planner.ExpressionInterpreter;
import com.facebook.presto.sql.planner.LiteralInterpreter;
import com.facebook.presto.sql.planner.NoOpSymbolResolver;
+import com.facebook.presto.sql.planner.SymbolAllocator;
import com.facebook.presto.sql.planner.iterative.Rule;
import com.facebook.presto.sql.planner.iterative.RuleSet;
import com.facebook.presto.sql.planner.iterative.rule.ExpressionRewriteRuleSet.FilterExpressionRewrite;
@@ -53,7 +55,7 @@ public class SimplifyExpressions
new JoinExpressionRewrite(this::rewrite),
new ValuesExpressionRewrite(this::rewrite)); // ApplyNode and AggregationNode are not supported, because ExpressionInterpreter doesn't support them
- static Expression rewrite(Expression expression, Rule.Context context, Metadata metadata, SqlParser sqlParser)
+ public static Expression rewrite(Expression expression, Session session, SymbolAllocator symbolAllocator, Metadata metadata, SqlParser sqlParser)
{
requireNonNull(metadata, "metadata is null");
requireNonNull(sqlParser, "sqlParser is null");
@@ -62,8 +64,8 @@ public class SimplifyExpressions
}
expression = pushDownNegations(expression);
expression = extractCommonPredicates(expression);
- Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(context.getSession(), metadata, sqlParser, context.getSymbolAllocator().getTypes(), expression, emptyList() /* parameters already replaced */);
- ExpressionInterpreter interpreter = ExpressionInterpreter.expressionOptimizer(expression, metadata, context.getSession(), expressionTypes);
+ Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(session, metadata, sqlParser, symbolAllocator.getTypes(), expression, emptyList() /* parameters already replaced */);
+ ExpressionInterpreter interpreter = ExpressionInterpreter.expressionOptimizer(expression, metadata, session, expressionTypes);
return LiteralInterpreter.toExpression(interpreter.optimize(NoOpSymbolResolver.INSTANCE), expressionTypes.get(NodeRef.of(expression)));
}
@@ -81,6 +83,6 @@ public class SimplifyExpressions
private Expression rewrite(Expression expression, Rule.Context context)
{
- return rewrite(expression, context, metadata, sqlParser);
+ return rewrite(expression, context.getSession(), context.getSymbolAllocator(), metadata, sqlParser);
}
}
