use of io.crate.analyze.WindowFrameDefinition in project crate by crate.
the class ExpressionAnalyzer method getWindowDefinition.
@Nullable
private WindowDefinition getWindowDefinition(Optional<Window> maybeWindow, ExpressionAnalysisContext context) {
if (!maybeWindow.isPresent()) {
return null;
}
var unresolvedWindow = maybeWindow.get();
final Window window;
if (unresolvedWindow.windowRef() != null) {
var refWindow = resolveWindowRef(unresolvedWindow.windowRef(), context.windows());
window = unresolvedWindow.merge(refWindow);
} else {
window = unresolvedWindow;
}
List<Symbol> partitionSymbols = new ArrayList<>(window.getPartitions().size());
for (Expression partition : window.getPartitions()) {
Symbol symbol = convert(partition, context);
SemanticSortValidator.validate(symbol, "PARTITION BY");
partitionSymbols.add(symbol);
}
OrderBy orderBy = OrderyByAnalyzer.analyzeSortItems(window.getOrderBy(), sortKey -> {
Symbol symbol = convert(sortKey, context);
SemanticSortValidator.validate(symbol);
return symbol;
});
WindowFrameDefinition windowFrameDefinition = WindowDefinition.RANGE_UNBOUNDED_PRECEDING_CURRENT_ROW;
if (window.getWindowFrame().isPresent()) {
WindowFrame windowFrame = window.getWindowFrame().get();
validateFrame(window, windowFrame);
FrameBound start = windowFrame.getStart();
FrameBoundDefinition startBound = convertToAnalyzedFrameBound(context, start);
FrameBoundDefinition endBound = windowFrame.getEnd().map(end -> convertToAnalyzedFrameBound(context, end)).orElse(new FrameBoundDefinition(FrameBound.Type.CURRENT_ROW, Literal.NULL));
windowFrameDefinition = new WindowFrameDefinition(windowFrame.mode(), startBound, endBound);
}
return new WindowDefinition(partitionSymbols, orderBy, windowFrameDefinition);
}
use of io.crate.analyze.WindowFrameDefinition in project crate by crate.
the class WindowFunction method toString.
@Override
public String toString(Style style) {
var builder = new StringBuilder(super.toString(style));
if (ignoreNulls != null) {
if (ignoreNulls) {
builder.append(" IGNORE NULLS");
} else {
builder.append(" RESPECT NULLS");
}
}
builder.append(" OVER (");
var partitions = windowDefinition.partitions();
if (!partitions.isEmpty()) {
builder.append("PARTITION BY ");
builder.append(Lists2.joinOn(", ", partitions, x -> x.toString(style)));
}
var orderBy = windowDefinition.orderBy();
if (orderBy != null) {
if (!partitions.isEmpty()) {
builder.append(" ");
}
builder.append("ORDER BY ");
OrderBy.explainRepresentation(builder, orderBy.orderBySymbols(), orderBy.reverseFlags(), orderBy.nullsFirst(), x -> x.toString(style));
}
WindowFrameDefinition frameDefinition = windowDefinition.windowFrameDefinition();
if (frameDefinition != WindowDefinition.RANGE_UNBOUNDED_PRECEDING_CURRENT_ROW) {
builder.append(" ");
builder.append(frameDefinition.mode().name());
builder.append(" BETWEEN ");
appendFrameBound(builder, style, frameDefinition.start());
builder.append(" AND ");
appendFrameBound(builder, style, frameDefinition.end());
}
builder.append(")");
return builder.toString();
}
Aggregations