Search in sources :

Example 1 with Symbols

use of io.crate.expression.symbol.Symbols in project crate by crate.

the class RoutedCollectPhase method normalize.

/**
 * normalizes the symbols of this node with the given normalizer
 *
 * @return a normalized node, if no changes occurred returns this
 */
public RoutedCollectPhase normalize(EvaluatingNormalizer normalizer, @Nonnull TransactionContext txnCtx) {
    RoutedCollectPhase result = this;
    Function<Symbol, Symbol> normalize = s -> normalizer.normalize(s, txnCtx);
    List<Symbol> newToCollect = Lists2.map(toCollect, normalize);
    boolean changed = !newToCollect.equals(toCollect);
    Symbol newWhereClause = normalizer.normalize(where, txnCtx);
    OrderBy orderBy = this.orderBy;
    if (orderBy != null) {
        orderBy = orderBy.map(normalize);
    }
    changed = changed || newWhereClause != where || orderBy != this.orderBy;
    if (changed) {
        result = new RoutedCollectPhase(jobId(), phaseId(), name(), routing, maxRowGranularity, newToCollect, projections, newWhereClause, distributionInfo);
        result.nodePageSizeHint(nodePageSizeHint);
        result.orderBy(orderBy);
    }
    return result;
}
Also used : TransactionContext(io.crate.metadata.TransactionContext) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Set(java.util.Set) IOException(java.io.IOException) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) UUID(java.util.UUID) Function(java.util.function.Function) Lists2(io.crate.common.collections.Lists2) SymbolVisitors(io.crate.expression.symbol.SymbolVisitors) Objects(java.util.Objects) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) RowGranularity(io.crate.metadata.RowGranularity) Routing(io.crate.metadata.Routing) Projection(io.crate.execution.dsl.projection.Projection) Symbol(io.crate.expression.symbol.Symbol) Symbols(io.crate.expression.symbol.Symbols) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Paging(io.crate.data.Paging) SelectSymbol(io.crate.expression.symbol.SelectSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) DistributionInfo(io.crate.planner.distribution.DistributionInfo) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) OrderBy(io.crate.analyze.OrderBy) Symbol(io.crate.expression.symbol.Symbol) SelectSymbol(io.crate.expression.symbol.SelectSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol)

Example 2 with Symbols

use of io.crate.expression.symbol.Symbols in project crate by crate.

the class SymbolToColumnDefinitionConverterTest method testAliasedNameToColumnDefinition.

@Test
public void testAliasedNameToColumnDefinition() throws IOException {
    String createTableStmt = "create table tbl (" + "   col_default_object object as (" + "       col_nested_integer integer," + "       col_nested_object object as (" + "           col_nested_timestamp_with_time_zone timestamp with time zone" + "       )" + "   )" + ")";
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable(createTableStmt).build();
    String selectStmt = "select " + "   col_default_object['col_nested_integer'] as col1, " + "   col_default_object['col_nested_object']['col_nested_timestamp_with_time_zone'] as col2, " + "   col_default_object['col_nested_object'] as col3 " + "from tbl";
    var analyzedRelation = e.analyze(selectStmt);
    var actual = Lists2.map(analyzedRelation.outputs(), Symbols::toColumnDefinition);
    assertThat(actual, containsInAnyOrder(isColumnDefinition("col1", isColumnType(DataTypes.INTEGER.getName())), isColumnDefinition("col2", isColumnType(DataTypes.TIMESTAMPZ.getName())), isColumnDefinition("col3", isObjectColumnType(DataTypes.UNTYPED_OBJECT.getName(), isColumnPolicy(OBJECT_TYPE_DEFAULT_COLUMN_POLICY), contains(isColumnDefinition("col_nested_timestamp_with_time_zone", isColumnType(DataTypes.TIMESTAMPZ.getName())))))));
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) Symbols(io.crate.expression.symbol.Symbols) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 3 with Symbols

use of io.crate.expression.symbol.Symbols in project crate by crate.

the class SymbolToColumnDefinitionConverterTest method getAllColumnDefinitionsFrom.

private List<ColumnDefinition<Expression>> getAllColumnDefinitionsFrom(String createTableStmt) throws IOException {
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable(createTableStmt).build();
    AnalyzedRelation analyzedRelation = e.analyze("select * from tbl");
    return Lists2.map(analyzedRelation.outputs(), Symbols::toColumnDefinition);
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) Symbols(io.crate.expression.symbol.Symbols) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation)

Example 4 with Symbols

use of io.crate.expression.symbol.Symbols in project crate by crate.

the class SymbolToColumnDefinitionConverterTest method testTypeCastedSymbolToColumnDefinition.

@Test
public void testTypeCastedSymbolToColumnDefinition() {
    // check for naming of the target columns
    String selectStmt = "select cast([0,1,5] as array(boolean)) AS active_threads, " + "   cast(port['http']as boolean) from sys.nodes limit 1 ";
    SQLExecutor e = SQLExecutor.builder(clusterService).build();
    var analyzedRelation = e.analyze(selectStmt);
    var actual = Lists2.map(analyzedRelation.outputs(), Symbols::toColumnDefinition);
    assertThat(actual, containsInAnyOrder(isColumnDefinition("cast(port['http'] AS boolean)", isColumnType(DataTypes.BOOLEAN.getName())), isColumnDefinition("active_threads", isCollectionColumnType(ArrayType.NAME.toUpperCase(), isColumnType(DataTypes.BOOLEAN.getName())))));
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) Symbols(io.crate.expression.symbol.Symbols) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 5 with Symbols

use of io.crate.expression.symbol.Symbols in project crate by crate.

the class MoveFilterBeneathGroupBy method apply.

@Override
public LogicalPlan apply(Filter filter, Captures captures, TableStats tableStats, TransactionContext txnCtx, NodeContext nodeCtx) {
    // Since something like `SELECT x, sum(y) FROM t GROUP BY x HAVING y > 10` is not valid
    // (y would have to be declared as group key) any parts of a HAVING that is not an aggregation can be moved.
    Symbol predicate = filter.query();
    List<Symbol> parts = AndOperator.split(predicate);
    ArrayList<Symbol> withAggregates = new ArrayList<>();
    ArrayList<Symbol> withoutAggregates = new ArrayList<>();
    for (Symbol part : parts) {
        if (SymbolVisitors.any(Symbols::isAggregate, part)) {
            withAggregates.add(part);
        } else {
            withoutAggregates.add(part);
        }
    }
    if (withoutAggregates.isEmpty()) {
        return null;
    }
    GroupHashAggregate groupBy = captures.get(groupByCapture);
    if (withoutAggregates.size() == parts.size()) {
        return transpose(filter, groupBy);
    }
    /* HAVING `count(*) > 1 AND x = 10`
         * withAggregates:    [count(*) > 1]
         * withoutAggregates: [x = 10]
         *
         * Filter
         *  |
         * GroupBy
         *
         * transforms to
         *
         * Filter (count(*) > 1)
         *  |
         * GroupBy
         *  |
         * Filter (x = 10)
         */
    LogicalPlan newGroupBy = groupBy.replaceSources(List.of(new Filter(groupBy.source(), AndOperator.join(withoutAggregates))));
    return new Filter(newGroupBy, AndOperator.join(withAggregates));
}
Also used : Filter(io.crate.planner.operators.Filter) Symbol(io.crate.expression.symbol.Symbol) Symbols(io.crate.expression.symbol.Symbols) ArrayList(java.util.ArrayList) GroupHashAggregate(io.crate.planner.operators.GroupHashAggregate) LogicalPlan(io.crate.planner.operators.LogicalPlan)

Aggregations

Symbols (io.crate.expression.symbol.Symbols)11 SQLExecutor (io.crate.testing.SQLExecutor)7 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)6 Test (org.junit.Test)6 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 Symbol (io.crate.expression.symbol.Symbol)3 RelationName (io.crate.metadata.RelationName)2 OrderBy (io.crate.analyze.OrderBy)1 StatementAnalysisContext (io.crate.analyze.relations.StatementAnalysisContext)1 Lists2 (io.crate.common.collections.Lists2)1 Paging (io.crate.data.Paging)1 Projection (io.crate.execution.dsl.projection.Projection)1 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)1 ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)1 SelectSymbol (io.crate.expression.symbol.SelectSymbol)1 SymbolVisitors (io.crate.expression.symbol.SymbolVisitors)1 Routing (io.crate.metadata.Routing)1 RowGranularity (io.crate.metadata.RowGranularity)1 TransactionContext (io.crate.metadata.TransactionContext)1 DistributionInfo (io.crate.planner.distribution.DistributionInfo)1