Search in sources :

Example 21 with Function

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

the class InsertPlannerTest method testInsertFromQueryWithPartitionedColumn.

@Test
public void testInsertFromQueryWithPartitionedColumn() throws Exception {
    Merge planNode = e.plan("insert into users (id, date) (select id, date from parted_pks)");
    Collect queryAndFetch = (Collect) planNode.subPlan();
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) queryAndFetch.collectPhase());
    List<Symbol> toCollect = collectPhase.toCollect();
    assertThat(toCollect.size(), is(2));
    assertThat(toCollect.get(0), isFunction("to_long"));
    assertThat(((Function) toCollect.get(0)).arguments().get(0), isReference("_doc['id']"));
    assertThat((Reference) toCollect.get(1), equalTo(new Reference(new ReferenceIdent(TableDefinitions.PARTED_PKS_IDENT, "date"), RowGranularity.PARTITION, DataTypes.TIMESTAMP)));
}
Also used : Function(io.crate.analyze.symbol.Function) Collect(io.crate.planner.node.dql.Collect) Symbol(io.crate.analyze.symbol.Symbol) Reference(io.crate.metadata.Reference) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) ReferenceIdent(io.crate.metadata.ReferenceIdent) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 22 with Function

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

the class LogFunctionTest method testNormalizeReference.

@Test
public void testNormalizeReference() throws Exception {
    Reference dB = createReference("dB", DataTypes.DOUBLE);
    LogFunction log10 = getFunction(LogFunction.NAME, DataTypes.DOUBLE);
    Function function = new Function(log10.info(), Arrays.<Symbol>asList(dB));
    Function normalized = (Function) log10.normalizeSymbol(function, transactionContext);
    assertThat(normalized, Matchers.sameInstance(function));
    LogFunction ln = getFunction(LogFunction.LnFunction.NAME, DataTypes.DOUBLE);
    function = new Function(ln.info(), Arrays.<Symbol>asList(dB));
    normalized = (Function) ln.normalizeSymbol(function, transactionContext);
    assertThat(normalized, Matchers.sameInstance(function));
    LogFunction logBase = getFunction(LogFunction.NAME, DataTypes.DOUBLE, DataTypes.LONG);
    function = new Function(logBase.info(), Arrays.<Symbol>asList(dB, Literal.of(10L)));
    normalized = (Function) logBase.normalizeSymbol(function, transactionContext);
    assertThat(normalized, Matchers.sameInstance(function));
    Reference base = createReference("base", DataTypes.INTEGER);
    function = new Function(logBase.info(), Arrays.<Symbol>asList(dB, base));
    normalized = (Function) logBase.normalizeSymbol(function, transactionContext);
    assertThat(normalized, Matchers.sameInstance(function));
}
Also used : Function(io.crate.analyze.symbol.Function) TestingHelpers.createReference(io.crate.testing.TestingHelpers.createReference) Reference(io.crate.metadata.Reference) Symbol(io.crate.analyze.symbol.Symbol) AbstractScalarFunctionsTest(io.crate.operation.scalar.AbstractScalarFunctionsTest) Test(org.junit.Test)

Example 23 with Function

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

the class MultiSourceAggregationConsumer method removeAggregationsAndLimitsFromMSS.

private static void removeAggregationsAndLimitsFromMSS(MultiSourceSelect mss, SplitPoints splitPoints) {
    QuerySpec querySpec = mss.querySpec();
    List<Symbol> outputs = Lists2.concatUnique(splitPoints.toCollect(), extractFieldsFromJoinConditions(mss));
    querySpec.outputs(outputs);
    querySpec.hasAggregates(false);
    // Limit & offset must be applied after the aggregation, so remove it from mss and sources.
    // OrderBy can be ignored because it's also applied after aggregation but there is always only 1 row so it
    // wouldn't have any effect.
    removeLimitOffsetAndOrder(querySpec);
    for (RelationSource relationSource : mss.sources().values()) {
        removeLimitOffsetAndOrder(relationSource.querySpec());
    }
    // need to change the types on the fields of the MSS to match the new outputs
    ListIterator<Field> fieldsIt = mss.fields().listIterator();
    Iterator<Function> outputsIt = splitPoints.aggregates().iterator();
    while (fieldsIt.hasNext()) {
        Field field = fieldsIt.next();
        Symbol output = outputsIt.next();
        fieldsIt.set(new Field(field.relation(), field.path(), output.valueType()));
    }
}
Also used : Field(io.crate.analyze.symbol.Field) Function(io.crate.analyze.symbol.Function) RelationSource(io.crate.analyze.RelationSource) Symbol(io.crate.analyze.symbol.Symbol) QuerySpec(io.crate.analyze.QuerySpec)

Example 24 with Function

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

the class WithinFunction method normalizeSymbol.

@Override
public Symbol normalizeSymbol(Function symbol, TransactionContext transactionContext) {
    Symbol left = symbol.arguments().get(0);
    Symbol right = symbol.arguments().get(1);
    boolean literalConverted = false;
    short numLiterals = 0;
    if (left.symbolType().isValueSymbol()) {
        numLiterals++;
        Symbol converted = convertTo(DataTypes.GEO_POINT, (Literal) left);
        literalConverted = converted != right;
        left = converted;
    }
    if (right.symbolType().isValueSymbol()) {
        numLiterals++;
        Symbol converted = convertTo(DataTypes.GEO_SHAPE, (Literal) right);
        literalConverted = literalConverted || converted != right;
        right = converted;
    }
    if (numLiterals == 2) {
        return Literal.of(evaluate((Input) left, (Input) right));
    }
    if (literalConverted) {
        return new Function(SHAPE_INFO, Arrays.asList(left, right));
    }
    return symbol;
}
Also used : Function(io.crate.analyze.symbol.Function) Input(io.crate.data.Input) Symbol(io.crate.analyze.symbol.Symbol)

Example 25 with Function

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

the class ExpressionAnalyzerTest method testSwapFunctionLeftSide.

@Test
public void testSwapFunctionLeftSide() throws Exception {
    SqlExpressions expressions = new SqlExpressions(T3.SOURCES);
    Function cmp = (Function) expressions.normalize(expressions.asSymbol("8 + 5 > t1.x"));
    // the comparison was swapped so the field is on the left side
    assertThat(cmp.info().ident().name(), is("op_<"));
    assertThat(cmp.arguments().get(0), isField("x"));
}
Also used : Function(io.crate.analyze.symbol.Function) SqlExpressions(io.crate.testing.SqlExpressions) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

Function (io.crate.analyze.symbol.Function)40 Test (org.junit.Test)25 Symbol (io.crate.analyze.symbol.Symbol)19 CrateUnitTest (io.crate.test.integration.CrateUnitTest)18 TransactionContext (io.crate.metadata.TransactionContext)8 Input (io.crate.data.Input)5 TableInfo (io.crate.metadata.table.TableInfo)5 Literal (io.crate.analyze.symbol.Literal)4 ArrayType (io.crate.types.ArrayType)4 QuerySpec (io.crate.analyze.QuerySpec)3 WhereClause (io.crate.analyze.WhereClause)3 Reference (io.crate.metadata.Reference)3 AbstractScalarFunctionsTest (io.crate.operation.scalar.AbstractScalarFunctionsTest)3 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)3 BytesRef (org.apache.lucene.util.BytesRef)3 SessionContext (io.crate.action.sql.SessionContext)2 OrderBy (io.crate.analyze.OrderBy)2 FullQualifedNameFieldProvider (io.crate.analyze.relations.FullQualifedNameFieldProvider)2 QueriedDocTable (io.crate.analyze.relations.QueriedDocTable)2 Field (io.crate.analyze.symbol.Field)2