use of io.crate.sql.tree.Window in project crate by crate.
the class AstBuilder method getWindowDefinitions.
private Map<String, Window> getWindowDefinitions(List<SqlBaseParser.NamedWindowContext> windowContexts) {
HashMap<String, Window> windows = new HashMap<>(windowContexts.size());
for (var windowContext : windowContexts) {
var name = getIdentText(windowContext.name);
if (windows.containsKey(name)) {
throw new IllegalArgumentException("Window " + name + " is already defined");
}
Window window = (Window) visit(windowContext.windowDefinition());
// E.g. WINDOW w AS (ww), ww AS (w)
if (window.windowRef() != null && !windows.containsKey(window.windowRef())) {
throw new IllegalArgumentException("Window " + window.windowRef() + " does not exist");
}
windows.put(name, window);
}
return windows;
}
use of io.crate.sql.tree.Window 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.sql.tree.Window in project crate by crate.
the class ExpressionAnalyzer method resolveWindowRef.
/**
* Resolved the window definition from the list of named windows.
* Reduces the named list of window definitions to a single one if
* they are inter-referenced.
*
* @param name A reference to a window definition.
* @param windows A map of named window definitions.
* @return A {@link Window} from the named window
* definitions or a new window if the targeted window has
* references to other window definitions.
* @throws IllegalArgumentException If the window definition is not found.
*/
private Window resolveWindowRef(@Nonnull String name, Map<String, Window> windows) {
var window = windows.get(name);
if (window == null) {
throw new IllegalArgumentException("Window " + name + " does not exist");
}
String windowRef = window.windowRef();
while (windowRef != null) {
Window refWindow = windows.get(windowRef);
window = window.merge(refWindow);
windowRef = refWindow.windowRef();
}
return window;
}
use of io.crate.sql.tree.Window in project crate by crate.
the class TestStatementBuilder method printStatement.
private static void printStatement(String sql) {
println(sql.trim());
println("");
Statement statement = SqlParser.createStatement(sql);
println(statement.toString());
println("");
// TODO: support formatting all statement types
if (statement instanceof Query || statement instanceof CreateTable || statement instanceof CopyFrom || statement instanceof SwapTable || statement instanceof GCDanglingArtifacts || statement instanceof CreateFunction || statement instanceof CreateUser || statement instanceof GrantPrivilege || statement instanceof DenyPrivilege || statement instanceof RevokePrivilege || statement instanceof DropUser || statement instanceof DropAnalyzer || statement instanceof DropFunction || statement instanceof DropTable || statement instanceof DropBlobTable || statement instanceof DropView || statement instanceof DropRepository || statement instanceof DropSnapshot || statement instanceof Update || statement instanceof Insert || statement instanceof SetSessionAuthorizationStatement || statement instanceof Window) {
println(SqlFormatter.formatSql(statement));
println("");
assertFormattedSql(statement);
}
println("=".repeat(60));
println("");
}
Aggregations