Search in sources :

Example 11 with Literal

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

the class ProjectionToProjectorVisitor method visitWriterProjection.

@Override
public Projector visitWriterProjection(WriterProjection projection, Context context) {
    InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns();
    List<Input<?>> inputs = null;
    if (!projection.inputs().isEmpty()) {
        ctx.add(projection.inputs());
        inputs = ctx.topLevelInputs();
    }
    Map<ColumnIdent, Object> overwrites = symbolMapToObject(projection.overwrites(), ctx, context.transactionContext);
    projection = projection.normalize(normalizer, context.transactionContext);
    String uri = ValueSymbolVisitor.STRING.process(projection.uri());
    assert uri != null : "URI must not be null";
    StringBuilder sb = new StringBuilder(uri);
    Symbol resolvedFileName = normalizer.normalize(WriterProjection.DIRECTORY_TO_FILENAME, context.transactionContext);
    assert resolvedFileName instanceof Literal : "resolvedFileName must be a Literal, but is: " + resolvedFileName;
    assert resolvedFileName.valueType() == StringType.INSTANCE : "resolvedFileName.valueType() must be " + StringType.INSTANCE;
    String fileName = ValueSymbolVisitor.STRING.process(resolvedFileName);
    if (!uri.endsWith("/")) {
        sb.append("/");
    }
    sb.append(fileName);
    if (projection.compressionType() == WriterProjection.CompressionType.GZIP) {
        sb.append(".gz");
    }
    uri = sb.toString();
    return new FileWriterProjector(((ThreadPoolExecutor) threadPool.generic()), uri, projection.compressionType(), inputs, ctx.expressions(), overwrites, projection.outputNames(), projection.outputFormat());
}
Also used : InputFactory(io.crate.operation.InputFactory) Symbol(io.crate.analyze.symbol.Symbol) CollectExpression(io.crate.operation.collect.CollectExpression) Input(io.crate.data.Input) Literal(io.crate.analyze.symbol.Literal) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 12 with Literal

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

the class DateTruncFunction method normalizeSymbol.

@Override
public Symbol normalizeSymbol(Function symbol, TransactionContext transactionContext) {
    assert symbol.arguments().size() > 1 && symbol.arguments().size() < 4 : "Invalid number of arguments";
    if (anyNonLiterals(symbol.arguments())) {
        return symbol;
    }
    Literal interval = (Literal) symbol.arguments().get(0);
    Literal tsSymbol;
    Literal timezone;
    if (symbol.arguments().size() == 2) {
        timezone = TimeZoneParser.DEFAULT_TZ_LITERAL;
        tsSymbol = (Literal) symbol.arguments().get(1);
    } else {
        timezone = (Literal) symbol.arguments().get(1);
        tsSymbol = (Literal) symbol.arguments().get(2);
    }
    return Literal.of(DataTypes.TIMESTAMP, evaluate(new Input[] { interval, timezone, tsSymbol }));
}
Also used : Input(io.crate.data.Input) Literal(io.crate.analyze.symbol.Literal)

Example 13 with Literal

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

the class ValueNormalizer method normalizeInputForReference.

/**
     * normalize and validate given value according to the corresponding {@link io.crate.metadata.Reference}
     *
     * @param valueSymbol the value to normalize, might be anything from {@link io.crate.metadata.Scalar} to {@link io.crate.analyze.symbol.Literal}
     * @param reference   the reference to which the value has to comply in terms of type-compatibility
     * @return the normalized Symbol, should be a literal
     * @throws io.crate.exceptions.ColumnValidationException
     */
public Symbol normalizeInputForReference(Symbol valueSymbol, Reference reference) {
    assert valueSymbol != null : "valueSymbol must not be null";
    DataType<?> targetType = getTargetType(valueSymbol, reference);
    if (!(valueSymbol instanceof Literal)) {
        return ExpressionAnalyzer.castIfNeededOrFail(valueSymbol, targetType);
    }
    Literal literal = (Literal) valueSymbol;
    try {
        literal = Literal.convert(literal, reference.valueType());
    } catch (ConversionException e) {
        throw new ColumnValidationException(reference.ident().columnIdent().name(), String.format(Locale.ENGLISH, "%s cannot be cast to type %s", SymbolPrinter.INSTANCE.printSimple(valueSymbol), reference.valueType().getName()));
    }
    Object value = literal.value();
    if (value == null) {
        return literal;
    }
    try {
        if (targetType == DataTypes.OBJECT) {
            //noinspection unchecked
            normalizeObjectValue((Map) value, reference);
        } else if (isObjectArray(targetType)) {
            normalizeObjectArrayValue((Object[]) value, reference);
        }
    } catch (ConversionException e) {
        throw new ColumnValidationException(reference.ident().columnIdent().name(), SymbolFormatter.format("\"%s\" has a type that can't be implicitly cast to that of \"%s\" (" + reference.valueType().getName() + ")", literal, reference));
    }
    return literal;
}
Also used : ConversionException(io.crate.exceptions.ConversionException) Literal(io.crate.analyze.symbol.Literal) ColumnValidationException(io.crate.exceptions.ColumnValidationException)

Example 14 with Literal

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

the class CompoundLiteralTest method testArrayConstructionWithOnlyLiterals.

@Test
public void testArrayConstructionWithOnlyLiterals() throws Exception {
    Literal emptyArray = (Literal) analyzeExpression("[]");
    assertThat((Object[]) emptyArray.value(), is(new Object[0]));
    assertThat(emptyArray.valueType(), is((DataType) new ArrayType(UndefinedType.INSTANCE)));
    Literal singleArray = (Literal) analyzeExpression("[1]");
    assertThat(singleArray.valueType(), is((DataType) new ArrayType(LongType.INSTANCE)));
    assertThat(((Object[]) singleArray.value()).length, is(1));
    assertThat(((Object[]) singleArray.value())[0], is((Object) 1L));
    Literal multiArray = (Literal) analyzeExpression("[1, 2, 3]");
    assertThat(multiArray.valueType(), is((DataType) new ArrayType(LongType.INSTANCE)));
    assertThat(((Object[]) multiArray.value()).length, is(3));
    assertThat((Object[]) multiArray.value(), is(new Object[] { 1L, 2L, 3L }));
}
Also used : Literal(io.crate.analyze.symbol.Literal) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 15 with Literal

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

the class AnyLikeOperatorTest method normalizeSymbol.

private static Symbol normalizeSymbol(String pattern, String... expressions) {
    Literal patternLiteral = Literal.of(pattern);
    Object[] value = new Object[expressions.length];
    for (int i = 0; i < expressions.length; i++) {
        value[i] = expressions[i] == null ? null : new BytesRef(expressions[i]);
    }
    Literal valuesLiteral = Literal.of(new ArrayType(DataTypes.STRING), value);
    AnyLikeOperator impl = (AnyLikeOperator) new AnyLikeOperator.AnyLikeResolver().getForTypes(Arrays.asList(patternLiteral.valueType(), valuesLiteral.valueType()));
    Function function = new Function(impl.info(), Arrays.<Symbol>asList(patternLiteral, valuesLiteral));
    return impl.normalizeSymbol(function, new TransactionContext(SessionContext.SYSTEM_SESSION));
}
Also used : ArrayType(io.crate.types.ArrayType) Function(io.crate.analyze.symbol.Function) TransactionContext(io.crate.metadata.TransactionContext) Literal(io.crate.analyze.symbol.Literal) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

Literal (io.crate.analyze.symbol.Literal)15 BytesRef (org.apache.lucene.util.BytesRef)8 ArrayType (io.crate.types.ArrayType)6 Function (io.crate.analyze.symbol.Function)4 Symbol (io.crate.analyze.symbol.Symbol)4 Input (io.crate.data.Input)4 TransactionContext (io.crate.metadata.TransactionContext)4 CrateUnitTest (io.crate.test.integration.CrateUnitTest)3 Test (org.junit.Test)3 ImmutableList (com.google.common.collect.ImmutableList)2 WhereClause (io.crate.analyze.WhereClause)2 FunctionImplementation (io.crate.metadata.FunctionImplementation)2 NotPredicate (io.crate.operation.predicate.NotPredicate)2 EvaluatingNormalizer (io.crate.analyze.EvaluatingNormalizer)1 SymbolToTrueVisitor (io.crate.analyze.SymbolToTrueVisitor)1 ColumnValidationException (io.crate.exceptions.ColumnValidationException)1 ConversionException (io.crate.exceptions.ConversionException)1 InputFactory (io.crate.operation.InputFactory)1 CollectExpression (io.crate.operation.collect.CollectExpression)1 PartitionExpression (io.crate.operation.reference.partitioned.PartitionExpression)1