Search in sources :

Example 46 with Function

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

the class ProjectSet method pruneOutputsExcept.

@Override
public LogicalPlan pruneOutputsExcept(TableStats tableStats, Collection<Symbol> outputsToKeep) {
    HashSet<Symbol> toKeep = new HashSet<>();
    LinkedHashSet<Symbol> newStandalone = new LinkedHashSet<>();
    for (Symbol outputToKeep : outputsToKeep) {
        SymbolVisitors.intersection(outputToKeep, standalone, newStandalone::add);
    }
    for (Function tableFunction : tableFunctions) {
        SymbolVisitors.intersection(tableFunction, source.outputs(), toKeep::add);
    }
    toKeep.addAll(newStandalone);
    LogicalPlan newSource = source.pruneOutputsExcept(tableStats, toKeep);
    if (newSource == source) {
        return this;
    }
    return new ProjectSet(newSource, tableFunctions, List.copyOf(newStandalone));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Function(io.crate.expression.symbol.Function) Symbol(io.crate.expression.symbol.Symbol) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 47 with Function

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

the class EvaluatingNormalizerTest method prepareFunctionTree.

/**
 * prepare the following where clause as function symbol tree:
 * <p>
 * where test.dummy.load = 0.08 or name != 'x' and name != 'y'
 * <p>
 * test.dummy.load is a expression that can be evaluated on node level
 * name would be a doc level reference and is untouched
 */
private Function prepareFunctionTree() {
    Reference load_1 = dummyLoadInfo;
    Literal<Double> d01 = Literal.of(0.08);
    Function load_eq_01 = new Function(EqOperator.SIGNATURE, List.of(load_1, d01), EqOperator.RETURN_TYPE);
    Symbol name_ref = new Reference(new ReferenceIdent(new RelationName(Schemas.DOC_SCHEMA_NAME, "foo"), "name"), RowGranularity.DOC, DataTypes.STRING, 0, null);
    Symbol x_literal = Literal.of("x");
    Symbol y_literal = Literal.of("y");
    Function name_eq_x = new Function(EqOperator.SIGNATURE, List.of(name_ref, x_literal), EqOperator.RETURN_TYPE);
    Function nameNeqX = new Function(NotPredicate.SIGNATURE, Collections.singletonList(name_eq_x), NotPredicate.SIGNATURE.getReturnType().createType());
    Function name_eq_y = new Function(EqOperator.SIGNATURE, List.of(name_ref, y_literal), EqOperator.RETURN_TYPE);
    Function nameNeqY = new Function(NotPredicate.SIGNATURE, Collections.singletonList(name_eq_y), NotPredicate.SIGNATURE.getReturnType().createType());
    Function op_and = new Function(AndOperator.SIGNATURE, List.of(nameNeqX, nameNeqY), AndOperator.RETURN_TYPE);
    return new Function(OrOperator.SIGNATURE, List.of(load_eq_01, op_and), OrOperator.RETURN_TYPE);
}
Also used : Function(io.crate.expression.symbol.Function) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) RelationName(io.crate.metadata.RelationName) ReferenceIdent(io.crate.metadata.ReferenceIdent)

Example 48 with Function

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

the class GroupByAnalyzerTest method testGroupByHavingOtherColumnInAggregate.

@Test
public void testGroupByHavingOtherColumnInAggregate() throws Exception {
    QueriedSelectRelation relation = analyze("select sum(floats), name from users group by name having max(bytes) = 4::char");
    assertThat(relation.having(), isFunction("op_="));
    Function havingFunction = (Function) relation.having();
    assertThat(havingFunction.arguments().size(), is(2));
    assertThat(havingFunction.arguments().get(0), isFunction("max"));
    Function maxFunction = (Function) havingFunction.arguments().get(0);
    assertThat(maxFunction.arguments().get(0), isReference("bytes"));
    assertThat(havingFunction.arguments().get(1), isLiteral((byte) 4));
}
Also used : SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) Function(io.crate.expression.symbol.Function) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 49 with Function

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

the class GroupByAnalyzerTest method testGroupByHavingByGroupKey.

@Test
public void testGroupByHavingByGroupKey() throws Exception {
    QueriedSelectRelation relation = analyze("select sum(floats), name from users group by name having name like 'Slartibart%'");
    assertThat(relation.having(), isFunction(LikeOperators.OP_LIKE));
    Function havingFunction = (Function) relation.having();
    assertThat(havingFunction.arguments().size(), is(2));
    assertThat(havingFunction.arguments().get(0), isReference("name"));
    assertThat(havingFunction.arguments().get(1), isLiteral("Slartibart%"));
}
Also used : SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) Function(io.crate.expression.symbol.Function) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 50 with Function

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

the class InsertAnalyzerTest method testFromQueryWithOnDuplicateKeyValues.

@Test
public void testFromQueryWithOnDuplicateKeyValues() throws Exception {
    var insert = "insert into users (id, name) (select id, name from users) " + "on conflict (id) do update set name = substr(excluded.name, 1, 1)";
    AnalyzedInsertStatement statement = e.analyze(insert);
    Assert.assertThat(statement.onDuplicateKeyAssignments().size(), is(1));
    for (Map.Entry<Reference, Symbol> entry : statement.onDuplicateKeyAssignments().entrySet()) {
        assertThat(entry.getKey(), isReference("name"));
        assertThat(entry.getValue(), isFunction(SubstrFunction.NAME));
        Function function = (Function) entry.getValue();
        assertThat(function.arguments().get(0), instanceOf(InputColumn.class));
        InputColumn inputColumn = (InputColumn) function.arguments().get(0);
        assertThat(inputColumn.index(), is(1));
        assertThat(inputColumn.valueType(), instanceOf(StringType.class));
    }
}
Also used : SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) Function(io.crate.expression.symbol.Function) SubstrFunction(io.crate.expression.scalar.SubstrFunction) StringType(io.crate.types.StringType) Reference(io.crate.metadata.Reference) SymbolMatchers.isReference(io.crate.testing.SymbolMatchers.isReference) ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) SymbolMatchers.isInputColumn(io.crate.testing.SymbolMatchers.isInputColumn) Map(java.util.Map) 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