Search in sources :

Example 16 with AggregationNode

use of io.trino.sql.planner.plan.AggregationNode in project trino by trinodb.

the class TestTypeValidator method testValidAggregation.

@Test
public void testValidAggregation() {
    Symbol aggregationSymbol = symbolAllocator.newSymbol("sum", DOUBLE);
    PlanNode node = new AggregationNode(newId(), baseTableScan, ImmutableMap.of(aggregationSymbol, new Aggregation(functionResolution.resolveFunction(QualifiedName.of("sum"), fromTypes(DOUBLE)), ImmutableList.of(columnC.toSymbolReference()), false, Optional.empty(), Optional.empty(), Optional.empty())), singleGroupingSet(ImmutableList.of(columnA, columnB)), ImmutableList.of(), SINGLE, Optional.empty(), Optional.empty());
    assertTypesValid(node);
}
Also used : Aggregation(io.trino.sql.planner.plan.AggregationNode.Aggregation) PlanNode(io.trino.sql.planner.plan.PlanNode) AggregationNode(io.trino.sql.planner.plan.AggregationNode) Test(org.testng.annotations.Test)

Example 17 with AggregationNode

use of io.trino.sql.planner.plan.AggregationNode in project trino by trinodb.

the class TestTypeValidator method testInvalidAggregationFunctionSignature.

@Test
public void testInvalidAggregationFunctionSignature() {
    Symbol aggregationSymbol = symbolAllocator.newSymbol("sum", BIGINT);
    PlanNode node = new AggregationNode(newId(), baseTableScan, ImmutableMap.of(aggregationSymbol, new Aggregation(functionResolution.resolveFunction(QualifiedName.of("sum"), fromTypes(DOUBLE)), ImmutableList.of(columnC.toSymbolReference()), false, Optional.empty(), Optional.empty(), Optional.empty())), singleGroupingSet(ImmutableList.of(columnA, columnB)), ImmutableList.of(), SINGLE, Optional.empty(), Optional.empty());
    assertThatThrownBy(() -> assertTypesValid(node)).isInstanceOf(IllegalArgumentException.class).hasMessageMatching("type of symbol 'sum(_[0-9]+)?' is expected to be bigint, but the actual type is double");
}
Also used : Aggregation(io.trino.sql.planner.plan.AggregationNode.Aggregation) PlanNode(io.trino.sql.planner.plan.PlanNode) AggregationNode(io.trino.sql.planner.plan.AggregationNode) Test(org.testng.annotations.Test)

Example 18 with AggregationNode

use of io.trino.sql.planner.plan.AggregationNode in project trino by trinodb.

the class TestEffectivePredicateExtractor method testAggregation.

@Test
public void testAggregation() {
    PlanNode node = new AggregationNode(newId(), filter(baseTableScan, and(equals(AE, DE), equals(BE, EE), equals(CE, FE), lessThan(DE, bigintLiteral(10)), lessThan(CE, DE), greaterThan(AE, bigintLiteral(2)), equals(EE, FE))), ImmutableMap.of(C, new Aggregation(fakeFunction("test"), ImmutableList.of(), false, Optional.empty(), Optional.empty(), Optional.empty()), D, new Aggregation(fakeFunction("test"), ImmutableList.of(), false, Optional.empty(), Optional.empty(), Optional.empty())), singleGroupingSet(ImmutableList.of(A, B, C)), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty());
    Expression effectivePredicate = effectivePredicateExtractor.extract(SESSION, node, TypeProvider.empty(), typeAnalyzer);
    // Rewrite in terms of group by symbols
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(lessThan(AE, bigintLiteral(10)), lessThan(BE, AE), greaterThan(AE, bigintLiteral(2)), equals(BE, CE)));
}
Also used : Aggregation(io.trino.sql.planner.plan.AggregationNode.Aggregation) AggregationNode.globalAggregation(io.trino.sql.planner.plan.AggregationNode.globalAggregation) PlanNode(io.trino.sql.planner.plan.PlanNode) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Expression(io.trino.sql.tree.Expression) AggregationNode(io.trino.sql.planner.plan.AggregationNode) Test(org.testng.annotations.Test)

Example 19 with AggregationNode

use of io.trino.sql.planner.plan.AggregationNode in project trino by trinodb.

the class AggregationStepMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    AggregationNode aggregationNode = (AggregationNode) node;
    if (aggregationNode.getStep() != step) {
        return NO_MATCH;
    }
    return match();
}
Also used : AggregationNode(io.trino.sql.planner.plan.AggregationNode)

Example 20 with AggregationNode

use of io.trino.sql.planner.plan.AggregationNode in project trino by trinodb.

the class LogicalPlanner method createAnalyzePlan.

private RelationPlan createAnalyzePlan(Analysis analysis, Analyze analyzeStatement) {
    TableHandle targetTable = analysis.getAnalyzeTarget().orElseThrow();
    // Plan table scan
    Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, targetTable);
    ImmutableList.Builder<Symbol> tableScanOutputs = ImmutableList.builder();
    ImmutableMap.Builder<Symbol, ColumnHandle> symbolToColumnHandle = ImmutableMap.builder();
    ImmutableMap.Builder<String, Symbol> columnNameToSymbol = ImmutableMap.builder();
    TableMetadata tableMetadata = metadata.getTableMetadata(session, targetTable);
    for (ColumnMetadata column : tableMetadata.getColumns()) {
        Symbol symbol = symbolAllocator.newSymbol(column.getName(), column.getType());
        tableScanOutputs.add(symbol);
        symbolToColumnHandle.put(symbol, columnHandles.get(column.getName()));
        columnNameToSymbol.put(column.getName(), symbol);
    }
    TableStatisticsMetadata tableStatisticsMetadata = metadata.getStatisticsCollectionMetadata(session, targetTable.getCatalogName().getCatalogName(), tableMetadata.getMetadata());
    TableStatisticAggregation tableStatisticAggregation = statisticsAggregationPlanner.createStatisticsAggregation(tableStatisticsMetadata, columnNameToSymbol.buildOrThrow());
    StatisticAggregations statisticAggregations = tableStatisticAggregation.getAggregations();
    List<Symbol> groupingSymbols = statisticAggregations.getGroupingSymbols();
    PlanNode planNode = new StatisticsWriterNode(idAllocator.getNextId(), new AggregationNode(idAllocator.getNextId(), TableScanNode.newInstance(idAllocator.getNextId(), targetTable, tableScanOutputs.build(), symbolToColumnHandle.buildOrThrow(), false, Optional.empty()), statisticAggregations.getAggregations(), singleGroupingSet(groupingSymbols), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty()), new StatisticsWriterNode.WriteStatisticsReference(targetTable), symbolAllocator.newSymbol("rows", BIGINT), tableStatisticsMetadata.getTableStatistics().contains(ROW_COUNT), tableStatisticAggregation.getDescriptor());
    return new RelationPlan(planNode, analysis.getScope(analyzeStatement), planNode.getOutputSymbols(), Optional.empty());
}
Also used : ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) TableMetadata(io.trino.metadata.TableMetadata) ColumnHandle(io.trino.spi.connector.ColumnHandle) TableStatisticsMetadata(io.trino.spi.statistics.TableStatisticsMetadata) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) TableStatisticAggregation(io.trino.sql.planner.StatisticsAggregationPlanner.TableStatisticAggregation) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) StatisticsWriterNode(io.trino.sql.planner.plan.StatisticsWriterNode) AggregationNode(io.trino.sql.planner.plan.AggregationNode) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) StatisticAggregations(io.trino.sql.planner.plan.StatisticAggregations) PlanNode(io.trino.sql.planner.plan.PlanNode) TableHandle(io.trino.metadata.TableHandle)

Aggregations

AggregationNode (io.trino.sql.planner.plan.AggregationNode)49 Symbol (io.trino.sql.planner.Symbol)32 PlanNode (io.trino.sql.planner.plan.PlanNode)32 ProjectNode (io.trino.sql.planner.plan.ProjectNode)21 Map (java.util.Map)18 Aggregation (io.trino.sql.planner.plan.AggregationNode.Aggregation)16 ImmutableMap (com.google.common.collect.ImmutableMap)15 Assignments (io.trino.sql.planner.plan.Assignments)15 Expression (io.trino.sql.tree.Expression)15 ImmutableList (com.google.common.collect.ImmutableList)14 JoinNode (io.trino.sql.planner.plan.JoinNode)12 List (java.util.List)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)10 CorrelatedJoinNode (io.trino.sql.planner.plan.CorrelatedJoinNode)10 AssignUniqueId (io.trino.sql.planner.plan.AssignUniqueId)9 Optional (java.util.Optional)9 Session (io.trino.Session)7 ResolvedFunction (io.trino.metadata.ResolvedFunction)7 Captures (io.trino.matching.Captures)6 Pattern (io.trino.matching.Pattern)6