OmniSciDB  c07336695a
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 4571 of file SqlToRelConverter.java.

Constructor & Destructor Documentation

◆ AggConverter()

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

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

4619  {
4620  this.bb = bb;
4621  this.aggregatingSelectScope =
4622  (AggregatingSelectScope) bb.getValidator().getSelectScope(select);
4623 
4624  // Collect all expressions used in the select list so that aggregate
4625  // calls can be named correctly.
4626  final SqlNodeList selectList = select.getSelectList();
4627  for (int i = 0; i < selectList.size(); i++) {
4628  SqlNode selectItem = selectList.get(i);
4629  String name = null;
4630  if (SqlUtil.isCallTo(selectItem, SqlStdOperatorTable.AS)) {
4631  final SqlCall call = (SqlCall) selectItem;
4632  selectItem = call.operand(0);
4633  name = call.operand(1).toString();
4634  }
4635  if (name == null) {
4636  name = validator.deriveAlias(selectItem, i);
4637  }
4638  nameMap.put(selectItem.toString(), name);
4639  }
4640  }
+ Here is the call graph for this function:

Member Function Documentation

◆ addAuxiliaryGroupExpr()

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

Definition at line 4664 of file SqlToRelConverter.java.

4664  {
4665  for (SqlNode node2 : auxiliaryGroupExprs.keySet()) {
4666  if (node2.equalsDeep(node, Litmus.IGNORE)) {
4667  return;
4668  }
4669  }
4670  auxiliaryGroupExprs.put(node, Ord.of(index, converter));
4671  }
final Map< SqlNode, Ord< AuxiliaryConverter > > auxiliaryGroupExprs

◆ addExpr()

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

References org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.root.

4679  {
4680  if ((name == null) && (expr instanceof RexInputRef)) {
4681  final int i = ((RexInputRef) expr).getIndex();
4682  name = bb.root.getRowType().getFieldList().get(i).getName();
4683  }
4684  if (Pair.right(convertedInputExprs).contains(name)) {
4685  // In case like 'SELECT ... GROUP BY x, y, x', don't add
4686  // name 'x' twice.
4687  name = null;
4688  }
4689  convertedInputExprs.add(Pair.of(expr, name));
4690  }

◆ addGroupExpr()

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

Definition at line 4642 of file SqlToRelConverter.java.

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

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

4642  {
4643  int ref = lookupGroupExpr(expr);
4644  if (ref >= 0) {
4645  return ref;
4646  }
4647  final int index = groupExprs.size();
4648  groupExprs.add(expr);
4649  String name = nameMap.get(expr.toString());
4650  RexNode convExpr = bb.convertExpression(expr);
4651  addExpr(convExpr, name);
4652 
4653  if (expr instanceof SqlCall) {
4654  SqlCall call = (SqlCall) expr;
4655  for (Pair<SqlNode, AuxiliaryConverter> p :
4656  SqlStdOperatorTable.convertGroupToAuxiliaryCalls(call)) {
4657  addAuxiliaryGroupExpr(p.left, index, p.right);
4658  }
4659  }
4660 
4661  return index;
4662  }
void addAuxiliaryGroupExpr(SqlNode node, int index, AuxiliaryConverter converter)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getAggCalls()

List<AggregateCall> org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.getAggCalls ( )
inline

Definition at line 4937 of file SqlToRelConverter.java.

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

4937  {
4938  return aggCalls;
4939  }
+ Here is the caller graph for this function:

◆ getPreExprs()

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

Definition at line 4933 of file SqlToRelConverter.java.

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

4933  {
4934  return convertedInputExprs;
4935  }
+ Here is the caller graph for this function:

◆ getTypeFactory()

RelDataTypeFactory org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.getTypeFactory ( )
inline

Definition at line 4941 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory.

4941  {
4942  return typeFactory;
4943  }

◆ lookupAggregates()

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.lookupAggregates ( SqlCall  call)
inline

Definition at line 4915 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.agg, org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder, and org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.root.

4915  {
4916  // assert call.getOperator().isAggregator();
4917  assert bb.agg == this;
4918 
4919  for (Map.Entry<SqlNode, Ord<AuxiliaryConverter>> e :
4920  auxiliaryGroupExprs.entrySet()) {
4921  if (call.equalsDeep(e.getKey(), Litmus.IGNORE)) {
4922  AuxiliaryConverter converter = e.getValue().e;
4923  final int groupOrdinal = e.getValue().i;
4924  return converter.convert(rexBuilder,
4925  convertedInputExprs.get(groupOrdinal).left,
4926  rexBuilder.makeInputRef(bb.root, groupOrdinal));
4927  }
4928  }
4929 
4930  return aggMapping.get(call);
4931  }
final Map< SqlNode, Ord< AuxiliaryConverter > > auxiliaryGroupExprs

◆ lookupGroupExpr()

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

4905  {
4906  for (int i = 0; i < groupExprs.size(); i++) {
4907  SqlNode groupExpr = groupExprs.get(i);
4908  if (expr.equalsDeep(groupExpr, Litmus.IGNORE)) {
4909  return i;
4910  }
4911  }
4912  return -1;
4913  }

◆ lookupOrCreateGroupExpr()

int org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.lookupOrCreateGroupExpr ( RexNode  expr)
inlineprivate

Definition at line 4886 of file SqlToRelConverter.java.

4886  {
4887  int index = 0;
4888  for (RexNode convertedInputExpr : Pair.left(convertedInputExprs)) {
4889  if (expr.equals(convertedInputExpr)) {
4890  return index;
4891  }
4892  ++index;
4893  }
4894 
4895  // not found -- add it
4896  addExpr(expr, null);
4897  return index;
4898  }

◆ translateAgg() [1/2]

void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.translateAgg ( SqlCall  call)
inlineprivate

Definition at line 4772 of file SqlToRelConverter.java.

4772  {
4773  translateAgg(call, null, null, false, call);
4774  }

◆ translateAgg() [2/2]

void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.translateAgg ( SqlCall  call,
SqlNode  filter,
SqlNodeList  orderList,
boolean  ignoreNulls,
SqlCall  outerCall 
)
inlineprivate

Definition at line 4776 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.agg, run-benchmark-import.args, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.convertExpression(), org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.convertSortExpression(), org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.scope, run-benchmark-import.type, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

4780  {
4781  assert bb.agg == this;
4782  assert outerCall != null;
4783  switch (call.getKind()) {
4784  case FILTER:
4785  assert filter == null;
4786  translateAgg(
4787  call.operand(0), call.operand(1), orderList, ignoreNulls, outerCall);
4788  return;
4789  case WITHIN_GROUP:
4790  assert orderList == null;
4791  translateAgg(call.operand(0), filter, call.operand(1), ignoreNulls, outerCall);
4792  return;
4793  case IGNORE_NULLS:
4794  ignoreNulls = true;
4795  // fall through
4796  case RESPECT_NULLS:
4797  translateAgg(call.operand(0), filter, orderList, ignoreNulls, outerCall);
4798  return;
4799  }
4800  final List<Integer> args = new ArrayList<>();
4801  int filterArg = -1;
4802  final List<RelDataType> argTypes = call.getOperator() instanceof SqlCountAggFunction
4803  ? new ArrayList<>(call.getOperandList().size())
4804  : null;
4805  try {
4806  // switch out of agg mode
4807  bb.agg = null;
4808  for (SqlNode operand : call.getOperandList()) {
4809  // special case for COUNT(*): delete the *
4810  if (operand instanceof SqlIdentifier) {
4811  SqlIdentifier id = (SqlIdentifier) operand;
4812  if (id.isStar()) {
4813  assert call.operandCount() == 1;
4814  assert args.isEmpty();
4815  break;
4816  }
4817  }
4818  RexNode convertedExpr = bb.convertExpression(operand);
4819  assert convertedExpr != null;
4820  if (argTypes != null) {
4821  argTypes.add(convertedExpr.getType());
4822  }
4823  args.add(lookupOrCreateGroupExpr(convertedExpr));
4824  }
4825 
4826  if (filter != null) {
4827  RexNode convertedExpr = bb.convertExpression(filter);
4828  assert convertedExpr != null;
4829  if (convertedExpr.getType().isNullable()) {
4830  convertedExpr =
4831  rexBuilder.makeCall(SqlStdOperatorTable.IS_TRUE, convertedExpr);
4832  }
4833  filterArg = lookupOrCreateGroupExpr(convertedExpr);
4834  }
4835  } finally {
4836  // switch back into agg mode
4837  bb.agg = this;
4838  }
4839 
4840  SqlAggFunction aggFunction = (SqlAggFunction) call.getOperator();
4841  final RelDataType type = validator.deriveType(bb.scope, call);
4842  boolean distinct = false;
4843  SqlLiteral quantifier = call.getFunctionQuantifier();
4844  if ((null != quantifier) && (quantifier.getValue() == SqlSelectKeyword.DISTINCT)) {
4845  distinct = true;
4846  }
4847  boolean approximate = false;
4848  if (aggFunction == SqlStdOperatorTable.APPROX_COUNT_DISTINCT) {
4849  aggFunction = SqlStdOperatorTable.COUNT;
4850  distinct = true;
4851  approximate = true;
4852  }
4853  final RelCollation collation;
4854  if (orderList == null || orderList.size() == 0) {
4855  collation = RelCollations.EMPTY;
4856  } else {
4857  collation = RelCollations.of(
4858  orderList.getList()
4859  .stream()
4860  .map(order
4861  -> bb.convertSortExpression(order,
4862  RelFieldCollation.Direction.ASCENDING,
4863  RelFieldCollation.NullDirection.UNSPECIFIED))
4864  .map(fieldCollation
4865  -> new RelFieldCollation(
4866  lookupOrCreateGroupExpr(fieldCollation.left),
4867  fieldCollation.getDirection(),
4868  fieldCollation.getNullDirection()))
4869  .collect(Collectors.toList()));
4870  }
4871  final AggregateCall aggCall = AggregateCall.create(aggFunction,
4872  distinct,
4873  approximate,
4874  ignoreNulls,
4875  args,
4876  filterArg,
4877  collation,
4878  type,
4879  nameMap.get(outerCall.toString()));
4880  final AggregatingSelectScope.Resolved r = aggregatingSelectScope.resolved.get();
4881  RexNode rex = rexBuilder.addAggCall(
4882  aggCall, groupExprs.size(), aggCalls, aggCallMapping, argTypes);
4883  aggMapping.put(outerCall, rex);
4884  }
RexFieldCollation convertSortExpression(SqlNode expr, RelFieldCollation.Direction direction, RelFieldCollation.NullDirection nullDirection)
+ Here is the call graph for this function:

◆ visit() [1/7]

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

Definition at line 4692 of file SqlToRelConverter.java.

4692  {
4693  return null;
4694  }

◆ visit() [2/7]

Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlNodeList  nodeList)
inline

Definition at line 4696 of file SqlToRelConverter.java.

4696  {
4697  for (int i = 0; i < nodeList.size(); i++) {
4698  nodeList.get(i).accept(this);
4699  }
4700  return null;
4701  }

◆ visit() [3/7]

Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlLiteral  lit)
inline

Definition at line 4703 of file SqlToRelConverter.java.

4703  {
4704  return null;
4705  }

◆ visit() [4/7]

Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlDataTypeSpec  type)
inline

Definition at line 4707 of file SqlToRelConverter.java.

4707  {
4708  return null;
4709  }

◆ visit() [5/7]

Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlDynamicParam  param)
inline

Definition at line 4711 of file SqlToRelConverter.java.

4711  {
4712  return null;
4713  }

◆ visit() [6/7]

Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlIntervalQualifier  intervalQualifier)
inline

Definition at line 4715 of file SqlToRelConverter.java.

4715  {
4716  return null;
4717  }

◆ visit() [7/7]

Void org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.visit ( SqlCall  call)
inline

Definition at line 4719 of file SqlToRelConverter.java.

4719  {
4720  switch (call.getKind()) {
4721  case FILTER:
4722  case WITHIN_GROUP:
4723  translateAgg(call);
4724  return null;
4725  case SELECT:
4726  // rchen 2006-10-17:
4727  // for now do not detect aggregates in sub-queries.
4728  return null;
4729  }
4730  final boolean prevInOver = inOver;
4731  // Ignore window aggregates and ranking functions (associated with OVER
4732  // operator). However, do not ignore nested window aggregates.
4733  if (call.getOperator().getKind() == SqlKind.OVER) {
4734  // Track aggregate nesting levels only within an OVER operator.
4735  List<SqlNode> operandList = call.getOperandList();
4736  assert operandList.size() == 2;
4737 
4738  // Ignore the top level window aggregates and ranking functions
4739  // positioned as the first operand of a OVER operator
4740  inOver = true;
4741  operandList.get(0).accept(this);
4742 
4743  // Normal translation for the second operand of a OVER operator
4744  inOver = false;
4745  operandList.get(1).accept(this);
4746  return null;
4747  }
4748 
4749  // Do not translate the top level window aggregate. Only do so for
4750  // nested aggregates, if present
4751  if (call.getOperator().isAggregator()) {
4752  if (inOver) {
4753  // Add the parent aggregate level before visiting its children
4754  inOver = false;
4755  } else {
4756  // We're beyond the one ignored level
4757  translateAgg(call);
4758  return null;
4759  }
4760  }
4761  for (SqlNode operand : call.getOperandList()) {
4762  // Operands are occasionally null, e.g. switched CASE arg 0.
4763  if (operand != null) {
4764  operand.accept(this);
4765  }
4766  }
4767  // Remove the parent aggregate level after visiting its children
4768  inOver = prevInOver;
4769  return null;
4770  }

Member Data Documentation

◆ aggCallMapping

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

Definition at line 4605 of file SqlToRelConverter.java.

◆ aggCalls

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

◆ aggMapping

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

Definition at line 4604 of file SqlToRelConverter.java.

◆ aggregatingSelectScope

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

◆ auxiliaryGroupExprs

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

◆ bb

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

Definition at line 4572 of file SqlToRelConverter.java.

◆ convertedInputExprs

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

◆ groupExprs

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

The group-by expressions, in SqlNode format.

Definition at line 4580 of file SqlToRelConverter.java.

◆ inOver

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

Are we directly inside a windowed aggregate?

Definition at line 4608 of file SqlToRelConverter.java.

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

◆ nameMap

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

Definition at line 4575 of file SqlToRelConverter.java.


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