Search in sources :

Example 51 with Function

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

the class DocLevelCollectTest method testCollectDocLevelWhereClause.

@Test
public void testCollectDocLevelWhereClause() throws Throwable {
    List<Symbol> arguments = Arrays.asList(testDocLevelReference, Literal.of(2));
    EqOperator op = (EqOperator) functions.get(null, EqOperator.NAME, arguments, SearchPath.pathWithPGCatalogAndDoc());
    List<Symbol> toCollect = Collections.singletonList(testDocLevelReference);
    WhereClause whereClause = new WhereClause(new Function(op.signature(), arguments, EqOperator.RETURN_TYPE));
    RoutedCollectPhase collectNode = getCollectNode(toCollect, whereClause);
    Bucket result = collect(collectNode);
    assertThat(result, contains(isRow(2)));
}
Also used : Function(io.crate.expression.symbol.Function) EqOperator(io.crate.expression.operator.EqOperator) Bucket(io.crate.data.Bucket) StreamBucket(io.crate.execution.engine.distribution.StreamBucket) Symbol(io.crate.expression.symbol.Symbol) WhereClause(io.crate.analyze.WhereClause) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) Test(org.junit.Test)

Example 52 with Function

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

the class AbstractTableFunctionsTest method assertCompile.

public void assertCompile(String functionExpression, java.util.function.Function<Scalar, Matcher<Scalar>> matcher) {
    Symbol functionSymbol = sqlExpressions.asSymbol(functionExpression);
    functionSymbol = sqlExpressions.normalize(functionSymbol);
    assertThat("function expression was normalized, compile would not be hit", functionSymbol, not(instanceOf(Literal.class)));
    Function function = (Function) functionSymbol;
    Scalar scalar = (Scalar) sqlExpressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
    assertThat("Function implementation not found using full qualified lookup", scalar, Matchers.notNullValue());
    Scalar compiled = scalar.compile(function.arguments());
    assertThat(compiled, matcher.apply(scalar));
}
Also used : Function(io.crate.expression.symbol.Function) Symbol(io.crate.expression.symbol.Symbol) Scalar(io.crate.metadata.Scalar)

Example 53 with Function

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

the class AbstractTableFunctionsTest method execute.

protected Iterable<Row> execute(String expr) {
    Symbol functionSymbol = sqlExpressions.normalize(sqlExpressions.asSymbol(expr));
    var function = (Function) functionSymbol;
    var functionImplementation = (TableFunctionImplementation<?>) sqlExpressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
    if (functionImplementation.returnType().numElements() > 1) {
        // See classdocs of TableFunctionImplementation for an explanation
        assertThat("If the rowType has multiple elements, the returnType of the boundSignature " + "must be an exact match of the returnType", functionImplementation.boundSignature().getReturnType().createType(), is(functionImplementation.returnType()));
    }
    // noinspection unchecked,rawtypes
    return functionImplementation.evaluate(txnCtx, null, function.arguments().stream().map(a -> (Input) a).toArray(Input[]::new));
}
Also used : Function(io.crate.expression.symbol.Function) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) Input(io.crate.data.Input) Symbol(io.crate.expression.symbol.Symbol)

Example 54 with Function

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

the class JoinPhaseTest method setup.

@Before
public void setup() {
    topNProjection = new TopNProjection(10, 0, Collections.emptyList());
    jobId = UUID.randomUUID();
    mp1 = new MergePhase(jobId, 2, "merge", 1, 1, Collections.emptyList(), List.of(DataTypes.STRING), List.of(), DistributionInfo.DEFAULT_BROADCAST, null);
    mp2 = new MergePhase(jobId, 3, "merge", 1, 1, Collections.emptyList(), List.of(DataTypes.STRING), List.of(), DistributionInfo.DEFAULT_BROADCAST, null);
    joinCondition = new Function(EqOperator.SIGNATURE, List.of(new InputColumn(0, DataTypes.STRING), new InputColumn(1, DataTypes.STRING)), EqOperator.RETURN_TYPE);
}
Also used : Function(io.crate.expression.symbol.Function) MergePhase(io.crate.execution.dsl.phases.MergePhase) InputColumn(io.crate.expression.symbol.InputColumn) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) Before(org.junit.Before)

Example 55 with Function

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

the class RelationAnalyzer method visitTableFunction.

@Override
public AnalyzedRelation visitTableFunction(TableFunction node, StatementAnalysisContext statementContext) {
    RelationAnalysisContext context = statementContext.currentRelationContext();
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(statementContext.transactionContext(), nodeCtx, statementContext.paramTyeHints(), FieldProvider.UNSUPPORTED, null);
    ExpressionAnalysisContext expressionContext = context.expressionAnalysisContext();
    // we support `FROM scalar()` but not `FROM 'literal'` -> we turn off eager normalization
    // so we can distinguish between Function and Literal.
    final boolean allowEagerNormalizeOriginalValue = expressionContext.isEagerNormalizationAllowed();
    expressionContext.allowEagerNormalize(false);
    Symbol symbol = expressionAnalyzer.convert(node.functionCall(), expressionContext);
    expressionContext.allowEagerNormalize(allowEagerNormalizeOriginalValue);
    if (!(symbol instanceof Function)) {
        throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "Symbol '%s' is not supported in FROM clause", node.name()));
    }
    Function function = (Function) symbol;
    FunctionImplementation functionImplementation = nodeCtx.functions().getQualified(function, statementContext.sessionContext().searchPath());
    assert functionImplementation != null : "Function implementation not found using full qualified lookup";
    TableFunctionImplementation<?> tableFunction = TableFunctionFactory.from(functionImplementation);
    TableFunctionRelation tableRelation = new TableFunctionRelation(tableFunction, function);
    context.addSourceRelation(tableRelation);
    return tableRelation;
}
Also used : ValuesFunction(io.crate.expression.tablefunctions.ValuesFunction) TableFunction(io.crate.sql.tree.TableFunction) ArrayFunction(io.crate.expression.scalar.arithmetic.ArrayFunction) Function(io.crate.expression.symbol.Function) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) FunctionImplementation(io.crate.metadata.FunctionImplementation) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation)

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