Search in sources :

Example 16 with Row

use of io.trino.sql.tree.Row in project trino by trinodb.

the class TestSqlParser method testSelectWithRowType.

@Test
public void testSelectWithRowType() {
    assertStatement("SELECT col1.f1, col2, col3.f1.f2.f3 FROM table1", simpleQuery(selectList(new DereferenceExpression(new Identifier("col1"), identifier("f1")), new Identifier("col2"), new DereferenceExpression(new DereferenceExpression(new DereferenceExpression(new Identifier("col3"), identifier("f1")), identifier("f2")), identifier("f3"))), new Table(QualifiedName.of("table1"))));
    assertStatement("SELECT col1.f1[0], col2, col3[2].f2.f3, col4[4] FROM table1", simpleQuery(selectList(new SubscriptExpression(new DereferenceExpression(new Identifier("col1"), identifier("f1")), new LongLiteral("0")), new Identifier("col2"), new DereferenceExpression(new DereferenceExpression(new SubscriptExpression(new Identifier("col3"), new LongLiteral("2")), identifier("f2")), identifier("f3")), new SubscriptExpression(new Identifier("col4"), new LongLiteral("4"))), new Table(QualifiedName.of("table1"))));
    assertStatement("SELECT CAST(ROW(11, 12) AS ROW(COL0 INTEGER, COL1 INTEGER)).col0", simpleQuery(selectList(new DereferenceExpression(new Cast(new Row(Lists.newArrayList(new LongLiteral("11"), new LongLiteral("12"))), rowType(location(1, 26), field(location(1, 30), "COL0", simpleType(location(1, 35), "INTEGER")), field(location(1, 44), "COL1", simpleType(location(1, 49), "INTEGER")))), identifier("col0")))));
}
Also used : Cast(io.trino.sql.tree.Cast) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) CreateTable(io.trino.sql.tree.CreateTable) DropTable(io.trino.sql.tree.DropTable) Table(io.trino.sql.tree.Table) TruncateTable(io.trino.sql.tree.TruncateTable) RenameTable(io.trino.sql.tree.RenameTable) LongLiteral(io.trino.sql.tree.LongLiteral) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) SkipTo.skipToNextRow(io.trino.sql.tree.SkipTo.skipToNextRow) Row(io.trino.sql.tree.Row) Test(org.junit.jupiter.api.Test)

Example 17 with Row

use of io.trino.sql.tree.Row in project trino by trinodb.

the class TestStageStateMachine method createValuesPlan.

private static PlanFragment createValuesPlan() {
    Symbol symbol = new Symbol("column");
    PlanNodeId valuesNodeId = new PlanNodeId("plan");
    PlanFragment planFragment = new PlanFragment(new PlanFragmentId("plan"), new ValuesNode(valuesNodeId, ImmutableList.of(symbol), ImmutableList.of(new Row(ImmutableList.of(new StringLiteral("foo"))))), ImmutableMap.of(symbol, VARCHAR), SOURCE_DISTRIBUTION, ImmutableList.of(valuesNodeId), new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(symbol)), ungroupedExecution(), StatsAndCosts.empty(), Optional.empty());
    return planFragment;
}
Also used : PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) ValuesNode(io.trino.sql.planner.plan.ValuesNode) StringLiteral(io.trino.sql.tree.StringLiteral) Symbol(io.trino.sql.planner.Symbol) PartitioningScheme(io.trino.sql.planner.PartitioningScheme) PlanFragmentId(io.trino.sql.planner.plan.PlanFragmentId) Row(io.trino.sql.tree.Row) PlanFragment(io.trino.sql.planner.PlanFragment)

Example 18 with Row

use of io.trino.sql.tree.Row in project trino by trinodb.

the class PushAggregationThroughOuterJoin method createAggregationOverNull.

private MappedAggregationInfo createAggregationOverNull(AggregationNode referenceAggregation, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator) {
    // Create a values node that consists of a single row of nulls.
    // Map the output symbols from the referenceAggregation's source
    // to symbol references for the new values node.
    ImmutableList.Builder<Symbol> nullSymbols = ImmutableList.builder();
    ImmutableList.Builder<Expression> nullLiterals = ImmutableList.builder();
    ImmutableMap.Builder<Symbol, Symbol> sourcesSymbolMappingBuilder = ImmutableMap.builder();
    for (Symbol sourceSymbol : referenceAggregation.getSource().getOutputSymbols()) {
        Type type = symbolAllocator.getTypes().get(sourceSymbol);
        nullLiterals.add(new Cast(new NullLiteral(), toSqlType(type)));
        Symbol nullSymbol = symbolAllocator.newSymbol("null", type);
        nullSymbols.add(nullSymbol);
        sourcesSymbolMappingBuilder.put(sourceSymbol, nullSymbol);
    }
    ValuesNode nullRow = new ValuesNode(idAllocator.getNextId(), nullSymbols.build(), ImmutableList.of(new Row(nullLiterals.build())));
    // For each aggregation function in the reference node, create a corresponding aggregation function
    // that points to the nullRow. Map the symbols from the aggregations in referenceAggregation to the
    // symbols in these new aggregations.
    ImmutableMap.Builder<Symbol, Symbol> aggregationsSymbolMappingBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Symbol, AggregationNode.Aggregation> aggregationsOverNullBuilder = ImmutableMap.builder();
    SymbolMapper mapper = symbolMapper(sourcesSymbolMappingBuilder.buildOrThrow());
    for (Map.Entry<Symbol, AggregationNode.Aggregation> entry : referenceAggregation.getAggregations().entrySet()) {
        Symbol aggregationSymbol = entry.getKey();
        Aggregation overNullAggregation = mapper.map(entry.getValue());
        Symbol overNullSymbol = symbolAllocator.newSymbol(overNullAggregation.getResolvedFunction().getSignature().getName(), symbolAllocator.getTypes().get(aggregationSymbol));
        aggregationsOverNullBuilder.put(overNullSymbol, overNullAggregation);
        aggregationsSymbolMappingBuilder.put(aggregationSymbol, overNullSymbol);
    }
    Map<Symbol, Symbol> aggregationsSymbolMapping = aggregationsSymbolMappingBuilder.buildOrThrow();
    // create an aggregation node whose source is the null row.
    AggregationNode aggregationOverNullRow = new AggregationNode(idAllocator.getNextId(), nullRow, aggregationsOverNullBuilder.buildOrThrow(), globalAggregation(), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty());
    return new MappedAggregationInfo(aggregationOverNullRow, aggregationsSymbolMapping);
}
Also used : Cast(io.trino.sql.tree.Cast) ValuesNode(io.trino.sql.planner.plan.ValuesNode) SymbolMapper(io.trino.sql.planner.optimizations.SymbolMapper) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Symbol(io.trino.sql.planner.Symbol) AggregationNode(io.trino.sql.planner.plan.AggregationNode) ImmutableMap(com.google.common.collect.ImmutableMap) Aggregation(io.trino.sql.planner.plan.AggregationNode.Aggregation) AggregationNode.globalAggregation(io.trino.sql.planner.plan.AggregationNode.globalAggregation) Type(io.trino.spi.type.Type) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) Expression(io.trino.sql.tree.Expression) Row(io.trino.sql.tree.Row) NullLiteral(io.trino.sql.tree.NullLiteral) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 19 with Row

use of io.trino.sql.tree.Row in project trino by trinodb.

the class MergeProjectWithValues method apply.

@Override
public Result apply(ProjectNode node, Captures captures, Context context) {
    ValuesNode valuesNode = captures.get(VALUES);
    // handle projection which prunes all symbols
    if (node.getOutputSymbols().isEmpty()) {
        return Result.ofPlanNode(new ValuesNode(valuesNode.getId(), valuesNode.getRowCount()));
    }
    // fix iteration order over ProjectNode's assignments
    List<Map.Entry<Symbol, Expression>> assignments = ImmutableList.copyOf(node.getAssignments().entrySet());
    List<Symbol> outputs = assignments.stream().map(Map.Entry::getKey).collect(toImmutableList());
    List<Expression> expressions = assignments.stream().map(Map.Entry::getValue).collect(toImmutableList());
    // handle values with no output symbols
    if (valuesNode.getOutputSymbols().isEmpty()) {
        return Result.ofPlanNode(new ValuesNode(valuesNode.getId(), outputs, nCopies(valuesNode.getRowCount(), new Row(ImmutableList.copyOf(expressions)))));
    }
    // do not proceed if ValuesNode contains a non-deterministic expression and it is referenced more than once by the projection
    Set<Symbol> nonDeterministicValuesOutputs = new HashSet<>();
    for (Expression rowExpression : valuesNode.getRows().get()) {
        Row row = (Row) rowExpression;
        for (int i = 0; i < valuesNode.getOutputSymbols().size(); i++) {
            if (!isDeterministic(row.getItems().get(i), metadata)) {
                nonDeterministicValuesOutputs.add(valuesNode.getOutputSymbols().get(i));
            }
        }
    }
    Set<Symbol> multipleReferencedSymbols = expressions.stream().flatMap(expression -> SymbolsExtractor.extractAll(expression).stream()).collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream().filter(entry -> entry.getValue() > 1).map(Map.Entry::getKey).collect(toImmutableSet());
    if (!Sets.intersection(nonDeterministicValuesOutputs, multipleReferencedSymbols).isEmpty()) {
        return Result.empty();
    }
    // inline values expressions into projection's assignments
    ImmutableList.Builder<Expression> projectedRows = ImmutableList.builder();
    for (Expression rowExpression : valuesNode.getRows().get()) {
        Map<SymbolReference, Expression> mapping = buildMappings(valuesNode.getOutputSymbols(), (Row) rowExpression);
        Row projectedRow = new Row(expressions.stream().map(expression -> replaceExpression(expression, mapping)).collect(toImmutableList()));
        projectedRows.add(projectedRow);
    }
    return Result.ofPlanNode(new ValuesNode(valuesNode.getId(), outputs, projectedRows.build()));
}
Also used : ValuesNode(io.trino.sql.planner.plan.ValuesNode) Symbol(io.trino.sql.planner.Symbol) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SymbolReference(io.trino.sql.tree.SymbolReference) ExpressionNodeInliner.replaceExpression(io.trino.sql.planner.ExpressionNodeInliner.replaceExpression) Expression(io.trino.sql.tree.Expression) Row(io.trino.sql.tree.Row) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashSet(java.util.HashSet)

Example 20 with Row

use of io.trino.sql.tree.Row in project trino by trinodb.

the class PruneValuesColumns method pushDownProjectOff.

@Override
protected Optional<PlanNode> pushDownProjectOff(Context context, ValuesNode valuesNode, Set<Symbol> referencedOutputs) {
    // no symbols to prune
    if (valuesNode.getOutputSymbols().isEmpty()) {
        return Optional.empty();
    }
    List<Symbol> newOutputs = filteredCopy(valuesNode.getOutputSymbols(), referencedOutputs::contains);
    // no output symbols left
    if (newOutputs.isEmpty()) {
        return Optional.of(new ValuesNode(valuesNode.getId(), valuesNode.getRowCount()));
    }
    checkState(valuesNode.getRows().isPresent(), "rows is empty");
    // if any of ValuesNode's rows is specified by expression other than Row, the redundant piece cannot be extracted and pruned
    if (!valuesNode.getRows().get().stream().allMatch(Row.class::isInstance)) {
        return Optional.empty();
    }
    // for each output of project, the corresponding column in the values node
    int[] mapping = new int[newOutputs.size()];
    for (int i = 0; i < mapping.length; i++) {
        mapping[i] = valuesNode.getOutputSymbols().indexOf(newOutputs.get(i));
    }
    ImmutableList.Builder<Expression> rowsBuilder = ImmutableList.builder();
    for (Expression row : valuesNode.getRows().get()) {
        rowsBuilder.add(new Row(Arrays.stream(mapping).mapToObj(i -> ((Row) row).getItems().get(i)).collect(Collectors.toList())));
    }
    return Optional.of(new ValuesNode(valuesNode.getId(), newOutputs, rowsBuilder.build()));
}
Also used : Symbol(io.trino.sql.planner.Symbol) Arrays(java.util.Arrays) MoreLists.filteredCopy(io.trino.util.MoreLists.filteredCopy) Patterns.values(io.trino.sql.planner.plan.Patterns.values) Set(java.util.Set) Collectors(java.util.stream.Collectors) PlanNode(io.trino.sql.planner.plan.PlanNode) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Row(io.trino.sql.tree.Row) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) ValuesNode(io.trino.sql.planner.plan.ValuesNode) ValuesNode(io.trino.sql.planner.plan.ValuesNode) Expression(io.trino.sql.tree.Expression) Symbol(io.trino.sql.planner.Symbol) ImmutableList(com.google.common.collect.ImmutableList) Row(io.trino.sql.tree.Row)

Aggregations

Row (io.trino.sql.tree.Row)26 ImmutableList (com.google.common.collect.ImmutableList)15 Cast (io.trino.sql.tree.Cast)15 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)14 ValuesNode (io.trino.sql.planner.plan.ValuesNode)13 Symbol (io.trino.sql.planner.Symbol)12 Expression (io.trino.sql.tree.Expression)11 FunctionCall (io.trino.sql.tree.FunctionCall)11 LongLiteral (io.trino.sql.tree.LongLiteral)11 Assignments (io.trino.sql.planner.plan.Assignments)10 NullLiteral (io.trino.sql.tree.NullLiteral)10 QualifiedName (io.trino.sql.tree.QualifiedName)10 StringLiteral (io.trino.sql.tree.StringLiteral)10 Test (org.testng.annotations.Test)10 RowType (io.trino.spi.type.RowType)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8 BIGINT (io.trino.spi.type.BigintType.BIGINT)8 VARCHAR (io.trino.spi.type.VarcharType.VARCHAR)8 Type (io.trino.spi.type.Type)7 TypeSignatureProvider.fromTypes (io.trino.sql.analyzer.TypeSignatureProvider.fromTypes)7