OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter Class Reference
+ Inheritance diagram for org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter:
+ Collaboration diagram for org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter:

Public Member Functions

 AggConverter (Blackboard bb, SqlSelect select)
 
int addGroupExpr (SqlNode expr)
 
Void visit (SqlIdentifier id)
 
Void visit (SqlNodeList nodeList)
 
Void visit (SqlLiteral lit)
 
Void visit (SqlDataTypeSpec type)
 
Void visit (SqlDynamicParam param)
 
Void visit (SqlIntervalQualifier intervalQualifier)
 
Void visit (SqlCall call)
 
int lookupGroupExpr (SqlNode expr)
 
RexNode lookupAggregates (SqlCall call)
 
List< Pair< RexNode, String > > getPreExprs ()
 
List< AggregateCall > getAggCalls ()
 
RelDataTypeFactory getTypeFactory ()
 

Public Attributes

final AggregatingSelectScope aggregatingSelectScope
 

Package Functions

void addAuxiliaryGroupExpr (SqlNode node, int index, AuxiliaryConverter converter)
 

Private Member Functions

void addExpr (RexNode expr, String name)
 
void translateAgg (SqlCall call)
 
void translateAgg (SqlCall call, SqlNode filter, SqlNodeList orderList, boolean ignoreNulls, SqlCall outerCall)
 
int lookupOrCreateGroupExpr (RexNode expr)
 

Private Attributes

final Blackboard bb
 
final Map< String, String > nameMap = new HashMap<>()
 
final SqlNodeList groupExprs = new SqlNodeList(SqlParserPos.ZERO)
 
final Map< SqlNode, Ord
< AuxiliaryConverter > > 
auxiliaryGroupExprs
 
final List< Pair< RexNode,
String > > 
convertedInputExprs = new ArrayList<>()
 
final List< AggregateCall > aggCalls = new ArrayList<>()
 
final Map< SqlNode, RexNode > aggMapping = new HashMap<>()
 
final Map< AggregateCall, RexNode > aggCallMapping = new HashMap<>()
 
boolean inOver = false
 

Detailed Description

Converts expressions to aggregates.

Consider the expression

SELECT deptno, SUM(2 * sal) FROM emp GROUP BY deptno

Then:

  • groupExprs = {SqlIdentifier(deptno)}
  • convertedInputExprs = {RexInputRef(deptno), 2 * RefInputRef(sal)}
  • inputRefs = {RefInputRef(#0), RexInputRef(#1)}
  • aggCalls = {AggCall(SUM, {1})}

Definition at line 4617 of file SqlToRelConverter.java.

Constructor & Destructor Documentation

org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.AggConverter ( Blackboard  bb,
SqlSelect  select 
)
inline

Creates an AggConverter.

The select parameter provides enough context to name aggregate calls which are top-level select list items.

Parameters
bbBlackboard
selectQuery being translated; provides context to give

Definition at line 4666 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.getValidator(), and Experimental.String.

4666  {
4667  this.bb = bb;
4668  this.aggregatingSelectScope =
4669  (AggregatingSelectScope) bb.getValidator().getSelectScope(select);
4670 
4671  // Collect all expressions used in the select list so that aggregate
4672  // calls can be named correctly.
4673  final SqlNodeList selectList = select.getSelectList();
4674  for (int i = 0; i < selectList.size(); i++) {
4675  SqlNode selectItem = selectList.get(i);
4676  String name = null;
4677  if (SqlUtil.isCallTo(selectItem, SqlStdOperatorTable.AS)) {
4678  final SqlCall call = (SqlCall) selectItem;
4679  selectItem = call.operand(0);
4680  name = call.operand(1).toString();
4681  }
4682  if (name == null) {
4683  name = validator.deriveAlias(selectItem, i);
4684  }
4685  nameMap.put(selectItem.toString(), name);
4686  }
4687  }

+ Here is the call graph for this function:

Member Function Documentation

void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.addAuxiliaryGroupExpr ( SqlNode  node,
int  index,
AuxiliaryConverter  converter 
)
inlinepackage

Definition at line 4711 of file SqlToRelConverter.java.

4711  {
4712  for (SqlNode node2 : auxiliaryGroupExprs.keySet()) {
4713  if (node2.equalsDeep(node, Litmus.IGNORE)) {
4714  return;
4715  }
4716  }
4717  auxiliaryGroupExprs.put(node, Ord.of(index, converter));
4718  }
final Map< SqlNode, Ord< AuxiliaryConverter > > auxiliaryGroupExprs
void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.addExpr ( RexNode  expr,
String  name 
)
inlineprivate

Adds an expression, deducing an appropriate name if possible.

Parameters
exprExpression
nameSuggested name

Definition at line 4726 of file SqlToRelConverter.java.

4726  {
4727  if ((name == null) && (expr instanceof RexInputRef)) {
4728  final int i = ((RexInputRef) expr).getIndex();
4729  name = bb.root.getRowType().getFieldList().get(i).getName();
4730  }
4731  if (Pair.right(convertedInputExprs).contains(name)) {
4732  // In case like 'SELECT ... GROUP BY x, y, x', don't add
4733  // name 'x' twice.
4734  name = null;
4735  }
4736  convertedInputExprs.add(Pair.of(expr, name));
4737  }
int org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.addGroupExpr ( SqlNode  expr)
inline

Definition at line 4689 of file SqlToRelConverter.java.

References Experimental.String.

4689  {
4690  int ref = lookupGroupExpr(expr);
4691  if (ref >= 0) {
4692  return ref;
4693  }
4694  final int index = groupExprs.size();
4695  groupExprs.add(expr);
4696  String name = nameMap.get(expr.toString());
4697  RexNode convExpr = bb.convertExpression(expr);
4698  addExpr(convExpr, name);
4699 
4700  if (expr instanceof SqlCall) {
4701  SqlCall call = (SqlCall) expr;
4702  for (Pair<SqlNode, AuxiliaryConverter> p :
4703  SqlStdOperatorTable.convertGroupToAuxiliaryCalls(call)) {
4704  addAuxiliaryGroupExpr(p.left, index, p.right);
4705  }
4706  }
4707 
4708  return index;
4709  }
void addAuxiliaryGroupExpr(SqlNode node, int index, AuxiliaryConverter converter)
List<AggregateCall> org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.getAggCalls ( )
inline

Definition at line 4983 of file SqlToRelConverter.java.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.createAggImpl().

4983  {
4984  return aggCalls;
4985  }

+ Here is the caller graph for this function:

List<Pair<RexNode, String> > org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.getPreExprs ( )
inline

Definition at line 4979 of file SqlToRelConverter.java.

4979  {
4980  return convertedInputExprs;
4981  }
RelDataTypeFactory org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.getTypeFactory ( )
inline

Definition at line 4987 of file SqlToRelConverter.java.

4987  {
4988  return typeFactory;
4989  }
RexNode org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.lookupAggregates ( SqlCall  call)
inline

Definition at line 4961 of file SqlToRelConverter.java.

References assert().

4961  {
4962  // assert call.getOperator().isAggregator();
4963  assert bb.agg == this;
4964 
4965  for (Map.Entry<SqlNode, Ord<AuxiliaryConverter>> e :
4966  auxiliaryGroupExprs.entrySet()) {
4967  if (call.equalsDeep(e.getKey(), Litmus.IGNORE)) {
4968  AuxiliaryConverter converter = e.getValue().e;
4969  final int groupOrdinal = e.getValue().i;
4970  return converter.convert(rexBuilder,
4971  convertedInputExprs.get(groupOrdinal).left,
4972  rexBuilder.makeInputRef(bb.root, groupOrdinal));
4973  }
4974  }
4975 
4976  return aggMapping.get(call);
4977  }
final Map< SqlNode, Ord< AuxiliaryConverter > > auxiliaryGroupExprs
int64_t const int32_t sz assert(dest)

+ Here is the call graph for this function:

int org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.lookupGroupExpr ( SqlNode  expr)
inline

If an expression is structurally identical to one of the group-by expressions, returns a reference to the expression, otherwise returns null.

Definition at line 4951 of file SqlToRelConverter.java.

4951  {
4952  for (int i = 0; i < groupExprs.size(); i++) {
4953  SqlNode groupExpr = groupExprs.get(i);
4954  if (expr.equalsDeep(groupExpr, Litmus.IGNORE)) {
4955  return i;
4956  }
4957  }
4958  return -1;
4959  }
int org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.lookupOrCreateGroupExpr ( RexNode  expr)
inlineprivate

Definition at line 4933 of file SqlToRelConverter.java.

4933  {
4934  int index = 0;
4935  for (RexNode convertedInputExpr : Pair.left(convertedInputExprs)) {
4936  if (expr.equals(convertedInputExpr)) {
4937  return index;
4938  }
4939  ++index;
4940  }
4941 
4942  // not found -- add it
4943  addExpr(expr, null);
4944  return index;
4945  }
void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.translateAgg ( SqlCall  call)
inlineprivate

Definition at line 4819 of file SqlToRelConverter.java.

4819  {
4820  translateAgg(call, null, null, false, call);
4821  }
void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.translateAgg ( SqlCall  call,
SqlNode  filter,
SqlNodeList  orderList,
boolean  ignoreNulls,
SqlCall  outerCall 
)
inlineprivate

Definition at line 4823 of file SqlToRelConverter.java.

References run_benchmark_import.args, assert(), and run_benchmark_import.type.

4827  {
4828  assert bb.agg == this;
4829  assert outerCall != null;
4830  switch (call.getKind()) {
4831  case FILTER:
4832  assert filter == null;
4833  translateAgg(
4834  call.operand(0), call.operand(1), orderList, ignoreNulls, outerCall);
4835  return;
4836  case WITHIN_GROUP:
4837  assert orderList == null;
4838  translateAgg(call.operand(0), filter, call.operand(1), ignoreNulls, outerCall);
4839  return;
4840  case IGNORE_NULLS:
4841  ignoreNulls = true;
4842  // fall through
4843  case RESPECT_NULLS:
4844  translateAgg(call.operand(0), filter, orderList, ignoreNulls, outerCall);
4845  return;
4846  }
4847  final List<Integer> args = new ArrayList<>();
4848  int filterArg = -1;
4849  final List<RelDataType> argTypes = call.getOperator() instanceof SqlCountAggFunction
4850  ? new ArrayList<>(call.getOperandList().size())
4851  : null;
4852  try {
4853  // switch out of agg mode
4854  bb.agg = null;
4855  for (SqlNode operand : call.getOperandList()) {
4856  // special case for COUNT(*): delete the *
4857  if (operand instanceof SqlIdentifier) {
4858  SqlIdentifier id = (SqlIdentifier) operand;
4859  if (id.isStar()) {
4860  assert call.operandCount() == 1;
4861  assert args.isEmpty();
4862  break;
4863  }
4864  }
4865  RexNode convertedExpr = bb.convertExpression(operand);
4866  assert convertedExpr != null;
4867  if (argTypes != null) {
4868  argTypes.add(convertedExpr.getType());
4869  }
4870  args.add(lookupOrCreateGroupExpr(convertedExpr));
4871  }
4872 
4873  if (filter != null) {
4874  RexNode convertedExpr = bb.convertExpression(filter);
4875  assert convertedExpr != null;
4876  if (convertedExpr.getType().isNullable()) {
4877  convertedExpr =
4878  rexBuilder.makeCall(SqlStdOperatorTable.IS_TRUE, convertedExpr);
4879  }
4880  filterArg = lookupOrCreateGroupExpr(convertedExpr);
4881  }
4882  } finally {
4883  // switch back into agg mode
4884  bb.agg = this;
4885  }
4886 
4887  SqlAggFunction aggFunction = (SqlAggFunction) call.getOperator();
4888  final RelDataType type = validator.deriveType(bb.scope, call);
4889  boolean distinct = false;
4890  SqlLiteral quantifier = call.getFunctionQuantifier();
4891  if ((null != quantifier) && (quantifier.getValue() == SqlSelectKeyword.DISTINCT)) {
4892  distinct = true;
4893  }
4894  boolean approximate = false;
4895  if (aggFunction == SqlStdOperatorTable.APPROX_COUNT_DISTINCT) {
4896  aggFunction = SqlStdOperatorTable.COUNT;
4897  distinct = true;
4898  approximate = true;
4899  }
4900  final RelCollation collation;
4901  if (orderList == null || orderList.size() == 0) {
4902  collation = RelCollations.EMPTY;
4903  } else {
4904  collation = RelCollations.of(
4905  orderList.getList()
4906  .stream()
4907  .map(order
4908  -> bb.convertSortExpression(order,
4909  RelFieldCollation.Direction.ASCENDING,
4910  RelFieldCollation.NullDirection.UNSPECIFIED))
4911  .map(fieldCollation
4912  -> new RelFieldCollation(
4913  lookupOrCreateGroupExpr(fieldCollation.left),
4914  fieldCollation.getDirection(),
4915  fieldCollation.getNullDirection()))
4916  .collect(Collectors.toList()));
4917  }
4918  final AggregateCall aggCall = AggregateCall.create(aggFunction,
4919  distinct,
4920  approximate,
4921  ignoreNulls,
4922  args,
4923  filterArg,
4924  collation,
4925  type,
4926  nameMap.get(outerCall.toString()));
4927  final AggregatingSelectScope.Resolved r = aggregatingSelectScope.resolved.get();
4928  RexNode rex = rexBuilder.addAggCall(
4929  aggCall, groupExprs.size(), aggCalls, aggCallMapping, argTypes);
4930  aggMapping.put(outerCall, rex);
4931  }
int64_t const int32_t sz assert(dest)

+ Here is the call graph for this function:

Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlIdentifier  id)
inline

Definition at line 4739 of file SqlToRelConverter.java.

References Void.

4739  {
4740  return null;
4741  }
Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlNodeList  nodeList)
inline

Definition at line 4743 of file SqlToRelConverter.java.

References Void.

4743  {
4744  for (int i = 0; i < nodeList.size(); i++) {
4745  nodeList.get(i).accept(this);
4746  }
4747  return null;
4748  }
Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlLiteral  lit)
inline

Definition at line 4750 of file SqlToRelConverter.java.

References Void.

4750  {
4751  return null;
4752  }
Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlDataTypeSpec  type)
inline

Definition at line 4754 of file SqlToRelConverter.java.

References Void.

4754  {
4755  return null;
4756  }
Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlDynamicParam  param)
inline

Definition at line 4758 of file SqlToRelConverter.java.

References Void.

4758  {
4759  return null;
4760  }
Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlIntervalQualifier  intervalQualifier)
inline

Definition at line 4762 of file SqlToRelConverter.java.

References Void.

4762  {
4763  return null;
4764  }
Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlCall  call)
inline

Definition at line 4766 of file SqlToRelConverter.java.

References assert(), and Void.

4766  {
4767  switch (call.getKind()) {
4768  case FILTER:
4769  case WITHIN_GROUP:
4770  translateAgg(call);
4771  return null;
4772  case SELECT:
4773  // rchen 2006-10-17:
4774  // for now do not detect aggregates in sub-queries.
4775  return null;
4776  }
4777  final boolean prevInOver = inOver;
4778  // Ignore window aggregates and ranking functions (associated with OVER
4779  // operator). However, do not ignore nested window aggregates.
4780  if (call.getOperator().getKind() == SqlKind.OVER) {
4781  // Track aggregate nesting levels only within an OVER operator.
4782  List<SqlNode> operandList = call.getOperandList();
4783  assert operandList.size() == 2;
4784 
4785  // Ignore the top level window aggregates and ranking functions
4786  // positioned as the first operand of a OVER operator
4787  inOver = true;
4788  operandList.get(0).accept(this);
4789 
4790  // Normal translation for the second operand of a OVER operator
4791  inOver = false;
4792  operandList.get(1).accept(this);
4793  return null;
4794  }
4795 
4796  // Do not translate the top level window aggregate. Only do so for
4797  // nested aggregates, if present
4798  if (call.getOperator().isAggregator()) {
4799  if (inOver) {
4800  // Add the parent aggregate level before visiting its children
4801  inOver = false;
4802  } else {
4803  // We're beyond the one ignored level
4804  translateAgg(call);
4805  return null;
4806  }
4807  }
4808  for (SqlNode operand : call.getOperandList()) {
4809  // Operands are occasionally null, e.g. switched CASE arg 0.
4810  if (operand != null) {
4811  operand.accept(this);
4812  }
4813  }
4814  // Remove the parent aggregate level after visiting its children
4815  inOver = prevInOver;
4816  return null;
4817  }
int64_t const int32_t sz assert(dest)

+ Here is the call graph for this function:

Member Data Documentation

final Map<AggregateCall, RexNode> org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.aggCallMapping = new HashMap<>()
private

Definition at line 4651 of file SqlToRelConverter.java.

final List<AggregateCall> org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.aggCalls = new ArrayList<>()
private

Expressions to be evaluated as rows are being placed into the aggregate's hash table. This is when group functions such as TUMBLE cause rows to be expanded.

Definition at line 4649 of file SqlToRelConverter.java.

final Map<SqlNode, RexNode> org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.aggMapping = new HashMap<>()
private

Definition at line 4650 of file SqlToRelConverter.java.

final AggregatingSelectScope org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.aggregatingSelectScope

Definition at line 4619 of file SqlToRelConverter.java.

final Map<SqlNode, Ord<AuxiliaryConverter> > org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.auxiliaryGroupExprs
private
Initial value:
=
new HashMap<>()

The auxiliary group-by expressions.

Definition at line 4631 of file SqlToRelConverter.java.

final Blackboard org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.bb
private

Definition at line 4618 of file SqlToRelConverter.java.

final List<Pair<RexNode, String> > org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.convertedInputExprs = new ArrayList<>()
private

Input expressions for the group columns and aggregates, in RexNode format. The first elements of the list correspond to the elements in groupExprs; the remaining elements are for aggregates. The right field of each pair is the name of the expression, where the expressions are simple mappings to input fields.

Definition at line 4641 of file SqlToRelConverter.java.

final SqlNodeList org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.groupExprs = new SqlNodeList(SqlParserPos.ZERO)
private

The group-by expressions, in SqlNode format.

Definition at line 4626 of file SqlToRelConverter.java.

boolean org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.inOver = false
private

Are we directly inside a windowed aggregate?

Definition at line 4654 of file SqlToRelConverter.java.

final Map<String, String> org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.nameMap = new HashMap<>()
private

Definition at line 4621 of file SqlToRelConverter.java.


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