Search in sources :

Example 6 with WindowDefinition

use of io.crate.analyze.WindowDefinition in project crate by crate.

the class WindowAggProjectionSerialisationTest method test_window_agg_projection_serialization_with_filter_before_4_1_0.

@Test
public void test_window_agg_projection_serialization_with_filter_before_4_1_0() throws IOException {
    FunctionImplementation sumFunctionImpl = getSumFunction();
    WindowDefinition partitionByOneWindowDef = new WindowDefinition(singletonList(Literal.of(1L)), null, null);
    WindowFunction windowFunction = new WindowFunction(sumFunctionImpl.signature(), singletonList(Literal.of(2L)), sumFunctionImpl.boundSignature().getReturnType().createType(), null, partitionByOneWindowDef, null);
    Symbol standaloneInput = Literal.of(42L);
    var windowAggProjection = new WindowAggProjection(partitionByOneWindowDef, List.of(windowFunction), List.of(standaloneInput));
    var output = new BytesStreamOutput();
    output.setVersion(Version.V_4_0_0);
    windowAggProjection.writeTo(output);
    var input = output.bytes().streamInput();
    input.setVersion(Version.V_4_0_0);
    var actualWindowAggProjection = new WindowAggProjection(input);
    assertThat(actualWindowAggProjection.outputs(), contains(standaloneInput, windowFunction));
    assertThat(actualWindowAggProjection.windowFunctions().get(0).filter(), Matchers.nullValue());
}
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 7 with WindowDefinition

use of io.crate.analyze.WindowDefinition in project crate by crate.

the class WindowProjector method createComputeEndFrameBoundary.

static ComputeFrameBoundary<Object[]> createComputeEndFrameBoundary(int numCellsInSourceRow, TransactionContext txnCtx, NodeContext nodeCtx, WindowDefinition windowDefinition, Comparator<Object[]> cmpOrderBy) {
    var frameDefinition = windowDefinition.windowFrameDefinition();
    var frameBoundEnd = frameDefinition.end();
    var framingMode = frameDefinition.mode();
    DataType offsetType = frameBoundEnd.value().valueType();
    Object offsetValue = evaluateWithoutParams(txnCtx, nodeCtx, frameBoundEnd.value());
    Object[] endProbeValues = new Object[numCellsInSourceRow];
    BiFunction<Object[], Object[], Object[]> updateProbeValues;
    if (offsetValue != null && framingMode == WindowFrame.Mode.RANGE) {
        updateProbeValues = createUpdateProbeValueFunction(windowDefinition, ArithmeticOperatorsFactory::getAddFunction, offsetValue, offsetType);
    } else {
        updateProbeValues = (currentRow, x) -> x;
    }
    return (partitionStart, partitionEnd, currentIndex, sortedRows) -> frameBoundEnd.type().getEnd(framingMode, partitionStart, partitionEnd, currentIndex, offsetValue, updateProbeValues.apply(sortedRows.get(currentIndex), endProbeValues), cmpOrderBy, sortedRows);
}
Also used : Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) InputColumn(io.crate.expression.symbol.InputColumn) WindowFrame(io.crate.sql.tree.WindowFrame) BiFunction(java.util.function.BiFunction) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Projector(io.crate.data.Projector) IntervalType(io.crate.types.IntervalType) Symbols(io.crate.expression.symbol.Symbols) Nullable(javax.annotation.Nullable) IntSupplier(java.util.function.IntSupplier) NodeContext(io.crate.metadata.NodeContext) Executor(java.util.concurrent.Executor) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) SymbolType(io.crate.expression.symbol.SymbolType) Comparators.createComparator(io.crate.execution.engine.sort.Comparators.createComparator) DataType(io.crate.types.DataType) SymbolEvaluator.evaluateWithoutParams(io.crate.analyze.SymbolEvaluator.evaluateWithoutParams) MemoryManager(io.crate.memory.MemoryManager) RamAccounting(io.crate.breaker.RamAccounting) ExpressionsInput(io.crate.expression.ExpressionsInput) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) Version(org.elasticsearch.Version) Row(io.crate.data.Row) Literal(io.crate.expression.symbol.Literal) WindowDefinition(io.crate.analyze.WindowDefinition) WindowAggProjection(io.crate.execution.dsl.projection.WindowAggProjection) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) InputFactory(io.crate.expression.InputFactory) Comparator(java.util.Comparator) DataType(io.crate.types.DataType)

Example 8 with WindowDefinition

use of io.crate.analyze.WindowDefinition in project crate by crate.

the class WindowProjector method createComputeStartFrameBoundary.

static ComputeFrameBoundary<Object[]> createComputeStartFrameBoundary(int numCellsInSourceRow, TransactionContext txnCtx, NodeContext nodeCtx, WindowDefinition windowDefinition, @Nullable Comparator<Object[]> cmpOrderBy) {
    var frameDefinition = windowDefinition.windowFrameDefinition();
    var frameBoundStart = frameDefinition.start();
    var framingMode = frameDefinition.mode();
    DataType offsetType = frameBoundStart.value().valueType();
    Object offsetValue = evaluateWithoutParams(txnCtx, nodeCtx, frameBoundStart.value());
    Object[] startProbeValues = new Object[numCellsInSourceRow];
    BiFunction<Object[], Object[], Object[]> updateStartProbeValue;
    if (offsetValue != null && framingMode == WindowFrame.Mode.RANGE) {
        updateStartProbeValue = createUpdateProbeValueFunction(windowDefinition, ArithmeticOperatorsFactory::getSubtractFunction, offsetValue, offsetType);
    } else {
        updateStartProbeValue = (currentRow, x) -> x;
    }
    return (partitionStart, partitionEnd, currentIndex, sortedRows) -> frameBoundStart.type().getStart(framingMode, partitionStart, partitionEnd, currentIndex, offsetValue, updateStartProbeValue.apply(sortedRows.get(currentIndex), startProbeValues), cmpOrderBy, sortedRows);
}
Also used : Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) InputColumn(io.crate.expression.symbol.InputColumn) WindowFrame(io.crate.sql.tree.WindowFrame) BiFunction(java.util.function.BiFunction) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Projector(io.crate.data.Projector) IntervalType(io.crate.types.IntervalType) Symbols(io.crate.expression.symbol.Symbols) Nullable(javax.annotation.Nullable) IntSupplier(java.util.function.IntSupplier) NodeContext(io.crate.metadata.NodeContext) Executor(java.util.concurrent.Executor) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) SymbolType(io.crate.expression.symbol.SymbolType) Comparators.createComparator(io.crate.execution.engine.sort.Comparators.createComparator) DataType(io.crate.types.DataType) SymbolEvaluator.evaluateWithoutParams(io.crate.analyze.SymbolEvaluator.evaluateWithoutParams) MemoryManager(io.crate.memory.MemoryManager) RamAccounting(io.crate.breaker.RamAccounting) ExpressionsInput(io.crate.expression.ExpressionsInput) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) Version(org.elasticsearch.Version) Row(io.crate.data.Row) Literal(io.crate.expression.symbol.Literal) WindowDefinition(io.crate.analyze.WindowDefinition) WindowAggProjection(io.crate.execution.dsl.projection.WindowAggProjection) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) InputFactory(io.crate.expression.InputFactory) Comparator(java.util.Comparator) DataType(io.crate.types.DataType)

Example 9 with WindowDefinition

use of io.crate.analyze.WindowDefinition in project crate by crate.

the class WindowAgg method create.

@VisibleForTesting
public static LogicalPlan create(LogicalPlan source, List<WindowFunction> windowFunctions) {
    if (windowFunctions.isEmpty()) {
        return source;
    }
    LinkedHashMap<WindowDefinition, ArrayList<WindowFunction>> groupedFunctions = new LinkedHashMap<>();
    for (WindowFunction windowFunction : windowFunctions) {
        WindowDefinition windowDefinition = windowFunction.windowDefinition();
        ArrayList<WindowFunction> functions = groupedFunctions.computeIfAbsent(windowDefinition, w -> new ArrayList<>());
        functions.add(windowFunction);
    }
    LogicalPlan lastWindowAgg = source;
    for (Map.Entry<WindowDefinition, ArrayList<WindowFunction>> entry : groupedFunctions.entrySet()) {
        /*
             * Pass along the source outputs as standalone symbols as they might be required in cases like:
             *      select x, avg(x) OVER() from t;
             */
        ArrayList<WindowFunction> functions = entry.getValue();
        WindowDefinition windowDefinition = entry.getKey();
        OrderBy orderBy = windowDefinition.orderBy();
        if (orderBy == null || lastWindowAgg.outputs().containsAll(orderBy.orderBySymbols())) {
            lastWindowAgg = new WindowAgg(lastWindowAgg, windowDefinition, functions, lastWindowAgg.outputs());
        } else {
            // ``WindowProjector.createUpdateProbeValueFunction` expects that all OrderBY symbols are `InputColumn`
            // Here we have a case where there is a function or something in the orderBy expression that is *not*
            // already provided by the source.
            // -> Inject `eval` so that the `orderBy` of the window-function will turn into a `InputColumn`
            Eval eval = new Eval(lastWindowAgg, Lists2.concatUnique(lastWindowAgg.outputs(), orderBy.orderBySymbols()));
            lastWindowAgg = new WindowAgg(eval, windowDefinition, functions, eval.outputs());
        }
    }
    return lastWindowAgg;
}
Also used : OrderBy(io.crate.analyze.OrderBy) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) WindowFunction(io.crate.expression.symbol.WindowFunction) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) WindowDefinition(io.crate.analyze.WindowDefinition) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 10 with WindowDefinition

use of io.crate.analyze.WindowDefinition 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)

Aggregations

WindowDefinition (io.crate.analyze.WindowDefinition)11 Symbol (io.crate.expression.symbol.Symbol)9 OrderBy (io.crate.analyze.OrderBy)8 FunctionImplementation (io.crate.metadata.FunctionImplementation)8 ArrayList (java.util.ArrayList)8 NodeContext (io.crate.metadata.NodeContext)7 List (java.util.List)7 Literal (io.crate.expression.symbol.Literal)6 Symbols (io.crate.expression.symbol.Symbols)6 WindowFunction (io.crate.expression.symbol.WindowFunction)6 WindowFrame (io.crate.sql.tree.WindowFrame)6 DataType (io.crate.types.DataType)6 Nullable (javax.annotation.Nullable)6 TransactionContext (io.crate.metadata.TransactionContext)5 SymbolEvaluator.evaluateWithoutParams (io.crate.analyze.SymbolEvaluator.evaluateWithoutParams)4 RamAccounting (io.crate.breaker.RamAccounting)4 RowAccountingWithEstimators (io.crate.breaker.RowAccountingWithEstimators)4 Input (io.crate.data.Input)4 Projector (io.crate.data.Projector)4 Row (io.crate.data.Row)4