Search in sources :

Example 6 with TypeProvider

use of com.facebook.presto.sql.planner.TypeProvider in project presto by prestodb.

the class TestJdbcComputePushdown method testJdbcComputePushdownBooleanOperations.

@Test
public void testJdbcComputePushdownBooleanOperations() {
    String table = "test_table";
    String schema = "test_schema";
    String expression = "(((c1 + c2) - c2 <> c2) OR c2 = c1) AND c1 <> c2";
    TypeProvider typeProvider = TypeProvider.copyOf(ImmutableMap.of("c1", BIGINT, "c2", BIGINT));
    RowExpression rowExpression = sqlToRowExpressionTranslator.translateAndOptimize(expression(expression), typeProvider);
    Set<ColumnHandle> columns = Stream.of("c1", "c2").map(TestJdbcComputePushdown::integerJdbcColumnHandle).collect(Collectors.toSet());
    PlanNode original = filter(jdbcTableScan(schema, table, BIGINT, "c1", "c2"), rowExpression);
    JdbcTableHandle jdbcTableHandle = new JdbcTableHandle(CONNECTOR_ID, new SchemaTableName(schema, table), CATALOG_NAME, schema, table);
    ConnectorSession session = new TestingConnectorSession(ImmutableList.of());
    JdbcTableLayoutHandle jdbcTableLayoutHandle = new JdbcTableLayoutHandle(session.getSqlFunctionProperties(), jdbcTableHandle, TupleDomain.none(), Optional.of(new JdbcExpression("((((((('c1' + 'c2') - 'c2') <> 'c2')) OR (('c2' = 'c1')))) AND (('c1' <> 'c2')))")));
    PlanNode actual = this.jdbcComputePushdown.optimize(original, session, null, ID_ALLOCATOR);
    assertPlanMatch(actual, PlanMatchPattern.filter(expression, JdbcTableScanMatcher.jdbcTableScanPattern(jdbcTableLayoutHandle, columns)));
}
Also used : JdbcColumnHandle(com.facebook.presto.plugin.jdbc.JdbcColumnHandle) ColumnHandle(com.facebook.presto.spi.ColumnHandle) PlanNode(com.facebook.presto.spi.plan.PlanNode) JdbcTableLayoutHandle(com.facebook.presto.plugin.jdbc.JdbcTableLayoutHandle) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) RowExpression(com.facebook.presto.spi.relation.RowExpression) ConnectorSession(com.facebook.presto.spi.ConnectorSession) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) JdbcTableHandle(com.facebook.presto.plugin.jdbc.JdbcTableHandle) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Test(org.testng.annotations.Test)

Example 7 with TypeProvider

use of com.facebook.presto.sql.planner.TypeProvider in project presto by prestodb.

the class TestJdbcComputePushdown method testJdbcComputePushdownUnsupported.

@Test
public void testJdbcComputePushdownUnsupported() {
    String table = "test_table";
    String schema = "test_schema";
    String expression = "(c1 + c2) > c2";
    TypeProvider typeProvider = TypeProvider.copyOf(ImmutableMap.of("c1", BIGINT, "c2", BIGINT));
    RowExpression rowExpression = sqlToRowExpressionTranslator.translateAndOptimize(expression(expression), typeProvider);
    Set<ColumnHandle> columns = Stream.of("c1", "c2").map(TestJdbcComputePushdown::integerJdbcColumnHandle).collect(Collectors.toSet());
    PlanNode original = filter(jdbcTableScan(schema, table, BIGINT, "c1", "c2"), rowExpression);
    JdbcTableHandle jdbcTableHandle = new JdbcTableHandle(CONNECTOR_ID, new SchemaTableName(schema, table), CATALOG_NAME, schema, table);
    ConnectorSession session = new TestingConnectorSession(ImmutableList.of());
    // Test should expect an empty entry for translatedSql since > is an unsupported function currently in the optimizer
    JdbcTableLayoutHandle jdbcTableLayoutHandle = new JdbcTableLayoutHandle(session.getSqlFunctionProperties(), jdbcTableHandle, TupleDomain.none(), Optional.empty());
    PlanNode actual = this.jdbcComputePushdown.optimize(original, session, null, ID_ALLOCATOR);
    assertPlanMatch(actual, PlanMatchPattern.filter(expression, JdbcTableScanMatcher.jdbcTableScanPattern(jdbcTableLayoutHandle, columns)));
}
Also used : JdbcColumnHandle(com.facebook.presto.plugin.jdbc.JdbcColumnHandle) ColumnHandle(com.facebook.presto.spi.ColumnHandle) PlanNode(com.facebook.presto.spi.plan.PlanNode) JdbcTableLayoutHandle(com.facebook.presto.plugin.jdbc.JdbcTableLayoutHandle) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) RowExpression(com.facebook.presto.spi.relation.RowExpression) ConnectorSession(com.facebook.presto.spi.ConnectorSession) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) JdbcTableHandle(com.facebook.presto.plugin.jdbc.JdbcTableHandle) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Test(org.testng.annotations.Test)

Example 8 with TypeProvider

use of com.facebook.presto.sql.planner.TypeProvider in project presto by prestodb.

the class PlanPrinter method formatFragment.

private static String formatFragment(FunctionAndTypeManager functionAndTypeManager, Session session, PlanFragment fragment, Optional<StageInfo> stageInfo, Optional<Map<PlanNodeId, PlanNodeStats>> planNodeStats, boolean verbose) {
    StringBuilder builder = new StringBuilder();
    builder.append(format("Fragment %s [%s]\n", fragment.getId(), fragment.getPartitioning()));
    if (stageInfo.isPresent()) {
        StageExecutionStats stageExecutionStats = stageInfo.get().getLatestAttemptExecutionInfo().getStats();
        List<TaskInfo> tasks = stageInfo.get().getLatestAttemptExecutionInfo().getTasks();
        double avgPositionsPerTask = tasks.stream().mapToLong(task -> task.getStats().getProcessedInputPositions()).average().orElse(Double.NaN);
        double squaredDifferences = tasks.stream().mapToDouble(task -> Math.pow(task.getStats().getProcessedInputPositions() - avgPositionsPerTask, 2)).sum();
        double sdAmongTasks = Math.sqrt(squaredDifferences / tasks.size());
        builder.append(indentString(1)).append(format("CPU: %s, Scheduled: %s, Input: %s (%s); per task: avg.: %s std.dev.: %s, Output: %s (%s)\n", stageExecutionStats.getTotalCpuTime().convertToMostSuccinctTimeUnit(), stageExecutionStats.getTotalScheduledTime().convertToMostSuccinctTimeUnit(), formatPositions(stageExecutionStats.getProcessedInputPositions()), stageExecutionStats.getProcessedInputDataSize(), formatDouble(avgPositionsPerTask), formatDouble(sdAmongTasks), formatPositions(stageExecutionStats.getOutputPositions()), stageExecutionStats.getOutputDataSize()));
    }
    PartitioningScheme partitioningScheme = fragment.getPartitioningScheme();
    builder.append(indentString(1)).append(format("Output layout: [%s]\n", Joiner.on(", ").join(partitioningScheme.getOutputLayout())));
    builder.append(indentString(1));
    boolean replicateNullsAndAny = partitioningScheme.isReplicateNullsAndAny();
    if (replicateNullsAndAny) {
        builder.append(format("Output partitioning: %s (replicate nulls and any) [%s]%s\n", partitioningScheme.getPartitioning().getHandle(), Joiner.on(", ").join(partitioningScheme.getPartitioning().getArguments()), formatHash(partitioningScheme.getHashColumn())));
    } else {
        builder.append(format("Output partitioning: %s [%s]%s\n", partitioningScheme.getPartitioning().getHandle(), Joiner.on(", ").join(partitioningScheme.getPartitioning().getArguments()), formatHash(partitioningScheme.getHashColumn())));
    }
    builder.append(indentString(1)).append(format("Stage Execution Strategy: %s\n", fragment.getStageExecutionDescriptor().getStageExecutionStrategy()));
    TypeProvider typeProvider = TypeProvider.fromVariables(fragment.getVariables());
    builder.append(textLogicalPlan(fragment.getRoot(), typeProvider, Optional.of(fragment.getStageExecutionDescriptor()), functionAndTypeManager, fragment.getStatsAndCosts(), session, planNodeStats, 1, verbose)).append("\n");
    return builder.toString();
}
Also used : TaskInfo(com.facebook.presto.execution.TaskInfo) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) StageInfo(com.facebook.presto.execution.StageInfo) StageExecutionDescriptor(com.facebook.presto.operator.StageExecutionDescriptor) Map(java.util.Map) IndexJoinNode(com.facebook.presto.sql.planner.plan.IndexJoinNode) PartitioningScheme(com.facebook.presto.sql.planner.PartitioningScheme) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) StageExecutionStats(com.facebook.presto.execution.StageExecutionStats) StageInfo.getAllStages(com.facebook.presto.execution.StageInfo.getAllStages) InterpretedFunctionInvoker(com.facebook.presto.sql.InterpretedFunctionInvoker) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) RowNumberNode(com.facebook.presto.sql.planner.plan.RowNumberNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TextRenderer.formatDouble(com.facebook.presto.sql.planner.planPrinter.TextRenderer.formatDouble) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ConnectorSession(com.facebook.presto.spi.ConnectorSession) LimitNode(com.facebook.presto.spi.plan.LimitNode) Stream(java.util.stream.Stream) OperatorNotFoundException(com.facebook.presto.metadata.OperatorNotFoundException) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) TextRenderer.indentString(com.facebook.presto.sql.planner.planPrinter.TextRenderer.indentString) ApplyNode(com.facebook.presto.sql.planner.plan.ApplyNode) AssignUniqueId(com.facebook.presto.sql.planner.plan.AssignUniqueId) CAST(com.facebook.presto.metadata.CastType.CAST) Joiner(com.google.common.base.Joiner) Iterables(com.google.common.collect.Iterables) SINGLE_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.SINGLE_DISTRIBUTION) Slice(io.airlift.slice.Slice) GroupIdNode(com.facebook.presto.sql.planner.plan.GroupIdNode) Assignments(com.facebook.presto.spi.plan.Assignments) DistinctLimitNode(com.facebook.presto.spi.plan.DistinctLimitNode) PlanCostEstimate(com.facebook.presto.cost.PlanCostEstimate) ArrayList(java.util.ArrayList) TableWriterMergeNode(com.facebook.presto.sql.planner.plan.TableWriterMergeNode) Lists(com.google.common.collect.Lists) LogicalRowExpressions(com.facebook.presto.expressions.LogicalRowExpressions) InternalPlanVisitor(com.facebook.presto.sql.planner.plan.InternalPlanVisitor) TableHandle(com.facebook.presto.spi.TableHandle) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) PlanFragmentId(com.facebook.presto.sql.planner.plan.PlanFragmentId) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Functions(com.google.common.base.Functions) PlanNodeStatsEstimate(com.facebook.presto.cost.PlanNodeStatsEstimate) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) Session(com.facebook.presto.Session) Domain(com.facebook.presto.common.predicate.Domain) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) SemiJoinNode(com.facebook.presto.sql.planner.plan.SemiJoinNode) TopNNode(com.facebook.presto.spi.plan.TopNNode) PlanNodeStatsSummarizer.aggregateStageStats(com.facebook.presto.sql.planner.planPrinter.PlanNodeStatsSummarizer.aggregateStageStats) TaskInfo(com.facebook.presto.execution.TaskInfo) DynamicFilters.extractDynamicFilters(com.facebook.presto.expressions.DynamicFilters.extractDynamicFilters) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) IntersectNode(com.facebook.presto.spi.plan.IntersectNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) ExplainAnalyzeNode(com.facebook.presto.sql.planner.plan.ExplainAnalyzeNode) SpatialJoinNode(com.facebook.presto.sql.planner.plan.SpatialJoinNode) RemoteSourceNode(com.facebook.presto.sql.planner.plan.RemoteSourceNode) SortNode(com.facebook.presto.sql.planner.plan.SortNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) MetadataDeleteNode(com.facebook.presto.sql.planner.plan.MetadataDeleteNode) TableWriterNode(com.facebook.presto.sql.planner.plan.TableWriterNode) Duration(io.airlift.units.Duration) GraphvizPrinter(com.facebook.presto.util.GraphvizPrinter) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) StatisticAggregations(com.facebook.presto.sql.planner.plan.StatisticAggregations) TopNRowNumberNode(com.facebook.presto.sql.planner.plan.TopNRowNumberNode) IndexSourceNode(com.facebook.presto.sql.planner.plan.IndexSourceNode) CallExpression(com.facebook.presto.spi.relation.CallExpression) OutputNode(com.facebook.presto.sql.planner.plan.OutputNode) ImmutableSet(com.google.common.collect.ImmutableSet) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Range(com.facebook.presto.common.predicate.Range) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) SampleNode(com.facebook.presto.sql.planner.plan.SampleNode) Optional(java.util.Optional) JoinNodeUtils(com.facebook.presto.sql.planner.optimizations.JoinNodeUtils) Arrays.stream(java.util.Arrays.stream) MarkDistinctNode(com.facebook.presto.spi.plan.MarkDistinctNode) DynamicFilterExtractResult(com.facebook.presto.expressions.DynamicFilters.DynamicFilterExtractResult) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) UnionNode(com.facebook.presto.spi.plan.UnionNode) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) ConnectorTableLayoutHandle(com.facebook.presto.spi.ConnectorTableLayoutHandle) TextRenderer.formatPositions(com.facebook.presto.sql.planner.planPrinter.TextRenderer.formatPositions) RowExpressionDeterminismEvaluator(com.facebook.presto.sql.relational.RowExpressionDeterminismEvaluator) Function(java.util.function.Function) ExceptNode(com.facebook.presto.spi.plan.ExceptNode) FilterNode(com.facebook.presto.spi.plan.FilterNode) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) LinkedList(java.util.LinkedList) UnnestNode(com.facebook.presto.sql.planner.plan.UnnestNode) Type(com.facebook.presto.common.type.Type) GroupReference(com.facebook.presto.sql.planner.iterative.GroupReference) SourceLocation(com.facebook.presto.spi.SourceLocation) Partitioning(com.facebook.presto.sql.planner.Partitioning) RowExpression(com.facebook.presto.spi.relation.RowExpression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) SubPlan(com.facebook.presto.sql.planner.SubPlan) CaseFormat(com.google.common.base.CaseFormat) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) JsonPlanFragment(com.facebook.presto.sql.planner.planPrinter.JsonRenderer.JsonPlanFragment) PlanNode(com.facebook.presto.spi.plan.PlanNode) UPPER_UNDERSCORE(com.google.common.base.CaseFormat.UPPER_UNDERSCORE) Collectors.toList(java.util.stream.Collectors.toList) Expression(com.facebook.presto.sql.tree.Expression) StatsAndCosts(com.facebook.presto.cost.StatsAndCosts) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode) EnforceSingleRowNode(com.facebook.presto.sql.planner.plan.EnforceSingleRowNode) ExchangeNode(com.facebook.presto.sql.planner.plan.ExchangeNode) StatisticsWriterNode(com.facebook.presto.sql.planner.plan.StatisticsWriterNode) PartitioningScheme(com.facebook.presto.sql.planner.PartitioningScheme) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) StageExecutionStats(com.facebook.presto.execution.StageExecutionStats)

Example 9 with TypeProvider

use of com.facebook.presto.sql.planner.TypeProvider in project presto by prestodb.

the class TestCostCalculator method assertCostSingleStageFragmentedPlan.

private CostAssertionBuilder assertCostSingleStageFragmentedPlan(PlanNode node, Map<String, PlanCostEstimate> costs, Map<String, PlanNodeStatsEstimate> stats, Map<String, Type> types) {
    TypeProvider typeProvider = TypeProvider.copyOf(types);
    StatsProvider statsProvider = new CachingStatsProvider(statsCalculator(stats), session, typeProvider);
    CostProvider costProvider = new TestingCostProvider(costs, costCalculatorUsingExchanges, statsProvider, session);
    // Explicitly generate the statsAndCosts, bypass fragment generation and sanity checks for mock plans.
    StatsAndCosts statsAndCosts = StatsAndCosts.create(node, statsProvider, costProvider).getForSubplan(node);
    return new CostAssertionBuilder(statsAndCosts.getCosts().getOrDefault(node.getId(), PlanCostEstimate.unknown()));
}
Also used : TypeProvider(com.facebook.presto.sql.planner.TypeProvider)

Example 10 with TypeProvider

use of com.facebook.presto.sql.planner.TypeProvider in project presto by prestodb.

the class TestCostCalculator method calculateCostFragmentedPlan.

private PlanCostEstimate calculateCostFragmentedPlan(PlanNode node, StatsCalculator statsCalculator, Map<String, Type> types) {
    TypeProvider typeProvider = TypeProvider.copyOf(types);
    StatsProvider statsProvider = new CachingStatsProvider(statsCalculator, session, typeProvider);
    CostProvider costProvider = new CachingCostProvider(costCalculatorUsingExchanges, statsProvider, Optional.empty(), session);
    node = translateExpression(node, statsCalculator, typeProvider);
    SubPlan subPlan = fragment(new Plan(node, typeProvider, StatsAndCosts.create(node, statsProvider, costProvider)));
    return subPlan.getFragment().getStatsAndCosts().getCosts().getOrDefault(node.getId(), PlanCostEstimate.unknown());
}
Also used : TypeProvider(com.facebook.presto.sql.planner.TypeProvider) Plan(com.facebook.presto.sql.planner.Plan) SubPlan(com.facebook.presto.sql.planner.SubPlan) SubPlan(com.facebook.presto.sql.planner.SubPlan)

Aggregations

TypeProvider (com.facebook.presto.sql.planner.TypeProvider)28 RowExpression (com.facebook.presto.spi.relation.RowExpression)17 PlanNode (com.facebook.presto.spi.plan.PlanNode)12 Test (org.testng.annotations.Test)12 ColumnHandle (com.facebook.presto.spi.ColumnHandle)7 Session (com.facebook.presto.Session)6 TranslatedExpression (com.facebook.presto.expressions.translator.TranslatedExpression)6 ConnectorSession (com.facebook.presto.spi.ConnectorSession)6 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)6 List (java.util.List)6 JdbcColumnHandle (com.facebook.presto.plugin.jdbc.JdbcColumnHandle)5 JdbcTableHandle (com.facebook.presto.plugin.jdbc.JdbcTableHandle)5 JdbcTableLayoutHandle (com.facebook.presto.plugin.jdbc.JdbcTableLayoutHandle)5 SchemaTableName (com.facebook.presto.spi.SchemaTableName)5 Expression (com.facebook.presto.sql.tree.Expression)5 TestingConnectorSession (com.facebook.presto.testing.TestingConnectorSession)5 Map (java.util.Map)5 CallExpression (com.facebook.presto.spi.relation.CallExpression)4 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)3 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)3