org.apache.calcite.rex.RexOver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of calcite-core Show documentation
Show all versions of calcite-core Show documentation
Core Calcite APIs and engine.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.rex;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlWindow;
import org.apache.calcite.util.ControlFlowException;
import org.apache.calcite.util.Util;
import com.google.common.base.Preconditions;
import java.util.List;
import java.util.Objects;
/**
* Call to an aggregate function over a window.
*/
public class RexOver extends RexCall {
private static final Finder FINDER = new Finder();
//~ Instance fields --------------------------------------------------------
private final RexWindow window;
private final boolean distinct;
//~ Constructors -----------------------------------------------------------
/**
* Creates a RexOver.
*
* For example, "SUM(DISTINCT x) OVER (ROWS 3 PRECEDING)" is represented
* as:
*
*
* - type = Integer,
*
- op = {@link org.apache.calcite.sql.fun.SqlStdOperatorTable#SUM},
*
- operands = { {@link RexFieldAccess}("x") }
*
- window = {@link SqlWindow}(ROWS 3 PRECEDING)
*
*
* @param type Result type
* @param op Aggregate operator
* @param operands Operands list
* @param window Window specification
* @param distinct Aggregate operator is applied on distinct elements
*/
RexOver(
RelDataType type,
SqlAggFunction op,
List operands,
RexWindow window,
boolean distinct) {
super(type, op, operands);
Preconditions.checkArgument(op.isAggregator());
this.window = Objects.requireNonNull(window);
this.distinct = distinct;
}
//~ Methods ----------------------------------------------------------------
/**
* Returns the aggregate operator for this expression.
*/
public SqlAggFunction getAggOperator() {
return (SqlAggFunction) getOperator();
}
public RexWindow getWindow() {
return window;
}
public boolean isDistinct() {
return distinct;
}
@Override protected String computeDigest(boolean withType) {
final StringBuilder sb = new StringBuilder(op.getName());
sb.append("(");
if (distinct) {
sb.append("DISTINCT ");
}
for (int i = 0; i < operands.size(); i++) {
if (i > 0) {
sb.append(", ");
}
RexNode operand = operands.get(i);
sb.append(operand.toString());
}
sb.append(")");
if (withType) {
sb.append(":");
sb.append(type.getFullTypeString());
}
sb.append(" OVER (")
.append(window)
.append(")");
return sb.toString();
}
public R accept(RexVisitor visitor) {
return visitor.visitOver(this);
}
public R accept(RexBiVisitor visitor, P arg) {
return visitor.visitOver(this, arg);
}
/**
* Returns whether an expression contains an OVER clause.
*/
public static boolean containsOver(RexNode expr) {
try {
expr.accept(FINDER);
return false;
} catch (OverFound e) {
Util.swallow(e, null);
return true;
}
}
/**
* Returns whether a program contains an OVER clause.
*/
public static boolean containsOver(RexProgram program) {
try {
RexUtil.apply(FINDER, program.getExprList(), null);
return false;
} catch (OverFound e) {
Util.swallow(e, null);
return true;
}
}
/**
* Returns whether an expression list contains an OVER clause.
*/
public static boolean containsOver(List exprs, RexNode condition) {
try {
RexUtil.apply(FINDER, exprs, condition);
return false;
} catch (OverFound e) {
Util.swallow(e, null);
return true;
}
}
@Override public RexCall clone(RelDataType type, List operands) {
throw new UnsupportedOperationException();
}
//~ Inner Classes ----------------------------------------------------------
/** Exception thrown when an OVER is found. */
private static class OverFound extends ControlFlowException {
public static final OverFound INSTANCE = new OverFound();
}
/**
* Visitor which detects a {@link RexOver} inside a {@link RexNode}
* expression.
*
* It is re-entrant (two threads can use an instance at the same time)
* and it can be re-used for multiple visits.
*/
private static class Finder extends RexVisitorImpl {
Finder() {
super(true);
}
public Void visitOver(RexOver over) {
throw OverFound.INSTANCE;
}
}
}
// End RexOver.java
© 2015 - 2024 Weber Informatics LLC | Privacy Policy