Search in sources :

Example 31 with Language

use of org.intellij.lang.annotations.Language in project presto by prestodb.

the class TestOptimizeMixedDistinctAggregations method testMixedDistinctAggregationOptimizer.

@Test
public void testMixedDistinctAggregationOptimizer() {
    @Language("SQL") String sql = "SELECT custkey, max(totalprice) AS s, count(DISTINCT orderdate) AS d FROM orders GROUP BY custkey";
    String group = "GROUP";
    // Original keys
    String groupBy = "CUSTKEY";
    String aggregate = "TOTALPRICE";
    String distinctAggregation = "ORDERDATE";
    // Second Aggregation data
    List<String> groupByKeysSecond = ImmutableList.of(groupBy);
    Map<Optional<String>, ExpectedValueProvider<FunctionCall>> aggregationsSecond = ImmutableMap.of(Optional.of("arbitrary"), PlanMatchPattern.functionCall("arbitrary", false, ImmutableList.of(anySymbol())), Optional.of("count"), PlanMatchPattern.functionCall("count", false, ImmutableList.of(anySymbol())));
    // First Aggregation data
    List<String> groupByKeysFirst = ImmutableList.of(groupBy, distinctAggregation, group);
    Map<Optional<String>, ExpectedValueProvider<FunctionCall>> aggregationsFirst = ImmutableMap.of(Optional.of("MAX"), functionCall("max", ImmutableList.of("TOTALPRICE")));
    PlanMatchPattern tableScan = tableScan("orders", ImmutableMap.of("TOTALPRICE", "totalprice", "CUSTKEY", "custkey", "ORDERDATE", "orderdate"));
    // GroupingSet symbols
    ImmutableList.Builder<List<String>> groups = ImmutableList.builder();
    groups.add(ImmutableList.of(groupBy, aggregate));
    groups.add(ImmutableList.of(groupBy, distinctAggregation));
    PlanMatchPattern expectedPlanPattern = anyTree(aggregation(singleGroupingSet(groupByKeysSecond), aggregationsSecond, ImmutableMap.of(), Optional.empty(), SINGLE, project(aggregation(singleGroupingSet(groupByKeysFirst), aggregationsFirst, ImmutableMap.of(), Optional.empty(), SINGLE, groupingSet(groups.build(), group, anyTree(tableScan))))));
    assertUnitPlan(sql, expectedPlanPattern);
}
Also used : ExpectedValueProvider(com.facebook.presto.sql.planner.assertions.ExpectedValueProvider) Language(org.intellij.lang.annotations.Language) Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Example 32 with Language

use of org.intellij.lang.annotations.Language in project presto by prestodb.

the class TestMergeWindows method testNotMergeAcrossJoinBranches.

@Test
public void testNotMergeAcrossJoinBranches() {
    String rOrderkeyAlias = "R_ORDERKEY";
    String rShipdateAlias = "R_SHIPDATE";
    String rQuantityAlias = "R_QUANTITY";
    @Language("SQL") String sql = "WITH foo AS (" + "SELECT " + "suppkey, orderkey, partkey, " + "SUM(discount) OVER (PARTITION BY orderkey ORDER BY shipdate, quantity DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) a " + "FROM lineitem WHERE (partkey = 272 OR partkey = 273) AND suppkey > 50 " + "), " + "bar AS ( " + "SELECT " + "suppkey, orderkey, partkey, " + "AVG(quantity) OVER (PARTITION BY orderkey ORDER BY shipdate, quantity DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) b " + "FROM lineitem WHERE (partkey = 272 OR partkey = 273) AND suppkey > 50 " + ")" + "SELECT * FROM foo, bar WHERE foo.a = bar.b";
    ExpectedValueProvider<WindowNode.Specification> leftSpecification = specification(ImmutableList.of(ORDERKEY_ALIAS), ImmutableList.of(SHIPDATE_ALIAS, QUANTITY_ALIAS), ImmutableMap.of(SHIPDATE_ALIAS, SortOrder.ASC_NULLS_LAST, QUANTITY_ALIAS, SortOrder.DESC_NULLS_LAST));
    ExpectedValueProvider<WindowNode.Specification> rightSpecification = specification(ImmutableList.of(rOrderkeyAlias), ImmutableList.of(rShipdateAlias, rQuantityAlias), ImmutableMap.of(rShipdateAlias, SortOrder.ASC_NULLS_LAST, rQuantityAlias, SortOrder.DESC_NULLS_LAST));
    // Too many items in the map to call ImmutableMap.of() :-(
    ImmutableMap.Builder<String, String> leftTableScanBuilder = ImmutableMap.builder();
    leftTableScanBuilder.put(DISCOUNT_ALIAS, "discount");
    leftTableScanBuilder.put(ORDERKEY_ALIAS, "orderkey");
    leftTableScanBuilder.put("PARTKEY", "partkey");
    leftTableScanBuilder.put(SUPPKEY_ALIAS, "suppkey");
    leftTableScanBuilder.put(QUANTITY_ALIAS, "quantity");
    leftTableScanBuilder.put(SHIPDATE_ALIAS, "shipdate");
    PlanMatchPattern leftTableScan = tableScan("lineitem", leftTableScanBuilder.build());
    PlanMatchPattern rightTableScan = tableScan("lineitem", ImmutableMap.of(rOrderkeyAlias, "orderkey", "R_PARTKEY", "partkey", "R_SUPPKEY", "suppkey", rQuantityAlias, "quantity", rShipdateAlias, "shipdate"));
    assertUnitPlan(sql, anyTree(filter("SUM = AVG", join(JoinNode.Type.INNER, ImmutableList.of(), any(window(windowMatcherBuilder -> windowMatcherBuilder.specification(leftSpecification).addFunction("SUM", functionCall("sum", COMMON_FRAME, ImmutableList.of(DISCOUNT_ALIAS))), any(leftTableScan))), any(window(windowMatcherBuilder -> windowMatcherBuilder.specification(rightSpecification).addFunction("AVG", functionCall("avg", COMMON_FRAME, ImmutableList.of(rQuantityAlias))), any(rightTableScan)))))));
}
Also used : TranslateExpressions(com.facebook.presto.sql.planner.iterative.rule.TranslateExpressions) PlanMatchPattern.specification(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.specification) PlanMatchPattern.filter(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.filter) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) FrameBound(com.facebook.presto.sql.tree.FrameBound) PlanMatchPattern.anyTree(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.anyTree) IterativeOptimizer(com.facebook.presto.sql.planner.iterative.IterativeOptimizer) PlanMatchPattern.anyNot(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.anyNot) Test(org.testng.annotations.Test) RemoveRedundantIdentityProjections(com.facebook.presto.sql.planner.iterative.rule.RemoveRedundantIdentityProjections) PlanMatchPattern.window(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.window) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) WindowFrame(com.facebook.presto.sql.tree.WindowFrame) PlanMatchPattern.join(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.join) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) RuleStatsRecorder(com.facebook.presto.sql.planner.RuleStatsRecorder) PlanMatchPattern.any(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.any) PlanMatchPattern.expression(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.expression) PlanMatchPattern.functionCall(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.functionCall) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) ImmutableSet(com.google.common.collect.ImmutableSet) PlanMatchPattern.project(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.project) SortOrder(com.facebook.presto.common.block.SortOrder) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) ImmutableMap(com.google.common.collect.ImmutableMap) Language(org.intellij.lang.annotations.Language) Rule(com.facebook.presto.sql.planner.iterative.Rule) PlanMatchPattern.tableScan(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.tableScan) SqlParser(com.facebook.presto.sql.parser.SqlParser) List(java.util.List) Optional(java.util.Optional) ExpectedValueProvider(com.facebook.presto.sql.planner.assertions.ExpectedValueProvider) GatherAndMergeWindows(com.facebook.presto.sql.planner.iterative.rule.GatherAndMergeWindows) Language(org.intellij.lang.annotations.Language) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) ImmutableMap(com.google.common.collect.ImmutableMap) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Example 33 with Language

use of org.intellij.lang.annotations.Language in project presto by prestodb.

the class TestMergeWindows method testMergeDifferentFramesWithDefault.

@Test
public void testMergeDifferentFramesWithDefault() {
    Optional<WindowFrame> frameD = Optional.of(new WindowFrame(WindowFrame.Type.ROWS, new FrameBound(FrameBound.Type.CURRENT_ROW), Optional.of(new FrameBound(FrameBound.Type.UNBOUNDED_FOLLOWING))));
    ExpectedValueProvider<WindowNode.Specification> specificationD = specification(ImmutableList.of(SUPPKEY_ALIAS), ImmutableList.of(ORDERKEY_ALIAS), ImmutableMap.of(ORDERKEY_ALIAS, SortOrder.ASC_NULLS_LAST));
    @Language("SQL") String sql = "SELECT " + "SUM(quantity) OVER (PARTITION BY suppkey ORDER BY orderkey) sum_quantity_C, " + "AVG(quantity) OVER (PARTITION BY suppkey ORDER BY orderkey ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) avg_quantity_D, " + "SUM(discount) OVER (PARTITION BY suppkey ORDER BY orderkey) sum_discount_C " + "FROM lineitem";
    assertUnitPlan(sql, anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(specificationD).addFunction(functionCall("avg", frameD, ImmutableList.of(QUANTITY_ALIAS))).addFunction(functionCall("sum", UNSPECIFIED_FRAME, ImmutableList.of(DISCOUNT_ALIAS))).addFunction(functionCall("sum", UNSPECIFIED_FRAME, ImmutableList.of(QUANTITY_ALIAS))), LINEITEM_TABLESCAN_DOQS)));
}
Also used : WindowFrame(com.facebook.presto.sql.tree.WindowFrame) Language(org.intellij.lang.annotations.Language) FrameBound(com.facebook.presto.sql.tree.FrameBound) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Example 34 with Language

use of org.intellij.lang.annotations.Language in project presto by prestodb.

the class TestReorderWindows method testNonMergeableABAReordersToAABAllOptimizers.

@Test
public void testNonMergeableABAReordersToAABAllOptimizers() {
    @Language("SQL") String sql = "select " + "sum(quantity) over(PARTITION BY suppkey ORDER BY orderkey ASC NULLS LAST) sum_quantity_A, " + "avg(discount) over(PARTITION BY partkey ORDER BY receiptdate ASC NULLS LAST) avg_discount_B, " + "min(tax) over(PARTITION BY suppkey ORDER BY shipdate ASC NULLS LAST) min_tax_A " + "from lineitem";
    PlanMatchPattern pattern = anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(windowAp).addFunction(functionCall("min", commonFrame, ImmutableList.of(TAX_ALIAS))), project(window(windowMatcherBuilder -> windowMatcherBuilder.specification(windowA).addFunction(functionCall("sum", commonFrame, ImmutableList.of(QUANTITY_ALIAS))), project(window(windowMatcherBuilder -> windowMatcherBuilder.specification(windowB).addFunction(functionCall("avg", commonFrame, ImmutableList.of(DISCOUNT_ALIAS))), anyTree(LINEITEM_TABLESCAN_DOQPRSST)))))));
    assertPlan(sql, pattern);
}
Also used : TranslateExpressions(com.facebook.presto.sql.planner.iterative.rule.TranslateExpressions) PlanMatchPattern.specification(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.specification) PlanMatchPattern.filter(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.filter) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) PlanMatchPattern.anyTree(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.anyTree) IterativeOptimizer(com.facebook.presto.sql.planner.iterative.IterativeOptimizer) Test(org.testng.annotations.Test) RemoveRedundantIdentityProjections(com.facebook.presto.sql.planner.iterative.rule.RemoveRedundantIdentityProjections) PlanMatchPattern.window(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.window) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) WindowFrame(com.facebook.presto.sql.tree.WindowFrame) ImmutableList(com.google.common.collect.ImmutableList) RuleStatsRecorder(com.facebook.presto.sql.planner.RuleStatsRecorder) PlanMatchPattern.expression(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.expression) PlanMatchPattern.functionCall(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.functionCall) ImmutableSet(com.google.common.collect.ImmutableSet) PlanMatchPattern.project(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.project) SortOrder(com.facebook.presto.common.block.SortOrder) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) ImmutableMap(com.google.common.collect.ImmutableMap) Language(org.intellij.lang.annotations.Language) PlanMatchPattern.tableScan(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.tableScan) SqlParser(com.facebook.presto.sql.parser.SqlParser) List(java.util.List) Optional(java.util.Optional) ExpectedValueProvider(com.facebook.presto.sql.planner.assertions.ExpectedValueProvider) GatherAndMergeWindows(com.facebook.presto.sql.planner.iterative.rule.GatherAndMergeWindows) Language(org.intellij.lang.annotations.Language) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Example 35 with Language

use of org.intellij.lang.annotations.Language in project presto by prestodb.

the class TestRaptorIntegrationSmokeTest method testTableStatsSystemTable.

@SuppressWarnings("OverlyStrongTypeCast")
@Test
public void testTableStatsSystemTable() {
    // basic sanity tests
    assertQuery("" + "SELECT table_schema, table_name, sum(row_count)\n" + "FROM system.table_stats\n" + "WHERE table_schema = 'tpch'\n" + "  AND table_name IN ('orders', 'lineitem')\n" + "GROUP BY 1, 2", "" + "SELECT 'tpch', 'orders', (SELECT count(*) FROM orders)\n" + "UNION ALL\n" + "SELECT 'tpch', 'lineitem', (SELECT count(*) FROM lineitem)");
    assertQuery("" + "SELECT\n" + "  bool_and(row_count >= shard_count)\n" + ", bool_and(update_time >= create_time)\n" + ", bool_and(table_version >= 1)\n" + "FROM system.table_stats\n" + "WHERE row_count > 0", "SELECT true, true, true");
    // create empty table
    assertUpdate("CREATE TABLE test_table_stats (x bigint)");
    @Language("SQL") String sql = "" + "SELECT create_time, update_time, table_version," + "  shard_count, row_count, uncompressed_size\n" + "FROM system.table_stats\n" + "WHERE table_schema = 'tpch'\n" + "  AND table_name = 'test_table_stats'";
    MaterializedRow row = getOnlyElement(computeActual(sql).getMaterializedRows());
    LocalDateTime createTime = (LocalDateTime) row.getField(0);
    LocalDateTime updateTime1 = (LocalDateTime) row.getField(1);
    assertEquals(createTime, updateTime1);
    // table_version
    assertEquals(row.getField(2), 1L);
    // shard_count
    assertEquals(row.getField(3), 0L);
    // row_count
    assertEquals(row.getField(4), 0L);
    // uncompressed_size
    long size1 = (long) row.getField(5);
    // insert
    assertUpdate("INSERT INTO test_table_stats VALUES (1), (2), (3), (4)", 4);
    row = getOnlyElement(computeActual(sql).getMaterializedRows());
    assertEquals(row.getField(0), createTime);
    LocalDateTime updateTime2 = (LocalDateTime) row.getField(1);
    assertLessThan(updateTime1, updateTime2);
    // table_version
    assertEquals(row.getField(2), 2L);
    // shard_count
    assertGreaterThanOrEqual((Long) row.getField(3), 1L);
    // row_count
    assertEquals(row.getField(4), 4L);
    // uncompressed_size
    long size2 = (long) row.getField(5);
    assertGreaterThan(size2, size1);
    // delete
    assertUpdate("DELETE FROM test_table_stats WHERE x IN (2, 4)", 2);
    row = getOnlyElement(computeActual(sql).getMaterializedRows());
    assertEquals(row.getField(0), createTime);
    LocalDateTime updateTime3 = (LocalDateTime) row.getField(1);
    assertLessThan(updateTime2, updateTime3);
    // table_version
    assertEquals(row.getField(2), 3L);
    // shard_count
    assertGreaterThanOrEqual((Long) row.getField(3), 1L);
    // row_count
    assertEquals(row.getField(4), 2L);
    // uncompressed_Size
    long size3 = (long) row.getField(5);
    assertLessThan(size3, size2);
    // add column
    assertUpdate("ALTER TABLE test_table_stats ADD COLUMN y bigint");
    row = getOnlyElement(computeActual(sql).getMaterializedRows());
    assertEquals(row.getField(0), createTime);
    assertLessThan(updateTime3, (LocalDateTime) row.getField(1));
    // table_version
    assertEquals(row.getField(2), 4L);
    // row_count
    assertEquals(row.getField(4), 2L);
    // uncompressed_size
    assertEquals(row.getField(5), size3);
    // cleanup
    assertUpdate("DROP TABLE test_table_stats");
}
Also used : LocalDateTime(java.time.LocalDateTime) Language(org.intellij.lang.annotations.Language) MaterializedRow(com.facebook.presto.testing.MaterializedRow) Test(org.testng.annotations.Test) AbstractTestIntegrationSmokeTest(com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)

Aggregations

Language (org.intellij.lang.annotations.Language)111 TableMetadata (com.facebook.presto.metadata.TableMetadata)25 Test (org.testng.annotations.Test)24 MaterializedResult (com.facebook.presto.testing.MaterializedResult)19 PsiFile (com.intellij.psi.PsiFile)18 MaterializedRow (com.facebook.presto.testing.MaterializedRow)11 List (java.util.List)11 Optional (java.util.Optional)11 Test (org.junit.Test)11 ImmutableList (com.google.common.collect.ImmutableList)10 Session (com.facebook.presto.Session)9 Constraint (com.facebook.presto.spi.Constraint)9 AbstractTestIntegrationSmokeTest (com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)9 BasePlanTest (com.facebook.presto.sql.planner.assertions.BasePlanTest)8 ColumnConstraint (com.facebook.presto.sql.planner.planPrinter.IOPlanPrinter.ColumnConstraint)8 ImmutableMap (com.google.common.collect.ImmutableMap)8 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)7 ExpectedValueProvider (com.facebook.presto.sql.planner.assertions.ExpectedValueProvider)7 PlanMatchPattern (com.facebook.presto.sql.planner.assertions.PlanMatchPattern)7 SortOrder (com.facebook.presto.common.block.SortOrder)6