OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 5058 of file SqlToRelConverter.java.

Constructor & Destructor Documentation

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

Definition at line 5073 of file SqlToRelConverter.java.

5079  {
5080  this.partitionKeys = partitionKeys;
5081  this.orderKeys = orderKeys;
5082  this.lowerBound = lowerBound;
5083  this.upperBound = upperBound;
5084  this.window = window;
5085  this.distinct = distinct;
5086  this.ignoreNulls = ignoreNulls;
5087  }

Member Function Documentation

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 5199 of file SqlToRelConverter.java.

References run_benchmark_import.type.

5199  {
5200  if (SqlTypeUtil.isExactNumeric(type)
5201  && type.getSqlTypeName() != SqlTypeName.BIGINT) {
5202  return typeFactory.createSqlType(SqlTypeName.BIGINT);
5203  } else if (SqlTypeUtil.isApproximateNumeric(type)
5204  && type.getSqlTypeName() != SqlTypeName.DOUBLE) {
5205  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
5206  } else {
5207  return type;
5208  }
5209  }
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 5181 of file SqlToRelConverter.java.

5181  {
5182  if (aggFunction == SqlStdOperatorTable.MIN) {
5183  return SqlStdOperatorTable.HISTOGRAM_MIN;
5184  } else if (aggFunction == SqlStdOperatorTable.MAX) {
5185  return SqlStdOperatorTable.HISTOGRAM_MAX;
5186  } else if (aggFunction == SqlStdOperatorTable.FIRST_VALUE) {
5187  return SqlStdOperatorTable.HISTOGRAM_FIRST_VALUE;
5188  } else if (aggFunction == SqlStdOperatorTable.LAST_VALUE) {
5189  return SqlStdOperatorTable.HISTOGRAM_LAST_VALUE;
5190  } else {
5191  return null;
5192  }
5193  }
RexNode org.apache.calcite.sql2rel.SqlToRelConverter.HistogramShuttle.visitCall ( RexCall  call)
inline

Definition at line 5089 of file SqlToRelConverter.java.

References run_benchmark_import.type.

5089  {
5090  final SqlOperator op = call.getOperator();
5091  if (!(op instanceof SqlAggFunction)) {
5092  return super.visitCall(call);
5093  }
5094  final SqlAggFunction aggOp = (SqlAggFunction) op;
5095  final RelDataType type = call.getType();
5096  List<RexNode> exprs = call.getOperands();
5097 
5098  SqlFunction histogramOp = !ENABLE_HISTOGRAM_AGG ? null : getHistogramOp(aggOp);
5099 
5100  if (histogramOp != null) {
5101  final RelDataType histogramType = computeHistogramType(type);
5102 
5103  // For DECIMAL, since it's already represented as a bigint we
5104  // want to do a reinterpretCast instead of a cast to avoid
5105  // losing any precision.
5106  boolean reinterpretCast = type.getSqlTypeName() == SqlTypeName.DECIMAL;
5107 
5108  // Replace original expression with CAST of not one
5109  // of the supported types
5110  if (histogramType != type) {
5111  exprs = new ArrayList<>(exprs);
5112  exprs.set(0,
5113  reinterpretCast ? rexBuilder.makeReinterpretCast(
5114  histogramType, exprs.get(0), rexBuilder.makeLiteral(false))
5115  : rexBuilder.makeCast(histogramType, exprs.get(0)));
5116  }
5117 
5118  RexCallBinding bind = new RexCallBinding(rexBuilder.getTypeFactory(),
5119  SqlStdOperatorTable.HISTOGRAM_AGG,
5120  exprs,
5121  ImmutableList.of());
5122 
5123  RexNode over = rexBuilder.makeOver(
5124  SqlStdOperatorTable.HISTOGRAM_AGG.inferReturnType(bind),
5125  SqlStdOperatorTable.HISTOGRAM_AGG,
5126  exprs,
5127  partitionKeys,
5128  orderKeys,
5129  lowerBound,
5130  upperBound,
5131  window.isRows(),
5132  window.isAllowPartial(),
5133  false,
5134  distinct,
5135  ignoreNulls);
5136 
5137  RexNode histogramCall =
5138  rexBuilder.makeCall(histogramType, histogramOp, ImmutableList.of(over));
5139 
5140  // If needed, post Cast result back to original
5141  // type.
5142  if (histogramType != type) {
5143  if (reinterpretCast) {
5144  histogramCall = rexBuilder.makeReinterpretCast(
5145  type, histogramCall, rexBuilder.makeLiteral(false));
5146  } else {
5147  histogramCall = rexBuilder.makeCast(type, histogramCall);
5148  }
5149  }
5150 
5151  return histogramCall;
5152  } else {
5153  boolean needSum0 = aggOp == SqlStdOperatorTable.SUM && type.isNullable();
5154  SqlAggFunction aggOpToUse = needSum0 ? SqlStdOperatorTable.SUM0 : aggOp;
5155  return rexBuilder.makeOver(type,
5156  aggOpToUse,
5157  exprs,
5158  partitionKeys,
5159  orderKeys,
5160  lowerBound,
5161  upperBound,
5162  window.isRows(),
5163  window.isAllowPartial(),
5164  needSum0,
5165  distinct,
5166  ignoreNulls);
5167  }
5168  }

Member Data Documentation

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

Definition at line 5070 of file SqlToRelConverter.java.

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 5063 of file SqlToRelConverter.java.

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

Definition at line 5071 of file SqlToRelConverter.java.

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

Definition at line 5067 of file SqlToRelConverter.java.

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

Definition at line 5066 of file SqlToRelConverter.java.

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

Definition at line 5065 of file SqlToRelConverter.java.

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

Definition at line 5068 of file SqlToRelConverter.java.

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

Definition at line 5069 of file SqlToRelConverter.java.


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