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

[WIP] Add native row expression optimizer #24238

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,13 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-tests</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-benchmark</artifactId>
Expand Down Expand Up @@ -953,6 +960,19 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-native-plugin</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-native-execution</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
</dependency>

<dependency>
<groupId>com.facebook.hive</groupId>
<artifactId>hive-dwrf</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public ConnectorPlanOptimizerProvider getConnectorPlanOptimizerProvider()
functionManager,
functionResolution,
rowExpressionService.getDeterminismEvaluator(),
rowExpressionService.getExpressionOptimizer());
rowExpressionService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.facebook.presto.spi.plan.PlanNodeIdAllocator;
import com.facebook.presto.spi.plan.TableScanNode;
import com.facebook.presto.spi.relation.DeterminismEvaluator;
import com.facebook.presto.spi.relation.ExpressionOptimizer;
import com.facebook.presto.spi.relation.ExpressionOptimizerProvider;
import com.facebook.presto.spi.relation.RowExpression;

import java.util.Optional;
Expand All @@ -44,15 +44,15 @@
public class JdbcComputePushdown
implements ConnectorPlanOptimizer
{
private final ExpressionOptimizer expressionOptimizer;
private final ExpressionOptimizerProvider expressionOptimizerProvider;
private final JdbcFilterToSqlTranslator jdbcFilterToSqlTranslator;
private final LogicalRowExpressions logicalRowExpressions;

public JdbcComputePushdown(
FunctionMetadataManager functionMetadataManager,
StandardFunctionResolution functionResolution,
DeterminismEvaluator determinismEvaluator,
ExpressionOptimizer expressionOptimizer,
ExpressionOptimizerProvider expressionOptimizerProvider,
String identifierQuote,
Set<Class<?>> functionTranslators)
{
Expand All @@ -62,7 +62,7 @@ public JdbcComputePushdown(
requireNonNull(determinismEvaluator, "determinismEvaluator is null");
requireNonNull(functionResolution, "functionResolution is null");

this.expressionOptimizer = requireNonNull(expressionOptimizer, "expressionOptimizer is null");
this.expressionOptimizerProvider = requireNonNull(expressionOptimizerProvider, "expressionOptimizerProvider is null");
this.jdbcFilterToSqlTranslator = new JdbcFilterToSqlTranslator(
functionMetadataManager,
buildFunctionTranslator(functionTranslators),
Expand Down 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 = expressionOptimizer.optimize(node.getPredicate(), OPTIMIZED, session);
RowExpression predicate = expressionOptimizerProvider.getExpressionOptimizer().optimize(node.getPredicate(), OPTIMIZED, session);
predicate = logicalRowExpressions.convertToConjunctiveNormalForm(predicate);
TranslatedExpression<JdbcExpression> jdbcExpression = translateWith(
predicate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.facebook.presto.spi.function.FunctionMetadataManager;
import com.facebook.presto.spi.function.StandardFunctionResolution;
import com.facebook.presto.spi.relation.DeterminismEvaluator;
import com.facebook.presto.spi.relation.ExpressionOptimizer;
import com.facebook.presto.spi.relation.ExpressionOptimizerProvider;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Inject;

Expand All @@ -34,7 +34,7 @@ public class JdbcPlanOptimizerProvider
private final FunctionMetadataManager functionManager;
private final StandardFunctionResolution functionResolution;
private final DeterminismEvaluator determinismEvaluator;
private final ExpressionOptimizer expressionOptimizer;
private final ExpressionOptimizerProvider expressionOptimizerProvider;
private final String identifierQuote;

@Inject
Expand All @@ -43,12 +43,12 @@ public JdbcPlanOptimizerProvider(
FunctionMetadataManager functionManager,
StandardFunctionResolution functionResolution,
DeterminismEvaluator determinismEvaluator,
ExpressionOptimizer expressionOptimizer)
ExpressionOptimizerProvider expressionOptimizerProvider)
{
this.functionManager = requireNonNull(functionManager, "functionManager is null");
this.functionResolution = requireNonNull(functionResolution, "functionResolution is null");
this.determinismEvaluator = requireNonNull(determinismEvaluator, "determinismEvaluator is null");
this.expressionOptimizer = requireNonNull(expressionOptimizer, "expressionOptimizer is null");
this.expressionOptimizerProvider = requireNonNull(expressionOptimizerProvider, "expressionOptimizerProvider is null");
this.identifierQuote = jdbcClient.getIdentifierQuote();
}

Expand All @@ -65,7 +65,7 @@ public Set<ConnectorPlanOptimizer> getPhysicalPlanOptimizers()
functionManager,
functionResolution,
determinismEvaluator,
expressionOptimizer,
expressionOptimizerProvider,
identifierQuote,
getFunctionTranslators()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public TestJdbcComputePushdown()
functionAndTypeManager,
functionResolution,
determinismEvaluator,
new RowExpressionOptimizer(METADATA),
() -> new RowExpressionOptimizer(METADATA),
"'",
getFunctionTranslators());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public String formatRowExpression(ConnectorSession session, RowExpression expres
};

public static final FilterStatsCalculatorService FILTER_STATS_CALCULATOR_SERVICE = new ConnectorFilterStatsCalculatorService(
new FilterStatsCalculator(METADATA, new ScalarStatsCalculator(METADATA), new StatsNormalizer()));
new FilterStatsCalculator(METADATA, new ScalarStatsCalculator(METADATA, ROW_EXPRESSION_SERVICE), new StatsNormalizer()));

public static final HiveClientConfig HIVE_CLIENT_CONFIG = new HiveClientConfig();
public static final MetastoreClientConfig METASTORE_CLIENT_CONFIG = new MetastoreClientConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ public final class SystemSessionProperties
public static final String INLINE_PROJECTIONS_ON_VALUES = "inline_projections_on_values";
public static final String INCLUDE_VALUES_NODE_IN_CONNECTOR_OPTIMIZER = "include_values_node_in_connector_optimizer";
public static final String SINGLE_NODE_EXECUTION_ENABLED = "single_node_execution_enabled";
public static final String DELEGATING_ROW_EXPRESSION_OPTIMIZER_ENABLED = "delegating_row_expression_optimizer_enabled";

// TODO: Native execution related session properties that are temporarily put here. They will be relocated in the future.
public static final String NATIVE_AGGREGATION_SPILL_ALL = "native_aggregation_spill_all";
Expand Down Expand Up @@ -1835,6 +1836,11 @@ public SystemSessionProperties(
SINGLE_NODE_EXECUTION_ENABLED,
"Enable single node execution",
featuresConfig.isSingleNodeExecutionEnabled(),
false),
booleanProperty(
DELEGATING_ROW_EXPRESSION_OPTIMIZER_ENABLED,
"Enable delegating row optimizer",
featuresConfig.isDelegatingRowExpressionOptimizerEnabled(),
false));
}

Expand Down Expand Up @@ -3121,4 +3127,9 @@ public static int getMinColumnarEncodingChannelsToPreferRowWiseEncoding(Session
{
return session.getSystemProperty(NATIVE_MIN_COLUMNAR_ENCODING_CHANNELS_TO_PREFER_ROW_WISE_ENCODING, Integer.class);
}

public static boolean isDelegatingRowExpressionOptimizerEnabled(Session session)
{
return session.getSystemProperty(DELEGATING_ROW_EXPRESSION_OPTIMIZER_ENABLED, Boolean.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@
import com.facebook.presto.split.RecordPageSourceProvider;
import com.facebook.presto.split.SplitManager;
import com.facebook.presto.sql.analyzer.FeaturesConfig;
import com.facebook.presto.sql.expressions.ExpressionOptimizerManager;
import com.facebook.presto.sql.planner.ConnectorPlanOptimizerManager;
import com.facebook.presto.sql.planner.PartitioningProviderManager;
import com.facebook.presto.sql.planner.planPrinter.RowExpressionFormatter;
import com.facebook.presto.sql.relational.ConnectorRowExpressionService;
import com.facebook.presto.sql.relational.FunctionResolution;
import com.facebook.presto.sql.relational.RowExpressionOptimizer;
import com.facebook.presto.transaction.TransactionManager;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -116,6 +116,7 @@ public class ConnectorManager
private final PageIndexerFactory pageIndexerFactory;
private final NodeInfo nodeInfo;
private final TransactionManager transactionManager;
private final ExpressionOptimizerManager expressionOptimizerManager;
private final DomainTranslator domainTranslator;
private final PredicateCompiler predicateCompiler;
private final DeterminismEvaluator determinismEvaluator;
Expand Down Expand Up @@ -151,6 +152,7 @@ public ConnectorManager(
PageSorter pageSorter,
PageIndexerFactory pageIndexerFactory,
TransactionManager transactionManager,
ExpressionOptimizerManager expressionOptimizerManager,
DomainTranslator domainTranslator,
PredicateCompiler predicateCompiler,
DeterminismEvaluator determinismEvaluator,
Expand All @@ -176,6 +178,7 @@ public ConnectorManager(
this.pageIndexerFactory = requireNonNull(pageIndexerFactory, "pageIndexerFactory is null");
this.nodeInfo = requireNonNull(nodeInfo, "nodeInfo is null");
this.transactionManager = requireNonNull(transactionManager, "transactionManager is null");
this.expressionOptimizerManager = requireNonNull(expressionOptimizerManager, "expressionOptimizerManager is null");
this.domainTranslator = requireNonNull(domainTranslator, "domainTranslator is null");
this.predicateCompiler = requireNonNull(predicateCompiler, "predicateCompiler is null");
this.determinismEvaluator = requireNonNull(determinismEvaluator, "determinismEvaluator is null");
Expand Down Expand Up @@ -382,7 +385,7 @@ private Connector createConnector(ConnectorId connectorId, ConnectorFactory fact
pageIndexerFactory,
new ConnectorRowExpressionService(
domainTranslator,
new RowExpressionOptimizer(metadataManager),
expressionOptimizerManager,
predicateCompiler,
determinismEvaluator,
new RowExpressionFormatter(metadataManager.getFunctionAndTypeManager())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.facebook.presto.spi.function.FunctionMetadata;
import com.facebook.presto.spi.relation.CallExpression;
import com.facebook.presto.spi.relation.ConstantExpression;
import com.facebook.presto.spi.relation.ExpressionOptimizerProvider;
import com.facebook.presto.spi.relation.InputReferenceExpression;
import com.facebook.presto.spi.relation.LambdaDefinitionExpression;
import com.facebook.presto.spi.relation.RowExpression;
Expand All @@ -31,11 +32,11 @@
import com.facebook.presto.spi.relation.VariableReferenceExpression;
import com.facebook.presto.sql.analyzer.ExpressionAnalyzer;
import com.facebook.presto.sql.analyzer.Scope;
import com.facebook.presto.sql.expressions.ExpressionOptimizerManager;
import com.facebook.presto.sql.planner.ExpressionInterpreter;
import com.facebook.presto.sql.planner.NoOpVariableResolver;
import com.facebook.presto.sql.planner.TypeProvider;
import com.facebook.presto.sql.relational.FunctionResolution;
import com.facebook.presto.sql.relational.RowExpressionOptimizer;
import com.facebook.presto.sql.tree.ArithmeticBinaryExpression;
import com.facebook.presto.sql.tree.ArithmeticUnaryExpression;
import com.facebook.presto.sql.tree.AstVisitor;
Expand Down Expand Up @@ -78,11 +79,18 @@
public class ScalarStatsCalculator
{
private final Metadata metadata;
private final ExpressionOptimizerProvider expressionOptimizerProvider;

@Inject
public ScalarStatsCalculator(Metadata metadata)
public ScalarStatsCalculator(Metadata metadata, ExpressionOptimizerManager expressionOptimizerManager)
{
this(metadata, (ExpressionOptimizerProvider) expressionOptimizerManager);
}

public ScalarStatsCalculator(Metadata metadata, ExpressionOptimizerProvider expressionOptimizerProvider)
{
this.metadata = requireNonNull(metadata, "metadata can not be null");
this.expressionOptimizerProvider = requireNonNull(expressionOptimizerProvider, "expressionOptimizerManager can not be null");
}

@Deprecated
Expand Down Expand Up @@ -126,7 +134,7 @@ public VariableStatsEstimate visitCall(CallExpression call, Void context)
return computeArithmeticBinaryStatistics(call, context);
}

RowExpression value = new RowExpressionOptimizer(metadata).optimize(call, OPTIMIZED, session);
RowExpression value = expressionOptimizerProvider.getExpressionOptimizer().optimize(call, OPTIMIZED, session);

if (isNull(value)) {
return nullStatsEstimate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
public class HandleJsonModule
implements Module
{
private final HandleResolver handleResolver;

public HandleJsonModule()
{
this(null);
}

public HandleJsonModule(HandleResolver handleResolver)
{
this.handleResolver = handleResolver;
}

@Override
public void configure(Binder binder)
{
Expand All @@ -38,6 +50,11 @@ public void configure(Binder binder)
jsonBinder(binder).addModuleBinding().to(FunctionHandleJacksonModule.class);
jsonBinder(binder).addModuleBinding().to(MetadataUpdateJacksonModule.class);

binder.bind(HandleResolver.class).in(Scopes.SINGLETON);
if (handleResolver == null) {
binder.bind(HandleResolver.class).in(Scopes.SINGLETON);
}
else {
binder.bind(HandleResolver.class).toInstance(handleResolver);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@
import com.facebook.presto.spi.security.SystemAccessControlFactory;
import com.facebook.presto.spi.session.SessionPropertyConfigurationManagerFactory;
import com.facebook.presto.spi.session.WorkerSessionPropertyProviderFactory;
import com.facebook.presto.spi.sql.planner.ExpressionOptimizerFactory;
import com.facebook.presto.spi.statistics.HistoryBasedPlanStatisticsProvider;
import com.facebook.presto.spi.storage.TempStorageFactory;
import com.facebook.presto.spi.tracing.TracerProvider;
import com.facebook.presto.spi.ttl.ClusterTtlProviderFactory;
import com.facebook.presto.spi.ttl.NodeTtlFetcherFactory;
import com.facebook.presto.sql.analyzer.AnalyzerProviderManager;
import com.facebook.presto.sql.analyzer.QueryPreparerProviderManager;
import com.facebook.presto.sql.expressions.ExpressionOptimizerManager;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManager;
import com.facebook.presto.storage.TempStorageManager;
import com.facebook.presto.tracing.TracerProviderManager;
Expand Down Expand Up @@ -135,6 +137,7 @@ public class PluginManager
private final QueryPreparerProviderManager queryPreparerProviderManager;
private final NodeStatusNotificationManager nodeStatusNotificationManager;
private final PlanCheckerProviderManager planCheckerProviderManager;
private final ExpressionOptimizerManager expressionOptimizerManager;

@Inject
public PluginManager(
Expand All @@ -157,7 +160,8 @@ public PluginManager(
HistoryBasedPlanStatisticsManager historyBasedPlanStatisticsManager,
TracerProviderManager tracerProviderManager,
NodeStatusNotificationManager nodeStatusNotificationManager,
PlanCheckerProviderManager planCheckerProviderManager)
PlanCheckerProviderManager planCheckerProviderManager,
ExpressionOptimizerManager expressionOptimizerManager)
{
requireNonNull(nodeInfo, "nodeInfo is null");
requireNonNull(config, "config is null");
Expand Down Expand Up @@ -190,6 +194,7 @@ public PluginManager(
this.queryPreparerProviderManager = requireNonNull(queryPreparerProviderManager, "queryPreparerProviderManager is null");
this.nodeStatusNotificationManager = requireNonNull(nodeStatusNotificationManager, "nodeStatusNotificationManager is null");
this.planCheckerProviderManager = requireNonNull(planCheckerProviderManager, "planCheckerProviderManager is null");
this.expressionOptimizerManager = requireNonNull(expressionOptimizerManager, "expressionManager is null");
}

public void loadPlugins()
Expand Down Expand Up @@ -372,6 +377,11 @@ public void installCoordinatorPlugin(CoordinatorPlugin plugin)
log.info("Registering plan checker provider factory %s", planCheckerProviderFactory.getName());
planCheckerProviderManager.addPlanCheckerProviderFactory(planCheckerProviderFactory);
}

for (ExpressionOptimizerFactory expressionOptimizerFactory : plugin.getExpressionOptimizerFactories()) {
log.info("Registering expression optimizer factory %s", expressionOptimizerFactory.getName());
expressionOptimizerManager.addExpressionOptimizerFactory(expressionOptimizerFactory);
}
}

private URLClassLoader buildClassLoader(String plugin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.facebook.presto.server.security.PasswordAuthenticatorManager;
import com.facebook.presto.server.security.ServerSecurityModule;
import com.facebook.presto.sql.analyzer.FeaturesConfig;
import com.facebook.presto.sql.expressions.ExpressionOptimizerManager;
import com.facebook.presto.sql.parser.SqlParserOptions;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManager;
import com.facebook.presto.storage.TempStorageManager;
Expand Down Expand Up @@ -190,6 +191,8 @@ public void run()
PluginNodeManager pluginNodeManager = new PluginNodeManager(nodeManager, nodeInfo.getEnvironment());
planCheckerProviderManager.loadPlanCheckerProviders(pluginNodeManager);

injector.getInstance(ExpressionOptimizerManager.class).loadExpressionOptimizerFactory();

startAssociatedProcesses(injector);

injector.getInstance(Announcer.class).start();
Expand Down
Loading
Loading