Search in sources :

Example 21 with WindowFunction

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

the class MoveFilterBeneathWindowAggTest method test_filter_on_containing_windows_function_is_not_moved.

@Test
public void test_filter_on_containing_windows_function_is_not_moved() {
    var collect = e.logicalPlan("SELECT id FROM t1");
    WindowFunction windowFunction = (WindowFunction) e.asSymbol("ROW_NUMBER() OVER(PARTITION by id)");
    WindowAgg windowAgg = (WindowAgg) WindowAgg.create(collect, List.of(windowFunction));
    Symbol query = e.asSymbol("ROW_NUMBER() OVER(PARTITION by id) = 2");
    Filter filter = new Filter(windowAgg, query);
    var rule = new MoveFilterBeneathWindowAgg();
    Match<Filter> match = rule.pattern().accept(filter, Captures.empty());
    assertThat(match.isPresent(), is(true));
    assertThat(match.value(), Matchers.sameInstance(filter));
    LogicalPlan newPlan = rule.apply(match.value(), match.captures(), new TableStats(), CoordinatorTxnCtx.systemTransactionContext(), e.nodeCtx);
    assertThat(newPlan, nullValue());
}
Also used : WindowFunction(io.crate.expression.symbol.WindowFunction) WindowAgg(io.crate.planner.operators.WindowAgg) Filter(io.crate.planner.operators.Filter) Symbol(io.crate.expression.symbol.Symbol) LogicalPlan(io.crate.planner.operators.LogicalPlan) TableStats(io.crate.statistics.TableStats) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 22 with WindowFunction

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

the class ExpressionAnalyzer method allocateBuiltinOrUdfFunction.

/**
 * Creates a function symbol and tries to normalize the new function's
 * {@link FunctionImplementation} iff only Literals are supplied as arguments.
 * This folds any constant expressions like '1 + 1' => '2'.
 * @param schema The schema for udf functions
 * @param functionName The function name of the new function.
 * @param arguments The arguments to provide to the {@link Function}.
 * @param filter The filter clause to filter {@link Function}'s input values.
 * @param context Context holding the state for the current translation.
 * @param windowDefinition The definition of the window the allocated function will be executed against.
 * @param coordinatorTxnCtx {@link CoordinatorTxnCtx} for this transaction.
 * @param nodeCtx The {@link NodeContext} to normalize constant expressions.
 * @return The supplied {@link Function} or a {@link Literal} in case of constant folding.
 */
private static Symbol allocateBuiltinOrUdfFunction(@Nullable String schema, String functionName, List<Symbol> arguments, @Nullable Symbol filter, @Nullable Boolean ignoreNulls, ExpressionAnalysisContext context, @Nullable WindowDefinition windowDefinition, CoordinatorTxnCtx coordinatorTxnCtx, NodeContext nodeCtx) {
    FunctionImplementation funcImpl = nodeCtx.functions().get(schema, functionName, arguments, coordinatorTxnCtx.sessionContext().searchPath());
    Signature signature = funcImpl.signature();
    Signature boundSignature = funcImpl.boundSignature();
    List<Symbol> castArguments = cast(arguments, boundSignature.getArgumentDataTypes());
    Function newFunction;
    if (windowDefinition == null) {
        if (signature.getKind() == FunctionType.AGGREGATE) {
            context.indicateAggregates();
        } else if (filter != null) {
            throw new UnsupportedOperationException("Only aggregate functions allow a FILTER clause");
        }
        if (ignoreNulls != null) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "%s cannot accept RESPECT or IGNORE NULLS flag.", functionName));
        }
        newFunction = new Function(signature, castArguments, boundSignature.getReturnType().createType(), filter);
    } else {
        if (signature.getKind() != FunctionType.WINDOW) {
            if (signature.getKind() != FunctionType.AGGREGATE) {
                throw new IllegalArgumentException(String.format(Locale.ENGLISH, "OVER clause was specified, but %s is neither a window nor an aggregate function.", functionName));
            } else {
                if (ignoreNulls != null) {
                    throw new IllegalArgumentException(String.format(Locale.ENGLISH, "%s cannot accept RESPECT or IGNORE NULLS flag.", functionName));
                }
            }
        }
        newFunction = new WindowFunction(signature, castArguments, boundSignature.getReturnType().createType(), filter, windowDefinition, ignoreNulls);
    }
    return newFunction;
}
Also used : CurrentDateFunction(io.crate.expression.scalar.CurrentDateFunction) MapFunction(io.crate.expression.scalar.arithmetic.MapFunction) CurrentTimestampFunction(io.crate.expression.scalar.timestamp.CurrentTimestampFunction) WindowFunction(io.crate.expression.symbol.WindowFunction) ArrayFunction(io.crate.expression.scalar.arithmetic.ArrayFunction) CurrentTimeFunction(io.crate.expression.scalar.timestamp.CurrentTimeFunction) IfFunction(io.crate.expression.scalar.conditional.IfFunction) ArraySliceFunction(io.crate.expression.scalar.ArraySliceFunction) Function(io.crate.expression.symbol.Function) SubscriptFunction(io.crate.expression.scalar.SubscriptFunction) WindowFunction(io.crate.expression.symbol.WindowFunction) SelectSymbol(io.crate.expression.symbol.SelectSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Symbol(io.crate.expression.symbol.Symbol) Signature(io.crate.metadata.functions.Signature) FunctionImplementation(io.crate.metadata.FunctionImplementation)

Example 23 with WindowFunction

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

the class WindowDefinitionTest method testUnboundedPrecedingUnboundedFollowingFrameIsAllowed.

@Test
public void testUnboundedPrecedingUnboundedFollowingFrameIsAllowed() {
    Collect collect = e.plan("select sum(col1) over(RANGE BETWEEN UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING) FROM " + "unnest([1, 2, 1, 1, 1, 4])");
    List<Projection> projections = collect.collectPhase().projections();
    assertThat(projections.size(), is(2));
    WindowAggProjection windowProjection = null;
    for (Projection projection : projections) {
        if (projection instanceof WindowAggProjection) {
            windowProjection = (WindowAggProjection) projection;
            break;
        }
    }
    assertThat(windowProjection, is(notNullValue()));
    List<? extends Symbol> outputs = windowProjection.outputs();
    // IC and window function
    assertThat(outputs.size(), is(2));
    WindowFunction windowFunction = null;
    for (Symbol output : outputs) {
        if (output instanceof WindowFunction) {
            windowFunction = (WindowFunction) output;
        }
    }
    assertThat(windowFunction, is(notNullValue()));
    assertThat(windowFunction.windowDefinition().windowFrameDefinition().start().type(), is(UNBOUNDED_PRECEDING));
    assertThat(windowFunction.windowDefinition().windowFrameDefinition().end().type(), is(UNBOUNDED_FOLLOWING));
}
Also used : WindowFunction(io.crate.expression.symbol.WindowFunction) Collect(io.crate.planner.node.dql.Collect) Symbol(io.crate.expression.symbol.Symbol) Projection(io.crate.execution.dsl.projection.Projection) WindowAggProjection(io.crate.execution.dsl.projection.WindowAggProjection) WindowAggProjection(io.crate.execution.dsl.projection.WindowAggProjection) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 24 with WindowFunction

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

the class WindowDefinitionTest method testFrameEndDefaultsToCurrentRowIfNotSpecified.

@Test
public void testFrameEndDefaultsToCurrentRowIfNotSpecified() {
    AnalyzedRelation analyze = e.analyze("select sum(col1) over(RANGE UNBOUNDED PRECEDING) FROM " + "unnest([1, 2, 1, 1, 1, 4])");
    assertThat(analyze, is(instanceOf(QueriedSelectRelation.class)));
    List<Symbol> outputs = analyze.outputs();
    assertThat(outputs.size(), is(1));
    WindowFunction windowFunction = (WindowFunction) outputs.get(0);
    assertThat(windowFunction.windowDefinition().windowFrameDefinition().end().type(), is(CURRENT_ROW));
}
Also used : WindowFunction(io.crate.expression.symbol.WindowFunction) Symbol(io.crate.expression.symbol.Symbol) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Aggregations

WindowFunction (io.crate.expression.symbol.WindowFunction)24 Symbol (io.crate.expression.symbol.Symbol)21 Test (org.junit.Test)16 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)14 Filter (io.crate.planner.operators.Filter)6 LogicalPlan (io.crate.planner.operators.LogicalPlan)6 WindowAgg (io.crate.planner.operators.WindowAgg)6 TableStats (io.crate.statistics.TableStats)6 ArrayList (java.util.ArrayList)5 WindowDefinition (io.crate.analyze.WindowDefinition)4 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 Function (io.crate.expression.symbol.Function)3 FunctionImplementation (io.crate.metadata.FunctionImplementation)3 List (java.util.List)3 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)2 OrderBy (io.crate.analyze.OrderBy)2 Projection (io.crate.execution.dsl.projection.Projection)2 WindowAggProjection (io.crate.execution.dsl.projection.WindowAggProjection)2 ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)2 SelectSymbol (io.crate.expression.symbol.SelectSymbol)2