Search in sources :

Example 16 with WindowFunction

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

the class SelectWindowFunctionAnalyzerTest method testOverWithPartitionByClause.

@Test
public void testOverWithPartitionByClause() {
    QueriedSelectRelation analysis = e.analyze("select avg(x) OVER (PARTITION BY x) from t");
    List<Symbol> outputSymbols = analysis.outputs();
    assertThat(outputSymbols.size(), is(1));
    assertThat(outputSymbols.get(0), instanceOf(WindowFunction.class));
    WindowFunction windowFunction = (WindowFunction) outputSymbols.get(0);
    assertThat(windowFunction.arguments().size(), is(1));
    WindowDefinition windowDefinition = windowFunction.windowDefinition();
    assertThat(windowDefinition.partitions().size(), is(1));
}
Also used : WindowFunction(io.crate.expression.symbol.WindowFunction) Symbol(io.crate.expression.symbol.Symbol) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 17 with WindowFunction

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

the class SelectWindowFunctionAnalyzerTest method testOverWithPartitionAndOrderByClauses.

@Test
public void testOverWithPartitionAndOrderByClauses() {
    QueriedSelectRelation analysis = e.analyze("select avg(x) OVER (PARTITION BY x ORDER BY x) from t");
    List<Symbol> outputSymbols = analysis.outputs();
    assertThat(outputSymbols.size(), is(1));
    assertThat(outputSymbols.get(0), instanceOf(WindowFunction.class));
    WindowFunction windowFunction = (WindowFunction) outputSymbols.get(0);
    assertThat(windowFunction.arguments().size(), is(1));
    WindowDefinition windowDefinition = windowFunction.windowDefinition();
    assertThat(windowDefinition.partitions().size(), is(1));
    assertThat(windowDefinition.orderBy().orderBySymbols().size(), is(1));
}
Also used : WindowFunction(io.crate.expression.symbol.WindowFunction) Symbol(io.crate.expression.symbol.Symbol) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 18 with WindowFunction

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

the class SelectWindowFunctionAnalyzerTest method testOverWithFrameDefinition.

@Test
public void testOverWithFrameDefinition() {
    QueriedSelectRelation analysis = e.analyze("select avg(x) OVER (PARTITION BY x ORDER BY x " + "RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) from t");
    List<Symbol> outputSymbols = analysis.outputs();
    assertThat(outputSymbols.size(), is(1));
    assertThat(outputSymbols.get(0), instanceOf(WindowFunction.class));
    WindowFunction windowFunction = (WindowFunction) outputSymbols.get(0);
    assertThat(windowFunction.arguments().size(), is(1));
    WindowFrameDefinition frameDefinition = windowFunction.windowDefinition().windowFrameDefinition();
    assertThat(frameDefinition.mode(), is(WindowFrame.Mode.RANGE));
    assertThat(frameDefinition.start().type(), is(FrameBound.Type.UNBOUNDED_PRECEDING));
    assertThat(frameDefinition.end().type(), is(FrameBound.Type.UNBOUNDED_FOLLOWING));
}
Also used : WindowFunction(io.crate.expression.symbol.WindowFunction) Symbol(io.crate.expression.symbol.Symbol) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 19 with WindowFunction

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

the class WindowAggProjectionSerialisationTest method testWindowAggProjectionSerialisation.

@Test
public void testWindowAggProjectionSerialisation() throws IOException {
    FunctionImplementation sumFunctionImpl = getSumFunction();
    WindowDefinition partitionByOneWindowDef = new WindowDefinition(singletonList(Literal.of(1L)), null, null);
    WindowDefinition partitionByTwoWindowDef = new WindowDefinition(singletonList(Literal.of(2L)), null, null);
    WindowFunction firstWindowFunction = new WindowFunction(sumFunctionImpl.signature(), singletonList(Literal.of(1L)), sumFunctionImpl.boundSignature().getReturnType().createType(), null, partitionByOneWindowDef, true);
    WindowFunction secondWindowFunction = new WindowFunction(sumFunctionImpl.signature(), singletonList(Literal.of(2L)), sumFunctionImpl.boundSignature().getReturnType().createType(), null, partitionByTwoWindowDef, null);
    Symbol standaloneInput = Literal.of(42L);
    var expectedWindowAggProjection = new WindowAggProjection(partitionByOneWindowDef, List.of(firstWindowFunction, secondWindowFunction), List.of(standaloneInput));
    var output = new BytesStreamOutput();
    expectedWindowAggProjection.writeTo(output);
    var in = output.bytes().streamInput();
    var actualWindowAggProjection = new WindowAggProjection(in);
    assertThat(actualWindowAggProjection.outputs(), contains(standaloneInput, firstWindowFunction, secondWindowFunction));
    assertThat(actualWindowAggProjection, is(expectedWindowAggProjection));
}
Also used : WindowFunction(io.crate.expression.symbol.WindowFunction) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) WindowDefinition(io.crate.analyze.WindowDefinition) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 20 with WindowFunction

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

the class MoveFilterBeneathWindowAggTest method test_filter_part_on_windows_function_partition_columns_is_moved.

@Test
public void test_filter_part_on_windows_function_partition_columns_is_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 AND id = 10 AND x = 1");
    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);
    var expectedPlan = "Filter[((row_number() OVER (PARTITION BY id) = 2) AND (x = 1))]\n" + "  └ WindowAgg[id, row_number() OVER (PARTITION BY id)]\n" + "    └ Filter[(id = 10)]\n" + "      └ Collect[doc.t1 | [id] | true]";
    assertThat(newPlan, isPlan(expectedPlan));
}
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)

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