Search in sources :

Example 1 with Plan

use of io.prestosql.sql.planner.Plan in project hetu-core by openlookeng.

the class RuleAssert method matches.

public void matches(PlanMatchPattern pattern) {
    RuleApplication ruleApplication = applyRule();
    TypeProvider typeProvider = ruleApplication.types;
    if (!ruleApplication.wasRuleApplied()) {
        fail(format("%s did not fire for:\n%s", rule.getClass().getName(), formatPlan(plan, typeProvider)));
    }
    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.getClass().getName(), formatPlan(plan, typeProvider)));
    }
    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.getClass().getName(), plan.getOutputSymbols(), actual.getOutputSymbols()));
    }
    inTransaction(session -> {
        assertPlan(session, metadata, ruleApplication.statsProvider, new Plan(actual, typeProvider, StatsAndCosts.empty()), ruleApplication.lookup, pattern);
        return null;
    });
}
Also used : PlanNode(io.prestosql.spi.plan.PlanNode) TypeProvider(io.prestosql.sql.planner.TypeProvider) Plan(io.prestosql.sql.planner.Plan) PlanPrinter.textLogicalPlan(io.prestosql.sql.planner.planprinter.PlanPrinter.textLogicalPlan) PlanAssert.assertPlan(io.prestosql.sql.planner.assertions.PlanAssert.assertPlan)

Example 2 with Plan

use of io.prestosql.sql.planner.Plan in project hetu-core by openlookeng.

the class BasePlanTest method assertPlanWithSession.

protected void assertPlanWithSession(@Language("SQL") String sql, Session session, PlanMatchPattern pattern, List<PlanOptimizer> optimizers) {
    queryRunner.inTransaction(session, transactionSession -> {
        Plan actualPlan = queryRunner.createPlan(transactionSession, sql, optimizers, WarningCollector.NOOP);
        PlanAssert.assertPlan(transactionSession, queryRunner.getMetadata(), queryRunner.getStatsCalculator(), actualPlan, pattern);
        return null;
    });
}
Also used : Plan(io.prestosql.sql.planner.Plan)

Example 3 with Plan

use of io.prestosql.sql.planner.Plan in project hetu-core by openlookeng.

the class TestUnion method testUnionOverSingleNodeAggregationAndUnion.

@Test
public void testUnionOverSingleNodeAggregationAndUnion() {
    Plan plan = plan("SELECT count(*) FROM (" + "SELECT 1 FROM nation GROUP BY regionkey " + "UNION ALL (" + "   SELECT 1 FROM nation " + "   UNION ALL " + "   SELECT 1 FROM nation))", LogicalPlanner.Stage.OPTIMIZED_AND_VALIDATED, false);
    List<PlanNode> remotes = searchFrom(plan.getRoot()).where(TestUnion::isRemoteExchange).findAll();
    assertEquals(remotes.size(), 2, "There should be exactly two RemoteExchanges");
    assertEquals(((ExchangeNode) remotes.get(0)).getType(), GATHER);
    assertEquals(((ExchangeNode) remotes.get(1)).getType(), REPARTITION);
}
Also used : PlanNode(io.prestosql.spi.plan.PlanNode) Plan(io.prestosql.sql.planner.Plan) BasePlanTest(io.prestosql.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Example 4 with Plan

use of io.prestosql.sql.planner.Plan in project hetu-core by openlookeng.

the class TestUnion method testPartialRollupAggregationsWithUnion.

@Test
public void testPartialRollupAggregationsWithUnion() {
    Plan plan = plan("SELECT orderstatus, sum(orderkey) FROM (SELECT orderkey, orderstatus FROM orders UNION ALL SELECT orderkey, orderstatus FROM orders) x GROUP BY ROLLUP (orderstatus)", LogicalPlanner.Stage.OPTIMIZED_AND_VALIDATED, false);
    assertAtMostOneAggregationBetweenRemoteExchanges(plan);
    assertPlanIsFullyDistributed(plan);
}
Also used : Plan(io.prestosql.sql.planner.Plan) BasePlanTest(io.prestosql.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Example 5 with Plan

use of io.prestosql.sql.planner.Plan in project hetu-core by openlookeng.

the class QueryAssertions method assertQuery.

private static void assertQuery(QueryRunner actualQueryRunner, Session actualQuerySession, @Language("SQL") String actual, H2QueryRunner h2QueryRunner, Session expectedQuerySession, @Language("SQL") String expected, boolean ensureOrdering, boolean compareUpdate, Optional<Consumer<Plan>> planAssertion) {
    long start = System.nanoTime();
    Optional<MaterializedResult> actualResultsOp = Optional.empty();
    Plan queryPlan = null;
    if (planAssertion.isPresent()) {
        try {
            MaterializedResultWithPlan resultWithPlan = actualQueryRunner.executeWithPlan(actualQuerySession, actual, WarningCollector.NOOP);
            queryPlan = resultWithPlan.getQueryPlan();
            actualResultsOp = Optional.of(resultWithPlan.getMaterializedResult().toTestTypes());
        } catch (RuntimeException ex) {
            fail("Execution of 'actual' query failed: " + actual, ex);
        }
    } else {
        try {
            actualResultsOp = Optional.of(actualQueryRunner.execute(actualQuerySession, actual).toTestTypes());
        } catch (RuntimeException ex) {
            fail("Execution of 'actual' query failed: " + actual, ex);
        }
    }
    if (planAssertion.isPresent()) {
        planAssertion.get().accept(queryPlan);
    }
    Duration actualTime = nanosSince(start);
    long expectedStart = System.nanoTime();
    Optional<MaterializedResult> expectedResultsOp = Optional.empty();
    try {
        expectedResultsOp = Optional.of(h2QueryRunner.execute(expectedQuerySession, expected, actualResultsOp.get().getTypes()));
    } catch (RuntimeException ex) {
        fail("Execution of 'expected' query failed: " + expected, ex);
    }
    Duration totalTime = nanosSince(start);
    if (totalTime.compareTo(Duration.succinctDuration(1, SECONDS)) > 0) {
        log.info("FINISHED in presto: %s, h2: %s, total: %s", actualTime, nanosSince(expectedStart), totalTime);
    }
    if (actualResultsOp.get().getUpdateType().isPresent() || actualResultsOp.get().getUpdateCount().isPresent()) {
        if (!actualResultsOp.get().getUpdateType().isPresent()) {
            fail("update count present without update type for query: \n" + actual);
        }
        if (!compareUpdate) {
            fail("update type should not be present (use assertUpdate) for query: \n" + actual);
        }
    }
    List<MaterializedRow> actualRows = actualResultsOp.get().getMaterializedRows();
    List<MaterializedRow> expectedRows = expectedResultsOp.get().getMaterializedRows();
    if (compareUpdate) {
        if (!actualResultsOp.get().getUpdateType().isPresent()) {
            fail("update type not present for query: \n" + actual);
        }
        if (!actualResultsOp.get().getUpdateCount().isPresent()) {
            fail("update count not present for query: \n" + actual);
        }
        assertEquals(actualRows.size(), 1, "For query: \n " + actual + "\n:");
        assertEquals(expectedRows.size(), 1, "For query: \n " + actual + "\n:");
        MaterializedRow row = expectedRows.get(0);
        assertEquals(row.getFieldCount(), 1, "For query: \n " + actual + "\n:");
        assertEquals(row.getField(0), actualResultsOp.get().getUpdateCount().getAsLong(), "For query: \n " + actual + "\n:");
    }
    if (ensureOrdering) {
        if (!actualRows.equals(expectedRows)) {
            assertEquals(actualRows, expectedRows, "For query: \n " + actual + "\n:");
        }
    } else {
        assertEqualsIgnoreOrder(actualRows, expectedRows, "For query: \n " + actual);
    }
}
Also used : Duration(io.airlift.units.Duration) MaterializedResult(io.prestosql.testing.MaterializedResult) Plan(io.prestosql.sql.planner.Plan) MaterializedResultWithPlan(io.prestosql.testing.QueryRunner.MaterializedResultWithPlan) MaterializedResultWithPlan(io.prestosql.testing.QueryRunner.MaterializedResultWithPlan) MaterializedRow(io.prestosql.testing.MaterializedRow)

Aggregations

Plan (io.prestosql.sql.planner.Plan)46 Test (org.testng.annotations.Test)28 SqlQueryManager (io.prestosql.execution.SqlQueryManager)19 AfterTest (org.testng.annotations.AfterTest)19 TestingMetadata (io.prestosql.testing.TestingMetadata)10 PlanNode (io.prestosql.spi.plan.PlanNode)9 BasePlanTest (io.prestosql.sql.planner.assertions.BasePlanTest)8 MaterializedResult (io.prestosql.testing.MaterializedResult)7 SchemaTableName (io.prestosql.spi.connector.SchemaTableName)5 MaterializedRow (io.prestosql.testing.MaterializedRow)5 List (java.util.List)5 Map (java.util.Map)5 SubPlan (io.prestosql.sql.planner.SubPlan)4 PlanOptimizer (io.prestosql.sql.planner.optimizations.PlanOptimizer)4 Duration (io.airlift.units.Duration)3 Session (io.prestosql.Session)3 LogicalPlanner (io.prestosql.sql.planner.LogicalPlanner)3 TypeProvider (io.prestosql.sql.planner.TypeProvider)3 ExchangeNode (io.prestosql.sql.planner.plan.ExchangeNode)3 ImmutableList (com.google.common.collect.ImmutableList)2