OmniSciDB  c07336695a
org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle Class Reference
+ Inheritance diagram for org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle:
+ Collaboration diagram for org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle:

Public Member Functions

RexNode visitCall (RexCall call)
 

Package Functions

 HistogramShuttle (List< RexNode > partitionKeys, ImmutableList< RexFieldCollation > orderKeys, RexWindowBound lowerBound, RexWindowBound upperBound, SqlWindow window, boolean distinct, boolean ignoreNulls)
 
SqlFunction getHistogramOp (SqlAggFunction aggFunction)
 

Static Package Attributes

static final boolean ENABLE_HISTOGRAM_AGG = false
 

Private Member Functions

RelDataType computeHistogramType (RelDataType type)
 

Private Attributes

final List< RexNode > partitionKeys
 
final ImmutableList< RexFieldCollation > orderKeys
 
final RexWindowBound lowerBound
 
final RexWindowBound upperBound
 
final SqlWindow window
 
final boolean distinct
 
final boolean ignoreNulls
 

Detailed Description

Shuttle which walks over a tree of RexNodes and applies 'over' to all agg functions.

This is necessary because the returned expression is not necessarily a call to an agg function. For example,

AVG(x)

becomes

SUM(x) / COUNT(x)

Any aggregate functions are converted to calls to the internal $Histogram aggregation function and accessors such as $HistogramMin; for example,

MIN(x), MAX(x)

are converted to

$HistogramMin($Histogram(x)), $HistogramMax($Histogram(x))

Common sub-expression elimination will ensure that only one histogram is computed.

Definition at line 5007 of file SqlToRelConverter.java.

Constructor & Destructor Documentation

◆ HistogramShuttle()

org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.HistogramShuttle ( List< RexNode >  partitionKeys,
ImmutableList< RexFieldCollation >  orderKeys,
RexWindowBound  lowerBound,
RexWindowBound  upperBound,
SqlWindow  window,
boolean  distinct,
boolean  ignoreNulls 
)
inlinepackage

Member Function Documentation

◆ computeHistogramType()

RelDataType org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.computeHistogramType ( RelDataType  type)
inlineprivate

Returns the type for a histogram function. It is either the actual type or an an approximation to it.

Definition at line 5148 of file SqlToRelConverter.java.

References run-benchmark-import.type, and org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory.

5148  {
5149  if (SqlTypeUtil.isExactNumeric(type)
5150  && type.getSqlTypeName() != SqlTypeName.BIGINT) {
5151  return typeFactory.createSqlType(SqlTypeName.BIGINT);
5152  } else if (SqlTypeUtil.isApproximateNumeric(type)
5153  && type.getSqlTypeName() != SqlTypeName.DOUBLE) {
5154  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
5155  } else {
5156  return type;
5157  }
5158  }

◆ getHistogramOp()

SqlFunction org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.getHistogramOp ( SqlAggFunction  aggFunction)
inlinepackage

Returns the histogram operator corresponding to a given aggregate function.

For example, getHistogramOp (SqlStdOperatorTable#MIN} returns SqlStdOperatorTable#HISTOGRAM_MIN.

Parameters
aggFunctionAn aggregate function
Returns
Its histogram function, or null

Definition at line 5130 of file SqlToRelConverter.java.

5130  {
5131  if (aggFunction == SqlStdOperatorTable.MIN) {
5132  return SqlStdOperatorTable.HISTOGRAM_MIN;
5133  } else if (aggFunction == SqlStdOperatorTable.MAX) {
5134  return SqlStdOperatorTable.HISTOGRAM_MAX;
5135  } else if (aggFunction == SqlStdOperatorTable.FIRST_VALUE) {
5136  return SqlStdOperatorTable.HISTOGRAM_FIRST_VALUE;
5137  } else if (aggFunction == SqlStdOperatorTable.LAST_VALUE) {
5138  return SqlStdOperatorTable.HISTOGRAM_LAST_VALUE;
5139  } else {
5140  return null;
5141  }
5142  }

◆ visitCall()

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.visitCall ( RexCall  call)
inline

Definition at line 5038 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder, and run-benchmark-import.type.

5038  {
5039  final SqlOperator op = call.getOperator();
5040  if (!(op instanceof SqlAggFunction)) {
5041  return super.visitCall(call);
5042  }
5043  final SqlAggFunction aggOp = (SqlAggFunction) op;
5044  final RelDataType type = call.getType();
5045  List<RexNode> exprs = call.getOperands();
5046 
5047  SqlFunction histogramOp = !ENABLE_HISTOGRAM_AGG ? null : getHistogramOp(aggOp);
5048 
5049  if (histogramOp != null) {
5050  final RelDataType histogramType = computeHistogramType(type);
5051 
5052  // For DECIMAL, since it's already represented as a bigint we
5053  // want to do a reinterpretCast instead of a cast to avoid
5054  // losing any precision.
5055  boolean reinterpretCast = type.getSqlTypeName() == SqlTypeName.DECIMAL;
5056 
5057  // Replace original expression with CAST of not one
5058  // of the supported types
5059  if (histogramType != type) {
5060  exprs = new ArrayList<>(exprs);
5061  exprs.set(0,
5062  reinterpretCast ? rexBuilder.makeReinterpretCast(
5063  histogramType, exprs.get(0), rexBuilder.makeLiteral(false))
5064  : rexBuilder.makeCast(histogramType, exprs.get(0)));
5065  }
5066 
5067  RexCallBinding bind = new RexCallBinding(rexBuilder.getTypeFactory(),
5068  SqlStdOperatorTable.HISTOGRAM_AGG,
5069  exprs,
5070  ImmutableList.of());
5071 
5072  RexNode over = rexBuilder.makeOver(
5073  SqlStdOperatorTable.HISTOGRAM_AGG.inferReturnType(bind),
5074  SqlStdOperatorTable.HISTOGRAM_AGG,
5075  exprs,
5076  partitionKeys,
5077  orderKeys,
5078  lowerBound,
5079  upperBound,
5080  window.isRows(),
5081  window.isAllowPartial(),
5082  false,
5083  distinct,
5084  ignoreNulls);
5085 
5086  RexNode histogramCall =
5087  rexBuilder.makeCall(histogramType, histogramOp, ImmutableList.of(over));
5088 
5089  // If needed, post Cast result back to original
5090  // type.
5091  if (histogramType != type) {
5092  if (reinterpretCast) {
5093  histogramCall = rexBuilder.makeReinterpretCast(
5094  type, histogramCall, rexBuilder.makeLiteral(false));
5095  } else {
5096  histogramCall = rexBuilder.makeCast(type, histogramCall);
5097  }
5098  }
5099 
5100  return histogramCall;
5101  } else {
5102  boolean needSum0 = aggOp == SqlStdOperatorTable.SUM && type.isNullable();
5103  SqlAggFunction aggOpToUse = needSum0 ? SqlStdOperatorTable.SUM0 : aggOp;
5104  return rexBuilder.makeOver(type,
5105  aggOpToUse,
5106  exprs,
5107  partitionKeys,
5108  orderKeys,
5109  lowerBound,
5110  upperBound,
5111  window.isRows(),
5112  window.isAllowPartial(),
5113  needSum0,
5114  distinct,
5115  ignoreNulls);
5116  }
5117  }

Member Data Documentation

◆ distinct

final boolean org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.distinct
private

Definition at line 5019 of file SqlToRelConverter.java.

◆ ENABLE_HISTOGRAM_AGG

final boolean org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.ENABLE_HISTOGRAM_AGG = false
staticpackage

Whether to convert calls to MIN(x) to HISTOGRAM_MIN(HISTOGRAM(x)). Histograms allow rolling computation, but require more space.

Definition at line 5012 of file SqlToRelConverter.java.

◆ ignoreNulls

final boolean org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.ignoreNulls
private

Definition at line 5020 of file SqlToRelConverter.java.

◆ lowerBound

final RexWindowBound org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.lowerBound
private

Definition at line 5016 of file SqlToRelConverter.java.

◆ orderKeys

final ImmutableList<RexFieldCollation> org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.orderKeys
private

Definition at line 5015 of file SqlToRelConverter.java.

◆ partitionKeys

final List<RexNode> org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.partitionKeys
private

Definition at line 5014 of file SqlToRelConverter.java.

◆ upperBound

final RexWindowBound org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.upperBound
private

Definition at line 5017 of file SqlToRelConverter.java.

◆ window

final SqlWindow org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.window
private

Definition at line 5018 of file SqlToRelConverter.java.


The documentation for this class was generated from the following file: