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

Classes

class  Blackboard
 
class  DeferredLookup
 
class  NoOpSubQueryConverter
 
class  RexAccessShuttle
 

Public Member Functions

 SqlToRelConverter (RelOptTable.ViewExpander viewExpander, SqlValidator validator, Prepare.CatalogReader catalogReader, RelOptPlanner planner, RexBuilder rexBuilder, SqlRexConvertletTable convertletTable)
 
 SqlToRelConverter (RelOptTable.ViewExpander viewExpander, SqlValidator validator, Prepare.CatalogReader catalogReader, RelOptCluster cluster, SqlRexConvertletTable convertletTable)
 
 SqlToRelConverter (RelOptTable.ViewExpander viewExpander, SqlValidator validator, Prepare.CatalogReader catalogReader, RelOptCluster cluster, SqlRexConvertletTable convertletTable, Config config)
 
RelOptCluster getCluster ()
 
RexBuilder getRexBuilder ()
 
int getDynamicParamCount ()
 
RelDataType getDynamicParamType (int index)
 
int getDynamicParamCountInExplain (boolean increment)
 
Map< SqlNode, RexNode > getMapConvertedNonCorrSubqs ()
 
void addConvertedNonCorrSubqs (Map< SqlNode, RexNode > alreadyConvertedNonCorrSubqs)
 
void setSubQueryConverter (SubQueryConverter converter)
 
void setDynamicParamCountInExplain (int explainParamCount)
 
RelNode flattenTypes (RelNode rootRel, boolean restructure)
 
RelNode decorrelate (SqlNode query, RelNode rootRel)
 
RelNode trimUnusedFields (boolean ordered, RelNode rootRel)
 
RelRoot convertQuery (SqlNode query, final boolean needsValidation, final boolean top)
 
RelNode convertSelect (SqlSelect select, boolean top)
 
RelNode convertToSingleValueSubq (SqlNode query, RelNode plan)
 
RexNode convertExpression (SqlNode node)
 
RexNode convertExpression (SqlNode node, Map< String, RexNode > nameToNodeMap)
 
RexDynamicParam convertDynamicParam (final SqlDynamicParam dynamicParam)
 
boolean isTrimUnusedFields ()
 
RelNode toRel (final RelOptTable table, final List< RelHint > hints)
 
RelRoot convertWith (SqlWith with, boolean top)
 
RelNode convertValues (SqlCall values, RelDataType targetRowType)
 

Static Public Member Functions

static boolean isOrdered (SqlNode query)
 

Public Attributes

final SqlToRelConverter.Config config
 
final RelOptTable.ViewExpander viewExpander
 

Static Public Attributes

static final int DEFAULT_IN_SUB_QUERY_THRESHOLD = 20
 
static final int DEFAULT_IN_SUBQUERY_THRESHOLD = DEFAULT_IN_SUB_QUERY_THRESHOLD
 

Protected Member Functions

RelFieldTrimmer newFieldTrimmer ()
 
Blackboard createBlackboard (SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)
 
void convertSelectImpl (final Blackboard bb, SqlSelect select)
 
void convertOrder (SqlSelect select, Blackboard bb, RelCollation collation, List< SqlNode > orderExprList, SqlNode offset, SqlNode fetch)
 
int getInSubqueryThreshold ()
 
RexNode convertExtendedExpression (SqlNode node, Blackboard bb)
 
void convertFrom (Blackboard bb, SqlNode from)
 
void convertFrom (Blackboard bb, SqlNode from, List< String > fieldNames)
 
void convertMatchRecognize (Blackboard bb, SqlCall call)
 
void convertCollectionTable (Blackboard bb, SqlCall call)
 
void afterTableFunction (SqlToRelConverter.Blackboard bb, SqlCall call, LogicalTableFunctionScan callRel)
 
RelNode createJoin (Blackboard bb, RelNode leftRel, RelNode rightRel, RexNode joinCond, JoinRelType joinType)
 
List< RelDataTypeField > getSystemFields ()
 
void convertAgg (Blackboard bb, SqlSelect select, List< SqlNode > orderExprList)
 
final void createAggImpl (Blackboard bb, final AggConverter aggConverter, SqlNodeList selectList, SqlNodeList groupList, SqlNode having, List< SqlNode > orderExprList)
 
RelNode createAggregate (Blackboard bb, ImmutableBitSet groupSet, ImmutableList< ImmutableBitSet > groupSets, List< AggregateCall > aggCalls)
 
void gatherOrderExprs (Blackboard bb, SqlSelect select, SqlNodeList orderList, List< SqlNode > extraOrderExprs, List< RelFieldCollation > collationList)
 
RelFieldCollation convertOrderItem (SqlSelect select, SqlNode orderItem, List< SqlNode > extraExprs, RelFieldCollation.Direction direction, RelFieldCollation.NullDirection nullDirection)
 
boolean enableDecorrelation ()
 
RelNode decorrelateQuery (RelNode rootRel)
 
RelRoot convertQueryRecursive (SqlNode query, boolean top, RelDataType targetRowType)
 
RelNode convertSetOp (SqlCall call)
 
RelNode convertInsert (SqlInsert call)
 
RelOptTable getTargetTable (SqlNode call)
 
RelNode convertColumnList (final SqlInsert call, RelNode source)
 
void collectInsertTargets (SqlInsert call, final RexNode sourceRef, final List< String > targetColumnNames, List< RexNode > columnExprs)
 
RexNode adjustInputRef (Blackboard bb, RexInputRef inputRef)
 
void extraSelectItems (Blackboard bb, SqlSelect select, List< RexNode > exprList, List< String > nameList, Collection< String > aliasList, List< SqlMonotonicity > columnMonotonicityList)
 

Protected Attributes

final SqlValidator validator
 
final RexBuilder rexBuilder
 
final Prepare.CatalogReader catalogReader
 
final RelOptCluster cluster
 
final Map< RelNode, Integer > leaves = new HashMap<>()
 
final RelDataTypeFactory typeFactory
 

Static Protected Attributes

static final Logger SQL2REL_LOGGER = CalciteTrace.getSqlToRelTracer()
 

Private Member Functions

void checkConvertedType (SqlNode query, RelNode result)
 
RelCollation requiredCollation (RelNode r)
 
void distinctify (Blackboard bb, boolean checkForDupExprs)
 
void convertWhere (final Blackboard bb, final SqlNode where)
 
void replaceSubQueries (final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
 
void substituteSubQuery (Blackboard bb, SubQuery subQuery)
 
RexNode translateIn (RelOptUtil.Logic logic, RelNode root, final RexNode rex)
 
boolean convertNonCorrelatedSubQuery (SubQuery subQuery, Blackboard bb, RelNode converted, boolean isExists)
 
RexNode convertInToOr (final Blackboard bb, final List< RexNode > leftKeys, SqlNodeList valuesList, SqlInOperator op)
 
RexNode ensureSqlType (RelDataType type, RexNode node)
 
RelOptUtil.Exists convertExists (SqlNode seek, RelOptUtil.SubQueryType subQueryType, RelOptUtil.Logic logic, boolean notIn, RelDataType targetDataType)
 
RelNode convertQueryOrInList (Blackboard bb, SqlNode seek, RelDataType targetRowType)
 
RelNode convertRowValues (Blackboard bb, SqlNode rowList, Collection< SqlNode > rows, boolean allowLiteralsOnly, RelDataType targetRowType)
 
RexLiteral convertLiteralInValuesList (SqlNode sqlNode, Blackboard bb, RelDataType rowType, int iField)
 
boolean isRowConstructor (SqlNode node)
 
void findSubQueries (Blackboard bb, SqlNode node, RelOptUtil.Logic logic, boolean registerOnlyScalarSubQueries)
 
RexNode convertOver (Blackboard bb, SqlNode node)
 
void convertUnnest (Blackboard bb, SqlCall call, List< String > fieldNames)
 
void convertIdentifier (Blackboard bb, SqlIdentifier id, SqlNodeList extendedColumns, SqlNodeList tableHints)
 
void convertTemporalTable (Blackboard bb, SqlCall call)
 
Set< RelColumnMapping > getColumnMappings (SqlOperator op)
 
CorrelationUse getCorrelationUse (Blackboard bb, final RelNode r0)
 
boolean isSubQueryNonCorrelated (RelNode subq, Blackboard bb)
 
RexNode convertJoinCondition (Blackboard bb, SqlValidatorNamespace leftNamespace, SqlValidatorNamespace rightNamespace, SqlNode condition, JoinConditionType conditionType, RelNode leftRel, RelNode rightRel)
 
RexNode convertUsing (SqlValidatorNamespace leftNamespace, SqlValidatorNamespace rightNamespace, List< String > nameList)
 
RelNode rewriteAggregateWithGroupId (Blackboard bb, AggregatingSelectScope.Resolved r, AggConverter converter)
 
boolean all (SqlCall call)
 
RelNode createModify (RelOptTable targetTable, RelNode source)
 
RelNode createSource (RelOptTable targetTable, RelNode source, ModifiableView modifiableView, RelDataType delegateRowType)
 
RelOptTable.ToRelContext createToRelContext (List< RelHint > hints)
 
Blackboard createInsertBlackboard (RelOptTable targetTable, RexNode sourceRef, List< String > targetColumnNames)
 
InitializerExpressionFactory getInitializerFactory (SqlValidatorTable validatorTable)
 
RexNode castNullLiteralIfNeeded (RexNode node, RelDataType type)
 
RelNode convertDelete (SqlDelete call)
 
RelNode convertUpdate (SqlUpdate call)
 
RelNode convertMerge (SqlMerge call)
 
RexNode convertIdentifier (Blackboard bb, SqlIdentifier identifier)
 
RelNode convertRowConstructor (Blackboard bb, SqlCall rowConstructor)
 
RelNode convertCursor (Blackboard bb, SubQuery subQuery)
 
RelNode convertMultisets (final List< SqlNode > operands, Blackboard bb)
 
void convertSelectList (Blackboard bb, SqlSelect select, List< SqlNode > orderList)
 
String deriveAlias (final SqlNode node, Collection< String > aliases, final int ordinal)
 
void convertValuesImpl (Blackboard bb, SqlCall values, RelDataType targetRowType)
 
SqlQuantifyOperator negate (SqlQuantifyOperator operator)
 

Static Private Member Functions

static boolean isStream (SqlNode query)
 
static boolean containsInOperator (SqlNode node)
 
static SqlNode pushDownNotForIn (SqlValidatorScope scope, SqlNode sqlNode)
 
static SqlNode reg (SqlValidatorScope scope, SqlNode e)
 
static boolean containsNullLiteral (SqlNodeList valueList)
 
static JoinRelType convertJoinType (JoinType joinType)
 
static boolean desc (RelFieldCollation.Direction direction)
 
static< T > T unwrap (Object o, Class< T > clazz)
 

Private Attributes

SubQueryConverter subQueryConverter
 
final List< SqlDynamicParam > dynamicParamSqlNodes = new ArrayList<>()
 
final SqlOperatorTable opTab
 
final SqlNodeToRexConverter exprConverter
 
final HintStrategyTable hintStrategies
 
int explainParamCount
 
final RelBuilder relBuilder
 
final Map< CorrelationId,
DeferredLookup
mapCorrelToDeferred = new HashMap<>()
 
final Deque< String > datasetStack = new ArrayDeque<>()
 
final Map< SqlNode, RexNode > mapConvertedNonCorrSubqs = new HashMap<>()
 

Detailed Description

Converts a SQL parse tree (consisting of org.apache.calcite.sql.SqlNode objects) into a relational algebra expression (consisting of org.apache.calcite.rel.RelNode objects).

The public entry points are: convertQuery, convertExpression(SqlNode).

Definition at line 232 of file SqlToRelConverter.java.

Constructor & Destructor Documentation

org.apache.calcite.sql2rel.SqlToRelConverter.SqlToRelConverter ( RelOptTable.ViewExpander  viewExpander,
SqlValidator  validator,
Prepare.CatalogReader  catalogReader,
RelOptPlanner  planner,
RexBuilder  rexBuilder,
SqlRexConvertletTable  convertletTable 
)
inline

Creates a converter.

Parameters
viewExpanderPreparing statement
validatorValidator
catalogReaderSchema
plannerPlanner
rexBuilderRex builder
convertletTableExpression converter

Definition at line 295 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader, org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder, org.apache.calcite.sql2rel.SqlToRelConverter.validator, and org.apache.calcite.sql2rel.SqlToRelConverter.viewExpander.

300  {
301  this(viewExpander,
302  validator,
304  RelOptCluster.create(planner, rexBuilder),
305  convertletTable,
306  Config.DEFAULT);
307  }
org.apache.calcite.sql2rel.SqlToRelConverter.SqlToRelConverter ( RelOptTable.ViewExpander  viewExpander,
SqlValidator  validator,
Prepare.CatalogReader  catalogReader,
RelOptCluster  cluster,
SqlRexConvertletTable  convertletTable 
)
inline
org.apache.calcite.sql2rel.SqlToRelConverter.SqlToRelConverter ( RelOptTable.ViewExpander  viewExpander,
SqlValidator  validator,
Prepare.CatalogReader  catalogReader,
RelOptCluster  cluster,
SqlRexConvertletTable  convertletTable,
Config  config 
)
inline

Definition at line 324 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader, org.apache.calcite.sql2rel.SqlToRelConverter.cluster, org.apache.calcite.sql2rel.SqlToRelConverter.config, File_Namespace.create(), org.apache.calcite.sql2rel.SqlToRelConverter.validator, and org.apache.calcite.sql2rel.SqlToRelConverter.viewExpander.

329  {
330  this.viewExpander = viewExpander;
331  this.opTab = (validator == null) ? SqlStdOperatorTable.instance()
332  : validator.getOperatorTable();
333  this.validator = validator;
334  this.catalogReader = catalogReader;
335  this.subQueryConverter = new NoOpSubQueryConverter();
336  this.rexBuilder = cluster.getRexBuilder();
337  this.typeFactory = rexBuilder.getTypeFactory();
338  this.exprConverter = new SqlNodeToRexConverterImpl(convertletTable);
339  this.explainParamCount = 0;
340  this.config = new ConfigBuilder().withConfig(config).build();
341  this.relBuilder = config.getRelBuilderFactory()
342  .create(cluster, null)
343  .transform(config.getRelBuilderConfigTransform());
344  this.hintStrategies = config.getHintStrategyTable();
345 
346  cluster.setHintStrategies(this.hintStrategies);
347  this.cluster = Objects.requireNonNull(cluster);
348  }
FILE * create(const std::string &basePath, const int fileId, const size_t pageSize, const size_t numPages)
Definition: File.cpp:49

+ Here is the call graph for this function:

Member Function Documentation

void org.apache.calcite.sql2rel.SqlToRelConverter.addConvertedNonCorrSubqs ( Map< SqlNode, RexNode >  alreadyConvertedNonCorrSubqs)
inline

Adds to the current map of non-correlated converted sub-queries the elements from another map that contains non-correlated sub-queries that have been converted by another SqlToRelConverter.

Parameters
alreadyConvertedNonCorrSubqsthe other map

Definition at line 418 of file SqlToRelConverter.java.

419  {
420  mapConvertedNonCorrSubqs.putAll(alreadyConvertedNonCorrSubqs);
421  }
RexNode org.apache.calcite.sql2rel.SqlToRelConverter.adjustInputRef ( Blackboard  bb,
RexInputRef  inputRef 
)
inlineprotected

Adjusts the type of a reference to an input field to account for nulls introduced by outer joins; and adjusts the offset to match the physical implementation.

Parameters
bbBlackboard
inputRefInput ref
Returns
Adjusted input ref

Definition at line 3825 of file SqlToRelConverter.java.

References field().

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

3825  {
3826  RelDataTypeField field = bb.getRootField(inputRef);
3827  if (field != null) {
3828  return rexBuilder.makeInputRef(field.getType(), inputRef.getIndex());
3829  }
3830  return inputRef;
3831  }
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.afterTableFunction ( SqlToRelConverter.Blackboard  bb,
SqlCall  call,
LogicalTableFunctionScan  callRel 
)
inlineprotected

Definition at line 2398 of file SqlToRelConverter.java.

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

2400  {}

+ Here is the caller graph for this function:

boolean org.apache.calcite.sql2rel.SqlToRelConverter.all ( SqlCall  call)
inlineprivate

Definition at line 3287 of file SqlToRelConverter.java.

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

3287  {
3288  return ((SqlSetOperator) call.getOperator()).isAll();
3289  }

+ Here is the caller graph for this function:

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.castNullLiteralIfNeeded ( RexNode  node,
RelDataType  type 
)
inlineprivate

Definition at line 3556 of file SqlToRelConverter.java.

References run_benchmark_import.type.

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

3556  {
3557  if (!RexLiteral.isNullLiteral(node)) {
3558  return node;
3559  }
3560  return rexBuilder.makeCast(type, node);
3561  }

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.checkConvertedType ( SqlNode  query,
RelNode  result 
)
inlineprivate

Definition at line 443 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(), and org.apache.calcite.sql2rel.SqlToRelConverter.decorrelate().

443  {
444  if (query.isA(SqlKind.DML)) {
445  return;
446  }
447  // Verify that conversion from SQL to relational algebra did
448  // not perturb any type information. (We can't do this if the
449  // SQL statement is something like an INSERT which has no
450  // validator type information associated with its result,
451  // hence the namespace check above.)
452  final List<RelDataTypeField> validatedFields =
453  validator.getValidatedNodeType(query).getFieldList();
454  final RelDataType validatedRowType =
455  validator.getTypeFactory().createStructType(Pair.right(validatedFields),
456  SqlValidatorUtil.uniquify(Pair.left(validatedFields),
457  catalogReader.nameMatcher().isCaseSensitive()));
458 
459  final List<RelDataTypeField> convertedFields =
460  result.getRowType().getFieldList().subList(0, validatedFields.size());
461  final RelDataType convertedRowType =
462  validator.getTypeFactory().createStructType(convertedFields);
463 
464  if (!RelOptUtil.equal("validated row type",
465  validatedRowType,
466  "converted row type",
467  convertedRowType,
468  Litmus.IGNORE)) {
469  throw new AssertionError("Conversion to relational algebra failed to "
470  + "preserve datatypes:\n"
471  + "validated type:\n" + validatedRowType.getFullTypeString()
472  + "\nconverted type:\n" + convertedRowType.getFullTypeString() + "\nrel:\n"
473  + RelOptUtil.toString(result));
474  }
475  }

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.collectInsertTargets ( SqlInsert  call,
final RexNode  sourceRef,
final List< String >  targetColumnNames,
List< RexNode >  columnExprs 
)
inlineprotected

Given an INSERT statement, collects the list of names to be populated and the expressions to put in them.

Parameters
callInsert statement
sourceRefExpression representing a row from the source relational expression
targetColumnNamesList of target column names, to be populated
columnExprsList of expressions, to be populated

Definition at line 3573 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader, org.apache.calcite.sql2rel.SqlToRelConverter.createInsertBlackboard(), f, field(), org.apache.calcite.sql2rel.SqlToRelConverter.getTargetTable(), i, Fragmenter_Namespace.indexOf(), org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

3576  {
3577  final RelOptTable targetTable = getTargetTable(call);
3578  final RelDataType tableRowType = targetTable.getRowType();
3579  SqlNodeList targetColumnList = call.getTargetColumnList();
3580  if (targetColumnList == null) {
3581  if (validator.config().sqlConformance().isInsertSubsetColumnsAllowed()) {
3582  final RelDataType targetRowType =
3583  typeFactory.createStructType(tableRowType.getFieldList().subList(
3584  0, sourceRef.getType().getFieldCount()));
3585  targetColumnNames.addAll(targetRowType.getFieldNames());
3586  } else {
3587  targetColumnNames.addAll(tableRowType.getFieldNames());
3588  }
3589  } else {
3590  for (int i = 0; i < targetColumnList.size(); i++) {
3591  SqlIdentifier id = (SqlIdentifier) targetColumnList.get(i);
3592  RelDataTypeField field = SqlValidatorUtil.getTargetField(
3593  tableRowType, typeFactory, id, catalogReader, targetTable);
3594  assert field != null : "column " + id.toString() + " not found";
3595  targetColumnNames.add(field.getName());
3596  }
3597  }
3598 
3599  final Blackboard bb =
3600  createInsertBlackboard(targetTable, sourceRef, targetColumnNames);
3601 
3602  // Next, assign expressions for generated columns.
3603  final List<ColumnStrategy> strategies = targetTable.getColumnStrategies();
3604  for (String columnName : targetColumnNames) {
3605  final int i = tableRowType.getFieldNames().indexOf(columnName);
3606  final RexNode expr;
3607  switch (strategies.get(i)) {
3608  case STORED:
3609  final InitializerExpressionFactory f =
3610  Util.first(targetTable.unwrap(InitializerExpressionFactory.class),
3611  NullInitializerExpressionFactory.INSTANCE);
3612  expr = f.newColumnDefaultValue(targetTable, i, bb);
3613  break;
3614  case VIRTUAL:
3615  expr = null;
3616  break;
3617  default:
3618  expr = bb.nameToNodeMap.get(columnName);
3619  }
3620  columnExprs.add(expr);
3621  }
3622 
3623  // Remove virtual columns from the list.
3624  for (int i = 0; i < targetColumnNames.size(); i++) {
3625  if (columnExprs.get(i) == null) {
3626  columnExprs.remove(i);
3627  targetColumnNames.remove(i);
3628  --i;
3629  }
3630  }
3631  }
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
size_t indexOf(std::vector< T > &vec, T val)
Blackboard createInsertBlackboard(RelOptTable targetTable, RexNode sourceRef, List< String > targetColumnNames)
char * f

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static boolean org.apache.calcite.sql2rel.SqlToRelConverter.containsInOperator ( SqlNode  node)
inlinestaticprivate

Returns whether a given node contains a SqlInOperator.

Parameters
nodea RexNode tree

Definition at line 854 of file SqlToRelConverter.java.

References Void.

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

854  {
855  try {
856  SqlVisitor<Void> visitor = new SqlBasicVisitor<Void>() {
857  public Void visit(SqlCall call) {
858  if (call.getOperator() instanceof SqlInOperator) {
859  throw new Util.FoundOne(call);
860  }
861  return super.visit(call);
862  }
863  };
864  node.accept(visitor);
865  return false;
866  } catch (Util.FoundOne e) {
867  Util.swallow(e, null);
868  return true;
869  }
870  }

+ Here is the caller graph for this function:

static boolean org.apache.calcite.sql2rel.SqlToRelConverter.containsNullLiteral ( SqlNodeList  valueList)
inlinestaticprivate

Definition at line 1339 of file SqlToRelConverter.java.

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

1339  {
1340  for (SqlNode node : valueList.getList()) {
1341  if (node instanceof SqlLiteral) {
1342  SqlLiteral lit = (SqlLiteral) node;
1343  if (lit.getValue() == null) {
1344  return true;
1345  }
1346  }
1347  }
1348  return false;
1349  }

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertAgg ( Blackboard  bb,
SqlSelect  select,
List< SqlNode >  orderExprList 
)
inlineprotected

Converts the SELECT, GROUP BY and HAVING clauses of an aggregate query.

This method extracts SELECT, GROUP BY and HAVING clauses, and creates an AggConverter, then delegates to createAggImpl. Derived class may override this method to change any of those clauses or specify a different AggConverter.

Parameters
bbScope within which to resolve identifiers
selectQuery
orderExprListAdditional expressions needed to implement ORDER BY

Definition at line 2765 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.createAggImpl().

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

2766  {
2767  assert bb.root != null : "precondition: child != null";
2768  SqlNodeList groupList = select.getGroup();
2769  SqlNodeList selectList = select.getSelectList();
2770  SqlNode having = select.getHaving();
2771 
2772  final AggConverter aggConverter = new AggConverter(bb, select);
2773  createAggImpl(bb, aggConverter, selectList, groupList, having, orderExprList);
2774  }
final void createAggImpl(Blackboard bb, final AggConverter aggConverter, SqlNodeList selectList, SqlNodeList groupList, SqlNode having, List< SqlNode > orderExprList)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertCollectionTable ( Blackboard  bb,
SqlCall  call 
)
inlineprotected

Definition at line 2347 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.afterTableFunction(), org.apache.calcite.sql2rel.SqlToRelConverter.cluster, org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(), org.apache.calcite.sql2rel.SqlToRelConverter.getColumnMappings(), org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries(), org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.scope, test_readcsv.table, org.apache.calcite.sql2rel.SqlToRelConverter.toRel(), and org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory.

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

2347  {
2348  final SqlOperator operator = call.getOperator();
2349  if (operator == SqlStdOperatorTable.TABLESAMPLE) {
2350  final String sampleName =
2351  SqlLiteral.unchain(call.operand(0)).getValueAs(String.class);
2352  datasetStack.push(sampleName);
2353  SqlCall cursorCall = call.operand(1);
2354  SqlNode query = cursorCall.operand(0);
2355  RelNode converted = convertQuery(query, false, false).rel;
2356  bb.setRoot(converted, false);
2357  datasetStack.pop();
2358  return;
2359  }
2360  replaceSubQueries(bb, call, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2361 
2362  // Expand table macro if possible. It's more efficient than
2363  // LogicalTableFunctionScan.
2364  final SqlCallBinding callBinding =
2365  new SqlCallBinding(bb.scope.getValidator(), bb.scope, call);
2366  if (operator instanceof SqlUserDefinedTableMacro) {
2367  final SqlUserDefinedTableMacro udf = (SqlUserDefinedTableMacro) operator;
2368  final TranslatableTable table = udf.getTable(callBinding);
2369  final RelDataType rowType = table.getRowType(typeFactory);
2370  RelOptTable relOptTable =
2371  RelOptTableImpl.create(null, rowType, table, udf.getNameAsId().names);
2372  RelNode converted = toRel(relOptTable, ImmutableList.of());
2373  bb.setRoot(converted, true);
2374  return;
2375  }
2376 
2377  Type elementType;
2378  if (operator instanceof SqlUserDefinedTableFunction) {
2379  SqlUserDefinedTableFunction udtf = (SqlUserDefinedTableFunction) operator;
2380  elementType = udtf.getElementType(callBinding);
2381  } else {
2382  elementType = null;
2383  }
2384 
2385  RexNode rexCall = bb.convertExpression(call);
2386  final List<RelNode> inputs = bb.retrieveCursors();
2387  Set<RelColumnMapping> columnMappings = getColumnMappings(operator);
2388  LogicalTableFunctionScan callRel = LogicalTableFunctionScan.create(cluster,
2389  inputs,
2390  rexCall,
2391  elementType,
2392  validator.getValidatedNodeType(call),
2393  columnMappings);
2394  bb.setRoot(callRel, true);
2395  afterTableFunction(bb, call, callRel);
2396  }
void afterTableFunction(SqlToRelConverter.Blackboard bb, SqlCall call, LogicalTableFunctionScan callRel)
RelNode toRel(final RelOptTable table, final List< RelHint > hints)
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
Set< RelColumnMapping > getColumnMappings(SqlOperator op)
RelRoot convertQuery(SqlNode query, final boolean needsValidation, final boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertColumnList ( final SqlInsert  call,
RelNode  source 
)
inlineprotected

Creates a source for an INSERT statement.

If the column list is not specified, source expressions match target columns in order.

If the column list is specified, Source expressions are mapped to target columns by name via targetColumnList, and may not cover the entire target table. So, we'll make up a full row, using a combination of default values and the source expressions provided.

Parameters
callInsert expression
sourceSource relational expression
Returns
Converted INSERT statement

Definition at line 3460 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.castNullLiteralIfNeeded(), org.apache.calcite.sql2rel.SqlToRelConverter.collectInsertTargets(), org.apache.calcite.sql2rel.SqlToRelConverter.createInsertBlackboard(), DEFAULT, field(), org.apache.calcite.sql2rel.SqlToRelConverter.getInitializerFactory(), org.apache.calcite.sql2rel.SqlToRelConverter.getTargetTable(), i, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

3460  {
3461  RelDataType sourceRowType = source.getRowType();
3462  final RexNode sourceRef = rexBuilder.makeRangeReference(sourceRowType, 0, false);
3463  final List<String> targetColumnNames = new ArrayList<>();
3464  final List<RexNode> columnExprs = new ArrayList<>();
3465  collectInsertTargets(call, sourceRef, targetColumnNames, columnExprs);
3466 
3467  final RelOptTable targetTable = getTargetTable(call);
3468  final RelDataType targetRowType = RelOptTableImpl.realRowType(targetTable);
3469  final List<RelDataTypeField> targetFields = targetRowType.getFieldList();
3470  final List<RexNode> sourceExps =
3471  new ArrayList<>(Collections.nCopies(targetFields.size(), null));
3472  final List<String> fieldNames =
3473  new ArrayList<>(Collections.nCopies(targetFields.size(), null));
3474 
3475  final InitializerExpressionFactory initializerFactory =
3476  getInitializerFactory(validator.getNamespace(call).getTable());
3477 
3478  // Walk the name list and place the associated value in the
3479  // expression list according to the ordinal value returned from
3480  // the table construct, leaving nulls in the list for columns
3481  // that are not referenced.
3482  final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
3483  for (Pair<String, RexNode> p : Pair.zip(targetColumnNames, columnExprs)) {
3484  RelDataTypeField field = nameMatcher.field(targetRowType, p.left);
3485  assert field != null : "column " + p.left + " not found";
3486  sourceExps.set(field.getIndex(), p.right);
3487  }
3488 
3489  // Lazily create a blackboard that contains all non-generated columns.
3490  final Supplier<Blackboard> bb =
3491  () -> createInsertBlackboard(targetTable, sourceRef, targetColumnNames);
3492 
3493  // Walk the expression list and get default values for any columns
3494  // that were not supplied in the statement. Get field names too.
3495  for (int i = 0; i < targetFields.size(); ++i) {
3496  final RelDataTypeField field = targetFields.get(i);
3497  final String fieldName = field.getName();
3498  fieldNames.set(i, fieldName);
3499  if (sourceExps.get(i) == null || sourceExps.get(i).getKind() == SqlKind.DEFAULT) {
3500  sourceExps.set(
3501  i, initializerFactory.newColumnDefaultValue(targetTable, i, bb.get()));
3502 
3503  // bare nulls are dangerous in the wrong hands
3504  sourceExps.set(i, castNullLiteralIfNeeded(sourceExps.get(i), field.getType()));
3505  }
3506  }
3507 
3508  return relBuilder.push(source).projectNamed(sourceExps, fieldNames, false).build();
3509  }
RexNode castNullLiteralIfNeeded(RexNode node, RelDataType type)
InitializerExpressionFactory getInitializerFactory(SqlValidatorTable validatorTable)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
void collectInsertTargets(SqlInsert call, final RexNode sourceRef, final List< String > targetColumnNames, List< RexNode > columnExprs)
Blackboard createInsertBlackboard(RelOptTable targetTable, RexNode sourceRef, List< String > targetColumnNames)
#define DEFAULT

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertCursor ( Blackboard  bb,
SubQuery  subQuery 
)
inlineprivate

Definition at line 3846 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery().

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

3846  {
3847  final SqlCall cursorCall = (SqlCall) subQuery.node;
3848  assert cursorCall.operandCount() == 1;
3849  SqlNode query = cursorCall.operand(0);
3850  RelNode converted = convertQuery(query, false, false).rel;
3851  int iCursor = bb.cursors.size();
3852  bb.cursors.add(converted);
3853  subQuery.expr = new RexInputRef(iCursor, converted.getRowType());
3854  return converted;
3855  }
RelRoot convertQuery(SqlNode query, final boolean needsValidation, final boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertDelete ( SqlDelete  call)
inlineprivate

Definition at line 3633 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader, org.apache.calcite.sql2rel.SqlToRelConverter.convertSelect(), DELETE, and org.apache.calcite.sql2rel.SqlToRelConverter.getTargetTable().

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

3633  {
3634  RelOptTable targetTable = getTargetTable(call);
3635  RelNode sourceRel = convertSelect(call.getSourceSelect(), false);
3636  return LogicalTableModify.create(targetTable,
3637  catalogReader,
3638  sourceRel,
3640  null,
3641  null,
3642  false);
3643  }
#define DELETE
RelNode convertSelect(SqlSelect select, boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RexDynamicParam org.apache.calcite.sql2rel.SqlToRelConverter.convertDynamicParam ( final SqlDynamicParam  dynamicParam)
inline

Definition at line 3073 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.getDynamicParamType().

3073  {
3074  // REVIEW jvs 8-Jan-2005: dynamic params may be encountered out of
3075  // order. Should probably cross-check with the count from the parser
3076  // at the end and make sure they all got filled in. Why doesn't List
3077  // have a resize() method?!? Make this a utility.
3078  while (dynamicParam.getIndex() >= dynamicParamSqlNodes.size()) {
3079  dynamicParamSqlNodes.add(null);
3080  }
3081 
3082  dynamicParamSqlNodes.set(dynamicParam.getIndex(), dynamicParam);
3083  return rexBuilder.makeDynamicParam(
3084  getDynamicParamType(dynamicParam.getIndex()), dynamicParam.getIndex());
3085  }

+ Here is the call graph for this function:

RelOptUtil.Exists org.apache.calcite.sql2rel.SqlToRelConverter.convertExists ( SqlNode  seek,
RelOptUtil.SubQueryType  subQueryType,
RelOptUtil.Logic  logic,
boolean  notIn,
RelDataType  targetDataType 
)
inlineprivate

Converts an EXISTS or IN predicate into a join. For EXISTS, the sub-query produces an indicator variable, and the result is a relational expression which outer joins that indicator to the original query. After performing the outer join, the condition will be TRUE if the EXISTS condition holds, NULL otherwise.

Parameters
seekA query, for example 'select * from emp' or 'values (1,2,3)' or '('Foo', 34)'.
subQueryTypeWhether sub-query is IN, EXISTS or scalar
logicWhether the answer needs to be in full 3-valued logic (TRUE, FALSE, UNKNOWN) will be required, or whether we can accept an approximation (say representing UNKNOWN as FALSE)
notInWhether the operation is NOT IN
Returns
join expression

Definition at line 1533 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryOrInList(), org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard(), org.apache.calcite.sql2rel.SqlToRelConverter.relBuilder, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

1537  {
1538  final SqlValidatorScope seekScope = (seek instanceof SqlSelect)
1539  ? validator.getSelectScope((SqlSelect) seek)
1540  : null;
1541  final Blackboard seekBb = createBlackboard(seekScope, null, false);
1542  RelNode seekRel = convertQueryOrInList(seekBb, seek, targetDataType);
1543 
1544  return RelOptUtil.createExistsPlan(seekRel, subQueryType, logic, notIn, relBuilder);
1545  }
RelNode convertQueryOrInList(Blackboard bb, SqlNode seek, RelDataType targetRowType)
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.convertExpression ( SqlNode  node)
inline

Converts an expression from SqlNode to RexNode format.

Parameters
nodeExpression to translate
Returns
Converted expression

Definition at line 1800 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard(), and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

1800  {
1801  Map<String, RelDataType> nameToTypeMap = Collections.emptyMap();
1802  final ParameterScope scope =
1803  new ParameterScope((SqlValidatorImpl) validator, nameToTypeMap);
1804  final Blackboard bb = createBlackboard(scope, null, false);
1805  return bb.convertExpression(node);
1806  }
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.convertExpression ( SqlNode  node,
Map< String, RexNode >  nameToNodeMap 
)
inline

Converts an expression from SqlNode to RexNode format, mapping identifier references to predefined expressions.

Parameters
nodeExpression to translate
nameToNodeMapmap from String to RexNode; when an SqlIdentifier is encountered, it is used as a key and translated to the corresponding value from this map
Returns
Converted expression

Definition at line 1819 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard(), and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

1819  {
1820  final Map<String, RelDataType> nameToTypeMap = new HashMap<>();
1821  for (Map.Entry<String, RexNode> entry : nameToNodeMap.entrySet()) {
1822  nameToTypeMap.put(entry.getKey(), entry.getValue().getType());
1823  }
1824  final ParameterScope scope =
1825  new ParameterScope((SqlValidatorImpl) validator, nameToTypeMap);
1826  final Blackboard bb = createBlackboard(scope, nameToNodeMap, false);
1827  return bb.convertExpression(node);
1828  }
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)

+ Here is the call graph for this function:

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.convertExtendedExpression ( SqlNode  node,
Blackboard  bb 
)
inlineprotected

Converts a non-standard expression.

This method is an extension-point that derived classes can override. If this method returns a null result, the normal expression translation process will proceed. The default implementation always returns null.

Parameters
nodeExpression
bbBlackboard
Returns
null to proceed with the usual expression translation process

Definition at line 1842 of file SqlToRelConverter.java.

1842  {
1843  return null;
1844  }
void org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom ( Blackboard  bb,
SqlNode  from 
)
inlineprotected

Definition at line 1938 of file SqlToRelConverter.java.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(), org.apache.calcite.sql2rel.SqlToRelConverter.convertIdentifier(), org.apache.calcite.sql2rel.SqlToRelConverter.convertMatchRecognize(), org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectImpl(), and org.apache.calcite.sql2rel.SqlToRelConverter.convertTemporalTable().

1938  {
1939  convertFrom(bb, from, Collections.emptyList());
1940  }
void convertFrom(Blackboard bb, SqlNode from)

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom ( Blackboard  bb,
SqlNode  from,
List< String >  fieldNames 
)
inlineprotected

Converts a FROM clause into a relational expression.

Parameters
bbScope within which to resolve identifiers
fromFROM clause of a query. Examples include:
              <ul>
              <li>a single table ("SALES.EMP"),
              <li>an aliased table ("EMP AS E"),
              <li>a list of tables ("EMP, DEPT"),
              <li>an ANSI Join expression ("EMP JOIN DEPT ON EMP.DEPTNO =
              DEPT.DEPTNO"),
              <li>a VALUES clause ("VALUES ('Fred', 20)"),
              <li>a query ("(SELECT * FROM EMP WHERE GENDER = 'F')"),
              <li>or any combination of the above.
              </ul>
fieldNamesField aliases, usually come from AS clause

Definition at line 1960 of file SqlToRelConverter.java.

References AS, org.apache.calcite.sql2rel.SqlToRelConverter.cluster, org.apache.calcite.sql2rel.SqlToRelConverter.convertCollectionTable(), org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(), org.apache.calcite.sql2rel.SqlToRelConverter.convertIdentifier(), org.apache.calcite.sql2rel.SqlToRelConverter.convertJoinCondition(), org.apache.calcite.sql2rel.SqlToRelConverter.convertJoinType(), org.apache.calcite.sql2rel.SqlToRelConverter.convertMatchRecognize(), org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(), org.apache.calcite.sql2rel.SqlToRelConverter.convertTemporalTable(), org.apache.calcite.sql2rel.SqlToRelConverter.convertUnnest(), org.apache.calcite.sql2rel.SqlToRelConverter.convertUsing(), org.apache.calcite.sql2rel.SqlToRelConverter.convertValuesImpl(), org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard(), org.apache.calcite.sql2rel.SqlToRelConverter.createJoin(), join(), setup.root, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.root, SELECT, UNION, VALUES, and WITH.

1960  {
1961  if (from == null) {
1962  bb.setRoot(LogicalValues.createOneRow(cluster), false);
1963  return;
1964  }
1965 
1966  final SqlCall call;
1967  final SqlNode[] operands;
1968  switch (from.getKind()) {
1969  case MATCH_RECOGNIZE:
1970  convertMatchRecognize(bb, (SqlCall) from);
1971  return;
1972 
1973  case AS:
1974  call = (SqlCall) from;
1975  SqlNode firstOperand = call.operand(0);
1976  final List<String> fieldNameList = new ArrayList<>();
1977  if (call.operandCount() > 2) {
1978  for (SqlNode node : Util.skip(call.getOperandList(), 2)) {
1979  fieldNameList.add(((SqlIdentifier) node).getSimple());
1980  }
1981  }
1982  convertFrom(bb, firstOperand, fieldNameList);
1983  return;
1984 
1985  case WITH_ITEM:
1986  convertFrom(bb, ((SqlWithItem) from).query);
1987  return;
1988 
1989  case WITH:
1990  convertFrom(bb, ((SqlWith) from).body);
1991  return;
1992 
1993  case TABLESAMPLE:
1994  operands = ((SqlBasicCall) from).getOperands();
1995  SqlSampleSpec sampleSpec = SqlLiteral.sampleValue(operands[1]);
1996  if (sampleSpec instanceof SqlSampleSpec.SqlSubstitutionSampleSpec) {
1997  String sampleName =
1998  ((SqlSampleSpec.SqlSubstitutionSampleSpec) sampleSpec).getName();
1999  datasetStack.push(sampleName);
2000  convertFrom(bb, operands[0]);
2001  datasetStack.pop();
2002  } else if (sampleSpec instanceof SqlSampleSpec.SqlTableSampleSpec) {
2003  SqlSampleSpec.SqlTableSampleSpec tableSampleSpec =
2004  (SqlSampleSpec.SqlTableSampleSpec) sampleSpec;
2005  convertFrom(bb, operands[0]);
2006  RelOptSamplingParameters params =
2007  new RelOptSamplingParameters(tableSampleSpec.isBernoulli(),
2008  tableSampleSpec.getSamplePercentage(),
2009  tableSampleSpec.isRepeatable(),
2010  tableSampleSpec.getRepeatableSeed());
2011  bb.setRoot(new Sample(cluster, bb.root, params), false);
2012  } else {
2013  throw new AssertionError("unknown TABLESAMPLE type: " + sampleSpec);
2014  }
2015  return;
2016 
2017  case TABLE_REF:
2018  call = (SqlCall) from;
2019  convertIdentifier(bb, call.operand(0), null, call.operand(1));
2020  return;
2021 
2022  case IDENTIFIER:
2023  convertIdentifier(bb, (SqlIdentifier) from, null, null);
2024  return;
2025 
2026  case EXTEND:
2027  call = (SqlCall) from;
2028  final SqlNode operand0 = call.getOperandList().get(0);
2029  final SqlIdentifier id = operand0.getKind() == SqlKind.TABLE_REF
2030  ? ((SqlCall) operand0).operand(0)
2031  : (SqlIdentifier) operand0;
2032  SqlNodeList extendedColumns = (SqlNodeList) call.getOperandList().get(1);
2033  convertIdentifier(bb, id, extendedColumns, null);
2034  return;
2035 
2036  case SNAPSHOT:
2037  convertTemporalTable(bb, (SqlCall) from);
2038  return;
2039 
2040  case JOIN:
2041  final SqlJoin join = (SqlJoin) from;
2042  final SqlValidatorScope scope = validator.getJoinScope(from);
2043  final Blackboard fromBlackboard = createBlackboard(scope, null, false);
2044  SqlNode left = join.getLeft();
2045  SqlNode right = join.getRight();
2046  final boolean isNatural = join.isNatural();
2047  final JoinType joinType = join.getJoinType();
2048  final SqlValidatorScope leftScope = Util.first(
2049  validator.getJoinScope(left), ((DelegatingScope) bb.scope).getParent());
2050  final Blackboard leftBlackboard = createBlackboard(leftScope, null, false);
2051  final SqlValidatorScope rightScope = Util.first(
2052  validator.getJoinScope(right), ((DelegatingScope) bb.scope).getParent());
2053  final Blackboard rightBlackboard = createBlackboard(rightScope, null, false);
2054  convertFrom(leftBlackboard, left);
2055  RelNode leftRel = leftBlackboard.root;
2056  convertFrom(rightBlackboard, right);
2057  RelNode rightRel = rightBlackboard.root;
2058  JoinRelType convertedJoinType = convertJoinType(joinType);
2059  RexNode conditionExp;
2060  final SqlValidatorNamespace leftNamespace = validator.getNamespace(left);
2061  final SqlValidatorNamespace rightNamespace = validator.getNamespace(right);
2062  if (isNatural) {
2063  final RelDataType leftRowType = leftNamespace.getRowType();
2064  final RelDataType rightRowType = rightNamespace.getRowType();
2065  final List<String> columnList = SqlValidatorUtil.deriveNaturalJoinColumnList(
2066  catalogReader.nameMatcher(), leftRowType, rightRowType);
2067  conditionExp = convertUsing(leftNamespace, rightNamespace, columnList);
2068  } else {
2069  conditionExp = convertJoinCondition(fromBlackboard,
2070  leftNamespace,
2071  rightNamespace,
2072  join.getCondition(),
2073  join.getConditionType(),
2074  leftRel,
2075  rightRel);
2076  }
2077 
2078  final RelNode joinRel = createJoin(
2079  fromBlackboard, leftRel, rightRel, conditionExp, convertedJoinType);
2080  bb.setRoot(joinRel, false);
2081  return;
2082 
2083  case SELECT:
2084  case INTERSECT:
2085  case EXCEPT:
2086  case UNION:
2087  final RelNode rel = convertQueryRecursive(from, false, null).project();
2088  bb.setRoot(rel, true);
2089  return;
2090 
2091  case VALUES:
2092  convertValuesImpl(bb, (SqlCall) from, null);
2093  if (fieldNames.size() > 0) {
2094  bb.setRoot(relBuilder.push(bb.root).rename(fieldNames).build(), true);
2095  }
2096  return;
2097 
2098  case UNNEST:
2099  convertUnnest(bb, (SqlCall) from, fieldNames);
2100  return;
2101 
2102  case COLLECTION_TABLE:
2103  call = (SqlCall) from;
2104 
2105  // Dig out real call; TABLE() wrapper is just syntactic.
2106  assert call.getOperandList().size() == 1;
2107  final SqlCall call2 = call.operand(0);
2108  convertCollectionTable(bb, call2);
2109  return;
2110 
2111  default:
2112  throw new AssertionError("not a join operator " + from);
2113  }
2114  }
#define VALUES
JoinType
Definition: sqldefs.h:108
void convertCollectionTable(Blackboard bb, SqlCall call)
std::string join(T const &container, std::string const &delim)
tuple root
Definition: setup.in.py:14
void convertValuesImpl(Blackboard bb, SqlCall values, RelDataType targetRowType)
void convertFrom(Blackboard bb, SqlNode from)
RelRoot convertQueryRecursive(SqlNode query, boolean top, RelDataType targetRowType)
void convertUnnest(Blackboard bb, SqlCall call, List< String > fieldNames)
#define WITH
RexNode convertUsing(SqlValidatorNamespace leftNamespace, SqlValidatorNamespace rightNamespace, List< String > nameList)
RexNode convertJoinCondition(Blackboard bb, SqlValidatorNamespace leftNamespace, SqlValidatorNamespace rightNamespace, SqlNode condition, JoinConditionType conditionType, RelNode leftRel, RelNode rightRel)
void convertIdentifier(Blackboard bb, SqlIdentifier id, SqlNodeList extendedColumns, SqlNodeList tableHints)
void convertMatchRecognize(Blackboard bb, SqlCall call)
bool g_enable_watchdog false
Definition: Execute.cpp:76
RelNode createJoin(Blackboard bb, RelNode leftRel, RelNode rightRel, RexNode joinCond, JoinRelType joinType)
void convertTemporalTable(Blackboard bb, SqlCall call)
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)
static JoinRelType convertJoinType(JoinType joinType)
if(yyssp >=yyss+yystacksize-1)
#define AS
#define SELECT

+ Here is the call graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertIdentifier ( Blackboard  bb,
SqlIdentifier  id,
SqlNodeList  extendedColumns,
SqlNodeList  tableHints 
)
inlineprivate

Definition at line 2314 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader, org.apache.calcite.sql2rel.SqlToRelConverter.cluster, org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(), org.apache.calcite.sql2rel.SqlToRelConverter.datasetStack, org.apache.calcite.sql2rel.SqlToRelConverter.hintStrategies, test_readcsv.table, org.apache.calcite.sql2rel.SqlToRelConverter.toRel(), and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

2317  {
2318  final SqlValidatorNamespace fromNamespace = validator.getNamespace(id).resolve();
2319  if (fromNamespace.getNode() != null) {
2320  convertFrom(bb, fromNamespace.getNode());
2321  return;
2322  }
2323  final String datasetName = datasetStack.isEmpty() ? null : datasetStack.peek();
2324  final boolean[] usedDataset = {false};
2325  RelOptTable table = SqlValidatorUtil.getRelOptTable(
2326  fromNamespace, catalogReader, datasetName, usedDataset);
2327  if (extendedColumns != null && extendedColumns.size() > 0) {
2328  assert table != null;
2329  final SqlValidatorTable validatorTable = table.unwrap(SqlValidatorTable.class);
2330  final List<RelDataTypeField> extendedFields = SqlValidatorUtil.getExtendedColumns(
2331  validator, validatorTable, extendedColumns);
2332  table = table.extend(extendedFields);
2333  }
2334  final RelNode tableRel;
2335  // Review Danny 2020-01-13: hacky to construct a new table scan
2336  // in order to apply the hint strategies.
2337  final List<RelHint> hints =
2338  hintStrategies.apply(SqlUtil.getRelHint(hintStrategies, tableHints),
2339  LogicalTableScan.create(cluster, table, ImmutableList.of()));
2340  tableRel = toRel(table, hints);
2341  bb.setRoot(tableRel, true);
2342  if (usedDataset[0]) {
2343  bb.setDataset(datasetName);
2344  }
2345  }
RelNode toRel(final RelOptTable table, final List< RelHint > hints)
void convertFrom(Blackboard bb, SqlNode from)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.convertIdentifier ( Blackboard  bb,
SqlIdentifier  identifier 
)
inlineprivate

Converts an identifier into an expression in a given scope. For example, the "empno" in "select empno from emp join dept" becomes "emp.empno".

Definition at line 3766 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.adjustInputRef(), i, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.isPatternVarRef, setup.name, and org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.scope.

3766  {
3767  // first check for reserved identifiers like CURRENT_USER
3768  final SqlCall call = bb.getValidator().makeNullaryCall(identifier);
3769  if (call != null) {
3770  return bb.convertExpression(call);
3771  }
3772 
3773  String pv = null;
3774  if (bb.isPatternVarRef && identifier.names.size() > 1) {
3775  pv = identifier.names.get(0);
3776  }
3777 
3778  final SqlQualified qualified;
3779  if (bb.scope != null) {
3780  qualified = bb.scope.fullyQualify(identifier);
3781  } else {
3782  qualified = SqlQualified.create(null, 1, null, identifier);
3783  }
3784  final Pair<RexNode, Map<String, Integer>> e0 = bb.lookupExp(qualified);
3785  RexNode e = e0.left;
3786  for (String name : qualified.suffix()) {
3787  if (e == e0.left && e0.right != null) {
3788  int i = e0.right.get(name);
3789  e = rexBuilder.makeFieldAccess(e, i);
3790  } else {
3791  final boolean caseSensitive = true; // name already fully-qualified
3792  if (identifier.isStar() && bb.scope instanceof MatchRecognizeScope) {
3793  e = rexBuilder.makeFieldAccess(e, 0);
3794  } else {
3795  e = rexBuilder.makeFieldAccess(e, name, caseSensitive);
3796  }
3797  }
3798  }
3799  if (e instanceof RexInputRef) {
3800  // adjust the type to account for nulls introduced by outer joins
3801  e = adjustInputRef(bb, (RexInputRef) e);
3802  if (pv != null) {
3803  e = RexPatternFieldRef.of(pv, (RexInputRef) e);
3804  }
3805  }
3806 
3807  if (e0.left instanceof RexCorrelVariable) {
3808  assert e instanceof RexFieldAccess;
3809  final RexNode prev = bb.mapCorrelateToRex.put(
3810  ((RexCorrelVariable) e0.left).id, (RexFieldAccess) e);
3811  assert prev == null;
3812  }
3813  return e;
3814  }
string name
Definition: setup.in.py:72
RexNode adjustInputRef(Blackboard bb, RexInputRef inputRef)

+ Here is the call graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertInsert ( SqlInsert  call)
inlineprotected

Definition at line 3291 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertColumnList(), org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(), org.apache.calcite.sql2rel.SqlToRelConverter.createModify(), and org.apache.calcite.sql2rel.SqlToRelConverter.getTargetTable().

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertMerge(), and org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive().

3291  {
3292  RelOptTable targetTable = getTargetTable(call);
3293 
3294  final RelDataType targetRowType = validator.getValidatedNodeType(call);
3295  assert targetRowType != null;
3296  RelNode sourceRel =
3297  convertQueryRecursive(call.getSource(), true, targetRowType).project();
3298  RelNode massagedRel = convertColumnList(call, sourceRel);
3299 
3300  return createModify(targetTable, massagedRel);
3301  }
RelRoot convertQueryRecursive(SqlNode query, boolean top, RelDataType targetRowType)
RelNode createModify(RelOptTable targetTable, RelNode source)
RelNode convertColumnList(final SqlInsert call, RelNode source)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.convertInToOr ( final Blackboard  bb,
final List< RexNode >  leftKeys,
SqlNodeList  valuesList,
SqlInOperator  op 
)
inlineprivate

Converts "x IN (1, 2, ...)" to "x=1 OR x=2 OR ...".

Parameters
leftKeysLHS
valuesListRHS
opThe operator (IN, NOT IN, > SOME, ...)
Returns
converted expression

Definition at line 1439 of file SqlToRelConverter.java.

References ALL, org.apache.calcite.sql2rel.SqlToRelConverter.ensureSqlType(), IN, NOT, org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder, and SOME.

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

1442  {
1443  final List<RexNode> comparisons = new ArrayList<>();
1444  for (SqlNode rightVals : valuesList) {
1445  RexNode rexComparison;
1446  final SqlOperator comparisonOp;
1447  if (op instanceof SqlQuantifyOperator) {
1448  comparisonOp = RelOptUtil.op(
1449  ((SqlQuantifyOperator) op).comparisonKind, SqlStdOperatorTable.EQUALS);
1450  } else {
1451  comparisonOp = SqlStdOperatorTable.EQUALS;
1452  }
1453  if (leftKeys.size() == 1) {
1454  rexComparison = rexBuilder.makeCall(comparisonOp,
1455  leftKeys.get(0),
1456  ensureSqlType(
1457  leftKeys.get(0).getType(), bb.convertExpression(rightVals)));
1458  } else {
1459  assert rightVals instanceof SqlCall;
1460  final SqlBasicCall call = (SqlBasicCall) rightVals;
1461  assert (call.getOperator() instanceof SqlRowOperator)
1462  && call.operandCount() == leftKeys.size();
1463  rexComparison = RexUtil.composeConjunction(rexBuilder,
1464  Iterables.transform(Pair.zip(leftKeys, call.getOperandList()),
1465  pair
1466  -> rexBuilder.makeCall(comparisonOp,
1467  pair.left,
1468  ensureSqlType(pair.left.getType(),
1469  bb.convertExpression(pair.right)))));
1470  }
1471  comparisons.add(rexComparison);
1472  }
1473 
1474  switch (op.kind) {
1475  case ALL:
1476  return RexUtil.composeConjunction(rexBuilder, comparisons, true);
1477  case NOT_IN:
1478  return rexBuilder.makeCall(SqlStdOperatorTable.NOT,
1479  RexUtil.composeDisjunction(rexBuilder, comparisons, true));
1480  case IN:
1481  case SOME:
1482  return RexUtil.composeDisjunction(rexBuilder, comparisons, true);
1483  default:
1484  throw new AssertionError();
1485  }
1486  }
#define NOT
RexNode ensureSqlType(RelDataType type, RexNode node)
#define IN
#define SOME
#define ALL

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.convertJoinCondition ( Blackboard  bb,
SqlValidatorNamespace  leftNamespace,
SqlValidatorNamespace  rightNamespace,
SqlNode  condition,
JoinConditionType  conditionType,
RelNode  leftRel,
RelNode  rightRel 
)
inlineprivate

Definition at line 2675 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertUsing(), setup.name, ON, and org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries().

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

2681  {
2682  if (condition == null) {
2683  return rexBuilder.makeLiteral(true);
2684  }
2685  bb.setRoot(ImmutableList.of(leftRel, rightRel));
2686  replaceSubQueries(bb, condition, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
2687  switch (conditionType) {
2688  case ON:
2689  bb.setRoot(ImmutableList.of(leftRel, rightRel));
2690  return bb.convertExpression(condition);
2691  case USING:
2692  final SqlNodeList list = (SqlNodeList) condition;
2693  final List<String> nameList = new ArrayList<>();
2694  for (SqlNode columnName : list) {
2695  final SqlIdentifier id = (SqlIdentifier) columnName;
2696  String name = id.getSimple();
2697  nameList.add(name);
2698  }
2699  return convertUsing(leftNamespace, rightNamespace, nameList);
2700  default:
2701  throw Util.unexpected(conditionType);
2702  }
2703  }
string name
Definition: setup.in.py:72
RexNode convertUsing(SqlValidatorNamespace leftNamespace, SqlValidatorNamespace rightNamespace, List< String > nameList)
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
#define ON

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static JoinRelType org.apache.calcite.sql2rel.SqlToRelConverter.convertJoinType ( JoinType  joinType)
inlinestaticprivate

Definition at line 2735 of file SqlToRelConverter.java.

References INNER, and LEFT.

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

2735  {
2736  switch (joinType) {
2737  case COMMA:
2738  case INNER:
2739  case CROSS:
2740  return JoinRelType.INNER;
2741  case FULL:
2742  return JoinRelType.FULL;
2743  case LEFT:
2744  return JoinRelType.LEFT;
2745  case RIGHT:
2746  return JoinRelType.RIGHT;
2747  default:
2748  throw Util.unexpected(joinType);
2749  }
2750  }

+ Here is the caller graph for this function:

RexLiteral org.apache.calcite.sql2rel.SqlToRelConverter.convertLiteralInValuesList ( SqlNode  sqlNode,
Blackboard  bb,
RelDataType  rowType,
int  iField 
)
inlineprivate

Definition at line 1637 of file SqlToRelConverter.java.

References CAST, field(), and run_benchmark_import.type.

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

1638  {
1639  if (!(sqlNode instanceof SqlLiteral)) {
1640  return null;
1641  }
1642  RelDataTypeField field = rowType.getFieldList().get(iField);
1643  RelDataType type = field.getType();
1644  if (type.isStruct()) {
1645  // null literals for weird stuff like UDT's need
1646  // special handling during type flattening, so
1647  // don't use LogicalValues for those
1648  return null;
1649  }
1650 
1651  RexNode literalExpr = exprConverter.convertLiteral(bb, (SqlLiteral) sqlNode);
1652 
1653  if (!(literalExpr instanceof RexLiteral)) {
1654  assert literalExpr.isA(SqlKind.CAST);
1655  RexNode child = ((RexCall) literalExpr).getOperands().get(0);
1656  assert RexLiteral.isNullLiteral(child);
1657 
1658  // NOTE jvs 22-Nov-2006: we preserve type info
1659  // in LogicalValues digest, so it's OK to lose it here
1660  return (RexLiteral) child;
1661  }
1662 
1663  RexLiteral literal = (RexLiteral) literalExpr;
1664 
1665  Comparable value = literal.getValue();
1666 
1667  if (SqlTypeUtil.isExactNumeric(type) && SqlTypeUtil.hasScale(type)) {
1668  BigDecimal roundedValue =
1669  NumberUtil.rescaleBigDecimal((BigDecimal) value, type.getScale());
1670  return rexBuilder.makeExactLiteral(roundedValue, type);
1671  }
1672 
1673  if ((value instanceof NlsString) && (type.getSqlTypeName() == SqlTypeName.CHAR)) {
1674  // pad fixed character type
1675  NlsString unpadded = (NlsString) value;
1676  return rexBuilder.makeCharLiteral(
1677  new NlsString(Spaces.padRight(unpadded.getValue(), type.getPrecision()),
1678  unpadded.getCharsetName(),
1679  unpadded.getCollation()));
1680  }
1681  return literal;
1682  }
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
#define CAST

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertMatchRecognize ( Blackboard  bb,
SqlCall  call 
)
inlineprotected

Definition at line 2146 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(), org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard(), org.apache.calcite.sql2rel.SqlToRelConverter.desc(), org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries(), and setup.root.

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

2146  {
2147  final SqlMatchRecognize matchRecognize = (SqlMatchRecognize) call;
2148  final SqlValidatorNamespace ns = validator.getNamespace(matchRecognize);
2149  final SqlValidatorScope scope = validator.getMatchRecognizeScope(matchRecognize);
2150 
2151  final Blackboard matchBb = createBlackboard(scope, null, false);
2152  final RelDataType rowType = ns.getRowType();
2153  // convert inner query, could be a table name or a derived table
2154  SqlNode expr = matchRecognize.getTableRef();
2155  convertFrom(matchBb, expr);
2156  final RelNode input = matchBb.root;
2157 
2158  // PARTITION BY
2159  final SqlNodeList partitionList = matchRecognize.getPartitionList();
2160  final ImmutableBitSet.Builder partitionKeys = ImmutableBitSet.builder();
2161  for (SqlNode partition : partitionList) {
2162  RexNode e = matchBb.convertExpression(partition);
2163  partitionKeys.set(((RexInputRef) e).getIndex());
2164  }
2165 
2166  // ORDER BY
2167  final SqlNodeList orderList = matchRecognize.getOrderList();
2168  final List<RelFieldCollation> orderKeys = new ArrayList<>();
2169  for (SqlNode order : orderList) {
2170  final RelFieldCollation.Direction direction;
2171  switch (order.getKind()) {
2172  case DESCENDING:
2173  direction = RelFieldCollation.Direction.DESCENDING;
2174  order = ((SqlCall) order).operand(0);
2175  break;
2176  case NULLS_FIRST:
2177  case NULLS_LAST:
2178  throw new AssertionError();
2179  default:
2180  direction = RelFieldCollation.Direction.ASCENDING;
2181  break;
2182  }
2183  final RelFieldCollation.NullDirection nullDirection =
2184  validator.config().defaultNullCollation().last(desc(direction))
2185  ? RelFieldCollation.NullDirection.LAST
2186  : RelFieldCollation.NullDirection.FIRST;
2187  RexNode e = matchBb.convertExpression(order);
2188  orderKeys.add(new RelFieldCollation(
2189  ((RexInputRef) e).getIndex(), direction, nullDirection));
2190  }
2191  final RelCollation orders = cluster.traitSet().canonize(RelCollations.of(orderKeys));
2192 
2193  // convert pattern
2194  final Set<String> patternVarsSet = new HashSet<>();
2195  SqlNode pattern = matchRecognize.getPattern();
2196  final SqlBasicVisitor<RexNode> patternVarVisitor = new SqlBasicVisitor<RexNode>() {
2197  @Override
2198  public RexNode visit(SqlCall call) {
2199  List<SqlNode> operands = call.getOperandList();
2200  List<RexNode> newOperands = new ArrayList<>();
2201  for (SqlNode node : operands) {
2202  newOperands.add(node.accept(this));
2203  }
2204  return rexBuilder.makeCall(
2205  validator.getUnknownType(), call.getOperator(), newOperands);
2206  }
2207 
2208  @Override
2209  public RexNode visit(SqlIdentifier id) {
2210  assert id.isSimple();
2211  patternVarsSet.add(id.getSimple());
2212  return rexBuilder.makeLiteral(id.getSimple());
2213  }
2214 
2215  @Override
2216  public RexNode visit(SqlLiteral literal) {
2217  if (literal instanceof SqlNumericLiteral) {
2218  return rexBuilder.makeExactLiteral(BigDecimal.valueOf(literal.intValue(true)));
2219  } else {
2220  return rexBuilder.makeLiteral(literal.booleanValue());
2221  }
2222  }
2223  };
2224  final RexNode patternNode = pattern.accept(patternVarVisitor);
2225 
2226  SqlLiteral interval = matchRecognize.getInterval();
2227  RexNode intervalNode = null;
2228  if (interval != null) {
2229  intervalNode = matchBb.convertLiteral(interval);
2230  }
2231 
2232  // convert subset
2233  final SqlNodeList subsets = matchRecognize.getSubsetList();
2234  final Map<String, TreeSet<String>> subsetMap = new HashMap<>();
2235  for (SqlNode node : subsets) {
2236  List<SqlNode> operands = ((SqlCall) node).getOperandList();
2237  SqlIdentifier left = (SqlIdentifier) operands.get(0);
2238  patternVarsSet.add(left.getSimple());
2239  SqlNodeList rights = (SqlNodeList) operands.get(1);
2240  final TreeSet<String> list = new TreeSet<>();
2241  for (SqlNode right : rights) {
2242  assert right instanceof SqlIdentifier;
2243  list.add(((SqlIdentifier) right).getSimple());
2244  }
2245  subsetMap.put(left.getSimple(), list);
2246  }
2247 
2248  SqlNode afterMatch = matchRecognize.getAfter();
2249  if (afterMatch == null) {
2250  afterMatch =
2251  SqlMatchRecognize.AfterOption.SKIP_TO_NEXT_ROW.symbol(SqlParserPos.ZERO);
2252  }
2253 
2254  final RexNode after;
2255  if (afterMatch instanceof SqlCall) {
2256  List<SqlNode> operands = ((SqlCall) afterMatch).getOperandList();
2257  SqlOperator operator = ((SqlCall) afterMatch).getOperator();
2258  assert operands.size() == 1;
2259  SqlIdentifier id = (SqlIdentifier) operands.get(0);
2260  assert patternVarsSet.contains(id.getSimple())
2261  : id.getSimple()
2262  + " not defined in pattern";
2263  RexNode rex = rexBuilder.makeLiteral(id.getSimple());
2264  after = rexBuilder.makeCall(
2265  validator.getUnknownType(), operator, ImmutableList.of(rex));
2266  } else {
2267  after = matchBb.convertExpression(afterMatch);
2268  }
2269 
2270  matchBb.setPatternVarRef(true);
2271 
2272  // convert measures
2273  final ImmutableMap.Builder<String, RexNode> measureNodes = ImmutableMap.builder();
2274  for (SqlNode measure : matchRecognize.getMeasureList()) {
2275  List<SqlNode> operands = ((SqlCall) measure).getOperandList();
2276  String alias = ((SqlIdentifier) operands.get(1)).getSimple();
2277  RexNode rex = matchBb.convertExpression(operands.get(0));
2278  measureNodes.put(alias, rex);
2279  }
2280 
2281  // convert definitions
2282  final ImmutableMap.Builder<String, RexNode> definitionNodes = ImmutableMap.builder();
2283  for (SqlNode def : matchRecognize.getPatternDefList()) {
2284  replaceSubQueries(matchBb, def, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
2285  List<SqlNode> operands = ((SqlCall) def).getOperandList();
2286  String alias = ((SqlIdentifier) operands.get(1)).getSimple();
2287  RexNode rex = matchBb.convertExpression(operands.get(0));
2288  definitionNodes.put(alias, rex);
2289  }
2290 
2291  final SqlLiteral rowsPerMatch = matchRecognize.getRowsPerMatch();
2292  final boolean allRows = rowsPerMatch != null
2293  && rowsPerMatch.getValue() == SqlMatchRecognize.RowsPerMatchOption.ALL_ROWS;
2294 
2295  matchBb.setPatternVarRef(false);
2296 
2297  final RelFactories.MatchFactory factory = RelFactories.DEFAULT_MATCH_FACTORY;
2298  final RelNode rel = factory.createMatch(input,
2299  patternNode,
2300  rowType,
2301  matchRecognize.getStrictStart().booleanValue(),
2302  matchRecognize.getStrictEnd().booleanValue(),
2303  definitionNodes.build(),
2304  measureNodes.build(),
2305  after,
2306  subsetMap,
2307  allRows,
2308  partitionKeys.build(),
2309  orders,
2310  intervalNode);
2311  bb.setRoot(rel, false);
2312  }
tuple root
Definition: setup.in.py:14
void convertFrom(Blackboard bb, SqlNode from)
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
static boolean desc(RelFieldCollation.Direction direction)
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertMerge ( SqlMerge  call)
inlineprivate

Definition at line 3682 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader, org.apache.calcite.sql2rel.SqlToRelConverter.convertInsert(), org.apache.calcite.sql2rel.SqlToRelConverter.convertSelect(), field(), org.apache.calcite.sql2rel.SqlToRelConverter.getTargetTable(), join(), and org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory.

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

3682  {
3683  RelOptTable targetTable = getTargetTable(call);
3684 
3685  // convert update column list from SqlIdentifier to String
3686  final List<String> targetColumnNameList = new ArrayList<>();
3687  final RelDataType targetRowType = targetTable.getRowType();
3688  SqlUpdate updateCall = call.getUpdateCall();
3689  if (updateCall != null) {
3690  for (SqlNode targetColumn : updateCall.getTargetColumnList()) {
3691  SqlIdentifier id = (SqlIdentifier) targetColumn;
3692  RelDataTypeField field = SqlValidatorUtil.getTargetField(
3693  targetRowType, typeFactory, id, catalogReader, targetTable);
3694  assert field != null : "column " + id.toString() + " not found";
3695  targetColumnNameList.add(field.getName());
3696  }
3697  }
3698 
3699  // replace the projection of the source select with a
3700  // projection that contains the following:
3701  // 1) the expressions corresponding to the new insert row (if there is
3702  // an insert)
3703  // 2) all columns from the target table (if there is an update)
3704  // 3) the set expressions in the update call (if there is an update)
3705 
3706  // first, convert the merge's source select to construct the columns
3707  // from the target table and the set expressions in the update call
3708  RelNode mergeSourceRel = convertSelect(call.getSourceSelect(), false);
3709 
3710  // then, convert the insert statement so we can get the insert
3711  // values expressions
3712  SqlInsert insertCall = call.getInsertCall();
3713  int nLevel1Exprs = 0;
3714  List<RexNode> level1InsertExprs = null;
3715  List<RexNode> level2InsertExprs = null;
3716  if (insertCall != null) {
3717  RelNode insertRel = convertInsert(insertCall);
3718 
3719  // if there are 2 level of projections in the insert source, combine
3720  // them into a single project; level1 refers to the topmost project;
3721  // the level1 projection contains references to the level2
3722  // expressions, except in the case where no target expression was
3723  // provided, in which case, the expression is the default value for
3724  // the column; or if the expressions directly map to the source
3725  // table
3726  level1InsertExprs = ((LogicalProject) insertRel.getInput(0)).getProjects();
3727  if (insertRel.getInput(0).getInput(0) instanceof LogicalProject) {
3728  level2InsertExprs =
3729  ((LogicalProject) insertRel.getInput(0).getInput(0)).getProjects();
3730  }
3731  nLevel1Exprs = level1InsertExprs.size();
3732  }
3733 
3734  LogicalJoin join = (LogicalJoin) mergeSourceRel.getInput(0);
3735  int nSourceFields = join.getLeft().getRowType().getFieldCount();
3736  final List<RexNode> projects = new ArrayList<>();
3737  for (int level1Idx = 0; level1Idx < nLevel1Exprs; level1Idx++) {
3738  if ((level2InsertExprs != null)
3739  && (level1InsertExprs.get(level1Idx) instanceof RexInputRef)) {
3740  int level2Idx = ((RexInputRef) level1InsertExprs.get(level1Idx)).getIndex();
3741  projects.add(level2InsertExprs.get(level2Idx));
3742  } else {
3743  projects.add(level1InsertExprs.get(level1Idx));
3744  }
3745  }
3746  if (updateCall != null) {
3747  final LogicalProject project = (LogicalProject) mergeSourceRel;
3748  projects.addAll(Util.skip(project.getProjects(), nSourceFields));
3749  }
3750 
3751  relBuilder.push(join).project(projects);
3752 
3753  return LogicalTableModify.create(targetTable,
3754  catalogReader,
3755  relBuilder.build(),
3756  LogicalTableModify.Operation.MERGE,
3757  targetColumnNameList,
3758  null,
3759  false);
3760  }
std::string join(T const &container, std::string const &delim)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
RelNode convertSelect(SqlSelect select, boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertMultisets ( final List< SqlNode >  operands,
Blackboard  bb 
)
inlineprivate

Definition at line 3857 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.cluster, org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(), org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryOrInList(), org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard(), i, setup.root, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.scope, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertRowConstructor(), and org.apache.calcite.sql2rel.SqlToRelConverter.substituteSubQuery().

3857  {
3858  // NOTE: Wael 2/04/05: this implementation is not the most efficient in
3859  // terms of planning since it generates XOs that can be reduced.
3860  final List<Object> joinList = new ArrayList<>();
3861  List<SqlNode> lastList = new ArrayList<>();
3862  for (int i = 0; i < operands.size(); i++) {
3863  SqlNode operand = operands.get(i);
3864  if (!(operand instanceof SqlCall)) {
3865  lastList.add(operand);
3866  continue;
3867  }
3868 
3869  final SqlCall call = (SqlCall) operand;
3870  final RelNode input;
3871  switch (call.getKind()) {
3872  case MULTISET_VALUE_CONSTRUCTOR:
3873  case ARRAY_VALUE_CONSTRUCTOR:
3874  final SqlNodeList list =
3875  new SqlNodeList(call.getOperandList(), call.getParserPosition());
3876  CollectNamespace nss = (CollectNamespace) validator.getNamespace(call);
3877  Blackboard usedBb;
3878  if (null != nss) {
3879  usedBb = createBlackboard(nss.getScope(), null, false);
3880  } else {
3881  usedBb = createBlackboard(new ListScope(bb.scope) {
3882  public SqlNode getNode() {
3883  return call;
3884  }
3885  }, null, false);
3886  }
3887  RelDataType multisetType = validator.getValidatedNodeType(call);
3888  validator.setValidatedNodeType(list, multisetType.getComponentType());
3889  input = convertQueryOrInList(usedBb, list, null);
3890  break;
3891  case MULTISET_QUERY_CONSTRUCTOR:
3892  case ARRAY_QUERY_CONSTRUCTOR:
3893  final RelRoot root = convertQuery(call.operand(0), false, true);
3894  input = root.rel;
3895  break;
3896  default:
3897  lastList.add(operand);
3898  continue;
3899  }
3900 
3901  if (lastList.size() > 0) {
3902  joinList.add(lastList);
3903  }
3904  lastList = new ArrayList<>();
3905  Collect collect = new Collect(cluster,
3906  cluster.traitSetOf(Convention.NONE),
3907  input,
3908  validator.deriveAlias(call, i));
3909  joinList.add(collect);
3910  }
3911 
3912  if (joinList.size() == 0) {
3913  joinList.add(lastList);
3914  }
3915 
3916  for (int i = 0; i < joinList.size(); i++) {
3917  Object o = joinList.get(i);
3918  if (o instanceof List) {
3919  @SuppressWarnings("unchecked")
3920  List<SqlNode> projectList = (List<SqlNode>) o;
3921  final List<RexNode> selectList = new ArrayList<>();
3922  final List<String> fieldNameList = new ArrayList<>();
3923  for (int j = 0; j < projectList.size(); j++) {
3924  SqlNode operand = projectList.get(j);
3925  selectList.add(bb.convertExpression(operand));
3926 
3927  // REVIEW angel 5-June-2005: Use deriveAliasFromOrdinal
3928  // instead of deriveAlias to match field names from
3929  // SqlRowOperator. Otherwise, get error Type
3930  // 'RecordType(INTEGER EMPNO)' has no field 'EXPR$0' when
3931  // doing select * from unnest( select multiset[empno]
3932  // from sales.emps);
3933 
3934  fieldNameList.add(SqlUtil.deriveAliasFromOrdinal(j));
3935  }
3936 
3937  relBuilder.push(LogicalValues.createOneRow(cluster))
3938  .projectNamed(selectList, fieldNameList, true);
3939 
3940  joinList.set(i, relBuilder.build());
3941  }
3942  }
3943 
3944  RelNode ret = (RelNode) joinList.get(0);
3945  for (int i = 1; i < joinList.size(); i++) {
3946  RelNode relNode = (RelNode) joinList.get(i);
3947  ret = RelFactories.DEFAULT_JOIN_FACTORY.createJoin(ret,
3948  relNode,
3949  ImmutableList.of(),
3950  rexBuilder.makeLiteral(true),
3951  ImmutableSet.of(),
3952  JoinRelType.INNER,
3953  false);
3954  }
3955  return ret;
3956  }
RelNode convertQueryOrInList(Blackboard bb, SqlNode seek, RelDataType targetRowType)
tuple root
Definition: setup.in.py:14
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)
RelRoot convertQuery(SqlNode query, final boolean needsValidation, final boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

boolean org.apache.calcite.sql2rel.SqlToRelConverter.convertNonCorrelatedSubQuery ( SubQuery  subQuery,
Blackboard  bb,
RelNode  converted,
boolean  isExists 
)
inlineprivate

Determines if a sub-query is non-correlated and if so, converts it to a constant.

Parameters
subQuerythe call that references the sub-query
bbblackboard used to convert the sub-query
convertedRelNode tree corresponding to the sub-query
isExiststrue if the sub-query is part of an EXISTS expression
Returns
Whether the sub-query can be converted to a constant

Definition at line 1361 of file SqlToRelConverter.java.

References if(), org.apache.calcite.sql2rel.SqlToRelConverter.isSubQueryNonCorrelated(), and org.apache.calcite.sql2rel.SqlToRelConverter.subQueryConverter.

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

1362  {
1363  SqlCall call = (SqlBasicCall) subQuery.node;
1364  if (subQueryConverter.canConvertSubQuery()
1365  && isSubQueryNonCorrelated(converted, bb)) {
1366  // First check if the sub-query has already been converted
1367  // because it's a nested sub-query. If so, don't re-evaluate
1368  // it again.
1369  RexNode constExpr = mapConvertedNonCorrSubqs.get(call);
1370  if (constExpr == null) {
1371  constExpr = subQueryConverter.convertSubQuery(
1372  call, this, isExists, config.isExplain());
1373  }
1374  if (constExpr != null) {
1375  subQuery.expr = constExpr;
1376  mapConvertedNonCorrSubqs.put(call, constExpr);
1377  return true;
1378  }
1379  }
1380  return false;
1381  }
boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb)
if(yyssp >=yyss+yystacksize-1)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertOrder ( SqlSelect  select,
Blackboard  bb,
RelCollation  collation,
List< SqlNode >  orderExprList,
SqlNode  offset,
SqlNode  fetch 
)
inlineprotected

Converts a query's ORDER BY clause, if any.

Ignores the ORDER BY clause if the query is not top-level and FETCH or OFFSET are not present.

Parameters
selectQuery
bbBlackboard
collationCollation list
orderExprListMethod populates this list with orderBy expressions not present in selectList
offsetExpression for number of rows to discard before returning first row
fetchExpression for number of rows to fetch

Definition at line 803 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertExpression(), i, setup.root, and org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.top.

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

808  {
809  if (!bb.top || select.getOrderList() == null
810  || select.getOrderList().getList().isEmpty()) {
811  assert !bb.top || collation.getFieldCollations().isEmpty();
812  if ((offset == null
813  || (offset instanceof SqlLiteral
814  && ((SqlLiteral) offset)
815  .bigDecimalValue()
816  .equals(BigDecimal.ZERO)))
817  && fetch == null) {
818  return;
819  }
820  }
821 
822  // Create a sorter using the previously constructed collations.
823  bb.setRoot(LogicalSort.create(bb.root,
824  collation,
825  offset == null ? null : convertExpression(offset),
826  fetch == null ? null : convertExpression(fetch)),
827  false);
828 
829  // If extra expressions were added to the project list for sorting,
830  // add another project to remove them. But make the collation empty, because
831  // we can't represent the real collation.
832  //
833  // If it is the top node, use the real collation, but don't trim fields.
834  if (orderExprList.size() > 0 && !bb.top) {
835  final List<RexNode> exprs = new ArrayList<>();
836  final RelDataType rowType = bb.root.getRowType();
837  final int fieldCount = rowType.getFieldCount() - orderExprList.size();
838  for (int i = 0; i < fieldCount; i++) {
839  exprs.add(rexBuilder.makeInputRef(bb.root, i));
840  }
841  bb.setRoot(LogicalProject.create(bb.root,
842  ImmutableList.of(),
843  exprs,
844  rowType.getFieldNames().subList(0, fieldCount)),
845  false);
846  }
847  }
tuple root
Definition: setup.in.py:14

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelFieldCollation org.apache.calcite.sql2rel.SqlToRelConverter.convertOrderItem ( SqlSelect  select,
SqlNode  orderItem,
List< SqlNode >  extraExprs,
RelFieldCollation.Direction  direction,
RelFieldCollation.NullDirection  nullDirection 
)
inlineprotected

Definition at line 3134 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.desc().

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

3138  {
3139  assert select != null;
3140  // Handle DESC keyword, e.g. 'select a, b from t order by a desc'.
3141  switch (orderItem.getKind()) {
3142  case DESCENDING:
3143  return convertOrderItem(select,
3144  ((SqlCall) orderItem).operand(0),
3145  extraExprs,
3146  RelFieldCollation.Direction.DESCENDING,
3147  nullDirection);
3148  case NULLS_FIRST:
3149  return convertOrderItem(select,
3150  ((SqlCall) orderItem).operand(0),
3151  extraExprs,
3152  direction,
3153  RelFieldCollation.NullDirection.FIRST);
3154  case NULLS_LAST:
3155  return convertOrderItem(select,
3156  ((SqlCall) orderItem).operand(0),
3157  extraExprs,
3158  direction,
3159  RelFieldCollation.NullDirection.LAST);
3160  }
3161 
3162  SqlNode converted = validator.expandOrderExpr(select, orderItem);
3163 
3164  switch (nullDirection) {
3165  case UNSPECIFIED:
3166  nullDirection = validator.config().defaultNullCollation().last(desc(direction))
3167  ? RelFieldCollation.NullDirection.LAST
3168  : RelFieldCollation.NullDirection.FIRST;
3169  }
3170 
3171  // Scan the select list and order exprs for an identical expression.
3172  final SelectScope selectScope = validator.getRawSelectScope(select);
3173  int ordinal = -1;
3174  for (SqlNode selectItem : selectScope.getExpandedSelectList()) {
3175  ++ordinal;
3176  if (converted.equalsDeep(stripAs(selectItem), Litmus.IGNORE)) {
3177  return new RelFieldCollation(ordinal, direction, nullDirection);
3178  }
3179  }
3180 
3181  for (SqlNode extraExpr : extraExprs) {
3182  ++ordinal;
3183  if (converted.equalsDeep(extraExpr, Litmus.IGNORE)) {
3184  return new RelFieldCollation(ordinal, direction, nullDirection);
3185  }
3186  }
3187 
3188  // TODO: handle collation sequence
3189  // TODO: flag expressions as non-standard
3190 
3191  extraExprs.add(converted);
3192  return new RelFieldCollation(ordinal + 1, direction, nullDirection);
3193  }
static boolean desc(RelFieldCollation.Direction direction)
RelFieldCollation convertOrderItem(SqlSelect select, SqlNode orderItem, List< SqlNode > extraExprs, RelFieldCollation.Direction direction, RelFieldCollation.NullDirection nullDirection)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.convertOver ( Blackboard  bb,
SqlNode  node 
)
inlineprivate

Definition at line 1846 of file SqlToRelConverter.java.

1846  {
1847  SqlCall call = (SqlCall) node;
1848  SqlCall aggCall = call.operand(0);
1849  boolean ignoreNulls = false;
1850  switch (aggCall.getKind()) {
1851  case IGNORE_NULLS:
1852  ignoreNulls = true;
1853  // fall through
1854  case RESPECT_NULLS:
1855  aggCall = aggCall.operand(0);
1856  }
1857 
1858  SqlNode windowOrRef = call.operand(1);
1859  final SqlWindow window = validator.resolveWindow(windowOrRef, bb.scope);
1860 
1861  SqlNode sqlLowerBound = window.getLowerBound();
1862  SqlNode sqlUpperBound = window.getUpperBound();
1863  boolean rows = window.isRows();
1864  SqlNodeList orderList = window.getOrderList();
1865 
1866  if (!aggCall.getOperator().allowsFraming()) {
1867  // If the operator does not allow framing, bracketing is implicitly
1868  // everything up to the current row.
1869  sqlLowerBound = SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO);
1870  sqlUpperBound = SqlWindow.createCurrentRow(SqlParserPos.ZERO);
1871  if (aggCall.getKind() == SqlKind.ROW_NUMBER) {
1872  // ROW_NUMBER() expects specific kind of framing.
1873  rows = true;
1874  }
1875  } else if (orderList.size() == 0) {
1876  // Without ORDER BY, there must be no bracketing.
1877  sqlLowerBound = SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO);
1878  sqlUpperBound = SqlWindow.createUnboundedFollowing(SqlParserPos.ZERO);
1879  } else if (sqlLowerBound == null && sqlUpperBound == null) {
1880  sqlLowerBound = SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO);
1881  sqlUpperBound = SqlWindow.createCurrentRow(SqlParserPos.ZERO);
1882  } else if (sqlUpperBound == null) {
1883  sqlUpperBound = SqlWindow.createCurrentRow(SqlParserPos.ZERO);
1884  } else if (sqlLowerBound == null) {
1885  sqlLowerBound = SqlWindow.createCurrentRow(SqlParserPos.ZERO);
1886  }
1887  final SqlNodeList partitionList = window.getPartitionList();
1888  final ImmutableList.Builder<RexNode> partitionKeys = ImmutableList.builder();
1889  for (SqlNode partition : partitionList) {
1890  partitionKeys.add(bb.convertExpression(partition));
1891  }
1892  final RexNode lowerBound = bb.convertExpression(sqlLowerBound);
1893  final RexNode upperBound = bb.convertExpression(sqlUpperBound);
1894  if (orderList.size() == 0 && !rows) {
1895  // A logical range requires an ORDER BY clause. Use the implicit
1896  // ordering of this relation. There must be one, otherwise it would
1897  // have failed validation.
1898  orderList = bb.scope.getOrderList();
1899  if (orderList == null) {
1900  throw new AssertionError("Relation should have sort key for implicit ORDER BY");
1901  }
1902  }
1903 
1904  final ImmutableList.Builder<RexFieldCollation> orderKeys = ImmutableList.builder();
1905  for (SqlNode order : orderList) {
1906  orderKeys.add(bb.convertSortExpression(order,
1907  RelFieldCollation.Direction.ASCENDING,
1908  RelFieldCollation.NullDirection.UNSPECIFIED));
1909  }
1910 
1911  try {
1912  Preconditions.checkArgument(bb.window == null, "already in window agg mode");
1913  bb.window = window;
1914  RexNode rexAgg = exprConverter.convertCall(bb, aggCall);
1915  rexAgg = rexBuilder.ensureType(validator.getValidatedNodeType(call), rexAgg, false);
1916 
1917  // Walk over the tree and apply 'over' to all agg functions. This is
1918  // necessary because the returned expression is not necessarily a call
1919  // to an agg function. For example, AVG(x) becomes SUM(x) / COUNT(x).
1920 
1921  final SqlLiteral q = aggCall.getFunctionQuantifier();
1922  final boolean isDistinct = q != null && q.getValue() == SqlSelectKeyword.DISTINCT;
1923 
1924  final RexShuttle visitor = new HistogramShuttle(partitionKeys.build(),
1925  orderKeys.build(),
1926  RexWindowBounds.create(sqlLowerBound, lowerBound),
1927  RexWindowBounds.create(sqlUpperBound, upperBound),
1928  rows,
1929  window.isAllowPartial(),
1930  isDistinct,
1931  ignoreNulls);
1932  return rexAgg.accept(visitor);
1933  } finally {
1934  bb.window = null;
1935  }
1936  }
RelRoot org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery ( SqlNode  query,
final boolean  needsValidation,
final boolean  top 
)
inline

Converts an unvalidated query's parse tree into a relational expression.

Parameters
queryQuery to convert
needsValidationWhether to validate the query before converting; false if the query has already been validated.
topWhether the query is top-level, say if its result will become a JDBC result set; false if the query will be part of a view.

Definition at line 567 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.checkConvertedType(), org.apache.calcite.sql2rel.SqlToRelConverter.cluster, org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(), org.apache.calcite.sql2rel.SqlToRelConverter.hintStrategies, org.apache.calcite.sql2rel.SqlToRelConverter.isOrdered(), org.apache.calcite.sql2rel.SqlToRelConverter.isStream(), org.apache.calcite.sql2rel.SqlToRelConverter.requiredCollation(), run_benchmark_import.result, SELECT, org.apache.calcite.sql2rel.SqlToRelConverter.SQL2REL_LOGGER, and TEXT.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertCollectionTable(), org.apache.calcite.sql2rel.SqlToRelConverter.convertCursor(), and org.apache.calcite.sql2rel.SqlToRelConverter.convertMultisets().

568  {
569  if (needsValidation) {
570  query = validator.validate(query);
571  }
572 
573  RelNode result = convertQueryRecursive(query, top, null).rel;
574  if (top) {
575  if (isStream(query)) {
576  result = new LogicalDelta(cluster, result.getTraitSet(), result);
577  }
578  }
579  RelCollation collation = RelCollations.EMPTY;
580  if (!query.isA(SqlKind.DML)) {
581  if (isOrdered(query)) {
582  collation = requiredCollation(result);
583  }
584  }
585  checkConvertedType(query, result);
586 
587  if (SQL2REL_LOGGER.isDebugEnabled()) {
588  SQL2REL_LOGGER.debug(RelOptUtil.dumpPlan("Plan after converting SqlNode to RelNode",
589  result,
591  SqlExplainLevel.EXPPLAN_ATTRIBUTES));
592  }
593 
594  final RelDataType validatedRowType = validator.getValidatedNodeType(query);
595  List<RelHint> hints = new ArrayList<>();
596  if (query.getKind() == SqlKind.SELECT) {
597  final SqlSelect select = (SqlSelect) query;
598  if (select.hasHints()) {
599  hints = SqlUtil.getRelHint(hintStrategies, select.getHints());
600  }
601  }
602  // propagate the hints.
603  result = RelOptUtil.propagateRelHints(result, false);
604  return RelRoot.of(result, validatedRowType, query.getKind())
605  .withCollation(collation)
606  .withHints(hints);
607  }
void checkConvertedType(SqlNode query, RelNode result)
#define TEXT
RelRoot convertQueryRecursive(SqlNode query, boolean top, RelDataType targetRowType)
#define SELECT

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryOrInList ( Blackboard  bb,
SqlNode  seek,
RelDataType  targetRowType 
)
inlineprivate

Definition at line 1547 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(), and org.apache.calcite.sql2rel.SqlToRelConverter.convertRowValues().

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertExists(), org.apache.calcite.sql2rel.SqlToRelConverter.convertMultisets(), and org.apache.calcite.sql2rel.SqlToRelConverter.substituteSubQuery().

1548  {
1549  // NOTE: Once we start accepting single-row queries as row constructors,
1550  // there will be an ambiguity here for a case like X IN ((SELECT Y FROM
1551  // Z)). The SQL standard resolves the ambiguity by saying that a lone
1552  // select should be interpreted as a table expression, not a row
1553  // expression. The semantic difference is that a table expression can
1554  // return multiple rows.
1555  if (seek instanceof SqlNodeList) {
1556  return convertRowValues(
1557  bb, seek, ((SqlNodeList) seek).getList(), false, targetRowType);
1558  } else {
1559  return convertQueryRecursive(seek, false, null).project();
1560  }
1561  }
RelNode convertRowValues(Blackboard bb, SqlNode rowList, Collection< SqlNode > rows, boolean allowLiteralsOnly, RelDataType targetRowType)
RelRoot convertQueryRecursive(SqlNode query, boolean top, RelDataType targetRowType)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelRoot org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive ( SqlNode  query,
boolean  top,
RelDataType  targetRowType 
)
inlineprotected

Recursively converts a query to a relational expression.

Parameters
queryQuery
topWhether this query is the top-level query of the statement
targetRowTypeTarget row type, or null
Returns
Relational expression

Definition at line 3235 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertDelete(), org.apache.calcite.sql2rel.SqlToRelConverter.convertInsert(), org.apache.calcite.sql2rel.SqlToRelConverter.convertMerge(), org.apache.calcite.sql2rel.SqlToRelConverter.convertSelect(), org.apache.calcite.sql2rel.SqlToRelConverter.convertSetOp(), org.apache.calcite.sql2rel.SqlToRelConverter.convertUpdate(), org.apache.calcite.sql2rel.SqlToRelConverter.convertValues(), org.apache.calcite.sql2rel.SqlToRelConverter.convertWith(), DELETE, INSERT, SELECT, UNION, UPDATE, VALUES, and WITH.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(), org.apache.calcite.sql2rel.SqlToRelConverter.convertInsert(), org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(), org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryOrInList(), and org.apache.calcite.sql2rel.SqlToRelConverter.convertSetOp().

3236  {
3237  final SqlKind kind = query.getKind();
3238  switch (kind) {
3239  case SELECT:
3240  return RelRoot.of(convertSelect((SqlSelect) query, top), kind);
3241  case INSERT:
3242  return RelRoot.of(convertInsert((SqlInsert) query), kind);
3243  case DELETE:
3244  return RelRoot.of(convertDelete((SqlDelete) query), kind);
3245  case UPDATE:
3246  return RelRoot.of(convertUpdate((SqlUpdate) query), kind);
3247  case MERGE:
3248  return RelRoot.of(convertMerge((SqlMerge) query), kind);
3249  case UNION:
3250  case INTERSECT:
3251  case EXCEPT:
3252  return RelRoot.of(convertSetOp((SqlCall) query), kind);
3253  case WITH:
3254  return convertWith((SqlWith) query, top);
3255  case VALUES:
3256  return RelRoot.of(convertValues((SqlCall) query, targetRowType), kind);
3257  default:
3258  throw new AssertionError("not a query: " + query);
3259  }
3260  }
RelRoot convertWith(SqlWith with, boolean top)
#define DELETE
#define VALUES
#define INSERT
#define WITH
RelNode convertValues(SqlCall values, RelDataType targetRowType)
#define UPDATE
#define SELECT
RelNode convertSelect(SqlSelect select, boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertRowConstructor ( Blackboard  bb,
SqlCall  rowConstructor 
)
inlineprivate

Converts a row constructor into a relational expression.

Parameters
bbBlackboard
rowConstructorRow constructor expression
Returns
Relational expression which returns a single row.

Definition at line 3840 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertMultisets(), and org.apache.calcite.sql2rel.SqlToRelConverter.isRowConstructor().

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

3840  {
3841  Preconditions.checkArgument(isRowConstructor(rowConstructor));
3842  final List<SqlNode> operands = rowConstructor.getOperandList();
3843  return convertMultisets(operands, bb);
3844  }
RelNode convertMultisets(final List< SqlNode > operands, Blackboard bb)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertRowValues ( Blackboard  bb,
SqlNode  rowList,
Collection< SqlNode >  rows,
boolean  allowLiteralsOnly,
RelDataType  targetRowType 
)
inlineprivate

Definition at line 1563 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.cluster, org.apache.calcite.sql2rel.SqlToRelConverter.config, org.apache.calcite.sql2rel.SqlToRelConverter.convertLiteralInValuesList(), org.apache.calcite.sql2rel.SqlToRelConverter.convertRowConstructor(), org.apache.calcite.sql2rel.SqlToRelConverter.isRowConstructor(), and org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory.

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

1567  {
1568  // NOTE jvs 30-Apr-2006: We combine all rows consisting entirely of
1569  // literals into a single LogicalValues; this gives the optimizer a smaller
1570  // input tree. For everything else (computed expressions, row
1571  // sub-queries), we union each row in as a projection on top of a
1572  // LogicalOneRow.
1573 
1574  final ImmutableList.Builder<ImmutableList<RexLiteral>> tupleList =
1575  ImmutableList.builder();
1576  final RelDataType rowType;
1577  if (targetRowType != null) {
1578  rowType = targetRowType;
1579  } else {
1580  rowType = SqlTypeUtil.promoteToRowType(
1581  typeFactory, validator.getValidatedNodeType(rowList), null);
1582  }
1583 
1584  final List<RelNode> unionInputs = new ArrayList<>();
1585  for (SqlNode node : rows) {
1586  SqlBasicCall call;
1587  if (isRowConstructor(node)) {
1588  call = (SqlBasicCall) node;
1589  ImmutableList.Builder<RexLiteral> tuple = ImmutableList.builder();
1590  for (Ord<SqlNode> operand : Ord.zip(call.operands)) {
1591  RexLiteral rexLiteral =
1592  convertLiteralInValuesList(operand.e, bb, rowType, operand.i);
1593  if ((rexLiteral == null) && allowLiteralsOnly) {
1594  return null;
1595  }
1596  if ((rexLiteral == null) || !config.isCreateValuesRel()) {
1597  // fallback to convertRowConstructor
1598  tuple = null;
1599  break;
1600  }
1601  tuple.add(rexLiteral);
1602  }
1603  if (tuple != null) {
1604  tupleList.add(tuple.build());
1605  continue;
1606  }
1607  } else {
1608  RexLiteral rexLiteral = convertLiteralInValuesList(node, bb, rowType, 0);
1609  if ((rexLiteral != null) && config.isCreateValuesRel()) {
1610  tupleList.add(ImmutableList.of(rexLiteral));
1611  continue;
1612  } else {
1613  if ((rexLiteral == null) && allowLiteralsOnly) {
1614  return null;
1615  }
1616  }
1617 
1618  // convert "1" to "row(1)"
1619  call = (SqlBasicCall) SqlStdOperatorTable.ROW.createCall(SqlParserPos.ZERO, node);
1620  }
1621  unionInputs.add(convertRowConstructor(bb, call));
1622  }
1623  LogicalValues values = LogicalValues.create(cluster, rowType, tupleList.build());
1624  RelNode resultRel;
1625  if (unionInputs.isEmpty()) {
1626  resultRel = values;
1627  } else {
1628  if (!values.getTuples().isEmpty()) {
1629  unionInputs.add(values);
1630  }
1631  resultRel = LogicalUnion.create(unionInputs, true);
1632  }
1633  leaves.put(resultRel, resultRel.getRowType().getFieldCount());
1634  return resultRel;
1635  }
RelNode convertRowConstructor(Blackboard bb, SqlCall rowConstructor)
RexLiteral convertLiteralInValuesList(SqlNode sqlNode, Blackboard bb, RelDataType rowType, int iField)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertSelect ( SqlSelect  select,
boolean  top 
)
inline

Converts a SELECT statement's parse tree into a relational expression.

Definition at line 644 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectImpl(), org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard(), and setup.root.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertDelete(), org.apache.calcite.sql2rel.SqlToRelConverter.convertMerge(), org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(), and org.apache.calcite.sql2rel.SqlToRelConverter.convertUpdate().

644  {
645  final SqlValidatorScope selectScope = validator.getWhereScope(select);
646  final Blackboard bb = createBlackboard(selectScope, null, top);
647  convertSelectImpl(bb, select);
648  return bb.root;
649  }
tuple root
Definition: setup.in.py:14
void convertSelectImpl(final Blackboard bb, SqlSelect select)
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectImpl ( final Blackboard  bb,
SqlSelect  select 
)
inlineprotected

Implementation of convertSelect(SqlSelect, boolean); derived class may override.

Definition at line 663 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertAgg(), org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(), org.apache.calcite.sql2rel.SqlToRelConverter.convertOrder(), org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectList(), org.apache.calcite.sql2rel.SqlToRelConverter.convertWhere(), org.apache.calcite.sql2rel.SqlToRelConverter.distinctify(), org.apache.calcite.sql2rel.SqlToRelConverter.gatherOrderExprs(), org.apache.calcite.sql2rel.SqlToRelConverter.hintStrategies, i, setup.root, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

663  {
664  convertFrom(bb, select.getFrom());
665  convertWhere(bb, select.getWhere());
666 
667  final List<SqlNode> orderExprList = new ArrayList<>();
668  final List<RelFieldCollation> collationList = new ArrayList<>();
669  gatherOrderExprs(bb, select, select.getOrderList(), orderExprList, collationList);
670  final RelCollation collation =
671  cluster.traitSet().canonize(RelCollations.of(collationList));
672 
673  if (validator.isAggregate(select)) {
674  convertAgg(bb, select, orderExprList);
675  } else {
676  convertSelectList(bb, select, orderExprList);
677  }
678 
679  if (select.isDistinct()) {
680  distinctify(bb, true);
681  }
682 
683  convertOrder(
684  select, bb, collation, orderExprList, select.getOffset(), select.getFetch());
685 
686  if (select.hasHints()) {
687  final List<RelHint> hints = SqlUtil.getRelHint(hintStrategies, select.getHints());
688  // Attach the hints to the first Hintable node we found from the root node.
689  bb.setRoot(bb.root.accept(new RelShuttleImpl() {
690  boolean attached = false;
691 
692  @Override
693  public RelNode visitChild(RelNode parent, int i, RelNode child) {
694  if (parent instanceof Hintable && !attached) {
695  attached = true;
696  return ((Hintable) parent).attachHints(hints);
697  } else {
698  return super.visitChild(parent, i, child);
699  }
700  }
701  }),
702  true);
703  } else {
704  bb.setRoot(bb.root, true);
705  }
706  }
tuple root
Definition: setup.in.py:14
void convertFrom(Blackboard bb, SqlNode from)
void distinctify(Blackboard bb, boolean checkForDupExprs)
void convertAgg(Blackboard bb, SqlSelect select, List< SqlNode > orderExprList)
void gatherOrderExprs(Blackboard bb, SqlSelect select, SqlNodeList orderList, List< SqlNode > extraOrderExprs, List< RelFieldCollation > collationList)
void convertWhere(final Blackboard bb, final SqlNode where)
void convertOrder(SqlSelect select, Blackboard bb, RelCollation collation, List< SqlNode > orderExprList, SqlNode offset, SqlNode fetch)
void convertSelectList(Blackboard bb, SqlSelect select, List< SqlNode > orderList)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectList ( Blackboard  bb,
SqlSelect  select,
List< SqlNode >  orderList 
)
inlineprivate

Definition at line 3958 of file SqlToRelConverter.java.

References i, and setup.root.

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

3959  {
3960  SqlNodeList selectList = select.getSelectList();
3961  selectList = validator.expandStar(selectList, select, false);
3962 
3963  replaceSubQueries(bb, selectList, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
3964 
3965  List<String> fieldNames = new ArrayList<>();
3966  final List<RexNode> exprs = new ArrayList<>();
3967  final Collection<String> aliases = new TreeSet<>();
3968 
3969  // Project any system fields. (Must be done before regular select items,
3970  // because offsets may be affected.)
3971  final List<SqlMonotonicity> columnMonotonicityList = new ArrayList<>();
3972  extraSelectItems(bb, select, exprs, fieldNames, aliases, columnMonotonicityList);
3973 
3974  // Project select clause.
3975  int i = -1;
3976  for (SqlNode expr : selectList) {
3977  ++i;
3978  exprs.add(bb.convertExpression(expr));
3979  fieldNames.add(deriveAlias(expr, aliases, i));
3980  }
3981 
3982  // Project extra fields for sorting.
3983  for (SqlNode expr : orderList) {
3984  ++i;
3985  SqlNode expr2 = validator.expandOrderExpr(select, expr);
3986  exprs.add(bb.convertExpression(expr2));
3987  fieldNames.add(deriveAlias(expr, aliases, i));
3988  }
3989 
3990  fieldNames = SqlValidatorUtil.uniquify(
3991  fieldNames, catalogReader.nameMatcher().isCaseSensitive());
3992 
3993  relBuilder.push(bb.root).projectNamed(exprs, fieldNames, true);
3994  bb.setRoot(relBuilder.build(), false);
3995 
3996  assert bb.columnMonotonicities.isEmpty();
3997  bb.columnMonotonicities.addAll(columnMonotonicityList);
3998  for (SqlNode selectItem : selectList) {
3999  bb.columnMonotonicities.add(selectItem.getMonotonicity(bb.scope));
4000  }
4001  }
void extraSelectItems(Blackboard bb, SqlSelect select, List< RexNode > exprList, List< String > nameList, Collection< String > aliasList, List< SqlMonotonicity > columnMonotonicityList)
tuple root
Definition: setup.in.py:14
String deriveAlias(final SqlNode node, Collection< String > aliases, final int ordinal)
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertSetOp ( SqlCall  call)
inlineprotected

Converts a set operation (UNION, INTERSECT, MINUS) into relational expressions.

Parameters
callCall to set operator
Returns
Relational expression

Definition at line 3269 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.all(), org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(), and UNION.

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

3269  {
3270  final RelNode left = convertQueryRecursive(call.operand(0), false, null).project();
3271  final RelNode right = convertQueryRecursive(call.operand(1), false, null).project();
3272  switch (call.getKind()) {
3273  case UNION:
3274  return LogicalUnion.create(ImmutableList.of(left, right), all(call));
3275 
3276  case INTERSECT:
3277  return LogicalIntersect.create(ImmutableList.of(left, right), all(call));
3278 
3279  case EXCEPT:
3280  return LogicalMinus.create(ImmutableList.of(left, right), all(call));
3281 
3282  default:
3283  throw Util.unexpected(call.getKind());
3284  }
3285  }
RelRoot convertQueryRecursive(SqlNode query, boolean top, RelDataType targetRowType)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertTemporalTable ( Blackboard  bb,
SqlCall  call 
)
inlineprivate

Definition at line 2402 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(), and setup.root.

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

2402  {
2403  final SqlSnapshot snapshot = (SqlSnapshot) call;
2404  final RexNode period = bb.convertExpression(snapshot.getPeriod());
2405 
2406  // convert inner query, could be a table name or a derived table
2407  SqlNode expr = snapshot.getTableRef();
2408  convertFrom(bb, expr);
2409 
2410  final RelNode snapshotRel = relBuilder.push(bb.root).snapshot(period).build();
2411 
2412  bb.setRoot(snapshotRel, false);
2413  }
tuple root
Definition: setup.in.py:14
void convertFrom(Blackboard bb, SqlNode from)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertToSingleValueSubq ( SqlNode  query,
RelNode  plan 
)
inline

Converts the RelNode tree for a select statement to a select that produces a single value.

Parameters
querythe query
planthe original RelNode tree corresponding to the statement
Returns
the converted RelNode tree

Definition at line 1391 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.cluster.

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

1391  {
1392  // Check whether query is guaranteed to produce a single value.
1393  if (query instanceof SqlSelect) {
1394  SqlSelect select = (SqlSelect) query;
1395  SqlNodeList selectList = select.getSelectList();
1396  SqlNodeList groupList = select.getGroup();
1397 
1398  if ((selectList.size() == 1) && ((groupList == null) || (groupList.size() == 0))) {
1399  SqlNode selectExpr = selectList.get(0);
1400  if (selectExpr instanceof SqlCall) {
1401  SqlCall selectExprCall = (SqlCall) selectExpr;
1402  if (Util.isSingleValue(selectExprCall)) {
1403  return plan;
1404  }
1405  }
1406 
1407  // If there is a limit with 0 or 1,
1408  // it is ensured to produce a single value
1409  if (select.getFetch() != null && select.getFetch() instanceof SqlNumericLiteral) {
1410  SqlNumericLiteral limitNum = (SqlNumericLiteral) select.getFetch();
1411  if (((BigDecimal) limitNum.getValue()).intValue() < 2) {
1412  return plan;
1413  }
1414  }
1415  }
1416  } else if (query instanceof SqlCall) {
1417  // If the query is (values ...),
1418  // it is necessary to look into the operands to determine
1419  // whether SingleValueAgg is necessary
1420  SqlCall exprCall = (SqlCall) query;
1421  if (exprCall.getOperator() instanceof SqlValuesOperator
1422  && Util.isSingleValue(exprCall)) {
1423  return plan;
1424  }
1425  }
1426 
1427  // If not, project SingleValueAgg
1428  return RelOptUtil.createSingleValueAggRel(cluster, plan);
1429  }

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertUnnest ( Blackboard  bb,
SqlCall  call,
List< String >  fieldNames 
)
inlineprivate

Definition at line 2116 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.cluster, org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries(), setup.root, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.root, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

2116  {
2117  final List<SqlNode> nodes = call.getOperandList();
2118  final SqlUnnestOperator operator = (SqlUnnestOperator) call.getOperator();
2119  for (SqlNode node : nodes) {
2120  replaceSubQueries(bb, node, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2121  }
2122  final List<RexNode> exprs = new ArrayList<>();
2123  for (Ord<SqlNode> node : Ord.zip(nodes)) {
2124  exprs.add(relBuilder.alias(
2125  bb.convertExpression(node.e), validator.deriveAlias(node.e, node.i)));
2126  }
2127  RelNode child = (null != bb.root) ? bb.root : LogicalValues.createOneRow(cluster);
2128  RelNode uncollect;
2129  if (validator.config().sqlConformance().allowAliasUnnestItems()) {
2130  uncollect = relBuilder.push(child)
2131  .project(exprs)
2132  .uncollect(fieldNames, operator.withOrdinality)
2133  .build();
2134  } else {
2135  // REVIEW danny 2020-04-26: should we unify the normal field aliases and the
2136  // item aliases ?
2137  uncollect = relBuilder.push(child)
2138  .project(exprs)
2139  .uncollect(Collections.emptyList(), operator.withOrdinality)
2140  .rename(fieldNames)
2141  .build();
2142  }
2143  bb.setRoot(uncollect, true);
2144  }
tuple root
Definition: setup.in.py:14
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertUpdate ( SqlUpdate  call)
inlineprivate

Definition at line 3645 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader, org.apache.calcite.sql2rel.SqlToRelConverter.convertSelect(), org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard(), field(), org.apache.calcite.sql2rel.SqlToRelConverter.getTargetTable(), org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries(), org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory, and UPDATE.

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

3645  {
3646  final SqlValidatorScope scope = validator.getWhereScope(call.getSourceSelect());
3647  Blackboard bb = createBlackboard(scope, null, false);
3648 
3649  replaceSubQueries(bb, call, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
3650 
3651  RelOptTable targetTable = getTargetTable(call);
3652 
3653  // convert update column list from SqlIdentifier to String
3654  final List<String> targetColumnNameList = new ArrayList<>();
3655  final RelDataType targetRowType = targetTable.getRowType();
3656  for (SqlNode node : call.getTargetColumnList()) {
3657  SqlIdentifier id = (SqlIdentifier) node;
3658  RelDataTypeField field = SqlValidatorUtil.getTargetField(
3659  targetRowType, typeFactory, id, catalogReader, targetTable);
3660  assert field != null : "column " + id.toString() + " not found";
3661  targetColumnNameList.add(field.getName());
3662  }
3663 
3664  RelNode sourceRel = convertSelect(call.getSourceSelect(), false);
3665 
3666  bb.setRoot(sourceRel, false);
3667  Builder<RexNode> rexNodeSourceExpressionListBuilder = ImmutableList.builder();
3668  for (SqlNode n : call.getSourceExpressionList()) {
3669  RexNode rn = bb.convertExpression(n);
3670  rexNodeSourceExpressionListBuilder.add(rn);
3671  }
3672 
3673  return LogicalTableModify.create(targetTable,
3674  catalogReader,
3675  sourceRel,
3677  targetColumnNameList,
3678  rexNodeSourceExpressionListBuilder.build(),
3679  false);
3680  }
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
#define UPDATE
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)
RelNode convertSelect(SqlSelect select, boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.convertUsing ( SqlValidatorNamespace  leftNamespace,
SqlValidatorNamespace  rightNamespace,
List< String >  nameList 
)
inlineprivate

Returns an expression for matching columns of a USING clause or inferred from NATURAL JOIN. "a JOIN b USING (x, y)" becomes "a.x = b.x AND a.y = b.y". Returns null if the column list is empty.

Parameters
leftNamespaceNamespace of left input to join
rightNamespaceNamespace of right input to join
nameListList of column names to join on
Returns
Expression to match columns from name list, or true if name list is empty

Definition at line 2716 of file SqlToRelConverter.java.

References field(), setup.name, and org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(), and org.apache.calcite.sql2rel.SqlToRelConverter.convertJoinCondition().

2718  {
2719  final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
2720  final List<RexNode> list = new ArrayList<>();
2721  for (String name : nameList) {
2722  List<RexNode> operands = new ArrayList<>();
2723  int offset = 0;
2724  for (SqlValidatorNamespace n : ImmutableList.of(leftNamespace, rightNamespace)) {
2725  final RelDataType rowType = n.getRowType();
2726  final RelDataTypeField field = nameMatcher.field(rowType, name);
2727  operands.add(rexBuilder.makeInputRef(field.getType(), offset + field.getIndex()));
2728  offset += rowType.getFieldList().size();
2729  }
2730  list.add(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, operands));
2731  }
2732  return RexUtil.composeConjunction(rexBuilder, list);
2733  }
string name
Definition: setup.in.py:72
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.convertValues ( SqlCall  values,
RelDataType  targetRowType 
)
inline

Converts a SELECT statement's parse tree into a relational expression.

Definition at line 4049 of file SqlToRelConverter.java.

References setup.root.

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

4049  {
4050  final SqlValidatorScope scope = validator.getOverScope(values);
4051  assert scope != null;
4052  final Blackboard bb = createBlackboard(scope, null, false);
4053  convertValuesImpl(bb, values, targetRowType);
4054  return bb.root;
4055  }
tuple root
Definition: setup.in.py:14
void convertValuesImpl(Blackboard bb, SqlCall values, RelDataType targetRowType)
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertValuesImpl ( Blackboard  bb,
SqlCall  values,
RelDataType  targetRowType 
)
inlineprivate

Converts a values clause (as in "INSERT INTO T(x,y) VALUES (1,2)") into a relational expression.

Parameters
bbBlackboard
valuesCall to SQL VALUES operator
targetRowTypeTarget row type

Definition at line 4065 of file SqlToRelConverter.java.

References setup.root, and org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.scope.

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

4066  {
4067  // Attempt direct conversion to LogicalValues; if that fails, deal with
4068  // fancy stuff like sub-queries below.
4069  RelNode valuesRel =
4070  convertRowValues(bb, values, values.getOperandList(), true, targetRowType);
4071  if (valuesRel != null) {
4072  bb.setRoot(valuesRel, true);
4073  return;
4074  }
4075 
4076  final List<RelNode> unionRels = new ArrayList<>();
4077  for (SqlNode rowConstructor1 : values.getOperandList()) {
4078  SqlCall rowConstructor = (SqlCall) rowConstructor1;
4079  Blackboard tmpBb = createBlackboard(bb.scope, null, false);
4080  replaceSubQueries(tmpBb, rowConstructor, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
4081  final List<Pair<RexNode, String>> exps = new ArrayList<>();
4082  for (Ord<SqlNode> operand : Ord.zip(rowConstructor.getOperandList())) {
4083  exps.add(Pair.of(tmpBb.convertExpression(operand.e),
4084  validator.deriveAlias(operand.e, operand.i)));
4085  }
4086  RelNode in =
4087  (null == tmpBb.root) ? LogicalValues.createOneRow(cluster) : tmpBb.root;
4088  unionRels.add(
4089  relBuilder.push(in).project(Pair.left(exps), Pair.right(exps)).build());
4090  }
4091 
4092  if (unionRels.size() == 0) {
4093  throw new AssertionError("empty values clause");
4094  } else if (unionRels.size() == 1) {
4095  bb.setRoot(unionRels.get(0), true);
4096  } else {
4097  bb.setRoot(LogicalUnion.create(unionRels, true), true);
4098  }
4099  }
tuple root
Definition: setup.in.py:14
RelNode convertRowValues(Blackboard bb, SqlNode rowList, Collection< SqlNode > rows, boolean allowLiteralsOnly, RelDataType targetRowType)
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.convertWhere ( final Blackboard  bb,
final SqlNode  where 
)
inlineprivate

Converts a WHERE clause.

Parameters
bbBlackboard
whereWHERE clause, may be null

Definition at line 978 of file SqlToRelConverter.java.

References f, org.apache.calcite.sql2rel.SqlToRelConverter.getCorrelationUse(), org.apache.calcite.sql2rel.SqlToRelConverter.pushDownNotForIn(), test_fsi.r, org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries(), setup.root, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.scope, and org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory.

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

978  {
979  if (where == null) {
980  return;
981  }
982  SqlNode newWhere = pushDownNotForIn(bb.scope, where);
983  replaceSubQueries(bb, newWhere, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
984  final RexNode convertedWhere = bb.convertExpression(newWhere);
985  final RexNode convertedWhere2 =
986  RexUtil.removeNullabilityCast(typeFactory, convertedWhere);
987 
988  // only allocate filter if the condition is not TRUE
989  if (convertedWhere2.isAlwaysTrue()) {
990  return;
991  }
992 
993  final RelFactories.FilterFactory filterFactory = RelFactories.DEFAULT_FILTER_FACTORY;
994  final RelNode filter =
995  filterFactory.createFilter(bb.root, convertedWhere2, ImmutableSet.of());
996  final RelNode r;
997  final CorrelationUse p = getCorrelationUse(bb, filter);
998  if (p != null) {
999  assert p.r instanceof Filter;
1000  Filter f = (Filter) p.r;
1001  r = LogicalFilter.create(f.getInput(), f.getCondition(), ImmutableSet.of(p.id));
1002  } else {
1003  r = filter;
1004  }
1005 
1006  bb.setRoot(r, false);
1007  }
static SqlNode pushDownNotForIn(SqlValidatorScope scope, SqlNode sqlNode)
tuple r
Definition: test_fsi.py:16
tuple root
Definition: setup.in.py:14
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
CorrelationUse getCorrelationUse(Blackboard bb, final RelNode r0)
char * f

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelRoot org.apache.calcite.sql2rel.SqlToRelConverter.convertWith ( SqlWith  with,
boolean  top 
)
inline

Converts a WITH sub-query into a relational expression.

Definition at line 4042 of file SqlToRelConverter.java.

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

4042  {
4043  return convertQuery(with.body, false, top);
4044  }
RelRoot convertQuery(SqlNode query, final boolean needsValidation, final boolean top)

+ Here is the caller graph for this function:

final void org.apache.calcite.sql2rel.SqlToRelConverter.createAggImpl ( Blackboard  bb,
final AggConverter  aggConverter,
SqlNodeList  selectList,
SqlNodeList  groupList,
SqlNode  having,
List< SqlNode >  orderExprList 
)
inlineprotected

Definition at line 2776 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.createAggregate(), generate_TableFunctionsFactory_init.k, org.apache.calcite.sql2rel.SqlToRelConverter.pushDownNotForIn(), test_fsi.r, org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries(), org.apache.calcite.sql2rel.SqlToRelConverter.rewriteAggregateWithGroupId(), setup.root, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.scope, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

2781  {
2782  // Find aggregate functions in SELECT and HAVING clause
2783  final AggregateFinder aggregateFinder = new AggregateFinder();
2784  selectList.accept(aggregateFinder);
2785  if (having != null) {
2786  having.accept(aggregateFinder);
2787  }
2788 
2789  // first replace the sub-queries inside the aggregates
2790  // because they will provide input rows to the aggregates.
2791  replaceSubQueries(bb, aggregateFinder.list, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2792 
2793  // also replace sub-queries inside filters in the aggregates
2795  bb, aggregateFinder.filterList, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2796 
2797  // also replace sub-queries inside ordering spec in the aggregates
2798  replaceSubQueries(bb, aggregateFinder.orderList, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2799 
2800  // If group-by clause is missing, pretend that it has zero elements.
2801  if (groupList == null) {
2802  groupList = SqlNodeList.EMPTY;
2803  }
2804 
2805  replaceSubQueries(bb, groupList, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2806 
2807  // register the group exprs
2808 
2809  // build a map to remember the projections from the top scope to the
2810  // output of the current root.
2811  //
2812  // Calcite allows expressions, not just column references in
2813  // group by list. This is not SQL 2003 compliant, but hey.
2814 
2815  final AggregatingSelectScope scope = aggConverter.aggregatingSelectScope;
2816  final AggregatingSelectScope.Resolved r = scope.resolved.get();
2817  for (SqlNode groupExpr : r.groupExprList) {
2818  aggConverter.addGroupExpr(groupExpr);
2819  }
2820 
2821  final RexNode havingExpr;
2822  final List<Pair<RexNode, String>> projects = new ArrayList<>();
2823 
2824  try {
2825  Preconditions.checkArgument(bb.agg == null, "already in agg mode");
2826  bb.agg = aggConverter;
2827 
2828  // convert the select and having expressions, so that the
2829  // agg converter knows which aggregations are required
2830 
2831  selectList.accept(aggConverter);
2832  // Assert we don't have dangling items left in the stack
2833  assert !aggConverter.inOver;
2834  for (SqlNode expr : orderExprList) {
2835  expr.accept(aggConverter);
2836  assert !aggConverter.inOver;
2837  }
2838  if (having != null) {
2839  having.accept(aggConverter);
2840  assert !aggConverter.inOver;
2841  }
2842 
2843  // compute inputs to the aggregator
2844  List<Pair<RexNode, String>> preExprs = aggConverter.getPreExprs();
2845 
2846  if (preExprs.size() == 0) {
2847  // Special case for COUNT(*), where we can end up with no inputs
2848  // at all. The rest of the system doesn't like 0-tuples, so we
2849  // select a dummy constant here.
2850  final RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.ZERO);
2851  preExprs = ImmutableList.of(Pair.of(zero, (String) null));
2852  }
2853 
2854  final RelNode inputRel = bb.root;
2855 
2856  // Project the expressions required by agg and having.
2857  bb.setRoot(relBuilder.push(inputRel)
2858  .projectNamed(Pair.left(preExprs), Pair.right(preExprs), false)
2859  .build(),
2860  false);
2861  bb.mapRootRelToFieldProjection.put(bb.root, r.groupExprProjection);
2862 
2863  // REVIEW jvs 31-Oct-2007: doesn't the declaration of
2864  // monotonicity here assume sort-based aggregation at
2865  // the physical level?
2866 
2867  // Tell bb which of group columns are sorted.
2868  bb.columnMonotonicities.clear();
2869  for (SqlNode groupItem : groupList) {
2870  bb.columnMonotonicities.add(bb.scope.getMonotonicity(groupItem));
2871  }
2872 
2873  final RelNode relNode = aggConverter.containsGroupId()
2874  ? rewriteAggregateWithGroupId(bb, r, aggConverter)
2875  : createAggregate(bb, r.groupSet, r.groupSets, aggConverter.getAggCalls());
2876 
2877  bb.setRoot(relNode, false);
2878  bb.mapRootRelToFieldProjection.put(bb.root, r.groupExprProjection);
2879 
2880  // Replace sub-queries in having here and modify having to use
2881  // the replaced expressions
2882  if (having != null) {
2883  SqlNode newHaving = pushDownNotForIn(bb.scope, having);
2884  replaceSubQueries(bb, newHaving, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
2885  havingExpr = bb.convertExpression(newHaving);
2886  } else {
2887  havingExpr = relBuilder.literal(true);
2888  }
2889 
2890  // Now convert the other sub-queries in the select list.
2891  // This needs to be done separately from the sub-query inside
2892  // any aggregate in the select list, and after the aggregate rel
2893  // is allocated.
2894  replaceSubQueries(bb, selectList, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2895 
2896  // Now sub-queries in the entire select list have been converted.
2897  // Convert the select expressions to get the final list to be
2898  // projected.
2899  int k = 0;
2900 
2901  // For select expressions, use the field names previously assigned
2902  // by the validator. If we derive afresh, we might generate names
2903  // like "EXPR$2" that don't match the names generated by the
2904  // validator. This is especially the case when there are system
2905  // fields; system fields appear in the relnode's rowtype but do not
2906  // (yet) appear in the validator type.
2907  final SelectScope selectScope = SqlValidatorUtil.getEnclosingSelectScope(bb.scope);
2908  assert selectScope != null;
2909  final SqlValidatorNamespace selectNamespace =
2910  validator.getNamespace(selectScope.getNode());
2911  final List<String> names = selectNamespace.getRowType().getFieldNames();
2912  int sysFieldCount = selectList.size() - names.size();
2913  for (SqlNode expr : selectList) {
2914  projects.add(Pair.of(bb.convertExpression(expr),
2915  k < sysFieldCount ? validator.deriveAlias(expr, k++)
2916  : names.get(k++ - sysFieldCount)));
2917  }
2918 
2919  for (SqlNode expr : orderExprList) {
2920  projects.add(
2921  Pair.of(bb.convertExpression(expr), validator.deriveAlias(expr, k++)));
2922  }
2923  } finally {
2924  bb.agg = null;
2925  }
2926 
2927  // implement HAVING (we have already checked that it is non-trivial)
2928  relBuilder.push(bb.root);
2929  if (havingExpr != null) {
2930  relBuilder.filter(havingExpr);
2931  }
2932 
2933  // implement the SELECT list
2934  relBuilder.project(Pair.left(projects), Pair.right(projects))
2935  .rename(Pair.right(projects));
2936  bb.setRoot(relBuilder.build(), false);
2937 
2938  // Tell bb which of group columns are sorted.
2939  bb.columnMonotonicities.clear();
2940  for (SqlNode selectItem : selectList) {
2941  bb.columnMonotonicities.add(bb.scope.getMonotonicity(selectItem));
2942  }
2943  }
static SqlNode pushDownNotForIn(SqlValidatorScope scope, SqlNode sqlNode)
tuple r
Definition: test_fsi.py:16
tuple root
Definition: setup.in.py:14
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
RelNode createAggregate(Blackboard bb, ImmutableBitSet groupSet, ImmutableList< ImmutableBitSet > groupSets, List< AggregateCall > aggCalls)
RelNode rewriteAggregateWithGroupId(Blackboard bb, AggregatingSelectScope.Resolved r, AggConverter converter)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.createAggregate ( Blackboard  bb,
ImmutableBitSet  groupSet,
ImmutableList< ImmutableBitSet >  groupSets,
List< AggregateCall >  aggCalls 
)
inlineprotected

Creates an Aggregate.

In case the aggregate rel changes the order in which it projects fields, the groupExprProjection parameter is provided, and the implementation of this method may modify it.

The sortedCount parameter is the number of expressions known to be monotonic. These expressions must be on the leading edge of the grouping keys. The default implementation of this method ignores this parameter.

Parameters
bbBlackboard
groupSetBit set of ordinals of grouping columns
groupSetsGrouping sets
aggCallsArray of calls to aggregate functions
Returns
LogicalAggregate

Definition at line 3065 of file SqlToRelConverter.java.

References setup.root.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.createAggImpl(), org.apache.calcite.sql2rel.SqlToRelConverter.distinctify(), and org.apache.calcite.sql2rel.SqlToRelConverter.rewriteAggregateWithGroupId().

3068  {
3069  return LogicalAggregate.create(
3070  bb.root, ImmutableList.of(), groupSet, groupSets, aggCalls);
3071  }
tuple root
Definition: setup.in.py:14

+ Here is the caller graph for this function:

Blackboard org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard ( SqlValidatorScope  scope,
Map< String, RexNode >  nameToNodeMap,
boolean  top 
)
inlineprotected
Blackboard org.apache.calcite.sql2rel.SqlToRelConverter.createInsertBlackboard ( RelOptTable  targetTable,
RexNode  sourceRef,
List< String >  targetColumnNames 
)
inlineprivate

Creates a blackboard for translating the expressions of generated columns in an INSERT statement.

Definition at line 3515 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard(), i, and generate_TableFunctionsFactory_init.j.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.collectInsertTargets(), org.apache.calcite.sql2rel.SqlToRelConverter.convertColumnList(), and org.apache.calcite.sql2rel.SqlToRelConverter.toRel().

3516  {
3517  final Map<String, RexNode> nameToNodeMap = new HashMap<>();
3518  int j = 0;
3519 
3520  // Assign expressions for non-generated columns.
3521  final List<ColumnStrategy> strategies = targetTable.getColumnStrategies();
3522  final List<String> targetFields = targetTable.getRowType().getFieldNames();
3523  for (String targetColumnName : targetColumnNames) {
3524  final int i = targetFields.indexOf(targetColumnName);
3525  switch (strategies.get(i)) {
3526  case STORED:
3527  case VIRTUAL:
3528  break;
3529  default:
3530  nameToNodeMap.put(targetColumnName, rexBuilder.makeFieldAccess(sourceRef, j++));
3531  }
3532  }
3533  return createBlackboard(null, nameToNodeMap, false);
3534  }
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.createJoin ( Blackboard  bb,
RelNode  leftRel,
RelNode  rightRel,
RexNode  joinCond,
JoinRelType  joinType 
)
inlineprotected

Definition at line 2454 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.getCorrelationUse(), org.apache.calcite.sql2rel.SqlToRelConverter.leaves, test_fsi.r, and org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder.

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

2458  {
2459  assert joinCond != null;
2460 
2461  final CorrelationUse p = getCorrelationUse(bb, rightRel);
2462  if (p != null) {
2463  RelNode innerRel = p.r;
2464  ImmutableBitSet requiredCols = p.requiredColumns;
2465 
2466  if (!joinCond.isAlwaysTrue()) {
2467  final RelFactories.FilterFactory factory = RelFactories.DEFAULT_FILTER_FACTORY;
2468  final RexCorrelVariable rexCorrel =
2469  (RexCorrelVariable) rexBuilder.makeCorrel(leftRel.getRowType(), p.id);
2470  final RexAccessShuttle shuttle = new RexAccessShuttle(rexBuilder, rexCorrel);
2471 
2472  // Replace outer RexInputRef with RexFieldAccess,
2473  // and push lateral join predicate into inner child
2474  final RexNode newCond = joinCond.accept(shuttle);
2475  innerRel = factory.createFilter(p.r, newCond, ImmutableSet.of());
2476  requiredCols =
2477  ImmutableBitSet.fromBitSet(shuttle.varCols).union(p.requiredColumns);
2478  }
2479 
2480  return LogicalCorrelate.create(leftRel, innerRel, p.id, requiredCols, joinType);
2481  }
2482 
2483  // MAt 8 Jan 2021 original code
2484  // final RelNode node = relBuilder.push(leftRel).push(rightRel).join(joinType,
2485  // joinCond).build();
2486 
2487  // MAT 8 Jan 2021 this is Omnisci code
2488  final Join originalJoin = (Join) RelFactories.DEFAULT_JOIN_FACTORY.createJoin(leftRel,
2489  rightRel,
2490  ImmutableList.of(),
2491  joinCond,
2492  ImmutableSet.of(),
2493  joinType,
2494  false);
2495  RelNode node = originalJoin;
2496  // MAT 08 Jan 2021 end of OmniSci Code
2497 
2498  // If join conditions are pushed down, update the leaves.
2499  if (node instanceof Project) {
2500  final Join newJoin = (Join) node.getInputs().get(0);
2501  if (leaves.containsKey(leftRel)) {
2502  leaves.put(newJoin.getLeft(), leaves.get(leftRel));
2503  }
2504  if (leaves.containsKey(rightRel)) {
2505  leaves.put(newJoin.getRight(), leaves.get(rightRel));
2506  }
2507  }
2508  return node;
2509  }
tuple r
Definition: test_fsi.py:16
CorrelationUse getCorrelationUse(Blackboard bb, final RelNode r0)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.createModify ( RelOptTable  targetTable,
RelNode  source 
)
inlineprivate

Creates a relational expression to modify a table or modifiable view.

Definition at line 3304 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader, org.apache.calcite.sql2rel.SqlToRelConverter.cluster, org.apache.calcite.sql2rel.SqlToRelConverter.createSource(), INSERT, and org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory.

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

3304  {
3305  final ModifiableTable modifiableTable = targetTable.unwrap(ModifiableTable.class);
3306  if (modifiableTable != null && modifiableTable == targetTable.unwrap(Table.class)) {
3307  return modifiableTable.toModificationRel(cluster,
3308  targetTable,
3309  catalogReader,
3310  source,
3312  null,
3313  null,
3314  false);
3315  }
3316  final ModifiableView modifiableView = targetTable.unwrap(ModifiableView.class);
3317  if (modifiableView != null) {
3318  final Table delegateTable = modifiableView.getTable();
3319  final RelDataType delegateRowType = delegateTable.getRowType(typeFactory);
3320  final RelOptTable delegateRelOptTable = RelOptTableImpl.create(
3321  null, delegateRowType, delegateTable, modifiableView.getTablePath());
3322  final RelNode newSource =
3323  createSource(targetTable, source, modifiableView, delegateRowType);
3324  return createModify(delegateRelOptTable, newSource);
3325  }
3326  return LogicalTableModify.create(targetTable,
3327  catalogReader,
3328  source,
3330  null,
3331  null,
3332  false);
3333  }
#define INSERT
RelNode createSource(RelOptTable targetTable, RelNode source, ModifiableView modifiableView, RelDataType delegateRowType)
RelNode createModify(RelOptTable targetTable, RelNode source)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.createSource ( RelOptTable  targetTable,
RelNode  source,
ModifiableView  modifiableView,
RelDataType  delegateRowType 
)
inlineprivate

Wraps a relational expression in the projects and filters implied by a ModifiableView.

The input relational expression is suitable for inserting into the view, and the returned relational expression is suitable for inserting into its delegate table.

In principle, the delegate table of a view might be another modifiable view, and if so, the process can be repeated.

Definition at line 3348 of file SqlToRelConverter.java.

References field(), i, and org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder.

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

3351  {
3352  final ImmutableIntList mapping = modifiableView.getColumnMapping();
3353  assert mapping.size() == targetTable.getRowType().getFieldCount();
3354 
3355  // For columns represented in the mapping, the expression is just a field
3356  // reference.
3357  final Map<Integer, RexNode> projectMap = new HashMap<>();
3358  final List<RexNode> filters = new ArrayList<>();
3359  for (int i = 0; i < mapping.size(); i++) {
3360  int target = mapping.get(i);
3361  if (target >= 0) {
3362  projectMap.put(target, RexInputRef.of(i, source.getRowType()));
3363  }
3364  }
3365 
3366  // For columns that are not in the mapping, and have a constraint of the
3367  // form "column = value", the expression is the literal "value".
3368  //
3369  // If a column has multiple constraints, the extra ones will become a
3370  // filter.
3371  final RexNode constraint = modifiableView.getConstraint(rexBuilder, delegateRowType);
3372  RelOptUtil.inferViewPredicates(projectMap, filters, constraint);
3373  final List<Pair<RexNode, String>> projects = new ArrayList<>();
3374  for (RelDataTypeField field : delegateRowType.getFieldList()) {
3375  RexNode node = projectMap.get(field.getIndex());
3376  if (node == null) {
3377  node = rexBuilder.makeNullLiteral(field.getType());
3378  }
3379  projects.add(Pair.of(
3380  rexBuilder.ensureType(field.getType(), node, false), field.getName()));
3381  }
3382 
3383  return relBuilder.push(source)
3384  .projectNamed(Pair.left(projects), Pair.right(projects), false)
3385  .filter(filters)
3386  .build();
3387  }
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelOptTable.ToRelContext org.apache.calcite.sql2rel.SqlToRelConverter.createToRelContext ( List< RelHint >  hints)
inlineprivate

Definition at line 3389 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.cluster, and org.apache.calcite.sql2rel.SqlToRelConverter.viewExpander.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.flattenTypes(), and org.apache.calcite.sql2rel.SqlToRelConverter.toRel().

3389  {
3390  return ViewExpanders.toRelContext(viewExpander, cluster, hints);
3391  }

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.decorrelate ( SqlNode  query,
RelNode  rootRel 
)
inline

If sub-query is correlated and decorrelation is enabled, performs decorrelation.

Parameters
queryQuery
rootRelRoot relational expression
Returns
New root relational expression after decorrelation

Definition at line 491 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.checkConvertedType(), org.apache.calcite.sql2rel.SqlToRelConverter.config, org.apache.calcite.sql2rel.SqlToRelConverter.decorrelateQuery(), and run_benchmark_import.result.

491  {
492  if (!config.isDecorrelationEnabled()) {
493  return rootRel;
494  }
495  final RelNode result = decorrelateQuery(rootRel);
496  if (result != rootRel) {
497  checkConvertedType(query, result);
498  }
499  return result;
500  }
void checkConvertedType(SqlNode query, RelNode result)

+ Here is the call graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.decorrelateQuery ( RelNode  rootRel)
inlineprotected

Definition at line 3212 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.relBuilder.

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

3212  {
3213  return RelDecorrelator.decorrelateQuery(rootRel, relBuilder);
3214  }

+ Here is the caller graph for this function:

String org.apache.calcite.sql2rel.SqlToRelConverter.deriveAlias ( final SqlNode  node,
Collection< String >  aliases,
final int  ordinal 
)
inlineprivate

Definition at line 4023 of file SqlToRelConverter.java.

References generate_TableFunctionsFactory_init.j.

4024  {
4025  String alias = validator.deriveAlias(node, ordinal);
4026  if ((alias == null) || aliases.contains(alias)) {
4027  String aliasBase = (alias == null) ? "EXPR$" : alias;
4028  for (int j = 0;; j++) {
4029  alias = aliasBase + j;
4030  if (!aliases.contains(alias)) {
4031  break;
4032  }
4033  }
4034  }
4035  aliases.add(alias);
4036  return alias;
4037  }
static boolean org.apache.calcite.sql2rel.SqlToRelConverter.desc ( RelFieldCollation.Direction  direction)
inlinestaticprivate

Definition at line 3195 of file SqlToRelConverter.java.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertMatchRecognize(), and org.apache.calcite.sql2rel.SqlToRelConverter.convertOrderItem().

3195  {
3196  switch (direction) {
3197  case DESCENDING:
3198  case STRICTLY_DESCENDING:
3199  return true;
3200  default:
3201  return false;
3202  }
3203  }

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.distinctify ( Blackboard  bb,
boolean  checkForDupExprs 
)
inlineprivate

Having translated 'SELECT ... FROM ... [GROUP BY ...] [HAVING ...]', adds a relational expression to make the results unique.

If the SELECT clause contains duplicate expressions, adds org.apache.calcite.rel.logical.LogicalProjects so that we are grouping on the minimal set of keys. The performance gain isn't huge, but it is difficult to detect these duplicate expressions later.

Parameters
bbBlackboard
checkForDupExprsCheck for duplicate expressions

Definition at line 721 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.createAggregate(), field(), i, and setup.root.

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

721  {
722  // Look for duplicate expressions in the project.
723  // Say we have 'select x, y, x, z'.
724  // Then dups will be {[2, 0]}
725  // and oldToNew will be {[0, 0], [1, 1], [2, 0], [3, 2]}
726  RelNode rel = bb.root;
727  if (checkForDupExprs && (rel instanceof LogicalProject)) {
728  LogicalProject project = (LogicalProject) rel;
729  final List<RexNode> projectExprs = project.getProjects();
730  final List<Integer> origins = new ArrayList<>();
731  int dupCount = 0;
732  for (int i = 0; i < projectExprs.size(); i++) {
733  int x = projectExprs.indexOf(projectExprs.get(i));
734  if (x >= 0 && x < i) {
735  origins.add(x);
736  ++dupCount;
737  } else {
738  origins.add(i);
739  }
740  }
741  if (dupCount == 0) {
742  distinctify(bb, false);
743  return;
744  }
745 
746  final Map<Integer, Integer> squished = new HashMap<>();
747  final List<RelDataTypeField> fields = rel.getRowType().getFieldList();
748  final List<Pair<RexNode, String>> newProjects = new ArrayList<>();
749  for (int i = 0; i < fields.size(); i++) {
750  if (origins.get(i) == i) {
751  squished.put(i, newProjects.size());
752  newProjects.add(RexInputRef.of2(i, fields));
753  }
754  }
755  rel = LogicalProject.create(
756  rel, ImmutableList.of(), Pair.left(newProjects), Pair.right(newProjects));
757  bb.root = rel;
758  distinctify(bb, false);
759  rel = bb.root;
760 
761  // Create the expressions to reverse the mapping.
762  // Project($0, $1, $0, $2).
763  final List<Pair<RexNode, String>> undoProjects = new ArrayList<>();
764  for (int i = 0; i < fields.size(); i++) {
765  final int origin = origins.get(i);
766  RelDataTypeField field = fields.get(i);
767  undoProjects.add(Pair.of(
768  new RexInputRef(squished.get(origin), field.getType()), field.getName()));
769  }
770 
771  rel = LogicalProject.create(
772  rel, ImmutableList.of(), Pair.left(undoProjects), Pair.right(undoProjects));
773  bb.setRoot(rel, false);
774 
775  return;
776  }
777 
778  // Usual case: all of the expressions in the SELECT clause are
779  // different.
780  final ImmutableBitSet groupSet =
781  ImmutableBitSet.range(rel.getRowType().getFieldCount());
782  rel = createAggregate(bb, groupSet, ImmutableList.of(groupSet), ImmutableList.of());
783 
784  bb.setRoot(rel, false);
785  }
tuple root
Definition: setup.in.py:14
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
void distinctify(Blackboard bb, boolean checkForDupExprs)
RelNode createAggregate(Blackboard bb, ImmutableBitSet groupSet, ImmutableList< ImmutableBitSet > groupSets, List< AggregateCall > aggCalls)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

boolean org.apache.calcite.sql2rel.SqlToRelConverter.enableDecorrelation ( )
inlineprotected

Definition at line 3206 of file SqlToRelConverter.java.

3206  {
3207  // disable sub-query decorrelation when needed.
3208  // e.g. if outer joins are not supported.
3209  return config.isDecorrelationEnabled();
3210  }
RexNode org.apache.calcite.sql2rel.SqlToRelConverter.ensureSqlType ( RelDataType  type,
RexNode  node 
)
inlineprivate

Ensures that an expression has a given SqlTypeName, applying a cast if necessary. If the expression already has the right type family, returns the expression unchanged.

Definition at line 1493 of file SqlToRelConverter.java.

References run_benchmark_import.type.

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

1493  {
1494  if (type.getSqlTypeName() == node.getType().getSqlTypeName()
1495  || (type.getSqlTypeName() == SqlTypeName.VARCHAR
1496  && node.getType().getSqlTypeName() == SqlTypeName.CHAR)) {
1497  return node;
1498  }
1499  return rexBuilder.ensureType(type, node, true);
1500  }

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.extraSelectItems ( Blackboard  bb,
SqlSelect  select,
List< RexNode >  exprList,
List< String >  nameList,
Collection< String >  aliasList,
List< SqlMonotonicity >  columnMonotonicityList 
)
inlineprotected

Adds extra select items. The default implementation adds nothing; derived classes may add columns to exprList, nameList, aliasList and columnMonotonicityList.

Parameters
bbBlackboard
selectSelect statement being translated
exprListList of expressions in select clause
nameListList of names, one per column
aliasListCollection of aliases that have been used already
columnMonotonicityListList of monotonicity, one per column

Definition at line 4016 of file SqlToRelConverter.java.

4021  {}
void org.apache.calcite.sql2rel.SqlToRelConverter.findSubQueries ( Blackboard  bb,
SqlNode  node,
RelOptUtil.Logic  logic,
boolean  registerOnlyScalarSubQueries 
)
inlineprivate

Builds a list of all IN or EXISTS operators inside SQL parse tree. Does not traverse inside queries.

Parameters
bbblackboard
nodethe SQL parse tree
logicWhether the answer needs to be in full 3-valued logic (TRUE, FALSE, UNKNOWN) will be required, or whether we can accept an approximation (say representing UNKNOWN as FALSE)
registerOnlyScalarSubQueriesif set to true and the parse tree corresponds to a variation of a select node, only register it if it's a scalar sub-query

Definition at line 1708 of file SqlToRelConverter.java.

References ALL, AND, CURSOR, EXISTS, IN, NOT, SELECT, SOME, and run_benchmark_import.type.

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

1711  {
1712  final SqlKind kind = node.getKind();
1713  switch (kind) {
1714  case EXISTS:
1715  case SELECT:
1716  case MULTISET_QUERY_CONSTRUCTOR:
1717  case MULTISET_VALUE_CONSTRUCTOR:
1718  case ARRAY_QUERY_CONSTRUCTOR:
1719  case CURSOR:
1720  case SCALAR_QUERY:
1721  if (!registerOnlyScalarSubQueries || (kind == SqlKind.SCALAR_QUERY)) {
1722  bb.registerSubQuery(node, RelOptUtil.Logic.TRUE_FALSE);
1723  }
1724  return;
1725  case IN:
1726  break;
1727  case NOT_IN:
1728  case NOT:
1729  logic = logic.negate();
1730  break;
1731  }
1732  if (node instanceof SqlCall) {
1733  switch (kind) {
1734  // Do no change logic for AND, IN and NOT IN expressions;
1735  // but do change logic for OR, NOT and others;
1736  // EXISTS was handled already.
1737  case AND:
1738  case IN:
1739  case NOT_IN:
1740  break;
1741  default:
1742  logic = RelOptUtil.Logic.TRUE_FALSE_UNKNOWN;
1743  break;
1744  }
1745  for (SqlNode operand : ((SqlCall) node).getOperandList()) {
1746  if (operand != null) {
1747  // In the case of an IN expression, locate scalar
1748  // sub-queries so we can convert them to constants
1749  findSubQueries(bb,
1750  operand,
1751  logic,
1752  kind == SqlKind.IN || kind == SqlKind.NOT_IN || kind == SqlKind.SOME
1753  || kind == SqlKind.ALL || registerOnlyScalarSubQueries);
1754  }
1755  }
1756  } else if (node instanceof SqlNodeList) {
1757  for (SqlNode child : (SqlNodeList) node) {
1758  findSubQueries(bb,
1759  child,
1760  logic,
1761  kind == SqlKind.IN || kind == SqlKind.NOT_IN || kind == SqlKind.SOME
1762  || kind == SqlKind.ALL || registerOnlyScalarSubQueries);
1763  }
1764  }
1765 
1766  // Now that we've located any scalar sub-queries inside the IN
1767  // expression, register the IN expression itself. We need to
1768  // register the scalar sub-queries first so they can be converted
1769  // before the IN expression is converted.
1770  switch (kind) {
1771  case IN:
1772  case NOT_IN:
1773  case SOME:
1774  case ALL:
1775  switch (logic) {
1776  case TRUE_FALSE_UNKNOWN:
1777  RelDataType type = validator.getValidatedNodeTypeIfKnown(node);
1778  if (type == null) {
1779  // The node might not be validated if we still don't know type of the node.
1780  // Therefore return directly.
1781  return;
1782  } else {
1783  break;
1784  }
1785  // fall through
1786  case UNKNOWN_AS_FALSE:
1787  logic = RelOptUtil.Logic.TRUE;
1788  }
1789  bb.registerSubQuery(node, logic);
1790  break;
1791  }
1792  }
#define NOT
#define CURSOR
#define EXISTS
#define IN
#define SOME
void findSubQueries(Blackboard bb, SqlNode node, RelOptUtil.Logic logic, boolean registerOnlyScalarSubQueries)
#define AND
#define ALL
#define SELECT

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.flattenTypes ( RelNode  rootRel,
boolean  restructure 
)
inline

Definition at line 477 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.createToRelContext(), org.apache.calcite.sql2rel.SqlToRelConverter.relBuilder, and org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder.

477  {
478  RelStructuredTypeFlattener typeFlattener = new RelStructuredTypeFlattener(
479  relBuilder, rexBuilder, createToRelContext(ImmutableList.of()), restructure);
480  return typeFlattener.rewrite(rootRel);
481  }
RelOptTable.ToRelContext createToRelContext(List< RelHint > hints)

+ Here is the call graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.gatherOrderExprs ( Blackboard  bb,
SqlSelect  select,
SqlNodeList  orderList,
List< SqlNode >  extraOrderExprs,
List< RelFieldCollation >  collationList 
)
inlineprotected

Creates a list of collations required to implement the ORDER BY clause, if there is one. Populates extraOrderExprs with any sort expressions which are not in the select clause.

Parameters
bbScope within which to resolve identifiers
selectSelect clause. Never null, because we invent a dummy SELECT if ORDER BY is applied to a set operation (UNION etc.)
orderListOrder by clause, may be null
extraOrderExprsSort expressions which are not in the select clause (output)
collationListList of collations (output)

Definition at line 3101 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.convertOrderItem(), and org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.top.

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

3105  {
3106  // TODO: add validation rules to SqlValidator also
3107  assert bb.root != null : "precondition: child != null";
3108  assert select != null;
3109  if (orderList == null) {
3110  return;
3111  }
3112 
3113  if (!bb.top) {
3114  SqlNode offset = select.getOffset();
3115  if ((offset == null
3116  || (offset instanceof SqlLiteral
3117  && ((SqlLiteral) offset)
3118  .bigDecimalValue()
3119  .equals(BigDecimal.ZERO)))
3120  && select.getFetch() == null) {
3121  return;
3122  }
3123  }
3124 
3125  for (SqlNode orderItem : orderList) {
3126  collationList.add(convertOrderItem(select,
3127  orderItem,
3128  extraOrderExprs,
3129  RelFieldCollation.Direction.ASCENDING,
3130  RelFieldCollation.NullDirection.UNSPECIFIED));
3131  }
3132  }
RelFieldCollation convertOrderItem(SqlSelect select, SqlNode orderItem, List< SqlNode > extraExprs, RelFieldCollation.Direction direction, RelFieldCollation.NullDirection nullDirection)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelOptCluster org.apache.calcite.sql2rel.SqlToRelConverter.getCluster ( )
inline

Returns the RelOptCluster in use.

Definition at line 353 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.cluster.

353  {
354  return cluster;
355  }
Set<RelColumnMapping> org.apache.calcite.sql2rel.SqlToRelConverter.getColumnMappings ( SqlOperator  op)
inlineprivate

Definition at line 2415 of file SqlToRelConverter.java.

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

2415  {
2416  SqlReturnTypeInference rti = op.getReturnTypeInference();
2417  if (rti == null) {
2418  return null;
2419  }
2420  if (rti instanceof TableFunctionReturnTypeInference) {
2421  TableFunctionReturnTypeInference tfrti = (TableFunctionReturnTypeInference) rti;
2422  return tfrti.getColumnMappings();
2423  } else {
2424  return null;
2425  }
2426  }

+ Here is the caller graph for this function:

CorrelationUse org.apache.calcite.sql2rel.SqlToRelConverter.getCorrelationUse ( Blackboard  bb,
final RelNode  r0 
)
inlineprivate

Definition at line 2511 of file SqlToRelConverter.java.

References field(), i, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.mapRootRelToFieldProjection, test_fsi.r, org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder, setup.root, and org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.root.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertWhere(), and org.apache.calcite.sql2rel.SqlToRelConverter.createJoin().

2511  {
2512  final Set<CorrelationId> correlatedVariables = RelOptUtil.getVariablesUsed(r0);
2513  if (correlatedVariables.isEmpty()) {
2514  return null;
2515  }
2516  final ImmutableBitSet.Builder requiredColumns = ImmutableBitSet.builder();
2517  final List<CorrelationId> correlNames = new ArrayList<>();
2518 
2519  // All correlations must refer the same namespace since correlation
2520  // produces exactly one correlation source.
2521  // The same source might be referenced by different variables since
2522  // DeferredLookups are not de-duplicated at create time.
2523  SqlValidatorNamespace prevNs = null;
2524 
2525  for (CorrelationId correlName : correlatedVariables) {
2526  DeferredLookup lookup = mapCorrelToDeferred.get(correlName);
2527  RexFieldAccess fieldAccess = lookup.getFieldAccess(correlName);
2528  String originalRelName = lookup.getOriginalRelName();
2529  String originalFieldName = fieldAccess.getField().getName();
2530 
2531  final SqlNameMatcher nameMatcher =
2532  bb.getValidator().getCatalogReader().nameMatcher();
2533  final SqlValidatorScope.ResolvedImpl resolved =
2534  new SqlValidatorScope.ResolvedImpl();
2535  lookup.bb.scope.resolve(
2536  ImmutableList.of(originalRelName), nameMatcher, false, resolved);
2537  assert resolved.count() == 1;
2538  final SqlValidatorScope.Resolve resolve = resolved.only();
2539  final SqlValidatorNamespace foundNs = resolve.namespace;
2540  final RelDataType rowType = resolve.rowType();
2541  final int childNamespaceIndex = resolve.path.steps().get(0).i;
2542  final SqlValidatorScope ancestorScope = resolve.scope;
2543  boolean correlInCurrentScope = bb.scope.isWithin(ancestorScope);
2544 
2545  if (!correlInCurrentScope) {
2546  continue;
2547  }
2548 
2549  if (prevNs == null) {
2550  prevNs = foundNs;
2551  } else {
2552  assert prevNs
2553  == foundNs : "All correlation variables should resolve"
2554  + " to the same namespace."
2555  + " Prev ns="
2556  + prevNs
2557  + ", new ns="
2558  + foundNs;
2559  }
2560 
2561  int namespaceOffset = 0;
2562  if (childNamespaceIndex > 0) {
2563  // If not the first child, need to figure out the width
2564  // of output types from all the preceding namespaces
2565  assert ancestorScope instanceof ListScope;
2566  List<SqlValidatorNamespace> children = ((ListScope) ancestorScope).getChildren();
2567 
2568  for (int i = 0; i < childNamespaceIndex; i++) {
2569  SqlValidatorNamespace child = children.get(i);
2570  namespaceOffset += child.getRowType().getFieldCount();
2571  }
2572  }
2573 
2574  RexFieldAccess topLevelFieldAccess = fieldAccess;
2575  while (topLevelFieldAccess.getReferenceExpr() instanceof RexFieldAccess) {
2576  topLevelFieldAccess = (RexFieldAccess) topLevelFieldAccess.getReferenceExpr();
2577  }
2578  final RelDataTypeField field = rowType.getFieldList().get(
2579  topLevelFieldAccess.getField().getIndex() - namespaceOffset);
2580  int pos = namespaceOffset + field.getIndex();
2581 
2582  assert field.getType() == topLevelFieldAccess.getField().getType();
2583 
2584  assert pos != -1;
2585 
2586  if (bb.mapRootRelToFieldProjection.containsKey(bb.root)) {
2587  // bb.root is an aggregate and only projects group by
2588  // keys.
2589  Map<Integer, Integer> exprProjection =
2590  bb.mapRootRelToFieldProjection.get(bb.root);
2591 
2592  // sub-query can reference group by keys projected from
2593  // the root of the outer relation.
2594  if (exprProjection.containsKey(pos)) {
2595  pos = exprProjection.get(pos);
2596  } else {
2597  // correl not grouped
2598  throw new AssertionError("Identifier '" + originalRelName + "."
2599  + originalFieldName + "' is not a group expr");
2600  }
2601  }
2602 
2603  requiredColumns.set(pos);
2604  correlNames.add(correlName);
2605  }
2606 
2607  if (correlNames.isEmpty()) {
2608  // None of the correlating variables originated in this scope.
2609  return null;
2610  }
2611 
2612  RelNode r = r0;
2613  if (correlNames.size() > 1) {
2614  // The same table was referenced more than once.
2615  // So we deduplicate.
2616  r = DeduplicateCorrelateVariables.go(
2617  rexBuilder, correlNames.get(0), Util.skip(correlNames), r0);
2618  // Add new node to leaves.
2619  leaves.put(r, r.getRowType().getFieldCount());
2620  }
2621  return new CorrelationUse(correlNames.get(0), requiredColumns.build(), r);
2622  }
tuple r
Definition: test_fsi.py:16
tuple root
Definition: setup.in.py:14
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int org.apache.calcite.sql2rel.SqlToRelConverter.getDynamicParamCount ( )
inline

Returns the number of dynamic parameters encountered during translation; this must only be called after convertQuery.

Returns
number of dynamic parameters

Definition at line 370 of file SqlToRelConverter.java.

370  {
371  return dynamicParamSqlNodes.size();
372  }
int org.apache.calcite.sql2rel.SqlToRelConverter.getDynamicParamCountInExplain ( boolean  increment)
inline

Returns the current count of the number of dynamic parameters in an EXPLAIN PLAN statement.

Parameters
incrementif true, increment the count
Returns
the current count before the optional increment

Definition at line 395 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.explainParamCount.

395  {
396  int retVal = explainParamCount;
397  if (increment) {
399  }
400  return retVal;
401  }
RelDataType org.apache.calcite.sql2rel.SqlToRelConverter.getDynamicParamType ( int  index)
inline

Returns the type inferred for a dynamic parameter.

Parameters
index0-based index of dynamic parameter
Returns
inferred type, never null

Definition at line 380 of file SqlToRelConverter.java.

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

380  {
381  SqlNode sqlNode = dynamicParamSqlNodes.get(index);
382  if (sqlNode == null) {
383  throw Util.needToImplement("dynamic param type inference");
384  }
385  return validator.getValidatedNodeType(sqlNode);
386  }

+ Here is the caller graph for this function:

InitializerExpressionFactory org.apache.calcite.sql2rel.SqlToRelConverter.getInitializerFactory ( SqlValidatorTable  validatorTable)
inlineprivate

Definition at line 3536 of file SqlToRelConverter.java.

References f, test_readcsv.table, and org.apache.calcite.sql2rel.SqlToRelConverter.unwrap().

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

3537  {
3538  // We might unwrap a null instead of a InitializerExpressionFactory.
3539  final Table table = unwrap(validatorTable, Table.class);
3540  if (table != null) {
3541  InitializerExpressionFactory f = unwrap(table, InitializerExpressionFactory.class);
3542  if (f != null) {
3543  return f;
3544  }
3545  }
3546  return NullInitializerExpressionFactory.INSTANCE;
3547  }
static< T > T unwrap(Object o, Class< T > clazz)
char * f

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int org.apache.calcite.sql2rel.SqlToRelConverter.getInSubqueryThreshold ( )
inlineprotected

Gets the list size threshold under which convertInToOr is used. Lists of this size or greater will instead be converted to use a join against an inline table (org.apache.calcite.rel.logical.LogicalValues) rather than a predicate. A threshold of 0 forces usage of an inline table in all cases; a threshold of Integer.MAX_VALUE forces usage of OR in all cases

Returns
threshold, default DEFAULT_IN_SUB_QUERY_THRESHOLD

Definition at line 1512 of file SqlToRelConverter.java.

1512  {
1513  return config.getInSubQueryThreshold();
1514  }
Map<SqlNode, RexNode> org.apache.calcite.sql2rel.SqlToRelConverter.getMapConvertedNonCorrSubqs ( )
inline

Returns the mapping of non-correlated sub-queries that have been converted to the constants that they evaluate to.

Definition at line 407 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.mapConvertedNonCorrSubqs.

407  {
409  }
final Map< SqlNode, RexNode > mapConvertedNonCorrSubqs
RexBuilder org.apache.calcite.sql2rel.SqlToRelConverter.getRexBuilder ( )
inline

Returns the row-expression builder.

Definition at line 360 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder.

360  {
361  return rexBuilder;
362  }
List<RelDataTypeField> org.apache.calcite.sql2rel.SqlToRelConverter.getSystemFields ( )
inlineprotected

Returns a list of fields to be prefixed to each relational expression.

Returns
List of system fields

Definition at line 2671 of file SqlToRelConverter.java.

2671  {
2672  return Collections.emptyList();
2673  }
RelOptTable org.apache.calcite.sql2rel.SqlToRelConverter.getTargetTable ( SqlNode  call)
inlineprotected

Definition at line 3432 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.collectInsertTargets(), org.apache.calcite.sql2rel.SqlToRelConverter.convertColumnList(), org.apache.calcite.sql2rel.SqlToRelConverter.convertDelete(), org.apache.calcite.sql2rel.SqlToRelConverter.convertInsert(), org.apache.calcite.sql2rel.SqlToRelConverter.convertMerge(), and org.apache.calcite.sql2rel.SqlToRelConverter.convertUpdate().

3432  {
3433  final SqlValidatorNamespace targetNs = validator.getNamespace(call);
3434  if (targetNs.isWrapperFor(SqlValidatorImpl.DmlNamespace.class)) {
3435  final SqlValidatorImpl.DmlNamespace dmlNamespace =
3436  targetNs.unwrap(SqlValidatorImpl.DmlNamespace.class);
3437  return SqlValidatorUtil.getRelOptTable(dmlNamespace, catalogReader, null, null);
3438  }
3439  final SqlValidatorNamespace resolvedNamespace = targetNs.resolve();
3440  return SqlValidatorUtil.getRelOptTable(resolvedNamespace, catalogReader, null, null);
3441  }

+ Here is the caller graph for this function:

static boolean org.apache.calcite.sql2rel.SqlToRelConverter.isOrdered ( SqlNode  query)
inlinestatic

Definition at line 614 of file SqlToRelConverter.java.

References SELECT, and WITH.

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

614  {
615  switch (query.getKind()) {
616  case SELECT:
617  return ((SqlSelect) query).getOrderList() != null
618  && ((SqlSelect) query).getOrderList().size() > 0;
619  case WITH:
620  return isOrdered(((SqlWith) query).body);
621  case ORDER_BY:
622  return ((SqlOrderBy) query).orderList.size() > 0;
623  default:
624  return false;
625  }
626  }
#define WITH
#define SELECT

+ Here is the caller graph for this function:

boolean org.apache.calcite.sql2rel.SqlToRelConverter.isRowConstructor ( SqlNode  node)
inlineprivate

Definition at line 1684 of file SqlToRelConverter.java.

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertRowConstructor(), and org.apache.calcite.sql2rel.SqlToRelConverter.convertRowValues().

1684  {
1685  if (!(node.getKind() == SqlKind.ROW)) {
1686  return false;
1687  }
1688  SqlCall call = (SqlCall) node;
1689  return call.getOperator().getName().equalsIgnoreCase("row");
1690  }

+ Here is the caller graph for this function:

static boolean org.apache.calcite.sql2rel.SqlToRelConverter.isStream ( SqlNode  query)
inlinestaticprivate

Definition at line 609 of file SqlToRelConverter.java.

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

609  {
610  return query instanceof SqlSelect
611  && ((SqlSelect) query).isKeywordPresent(SqlSelectKeyword.STREAM);
612  }

+ Here is the caller graph for this function:

boolean org.apache.calcite.sql2rel.SqlToRelConverter.isSubQueryNonCorrelated ( RelNode  subq,
Blackboard  bb 
)
inlineprivate

Determines whether a sub-query is non-correlated. Note that a non-correlated sub-query can contain correlated references, provided those references do not reference select statements that are parents of the sub-query.

Parameters
subqthe sub-query
bbblackboard used while converting the sub-query, i.e., the blackboard of the parent query of this sub-query
Returns
true if the sub-query is non-correlated

Definition at line 2634 of file SqlToRelConverter.java.

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

2634  {
2635  Set<CorrelationId> correlatedVariables = RelOptUtil.getVariablesUsed(subq);
2636  for (CorrelationId correlName : correlatedVariables) {
2637  DeferredLookup lookup = mapCorrelToDeferred.get(correlName);
2638  String originalRelName = lookup.getOriginalRelName();
2639 
2640  final SqlNameMatcher nameMatcher =
2641  lookup.bb.scope.getValidator().getCatalogReader().nameMatcher();
2642  final SqlValidatorScope.ResolvedImpl resolved =
2643  new SqlValidatorScope.ResolvedImpl();
2644  lookup.bb.scope.resolve(
2645  ImmutableList.of(originalRelName), nameMatcher, false, resolved);
2646 
2647  SqlValidatorScope ancestorScope = resolved.only().scope;
2648 
2649  // If the correlated reference is in a scope that's "above" the
2650  // sub-query, then this is a correlated sub-query.
2651  SqlValidatorScope parentScope = bb.scope;
2652  do {
2653  if (ancestorScope == parentScope) {
2654  return false;
2655  }
2656  if (parentScope instanceof DelegatingScope) {
2657  parentScope = ((DelegatingScope) parentScope).getParent();
2658  } else {
2659  break;
2660  }
2661  } while (parentScope != null);
2662  }
2663  return true;
2664  }

+ Here is the caller graph for this function:

boolean org.apache.calcite.sql2rel.SqlToRelConverter.isTrimUnusedFields ( )
inline

Returns whether to trim unused fields as part of the conversion process.

Returns
Whether to trim unused fields

Definition at line 3222 of file SqlToRelConverter.java.

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

3222  {
3223  return config.isTrimUnusedFields();
3224  }

+ Here is the caller graph for this function:

SqlQuantifyOperator org.apache.calcite.sql2rel.SqlToRelConverter.negate ( SqlQuantifyOperator  operator)
inlineprivate

Definition at line 4795 of file SqlToRelConverter.java.

References ALL.

4795  {
4796  assert operator.kind == SqlKind.ALL;
4797  return SqlStdOperatorTable.some(operator.comparisonKind.negateNullSafe());
4798  }
#define ALL
RelFieldTrimmer org.apache.calcite.sql2rel.SqlToRelConverter.newFieldTrimmer ( )
inlineprotected

Creates a RelFieldTrimmer.

Returns
Field trimmer

Definition at line 552 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.relBuilder, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

552  {
553  return new RelFieldTrimmer(validator, relBuilder);
554  }

+ Here is the caller graph for this function:

static SqlNode org.apache.calcite.sql2rel.SqlToRelConverter.pushDownNotForIn ( SqlValidatorScope  scope,
SqlNode  sqlNode 
)
inlinestaticprivate

Push down all the NOT logical operators into any IN/NOT IN operators.

Parameters
scopeScope where
sqlNode
occurs
sqlNodethe root node from which to look for NOT operators
Returns
the transformed SqlNode representation with NOT pushed down.

Definition at line 879 of file SqlToRelConverter.java.

References AND, CASE, org.apache.calcite.sql2rel.SqlToRelConverter.containsInOperator(), IN, NOT, OR, and org.apache.calcite.sql2rel.SqlToRelConverter.reg().

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertWhere(), and org.apache.calcite.sql2rel.SqlToRelConverter.createAggImpl().

879  {
880  if (!(sqlNode instanceof SqlCall) || !containsInOperator(sqlNode)) {
881  return sqlNode;
882  }
883  final SqlCall sqlCall = (SqlCall) sqlNode;
884  switch (sqlCall.getKind()) {
885  case AND:
886  case OR:
887  final List<SqlNode> operands = new ArrayList<>();
888  for (SqlNode operand : sqlCall.getOperandList()) {
889  operands.add(pushDownNotForIn(scope, operand));
890  }
891  final SqlCall newCall =
892  sqlCall.getOperator().createCall(sqlCall.getParserPosition(), operands);
893  return reg(scope, newCall);
894 
895  case NOT:
896  assert sqlCall.operand(0) instanceof SqlCall;
897  final SqlCall call = sqlCall.operand(0);
898  switch (sqlCall.operand(0).getKind()) {
899  case CASE:
900  final SqlCase caseNode = (SqlCase) call;
901  final SqlNodeList thenOperands = new SqlNodeList(SqlParserPos.ZERO);
902 
903  for (SqlNode thenOperand : caseNode.getThenOperands()) {
904  final SqlCall not =
905  SqlStdOperatorTable.NOT.createCall(SqlParserPos.ZERO, thenOperand);
906  thenOperands.add(pushDownNotForIn(scope, reg(scope, not)));
907  }
908  SqlNode elseOperand = caseNode.getElseOperand();
909  if (!SqlUtil.isNull(elseOperand)) {
910  // "not(unknown)" is "unknown", so no need to simplify
911  final SqlCall not =
912  SqlStdOperatorTable.NOT.createCall(SqlParserPos.ZERO, elseOperand);
913  elseOperand = pushDownNotForIn(scope, reg(scope, not));
914  }
915 
916  return reg(scope,
917  SqlStdOperatorTable.CASE.createCall(SqlParserPos.ZERO,
918  caseNode.getValueOperand(),
919  caseNode.getWhenOperands(),
920  thenOperands,
921  elseOperand));
922 
923  case AND:
924  final List<SqlNode> orOperands = new ArrayList<>();
925  for (SqlNode operand : call.getOperandList()) {
926  orOperands.add(pushDownNotForIn(scope,
927  reg(scope,
928  SqlStdOperatorTable.NOT.createCall(
929  SqlParserPos.ZERO, operand))));
930  }
931  return reg(scope,
932  SqlStdOperatorTable.OR.createCall(SqlParserPos.ZERO, orOperands));
933 
934  case OR:
935  final List<SqlNode> andOperands = new ArrayList<>();
936  for (SqlNode operand : call.getOperandList()) {
937  andOperands.add(pushDownNotForIn(scope,
938  reg(scope,
939  SqlStdOperatorTable.NOT.createCall(
940  SqlParserPos.ZERO, operand))));
941  }
942  return reg(scope,
943  SqlStdOperatorTable.AND.createCall(SqlParserPos.ZERO, andOperands));
944 
945  case NOT:
946  assert call.operandCount() == 1;
947  return pushDownNotForIn(scope, call.operand(0));
948 
949  case NOT_IN:
950  return reg(scope,
951  SqlStdOperatorTable.IN.createCall(
952  SqlParserPos.ZERO, call.getOperandList()));
953 
954  case IN:
955  return reg(scope,
956  SqlStdOperatorTable.NOT_IN.createCall(
957  SqlParserPos.ZERO, call.getOperandList()));
958  }
959  }
960  return sqlNode;
961  }
#define NOT
static SqlNode pushDownNotForIn(SqlValidatorScope scope, SqlNode sqlNode)
#define IN
#define CASE
#define OR
static SqlNode reg(SqlValidatorScope scope, SqlNode e)
#define AND

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static SqlNode org.apache.calcite.sql2rel.SqlToRelConverter.reg ( SqlValidatorScope  scope,
SqlNode  e 
)
inlinestaticprivate

Registers with the validator a SqlNode that has been created during the Sql-to-Rel process.

Definition at line 967 of file SqlToRelConverter.java.

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

967  {
968  scope.getValidator().deriveType(scope, e);
969  return e;
970  }

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries ( final Blackboard  bb,
final SqlNode  expr,
RelOptUtil.Logic  logic 
)
inlineprivate

Definition at line 1009 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.findSubQueries(), org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.subQueryList, and org.apache.calcite.sql2rel.SqlToRelConverter.substituteSubQuery().

Referenced by org.apache.calcite.sql2rel.SqlToRelConverter.convertCollectionTable(), org.apache.calcite.sql2rel.SqlToRelConverter.convertJoinCondition(), org.apache.calcite.sql2rel.SqlToRelConverter.convertMatchRecognize(), org.apache.calcite.sql2rel.SqlToRelConverter.convertUnnest(), org.apache.calcite.sql2rel.SqlToRelConverter.convertUpdate(), org.apache.calcite.sql2rel.SqlToRelConverter.convertWhere(), and org.apache.calcite.sql2rel.SqlToRelConverter.createAggImpl().

1010  {
1011  findSubQueries(bb, expr, logic, false);
1012  for (SubQuery node : bb.subQueryList) {
1013  substituteSubQuery(bb, node);
1014  }
1015  }
void substituteSubQuery(Blackboard bb, SubQuery subQuery)
void findSubQueries(Blackboard bb, SqlNode node, RelOptUtil.Logic logic, boolean registerOnlyScalarSubQueries)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelCollation org.apache.calcite.sql2rel.SqlToRelConverter.requiredCollation ( RelNode  r)
inlineprivate

Definition at line 628 of file SqlToRelConverter.java.

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

628  {
629  if (r instanceof Sort) {
630  return ((Sort) r).collation;
631  }
632  if (r instanceof Project) {
633  return requiredCollation(((Project) r).getInput());
634  }
635  if (r instanceof Delta) {
636  return requiredCollation(((Delta) r).getInput());
637  }
638  throw new AssertionError(r);
639  }
tuple r
Definition: test_fsi.py:16

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.rewriteAggregateWithGroupId ( Blackboard  bb,
AggregatingSelectScope.Resolved  r,
AggConverter  converter 
)
inlineprivate

The

GROUP_ID()

function is used to distinguish duplicate groups. However, as Aggregate normalizes group sets to canonical form (i.e., flatten, sorting, redundancy removal), this information is lost in RelNode. Therefore, it is impossible to implement the function in runtime.

To fill this gap, an aggregation query that contains

GROUP_ID()

function will generally be rewritten into UNION when converting to RelNode.

Also see the discussion in JIRA [CALCITE-1824] GROUP_ID returns wrong result.

Definition at line 2958 of file SqlToRelConverter.java.

References BIGINT, org.apache.calcite.sql2rel.SqlToRelConverter.createAggregate(), i, Integer, generate_TableFunctionsFactory_init.k, and test_fsi.r.

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

2959  {
2960  final List<AggregateCall> aggregateCalls = converter.getAggCalls();
2961  final ImmutableBitSet groupSet = r.groupSet;
2962  final Map<ImmutableBitSet, Integer> groupSetCount = r.groupSetCount;
2963 
2964  final List<String> fieldNamesIfNoRewrite =
2965  createAggregate(bb, groupSet, r.groupSets, aggregateCalls)
2966  .getRowType()
2967  .getFieldNames();
2968 
2969  // If n duplicates exist for a particular grouping, the {@code GROUP_ID()}
2970  // function produces values in the range 0 to n-1. For each value,
2971  // we need to figure out the corresponding group sets.
2972  //
2973  // For example, "... GROUPING SETS (a, a, b, c, c, c, c)"
2974  // (i) The max value of the GROUP_ID() function returns is 3
2975  // (ii) GROUPING SETS (a, b, c) produces value 0,
2976  // GROUPING SETS (a, c) produces value 1,
2977  // GROUPING SETS (c) produces value 2
2978  // GROUPING SETS (c) produces value 3
2979  final Map<Integer, Set<ImmutableBitSet>> groupIdToGroupSets = new HashMap<>();
2980  int maxGroupId = 0;
2981  for (Map.Entry<ImmutableBitSet, Integer> entry : groupSetCount.entrySet()) {
2982  int groupId = entry.getValue() - 1;
2983  if (groupId > maxGroupId) {
2984  maxGroupId = groupId;
2985  }
2986  for (int i = 0; i <= groupId; i++) {
2987  groupIdToGroupSets
2988  .computeIfAbsent(i, k -> Sets.newTreeSet(ImmutableBitSet.COMPARATOR))
2989  .add(entry.getKey());
2990  }
2991  }
2992 
2993  // AggregateCall list without GROUP_ID function
2994  final List<AggregateCall> aggregateCallsWithoutGroupId = new ArrayList<>();
2995  for (AggregateCall aggregateCall : aggregateCalls) {
2996  if (aggregateCall.getAggregation().kind != SqlKind.GROUP_ID) {
2997  aggregateCallsWithoutGroupId.add(aggregateCall);
2998  }
2999  }
3000  final List<RelNode> projects = new ArrayList<>();
3001  // For each group id value , we first construct an Aggregate without
3002  // GROUP_ID() function call, and then create a Project node on top of it.
3003  // The Project adds literal value for group id in right position.
3004  for (int groupId = 0; groupId <= maxGroupId; groupId++) {
3005  // Create the Aggregate node without GROUP_ID() call
3006  final ImmutableList<ImmutableBitSet> groupSets =
3007  ImmutableList.copyOf(groupIdToGroupSets.get(groupId));
3008  final RelNode aggregate =
3009  createAggregate(bb, groupSet, groupSets, aggregateCallsWithoutGroupId);
3010 
3011  // RexLiteral for each GROUP_ID, note the type should be BIGINT
3012  final RelDataType groupIdType = typeFactory.createSqlType(SqlTypeName.BIGINT);
3013  final RexNode groupIdLiteral =
3014  rexBuilder.makeExactLiteral(BigDecimal.valueOf(groupId), groupIdType);
3015 
3016  relBuilder.push(aggregate);
3017  final List<RexNode> selectList = new ArrayList<>();
3018  final int groupExprLength = r.groupExprList.size();
3019  // Project fields in group by expressions
3020  for (int i = 0; i < groupExprLength; i++) {
3021  selectList.add(relBuilder.field(i));
3022  }
3023  // Project fields in aggregate calls
3024  int groupIdCount = 0;
3025  for (int i = 0; i < aggregateCalls.size(); i++) {
3026  if (aggregateCalls.get(i).getAggregation().kind == SqlKind.GROUP_ID) {
3027  selectList.add(groupIdLiteral);
3028  groupIdCount++;
3029  } else {
3030  int ordinal = groupExprLength + i - groupIdCount;
3031  selectList.add(relBuilder.field(ordinal));
3032  }
3033  }
3034  final RelNode project =
3035  relBuilder.project(selectList, fieldNamesIfNoRewrite).build();
3036  projects.add(project);
3037  }
3038  // Skip to create Union when there is only one child, i.e., no duplicate group
3039  // set.
3040  if (projects.size() == 1) {
3041  return projects.get(0);
3042  }
3043  return LogicalUnion.create(projects, true);
3044  }
tuple r
Definition: test_fsi.py:16
#define BIGINT
RelNode createAggregate(Blackboard bb, ImmutableBitSet groupSet, ImmutableList< ImmutableBitSet > groupSets, List< AggregateCall > aggCalls)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.setDynamicParamCountInExplain ( int  explainParamCount)
inline

Sets the number of dynamic parameters in the current EXPLAIN PLAN statement.

Parameters
explainParamCountnumber of dynamic parameters in the statement

Definition at line 438 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.explainParamCount.

438  {
439  assert config.isExplain();
440  this.explainParamCount = explainParamCount;
441  }
void org.apache.calcite.sql2rel.SqlToRelConverter.setSubQueryConverter ( SubQueryConverter  converter)
inline

Sets a new SubQueryConverter. To have any effect, this must be called before any convert method.

Parameters
converternew SubQueryConverter

Definition at line 429 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.subQueryConverter.

429  {
430  subQueryConverter = converter;
431  }
void org.apache.calcite.sql2rel.SqlToRelConverter.substituteSubQuery ( Blackboard  bb,
SubQuery  subQuery 
)
inlineprivate

Definition at line 1017 of file SqlToRelConverter.java.

References ALL, run_benchmark_import.args, BIGINT, org.apache.calcite.sql2rel.SqlToRelConverter.config, org.apache.calcite.sql2rel.SqlToRelConverter.containsNullLiteral(), org.apache.calcite.sql2rel.SqlToRelConverter.convertCursor(), org.apache.calcite.sql2rel.SqlToRelConverter.convertExists(), org.apache.calcite.sql2rel.SqlToRelConverter.convertInToOr(), org.apache.calcite.sql2rel.SqlToRelConverter.convertMultisets(), org.apache.calcite.sql2rel.SqlToRelConverter.convertNonCorrelatedSubQuery(), org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryOrInList(), org.apache.calcite.sql2rel.SqlToRelConverter.convertToSingleValueSubq(), org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard(), CURSOR, Double, EXISTS, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.getTopNode(), IN, join(), NOT, test_fsi.r, org.apache.calcite.sql2rel.SqlToRelConverter.relBuilder, org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder, setup.root, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.root, SELECT, SOME, org.apache.calcite.sql2rel.SqlToRelConverter.translateIn(), org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

1017  {
1018  final RexNode expr = subQuery.expr;
1019  if (expr != null) {
1020  // Already done.
1021  return;
1022  }
1023 
1024  final SqlBasicCall call;
1025  final RelNode rel;
1026  final SqlNode query;
1027  final RelOptUtil.Exists converted;
1028 
1029  boolean isExpand = config.getExpandPredicate().test(bb.getTopNode(), subQuery.node);
1030 
1031  switch (subQuery.node.getKind()) {
1032  case CURSOR:
1033  convertCursor(bb, subQuery);
1034  return;
1035 
1036  case MULTISET_QUERY_CONSTRUCTOR:
1037  case MULTISET_VALUE_CONSTRUCTOR:
1038  case ARRAY_QUERY_CONSTRUCTOR:
1039  rel = convertMultisets(ImmutableList.of(subQuery.node), bb);
1040  subQuery.expr = bb.register(rel, JoinRelType.INNER);
1041  return;
1042 
1043  case IN:
1044  case NOT_IN:
1045  case SOME:
1046  case ALL:
1047  call = (SqlBasicCall) subQuery.node;
1048  query = call.operand(1);
1049  if (!isExpand && !(query instanceof SqlNodeList)) {
1050  return;
1051  }
1052  final SqlNode leftKeyNode = call.operand(0);
1053 
1054  final List<RexNode> leftKeys;
1055  switch (leftKeyNode.getKind()) {
1056  case ROW:
1057  leftKeys = new ArrayList<>();
1058  for (SqlNode sqlExpr : ((SqlBasicCall) leftKeyNode).getOperandList()) {
1059  leftKeys.add(bb.convertExpression(sqlExpr));
1060  }
1061  break;
1062  default:
1063  leftKeys = ImmutableList.of(bb.convertExpression(leftKeyNode));
1064  }
1065 
1066  if (query instanceof SqlNodeList) {
1067  SqlNodeList valueList = (SqlNodeList) query;
1068  if (!containsNullLiteral(valueList)
1069  && valueList.size() < config.getInSubQueryThreshold()) {
1070  // We're under the threshold, so convert to OR.
1071  subQuery.expr = convertInToOr(
1072  bb, leftKeys, valueList, (SqlInOperator) call.getOperator());
1073  return;
1074  }
1075 
1076  // Otherwise, let convertExists translate
1077  // values list into an inline table for the
1078  // reference to Q below.
1079  }
1080 
1081  // Project out the search columns from the left side
1082 
1083  // Q1:
1084  // "select from emp where emp.deptno in (select col1 from T)"
1085  //
1086  // is converted to
1087  //
1088  // "select from
1089  // emp inner join (select distinct col1 from T)) q
1090  // on emp.deptno = q.col1
1091  //
1092  // Q2:
1093  // "select from emp where emp.deptno not in (Q)"
1094  //
1095  // is converted to
1096  //
1097  // "select from
1098  // emp left outer join (select distinct col1, TRUE from T) q
1099  // on emp.deptno = q.col1
1100  // where emp.deptno <> null
1101  // and q.indicator <> TRUE"
1102  //
1103  // Note: Sub-query can be used as SqlUpdate#condition like below:
1104  //
1105  // UPDATE emp
1106  // SET empno = 1 WHERE emp.empno IN (
1107  // SELECT emp.empno FROM emp WHERE emp.empno = 2)
1108  //
1109  // In such case, when converting SqlUpdate#condition, bb.root is null
1110  // and it makes no sense to do the sub-query substitution.
1111  if (bb.root == null) {
1112  return;
1113  }
1114  final RelDataType targetRowType = SqlTypeUtil.promoteToRowType(
1115  typeFactory, validator.getValidatedNodeType(leftKeyNode), null);
1116  final boolean notIn = call.getOperator().kind == SqlKind.NOT_IN;
1117  converted = convertExists(
1118  query, RelOptUtil.SubQueryType.IN, subQuery.logic, notIn, targetRowType);
1119  if (converted.indicator) {
1120  // Generate
1121  // emp CROSS JOIN (SELECT COUNT(*) AS c,
1122  // COUNT(deptno) AS ck FROM dept)
1123  final RelDataType longType = typeFactory.createSqlType(SqlTypeName.BIGINT);
1124  final RelNode seek = converted.r.getInput(0); // fragile
1125  final int keyCount = leftKeys.size();
1126  final List<Integer> args = ImmutableIntList.range(0, keyCount);
1127  LogicalAggregate aggregate = LogicalAggregate.create(seek,
1128  ImmutableList.of(),
1129  ImmutableBitSet.of(),
1130  null,
1131  ImmutableList.of(AggregateCall.create(SqlStdOperatorTable.COUNT,
1132  false,
1133  false,
1134  false,
1135  ImmutableList.of(),
1136  -1,
1137  RelCollations.EMPTY,
1138  longType,
1139  null),
1140  AggregateCall.create(SqlStdOperatorTable.COUNT,
1141  false,
1142  false,
1143  false,
1144  args,
1145  -1,
1146  RelCollations.EMPTY,
1147  longType,
1148  null)));
1149  LogicalJoin join = LogicalJoin.create(bb.root,
1150  aggregate,
1151  ImmutableList.of(),
1152  rexBuilder.makeLiteral(true),
1153  ImmutableSet.of(),
1154  JoinRelType.INNER);
1155  bb.setRoot(join, false);
1156  }
1157  final RexNode rex = bb.register(converted.r,
1158  converted.outerJoin ? JoinRelType.LEFT : JoinRelType.INNER,
1159  leftKeys);
1160 
1161  RelOptUtil.Logic logic = subQuery.logic;
1162  switch (logic) {
1163  case TRUE_FALSE_UNKNOWN:
1164  case UNKNOWN_AS_TRUE:
1165  if (!converted.indicator) {
1166  logic = RelOptUtil.Logic.TRUE_FALSE;
1167  }
1168  }
1169  subQuery.expr = translateIn(logic, bb.root, rex);
1170  if (notIn) {
1171  subQuery.expr = rexBuilder.makeCall(SqlStdOperatorTable.NOT, subQuery.expr);
1172  }
1173  return;
1174 
1175  case EXISTS:
1176  // "select from emp where exists (select a from T)"
1177  //
1178  // is converted to the following if the sub-query is correlated:
1179  //
1180  // "select from emp left outer join (select AGG_TRUE() as indicator
1181  // from T group by corr_var) q where q.indicator is true"
1182  //
1183  // If there is no correlation, the expression is replaced with a
1184  // boolean indicating whether the sub-query returned 0 or >= 1 row.
1185  call = (SqlBasicCall) subQuery.node;
1186  query = call.operand(0);
1187  if (!isExpand) {
1188  return;
1189  }
1190  final SqlValidatorScope seekScope = (query instanceof SqlSelect)
1191  ? validator.getSelectScope((SqlSelect) query)
1192  : null;
1193  final Blackboard seekBb = createBlackboard(seekScope, null, false);
1194  final RelNode seekRel = convertQueryOrInList(seekBb, query, null);
1195  // An EXIST sub-query whose inner child has at least 1 tuple
1196  // (e.g. an Aggregate with no grouping columns or non-empty Values
1197  // node) should be simplified to a Boolean constant expression.
1198  final RelMetadataQuery mq = seekRel.getCluster().getMetadataQuery();
1199  final Double minRowCount = mq.getMinRowCount(seekRel);
1200  if (minRowCount != null && minRowCount >= 1D) {
1201  subQuery.expr = rexBuilder.makeLiteral(true);
1202  return;
1203  }
1204  converted = RelOptUtil.createExistsPlan(seekRel,
1206  subQuery.logic,
1207  true,
1208  relBuilder);
1209  assert !converted.indicator;
1210  if (convertNonCorrelatedSubQuery(subQuery, bb, converted.r, true)) {
1211  return;
1212  }
1213  subQuery.expr = bb.register(converted.r, JoinRelType.LEFT);
1214  return;
1215 
1216  case SCALAR_QUERY:
1217  // Convert the sub-query. If it's non-correlated, convert it
1218  // to a constant expression.
1219  if (!isExpand) {
1220  return;
1221  }
1222  call = (SqlBasicCall) subQuery.node;
1223  query = call.operand(0);
1224  converted = convertExists(
1225  query, RelOptUtil.SubQueryType.SCALAR, subQuery.logic, true, null);
1226  assert !converted.indicator;
1227  if (convertNonCorrelatedSubQuery(subQuery, bb, converted.r, false)) {
1228  return;
1229  }
1230  rel = convertToSingleValueSubq(query, converted.r);
1231  subQuery.expr = bb.register(rel, JoinRelType.LEFT);
1232  return;
1233 
1234  case SELECT:
1235  // This is used when converting multiset queries:
1236  //
1237  // select * from unnest(select multiset[deptno] from emps);
1238  //
1239  converted = convertExists(subQuery.node,
1240  RelOptUtil.SubQueryType.SCALAR,
1241  subQuery.logic,
1242  true,
1243  null);
1244  assert !converted.indicator;
1245  subQuery.expr = bb.register(converted.r, JoinRelType.LEFT);
1246 
1247  // This is used when converting window table functions:
1248  //
1249  // select * from table(tumble(table emps, descriptor(deptno), interval '3' DAY))
1250  //
1251  bb.cursors.add(converted.r);
1252  return;
1253 
1254  default:
1255  throw new AssertionError("unexpected kind of sub-query: " + subQuery.node);
1256  }
1257  }
#define NOT
#define CURSOR
#define EXISTS
RexNode translateIn(RelOptUtil.Logic logic, RelNode root, final RexNode rex)
RelNode convertCursor(Blackboard bb, SubQuery subQuery)
RelNode convertQueryOrInList(Blackboard bb, SqlNode seek, RelDataType targetRowType)
tuple r
Definition: test_fsi.py:16
std::string join(T const &container, std::string const &delim)
#define BIGINT
tuple root
Definition: setup.in.py:14
#define IN
RexNode convertInToOr(final Blackboard bb, final List< RexNode > leftKeys, SqlNodeList valuesList, SqlInOperator op)
RelOptUtil.Exists convertExists(SqlNode seek, RelOptUtil.SubQueryType subQueryType, RelOptUtil.Logic logic, boolean notIn, RelDataType targetDataType)
#define SOME
RelNode convertMultisets(final List< SqlNode > operands, Blackboard bb)
static boolean containsNullLiteral(SqlNodeList valueList)
boolean convertNonCorrelatedSubQuery(SubQuery subQuery, Blackboard bb, RelNode converted, boolean isExists)
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)
RelNode convertToSingleValueSubq(SqlNode query, RelNode plan)