Search in sources :

Example 1 with InputColumn

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

the class FetchRewrite method createFetchSources.

public Map<RelationName, FetchSource> createFetchSources() {
    HashMap<RelationName, FetchSource> fetchSources = new HashMap<>();
    List<Symbol> outputs = plan.outputs();
    for (int i = 0; i < outputs.size(); i++) {
        Symbol output = outputs.get(i);
        if (output instanceof FetchMarker) {
            FetchMarker fetchMarker = (FetchMarker) output;
            RelationName tableName = fetchMarker.fetchId().ident().tableIdent();
            FetchSource fetchSource = fetchSources.get(tableName);
            if (fetchSource == null) {
                fetchSource = new FetchSource();
                fetchSources.put(tableName, fetchSource);
            }
            fetchSource.addFetchIdColumn(new InputColumn(i, fetchMarker.valueType()));
            for (Reference fetchRef : fetchMarker.fetchRefs()) {
                fetchSource.addRefToFetch(fetchRef);
            }
        }
    }
    return fetchSources;
}
Also used : FetchSource(io.crate.planner.node.fetch.FetchSource) HashMap(java.util.HashMap) Symbol(io.crate.expression.symbol.Symbol) FetchMarker(io.crate.expression.symbol.FetchMarker) InputColumn(io.crate.expression.symbol.InputColumn) Reference(io.crate.metadata.Reference) RelationName(io.crate.metadata.RelationName)

Example 2 with InputColumn

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

the class InputColumns method tryCreateSubscriptOnRoot.

@Nullable
private static Symbol tryCreateSubscriptOnRoot(Symbol symbol, ColumnIdent column, HashMap<Symbol, InputColumn> inputs) {
    if (column.isTopLevel()) {
        return null;
    }
    ColumnIdent root = column.getRoot();
    InputColumn rootIC = lookupValueByColumn(inputs, root);
    if (rootIC == null) {
        return symbol;
    }
    DataType<?> returnType = symbol.valueType();
    List<String> path = column.path();
    List<Symbol> arguments = mapTail(rootIC, path, Literal::of);
    return new Function(SubscriptObjectFunction.SIGNATURE, arguments, returnType);
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) SubscriptObjectFunction(io.crate.expression.scalar.SubscriptObjectFunction) Function(io.crate.expression.symbol.Function) WindowFunction(io.crate.expression.symbol.WindowFunction) InputColumn(io.crate.expression.symbol.InputColumn) AliasSymbol(io.crate.expression.symbol.AliasSymbol) ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) SelectSymbol(io.crate.expression.symbol.SelectSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Symbol(io.crate.expression.symbol.Symbol) Literal(io.crate.expression.symbol.Literal) Nullable(javax.annotation.Nullable)

Example 3 with InputColumn

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

the class InsertFromSubQueryPlanner method plan.

public static LogicalPlan plan(AnalyzedInsertStatement statement, PlannerContext plannerContext, LogicalPlanner logicalPlanner, SubqueryPlanner subqueryPlanner) {
    if (statement.outputs() != null && !plannerContext.clusterState().getNodes().getMinNodeVersion().onOrAfter(Version.V_4_2_0)) {
        throw new UnsupportedFeatureException(RETURNING_VERSION_ERROR_MSG);
    }
    List<Reference> targetColsExclPartitionCols = new ArrayList<>(statement.columns().size() - statement.tableInfo().partitionedBy().size());
    for (Reference column : statement.columns()) {
        if (statement.tableInfo().partitionedBy().contains(column.column())) {
            continue;
        }
        targetColsExclPartitionCols.add(column);
    }
    List<Symbol> columnSymbols = InputColumns.create(targetColsExclPartitionCols, new InputColumns.SourceSymbols(statement.columns()));
    // if fields are null default to number of rows imported
    var outputs = statement.outputs() == null ? List.of(new InputColumn(0, DataTypes.LONG)) : statement.outputs();
    ColumnIndexWriterProjection indexWriterProjection = new ColumnIndexWriterProjection(statement.tableInfo().ident(), null, statement.tableInfo().primaryKey(), statement.columns(), targetColsExclPartitionCols, columnSymbols, statement.isIgnoreDuplicateKeys(), statement.onDuplicateKeyAssignments(), statement.primaryKeySymbols(), statement.partitionedBySymbols(), statement.tableInfo().clusteredBy(), statement.clusteredBySymbol(), Settings.EMPTY, statement.tableInfo().isPartitioned(), outputs, statement.outputs() == null ? List.of() : statement.outputs());
    LogicalPlan plannedSubQuery = logicalPlanner.plan(statement.subQueryRelation(), plannerContext, subqueryPlanner, true);
    EvalProjection castOutputs = EvalProjection.castValues(Symbols.typeView(statement.columns()), plannedSubQuery.outputs());
    return new Insert(plannedSubQuery, indexWriterProjection, castOutputs);
}
Also used : InputColumns(io.crate.execution.dsl.projection.builder.InputColumns) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) ColumnIndexWriterProjection(io.crate.execution.dsl.projection.ColumnIndexWriterProjection) Insert(io.crate.planner.operators.Insert) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) InputColumn(io.crate.expression.symbol.InputColumn) LogicalPlan(io.crate.planner.operators.LogicalPlan)

Example 4 with InputColumn

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

the class AnalyzedInsertStatement method symbolsFromTargetColumnPositionOrGeneratedExpression.

private List<Symbol> symbolsFromTargetColumnPositionOrGeneratedExpression(Map<ColumnIdent, Integer> targetColumnMap, List<Reference> targetColumns, List<ColumnIdent> columns, Map<ColumnIdent, GeneratedReference> generatedColumns, Map<ColumnIdent, Reference> defaultExpressionColumns, boolean alwaysRequireColumn) {
    if (columns.isEmpty()) {
        return Collections.emptyList();
    }
    List<Symbol> symbols = new ArrayList<>(columns.size());
    InputColumns.SourceSymbols sourceSymbols = new InputColumns.SourceSymbols(targetColumns);
    for (ColumnIdent column : columns) {
        Integer position = targetColumnMap.get(column);
        if (position == null) {
            final Symbol symbol;
            Reference reference = defaultExpressionColumns.get(column);
            if (reference != null) {
                symbol = InputColumns.create(requireNonNull(reference.defaultExpression(), "Column " + column + " must contain a default expression"), sourceSymbols);
            } else {
                GeneratedReference generatedRef = generatedColumns.get(column);
                if (generatedRef == null) {
                    Reference columnRef = requireNonNull(targetTable.getReference(column), "Column " + column + " must exist in table " + targetTable.ident());
                    symbol = InputColumns.create(columnRef, sourceSymbols);
                } else {
                    symbol = InputColumns.create(generatedRef.generatedExpression(), sourceSymbols);
                }
            }
            if (SymbolVisitors.any(Symbols.IS_COLUMN, symbol)) {
                if (alwaysRequireColumn) {
                    throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Column \"%s\" is required but is missing from the insert statement", column.sqlFqn()));
                } else {
                    symbols.add(Literal.NULL);
                }
            } else {
                symbols.add(symbol);
            }
        } else {
            symbols.add(new InputColumn(position, targetColumns.get(position).valueType()));
        }
    }
    return symbols;
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) InputColumns(io.crate.execution.dsl.projection.builder.InputColumns) GeneratedReference(io.crate.metadata.GeneratedReference) Symbol(io.crate.expression.symbol.Symbol) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) InputColumn(io.crate.expression.symbol.InputColumn) ArrayList(java.util.ArrayList)

Example 5 with InputColumn

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

the class WindowProjector method createUpdateProbeValueFunction.

private static BiFunction<Object[], Object[], Object[]> createUpdateProbeValueFunction(WindowDefinition windowDefinition, BiFunction<DataType<?>, DataType<?>, BiFunction> getOffsetApplicationFunction, Object offsetValue, DataType<?> offsetType) {
    OrderBy windowOrdering = windowDefinition.orderBy();
    assert windowOrdering != null : "The window definition must be ordered if custom offsets are specified";
    List<Symbol> orderBySymbols = windowOrdering.orderBySymbols();
    if (orderBySymbols.size() != 1) {
        throw new IllegalArgumentException("Must have exactly 1 ORDER BY expression if using <offset> FOLLOWING/PRECEDING");
    }
    int offsetColumnPosition;
    Symbol orderSymbol = orderBySymbols.get(0);
    BiFunction applyOffsetOnOrderingValue = getOffsetApplicationFunction.apply(orderSymbol.valueType(), offsetType);
    if (orderSymbol.symbolType() == SymbolType.LITERAL) {
        offsetColumnPosition = -1;
    } else {
        assert orderSymbol instanceof InputColumn : "ORDER BY expression must resolve to an InputColumn, but got: " + orderSymbol;
        offsetColumnPosition = ((InputColumn) orderSymbol).index();
    }
    var finalOffsetValue = offsetType.id() == IntervalType.ID ? offsetType.sanitizeValue(offsetValue) : orderSymbol.valueType().sanitizeValue(offsetValue);
    return (currentRow, x) -> {
        // `null`)
        if (offsetColumnPosition != -1) {
            x[offsetColumnPosition] = applyOffsetOnOrderingValue.apply(currentRow[offsetColumnPosition], finalOffsetValue);
        }
        return x;
    };
}
Also used : OrderBy(io.crate.analyze.OrderBy) Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) InputColumn(io.crate.expression.symbol.InputColumn) WindowFrame(io.crate.sql.tree.WindowFrame) BiFunction(java.util.function.BiFunction) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Projector(io.crate.data.Projector) IntervalType(io.crate.types.IntervalType) Symbols(io.crate.expression.symbol.Symbols) Nullable(javax.annotation.Nullable) IntSupplier(java.util.function.IntSupplier) NodeContext(io.crate.metadata.NodeContext) Executor(java.util.concurrent.Executor) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) SymbolType(io.crate.expression.symbol.SymbolType) Comparators.createComparator(io.crate.execution.engine.sort.Comparators.createComparator) DataType(io.crate.types.DataType) SymbolEvaluator.evaluateWithoutParams(io.crate.analyze.SymbolEvaluator.evaluateWithoutParams) MemoryManager(io.crate.memory.MemoryManager) RamAccounting(io.crate.breaker.RamAccounting) ExpressionsInput(io.crate.expression.ExpressionsInput) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) Version(org.elasticsearch.Version) Row(io.crate.data.Row) Literal(io.crate.expression.symbol.Literal) WindowDefinition(io.crate.analyze.WindowDefinition) WindowAggProjection(io.crate.execution.dsl.projection.WindowAggProjection) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) InputFactory(io.crate.expression.InputFactory) Comparator(java.util.Comparator) BiFunction(java.util.function.BiFunction) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn)

Aggregations

InputColumn (io.crate.expression.symbol.InputColumn)61 Test (org.junit.Test)47 Symbol (io.crate.expression.symbol.Symbol)38 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)25 Reference (io.crate.metadata.Reference)15 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)11 StreamInput (org.elasticsearch.common.io.stream.StreamInput)11 MergePhase (io.crate.execution.dsl.phases.MergePhase)10 GroupProjection (io.crate.execution.dsl.projection.GroupProjection)10 FilterProjection (io.crate.execution.dsl.projection.FilterProjection)9 OrderedTopNProjection (io.crate.execution.dsl.projection.OrderedTopNProjection)9 Aggregation (io.crate.expression.symbol.Aggregation)9 Function (io.crate.expression.symbol.Function)9 ArrayList (java.util.ArrayList)9 TopNProjection (io.crate.execution.dsl.projection.TopNProjection)8 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)7 Row (io.crate.data.Row)7 EvalProjection (io.crate.execution.dsl.projection.EvalProjection)7 Projection (io.crate.execution.dsl.projection.Projection)7 CountAggregation (io.crate.execution.engine.aggregation.impl.CountAggregation)7