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());
}
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);
}
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);
}
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;
}
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));
}
Aggregations