Search in sources :

Example 11 with Symbol

use of io.crate.analyze.symbol.Symbol in project crate by crate.

the class InsertFromValuesAnalyzer method analyze.

public AnalyzedStatement analyze(InsertFromValues node, Analysis analysis) {
    DocTableInfo tableInfo = schemas.getWritableTable(TableIdent.of(node.table(), analysis.sessionContext().defaultSchema()));
    Operation.blockedRaiseException(tableInfo, Operation.INSERT);
    DocTableRelation tableRelation = new DocTableRelation(tableInfo);
    FieldProvider fieldProvider = new NameFieldProvider(tableRelation);
    Function<ParameterExpression, Symbol> convertParamFunction = analysis.parameterContext();
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, fieldProvider, null);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    expressionAnalyzer.setResolveFieldsOperation(Operation.INSERT);
    ValuesResolver valuesResolver = new ValuesResolver(tableRelation);
    ExpressionAnalyzer valuesAwareExpressionAnalyzer = new ValuesAwareExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, fieldProvider, valuesResolver);
    InsertFromValuesAnalyzedStatement statement = new InsertFromValuesAnalyzedStatement(tableInfo, analysis.parameterContext().numBulkParams());
    handleInsertColumns(node, node.maxValuesLength(), statement);
    Set<Reference> allReferencedReferences = new HashSet<>();
    for (GeneratedReference reference : tableInfo.generatedColumns()) {
        allReferencedReferences.addAll(reference.referencedReferences());
    }
    ReferenceToLiteralConverter.Context referenceToLiteralContext = new ReferenceToLiteralConverter.Context(statement.columns(), allReferencedReferences);
    ValueNormalizer valuesNormalizer = new ValueNormalizer(schemas);
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.COPY, null, tableRelation);
    analyzeColumns(statement.tableInfo(), statement.columns());
    for (ValuesList valuesList : node.valuesLists()) {
        analyzeValues(tableRelation, valuesNormalizer, normalizer, expressionAnalyzer, expressionAnalysisContext, analysis.transactionContext(), valuesResolver, valuesAwareExpressionAnalyzer, valuesList, node.onDuplicateKeyAssignments(), statement, analysis.parameterContext(), referenceToLiteralContext);
    }
    return statement;
}
Also used : ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) DocTableInfo(io.crate.metadata.doc.DocTableInfo) ValueNormalizer(io.crate.analyze.expressions.ValueNormalizer) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) FieldProvider(io.crate.analyze.relations.FieldProvider) NameFieldProvider(io.crate.analyze.relations.NameFieldProvider) NameFieldProvider(io.crate.analyze.relations.NameFieldProvider) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Example 12 with Symbol

use of io.crate.analyze.symbol.Symbol in project crate by crate.

the class InsertFromValuesAnalyzer method processGeneratedExpressions.

private void processGeneratedExpressions(GeneratedExpressionContext context) {
    List<ColumnIdent> primaryKey = context.analyzedStatement.tableInfo().primaryKey();
    for (GeneratedReference reference : context.tableRelation.tableInfo().generatedColumns()) {
        Symbol valueSymbol = TO_LITERAL_CONVERTER.process(reference.generatedExpression(), context.referenceToLiteralContext);
        valueSymbol = context.normalizer.normalize(valueSymbol, context.transactionContext);
        if (valueSymbol.symbolType() == SymbolType.LITERAL) {
            Object value = ((Input) valueSymbol).value();
            if (primaryKey.contains(reference.ident().columnIdent()) && context.analyzedStatement.columns().indexOf(reference) == -1) {
                int idx = primaryKey.indexOf(reference.ident().columnIdent());
                addPrimaryKeyValue(idx, value, context.primaryKeyValues);
            }
            ColumnIdent routingColumn = context.analyzedStatement.tableInfo().clusteredBy();
            if (routingColumn != null && routingColumn.equals(reference.ident().columnIdent())) {
                context.routingValue = extractRoutingValue(routingColumn, value, context.analyzedStatement);
            }
            if (context.tableRelation.tableInfo().isPartitioned() && context.tableRelation.tableInfo().partitionedByColumns().contains(reference)) {
                addGeneratedPartitionedColumnValue(reference.ident().columnIdent(), value, context.analyzedStatement.currentPartitionMap());
            } else {
                context.insertValues = addGeneratedColumnValue(context.analyzedStatement, reference, value, context.insertValues);
            }
        }
    }
}
Also used : Input(io.crate.data.Input) Symbol(io.crate.analyze.symbol.Symbol)

Example 13 with Symbol

use of io.crate.analyze.symbol.Symbol in project crate by crate.

the class OrderBy method subset.

/**
     * Create a new OrderBy with symbols that match the predicate
     */
@Nullable
public OrderBy subset(Predicate<? super Symbol> predicate) {
    List<Integer> subSet = new ArrayList<>();
    Integer i = 0;
    for (Symbol orderBySymbol : orderBySymbols) {
        if (predicate.apply(orderBySymbol)) {
            subSet.add(i);
        }
        i++;
    }
    if (subSet.isEmpty()) {
        return null;
    }
    return subset(subSet);
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) Nullable(javax.annotation.Nullable)

Example 14 with Symbol

use of io.crate.analyze.symbol.Symbol in project crate by crate.

the class QuerySpec method castOutputs.

/**
     * Tries to cast the outputs to the given types. Types might contain Null values, which indicates to skip that
     * position. The iterator needs to return exactly the same number of types as there are outputs.
     *
     * @param types an iterable providing the types
     * @return -1 if all casts where successfully applied or the position of the failed cast
     */
public int castOutputs(Iterator<DataType> types) {
    int i = 0;
    ListIterator<Symbol> outputsIt = outputs.listIterator();
    while (types.hasNext() && outputsIt.hasNext()) {
        DataType targetType = types.next();
        assert targetType != null : "targetType must not be null";
        Symbol output = outputsIt.next();
        DataType sourceType = output.valueType();
        if (!sourceType.equals(targetType)) {
            if (sourceType.isConvertableTo(targetType)) {
                Symbol castFunction = CastFunctionResolver.generateCastFunction(output, targetType, false);
                if (groupBy.isPresent()) {
                    Collections.replaceAll(groupBy.get(), output, castFunction);
                }
                if (orderBy.isPresent()) {
                    Collections.replaceAll(orderBy.get().orderBySymbols(), output, castFunction);
                }
                outputsIt.set(castFunction);
            } else if (!targetType.equals(DataTypes.UNDEFINED)) {
                return i;
            }
        }
        i++;
    }
    assert i == outputs.size() : "i must be equal to outputs.size()";
    return -1;
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) DataType(io.crate.types.DataType)

Example 15 with Symbol

use of io.crate.analyze.symbol.Symbol in project crate by crate.

the class RelationAnalyzer method symbolFromSelectOutputReferenceOrExpression.

/**
     * <h2>resolve expression by also taking alias and ordinal-reference into account</h2>
     * <p>
     * <p>
     * in group by or order by clauses it is possible to reference anything in the
     * select list by using a number or alias
     * </p>
     * <p>
     * These are allowed:
     * <pre>
     *     select name as n  ... order by n
     *     select name  ... order by 1
     *     select name ... order by other_column
     * </pre>
     */
private Symbol symbolFromSelectOutputReferenceOrExpression(Expression expression, SelectAnalysis selectAnalysis, String clause, ExpressionAnalyzer expressionAnalyzer, ExpressionAnalysisContext expressionAnalysisContext) {
    Symbol symbol;
    if (expression instanceof QualifiedNameReference) {
        List<String> parts = ((QualifiedNameReference) expression).getName().getParts();
        if (parts.size() == 1) {
            symbol = getOneOrAmbiguous(selectAnalysis.outputMultiMap(), Iterables.getOnlyElement(parts));
            if (symbol != null) {
                return symbol;
            }
        }
    }
    symbol = expressionAnalyzer.convert(expression, expressionAnalysisContext);
    if (symbol.symbolType().isValueSymbol()) {
        Literal longLiteral;
        try {
            longLiteral = io.crate.analyze.symbol.Literal.convert(symbol, DataTypes.LONG);
        } catch (ClassCastException | IllegalArgumentException e) {
            throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "Cannot use %s in %s clause", SymbolPrinter.INSTANCE.printSimple(symbol), clause));
        }
        symbol = ordinalOutputReference(selectAnalysis.outputSymbols(), longLiteral, clause);
    }
    return symbol;
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) Literal(io.crate.analyze.symbol.Literal)

Aggregations

Symbol (io.crate.analyze.symbol.Symbol)109 Test (org.junit.Test)51 CrateUnitTest (io.crate.test.integration.CrateUnitTest)40 Function (io.crate.analyze.symbol.Function)14 Input (io.crate.data.Input)14 Reference (io.crate.metadata.Reference)11 WhereClause (io.crate.analyze.WhereClause)10 InputColumn (io.crate.analyze.symbol.InputColumn)8 InputFactory (io.crate.operation.InputFactory)8 OrderBy (io.crate.analyze.OrderBy)7 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)6 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)6 AbstractScalarFunctionsTest (io.crate.operation.scalar.AbstractScalarFunctionsTest)6 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)6 StreamInput (org.elasticsearch.common.io.stream.StreamInput)6 ImmutableList (com.google.common.collect.ImmutableList)5 DocTableInfo (io.crate.metadata.doc.DocTableInfo)5 TableInfo (io.crate.metadata.table.TableInfo)5 QuerySpec (io.crate.analyze.QuerySpec)4 Literal (io.crate.analyze.symbol.Literal)4