Search in sources :

Example 6 with Function

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

the class SubscriptObjectFunction method tryToInferReturnTypeFromObjectTypeAndArguments.

static Symbol tryToInferReturnTypeFromObjectTypeAndArguments(Function func) {
    if (!func.valueType().equals(DataTypes.UNDEFINED)) {
        return func;
    }
    var arguments = func.arguments();
    ObjectType objectType = (ObjectType) arguments.get(0).valueType();
    List<String> path = maybeCreatePath(arguments);
    if (path == null) {
        return func;
    } else {
        DataType<?> returnType = objectType.resolveInnerType(path);
        return returnType.equals(DataTypes.UNDEFINED) ? func : new Function(func.signature(), func.arguments(), returnType);
    }
}
Also used : ObjectType(io.crate.types.ObjectType) Function(io.crate.expression.symbol.Function)

Example 7 with Function

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

the class ScalarsAndRefsToTrue method visitFunction.

@Override
public Symbol visitFunction(Function symbol, Void context) {
    String functionName = symbol.name();
    if (functionName.equals(NotPredicate.NAME)) {
        Symbol argument = symbol.arguments().get(0);
        if (argument instanceof Reference) {
            return argument.accept(this, context);
        } else if (argument instanceof Function) {
            if (!Operators.LOGICAL_OPERATORS.contains(((Function) argument).name())) {
                return argument.accept(this, context);
            }
        }
    }
    List<Symbol> newArgs = new ArrayList<>(symbol.arguments().size());
    boolean allLiterals = true;
    boolean isNull = false;
    for (Symbol arg : symbol.arguments()) {
        Symbol processedArg = arg.accept(this, context);
        newArgs.add(processedArg);
        if (!processedArg.symbolType().isValueSymbol()) {
            allLiterals = false;
        }
        if (processedArg.valueType().id() == DataTypes.UNDEFINED.id()) {
            isNull = true;
        }
    }
    if (allLiterals && !Operators.LOGICAL_OPERATORS.contains(functionName)) {
        return isNull ? Literal.NULL : Literal.BOOLEAN_TRUE;
    }
    return new Function(symbol.signature(), newArgs, symbol.valueType());
}
Also used : Function(io.crate.expression.symbol.Function) Symbol(io.crate.expression.symbol.Symbol) Reference(io.crate.metadata.Reference) ArrayList(java.util.ArrayList)

Example 8 with Function

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

the class InputColumnsTest method testNonDeterministicFunctionsReplacement.

@Test
public void testNonDeterministicFunctionsReplacement() throws Exception {
    Function fn1 = (Function) sqlExpressions.asSymbol("random()");
    Function fn2 = (Function) sqlExpressions.asSymbol("random()");
    List<Symbol> inputSymbols = Arrays.<Symbol>asList(Literal.BOOLEAN_FALSE, sqlExpressions.asSymbol("upper(a)"), fn1, fn2);
    Function newSameFn = (Function) sqlExpressions.asSymbol("random()");
    Function newDifferentFn = (Function) sqlExpressions.asSymbol("random()");
    InputColumns.SourceSymbols sourceSymbols = new InputColumns.SourceSymbols(inputSymbols);
    Symbol replaced1 = InputColumns.create(fn1, sourceSymbols);
    assertThat(replaced1, is(instanceOf(InputColumn.class)));
    assertThat(((InputColumn) replaced1).index(), is(2));
    Symbol replaced2 = InputColumns.create(fn2, sourceSymbols);
    assertThat(replaced2, is(instanceOf(InputColumn.class)));
    assertThat(((InputColumn) replaced2).index(), is(3));
    Symbol replaced3 = InputColumns.create(newSameFn, sourceSymbols);
    // not replaced
    assertThat(replaced3, is(equalTo(newSameFn)));
    Symbol replaced4 = InputColumns.create(newDifferentFn, sourceSymbols);
    // not replaced
    assertThat(replaced4, is(equalTo(newDifferentFn)));
}
Also used : Function(io.crate.expression.symbol.Function) Symbol(io.crate.expression.symbol.Symbol) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 9 with Function

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

the class ProjectionToProjectorVisitorTest method testFilterProjection.

@Test
public void testFilterProjection() throws Exception {
    List<Symbol> arguments = Arrays.asList(Literal.of(2), new InputColumn(1));
    EqOperator op = (EqOperator) nodeCtx.functions().get(null, EqOperator.NAME, arguments, SearchPath.pathWithPGCatalogAndDoc());
    Function function = new Function(op.signature(), arguments, EqOperator.RETURN_TYPE);
    FilterProjection projection = new FilterProjection(function, Arrays.asList(new InputColumn(0), new InputColumn(1)));
    Projector projector = visitor.create(projection, txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, UUID.randomUUID());
    assertThat(projector, instanceOf(FilterProjector.class));
    List<Object[]> rows = new ArrayList<>();
    rows.add($("human", 2));
    rows.add($("vogon", 1));
    BatchIterator<Row> filteredBI = projector.apply(InMemoryBatchIterator.of(new CollectionBucket(rows), SENTINEL, true));
    TestingRowConsumer consumer = new TestingRowConsumer();
    consumer.accept(filteredBI, null);
    Bucket bucket = consumer.getBucket();
    assertThat(bucket.size(), is(1));
}
Also used : Projector(io.crate.data.Projector) SortingProjector(io.crate.execution.engine.sort.SortingProjector) SortingTopNProjector(io.crate.execution.engine.sort.SortingTopNProjector) GroupingProjector(io.crate.execution.engine.aggregation.GroupingProjector) FilterProjection(io.crate.execution.dsl.projection.FilterProjection) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) Function(io.crate.expression.symbol.Function) EqOperator(io.crate.expression.operator.EqOperator) Bucket(io.crate.data.Bucket) CollectionBucket(io.crate.data.CollectionBucket) InputColumn(io.crate.expression.symbol.InputColumn) Row(io.crate.data.Row) TestingHelpers.isRow(io.crate.testing.TestingHelpers.isRow) CollectionBucket(io.crate.data.CollectionBucket) TestingRowConsumer(io.crate.testing.TestingRowConsumer) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 10 with Function

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

the class ProjectingRowConsumerTest method testConsumerRequiresScrollAndProjectorsDontSupportScrolling.

@Test
public void testConsumerRequiresScrollAndProjectorsDontSupportScrolling() {
    List<Symbol> arguments = Arrays.asList(Literal.of(2), new InputColumn(1, DataTypes.INTEGER));
    EqOperator op = (EqOperator) nodeCtx.functions().get(null, EqOperator.NAME, arguments, SearchPath.pathWithPGCatalogAndDoc());
    Function function = new Function(op.signature(), arguments, EqOperator.RETURN_TYPE);
    FilterProjection filterProjection = new FilterProjection(function, Arrays.asList(new InputColumn(0), new InputColumn(1)));
    RowConsumer delegateConsumerRequiresScroll = new DummyRowConsumer(true);
    RowConsumer projectingConsumer = ProjectingRowConsumer.create(delegateConsumerRequiresScroll, Collections.singletonList(filterProjection), UUID.randomUUID(), txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, projectorFactory);
    assertThat(projectingConsumer.requiresScroll(), is(true));
}
Also used : Function(io.crate.expression.symbol.Function) FilterProjection(io.crate.execution.dsl.projection.FilterProjection) EqOperator(io.crate.expression.operator.EqOperator) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) RowConsumer(io.crate.data.RowConsumer) TestingRowConsumer(io.crate.testing.TestingRowConsumer) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

Function (io.crate.expression.symbol.Function)80 Test (org.junit.Test)56 Symbol (io.crate.expression.symbol.Symbol)43 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)43 SymbolMatchers.isFunction (io.crate.testing.SymbolMatchers.isFunction)35 SubscriptFunction (io.crate.expression.scalar.SubscriptFunction)25 TryCastFunction (io.crate.expression.scalar.cast.TryCastFunction)24 ExplicitCastFunction (io.crate.expression.scalar.cast.ExplicitCastFunction)23 DistanceFunction (io.crate.expression.scalar.geo.DistanceFunction)23 AliasSymbol (io.crate.expression.symbol.AliasSymbol)13 ParameterSymbol (io.crate.expression.symbol.ParameterSymbol)13 SelectSymbol (io.crate.expression.symbol.SelectSymbol)13 InputColumn (io.crate.expression.symbol.InputColumn)10 ArrayList (java.util.ArrayList)10 Literal (io.crate.expression.symbol.Literal)9 ArraySliceFunction (io.crate.expression.scalar.ArraySliceFunction)8 FunctionImplementation (io.crate.metadata.FunctionImplementation)8 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)7 ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)6 Reference (io.crate.metadata.Reference)6