Search in sources :

Example 11 with TypeProvider

use of io.trino.sql.planner.TypeProvider in project trino by trinodb.

the class TestCostCalculator method calculateCostFragmentedPlan.

private PlanCostEstimate calculateCostFragmentedPlan(PlanNode node, StatsCalculator statsCalculator, Map<String, Type> types) {
    TypeProvider typeProvider = TypeProvider.copyOf(types.entrySet().stream().collect(toImmutableMap(entry -> new Symbol(entry.getKey()), Map.Entry::getValue)));
    StatsProvider statsProvider = new CachingStatsProvider(statsCalculator, session, typeProvider);
    CostProvider costProvider = new CachingCostProvider(costCalculatorUsingExchanges, statsProvider, Optional.empty(), session, 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 : Symbol(io.trino.sql.planner.Symbol) TypeProvider(io.trino.sql.planner.TypeProvider) SubPlan(io.trino.sql.planner.SubPlan) Plan(io.trino.sql.planner.Plan) SubPlan(io.trino.sql.planner.SubPlan)

Example 12 with TypeProvider

use of io.trino.sql.planner.TypeProvider in project trino by trinodb.

the class TestExpressionEquivalence method assertEquivalent.

private static void assertEquivalent(@Language("SQL") String left, @Language("SQL") String right) {
    ParsingOptions parsingOptions = new ParsingOptions(AS_DOUBLE);
    Expression leftExpression = planExpression(PLANNER_CONTEXT, TEST_SESSION, TYPE_PROVIDER, SQL_PARSER.createExpression(left, parsingOptions));
    Expression rightExpression = planExpression(PLANNER_CONTEXT, TEST_SESSION, TYPE_PROVIDER, SQL_PARSER.createExpression(right, parsingOptions));
    Set<Symbol> symbols = extractUnique(ImmutableList.of(leftExpression, rightExpression));
    TypeProvider types = TypeProvider.copyOf(symbols.stream().collect(toMap(identity(), TestExpressionEquivalence::generateType)));
    assertTrue(areExpressionEquivalent(leftExpression, rightExpression, types), format("Expected (%s) and (%s) to be equivalent", left, right));
    assertTrue(areExpressionEquivalent(rightExpression, leftExpression, types), format("Expected (%s) and (%s) to be equivalent", right, left));
}
Also used : ParsingOptions(io.trino.sql.parser.ParsingOptions) ExpressionTestUtils.planExpression(io.trino.sql.ExpressionTestUtils.planExpression) Expression(io.trino.sql.tree.Expression) Symbol(io.trino.sql.planner.Symbol) TypeProvider(io.trino.sql.planner.TypeProvider)

Example 13 with TypeProvider

use of io.trino.sql.planner.TypeProvider in project trino by trinodb.

the class TestValidateLimitWithPresortedInput method validatePlan.

private void validatePlan(Function<PlanBuilder, PlanNode> planProvider) {
    LocalQueryRunner queryRunner = getQueryRunner();
    PlanBuilder builder = new PlanBuilder(idAllocator, queryRunner.getMetadata(), queryRunner.getDefaultSession());
    PlanNode planNode = planProvider.apply(builder);
    TypeProvider types = builder.getTypes();
    queryRunner.inTransaction(session -> {
        // metadata.getCatalogHandle() registers the catalog for the transaction
        session.getCatalog().ifPresent(catalog -> queryRunner.getMetadata().getCatalogHandle(session, catalog));
        TypeAnalyzer typeAnalyzer = createTestingTypeAnalyzer(queryRunner.getPlannerContext());
        new ValidateLimitWithPresortedInput().validate(planNode, session, queryRunner.getPlannerContext(), typeAnalyzer, types, WarningCollector.NOOP);
        return null;
    });
}
Also used : PlanNode(io.trino.sql.planner.plan.PlanNode) TypeProvider(io.trino.sql.planner.TypeProvider) PlanBuilder(io.trino.sql.planner.iterative.rule.test.PlanBuilder) LocalQueryRunner(io.trino.testing.LocalQueryRunner) TypeAnalyzer.createTestingTypeAnalyzer(io.trino.sql.planner.TypeAnalyzer.createTestingTypeAnalyzer) TypeAnalyzer(io.trino.sql.planner.TypeAnalyzer)

Example 14 with TypeProvider

use of io.trino.sql.planner.TypeProvider in project trino by trinodb.

the class DetermineJoinDistributionType method getJoinNodeWithCost.

private PlanNodeWithCost getJoinNodeWithCost(Context context, JoinNode possibleJoinNode) {
    TypeProvider types = context.getSymbolAllocator().getTypes();
    StatsProvider stats = context.getStatsProvider();
    boolean replicated = possibleJoinNode.getDistributionType().get() == REPLICATED;
    /*
         *   HACK!
         *
         *   Currently cost model always has to compute the total cost of an operation.
         *   For JOIN the total cost consist of 4 parts:
         *     - Cost of exchanges that have to be introduced to execute a JOIN
         *     - Cost of building a hash table
         *     - Cost of probing a hash table
         *     - Cost of building an output for matched rows
         *
         *   When output size for a JOIN cannot be estimated the cost model returns
         *   UNKNOWN cost for the join.
         *
         *   However assuming the cost of JOIN output is always the same, we can still make
         *   cost based decisions based on the input cost for different types of JOINs.
         *
         *   Although the side flipping can be made purely based on stats (smaller side
         *   always goes to the right), determining JOIN type is not that simple. As when
         *   choosing REPLICATED over REPARTITIONED join the cost of exchanging and building
         *   the hash table scales with the number of nodes where the build side is replicated.
         *
         *   TODO Decision about the distribution should be based on LocalCostEstimate only when PlanCostEstimate cannot be calculated. Otherwise cost comparator cannot take query.max-memory into account.
         */
    int estimatedSourceDistributedTaskCount = taskCountEstimator.estimateSourceDistributedTaskCount(context.getSession());
    LocalCostEstimate cost = calculateJoinCostWithoutOutput(possibleJoinNode.getLeft(), possibleJoinNode.getRight(), stats, types, replicated, estimatedSourceDistributedTaskCount);
    return new PlanNodeWithCost(cost.toPlanCost(), possibleJoinNode);
}
Also used : StatsProvider(io.trino.cost.StatsProvider) TypeProvider(io.trino.sql.planner.TypeProvider) LocalCostEstimate(io.trino.cost.LocalCostEstimate)

Example 15 with TypeProvider

use of io.trino.sql.planner.TypeProvider in project trino by trinodb.

the class RuleAssert method matches.

public void matches(PlanMatchPattern pattern) {
    RuleApplication ruleApplication = applyRule();
    TypeProvider types = ruleApplication.types;
    if (!ruleApplication.wasRuleApplied()) {
        fail(format("%s did not fire for:\n%s", rule, formatPlan(plan, types)));
    }
    PlanNode actual = ruleApplication.getTransformedPlan();
    if (actual == plan) {
        // plans are not comparable, so we can only ensure they are not the same instance
        fail(format("%s: rule fired but return the original plan:\n%s", rule, formatPlan(plan, types)));
    }
    if (!ImmutableSet.copyOf(plan.getOutputSymbols()).equals(ImmutableSet.copyOf(actual.getOutputSymbols()))) {
        fail(format("%s: output schema of transformed and original plans are not equivalent\n" + "\texpected: %s\n" + "\tactual:   %s", rule, plan.getOutputSymbols(), actual.getOutputSymbols()));
    }
    inTransaction(session -> {
        assertPlan(session, metadata, functionManager, ruleApplication.statsProvider, new Plan(actual, types, StatsAndCosts.empty()), ruleApplication.lookup, pattern);
        return null;
    });
}
Also used : PlanNode(io.trino.sql.planner.plan.PlanNode) TypeProvider(io.trino.sql.planner.TypeProvider) PlanAssert.assertPlan(io.trino.sql.planner.assertions.PlanAssert.assertPlan) PlanPrinter.textLogicalPlan(io.trino.sql.planner.planprinter.PlanPrinter.textLogicalPlan) Plan(io.trino.sql.planner.Plan)

Aggregations

TypeProvider (io.trino.sql.planner.TypeProvider)29 Symbol (io.trino.sql.planner.Symbol)20 Session (io.trino.Session)15 PlanNode (io.trino.sql.planner.plan.PlanNode)13 Expression (io.trino.sql.tree.Expression)13 Optional (java.util.Optional)13 Map (java.util.Map)10 Objects.requireNonNull (java.util.Objects.requireNonNull)10 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 ImmutableSet (com.google.common.collect.ImmutableSet)9 List (java.util.List)9 ProjectNode (io.trino.sql.planner.plan.ProjectNode)8 Set (java.util.Set)8 ImmutableList (com.google.common.collect.ImmutableList)6 Pattern (io.trino.matching.Pattern)6 FilterNode (io.trino.sql.planner.plan.FilterNode)6 JoinNode (io.trino.sql.planner.plan.JoinNode)6 SymbolReference (io.trino.sql.tree.SymbolReference)6 Preconditions.checkState (com.google.common.base.Preconditions.checkState)5 Metadata (io.trino.metadata.Metadata)5