Skip to content
Closed
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 @@ -106,7 +106,7 @@ public PlanNode visitFilter(FilterNode node, RewriteContext<Void> context)
TableHandle oldTableHandle = oldTableScanNode.getTable();
JdbcTableHandle oldConnectorTable = (JdbcTableHandle) oldTableHandle.getConnectorHandle();

RowExpression predicate = expressionOptimizerProvider.getExpressionOptimizer(session).optimize(node.getPredicate(), OPTIMIZED, session);
RowExpression predicate = expressionOptimizerProvider.getExpressionOptimizer(session).optimize(node.getPredicate(), OPTIMIZED, session, false);
predicate = logicalRowExpressions.convertToConjunctiveNormalForm(predicate);
TranslatedExpression<JdbcExpression> jdbcExpression = translateWith(
predicate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public PlanNode visitFilter(FilterNode node, Void context)
TableHandle oldTableHandle = oldTableScanNode.getTable();
ClickHouseTableHandle oldConnectorTable = (ClickHouseTableHandle) oldTableHandle.getConnectorHandle();

RowExpression predicate = rowExpressionService.getExpressionOptimizer(session).optimize(node.getPredicate(), OPTIMIZED, session);
RowExpression predicate = rowExpressionService.getExpressionOptimizer(session).optimize(node.getPredicate(), OPTIMIZED, session, false);
predicate = logicalRowExpressions.convertToConjunctiveNormalForm(predicate);
TranslatedExpression<ClickHouseExpression> clickHouseExpression = translateWith(
predicate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static Optional<Subfield> toSubfield(
RowExpression indexExpression = expressionOptimizer.optimize(
dereferenceExpression.getArguments().get(1),
ExpressionOptimizer.Level.OPTIMIZED,
connectorSession);
connectorSession, false);

if (indexExpression instanceof ConstantExpression) {
Object index = ((ConstantExpression) indexExpression).getValue();
Expand All @@ -143,7 +143,7 @@ private static Optional<Subfield> toSubfield(
RowExpression indexExpression = expressionOptimizer.optimize(
arguments.get(1),
ExpressionOptimizer.Level.OPTIMIZED,
connectorSession);
connectorSession, false);

if (indexExpression instanceof ConstantExpression) {
Object index = ((ConstantExpression) indexExpression).getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public ConnectorPushdownFilterResult pushdownFilter(
}

RowExpression optimizedRemainingExpression = rowExpressionService.getExpressionOptimizer(session)
.optimize(decomposedFilter.getRemainingExpression(), OPTIMIZED, session);
.optimize(decomposedFilter.getRemainingExpression(), OPTIMIZED, session, false);
if (optimizedRemainingExpression instanceof ConstantExpression) {
ConstantExpression constantExpression = (ConstantExpression) optimizedRemainingExpression;
if (FALSE_CONSTANT.equals(constantExpression) || constantExpression.getValue() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public FilteringPageSource(
columnHandle -> new VariableReferenceExpression(Optional.empty(), columnHandle.getName(), columnHandle.getHiveType().getType(typeManager)),
columnHandle -> new InputReferenceExpression(Optional.empty(), columnHandle.getHiveColumnIndex(), columnHandle.getHiveType().getType(typeManager))));

RowExpression optimizedRemainingPredicate = rowExpressionService.getExpressionOptimizer(session).optimize(remainingPredicate, OPTIMIZED, session);
RowExpression optimizedRemainingPredicate = rowExpressionService.getExpressionOptimizer(session).optimize(remainingPredicate, OPTIMIZED, session, false);
if (TRUE_CONSTANT.equals(optimizedRemainingPredicate)) {
this.filterFunction = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public HivePageSourceProvider(
this.optimizedRowExpressionCache = CacheBuilder.newBuilder()
.recordStats()
.maximumSize(10_000)
.build(CacheLoader.from(cacheKey -> rowExpressionService.getExpressionOptimizer(cacheKey.session).optimize(cacheKey.rowExpression, OPTIMIZED, cacheKey.session)));
.build(CacheLoader.from(cacheKey -> rowExpressionService.getExpressionOptimizer(cacheKey.session).optimize(cacheKey.rowExpression, OPTIMIZED, cacheKey.session, false)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class TestDomainTranslator
private static final ExpressionOptimizer TEST_EXPRESSION_OPTIMIZER = new ExpressionOptimizer()
{
@Override
public RowExpression optimize(RowExpression rowExpression, Level level, ConnectorSession session)
public RowExpression optimize(RowExpression rowExpression, Level level, ConnectorSession session, boolean useBuiltInFunctions)
{
return rowExpression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class TestSubfieldExtractor
private static final ExpressionOptimizer TEST_EXPRESSION_OPTIMIZER = new ExpressionOptimizer()
{
@Override
public RowExpression optimize(RowExpression rowExpression, Level level, ConnectorSession session)
public RowExpression optimize(RowExpression rowExpression, Level level, ConnectorSession session, boolean useBuiltInFunctions)
{
return rowExpression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public VariableStatsEstimate visitCall(CallExpression call, Void context)
return computeArithmeticBinaryStatistics(call, context);
}

RowExpression value = expressionOptimizerProvider.getExpressionOptimizer(session).optimize(call, OPTIMIZED, session);
RowExpression value = expressionOptimizerProvider.getExpressionOptimizer(session).optimize(call, OPTIMIZED, session, false);

if (isNull(value)) {
return nullStatsEstimate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.cost;

import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.sql.expressions.ExpressionOptimizerManager;
import com.google.common.collect.ImmutableList;
import com.google.inject.Binder;
import com.google.inject.Module;
Expand All @@ -36,6 +37,7 @@ public void configure(Binder binder)
configBinder(binder).bindConfig(HistoryBasedOptimizationConfig.class);
binder.bind(HistoryBasedPlanStatisticsManager.class).in(Scopes.SINGLETON);
binder.bind(FragmentStatsProvider.class).in(Scopes.SINGLETON);
binder.bind(ExpressionOptimizerManager.class).in(Scopes.SINGLETON);
}

@Provides
Expand All @@ -46,9 +48,10 @@ public static StatsCalculator createNewStatsCalculator(
StatsNormalizer normalizer,
FilterStatsCalculator filterStatsCalculator,
HistoryBasedPlanStatisticsManager historyBasedPlanStatisticsManager,
FragmentStatsProvider fragmentStatsProvider)
FragmentStatsProvider fragmentStatsProvider,
ExpressionOptimizerManager expressionOptimizerManager)
{
StatsCalculator delegate = createComposableStatsCalculator(metadata, scalarStatsCalculator, normalizer, filterStatsCalculator, fragmentStatsProvider);
StatsCalculator delegate = createComposableStatsCalculator(metadata, scalarStatsCalculator, normalizer, filterStatsCalculator, fragmentStatsProvider, expressionOptimizerManager);
return historyBasedPlanStatisticsManager.getHistoryBasedPlanStatisticsCalculator(delegate);
}

Expand All @@ -57,14 +60,15 @@ public static ComposableStatsCalculator createComposableStatsCalculator(
ScalarStatsCalculator scalarStatsCalculator,
StatsNormalizer normalizer,
FilterStatsCalculator filterStatsCalculator,
FragmentStatsProvider fragmentStatsProvider)
FragmentStatsProvider fragmentStatsProvider,
ExpressionOptimizerManager expressionOptimizerManager)
{
ImmutableList.Builder<ComposableStatsCalculator.Rule<?>> rules = ImmutableList.builder();
rules.add(new OutputStatsRule());
rules.add(new TableScanStatsRule(metadata, normalizer));
rules.add(new SimpleFilterProjectSemiJoinStatsRule(normalizer, filterStatsCalculator, metadata.getFunctionAndTypeManager())); // this must be before FilterStatsRule
rules.add(new FilterStatsRule(normalizer, filterStatsCalculator));
rules.add(new ValuesStatsRule(metadata));
rules.add(new ValuesStatsRule(metadata, expressionOptimizerManager));
rules.add(new LimitStatsRule(normalizer));
rules.add(new EnforceSingleRowStatsRule(normalizer));
rules.add(new ProjectStatsRule(scalarStatsCalculator, normalizer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import com.facebook.presto.matching.Pattern;
import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.spi.plan.ValuesNode;
import com.facebook.presto.spi.relation.ExpressionOptimizer;
import com.facebook.presto.spi.relation.VariableReferenceExpression;
import com.facebook.presto.sql.expressions.ExpressionOptimizerManager;
import com.facebook.presto.sql.planner.TypeProvider;
import com.facebook.presto.sql.planner.iterative.Lookup;

Expand All @@ -32,8 +34,8 @@

import static com.facebook.presto.common.type.UnknownType.UNKNOWN;
import static com.facebook.presto.cost.StatsUtil.toStatsRepresentation;
import static com.facebook.presto.spi.relation.ExpressionOptimizer.Level.EVALUATED;
import static com.facebook.presto.spi.statistics.SourceInfo.ConfidenceLevel.FACT;
import static com.facebook.presto.sql.planner.RowExpressionInterpreter.evaluateConstantRowExpression;
import static com.facebook.presto.sql.planner.plan.Patterns.values;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.stream.Collectors.toList;
Expand All @@ -44,10 +46,12 @@ public class ValuesStatsRule
private static final Pattern<ValuesNode> PATTERN = values();

private final Metadata metadata;
private final ExpressionOptimizerManager expressionOptimizerManager;

public ValuesStatsRule(Metadata metadata)
public ValuesStatsRule(Metadata metadata, ExpressionOptimizerManager expressionOptimizerManager)
{
this.metadata = metadata;
this.expressionOptimizerManager = expressionOptimizerManager;
}

@Override
Expand Down Expand Up @@ -80,9 +84,10 @@ private List<Object> getVariableValues(ValuesNode valuesNode, int symbolId, Sess
.mapToObj(rowId -> null)
.collect(toList());
}
ExpressionOptimizer expressionOptimizer = expressionOptimizerManager.getExpressionOptimizer(session.toConnectorSession());
return valuesNode.getRows().stream()
.map(row -> row.get(symbolId))
.map(rowExpression -> evaluateConstantRowExpression(rowExpression, metadata.getFunctionAndTypeManager(), session.toConnectorSession()))
.map(rowExpression -> expressionOptimizer.optimize(rowExpression, EVALUATED, session.toConnectorSession(), true))
.collect(toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.facebook.airlift.json.JsonCodec;
import com.facebook.presto.client.FailureInfo;
import com.facebook.presto.common.QualifiedObjectName;
import com.facebook.presto.common.block.BlockBuilder;
import com.facebook.presto.common.block.RowBlockBuilder;
import com.facebook.presto.common.function.OperatorType;
Expand Down Expand Up @@ -71,6 +72,7 @@
import static com.facebook.presto.common.type.VarcharType.VARCHAR;
import static com.facebook.presto.common.type.VarcharType.createVarcharType;
import static com.facebook.presto.expressions.DynamicFilters.isDynamicFilter;
import static com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.JAVA_BUILTIN_NAMESPACE;
import static com.facebook.presto.metadata.CastType.CAST;
import static com.facebook.presto.metadata.CastType.JSON_TO_ARRAY_CAST;
import static com.facebook.presto.metadata.CastType.JSON_TO_MAP_CAST;
Expand All @@ -93,6 +95,7 @@
import static com.facebook.presto.spi.relation.SpecialFormExpression.Form.ROW_CONSTRUCTOR;
import static com.facebook.presto.spi.relation.SpecialFormExpression.Form.SWITCH;
import static com.facebook.presto.spi.relation.SpecialFormExpression.Form.WHEN;
import static com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypeSignatures;
import static com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes;
import static com.facebook.presto.sql.gen.VarArgsToMapAdapterGenerator.generateVarArgsToMapAdapter;
import static com.facebook.presto.sql.planner.Interpreters.interpretDereference;
Expand Down Expand Up @@ -129,7 +132,7 @@ public class RowExpressionInterpreter
private final FunctionAndTypeManager functionAndTypeManager;
private final FunctionResolution resolution;

private final Visitor visitor;
private Visitor visitor;

public static Object evaluateConstantRowExpression(RowExpression expression, FunctionAndTypeManager functionAndTypeManager, ConnectorSession session)
{
Expand All @@ -144,6 +147,12 @@ public static RowExpressionInterpreter rowExpressionInterpreter(RowExpression ex
return new RowExpressionInterpreter(expression, functionAndTypeManager, session, EVALUATED);
}

public RowExpressionInterpreter(RowExpression expression, FunctionAndTypeManager functionAndTypeManager, ConnectorSession session, Level optimizationLevel, boolean useBuiltInFunction)
{
this(expression, functionAndTypeManager, session, optimizationLevel);
this.visitor = new Visitor(useBuiltInFunction);
}

public RowExpressionInterpreter(RowExpression expression, Metadata metadata, ConnectorSession session, Level optimizationLevel)
{
this(expression, metadata.getFunctionAndTypeManager(), session, optimizationLevel);
Expand All @@ -160,7 +169,7 @@ public RowExpressionInterpreter(RowExpression expression, FunctionAndTypeManager
this.resolution = new FunctionResolution(functionAndTypeManager.getFunctionAndTypeResolver());
this.functionAndTypeManager = functionAndTypeManager;

this.visitor = new Visitor();
this.visitor = new Visitor(false);
}

public Type getType()
Expand Down Expand Up @@ -192,6 +201,13 @@ public Object optimize(VariableResolver inputs)
private class Visitor
implements RowExpressionVisitor<Object, Object>
{
private final boolean useBuiltInFunctions;

private Visitor(boolean useBuiltInFunctions)
{
this.useBuiltInFunctions = useBuiltInFunctions;
}

@Override
public Object visitInputReference(InputReferenceExpression node, Object context)
{
Expand Down Expand Up @@ -224,8 +240,19 @@ public Object visitCall(CallExpression node, Object context)
argumentTypes.add(expression.getType());
}

FunctionHandle functionHandle = node.getFunctionHandle();
FunctionMetadata functionMetadata = functionAndTypeManager.getFunctionMetadata(node.getFunctionHandle());
FunctionMetadata functionMetadata = null;
FunctionHandle functionHandle = null;
if (useBuiltInFunctions) {
functionHandle = functionAndTypeManager.lookupFunction(
QualifiedObjectName.valueOf(JAVA_BUILTIN_NAMESPACE, node.getDisplayName()),
fromTypeSignatures(node.getFunctionHandle().getArgumentTypes()));
functionMetadata = functionAndTypeManager.getFunctionAndTypeResolver().getFunctionMetadata(functionHandle);
}
else {
functionHandle = node.getFunctionHandle();
functionMetadata = functionAndTypeManager.getFunctionMetadata(functionHandle);
}

if (!functionMetadata.isCalledOnNullInput()) {
for (Object value : argumentValues) {
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private RowExpression rewrite(RowExpression expression, Session session)
// Rewrite RowExpression first to reduce depth of RowExpression tree by balancing AND/OR predicates.
// It doesn't matter whether we rewrite/optimize first because this will be called by IterativeOptimizer.
RowExpression rewritten = RowExpressionTreeRewriter.rewriteWith(logicalExpressionRewriter, expression, true);
return expressionOptimizerManager.getExpressionOptimizer(session.toConnectorSession()).optimize(rewritten, SERIALIZABLE, session.toConnectorSession());
return expressionOptimizerManager.getExpressionOptimizer(session.toConnectorSession()).optimize(rewritten, SERIALIZABLE, session.toConnectorSession(), false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ private boolean canConvertOuterToInner(List<VariableReferenceExpression> innerVa
// Temporary implementation for joins because the SimplifyExpressions optimizers can not run properly on join clauses
private RowExpression simplifyExpression(RowExpression expression)
{
return expressionOptimizerProvider.getExpressionOptimizer(session.toConnectorSession()).optimize(expression, ExpressionOptimizer.Level.SERIALIZABLE, session.toConnectorSession());
return expressionOptimizerProvider.getExpressionOptimizer(session.toConnectorSession()).optimize(expression, ExpressionOptimizer.Level.SERIALIZABLE, session.toConnectorSession(), false);
}

private boolean areExpressionsEquivalent(RowExpression leftExpression, RowExpression rightExpression)
Expand All @@ -1454,7 +1454,7 @@ private RowExpression nullInputEvaluator(final Collection<VariableReferenceExpre
{
expression = RowExpressionNodeInliner.replaceExpression(expression, nullSymbols.stream()
.collect(Collectors.toMap(identity(), variable -> constantNull(variable.getSourceLocation(), variable.getType()))));
return expressionOptimizerProvider.getExpressionOptimizer(session.toConnectorSession()).optimize(expression, ExpressionOptimizer.Level.OPTIMIZED, session.toConnectorSession());
return expressionOptimizerProvider.getExpressionOptimizer(session.toConnectorSession()).optimize(expression, ExpressionOptimizer.Level.OPTIMIZED, session.toConnectorSession(), false);
}

private Predicate<RowExpression> joinEqualityExpression(final Collection<VariableReferenceExpression> leftVariables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ private static Optional<Subfield> toSubfield(
RowExpression indexExpression = expressionOptimizer.optimize(
dereference.getArguments().get(1),
ExpressionOptimizer.Level.OPTIMIZED,
connectorSession);
connectorSession, false);

if (indexExpression instanceof ConstantExpression) {
Object index = ((ConstantExpression) indexExpression).getValue();
Expand All @@ -613,7 +613,7 @@ private static Optional<Subfield> toSubfield(
RowExpression indexExpression = expressionOptimizer.optimize(
arguments.get(1),
ExpressionOptimizer.Level.OPTIMIZED,
connectorSession);
connectorSession, false);

if (indexExpression instanceof ConstantExpression) {
Object index = ((ConstantExpression) indexExpression).getValue();
Expand Down
Loading
Loading