com.hazelcast.org.apache.calcite.rex.RexOver Maven / Gradle / Ivy
/*
* 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 com.hazelcast.org.apache.calcite.rex;
import com.hazelcast.org.apache.calcite.rel.type.RelDataType;
import com.hazelcast.org.apache.calcite.sql.SqlAggFunction;
import com.hazelcast.org.apache.calcite.sql.SqlWindow;
import com.hazelcast.org.apache.calcite.util.ControlFlowException;
import com.hazelcast.org.apache.calcite.util.Util;
import com.hazelcast.com.google.common.base.Preconditions;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nonnull;
/**
* 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;
private final boolean ignoreNulls;
//~ Constructors -----------------------------------------------------------
/**
* Creates a RexOver.
*
* For example, "SUM(DISTINCT x) OVER (ROWS 3 PRECEDING)" is represented
* as:
*
*
* - type = Integer,
*
- op = {@link com.hazelcast.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,
boolean ignoreNulls) {
super(type, op, operands);
Preconditions.checkArgument(op.isAggregator());
this.window = Objects.requireNonNull(window);
this.distinct = distinct;
this.ignoreNulls = ignoreNulls;
}
//~ Methods ----------------------------------------------------------------
/**
* Returns the aggregate operator for this expression.
*/
public SqlAggFunction getAggOperator() {
return (SqlAggFunction) getOperator();
}
public RexWindow getWindow() {
return window;
}
public boolean isDistinct() {
return distinct;
}
public boolean ignoreNulls() {
return ignoreNulls;
}
@Override protected @Nonnull String computeDigest(boolean withType) {
final StringBuilder sb = new StringBuilder(op.getName());
sb.append("(");
if (distinct) {
sb.append("DISTINCT ");
}
appendOperands(sb);
sb.append(")");
if (ignoreNulls) {
sb.append(" IGNORE NULLS");
}
if (withType) {
sb.append(":");
sb.append(type.getFullTypeString());
}
sb.append(" OVER (");
window.appendDigest(sb, op.allowsFraming())
.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);
}
@Override public int nodeCount() {
return super.nodeCount() + window.nodeCount;
}
/**
* 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 extends RexNode> 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;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy