Search in sources :

Example 1 with FrameBound

use of io.crate.sql.tree.FrameBound 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);
}
Also used : Window(io.crate.sql.tree.Window) OrderBy(io.crate.analyze.OrderBy) ParamTypeHints(io.crate.analyze.ParamTypeHints) NullLiteral(io.crate.sql.tree.NullLiteral) ArrayLikePredicate(io.crate.sql.tree.ArrayLikePredicate) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) LikeOperators(io.crate.expression.operator.LikeOperators) IntervalParser(io.crate.interval.IntervalParser) SubscriptExpression(io.crate.sql.tree.SubscriptExpression) NegateFunctions(io.crate.expression.scalar.arithmetic.NegateFunctions) ExpressionFormatter(io.crate.sql.ExpressionFormatter) BetweenPredicate(io.crate.sql.tree.BetweenPredicate) Map(java.util.Map) ArrayLiteral(io.crate.sql.tree.ArrayLiteral) IntervalLiteral(io.crate.sql.tree.IntervalLiteral) AstVisitor(io.crate.sql.tree.AstVisitor) NotPredicate(io.crate.expression.predicate.NotPredicate) EscapedCharStringLiteral(io.crate.sql.tree.EscapedCharStringLiteral) CastMode(io.crate.expression.scalar.cast.CastMode) UndefinedType(io.crate.types.UndefinedType) TryCast(io.crate.sql.tree.TryCast) CurrentDateFunction(io.crate.expression.scalar.CurrentDateFunction) IfExpression(io.crate.sql.tree.IfExpression) NotExpression(io.crate.sql.tree.NotExpression) ObjectLiteral(io.crate.sql.tree.ObjectLiteral) HOUR(io.crate.sql.tree.IntervalLiteral.IntervalField.HOUR) MINUTE(io.crate.sql.tree.IntervalLiteral.IntervalField.MINUTE) SemanticSortValidator(io.crate.analyze.validator.SemanticSortValidator) FunctionCall(io.crate.sql.tree.FunctionCall) OrderyByAnalyzer(io.crate.analyze.relations.OrderyByAnalyzer) Extract(io.crate.sql.tree.Extract) SearchedCaseExpression(io.crate.sql.tree.SearchedCaseExpression) Operation(io.crate.metadata.table.Operation) InPredicate(io.crate.sql.tree.InPredicate) Operator(io.crate.expression.operator.Operator) ArrayList(java.util.ArrayList) CurrentTime(io.crate.sql.tree.CurrentTime) AndOperator(io.crate.expression.operator.AndOperator) BooleanLiteral(io.crate.sql.tree.BooleanLiteral) WindowFrameDefinition(io.crate.analyze.WindowFrameDefinition) QualifiedName(io.crate.sql.tree.QualifiedName) Nullable(javax.annotation.Nullable) MatchPredicate(io.crate.sql.tree.MatchPredicate) LogicalBinaryExpression(io.crate.sql.tree.LogicalBinaryExpression) CollectSetAggregation(io.crate.execution.engine.aggregation.impl.CollectSetAggregation) IsNullPredicate(io.crate.sql.tree.IsNullPredicate) ArraySliceExpression(io.crate.sql.tree.ArraySliceExpression) SubscriptFunctions(io.crate.expression.scalar.SubscriptFunctions) Reference(io.crate.metadata.Reference) IntegerLiteral(io.crate.sql.tree.IntegerLiteral) RecordSubscript(io.crate.sql.tree.RecordSubscript) Literal(io.crate.expression.symbol.Literal) BitStringType(io.crate.types.BitStringType) RegexpMatchCaseInsensitiveOperator(io.crate.expression.operator.RegexpMatchCaseInsensitiveOperator) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) ConversionException(io.crate.exceptions.ConversionException) ArraySubQueryExpression(io.crate.sql.tree.ArraySubQueryExpression) DoubleLiteral(io.crate.sql.tree.DoubleLiteral) SimpleCaseExpression(io.crate.sql.tree.SimpleCaseExpression) SubscriptContext(io.crate.analyze.SubscriptContext) MapFunction(io.crate.expression.scalar.arithmetic.MapFunction) ExtractFunctions(io.crate.expression.scalar.ExtractFunctions) AllOperator(io.crate.expression.operator.AllOperator) RelationName(io.crate.metadata.RelationName) FieldProvider(io.crate.analyze.relations.FieldProvider) SubqueryExpression(io.crate.sql.tree.SubqueryExpression) IsNotNullPredicate(io.crate.sql.tree.IsNotNullPredicate) Node(io.crate.sql.tree.Node) CurrentTimestampFunction(io.crate.expression.scalar.timestamp.CurrentTimestampFunction) ArrayType(io.crate.types.ArrayType) OrOperator(io.crate.expression.operator.OrOperator) NegateLiterals(io.crate.analyze.NegateLiterals) Locale(java.util.Locale) SelectSymbol(io.crate.expression.symbol.SelectSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) InListExpression(io.crate.sql.tree.InListExpression) SECOND(io.crate.sql.tree.IntervalLiteral.IntervalField.SECOND) YEAR(io.crate.sql.tree.IntervalLiteral.IntervalField.YEAR) Window(io.crate.sql.tree.Window) ArrayComparison(io.crate.sql.tree.ArrayComparison) NodeContext(io.crate.metadata.NodeContext) ParameterExpression(io.crate.sql.tree.ParameterExpression) Collection(java.util.Collection) EqOperator(io.crate.expression.operator.EqOperator) Signature(io.crate.metadata.functions.Signature) Lists2(io.crate.common.collections.Lists2) ArrayComparisonExpression(io.crate.sql.tree.ArrayComparisonExpression) BitString(io.crate.sql.tree.BitString) AnyOperator(io.crate.expression.operator.any.AnyOperator) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) DataTypes(io.crate.types.DataTypes) WindowFunction(io.crate.expression.symbol.WindowFunction) Optional(java.util.Optional) Expression(io.crate.sql.tree.Expression) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) StringLiteral(io.crate.sql.tree.StringLiteral) WindowFrame(io.crate.sql.tree.WindowFrame) LongLiteral(io.crate.sql.tree.LongLiteral) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference) HashMap(java.util.HashMap) RegexpMatchOperator(io.crate.expression.operator.RegexpMatchOperator) FrameBoundDefinition(io.crate.analyze.FrameBoundDefinition) MatchPredicateColumnIdent(io.crate.sql.tree.MatchPredicateColumnIdent) ComparisonExpression(io.crate.sql.tree.ComparisonExpression) HashSet(java.util.HashSet) SubscriptValidator(io.crate.analyze.SubscriptValidator) ArithmeticExpression(io.crate.sql.tree.ArithmeticExpression) Cast(io.crate.sql.tree.Cast) DAY(io.crate.sql.tree.IntervalLiteral.IntervalField.DAY) MONTH(io.crate.sql.tree.IntervalLiteral.IntervalField.MONTH) Symbols(io.crate.expression.symbol.Symbols) SqlParser(io.crate.sql.parser.SqlParser) Nonnull(javax.annotation.Nonnull) NegativeExpression(io.crate.sql.tree.NegativeExpression) WhenClause(io.crate.sql.tree.WhenClause) ArrayFunction(io.crate.expression.scalar.arithmetic.ArrayFunction) CurrentTimeFunction(io.crate.expression.scalar.timestamp.CurrentTimeFunction) LikePredicate(io.crate.sql.tree.LikePredicate) Period(org.joda.time.Period) IfFunction(io.crate.expression.scalar.conditional.IfFunction) ArraySliceFunction(io.crate.expression.scalar.ArraySliceFunction) DataType(io.crate.types.DataType) Function(io.crate.expression.symbol.Function) SubscriptFunction(io.crate.expression.scalar.SubscriptFunction) Collectors.toList(java.util.stream.Collectors.toList) WindowDefinition(io.crate.analyze.WindowDefinition) FunctionType(io.crate.metadata.FunctionType) DataTypeAnalyzer(io.crate.analyze.DataTypeAnalyzer) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) FrameBound(io.crate.sql.tree.FrameBound) SelectSymbol(io.crate.expression.symbol.SelectSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Symbol(io.crate.expression.symbol.Symbol) FrameBound(io.crate.sql.tree.FrameBound) ArrayList(java.util.ArrayList) FrameBoundDefinition(io.crate.analyze.FrameBoundDefinition) WindowFrame(io.crate.sql.tree.WindowFrame) SubscriptExpression(io.crate.sql.tree.SubscriptExpression) IfExpression(io.crate.sql.tree.IfExpression) NotExpression(io.crate.sql.tree.NotExpression) SearchedCaseExpression(io.crate.sql.tree.SearchedCaseExpression) LogicalBinaryExpression(io.crate.sql.tree.LogicalBinaryExpression) ArraySliceExpression(io.crate.sql.tree.ArraySliceExpression) ArraySubQueryExpression(io.crate.sql.tree.ArraySubQueryExpression) SimpleCaseExpression(io.crate.sql.tree.SimpleCaseExpression) SubqueryExpression(io.crate.sql.tree.SubqueryExpression) InListExpression(io.crate.sql.tree.InListExpression) ParameterExpression(io.crate.sql.tree.ParameterExpression) ArrayComparisonExpression(io.crate.sql.tree.ArrayComparisonExpression) Expression(io.crate.sql.tree.Expression) ComparisonExpression(io.crate.sql.tree.ComparisonExpression) ArithmeticExpression(io.crate.sql.tree.ArithmeticExpression) NegativeExpression(io.crate.sql.tree.NegativeExpression) WindowFrameDefinition(io.crate.analyze.WindowFrameDefinition) WindowDefinition(io.crate.analyze.WindowDefinition) Nullable(javax.annotation.Nullable)

Aggregations

DataTypeAnalyzer (io.crate.analyze.DataTypeAnalyzer)1 FrameBoundDefinition (io.crate.analyze.FrameBoundDefinition)1 NegateLiterals (io.crate.analyze.NegateLiterals)1 OrderBy (io.crate.analyze.OrderBy)1 ParamTypeHints (io.crate.analyze.ParamTypeHints)1 SubscriptContext (io.crate.analyze.SubscriptContext)1 SubscriptValidator (io.crate.analyze.SubscriptValidator)1 WindowDefinition (io.crate.analyze.WindowDefinition)1 WindowFrameDefinition (io.crate.analyze.WindowFrameDefinition)1 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)1 FieldProvider (io.crate.analyze.relations.FieldProvider)1 OrderyByAnalyzer (io.crate.analyze.relations.OrderyByAnalyzer)1 SemanticSortValidator (io.crate.analyze.validator.SemanticSortValidator)1 Lists2 (io.crate.common.collections.Lists2)1 ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)1 ConversionException (io.crate.exceptions.ConversionException)1 CollectSetAggregation (io.crate.execution.engine.aggregation.impl.CollectSetAggregation)1 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)1 AllOperator (io.crate.expression.operator.AllOperator)1 AndOperator (io.crate.expression.operator.AndOperator)1