Search in sources :

Example 11 with Literal

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

the class SelectStatementAnalyzerTest method testRewriteCountNull.

@Test
public void testRewriteCountNull() {
    var executor = SQLExecutor.builder(clusterService).build();
    AnalyzedRelation relation = executor.analyze("select count(null) from sys.nodes");
    List<Symbol> outputSymbols = relation.outputs();
    assertThat(outputSymbols.size(), is(1));
    assertThat(outputSymbols.get(0), instanceOf(Literal.class));
    assertThat(((Literal) outputSymbols.get(0)).value(), is(0L));
}
Also used : ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) SelectSymbol(io.crate.expression.symbol.SelectSymbol) Symbol(io.crate.expression.symbol.Symbol) AliasSymbol(io.crate.expression.symbol.AliasSymbol) Literal(io.crate.expression.symbol.Literal) SymbolMatchers.isLiteral(io.crate.testing.SymbolMatchers.isLiteral) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 12 with Literal

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

the class CompoundLiteralTest method testObjectConstructionWithExpressionsAsValues.

@Test
public void testObjectConstructionWithExpressionsAsValues() throws Exception {
    Literal objectLiteral = (Literal) expressions.normalize(expressions.asSymbol("{name = 1 + 2}"));
    assertThat(objectLiteral.symbolType(), is(SymbolType.LITERAL));
    assertThat(objectLiteral.value(), is(Map.<String, Object>of("name", 3)));
    Literal nestedObjectLiteral = (Literal) expressions.normalize(expressions.asSymbol("{a = {name = concat('foo', 'bar')}}"));
    @SuppressWarnings("unchecked") Map<String, Object> values = (Map<String, Object>) nestedObjectLiteral.value();
    assertThat(values, is(Map.of("a", Map.of("name", "foobar"))));
}
Also used : Literal(io.crate.expression.symbol.Literal) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 13 with Literal

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

the class CompoundLiteralTest method testObjectConstruction.

@Test
public void testObjectConstruction() throws Exception {
    Symbol s = expressions.asSymbol("{}");
    assertThat(s, instanceOf(Literal.class));
    Literal l = (Literal) s;
    assertThat(l.value(), is(new HashMap<String, Object>()));
    Literal objectLiteral = (Literal) expressions.normalize(expressions.asSymbol("{ident='value'}"));
    assertThat(objectLiteral.symbolType(), is(SymbolType.LITERAL));
    assertThat(objectLiteral.valueType().id(), is(ObjectType.ID));
    assertThat(objectLiteral.value(), is(Map.of("ident", "value")));
    Literal multipleObjectLiteral = (Literal) expressions.normalize(expressions.asSymbol("{\"Ident\"=123.4, a={}, ident='string'}"));
    Map<String, Object> values = (Map<String, Object>) multipleObjectLiteral.value();
    assertThat(values, is(MapBuilder.<String, Object>newMapBuilder().put("Ident", 123.4d).put("a", new HashMap<String, Object>()).put("ident", "string").map()));
}
Also used : HashMap(java.util.HashMap) Symbol(io.crate.expression.symbol.Symbol) Literal(io.crate.expression.symbol.Literal) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 14 with Literal

use of io.crate.expression.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(context.txnCtx);
    List<Input<?>> inputs = null;
    if (!projection.inputs().isEmpty()) {
        ctx.add(projection.inputs());
        inputs = ctx.topLevelInputs();
    }
    projection = projection.normalize(normalizer, context.txnCtx);
    String uri = DataTypes.STRING.sanitizeValue(SymbolEvaluator.evaluate(context.txnCtx, nodeCtx, projection.uri(), Row.EMPTY, SubQueryResults.EMPTY));
    assert uri != null : "URI must not be null";
    StringBuilder sb = new StringBuilder(uri);
    Symbol resolvedFileName = normalizer.normalize(WriterProjection.DIRECTORY_TO_FILENAME, context.txnCtx);
    assert resolvedFileName instanceof Literal : "resolvedFileName must be a Literal, but is: " + resolvedFileName;
    assert resolvedFileName.valueType().id() == StringType.ID : "resolvedFileName.valueType() must be " + StringType.INSTANCE;
    String fileName = (String) ((Literal) resolvedFileName).value();
    if (!uri.endsWith("/")) {
        sb.append("/");
    }
    sb.append(fileName);
    if (projection.compressionType() == WriterProjection.CompressionType.GZIP) {
        sb.append(".gz");
    }
    uri = sb.toString();
    Map<ColumnIdent, Object> overwrites = symbolMapToObject(projection.overwrites(), ctx, context.txnCtx);
    return new FileWriterProjector(threadPool.generic(), uri, projection.compressionType(), inputs, ctx.expressions(), overwrites, projection.outputNames(), projection.outputFormat(), fileOutputFactoryMap, projection.withClauseOptions());
}
Also used : InputFactory(io.crate.expression.InputFactory) Symbol(io.crate.expression.symbol.Symbol) NestableCollectExpression(io.crate.execution.engine.collect.NestableCollectExpression) CollectExpression(io.crate.execution.engine.collect.CollectExpression) ColumnIdent(io.crate.metadata.ColumnIdent) Input(io.crate.data.Input) FileWriterProjector(io.crate.execution.engine.export.FileWriterProjector) Literal(io.crate.expression.symbol.Literal)

Example 15 with Literal

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

the class RefAndLiteral method of.

@Nullable
public static RefAndLiteral of(Function function) {
    List<Symbol> args = function.arguments();
    assert args.size() == 2 : "Function must have 2 arguments";
    Symbol fst = args.get(0);
    Symbol snd = args.get(1);
    if (fst instanceof Reference && snd instanceof Literal) {
        return new RefAndLiteral((Reference) fst, (Literal) snd);
    } else {
        return null;
    }
}
Also used : Symbol(io.crate.expression.symbol.Symbol) Reference(io.crate.metadata.Reference) Literal(io.crate.expression.symbol.Literal) Nullable(javax.annotation.Nullable)

Aggregations

Literal (io.crate.expression.symbol.Literal)27 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)17 Symbol (io.crate.expression.symbol.Symbol)16 Test (org.junit.Test)16 SymbolMatchers.isLiteral (io.crate.testing.SymbolMatchers.isLiteral)13 HashMap (java.util.HashMap)12 ColumnIdent (io.crate.metadata.ColumnIdent)11 Reference (io.crate.metadata.Reference)11 Map (java.util.Map)11 Function (io.crate.expression.symbol.Function)5 DocTableInfo (io.crate.metadata.doc.DocTableInfo)5 List (java.util.List)5 Nullable (javax.annotation.Nullable)5 Lists2 (io.crate.common.collections.Lists2)4 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)4 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)4 DocTableRelation (io.crate.analyze.relations.DocTableRelation)3 Iterables (io.crate.common.collections.Iterables)3 Tuple (io.crate.common.collections.Tuple)3 Input (io.crate.data.Input)3