Search in sources :

Example 16 with Symbol

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

the class RelationAnalyzer method rewriteGlobalDistinct.

@Nullable
private List<Symbol> rewriteGlobalDistinct(List<Symbol> outputSymbols) {
    List<Symbol> groupBy = new ArrayList<>(outputSymbols.size());
    for (Symbol symbol : outputSymbols) {
        if (!Aggregations.containsAggregation(symbol)) {
            GroupBySymbolValidator.validate(symbol);
            groupBy.add(symbol);
        }
    }
    return groupBy;
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) Nullable(javax.annotation.Nullable)

Example 17 with Symbol

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

the class AnalyzedTableElements method processGeneratedExpression.

private void processGeneratedExpression(ExpressionAnalyzer expressionAnalyzer, SymbolPrinter symbolPrinter, AnalyzedColumnDefinition columnDefinition, ExpressionAnalysisContext expressionAnalysisContext) {
    // validate expression
    Symbol function = expressionAnalyzer.convert(columnDefinition.generatedExpression(), expressionAnalysisContext);
    String formattedExpression;
    DataType valueType = function.valueType();
    DataType definedType = columnDefinition.dataType() == null ? null : DataTypes.ofMappingNameSafe(columnDefinition.dataType());
    // check for optional defined type and add `cast` to expression if possible
    if (definedType != null && !definedType.equals(valueType)) {
        Preconditions.checkArgument(valueType.isConvertableTo(definedType), "generated expression value type '%s' not supported for conversion to '%s'", valueType, definedType.getName());
        Symbol castFunction = CastFunctionResolver.generateCastFunction(function, definedType, false);
        // no full qualified references here
        formattedExpression = symbolPrinter.print(castFunction, SymbolPrinter.Style.PARSEABLE_NOT_QUALIFIED);
    } else {
        columnDefinition.dataType(function.valueType().getName());
        // no full qualified references here
        formattedExpression = symbolPrinter.print(function, SymbolPrinter.Style.PARSEABLE_NOT_QUALIFIED);
    }
    columnDefinition.formattedGeneratedExpression(formattedExpression);
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) DataType(io.crate.types.DataType)

Example 18 with Symbol

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

the class CopyAnalyzer method createWhereClause.

private WhereClause createWhereClause(Optional<Expression> where, DocTableRelation tableRelation, List<String> partitions, EvaluatingNormalizer normalizer, ExpressionAnalyzer expressionAnalyzer, ExpressionAnalysisContext expressionAnalysisContext, TransactionContext transactionContext) {
    WhereClause whereClause = null;
    if (where.isPresent()) {
        WhereClauseAnalyzer whereClauseAnalyzer = new WhereClauseAnalyzer(functions, tableRelation);
        Symbol query = expressionAnalyzer.convert(where.get(), expressionAnalysisContext);
        whereClause = new WhereClause(normalizer.normalize(query, transactionContext));
        whereClause = whereClauseAnalyzer.analyze(whereClause, transactionContext);
    }
    if (whereClause == null) {
        return new WhereClause(null, null, partitions);
    } else if (whereClause.noMatch()) {
        return whereClause;
    } else {
        if (!whereClause.partitions().isEmpty() && !partitions.isEmpty() && !whereClause.partitions().equals(partitions)) {
            throw new IllegalArgumentException("Given partition ident does not match partition evaluated from where clause");
        }
        return new WhereClause(whereClause.query(), whereClause.docKeys().orElse(null), partitions.isEmpty() ? whereClause.partitions() : partitions);
    }
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) WhereClauseAnalyzer(io.crate.analyze.where.WhereClauseAnalyzer)

Example 19 with Symbol

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

the class CopyAnalyzer method settingsFromProperties.

private Settings settingsFromProperties(Map<String, Expression> properties, ExpressionAnalyzer expressionAnalyzer, ExpressionAnalysisContext expressionAnalysisContext) {
    Settings.Builder builder = Settings.builder();
    for (Map.Entry<String, Expression> entry : properties.entrySet()) {
        String key = entry.getKey();
        Expression expression = entry.getValue();
        if (expression instanceof ArrayLiteral) {
            throw new IllegalArgumentException("Invalid argument(s) passed to parameter");
        }
        if (expression instanceof QualifiedNameReference) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Can't use column reference in property assignment \"%s = %s\". Use literals instead.", key, ((QualifiedNameReference) expression).getName().toString()));
        }
        Symbol v = expressionAnalyzer.convert(expression, expressionAnalysisContext);
        if (!v.symbolType().isValueSymbol()) {
            throw new UnsupportedFeatureException("Only literals are allowed as parameter values");
        }
        builder.put(key, ValueSymbolVisitor.STRING.process(v));
    }
    return builder.build();
}
Also used : UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) Symbol(io.crate.analyze.symbol.Symbol) ImmutableMap(com.google.common.collect.ImmutableMap) Settings(org.elasticsearch.common.settings.Settings)

Example 20 with Symbol

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

the class ReferenceToLiteralConverterTest method testReplaceSubscriptReference.

@Test
public void testReplaceSubscriptReference() throws Exception {
    Object[] inputValues = new Object[] { MapBuilder.newMapBuilder().put("name", "Ford").map() };
    Reference userRef = new Reference(new ReferenceIdent(TABLE_IDENT, new ColumnIdent("user")), RowGranularity.DOC, DataTypes.OBJECT);
    Reference nameRef = new Reference(new ReferenceIdent(TABLE_IDENT, new ColumnIdent("user", ImmutableList.of("name"))), RowGranularity.DOC, DataTypes.STRING);
    ReferenceToLiteralConverter.Context context = new ReferenceToLiteralConverter.Context(ImmutableList.of(userRef), ImmutableList.of(nameRef));
    context.values(inputValues);
    Symbol replacedSymbol = REFERENCE_TO_LITERAL_CONVERTER.process(nameRef, context);
    assertThat(replacedSymbol, isLiteral("Ford", DataTypes.STRING));
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

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