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());
}
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 }));
}
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;
}
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 }));
}
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));
}
Aggregations