OmniSciDB  1dac507f6e
 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  AggConverter
 
class  AggregateFinder
 
class  Blackboard
 
interface  Config
 
class  ConfigBuilder
 
class  ConfigImpl
 
class  CorrelationUse
 
class  DeferredLookup
 
class  HistogramShuttle
 
class  LookupContext
 
class  NoOpSubQueryConverter
 
class  RexAccessShuttle
 
class  SubQuery
 

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)
 
RelRoot convertWith (SqlWith with, boolean top)
 
RelNode convertValues (SqlCall values, RelDataType targetRowType)
 

Static Public Member Functions

static boolean isOrdered (SqlNode query)
 
static ConfigBuilder configBuilder ()
 

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 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 List< RelNode > leaves = new ArrayList<>()
 
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 convertIdentifier (Blackboard bb, SqlIdentifier id, SqlNodeList extendedColumns)
 
void snapshotTemporalTable (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)
 
boolean all (SqlCall call)
 
RelNode createModify (RelOptTable targetTable, RelNode source)
 
RelNode createSource (RelOptTable targetTable, RelNode source, ModifiableView modifiableView, RelDataType delegateRowType)
 
RelOptTable.ToRelContext createToRelContext ()
 
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
 
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<>()
 

Static Private Attributes

static final BigDecimal TWO = BigDecimal.valueOf(2L)
 

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

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

286  {
287  this(viewExpander,
288  validator,
290  RelOptCluster.create(planner, rexBuilder),
291  convertletTable,
292  Config.DEFAULT);
293  }
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 310 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.

315  {
316  this.viewExpander = viewExpander;
317  this.opTab = (validator == null) ? SqlStdOperatorTable.instance()
318  : validator.getOperatorTable();
319  this.validator = validator;
320  this.catalogReader = catalogReader;
321  this.subQueryConverter = new NoOpSubQueryConverter();
322  this.rexBuilder = cluster.getRexBuilder();
323  this.typeFactory = rexBuilder.getTypeFactory();
324  this.cluster = Objects.requireNonNull(cluster);
325  this.exprConverter = new SqlNodeToRexConverterImpl(convertletTable);
326  this.explainParamCount = 0;
327  this.config = new ConfigBuilder().withConfig(config).build();
328  this.relBuilder = config.getRelBuilderFactory().create(cluster, null);
329  }
FILE * create(const std::string &basePath, const int fileId, const size_t pageSize, const size_t numPages)
Definition: File.cpp:35

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

402  {
403  mapConvertedNonCorrSubqs.putAll(alreadyConvertedNonCorrSubqs);
404  }
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 3599 of file SqlToRelConverter.java.

References field().

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

3599  {
3600  RelDataTypeField field = bb.getRootField(inputRef);
3601  if (field != null) {
3602  return rexBuilder.makeInputRef(field.getType(), inputRef.getIndex());
3603  }
3604  return inputRef;
3605  }
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 2293 of file SqlToRelConverter.java.

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

2295  {}

+ Here is the caller graph for this function:

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

Definition at line 3066 of file SqlToRelConverter.java.

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

3066  {
3067  return ((SqlSetOperator) call.getOperator()).isAll();
3068  }

+ Here is the caller graph for this function:

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

Definition at line 3331 of file SqlToRelConverter.java.

References run_benchmark_import.type.

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

3331  {
3332  if (!RexLiteral.isNullLiteral(node)) {
3333  return node;
3334  }
3335  return rexBuilder.makeCast(type, node);
3336  }

+ Here is the caller graph for this function:

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

Definition at line 426 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().

426  {
427  if (query.isA(SqlKind.DML)) {
428  return;
429  }
430  // Verify that conversion from SQL to relational algebra did
431  // not perturb any type information. (We can't do this if the
432  // SQL statement is something like an INSERT which has no
433  // validator type information associated with its result,
434  // hence the namespace check above.)
435  final List<RelDataTypeField> validatedFields =
436  validator.getValidatedNodeType(query).getFieldList();
437  final RelDataType validatedRowType =
438  validator.getTypeFactory().createStructType(Pair.right(validatedFields),
439  SqlValidatorUtil.uniquify(Pair.left(validatedFields),
440  catalogReader.nameMatcher().isCaseSensitive()));
441 
442  final List<RelDataTypeField> convertedFields =
443  result.getRowType().getFieldList().subList(0, validatedFields.size());
444  final RelDataType convertedRowType =
445  validator.getTypeFactory().createStructType(convertedFields);
446 
447  if (!RelOptUtil.equal("validated row type",
448  validatedRowType,
449  "converted row type",
450  convertedRowType,
451  Litmus.IGNORE)) {
452  throw new AssertionError("Conversion to relational algebra failed to "
453  + "preserve datatypes:\n"
454  + "validated type:\n" + validatedRowType.getFullTypeString()
455  + "\nconverted type:\n" + convertedRowType.getFullTypeString() + "\nrel:\n"
456  + RelOptUtil.toString(result));
457  }
458  }

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

References assert(), org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader, org.apache.calcite.sql2rel.SqlToRelConverter.createInsertBlackboard(), field(), org.apache.calcite.sql2rel.SqlToRelConverter.getTargetTable(), Fragmenter_Namespace.indexOf(), Experimental.String, org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

3351  {
3352  final RelOptTable targetTable = getTargetTable(call);
3353  final RelDataType tableRowType = targetTable.getRowType();
3354  SqlNodeList targetColumnList = call.getTargetColumnList();
3355  if (targetColumnList == null) {
3356  if (validator.getConformance().isInsertSubsetColumnsAllowed()) {
3357  final RelDataType targetRowType =
3358  typeFactory.createStructType(tableRowType.getFieldList().subList(
3359  0, sourceRef.getType().getFieldCount()));
3360  targetColumnNames.addAll(targetRowType.getFieldNames());
3361  } else {
3362  targetColumnNames.addAll(tableRowType.getFieldNames());
3363  }
3364  } else {
3365  for (int i = 0; i < targetColumnList.size(); i++) {
3366  SqlIdentifier id = (SqlIdentifier) targetColumnList.get(i);
3367  RelDataTypeField field = SqlValidatorUtil.getTargetField(
3368  tableRowType, typeFactory, id, catalogReader, targetTable);
3369  assert field != null : "column " + id.toString() + " not found";
3370  targetColumnNames.add(field.getName());
3371  }
3372  }
3373 
3374  final Blackboard bb =
3375  createInsertBlackboard(targetTable, sourceRef, targetColumnNames);
3376 
3377  // Next, assign expressions for generated columns.
3378  final List<ColumnStrategy> strategies = targetTable.getColumnStrategies();
3379  for (String columnName : targetColumnNames) {
3380  final int i = tableRowType.getFieldNames().indexOf(columnName);
3381  final RexNode expr;
3382  switch (strategies.get(i)) {
3383  case STORED:
3384  final InitializerExpressionFactory f =
3385  Util.first(targetTable.unwrap(InitializerExpressionFactory.class),
3386  NullInitializerExpressionFactory.INSTANCE);
3387  expr = f.newColumnDefaultValue(targetTable, i, bb);
3388  break;
3389  case VIRTUAL:
3390  expr = null;
3391  break;
3392  default:
3393  expr = bb.nameToNodeMap.get(columnName);
3394  }
3395  columnExprs.add(expr);
3396  }
3397 
3398  // Remove virtual columns from the list.
3399  for (int i = 0; i < targetColumnNames.size(); i++) {
3400  if (columnExprs.get(i) == null) {
3401  columnExprs.remove(i);
3402  targetColumnNames.remove(i);
3403  --i;
3404  }
3405  }
3406  }
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)
int64_t const int32_t sz assert(dest)
Blackboard createInsertBlackboard(RelOptTable targetTable, RexNode sourceRef, List< String > targetColumnNames)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ConfigBuilder org.apache.calcite.sql2rel.SqlToRelConverter.configBuilder ( )
inlinestatic

Creates a builder for a Config.

Definition at line 5296 of file SqlToRelConverter.java.

5296  {
5297  return new ConfigBuilder();
5298  }
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 804 of file SqlToRelConverter.java.

References Void.

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

804  {
805  try {
806  SqlVisitor<Void> visitor = new SqlBasicVisitor<Void>() {
807  public Void visit(SqlCall call) {
808  if (call.getOperator() instanceof SqlInOperator) {
809  throw new Util.FoundOne(call);
810  }
811  return super.visit(call);
812  }
813  };
814  node.accept(visitor);
815  return false;
816  } catch (Util.FoundOne e) {
817  Util.swallow(e, null);
818  return true;
819  }
820  }

+ Here is the caller graph for this function:

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

Definition at line 1281 of file SqlToRelConverter.java.

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

1281  {
1282  for (SqlNode node : valueList.getList()) {
1283  if (node instanceof SqlLiteral) {
1284  SqlLiteral lit = (SqlLiteral) node;
1285  if (lit.getValue() == null) {
1286  return true;
1287  }
1288  }
1289  }
1290  return false;
1291  }

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

References assert(), and org.apache.calcite.sql2rel.SqlToRelConverter.createAggImpl().

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

2648  {
2649  assert bb.root != null : "precondition: child != null";
2650  SqlNodeList groupList = select.getGroup();
2651  SqlNodeList selectList = select.getSelectList();
2652  SqlNode having = select.getHaving();
2653 
2654  final AggConverter aggConverter = new AggConverter(bb, select);
2655  createAggImpl(bb, aggConverter, selectList, groupList, having, orderExprList);
2656  }
int64_t const int32_t sz assert(dest)
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 2242 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, Experimental.String, org.apache.calcite.sql2rel.SqlToRelConverter.toRel(), and org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory.

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

2242  {
2243  final SqlOperator operator = call.getOperator();
2244  if (operator == SqlStdOperatorTable.TABLESAMPLE) {
2245  final String sampleName =
2246  SqlLiteral.unchain(call.operand(0)).getValueAs(String.class);
2247  datasetStack.push(sampleName);
2248  SqlCall cursorCall = call.operand(1);
2249  SqlNode query = cursorCall.operand(0);
2250  RelNode converted = convertQuery(query, false, false).rel;
2251  bb.setRoot(converted, false);
2252  datasetStack.pop();
2253  return;
2254  }
2255  replaceSubQueries(bb, call, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2256 
2257  // Expand table macro if possible. It's more efficient than
2258  // LogicalTableFunctionScan.
2259  final SqlCallBinding callBinding =
2260  new SqlCallBinding(bb.scope.getValidator(), bb.scope, call);
2261  if (operator instanceof SqlUserDefinedTableMacro) {
2262  final SqlUserDefinedTableMacro udf = (SqlUserDefinedTableMacro) operator;
2263  final TranslatableTable table = udf.getTable(typeFactory, callBinding.operands());
2264  final RelDataType rowType = table.getRowType(typeFactory);
2265  RelOptTable relOptTable =
2266  RelOptTableImpl.create(null, rowType, table, udf.getNameAsId().names);
2267  RelNode converted = toRel(relOptTable);
2268  bb.setRoot(converted, true);
2269  return;
2270  }
2271 
2272  Type elementType;
2273  if (operator instanceof SqlUserDefinedTableFunction) {
2274  SqlUserDefinedTableFunction udtf = (SqlUserDefinedTableFunction) operator;
2275  elementType = udtf.getElementType(typeFactory, callBinding.operands());
2276  } else {
2277  elementType = null;
2278  }
2279 
2280  RexNode rexCall = bb.convertExpression(call);
2281  final List<RelNode> inputs = bb.retrieveCursors();
2282  Set<RelColumnMapping> columnMappings = getColumnMappings(operator);
2283  LogicalTableFunctionScan callRel = LogicalTableFunctionScan.create(cluster,
2284  inputs,
2285  rexCall,
2286  elementType,
2287  validator.getValidatedNodeType(call),
2288  columnMappings);
2289  bb.setRoot(callRel, true);
2290  afterTableFunction(bb, call, callRel);
2291  }
void afterTableFunction(SqlToRelConverter.Blackboard bb, SqlCall call, LogicalTableFunctionScan callRel)
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 3235 of file SqlToRelConverter.java.

References assert(), org.apache.calcite.sql2rel.SqlToRelConverter.castNullLiteralIfNeeded(), org.apache.calcite.sql2rel.SqlToRelConverter.collectInsertTargets(), org.apache.calcite.sql2rel.SqlToRelConverter.createInsertBlackboard(), field(), org.apache.calcite.sql2rel.SqlToRelConverter.getInitializerFactory(), org.apache.calcite.sql2rel.SqlToRelConverter.getTargetTable(), Experimental.String, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

3235  {
3236  RelDataType sourceRowType = source.getRowType();
3237  final RexNode sourceRef = rexBuilder.makeRangeReference(sourceRowType, 0, false);
3238  final List<String> targetColumnNames = new ArrayList<>();
3239  final List<RexNode> columnExprs = new ArrayList<>();
3240  collectInsertTargets(call, sourceRef, targetColumnNames, columnExprs);
3241 
3242  final RelOptTable targetTable = getTargetTable(call);
3243  final RelDataType targetRowType = RelOptTableImpl.realRowType(targetTable);
3244  final List<RelDataTypeField> targetFields = targetRowType.getFieldList();
3245  final List<RexNode> sourceExps =
3246  new ArrayList<>(Collections.nCopies(targetFields.size(), null));
3247  final List<String> fieldNames =
3248  new ArrayList<>(Collections.nCopies(targetFields.size(), null));
3249 
3250  final InitializerExpressionFactory initializerFactory =
3251  getInitializerFactory(validator.getNamespace(call).getTable());
3252 
3253  // Walk the name list and place the associated value in the
3254  // expression list according to the ordinal value returned from
3255  // the table construct, leaving nulls in the list for columns
3256  // that are not referenced.
3257  final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
3258  for (Pair<String, RexNode> p : Pair.zip(targetColumnNames, columnExprs)) {
3259  RelDataTypeField field = nameMatcher.field(targetRowType, p.left);
3260  assert field != null : "column " + p.left + " not found";
3261  sourceExps.set(field.getIndex(), p.right);
3262  }
3263 
3264  // Lazily create a blackboard that contains all non-generated columns.
3265  final Supplier<Blackboard> bb =
3266  () -> createInsertBlackboard(targetTable, sourceRef, targetColumnNames);
3267 
3268  // Walk the expression list and get default values for any columns
3269  // that were not supplied in the statement. Get field names too.
3270  for (int i = 0; i < targetFields.size(); ++i) {
3271  final RelDataTypeField field = targetFields.get(i);
3272  final String fieldName = field.getName();
3273  fieldNames.set(i, fieldName);
3274  if (sourceExps.get(i) == null || sourceExps.get(i).getKind() == SqlKind.DEFAULT) {
3275  sourceExps.set(
3276  i, initializerFactory.newColumnDefaultValue(targetTable, i, bb.get()));
3277 
3278  // bare nulls are dangerous in the wrong hands
3279  sourceExps.set(i, castNullLiteralIfNeeded(sourceExps.get(i), field.getType()));
3280  }
3281  }
3282 
3283  return relBuilder.push(source).projectNamed(sourceExps, fieldNames, false).build();
3284  }
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)
int64_t const int32_t sz assert(dest)
Blackboard createInsertBlackboard(RelOptTable targetTable, RexNode sourceRef, List< String > targetColumnNames)

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

References assert(), org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(), and org.apache.calcite.sql2rel.SqlToRelConverter.SubQuery.node.

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

3620  {
3621  final SqlCall cursorCall = (SqlCall) subQuery.node;
3622  assert cursorCall.operandCount() == 1;
3623  SqlNode query = cursorCall.operand(0);
3624  RelNode converted = convertQuery(query, false, false).rel;
3625  int iCursor = bb.cursors.size();
3626  bb.cursors.add(converted);
3627  subQuery.expr = new RexInputRef(iCursor, converted.getRowType());
3628  return converted;
3629  }
int64_t const int32_t sz assert(dest)
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 3408 of file SqlToRelConverter.java.

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

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

3408  {
3409  RelOptTable targetTable = getTargetTable(call);
3410  RelNode sourceRel = convertSelect(call.getSourceSelect(), false);
3411  return LogicalTableModify.create(targetTable,
3412  catalogReader,
3413  sourceRel,
3414  LogicalTableModify.Operation.DELETE,
3415  null,
3416  null,
3417  false);
3418  }
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 2852 of file SqlToRelConverter.java.

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

2852  {
2853  // REVIEW jvs 8-Jan-2005: dynamic params may be encountered out of
2854  // order. Should probably cross-check with the count from the parser
2855  // at the end and make sure they all got filled in. Why doesn't List
2856  // have a resize() method?!? Make this a utility.
2857  while (dynamicParam.getIndex() >= dynamicParamSqlNodes.size()) {
2858  dynamicParamSqlNodes.add(null);
2859  }
2860 
2861  dynamicParamSqlNodes.set(dynamicParam.getIndex(), dynamicParam);
2862  return rexBuilder.makeDynamicParam(
2863  getDynamicParamType(dynamicParam.getIndex()), dynamicParam.getIndex());
2864  }

+ 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 1475 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().

1479  {
1480  final SqlValidatorScope seekScope = (seek instanceof SqlSelect)
1481  ? validator.getSelectScope((SqlSelect) seek)
1482  : null;
1483  final Blackboard seekBb = createBlackboard(seekScope, null, false);
1484  RelNode seekRel = convertQueryOrInList(seekBb, seek, targetDataType);
1485 
1486  return RelOptUtil.createExistsPlan(seekRel, subQueryType, logic, notIn, relBuilder);
1487  }
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 1742 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().

1742  {
1743  Map<String, RelDataType> nameToTypeMap = Collections.emptyMap();
1744  final ParameterScope scope =
1745  new ParameterScope((SqlValidatorImpl) validator, nameToTypeMap);
1746  final Blackboard bb = createBlackboard(scope, null, false);
1747  return bb.convertExpression(node);
1748  }
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 1761 of file SqlToRelConverter.java.

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

1761  {
1762  final Map<String, RelDataType> nameToTypeMap = new HashMap<>();
1763  for (Map.Entry<String, RexNode> entry : nameToNodeMap.entrySet()) {
1764  nameToTypeMap.put(entry.getKey(), entry.getValue().getType());
1765  }
1766  final ParameterScope scope =
1767  new ParameterScope((SqlValidatorImpl) validator, nameToTypeMap);
1768  final Blackboard bb = createBlackboard(scope, nameToNodeMap, false);
1769  return bb.convertExpression(node);
1770  }
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 1784 of file SqlToRelConverter.java.

1784  {
1785  return null;
1786  }
void org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom ( Blackboard  bb,
SqlNode  from 
)
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>

Definition at line 1879 of file SqlToRelConverter.java.

References assert(), org.apache.calcite.sql2rel.SqlToRelConverter.cluster, org.apache.calcite.sql2rel.SqlToRelConverter.convertCollectionTable(), 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.convertUsing(), org.apache.calcite.sql2rel.SqlToRelConverter.convertValuesImpl(), org.apache.calcite.sql2rel.SqlToRelConverter.createBlackboard(), org.apache.calcite.sql2rel.SqlToRelConverter.createJoin(), join(), org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries(), org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.root, org.apache.calcite.sql2rel.SqlToRelConverter.snapshotTemporalTable(), and Experimental.String.

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

1879  {
1880  if (from == null) {
1881  bb.setRoot(LogicalValues.createOneRow(cluster), false);
1882  return;
1883  }
1884 
1885  final SqlCall call;
1886  final SqlNode[] operands;
1887  switch (from.getKind()) {
1888  case MATCH_RECOGNIZE:
1889  convertMatchRecognize(bb, (SqlCall) from);
1890  return;
1891 
1892  case AS:
1893  call = (SqlCall) from;
1894  convertFrom(bb, call.operand(0));
1895  if (call.operandCount() > 2
1896  && (bb.root instanceof Values || bb.root instanceof Uncollect)) {
1897  final List<String> fieldNames = new ArrayList<>();
1898  for (SqlNode node : Util.skip(call.getOperandList(), 2)) {
1899  fieldNames.add(((SqlIdentifier) node).getSimple());
1900  }
1901  bb.setRoot(relBuilder.push(bb.root).rename(fieldNames).build(), true);
1902  }
1903  return;
1904 
1905  case WITH_ITEM:
1906  convertFrom(bb, ((SqlWithItem) from).query);
1907  return;
1908 
1909  case WITH:
1910  convertFrom(bb, ((SqlWith) from).body);
1911  return;
1912 
1913  case TABLESAMPLE:
1914  operands = ((SqlBasicCall) from).getOperands();
1915  SqlSampleSpec sampleSpec = SqlLiteral.sampleValue(operands[1]);
1916  if (sampleSpec instanceof SqlSampleSpec.SqlSubstitutionSampleSpec) {
1917  String sampleName =
1918  ((SqlSampleSpec.SqlSubstitutionSampleSpec) sampleSpec).getName();
1919  datasetStack.push(sampleName);
1920  convertFrom(bb, operands[0]);
1921  datasetStack.pop();
1922  } else if (sampleSpec instanceof SqlSampleSpec.SqlTableSampleSpec) {
1923  SqlSampleSpec.SqlTableSampleSpec tableSampleSpec =
1924  (SqlSampleSpec.SqlTableSampleSpec) sampleSpec;
1925  convertFrom(bb, operands[0]);
1926  RelOptSamplingParameters params =
1927  new RelOptSamplingParameters(tableSampleSpec.isBernoulli(),
1928  tableSampleSpec.getSamplePercentage(),
1929  tableSampleSpec.isRepeatable(),
1930  tableSampleSpec.getRepeatableSeed());
1931  bb.setRoot(new Sample(cluster, bb.root, params), false);
1932  } else {
1933  throw new AssertionError("unknown TABLESAMPLE type: " + sampleSpec);
1934  }
1935  return;
1936 
1937  case IDENTIFIER:
1938  convertIdentifier(bb, (SqlIdentifier) from, null);
1939  return;
1940 
1941  case EXTEND:
1942  call = (SqlCall) from;
1943  SqlIdentifier id = (SqlIdentifier) call.getOperandList().get(0);
1944  SqlNodeList extendedColumns = (SqlNodeList) call.getOperandList().get(1);
1945  convertIdentifier(bb, id, extendedColumns);
1946  return;
1947 
1948  case SNAPSHOT:
1949  snapshotTemporalTable(bb, (SqlCall) from);
1950  return;
1951 
1952  case JOIN:
1953  final SqlJoin join = (SqlJoin) from;
1954  final SqlValidatorScope scope = validator.getJoinScope(from);
1955  final Blackboard fromBlackboard = createBlackboard(scope, null, false);
1956  SqlNode left = join.getLeft();
1957  SqlNode right = join.getRight();
1958  final boolean isNatural = join.isNatural();
1959  final JoinType joinType = join.getJoinType();
1960  final SqlValidatorScope leftScope = Util.first(
1961  validator.getJoinScope(left), ((DelegatingScope) bb.scope).getParent());
1962  final Blackboard leftBlackboard = createBlackboard(leftScope, null, false);
1963  final SqlValidatorScope rightScope = Util.first(
1964  validator.getJoinScope(right), ((DelegatingScope) bb.scope).getParent());
1965  final Blackboard rightBlackboard = createBlackboard(rightScope, null, false);
1966  convertFrom(leftBlackboard, left);
1967  RelNode leftRel = leftBlackboard.root;
1968  convertFrom(rightBlackboard, right);
1969  RelNode rightRel = rightBlackboard.root;
1970  JoinRelType convertedJoinType = convertJoinType(joinType);
1971  RexNode conditionExp;
1972  final SqlValidatorNamespace leftNamespace = validator.getNamespace(left);
1973  final SqlValidatorNamespace rightNamespace = validator.getNamespace(right);
1974  if (isNatural) {
1975  final RelDataType leftRowType = leftNamespace.getRowType();
1976  final RelDataType rightRowType = rightNamespace.getRowType();
1977  final List<String> columnList = SqlValidatorUtil.deriveNaturalJoinColumnList(
1978  catalogReader.nameMatcher(), leftRowType, rightRowType);
1979  conditionExp = convertUsing(leftNamespace, rightNamespace, columnList);
1980  } else {
1981  conditionExp = convertJoinCondition(fromBlackboard,
1982  leftNamespace,
1983  rightNamespace,
1984  join.getCondition(),
1985  join.getConditionType(),
1986  leftRel,
1987  rightRel);
1988  }
1989 
1990  final RelNode joinRel = createJoin(
1991  fromBlackboard, leftRel, rightRel, conditionExp, convertedJoinType);
1992  bb.setRoot(joinRel, false);
1993  return;
1994 
1995  case SELECT:
1996  case INTERSECT:
1997  case EXCEPT:
1998  case UNION:
1999  final RelNode rel = convertQueryRecursive(from, false, null).project();
2000  bb.setRoot(rel, true);
2001  return;
2002 
2003  case VALUES:
2004  convertValuesImpl(bb, (SqlCall) from, null);
2005  return;
2006 
2007  case UNNEST:
2008  call = (SqlCall) from;
2009  final List<SqlNode> nodes = call.getOperandList();
2010  final SqlUnnestOperator operator = (SqlUnnestOperator) call.getOperator();
2011  for (SqlNode node : nodes) {
2012  replaceSubQueries(bb, node, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2013  }
2014  final List<RexNode> exprs = new ArrayList<>();
2015  final List<String> fieldNames = new ArrayList<>();
2016  for (Ord<SqlNode> node : Ord.zip(nodes)) {
2017  exprs.add(bb.convertExpression(node.e));
2018  fieldNames.add(validator.deriveAlias(node.e, node.i));
2019  }
2020  RelNode child = (null != bb.root) ? bb.root : LogicalValues.createOneRow(cluster);
2021  relBuilder.push(child).projectNamed(exprs, fieldNames, false);
2022 
2023  Uncollect uncollect = new Uncollect(cluster,
2024  cluster.traitSetOf(Convention.NONE),
2025  relBuilder.build(),
2026  operator.withOrdinality);
2027  bb.setRoot(uncollect, true);
2028  return;
2029 
2030  case COLLECTION_TABLE:
2031  call = (SqlCall) from;
2032 
2033  // Dig out real call; TABLE() wrapper is just syntactic.
2034  assert call.getOperandList().size() == 1;
2035  final SqlCall call2 = call.operand(0);
2036  convertCollectionTable(bb, call2);
2037  return;
2038 
2039  default:
2040  throw new AssertionError("not a join operator " + from);
2041  }
2042  }
void snapshotTemporalTable(Blackboard bb, SqlCall call)
JoinType
Definition: sqldefs.h:98
void convertCollectionTable(Blackboard bb, SqlCall call)
std::string join(T const &container, std::string const &delim)
void convertValuesImpl(Blackboard bb, SqlCall values, RelDataType targetRowType)
void convertFrom(Blackboard bb, SqlNode from)
RelRoot convertQueryRecursive(SqlNode query, boolean top, RelDataType targetRowType)
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 replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
void convertIdentifier(Blackboard bb, SqlIdentifier id, SqlNodeList extendedColumns)
int64_t const int32_t sz assert(dest)
void convertMatchRecognize(Blackboard bb, SqlCall call)
bool g_enable_watchdog false
Definition: Execute.cpp:71
RelNode createJoin(Blackboard bb, RelNode leftRel, RelNode rightRel, RexNode joinCond, JoinRelType joinType)
Blackboard createBlackboard(SqlValidatorScope scope, Map< String, RexNode > nameToNodeMap, boolean top)
static JoinRelType convertJoinType(JoinType joinType)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 2212 of file SqlToRelConverter.java.

References assert(), org.apache.calcite.sql2rel.SqlToRelConverter.catalogReader, org.apache.calcite.sql2rel.SqlToRelConverter.cluster, org.apache.calcite.sql2rel.SqlToRelConverter.config, org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(), org.apache.calcite.sql2rel.SqlToRelConverter.datasetStack, Experimental.String, org.apache.calcite.sql2rel.SqlToRelConverter.toRel(), and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

2213  {
2214  final SqlValidatorNamespace fromNamespace = validator.getNamespace(id).resolve();
2215  if (fromNamespace.getNode() != null) {
2216  convertFrom(bb, fromNamespace.getNode());
2217  return;
2218  }
2219  final String datasetName = datasetStack.isEmpty() ? null : datasetStack.peek();
2220  final boolean[] usedDataset = {false};
2221  RelOptTable table = SqlValidatorUtil.getRelOptTable(
2222  fromNamespace, catalogReader, datasetName, usedDataset);
2223  if (extendedColumns != null && extendedColumns.size() > 0) {
2224  assert table != null;
2225  final SqlValidatorTable validatorTable = table.unwrap(SqlValidatorTable.class);
2226  final List<RelDataTypeField> extendedFields = SqlValidatorUtil.getExtendedColumns(
2227  validator, validatorTable, extendedColumns);
2228  table = table.extend(extendedFields);
2229  }
2230  final RelNode tableRel;
2231  if (config.isConvertTableAccess()) {
2232  tableRel = toRel(table);
2233  } else {
2234  tableRel = LogicalTableScan.create(cluster, table);
2235  }
2236  bb.setRoot(tableRel, true);
2237  if (usedDataset[0]) {
2238  bb.setDataset(datasetName);
2239  }
2240  }
void convertFrom(Blackboard bb, SqlNode from)
int64_t const int32_t sz assert(dest)

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

References org.apache.calcite.sql2rel.SqlToRelConverter.adjustInputRef(), assert(), org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.isPatternVarRef, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.scope, and Experimental.String.

3540  {
3541  // first check for reserved identifiers like CURRENT_USER
3542  final SqlCall call = bb.getValidator().makeNullaryCall(identifier);
3543  if (call != null) {
3544  return bb.convertExpression(call);
3545  }
3546 
3547  String pv = null;
3548  if (bb.isPatternVarRef && identifier.names.size() > 1) {
3549  pv = identifier.names.get(0);
3550  }
3551 
3552  final SqlQualified qualified;
3553  if (bb.scope != null) {
3554  qualified = bb.scope.fullyQualify(identifier);
3555  } else {
3556  qualified = SqlQualified.create(null, 1, null, identifier);
3557  }
3558  final Pair<RexNode, Map<String, Integer>> e0 = bb.lookupExp(qualified);
3559  RexNode e = e0.left;
3560  for (String name : qualified.suffix()) {
3561  if (e == e0.left && e0.right != null) {
3562  int i = e0.right.get(name);
3563  e = rexBuilder.makeFieldAccess(e, i);
3564  } else {
3565  final boolean caseSensitive = true; // name already fully-qualified
3566  if (identifier.isStar() && bb.scope instanceof MatchRecognizeScope) {
3567  e = rexBuilder.makeFieldAccess(e, 0);
3568  } else {
3569  e = rexBuilder.makeFieldAccess(e, name, caseSensitive);
3570  }
3571  }
3572  }
3573  if (e instanceof RexInputRef) {
3574  // adjust the type to account for nulls introduced by outer joins
3575  e = adjustInputRef(bb, (RexInputRef) e);
3576  if (pv != null) {
3577  e = RexPatternFieldRef.of(pv, (RexInputRef) e);
3578  }
3579  }
3580 
3581  if (e0.left instanceof RexCorrelVariable) {
3582  assert e instanceof RexFieldAccess;
3583  final RexNode prev = bb.mapCorrelateToRex.put(
3584  ((RexCorrelVariable) e0.left).id, (RexFieldAccess) e);
3585  assert prev == null;
3586  }
3587  return e;
3588  }
int64_t const int32_t sz assert(dest)
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 3070 of file SqlToRelConverter.java.

References assert(), 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().

3070  {
3071  RelOptTable targetTable = getTargetTable(call);
3072 
3073  final RelDataType targetRowType = validator.getValidatedNodeType(call);
3074  assert targetRowType != null;
3075  RelNode sourceRel =
3076  convertQueryRecursive(call.getSource(), false, targetRowType).project();
3077  RelNode massagedRel = convertColumnList(call, sourceRel);
3078 
3079  return createModify(targetTable, massagedRel);
3080  }
RelRoot convertQueryRecursive(SqlNode query, boolean top, RelDataType targetRowType)
RelNode createModify(RelOptTable targetTable, RelNode source)
int64_t const int32_t sz assert(dest)
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 1381 of file SqlToRelConverter.java.

References assert(), org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.convertExpression(), org.apache.calcite.sql2rel.SqlToRelConverter.ensureSqlType(), and org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder.

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

1384  {
1385  final List<RexNode> comparisons = new ArrayList<>();
1386  for (SqlNode rightVals : valuesList) {
1387  RexNode rexComparison;
1388  final SqlOperator comparisonOp;
1389  if (op instanceof SqlQuantifyOperator) {
1390  comparisonOp = RelOptUtil.op(
1391  ((SqlQuantifyOperator) op).comparisonKind, SqlStdOperatorTable.EQUALS);
1392  } else {
1393  comparisonOp = SqlStdOperatorTable.EQUALS;
1394  }
1395  if (leftKeys.size() == 1) {
1396  rexComparison = rexBuilder.makeCall(comparisonOp,
1397  leftKeys.get(0),
1398  ensureSqlType(
1399  leftKeys.get(0).getType(), bb.convertExpression(rightVals)));
1400  } else {
1401  assert rightVals instanceof SqlCall;
1402  final SqlBasicCall call = (SqlBasicCall) rightVals;
1403  assert (call.getOperator() instanceof SqlRowOperator)
1404  && call.operandCount() == leftKeys.size();
1405  rexComparison = RexUtil.composeConjunction(rexBuilder,
1406  Iterables.transform(Pair.zip(leftKeys, call.getOperandList()),
1407  pair
1408  -> rexBuilder.makeCall(comparisonOp,
1409  pair.left,
1410  ensureSqlType(pair.left.getType(),
1411  bb.convertExpression(pair.right)))));
1412  }
1413  comparisons.add(rexComparison);
1414  }
1415 
1416  switch (op.kind) {
1417  case ALL:
1418  return RexUtil.composeConjunction(rexBuilder, comparisons, true);
1419  case NOT_IN:
1420  return rexBuilder.makeCall(SqlStdOperatorTable.NOT,
1421  RexUtil.composeDisjunction(rexBuilder, comparisons, true));
1422  case IN:
1423  case SOME:
1424  return RexUtil.composeDisjunction(rexBuilder, comparisons, true);
1425  default:
1426  throw new AssertionError();
1427  }
1428  }
RexNode ensureSqlType(RelDataType type, RexNode node)
int64_t const int32_t sz assert(dest)

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

References org.apache.calcite.sql2rel.SqlToRelConverter.convertUsing(), org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries(), and Experimental.String.

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

2563  {
2564  if (condition == null) {
2565  return rexBuilder.makeLiteral(true);
2566  }
2567  bb.setRoot(ImmutableList.of(leftRel, rightRel));
2568  replaceSubQueries(bb, condition, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
2569  switch (conditionType) {
2570  case ON:
2571  bb.setRoot(ImmutableList.of(leftRel, rightRel));
2572  return bb.convertExpression(condition);
2573  case USING:
2574  final SqlNodeList list = (SqlNodeList) condition;
2575  final List<String> nameList = new ArrayList<>();
2576  for (SqlNode columnName : list) {
2577  final SqlIdentifier id = (SqlIdentifier) columnName;
2578  String name = id.getSimple();
2579  nameList.add(name);
2580  }
2581  return convertUsing(leftNamespace, rightNamespace, nameList);
2582  default:
2583  throw Util.unexpected(conditionType);
2584  }
2585  }
RexNode convertUsing(SqlValidatorNamespace leftNamespace, SqlValidatorNamespace rightNamespace, List< String > nameList)
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:

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

Definition at line 2617 of file SqlToRelConverter.java.

References INNER, and LEFT.

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

2617  {
2618  switch (joinType) {
2619  case COMMA:
2620  case INNER:
2621  case CROSS:
2622  return JoinRelType.INNER;
2623  case FULL:
2624  return JoinRelType.FULL;
2625  case LEFT:
2626  return JoinRelType.LEFT;
2627  case RIGHT:
2628  return JoinRelType.RIGHT;
2629  default:
2630  throw Util.unexpected(joinType);
2631  }
2632  }

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

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

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

1580  {
1581  if (!(sqlNode instanceof SqlLiteral)) {
1582  return null;
1583  }
1584  RelDataTypeField field = rowType.getFieldList().get(iField);
1585  RelDataType type = field.getType();
1586  if (type.isStruct()) {
1587  // null literals for weird stuff like UDT's need
1588  // special handling during type flattening, so
1589  // don't use LogicalValues for those
1590  return null;
1591  }
1592 
1593  RexNode literalExpr = exprConverter.convertLiteral(bb, (SqlLiteral) sqlNode);
1594 
1595  if (!(literalExpr instanceof RexLiteral)) {
1596  assert literalExpr.isA(SqlKind.CAST);
1597  RexNode child = ((RexCall) literalExpr).getOperands().get(0);
1598  assert RexLiteral.isNullLiteral(child);
1599 
1600  // NOTE jvs 22-Nov-2006: we preserve type info
1601  // in LogicalValues digest, so it's OK to lose it here
1602  return (RexLiteral) child;
1603  }
1604 
1605  RexLiteral literal = (RexLiteral) literalExpr;
1606 
1607  Comparable value = literal.getValue();
1608 
1609  if (SqlTypeUtil.isExactNumeric(type) && SqlTypeUtil.hasScale(type)) {
1610  BigDecimal roundedValue =
1611  NumberUtil.rescaleBigDecimal((BigDecimal) value, type.getScale());
1612  return rexBuilder.makeExactLiteral(roundedValue, type);
1613  }
1614 
1615  if ((value instanceof NlsString) && (type.getSqlTypeName() == SqlTypeName.CHAR)) {
1616  // pad fixed character type
1617  NlsString unpadded = (NlsString) value;
1618  return rexBuilder.makeCharLiteral(
1619  new NlsString(Spaces.padRight(unpadded.getValue(), type.getPrecision()),
1620  unpadded.getCharsetName(),
1621  unpadded.getCollation()));
1622  }
1623  return literal;
1624  }
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
int64_t const int32_t sz assert(dest)

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

References assert(), 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 Experimental.String.

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

2044  {
2045  final SqlMatchRecognize matchRecognize = (SqlMatchRecognize) call;
2046  final SqlValidatorNamespace ns = validator.getNamespace(matchRecognize);
2047  final SqlValidatorScope scope = validator.getMatchRecognizeScope(matchRecognize);
2048 
2049  final Blackboard matchBb = createBlackboard(scope, null, false);
2050  final RelDataType rowType = ns.getRowType();
2051  // convert inner query, could be a table name or a derived table
2052  SqlNode expr = matchRecognize.getTableRef();
2053  convertFrom(matchBb, expr);
2054  final RelNode input = matchBb.root;
2055 
2056  // PARTITION BY
2057  final SqlNodeList partitionList = matchRecognize.getPartitionList();
2058  final ImmutableBitSet.Builder partitionKeys = ImmutableBitSet.builder();
2059  for (SqlNode partition : partitionList) {
2060  RexNode e = matchBb.convertExpression(partition);
2061  partitionKeys.set(((RexInputRef) e).getIndex());
2062  }
2063 
2064  // ORDER BY
2065  final SqlNodeList orderList = matchRecognize.getOrderList();
2066  final List<RelFieldCollation> orderKeys = new ArrayList<>();
2067  for (SqlNode order : orderList) {
2068  final RelFieldCollation.Direction direction;
2069  switch (order.getKind()) {
2070  case DESCENDING:
2071  direction = RelFieldCollation.Direction.DESCENDING;
2072  order = ((SqlCall) order).operand(0);
2073  break;
2074  case NULLS_FIRST:
2075  case NULLS_LAST:
2076  throw new AssertionError();
2077  default:
2078  direction = RelFieldCollation.Direction.ASCENDING;
2079  break;
2080  }
2081  final RelFieldCollation.NullDirection nullDirection =
2082  validator.getDefaultNullCollation().last(desc(direction))
2083  ? RelFieldCollation.NullDirection.LAST
2084  : RelFieldCollation.NullDirection.FIRST;
2085  RexNode e = matchBb.convertExpression(order);
2086  orderKeys.add(new RelFieldCollation(
2087  ((RexInputRef) e).getIndex(), direction, nullDirection));
2088  }
2089  final RelCollation orders = cluster.traitSet().canonize(RelCollations.of(orderKeys));
2090 
2091  // convert pattern
2092  final Set<String> patternVarsSet = new HashSet<>();
2093  SqlNode pattern = matchRecognize.getPattern();
2094  final SqlBasicVisitor<RexNode> patternVarVisitor = new SqlBasicVisitor<RexNode>() {
2095  @Override
2096  public RexNode visit(SqlCall call) {
2097  List<SqlNode> operands = call.getOperandList();
2098  List<RexNode> newOperands = new ArrayList<>();
2099  for (SqlNode node : operands) {
2100  newOperands.add(node.accept(this));
2101  }
2102  return rexBuilder.makeCall(
2103  validator.getUnknownType(), call.getOperator(), newOperands);
2104  }
2105 
2106  @Override
2107  public RexNode visit(SqlIdentifier id) {
2108  assert id.isSimple();
2109  patternVarsSet.add(id.getSimple());
2110  return rexBuilder.makeLiteral(id.getSimple());
2111  }
2112 
2113  @Override
2114  public RexNode visit(SqlLiteral literal) {
2115  if (literal instanceof SqlNumericLiteral) {
2116  return rexBuilder.makeExactLiteral(BigDecimal.valueOf(literal.intValue(true)));
2117  } else {
2118  return rexBuilder.makeLiteral(literal.booleanValue());
2119  }
2120  }
2121  };
2122  final RexNode patternNode = pattern.accept(patternVarVisitor);
2123 
2124  SqlLiteral interval = matchRecognize.getInterval();
2125  RexNode intervalNode = null;
2126  if (interval != null) {
2127  intervalNode = matchBb.convertLiteral(interval);
2128  }
2129 
2130  // convert subset
2131  final SqlNodeList subsets = matchRecognize.getSubsetList();
2132  final Map<String, TreeSet<String>> subsetMap = new HashMap<>();
2133  for (SqlNode node : subsets) {
2134  List<SqlNode> operands = ((SqlCall) node).getOperandList();
2135  SqlIdentifier left = (SqlIdentifier) operands.get(0);
2136  patternVarsSet.add(left.getSimple());
2137  SqlNodeList rights = (SqlNodeList) operands.get(1);
2138  final TreeSet<String> list = new TreeSet<>();
2139  for (SqlNode right : rights) {
2140  assert right instanceof SqlIdentifier;
2141  list.add(((SqlIdentifier) right).getSimple());
2142  }
2143  subsetMap.put(left.getSimple(), list);
2144  }
2145 
2146  SqlNode afterMatch = matchRecognize.getAfter();
2147  if (afterMatch == null) {
2148  afterMatch =
2149  SqlMatchRecognize.AfterOption.SKIP_TO_NEXT_ROW.symbol(SqlParserPos.ZERO);
2150  }
2151 
2152  final RexNode after;
2153  if (afterMatch instanceof SqlCall) {
2154  List<SqlNode> operands = ((SqlCall) afterMatch).getOperandList();
2155  SqlOperator operator = ((SqlCall) afterMatch).getOperator();
2156  assert operands.size() == 1;
2157  SqlIdentifier id = (SqlIdentifier) operands.get(0);
2158  assert patternVarsSet.contains(id.getSimple())
2159  : id.getSimple()
2160  + " not defined in pattern";
2161  RexNode rex = rexBuilder.makeLiteral(id.getSimple());
2162  after = rexBuilder.makeCall(
2163  validator.getUnknownType(), operator, ImmutableList.of(rex));
2164  } else {
2165  after = matchBb.convertExpression(afterMatch);
2166  }
2167 
2168  matchBb.setPatternVarRef(true);
2169 
2170  // convert measures
2171  final ImmutableMap.Builder<String, RexNode> measureNodes = ImmutableMap.builder();
2172  for (SqlNode measure : matchRecognize.getMeasureList()) {
2173  List<SqlNode> operands = ((SqlCall) measure).getOperandList();
2174  String alias = ((SqlIdentifier) operands.get(1)).getSimple();
2175  RexNode rex = matchBb.convertExpression(operands.get(0));
2176  measureNodes.put(alias, rex);
2177  }
2178 
2179  // convert definitions
2180  final ImmutableMap.Builder<String, RexNode> definitionNodes = ImmutableMap.builder();
2181  for (SqlNode def : matchRecognize.getPatternDefList()) {
2182  replaceSubQueries(matchBb, def, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
2183  List<SqlNode> operands = ((SqlCall) def).getOperandList();
2184  String alias = ((SqlIdentifier) operands.get(1)).getSimple();
2185  RexNode rex = matchBb.convertExpression(operands.get(0));
2186  definitionNodes.put(alias, rex);
2187  }
2188 
2189  final SqlLiteral rowsPerMatch = matchRecognize.getRowsPerMatch();
2190  final boolean allRows = rowsPerMatch != null
2191  && rowsPerMatch.getValue() == SqlMatchRecognize.RowsPerMatchOption.ALL_ROWS;
2192 
2193  matchBb.setPatternVarRef(false);
2194 
2195  final RelFactories.MatchFactory factory = RelFactories.DEFAULT_MATCH_FACTORY;
2196  final RelNode rel = factory.createMatch(input,
2197  patternNode,
2198  rowType,
2199  matchRecognize.getStrictStart().booleanValue(),
2200  matchRecognize.getStrictEnd().booleanValue(),
2201  definitionNodes.build(),
2202  measureNodes.build(),
2203  after,
2204  subsetMap,
2205  allRows,
2206  partitionKeys.build(),
2207  orders,
2208  intervalNode);
2209  bb.setRoot(rel, false);
2210  }
void convertFrom(Blackboard bb, SqlNode from)
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
int64_t const int32_t sz assert(dest)
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 3456 of file SqlToRelConverter.java.

References assert(), 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().

3456  {
3457  RelOptTable targetTable = getTargetTable(call);
3458 
3459  // convert update column list from SqlIdentifier to String
3460  final List<String> targetColumnNameList = new ArrayList<>();
3461  final RelDataType targetRowType = targetTable.getRowType();
3462  SqlUpdate updateCall = call.getUpdateCall();
3463  if (updateCall != null) {
3464  for (SqlNode targetColumn : updateCall.getTargetColumnList()) {
3465  SqlIdentifier id = (SqlIdentifier) targetColumn;
3466  RelDataTypeField field = SqlValidatorUtil.getTargetField(
3467  targetRowType, typeFactory, id, catalogReader, targetTable);
3468  assert field != null : "column " + id.toString() + " not found";
3469  targetColumnNameList.add(field.getName());
3470  }
3471  }
3472 
3473  // replace the projection of the source select with a
3474  // projection that contains the following:
3475  // 1) the expressions corresponding to the new insert row (if there is
3476  // an insert)
3477  // 2) all columns from the target table (if there is an update)
3478  // 3) the set expressions in the update call (if there is an update)
3479 
3480  // first, convert the merge's source select to construct the columns
3481  // from the target table and the set expressions in the update call
3482  RelNode mergeSourceRel = convertSelect(call.getSourceSelect(), false);
3483 
3484  // then, convert the insert statement so we can get the insert
3485  // values expressions
3486  SqlInsert insertCall = call.getInsertCall();
3487  int nLevel1Exprs = 0;
3488  List<RexNode> level1InsertExprs = null;
3489  List<RexNode> level2InsertExprs = null;
3490  if (insertCall != null) {
3491  RelNode insertRel = convertInsert(insertCall);
3492 
3493  // if there are 2 level of projections in the insert source, combine
3494  // them into a single project; level1 refers to the topmost project;
3495  // the level1 projection contains references to the level2
3496  // expressions, except in the case where no target expression was
3497  // provided, in which case, the expression is the default value for
3498  // the column; or if the expressions directly map to the source
3499  // table
3500  level1InsertExprs = ((LogicalProject) insertRel.getInput(0)).getProjects();
3501  if (insertRel.getInput(0).getInput(0) instanceof LogicalProject) {
3502  level2InsertExprs =
3503  ((LogicalProject) insertRel.getInput(0).getInput(0)).getProjects();
3504  }
3505  nLevel1Exprs = level1InsertExprs.size();
3506  }
3507 
3508  LogicalJoin join = (LogicalJoin) mergeSourceRel.getInput(0);
3509  int nSourceFields = join.getLeft().getRowType().getFieldCount();
3510  final List<RexNode> projects = new ArrayList<>();
3511  for (int level1Idx = 0; level1Idx < nLevel1Exprs; level1Idx++) {
3512  if ((level2InsertExprs != null)
3513  && (level1InsertExprs.get(level1Idx) instanceof RexInputRef)) {
3514  int level2Idx = ((RexInputRef) level1InsertExprs.get(level1Idx)).getIndex();
3515  projects.add(level2InsertExprs.get(level2Idx));
3516  } else {
3517  projects.add(level1InsertExprs.get(level1Idx));
3518  }
3519  }
3520  if (updateCall != null) {
3521  final LogicalProject project = (LogicalProject) mergeSourceRel;
3522  projects.addAll(Util.skip(project.getProjects(), nSourceFields));
3523  }
3524 
3525  relBuilder.push(join).project(projects);
3526 
3527  return LogicalTableModify.create(targetTable,
3528  catalogReader,
3529  relBuilder.build(),
3530  LogicalTableModify.Operation.MERGE,
3531  targetColumnNameList,
3532  null,
3533  false);
3534  }
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
int64_t const int32_t sz assert(dest)
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 3631 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(), 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().

3631  {
3632  // NOTE: Wael 2/04/05: this implementation is not the most efficient in
3633  // terms of planning since it generates XOs that can be reduced.
3634  final List<Object> joinList = new ArrayList<>();
3635  List<SqlNode> lastList = new ArrayList<>();
3636  for (int i = 0; i < operands.size(); i++) {
3637  SqlNode operand = operands.get(i);
3638  if (!(operand instanceof SqlCall)) {
3639  lastList.add(operand);
3640  continue;
3641  }
3642 
3643  final SqlCall call = (SqlCall) operand;
3644  final RelNode input;
3645  switch (call.getKind()) {
3646  case MULTISET_VALUE_CONSTRUCTOR:
3647  case ARRAY_VALUE_CONSTRUCTOR:
3648  final SqlNodeList list =
3649  new SqlNodeList(call.getOperandList(), call.getParserPosition());
3650  CollectNamespace nss = (CollectNamespace) validator.getNamespace(call);
3651  Blackboard usedBb;
3652  if (null != nss) {
3653  usedBb = createBlackboard(nss.getScope(), null, false);
3654  } else {
3655  usedBb = createBlackboard(new ListScope(bb.scope) {
3656  public SqlNode getNode() {
3657  return call;
3658  }
3659  }, null, false);
3660  }
3661  RelDataType multisetType = validator.getValidatedNodeType(call);
3662  ((SqlValidatorImpl) validator)
3663  .setValidatedNodeType(list, multisetType.getComponentType());
3664  input = convertQueryOrInList(usedBb, list, null);
3665  break;
3666  case MULTISET_QUERY_CONSTRUCTOR:
3667  case ARRAY_QUERY_CONSTRUCTOR:
3668  final RelRoot root = convertQuery(call.operand(0), false, true);
3669  input = root.rel;
3670  break;
3671  default:
3672  lastList.add(operand);
3673  continue;
3674  }
3675 
3676  if (lastList.size() > 0) {
3677  joinList.add(lastList);
3678  }
3679  lastList = new ArrayList<>();
3680  Collect collect = new Collect(cluster,
3681  cluster.traitSetOf(Convention.NONE),
3682  input,
3683  validator.deriveAlias(call, i));
3684  joinList.add(collect);
3685  }
3686 
3687  if (joinList.size() == 0) {
3688  joinList.add(lastList);
3689  }
3690 
3691  for (int i = 0; i < joinList.size(); i++) {
3692  Object o = joinList.get(i);
3693  if (o instanceof List) {
3694  @SuppressWarnings("unchecked")
3695  List<SqlNode> projectList = (List<SqlNode>) o;
3696  final List<RexNode> selectList = new ArrayList<>();
3697  final List<String> fieldNameList = new ArrayList<>();
3698  for (int j = 0; j < projectList.size(); j++) {
3699  SqlNode operand = projectList.get(j);
3700  selectList.add(bb.convertExpression(operand));
3701 
3702  // REVIEW angel 5-June-2005: Use deriveAliasFromOrdinal
3703  // instead of deriveAlias to match field names from
3704  // SqlRowOperator. Otherwise, get error Type
3705  // 'RecordType(INTEGER EMPNO)' has no field 'EXPR$0' when
3706  // doing select * from unnest( select multiset[empno]
3707  // from sales.emps);
3708 
3709  fieldNameList.add(SqlUtil.deriveAliasFromOrdinal(j));
3710  }
3711 
3712  relBuilder.push(LogicalValues.createOneRow(cluster))
3713  .projectNamed(selectList, fieldNameList, true);
3714 
3715  joinList.set(i, relBuilder.build());
3716  }
3717  }
3718 
3719  RelNode ret = (RelNode) joinList.get(0);
3720  for (int i = 1; i < joinList.size(); i++) {
3721  RelNode relNode = (RelNode) joinList.get(i);
3722  ret = RelFactories.DEFAULT_JOIN_FACTORY.createJoin(ret,
3723  relNode,
3724  rexBuilder.makeLiteral(true),
3725  ImmutableSet.of(),
3726  JoinRelType.INNER,
3727  false);
3728  }
3729  return ret;
3730  }
RelNode convertQueryOrInList(Blackboard bb, SqlNode seek, RelDataType targetRowType)
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 1303 of file SqlToRelConverter.java.

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

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

1304  {
1305  SqlCall call = (SqlBasicCall) subQuery.node;
1306  if (subQueryConverter.canConvertSubQuery()
1307  && isSubQueryNonCorrelated(converted, bb)) {
1308  // First check if the sub-query has already been converted
1309  // because it's a nested sub-query. If so, don't re-evaluate
1310  // it again.
1311  RexNode constExpr = mapConvertedNonCorrSubqs.get(call);
1312  if (constExpr == null) {
1313  constExpr = subQueryConverter.convertSubQuery(
1314  call, this, isExists, config.isExplain());
1315  }
1316  if (constExpr != null) {
1317  subQuery.expr = constExpr;
1318  mapConvertedNonCorrSubqs.put(call, constExpr);
1319  return true;
1320  }
1321  }
1322  return false;
1323  }
boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb)

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

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

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

760  {
761  if (!bb.top || select.getOrderList() == null
762  || select.getOrderList().getList().isEmpty()) {
763  assert !bb.top || collation.getFieldCollations().isEmpty();
764  if ((offset == null
765  || (offset instanceof SqlLiteral
766  && ((SqlLiteral) offset)
767  .bigDecimalValue()
768  .equals(BigDecimal.ZERO)))
769  && fetch == null) {
770  return;
771  }
772  }
773 
774  // Create a sorter using the previously constructed collations.
775  bb.setRoot(LogicalSort.create(bb.root,
776  collation,
777  offset == null ? null : convertExpression(offset),
778  fetch == null ? null : convertExpression(fetch)),
779  false);
780 
781  // If extra expressions were added to the project list for sorting,
782  // add another project to remove them. But make the collation empty, because
783  // we can't represent the real collation.
784  //
785  // If it is the top node, use the real collation, but don't trim fields.
786  if (orderExprList.size() > 0 && !bb.top) {
787  final List<RexNode> exprs = new ArrayList<>();
788  final RelDataType rowType = bb.root.getRowType();
789  final int fieldCount = rowType.getFieldCount() - orderExprList.size();
790  for (int i = 0; i < fieldCount; i++) {
791  exprs.add(rexBuilder.makeInputRef(bb.root, i));
792  }
793  bb.setRoot(LogicalProject.create(
794  bb.root, exprs, rowType.getFieldNames().subList(0, fieldCount)),
795  false);
796  }
797  }
int64_t const int32_t sz assert(dest)

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

References assert(), and org.apache.calcite.sql2rel.SqlToRelConverter.desc().

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

2917  {
2918  assert select != null;
2919  // Handle DESC keyword, e.g. 'select a, b from t order by a desc'.
2920  switch (orderItem.getKind()) {
2921  case DESCENDING:
2922  return convertOrderItem(select,
2923  ((SqlCall) orderItem).operand(0),
2924  extraExprs,
2925  RelFieldCollation.Direction.DESCENDING,
2926  nullDirection);
2927  case NULLS_FIRST:
2928  return convertOrderItem(select,
2929  ((SqlCall) orderItem).operand(0),
2930  extraExprs,
2931  direction,
2932  RelFieldCollation.NullDirection.FIRST);
2933  case NULLS_LAST:
2934  return convertOrderItem(select,
2935  ((SqlCall) orderItem).operand(0),
2936  extraExprs,
2937  direction,
2938  RelFieldCollation.NullDirection.LAST);
2939  }
2940 
2941  SqlNode converted = validator.expandOrderExpr(select, orderItem);
2942 
2943  switch (nullDirection) {
2944  case UNSPECIFIED:
2945  nullDirection = validator.getDefaultNullCollation().last(desc(direction))
2946  ? RelFieldCollation.NullDirection.LAST
2947  : RelFieldCollation.NullDirection.FIRST;
2948  }
2949 
2950  // Scan the select list and order exprs for an identical expression.
2951  final SelectScope selectScope = validator.getRawSelectScope(select);
2952  int ordinal = -1;
2953  for (SqlNode selectItem : selectScope.getExpandedSelectList()) {
2954  ++ordinal;
2955  if (converted.equalsDeep(stripAs(selectItem), Litmus.IGNORE)) {
2956  return new RelFieldCollation(ordinal, direction, nullDirection);
2957  }
2958  }
2959 
2960  for (SqlNode extraExpr : extraExprs) {
2961  ++ordinal;
2962  if (converted.equalsDeep(extraExpr, Litmus.IGNORE)) {
2963  return new RelFieldCollation(ordinal, direction, nullDirection);
2964  }
2965  }
2966 
2967  // TODO: handle collation sequence
2968  // TODO: flag expressions as non-standard
2969 
2970  extraExprs.add(converted);
2971  return new RelFieldCollation(ordinal + 1, direction, nullDirection);
2972  }
int64_t const int32_t sz assert(dest)
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 1788 of file SqlToRelConverter.java.

1788  {
1789  SqlCall call = (SqlCall) node;
1790  SqlCall aggCall = call.operand(0);
1791  boolean ignoreNulls = false;
1792  switch (aggCall.getKind()) {
1793  case IGNORE_NULLS:
1794  ignoreNulls = true;
1795  // fall through
1796  case RESPECT_NULLS:
1797  aggCall = aggCall.operand(0);
1798  }
1799 
1800  SqlNode windowOrRef = call.operand(1);
1801  final SqlWindow window = validator.resolveWindow(windowOrRef, bb.scope, true);
1802 
1803  // ROW_NUMBER() expects specific kind of framing.
1804  if (aggCall.getKind() == SqlKind.ROW_NUMBER) {
1805  window.setLowerBound(SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO));
1806  window.setUpperBound(SqlWindow.createCurrentRow(SqlParserPos.ZERO));
1807  window.setRows(SqlLiteral.createBoolean(true, SqlParserPos.ZERO));
1808  }
1809  final SqlNodeList partitionList = window.getPartitionList();
1810  final ImmutableList.Builder<RexNode> partitionKeys = ImmutableList.builder();
1811  for (SqlNode partition : partitionList) {
1812  partitionKeys.add(bb.convertExpression(partition));
1813  }
1814  RexNode lowerBound = bb.convertExpression(window.getLowerBound());
1815  RexNode upperBound = bb.convertExpression(window.getUpperBound());
1816  SqlNodeList orderList = window.getOrderList();
1817  if ((orderList.size() == 0) && !window.isRows()) {
1818  // A logical range requires an ORDER BY clause. Use the implicit
1819  // ordering of this relation. There must be one, otherwise it would
1820  // have failed validation.
1821  orderList = bb.scope.getOrderList();
1822  if (orderList == null) {
1823  throw new AssertionError("Relation should have sort key for implicit ORDER BY");
1824  }
1825  }
1826 
1827  final ImmutableList.Builder<RexFieldCollation> orderKeys = ImmutableList.builder();
1828  for (SqlNode order : orderList) {
1829  orderKeys.add(bb.convertSortExpression(order,
1830  RelFieldCollation.Direction.ASCENDING,
1831  RelFieldCollation.NullDirection.UNSPECIFIED));
1832  }
1833 
1834  try {
1835  Preconditions.checkArgument(bb.window == null, "already in window agg mode");
1836  bb.window = window;
1837  RexNode rexAgg = exprConverter.convertCall(bb, aggCall);
1838  rexAgg = rexBuilder.ensureType(validator.getValidatedNodeType(call), rexAgg, false);
1839 
1840  // Walk over the tree and apply 'over' to all agg functions. This is
1841  // necessary because the returned expression is not necessarily a call
1842  // to an agg function. For example, AVG(x) becomes SUM(x) / COUNT(x).
1843 
1844  final SqlLiteral q = aggCall.getFunctionQuantifier();
1845  final boolean isDistinct = q != null && q.getValue() == SqlSelectKeyword.DISTINCT;
1846 
1847  final RexShuttle visitor = new HistogramShuttle(partitionKeys.build(),
1848  orderKeys.build(),
1849  RexWindowBound.create(window.getLowerBound(), lowerBound),
1850  RexWindowBound.create(window.getUpperBound(), upperBound),
1851  window,
1852  isDistinct,
1853  ignoreNulls);
1854  RexNode overNode = rexAgg.accept(visitor);
1855 
1856  return overNode;
1857  } finally {
1858  bb.window = null;
1859  }
1860  }
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 550 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.isOrdered(), org.apache.calcite.sql2rel.SqlToRelConverter.isStream(), org.apache.calcite.sql2rel.SqlToRelConverter.requiredCollation(), run_benchmark_import.result, and org.apache.calcite.sql2rel.SqlToRelConverter.SQL2REL_LOGGER.

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

551  {
552  if (needsValidation) {
553  query = validator.validate(query);
554  }
555 
556  RelMetadataQuery.THREAD_PROVIDERS.set(
557  JaninoRelMetadataProvider.of(cluster.getMetadataProvider()));
558  RelNode result = convertQueryRecursive(query, top, null).rel;
559  if (top) {
560  if (isStream(query)) {
561  result = new LogicalDelta(cluster, result.getTraitSet(), result);
562  }
563  }
564  RelCollation collation = RelCollations.EMPTY;
565  if (!query.isA(SqlKind.DML)) {
566  if (isOrdered(query)) {
567  collation = requiredCollation(result);
568  }
569  }
570  checkConvertedType(query, result);
571 
572  if (SQL2REL_LOGGER.isDebugEnabled()) {
573  SQL2REL_LOGGER.debug(RelOptUtil.dumpPlan("Plan after converting SqlNode to RelNode",
574  result,
575  SqlExplainFormat.TEXT,
576  SqlExplainLevel.EXPPLAN_ATTRIBUTES));
577  }
578 
579  final RelDataType validatedRowType = validator.getValidatedNodeType(query);
580  return RelRoot.of(result, validatedRowType, query.getKind()).withCollation(collation);
581  }
void checkConvertedType(SqlNode query, RelNode result)
RelRoot convertQueryRecursive(SqlNode query, boolean top, RelDataType targetRowType)

+ 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 1489 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().

1490  {
1491  // NOTE: Once we start accepting single-row queries as row constructors,
1492  // there will be an ambiguity here for a case like X IN ((SELECT Y FROM
1493  // Z)). The SQL standard resolves the ambiguity by saying that a lone
1494  // select should be interpreted as a table expression, not a row
1495  // expression. The semantic difference is that a table expression can
1496  // return multiple rows.
1497  if (seek instanceof SqlNodeList) {
1498  return convertRowValues(
1499  bb, seek, ((SqlNodeList) seek).getList(), false, targetRowType);
1500  } else {
1501  return convertQueryRecursive(seek, false, null).project();
1502  }
1503  }
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 3014 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(), and org.apache.calcite.sql2rel.SqlToRelConverter.convertWith().

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().

3015  {
3016  final SqlKind kind = query.getKind();
3017  switch (kind) {
3018  case SELECT:
3019  return RelRoot.of(convertSelect((SqlSelect) query, top), kind);
3020  case INSERT:
3021  return RelRoot.of(convertInsert((SqlInsert) query), kind);
3022  case DELETE:
3023  return RelRoot.of(convertDelete((SqlDelete) query), kind);
3024  case UPDATE:
3025  return RelRoot.of(convertUpdate((SqlUpdate) query), kind);
3026  case MERGE:
3027  return RelRoot.of(convertMerge((SqlMerge) query), kind);
3028  case UNION:
3029  case INTERSECT:
3030  case EXCEPT:
3031  return RelRoot.of(convertSetOp((SqlCall) query), kind);
3032  case WITH:
3033  return convertWith((SqlWith) query, top);
3034  case VALUES:
3035  return RelRoot.of(convertValues((SqlCall) query, targetRowType), kind);
3036  default:
3037  throw new AssertionError("not a query: " + query);
3038  }
3039  }
RelRoot convertWith(SqlWith with, boolean top)
RelNode convertValues(SqlCall values, RelDataType targetRowType)
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 3614 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().

3614  {
3615  Preconditions.checkArgument(isRowConstructor(rowConstructor));
3616  final List<SqlNode> operands = rowConstructor.getOperandList();
3617  return convertMultisets(operands, bb);
3618  }
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 1505 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().

1509  {
1510  // NOTE jvs 30-Apr-2006: We combine all rows consisting entirely of
1511  // literals into a single LogicalValues; this gives the optimizer a smaller
1512  // input tree. For everything else (computed expressions, row
1513  // sub-queries), we union each row in as a projection on top of a
1514  // LogicalOneRow.
1515 
1516  final ImmutableList.Builder<ImmutableList<RexLiteral>> tupleList =
1517  ImmutableList.builder();
1518  final RelDataType rowType;
1519  if (targetRowType != null) {
1520  rowType = targetRowType;
1521  } else {
1522  rowType = SqlTypeUtil.promoteToRowType(
1523  typeFactory, validator.getValidatedNodeType(rowList), null);
1524  }
1525 
1526  final List<RelNode> unionInputs = new ArrayList<>();
1527  for (SqlNode node : rows) {
1528  SqlBasicCall call;
1529  if (isRowConstructor(node)) {
1530  call = (SqlBasicCall) node;
1531  ImmutableList.Builder<RexLiteral> tuple = ImmutableList.builder();
1532  for (Ord<SqlNode> operand : Ord.zip(call.operands)) {
1533  RexLiteral rexLiteral =
1534  convertLiteralInValuesList(operand.e, bb, rowType, operand.i);
1535  if ((rexLiteral == null) && allowLiteralsOnly) {
1536  return null;
1537  }
1538  if ((rexLiteral == null) || !config.isCreateValuesRel()) {
1539  // fallback to convertRowConstructor
1540  tuple = null;
1541  break;
1542  }
1543  tuple.add(rexLiteral);
1544  }
1545  if (tuple != null) {
1546  tupleList.add(tuple.build());
1547  continue;
1548  }
1549  } else {
1550  RexLiteral rexLiteral = convertLiteralInValuesList(node, bb, rowType, 0);
1551  if ((rexLiteral != null) && config.isCreateValuesRel()) {
1552  tupleList.add(ImmutableList.of(rexLiteral));
1553  continue;
1554  } else {
1555  if ((rexLiteral == null) && allowLiteralsOnly) {
1556  return null;
1557  }
1558  }
1559 
1560  // convert "1" to "row(1)"
1561  call = (SqlBasicCall) SqlStdOperatorTable.ROW.createCall(SqlParserPos.ZERO, node);
1562  }
1563  unionInputs.add(convertRowConstructor(bb, call));
1564  }
1565  LogicalValues values = LogicalValues.create(cluster, rowType, tupleList.build());
1566  RelNode resultRel;
1567  if (unionInputs.isEmpty()) {
1568  resultRel = values;
1569  } else {
1570  if (!values.getTuples().isEmpty()) {
1571  unionInputs.add(values);
1572  }
1573  resultRel = LogicalUnion.create(unionInputs, true);
1574  }
1575  leaves.add(resultRel);
1576  return resultRel;
1577  }
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 618 of file SqlToRelConverter.java.

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

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().

618  {
619  final SqlValidatorScope selectScope = validator.getWhereScope(select);
620  final Blackboard bb = createBlackboard(selectScope, null, top);
621  convertSelectImpl(bb, select);
622  return bb.root;
623  }
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 637 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(), and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

637  {
638  convertFrom(bb, select.getFrom());
639  convertWhere(bb, select.getWhere());
640 
641  final List<SqlNode> orderExprList = new ArrayList<>();
642  final List<RelFieldCollation> collationList = new ArrayList<>();
643  gatherOrderExprs(bb, select, select.getOrderList(), orderExprList, collationList);
644  final RelCollation collation =
645  cluster.traitSet().canonize(RelCollations.of(collationList));
646 
647  if (validator.isAggregate(select)) {
648  convertAgg(bb, select, orderExprList);
649  } else {
650  convertSelectList(bb, select, orderExprList);
651  }
652 
653  if (select.isDistinct()) {
654  distinctify(bb, true);
655  }
656  convertOrder(
657  select, bb, collation, orderExprList, select.getOffset(), select.getFetch());
658  bb.setRoot(bb.root, true);
659  }
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 3732 of file SqlToRelConverter.java.

References assert().

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

3733  {
3734  SqlNodeList selectList = select.getSelectList();
3735  selectList = validator.expandStar(selectList, select, false);
3736 
3737  replaceSubQueries(bb, selectList, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
3738 
3739  List<String> fieldNames = new ArrayList<>();
3740  final List<RexNode> exprs = new ArrayList<>();
3741  final Collection<String> aliases = new TreeSet<>();
3742 
3743  // Project any system fields. (Must be done before regular select items,
3744  // because offsets may be affected.)
3745  final List<SqlMonotonicity> columnMonotonicityList = new ArrayList<>();
3746  extraSelectItems(bb, select, exprs, fieldNames, aliases, columnMonotonicityList);
3747 
3748  // Project select clause.
3749  int i = -1;
3750  for (SqlNode expr : selectList) {
3751  ++i;
3752  exprs.add(bb.convertExpression(expr));
3753  fieldNames.add(deriveAlias(expr, aliases, i));
3754  }
3755 
3756  // Project extra fields for sorting.
3757  for (SqlNode expr : orderList) {
3758  ++i;
3759  SqlNode expr2 = validator.expandOrderExpr(select, expr);
3760  exprs.add(bb.convertExpression(expr2));
3761  fieldNames.add(deriveAlias(expr, aliases, i));
3762  }
3763 
3764  fieldNames = SqlValidatorUtil.uniquify(
3765  fieldNames, catalogReader.nameMatcher().isCaseSensitive());
3766 
3767  relBuilder.push(bb.root).projectNamed(exprs, fieldNames, true);
3768  bb.setRoot(relBuilder.build(), false);
3769 
3770  assert bb.columnMonotonicities.isEmpty();
3771  bb.columnMonotonicities.addAll(columnMonotonicityList);
3772  for (SqlNode selectItem : selectList) {
3773  bb.columnMonotonicities.add(selectItem.getMonotonicity(bb.scope));
3774  }
3775  }
void extraSelectItems(Blackboard bb, SqlSelect select, List< RexNode > exprList, List< String > nameList, Collection< String > aliasList, List< SqlMonotonicity > columnMonotonicityList)
String deriveAlias(final SqlNode node, Collection< String > aliases, final int ordinal)
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
int64_t const int32_t sz assert(dest)

+ Here is the call graph for this function:

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

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

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

3048  {
3049  final RelNode left = convertQueryRecursive(call.operand(0), false, null).project();
3050  final RelNode right = convertQueryRecursive(call.operand(1), false, null).project();
3051  switch (call.getKind()) {
3052  case UNION:
3053  return LogicalUnion.create(ImmutableList.of(left, right), all(call));
3054 
3055  case INTERSECT:
3056  return LogicalIntersect.create(ImmutableList.of(left, right), all(call));
3057 
3058  case EXCEPT:
3059  return LogicalMinus.create(ImmutableList.of(left, right), all(call));
3060 
3061  default:
3062  throw Util.unexpected(call.getKind());
3063  }
3064  }
RelRoot convertQueryRecursive(SqlNode query, boolean top, RelDataType targetRowType)

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

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

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

1333  {
1334  // Check whether query is guaranteed to produce a single value.
1335  if (query instanceof SqlSelect) {
1336  SqlSelect select = (SqlSelect) query;
1337  SqlNodeList selectList = select.getSelectList();
1338  SqlNodeList groupList = select.getGroup();
1339 
1340  if ((selectList.size() == 1) && ((groupList == null) || (groupList.size() == 0))) {
1341  SqlNode selectExpr = selectList.get(0);
1342  if (selectExpr instanceof SqlCall) {
1343  SqlCall selectExprCall = (SqlCall) selectExpr;
1344  if (Util.isSingleValue(selectExprCall)) {
1345  return plan;
1346  }
1347  }
1348 
1349  // If there is a limit with 0 or 1,
1350  // it is ensured to produce a single value
1351  if (select.getFetch() != null && select.getFetch() instanceof SqlNumericLiteral) {
1352  SqlNumericLiteral limitNum = (SqlNumericLiteral) select.getFetch();
1353  if (((BigDecimal) limitNum.getValue()).intValue() < 2) {
1354  return plan;
1355  }
1356  }
1357  }
1358  } else if (query instanceof SqlCall) {
1359  // If the query is (values ...),
1360  // it is necessary to look into the operands to determine
1361  // whether SingleValueAgg is necessary
1362  SqlCall exprCall = (SqlCall) query;
1363  if (exprCall.getOperator() instanceof SqlValuesOperator
1364  && Util.isSingleValue(exprCall)) {
1365  return plan;
1366  }
1367  }
1368 
1369  // If not, project SingleValueAgg
1370  return RelOptUtil.createSingleValueAggRel(cluster, plan);
1371  }

+ Here is the caller graph for this function:

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

Definition at line 3420 of file SqlToRelConverter.java.

References assert(), 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(), and org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory.

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

3420  {
3421  final SqlValidatorScope scope = validator.getWhereScope(call.getSourceSelect());
3422  Blackboard bb = createBlackboard(scope, null, false);
3423 
3424  replaceSubQueries(bb, call, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
3425 
3426  Builder<RexNode> rexNodeSourceExpressionListBuilder = ImmutableList.builder();
3427  for (SqlNode n : call.getSourceExpressionList()) {
3428  RexNode rn = bb.convertExpression(n);
3429  rexNodeSourceExpressionListBuilder.add(rn);
3430  }
3431 
3432  RelOptTable targetTable = getTargetTable(call);
3433 
3434  // convert update column list from SqlIdentifier to String
3435  final List<String> targetColumnNameList = new ArrayList<>();
3436  final RelDataType targetRowType = targetTable.getRowType();
3437  for (SqlNode node : call.getTargetColumnList()) {
3438  SqlIdentifier id = (SqlIdentifier) node;
3439  RelDataTypeField field = SqlValidatorUtil.getTargetField(
3440  targetRowType, typeFactory, id, catalogReader, targetTable);
3441  assert field != null : "column " + id.toString() + " not found";
3442  targetColumnNameList.add(field.getName());
3443  }
3444 
3445  RelNode sourceRel = convertSelect(call.getSourceSelect(), false);
3446 
3447  return LogicalTableModify.create(targetTable,
3448  catalogReader,
3449  sourceRel,
3450  LogicalTableModify.Operation.UPDATE,
3451  targetColumnNameList,
3452  rexNodeSourceExpressionListBuilder.build(),
3453  false);
3454  }
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)
int64_t const int32_t sz assert(dest)
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 2598 of file SqlToRelConverter.java.

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

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

2600  {
2601  final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
2602  final List<RexNode> list = new ArrayList<>();
2603  for (String name : nameList) {
2604  List<RexNode> operands = new ArrayList<>();
2605  int offset = 0;
2606  for (SqlValidatorNamespace n : ImmutableList.of(leftNamespace, rightNamespace)) {
2607  final RelDataType rowType = n.getRowType();
2608  final RelDataTypeField field = nameMatcher.field(rowType, name);
2609  operands.add(rexBuilder.makeInputRef(field.getType(), offset + field.getIndex()));
2610  offset += rowType.getFieldList().size();
2611  }
2612  list.add(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, operands));
2613  }
2614  return RexUtil.composeConjunction(rexBuilder, list);
2615  }
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 3823 of file SqlToRelConverter.java.

References assert().

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

3823  {
3824  final SqlValidatorScope scope = validator.getOverScope(values);
3825  assert scope != null;
3826  final Blackboard bb = createBlackboard(scope, null, false);
3827  convertValuesImpl(bb, values, targetRowType);
3828  return bb.root;
3829  }
void convertValuesImpl(Blackboard bb, SqlCall values, RelDataType targetRowType)
int64_t const int32_t sz assert(dest)
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.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 3839 of file SqlToRelConverter.java.

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

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

3840  {
3841  // Attempt direct conversion to LogicalValues; if that fails, deal with
3842  // fancy stuff like sub-queries below.
3843  RelNode valuesRel =
3844  convertRowValues(bb, values, values.getOperandList(), true, targetRowType);
3845  if (valuesRel != null) {
3846  bb.setRoot(valuesRel, true);
3847  return;
3848  }
3849 
3850  final List<RelNode> unionRels = new ArrayList<>();
3851  for (SqlNode rowConstructor1 : values.getOperandList()) {
3852  SqlCall rowConstructor = (SqlCall) rowConstructor1;
3853  Blackboard tmpBb = createBlackboard(bb.scope, null, false);
3854  replaceSubQueries(tmpBb, rowConstructor, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
3855  final List<Pair<RexNode, String>> exps = new ArrayList<>();
3856  for (Ord<SqlNode> operand : Ord.zip(rowConstructor.getOperandList())) {
3857  exps.add(Pair.of(tmpBb.convertExpression(operand.e),
3858  validator.deriveAlias(operand.e, operand.i)));
3859  }
3860  RelNode in =
3861  (null == tmpBb.root) ? LogicalValues.createOneRow(cluster) : tmpBb.root;
3862  unionRels.add(
3863  relBuilder.push(in).project(Pair.left(exps), Pair.right(exps)).build());
3864  }
3865 
3866  if (unionRels.size() == 0) {
3867  throw new AssertionError("empty values clause");
3868  } else if (unionRels.size() == 1) {
3869  bb.setRoot(unionRels.get(0), true);
3870  } else {
3871  bb.setRoot(LogicalUnion.create(unionRels, true), true);
3872  }
3873  }
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 928 of file SqlToRelConverter.java.

References assert(), org.apache.calcite.sql2rel.SqlToRelConverter.getCorrelationUse(), org.apache.calcite.sql2rel.SqlToRelConverter.CorrelationUse.id, org.apache.calcite.sql2rel.SqlToRelConverter.pushDownNotForIn(), org.apache.calcite.sql2rel.SqlToRelConverter.CorrelationUse.r, org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries(), org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.scope, and org.apache.calcite.sql2rel.SqlToRelConverter.typeFactory.

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

928  {
929  if (where == null) {
930  return;
931  }
932  SqlNode newWhere = pushDownNotForIn(bb.scope, where);
933  replaceSubQueries(bb, newWhere, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
934  final RexNode convertedWhere = bb.convertExpression(newWhere);
935  final RexNode convertedWhere2 =
936  RexUtil.removeNullabilityCast(typeFactory, convertedWhere);
937 
938  // only allocate filter if the condition is not TRUE
939  if (convertedWhere2.isAlwaysTrue()) {
940  return;
941  }
942 
943  final RelFactories.FilterFactory filterFactory = RelFactories.DEFAULT_FILTER_FACTORY;
944  final RelNode filter =
945  filterFactory.createFilter(bb.root, convertedWhere2, ImmutableSet.of());
946  final RelNode r;
947  final CorrelationUse p = getCorrelationUse(bb, filter);
948  if (p != null) {
949  assert p.r instanceof Filter;
950  Filter f = (Filter) p.r;
951  r = LogicalFilter.create(f.getInput(), f.getCondition(), ImmutableSet.of(p.id));
952  } else {
953  r = filter;
954  }
955 
956  bb.setRoot(r, false);
957  }
static SqlNode pushDownNotForIn(SqlValidatorScope scope, SqlNode sqlNode)
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
CorrelationUse getCorrelationUse(Blackboard bb, final RelNode r0)
int64_t const int32_t sz assert(dest)

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

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

3816  {
3817  return convertQuery(with.body, false, top);
3818  }
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 2658 of file SqlToRelConverter.java.

References assert(), org.apache.calcite.sql2rel.SqlToRelConverter.createAggregate(), org.apache.calcite.sql2rel.SqlToRelConverter.AggregateFinder.filterList, org.apache.calcite.sql2rel.SqlToRelConverter.AggConverter.getAggCalls(), org.apache.calcite.sql2rel.SqlToRelConverter.AggregateFinder.list, org.apache.calcite.sql2rel.SqlToRelConverter.AggregateFinder.orderList, org.apache.calcite.sql2rel.SqlToRelConverter.pushDownNotForIn(), org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries(), org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.scope, Experimental.String, and org.apache.calcite.sql2rel.SqlToRelConverter.validator.

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

2663  {
2664  // Find aggregate functions in SELECT and HAVING clause
2665  final AggregateFinder aggregateFinder = new AggregateFinder();
2666  selectList.accept(aggregateFinder);
2667  if (having != null) {
2668  having.accept(aggregateFinder);
2669  }
2670 
2671  // first replace the sub-queries inside the aggregates
2672  // because they will provide input rows to the aggregates.
2673  replaceSubQueries(bb, aggregateFinder.list, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2674 
2675  // also replace sub-queries inside filters in the aggregates
2677  bb, aggregateFinder.filterList, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2678 
2679  // also replace sub-queries inside ordering spec in the aggregates
2680  replaceSubQueries(bb, aggregateFinder.orderList, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2681 
2682  // If group-by clause is missing, pretend that it has zero elements.
2683  if (groupList == null) {
2684  groupList = SqlNodeList.EMPTY;
2685  }
2686 
2687  replaceSubQueries(bb, groupList, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2688 
2689  // register the group exprs
2690 
2691  // build a map to remember the projections from the top scope to the
2692  // output of the current root.
2693  //
2694  // Calcite allows expressions, not just column references in
2695  // group by list. This is not SQL 2003 compliant, but hey.
2696 
2697  final AggregatingSelectScope scope = aggConverter.aggregatingSelectScope;
2698  final AggregatingSelectScope.Resolved r = scope.resolved.get();
2699  for (SqlNode groupExpr : r.groupExprList) {
2700  aggConverter.addGroupExpr(groupExpr);
2701  }
2702 
2703  final RexNode havingExpr;
2704  final List<Pair<RexNode, String>> projects = new ArrayList<>();
2705 
2706  try {
2707  Preconditions.checkArgument(bb.agg == null, "already in agg mode");
2708  bb.agg = aggConverter;
2709 
2710  // convert the select and having expressions, so that the
2711  // agg converter knows which aggregations are required
2712 
2713  selectList.accept(aggConverter);
2714  // Assert we don't have dangling items left in the stack
2715  assert !aggConverter.inOver;
2716  for (SqlNode expr : orderExprList) {
2717  expr.accept(aggConverter);
2718  assert !aggConverter.inOver;
2719  }
2720  if (having != null) {
2721  having.accept(aggConverter);
2722  assert !aggConverter.inOver;
2723  }
2724 
2725  // compute inputs to the aggregator
2726  List<Pair<RexNode, String>> preExprs = aggConverter.getPreExprs();
2727 
2728  if (preExprs.size() == 0) {
2729  // Special case for COUNT(*), where we can end up with no inputs
2730  // at all. The rest of the system doesn't like 0-tuples, so we
2731  // select a dummy constant here.
2732  final RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.ZERO);
2733  preExprs = ImmutableList.of(Pair.of(zero, (String) null));
2734  }
2735 
2736  final RelNode inputRel = bb.root;
2737 
2738  // Project the expressions required by agg and having.
2739  bb.setRoot(relBuilder.push(inputRel)
2740  .projectNamed(Pair.left(preExprs), Pair.right(preExprs), false)
2741  .build(),
2742  false);
2743  bb.mapRootRelToFieldProjection.put(bb.root, r.groupExprProjection);
2744 
2745  // REVIEW jvs 31-Oct-2007: doesn't the declaration of
2746  // monotonicity here assume sort-based aggregation at
2747  // the physical level?
2748 
2749  // Tell bb which of group columns are sorted.
2750  bb.columnMonotonicities.clear();
2751  for (SqlNode groupItem : groupList) {
2752  bb.columnMonotonicities.add(bb.scope.getMonotonicity(groupItem));
2753  }
2754 
2755  // Add the aggregator
2756  bb.setRoot(createAggregate(bb, r.groupSet, r.groupSets, aggConverter.getAggCalls()),
2757  false);
2758 
2759  bb.mapRootRelToFieldProjection.put(bb.root, r.groupExprProjection);
2760 
2761  // Replace sub-queries in having here and modify having to use
2762  // the replaced expressions
2763  if (having != null) {
2764  SqlNode newHaving = pushDownNotForIn(bb.scope, having);
2765  replaceSubQueries(bb, newHaving, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
2766  havingExpr = bb.convertExpression(newHaving);
2767  } else {
2768  havingExpr = relBuilder.literal(true);
2769  }
2770 
2771  // Now convert the other sub-queries in the select list.
2772  // This needs to be done separately from the sub-query inside
2773  // any aggregate in the select list, and after the aggregate rel
2774  // is allocated.
2775  replaceSubQueries(bb, selectList, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
2776 
2777  // Now sub-queries in the entire select list have been converted.
2778  // Convert the select expressions to get the final list to be
2779  // projected.
2780  int k = 0;
2781 
2782  // For select expressions, use the field names previously assigned
2783  // by the validator. If we derive afresh, we might generate names
2784  // like "EXPR$2" that don't match the names generated by the
2785  // validator. This is especially the case when there are system
2786  // fields; system fields appear in the relnode's rowtype but do not
2787  // (yet) appear in the validator type.
2788  final SelectScope selectScope = SqlValidatorUtil.getEnclosingSelectScope(bb.scope);
2789  assert selectScope != null;
2790  final SqlValidatorNamespace selectNamespace =
2791  validator.getNamespace(selectScope.getNode());
2792  final List<String> names = selectNamespace.getRowType().getFieldNames();
2793  int sysFieldCount = selectList.size() - names.size();
2794  for (SqlNode expr : selectList) {
2795  projects.add(Pair.of(bb.convertExpression(expr),
2796  k < sysFieldCount ? validator.deriveAlias(expr, k++)
2797  : names.get(k++ - sysFieldCount)));
2798  }
2799 
2800  for (SqlNode expr : orderExprList) {
2801  projects.add(
2802  Pair.of(bb.convertExpression(expr), validator.deriveAlias(expr, k++)));
2803  }
2804  } finally {
2805  bb.agg = null;
2806  }
2807 
2808  // implement HAVING (we have already checked that it is non-trivial)
2809  relBuilder.push(bb.root);
2810  if (havingExpr != null) {
2811  relBuilder.filter(havingExpr);
2812  }
2813 
2814  // implement the SELECT list
2815  relBuilder.project(Pair.left(projects), Pair.right(projects))
2816  .rename(Pair.right(projects));
2817  bb.setRoot(relBuilder.build(), false);
2818 
2819  // Tell bb which of group columns are sorted.
2820  bb.columnMonotonicities.clear();
2821  for (SqlNode selectItem : selectList) {
2822  bb.columnMonotonicities.add(bb.scope.getMonotonicity(selectItem));
2823  }
2824  }
static SqlNode pushDownNotForIn(SqlValidatorScope scope, SqlNode sqlNode)
void replaceSubQueries(final Blackboard bb, final SqlNode expr, RelOptUtil.Logic logic)
RelNode createAggregate(Blackboard bb, ImmutableBitSet groupSet, ImmutableList< ImmutableBitSet > groupSets, List< AggregateCall > aggCalls)
int64_t const int32_t sz assert(dest)

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

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

2848  {
2849  return LogicalAggregate.create(bb.root, groupSet, groupSets, aggCalls);
2850  }

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

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

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

3291  {
3292  final Map<String, RexNode> nameToNodeMap = new HashMap<>();
3293  int j = 0;
3294 
3295  // Assign expressions for non-generated columns.
3296  final List<ColumnStrategy> strategies = targetTable.getColumnStrategies();
3297  final List<String> targetFields = targetTable.getRowType().getFieldNames();
3298  for (String targetColumnName : targetColumnNames) {
3299  final int i = targetFields.indexOf(targetColumnName);
3300  switch (strategies.get(i)) {
3301  case STORED:
3302  case VIRTUAL:
3303  break;
3304  default:
3305  nameToNodeMap.put(targetColumnName, rexBuilder.makeFieldAccess(sourceRef, j++));
3306  }
3307  }
3308  return createBlackboard(null, nameToNodeMap, false);
3309  }
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 2350 of file SqlToRelConverter.java.

References assert(), org.apache.calcite.sql2rel.SqlToRelConverter.getCorrelationUse(), org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.getTopNode(), org.apache.calcite.sql2rel.SqlToRelConverter.relBuilder, org.apache.calcite.sql2rel.SqlToRelConverter.CorrelationUse.requiredColumns, and org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder.

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

2354  {
2355  assert joinCond != null;
2356 
2357  final CorrelationUse p = getCorrelationUse(bb, rightRel);
2358  if (p != null) {
2359  RelNode innerRel = p.r;
2360  ImmutableBitSet requiredCols = p.requiredColumns;
2361 
2362  if (!joinCond.isAlwaysTrue()) {
2363  final RelFactories.FilterFactory factory = RelFactories.DEFAULT_FILTER_FACTORY;
2364  final RexCorrelVariable rexCorrel =
2365  (RexCorrelVariable) rexBuilder.makeCorrel(leftRel.getRowType(), p.id);
2366  final RexAccessShuttle shuttle = new RexAccessShuttle(rexBuilder, rexCorrel);
2367 
2368  // Replace outer RexInputRef with RexFieldAccess,
2369  // and push lateral join predicate into inner child
2370  final RexNode newCond = joinCond.accept(shuttle);
2371  innerRel = factory.createFilter(p.r, newCond, ImmutableSet.of());
2372  requiredCols =
2373  ImmutableBitSet.fromBitSet(shuttle.varCols).union(p.requiredColumns);
2374  }
2375 
2376  LogicalCorrelate corr =
2377  LogicalCorrelate.create(leftRel, innerRel, p.id, requiredCols, joinType);
2378  return corr;
2379  }
2380 
2381  final Join originalJoin = (Join) RelFactories.DEFAULT_JOIN_FACTORY.createJoin(
2382  leftRel, rightRel, joinCond, ImmutableSet.of(), joinType, false);
2383 
2384  boolean applyPushdown =
2385  config.getPushdownJoinCondition().test(bb.getTopNode(), originalJoin);
2386  if (applyPushdown) {
2387  return RelOptUtil.pushDownJoinConditions(originalJoin, relBuilder);
2388  } else {
2389  return originalJoin;
2390  }
2391  }
CorrelationUse getCorrelationUse(Blackboard bb, final RelNode r0)
int64_t const int32_t sz assert(dest)

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

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

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

3083  {
3084  final ModifiableTable modifiableTable = targetTable.unwrap(ModifiableTable.class);
3085  if (modifiableTable != null && modifiableTable == targetTable.unwrap(Table.class)) {
3086  return modifiableTable.toModificationRel(cluster,
3087  targetTable,
3088  catalogReader,
3089  source,
3090  LogicalTableModify.Operation.INSERT,
3091  null,
3092  null,
3093  false);
3094  }
3095  final ModifiableView modifiableView = targetTable.unwrap(ModifiableView.class);
3096  if (modifiableView != null) {
3097  final Table delegateTable = modifiableView.getTable();
3098  final RelDataType delegateRowType = delegateTable.getRowType(typeFactory);
3099  final RelOptTable delegateRelOptTable = RelOptTableImpl.create(
3100  null, delegateRowType, delegateTable, modifiableView.getTablePath());
3101  final RelNode newSource =
3102  createSource(targetTable, source, modifiableView, delegateRowType);
3103  return createModify(delegateRelOptTable, newSource);
3104  }
3105  return LogicalTableModify.create(targetTable,
3106  catalogReader,
3107  source,
3108  LogicalTableModify.Operation.INSERT,
3109  null,
3110  null,
3111  false);
3112  }
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 3127 of file SqlToRelConverter.java.

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

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

3130  {
3131  final ImmutableIntList mapping = modifiableView.getColumnMapping();
3132  assert mapping.size() == targetTable.getRowType().getFieldCount();
3133 
3134  // For columns represented in the mapping, the expression is just a field
3135  // reference.
3136  final Map<Integer, RexNode> projectMap = new HashMap<>();
3137  final List<RexNode> filters = new ArrayList<>();
3138  for (int i = 0; i < mapping.size(); i++) {
3139  int target = mapping.get(i);
3140  if (target >= 0) {
3141  projectMap.put(target, RexInputRef.of(i, source.getRowType()));
3142  }
3143  }
3144 
3145  // For columns that are not in the mapping, and have a constraint of the
3146  // form "column = value", the expression is the literal "value".
3147  //
3148  // If a column has multiple constraints, the extra ones will become a
3149  // filter.
3150  final RexNode constraint = modifiableView.getConstraint(rexBuilder, delegateRowType);
3151  RelOptUtil.inferViewPredicates(projectMap, filters, constraint);
3152  final List<Pair<RexNode, String>> projects = new ArrayList<>();
3153  for (RelDataTypeField field : delegateRowType.getFieldList()) {
3154  RexNode node = projectMap.get(field.getIndex());
3155  if (node == null) {
3156  node = rexBuilder.makeNullLiteral(field.getType());
3157  }
3158  projects.add(Pair.of(
3159  rexBuilder.ensureType(field.getType(), node, false), field.getName()));
3160  }
3161 
3162  return relBuilder.push(source)
3163  .projectNamed(Pair.left(projects), Pair.right(projects), false)
3164  .filter(filters)
3165  .build();
3166  }
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
int64_t const int32_t sz assert(dest)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelOptTable.ToRelContext org.apache.calcite.sql2rel.SqlToRelConverter.createToRelContext ( )
inlineprivate

Definition at line 3168 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().

3168  {
3169  return ViewExpanders.toRelContext(viewExpander, cluster);
3170  }

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

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

474  {
475  if (!enableDecorrelation()) {
476  return rootRel;
477  }
478  final RelNode result = decorrelateQuery(rootRel);
479  if (result != rootRel) {
480  checkConvertedType(query, result);
481  }
482  return result;
483  }
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 2991 of file SqlToRelConverter.java.

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

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

2991  {
2992  return RelDecorrelator.decorrelateQuery(rootRel, relBuilder);
2993  }

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

References Experimental.String.

3798  {
3799  String alias = validator.deriveAlias(node, ordinal);
3800  if ((alias == null) || aliases.contains(alias)) {
3801  String aliasBase = (alias == null) ? "EXPR$" : alias;
3802  for (int j = 0;; j++) {
3803  alias = aliasBase + j;
3804  if (!aliases.contains(alias)) {
3805  break;
3806  }
3807  }
3808  }
3809  aliases.add(alias);
3810  return alias;
3811  }
static boolean org.apache.calcite.sql2rel.SqlToRelConverter.desc ( RelFieldCollation.Direction  direction)
inlinestaticprivate

Definition at line 2974 of file SqlToRelConverter.java.

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

2974  {
2975  switch (direction) {
2976  case DESCENDING:
2977  case STRICTLY_DESCENDING:
2978  return true;
2979  default:
2980  return false;
2981  }
2982  }

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

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

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

674  {
675  // Look for duplicate expressions in the project.
676  // Say we have 'select x, y, x, z'.
677  // Then dups will be {[2, 0]}
678  // and oldToNew will be {[0, 0], [1, 1], [2, 0], [3, 2]}
679  RelNode rel = bb.root;
680  if (checkForDupExprs && (rel instanceof LogicalProject)) {
681  LogicalProject project = (LogicalProject) rel;
682  final List<RexNode> projectExprs = project.getProjects();
683  final List<Integer> origins = new ArrayList<>();
684  int dupCount = 0;
685  for (int i = 0; i < projectExprs.size(); i++) {
686  int x = projectExprs.indexOf(projectExprs.get(i));
687  if (x >= 0 && x < i) {
688  origins.add(x);
689  ++dupCount;
690  } else {
691  origins.add(i);
692  }
693  }
694  if (dupCount == 0) {
695  distinctify(bb, false);
696  return;
697  }
698 
699  final Map<Integer, Integer> squished = new HashMap<>();
700  final List<RelDataTypeField> fields = rel.getRowType().getFieldList();
701  final List<Pair<RexNode, String>> newProjects = new ArrayList<>();
702  for (int i = 0; i < fields.size(); i++) {
703  if (origins.get(i) == i) {
704  squished.put(i, newProjects.size());
705  newProjects.add(RexInputRef.of2(i, fields));
706  }
707  }
708  rel = LogicalProject.create(rel, Pair.left(newProjects), Pair.right(newProjects));
709  bb.root = rel;
710  distinctify(bb, false);
711  rel = bb.root;
712 
713  // Create the expressions to reverse the mapping.
714  // Project($0, $1, $0, $2).
715  final List<Pair<RexNode, String>> undoProjects = new ArrayList<>();
716  for (int i = 0; i < fields.size(); i++) {
717  final int origin = origins.get(i);
718  RelDataTypeField field = fields.get(i);
719  undoProjects.add(
720  Pair.of((RexNode) new RexInputRef(squished.get(origin), field.getType()),
721  field.getName()));
722  }
723 
724  rel = LogicalProject.create(rel, Pair.left(undoProjects), Pair.right(undoProjects));
725  bb.setRoot(rel, false);
726 
727  return;
728  }
729 
730  // Usual case: all of the expressions in the SELECT clause are
731  // different.
732  final ImmutableBitSet groupSet =
733  ImmutableBitSet.range(rel.getRowType().getFieldCount());
734  rel = createAggregate(bb, groupSet, ImmutableList.of(groupSet), ImmutableList.of());
735 
736  bb.setRoot(rel, false);
737  }
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 2985 of file SqlToRelConverter.java.

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

2985  {
2986  // disable sub-query decorrelation when needed.
2987  // e.g. if outer joins are not supported.
2988  return config.isDecorrelationEnabled();
2989  }

+ Here is the caller graph for this function:

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

References run_benchmark_import.type.

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

1435  {
1436  if (type.getSqlTypeName() == node.getType().getSqlTypeName()
1437  || (type.getSqlTypeName() == SqlTypeName.VARCHAR
1438  && node.getType().getSqlTypeName() == SqlTypeName.CHAR)) {
1439  return node;
1440  }
1441  return rexBuilder.ensureType(type, node, true);
1442  }

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

3795  {}
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 1650 of file SqlToRelConverter.java.

References run_benchmark_import.type.

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

1653  {
1654  final SqlKind kind = node.getKind();
1655  switch (kind) {
1656  case EXISTS:
1657  case SELECT:
1658  case MULTISET_QUERY_CONSTRUCTOR:
1659  case MULTISET_VALUE_CONSTRUCTOR:
1660  case ARRAY_QUERY_CONSTRUCTOR:
1661  case CURSOR:
1662  case SCALAR_QUERY:
1663  if (!registerOnlyScalarSubQueries || (kind == SqlKind.SCALAR_QUERY)) {
1664  bb.registerSubQuery(node, RelOptUtil.Logic.TRUE_FALSE);
1665  }
1666  return;
1667  case IN:
1668  break;
1669  case NOT_IN:
1670  case NOT:
1671  logic = logic.negate();
1672  break;
1673  }
1674  if (node instanceof SqlCall) {
1675  switch (kind) {
1676  // Do no change logic for AND, IN and NOT IN expressions;
1677  // but do change logic for OR, NOT and others;
1678  // EXISTS was handled already.
1679  case AND:
1680  case IN:
1681  case NOT_IN:
1682  break;
1683  default:
1684  logic = RelOptUtil.Logic.TRUE_FALSE_UNKNOWN;
1685  break;
1686  }
1687  for (SqlNode operand : ((SqlCall) node).getOperandList()) {
1688  if (operand != null) {
1689  // In the case of an IN expression, locate scalar
1690  // sub-queries so we can convert them to constants
1691  findSubQueries(bb,
1692  operand,
1693  logic,
1694  kind == SqlKind.IN || kind == SqlKind.NOT_IN || kind == SqlKind.SOME
1695  || kind == SqlKind.ALL || registerOnlyScalarSubQueries);
1696  }
1697  }
1698  } else if (node instanceof SqlNodeList) {
1699  for (SqlNode child : (SqlNodeList) node) {
1700  findSubQueries(bb,
1701  child,
1702  logic,
1703  kind == SqlKind.IN || kind == SqlKind.NOT_IN || kind == SqlKind.SOME
1704  || kind == SqlKind.ALL || registerOnlyScalarSubQueries);
1705  }
1706  }
1707 
1708  // Now that we've located any scalar sub-queries inside the IN
1709  // expression, register the IN expression itself. We need to
1710  // register the scalar sub-queries first so they can be converted
1711  // before the IN expression is converted.
1712  switch (kind) {
1713  case IN:
1714  case NOT_IN:
1715  case SOME:
1716  case ALL:
1717  switch (logic) {
1718  case TRUE_FALSE_UNKNOWN:
1719  RelDataType type = validator.getValidatedNodeTypeIfKnown(node);
1720  if (type == null) {
1721  // The node might not be validated if we still don't know type of the node.
1722  // Therefore return directly.
1723  return;
1724  } else {
1725  break;
1726  }
1727  // fall through
1728  case UNKNOWN_AS_FALSE:
1729  logic = RelOptUtil.Logic.TRUE;
1730  }
1731  bb.registerSubQuery(node, logic);
1732  break;
1733  }
1734  }
void findSubQueries(Blackboard bb, SqlNode node, RelOptUtil.Logic logic, boolean registerOnlyScalarSubQueries)

+ Here is the caller graph for this function:

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

Definition at line 460 of file SqlToRelConverter.java.

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

460  {
461  RelStructuredTypeFlattener typeFlattener = new RelStructuredTypeFlattener(
462  relBuilder, rexBuilder, createToRelContext(), restructure);
463  return typeFlattener.rewrite(rootRel);
464  }

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

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

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

2884  {
2885  // TODO: add validation rules to SqlValidator also
2886  assert bb.root != null : "precondition: child != null";
2887  assert select != null;
2888  if (orderList == null) {
2889  return;
2890  }
2891 
2892  if (!bb.top) {
2893  SqlNode offset = select.getOffset();
2894  if ((offset == null
2895  || (offset instanceof SqlLiteral
2896  && ((SqlLiteral) offset)
2897  .bigDecimalValue()
2898  .equals(BigDecimal.ZERO)))
2899  && select.getFetch() == null) {
2900  return;
2901  }
2902  }
2903 
2904  for (SqlNode orderItem : orderList) {
2905  collationList.add(convertOrderItem(select,
2906  orderItem,
2907  extraOrderExprs,
2908  RelFieldCollation.Direction.ASCENDING,
2909  RelFieldCollation.NullDirection.UNSPECIFIED));
2910  }
2911  }
int64_t const int32_t sz assert(dest)
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 336 of file SqlToRelConverter.java.

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

336  {
337  return cluster;
338  }
Set<RelColumnMapping> org.apache.calcite.sql2rel.SqlToRelConverter.getColumnMappings ( SqlOperator  op)
inlineprivate

Definition at line 2311 of file SqlToRelConverter.java.

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

2311  {
2312  SqlReturnTypeInference rti = op.getReturnTypeInference();
2313  if (rti == null) {
2314  return null;
2315  }
2316  if (rti instanceof TableFunctionReturnTypeInference) {
2317  TableFunctionReturnTypeInference tfrti = (TableFunctionReturnTypeInference) rti;
2318  return tfrti.getColumnMappings();
2319  } else {
2320  return null;
2321  }
2322  }

+ Here is the caller graph for this function:

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

Definition at line 2393 of file SqlToRelConverter.java.

References assert(), field(), org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.mapRootRelToFieldProjection, org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.root, and Experimental.String.

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

2393  {
2394  final Set<CorrelationId> correlatedVariables = RelOptUtil.getVariablesUsed(r0);
2395  if (correlatedVariables.isEmpty()) {
2396  return null;
2397  }
2398  final ImmutableBitSet.Builder requiredColumns = ImmutableBitSet.builder();
2399  final List<CorrelationId> correlNames = new ArrayList<>();
2400 
2401  // All correlations must refer the same namespace since correlation
2402  // produces exactly one correlation source.
2403  // The same source might be referenced by different variables since
2404  // DeferredLookups are not de-duplicated at create time.
2405  SqlValidatorNamespace prevNs = null;
2406 
2407  for (CorrelationId correlName : correlatedVariables) {
2408  DeferredLookup lookup = mapCorrelToDeferred.get(correlName);
2409  RexFieldAccess fieldAccess = lookup.getFieldAccess(correlName);
2410  String originalRelName = lookup.getOriginalRelName();
2411  String originalFieldName = fieldAccess.getField().getName();
2412 
2413  final SqlNameMatcher nameMatcher =
2414  bb.getValidator().getCatalogReader().nameMatcher();
2415  final SqlValidatorScope.ResolvedImpl resolved =
2416  new SqlValidatorScope.ResolvedImpl();
2417  lookup.bb.scope.resolve(
2418  ImmutableList.of(originalRelName), nameMatcher, false, resolved);
2419  assert resolved.count() == 1;
2420  final SqlValidatorScope.Resolve resolve = resolved.only();
2421  final SqlValidatorNamespace foundNs = resolve.namespace;
2422  final RelDataType rowType = resolve.rowType();
2423  final int childNamespaceIndex = resolve.path.steps().get(0).i;
2424  final SqlValidatorScope ancestorScope = resolve.scope;
2425  boolean correlInCurrentScope = bb.scope.isWithin(ancestorScope);
2426 
2427  if (!correlInCurrentScope) {
2428  continue;
2429  }
2430 
2431  if (prevNs == null) {
2432  prevNs = foundNs;
2433  } else {
2434  assert prevNs
2435  == foundNs : "All correlation variables should resolve"
2436  + " to the same namespace."
2437  + " Prev ns="
2438  + prevNs
2439  + ", new ns="
2440  + foundNs;
2441  }
2442 
2443  int namespaceOffset = 0;
2444  if (childNamespaceIndex > 0) {
2445  // If not the first child, need to figure out the width
2446  // of output types from all the preceding namespaces
2447  assert ancestorScope instanceof ListScope;
2448  List<SqlValidatorNamespace> children = ((ListScope) ancestorScope).getChildren();
2449 
2450  for (int i = 0; i < childNamespaceIndex; i++) {
2451  SqlValidatorNamespace child = children.get(i);
2452  namespaceOffset += child.getRowType().getFieldCount();
2453  }
2454  }
2455 
2456  RexFieldAccess topLevelFieldAccess = fieldAccess;
2457  while (topLevelFieldAccess.getReferenceExpr() instanceof RexFieldAccess) {
2458  topLevelFieldAccess = (RexFieldAccess) topLevelFieldAccess.getReferenceExpr();
2459  }
2460  final RelDataTypeField field = rowType.getFieldList().get(
2461  topLevelFieldAccess.getField().getIndex() - namespaceOffset);
2462  int pos = namespaceOffset + field.getIndex();
2463 
2464  assert field.getType() == topLevelFieldAccess.getField().getType();
2465 
2466  assert pos != -1;
2467 
2468  if (bb.mapRootRelToFieldProjection.containsKey(bb.root)) {
2469  // bb.root is an aggregate and only projects group by
2470  // keys.
2471  Map<Integer, Integer> exprProjection =
2472  bb.mapRootRelToFieldProjection.get(bb.root);
2473 
2474  // sub-query can reference group by keys projected from
2475  // the root of the outer relation.
2476  if (exprProjection.containsKey(pos)) {
2477  pos = exprProjection.get(pos);
2478  } else {
2479  // correl not grouped
2480  throw new AssertionError("Identifier '" + originalRelName + "."
2481  + originalFieldName + "' is not a group expr");
2482  }
2483  }
2484 
2485  requiredColumns.set(pos);
2486  correlNames.add(correlName);
2487  }
2488 
2489  if (correlNames.isEmpty()) {
2490  // None of the correlating variables originated in this scope.
2491  return null;
2492  }
2493 
2494  RelNode r = r0;
2495  if (correlNames.size() > 1) {
2496  // The same table was referenced more than once.
2497  // So we deduplicate.
2498  r = DeduplicateCorrelateVariables.go(
2499  rexBuilder, correlNames.get(0), Util.skip(correlNames), r0);
2500  // Add new node to leaves.
2501  leaves.add(r);
2502  }
2503  return new CorrelationUse(correlNames.get(0), requiredColumns.build(), r);
2504  }
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
int64_t const int32_t sz assert(dest)

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

353  {
354  return dynamicParamSqlNodes.size();
355  }
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 378 of file SqlToRelConverter.java.

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

378  {
379  int retVal = explainParamCount;
380  if (increment) {
382  }
383  return retVal;
384  }
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 363 of file SqlToRelConverter.java.

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

363  {
364  SqlNode sqlNode = dynamicParamSqlNodes.get(index);
365  if (sqlNode == null) {
366  throw Util.needToImplement("dynamic param type inference");
367  }
368  return validator.getValidatedNodeType(sqlNode);
369  }

+ Here is the caller graph for this function:

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

Definition at line 3311 of file SqlToRelConverter.java.

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

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

3312  {
3313  // We might unwrap a null instead of a InitializerExpressionFactory.
3314  final Table table = unwrap(validatorTable, Table.class);
3315  if (table != null) {
3316  InitializerExpressionFactory f = unwrap(table, InitializerExpressionFactory.class);
3317  if (f != null) {
3318  return f;
3319  }
3320  }
3321  return NullInitializerExpressionFactory.INSTANCE;
3322  }
static< T > T unwrap(Object o, Class< T > clazz)

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

1454  {
1455  return config.getInSubQueryThreshold();
1456  }
Map<SqlNode, RexNode> org.apache.calcite.sql2rel.SqlToRelConverter.getMapConvertedNonCorrSubqs ( )
inline
Returns
mapping of non-correlated sub-queries that have been converted to the constants that they evaluate to

Definition at line 390 of file SqlToRelConverter.java.

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

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

Returns the row-expression builder.

Definition at line 343 of file SqlToRelConverter.java.

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

343  {
344  return rexBuilder;
345  }
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 2553 of file SqlToRelConverter.java.

2553  {
2554  return Collections.emptyList();
2555  }
RelOptTable org.apache.calcite.sql2rel.SqlToRelConverter.getTargetTable ( SqlNode  call)
inlineprotected

Definition at line 3207 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().

3207  {
3208  final SqlValidatorNamespace targetNs = validator.getNamespace(call);
3209  if (targetNs.isWrapperFor(SqlValidatorImpl.DmlNamespace.class)) {
3210  final SqlValidatorImpl.DmlNamespace dmlNamespace =
3211  targetNs.unwrap(SqlValidatorImpl.DmlNamespace.class);
3212  return SqlValidatorUtil.getRelOptTable(dmlNamespace, catalogReader, null, null);
3213  }
3214  final SqlValidatorNamespace resolvedNamespace = targetNs.resolve();
3215  return SqlValidatorUtil.getRelOptTable(resolvedNamespace, catalogReader, null, null);
3216  }

+ Here is the caller graph for this function:

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

Definition at line 588 of file SqlToRelConverter.java.

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

588  {
589  switch (query.getKind()) {
590  case SELECT:
591  return ((SqlSelect) query).getOrderList() != null
592  && ((SqlSelect) query).getOrderList().size() > 0;
593  case WITH:
594  return isOrdered(((SqlWith) query).body);
595  case ORDER_BY:
596  return ((SqlOrderBy) query).orderList.size() > 0;
597  default:
598  return false;
599  }
600  }

+ Here is the caller graph for this function:

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

Definition at line 1626 of file SqlToRelConverter.java.

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

1626  {
1627  if (!(node.getKind() == SqlKind.ROW)) {
1628  return false;
1629  }
1630  SqlCall call = (SqlCall) node;
1631  return call.getOperator().getName().equalsIgnoreCase("row");
1632  }

+ Here is the caller graph for this function:

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

Definition at line 583 of file SqlToRelConverter.java.

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

583  {
584  return query instanceof SqlSelect
585  && ((SqlSelect) query).isKeywordPresent(SqlSelectKeyword.STREAM);
586  }

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

References Experimental.String.

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

2516  {
2517  Set<CorrelationId> correlatedVariables = RelOptUtil.getVariablesUsed(subq);
2518  for (CorrelationId correlName : correlatedVariables) {
2519  DeferredLookup lookup = mapCorrelToDeferred.get(correlName);
2520  String originalRelName = lookup.getOriginalRelName();
2521 
2522  final SqlNameMatcher nameMatcher =
2523  lookup.bb.scope.getValidator().getCatalogReader().nameMatcher();
2524  final SqlValidatorScope.ResolvedImpl resolved =
2525  new SqlValidatorScope.ResolvedImpl();
2526  lookup.bb.scope.resolve(
2527  ImmutableList.of(originalRelName), nameMatcher, false, resolved);
2528 
2529  SqlValidatorScope ancestorScope = resolved.only().scope;
2530 
2531  // If the correlated reference is in a scope that's "above" the
2532  // sub-query, then this is a correlated sub-query.
2533  SqlValidatorScope parentScope = bb.scope;
2534  do {
2535  if (ancestorScope == parentScope) {
2536  return false;
2537  }
2538  if (parentScope instanceof DelegatingScope) {
2539  parentScope = ((DelegatingScope) parentScope).getParent();
2540  } else {
2541  break;
2542  }
2543  } while (parentScope != null);
2544  }
2545  return true;
2546  }

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

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

3001  {
3002  return config.isTrimUnusedFields();
3003  }

+ Here is the caller graph for this function:

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

Definition at line 4558 of file SqlToRelConverter.java.

References assert().

4558  {
4559  assert operator.kind == SqlKind.ALL;
4560  return SqlStdOperatorTable.some(operator.comparisonKind.negateNullSafe());
4561  }
int64_t const int32_t sz assert(dest)

+ Here is the call graph for this function:

RelFieldTrimmer org.apache.calcite.sql2rel.SqlToRelConverter.newFieldTrimmer ( )
inlineprotected

Creates a RelFieldTrimmer.

Returns
Field trimmer

Definition at line 535 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().

535  {
536  return new RelFieldTrimmer(validator, relBuilder);
537  }

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

References assert(), org.apache.calcite.sql2rel.SqlToRelConverter.containsInOperator(), and org.apache.calcite.sql2rel.SqlToRelConverter.reg().

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

829  {
830  if (!(sqlNode instanceof SqlCall) || !containsInOperator(sqlNode)) {
831  return sqlNode;
832  }
833  final SqlCall sqlCall = (SqlCall) sqlNode;
834  switch (sqlCall.getKind()) {
835  case AND:
836  case OR:
837  final List<SqlNode> operands = new ArrayList<>();
838  for (SqlNode operand : sqlCall.getOperandList()) {
839  operands.add(pushDownNotForIn(scope, operand));
840  }
841  final SqlCall newCall =
842  sqlCall.getOperator().createCall(sqlCall.getParserPosition(), operands);
843  return reg(scope, newCall);
844 
845  case NOT:
846  assert sqlCall.operand(0) instanceof SqlCall;
847  final SqlCall call = sqlCall.operand(0);
848  switch (sqlCall.operand(0).getKind()) {
849  case CASE:
850  final SqlCase caseNode = (SqlCase) call;
851  final SqlNodeList thenOperands = new SqlNodeList(SqlParserPos.ZERO);
852 
853  for (SqlNode thenOperand : caseNode.getThenOperands()) {
854  final SqlCall not =
855  SqlStdOperatorTable.NOT.createCall(SqlParserPos.ZERO, thenOperand);
856  thenOperands.add(pushDownNotForIn(scope, reg(scope, not)));
857  }
858  SqlNode elseOperand = caseNode.getElseOperand();
859  if (!SqlUtil.isNull(elseOperand)) {
860  // "not(unknown)" is "unknown", so no need to simplify
861  final SqlCall not =
862  SqlStdOperatorTable.NOT.createCall(SqlParserPos.ZERO, elseOperand);
863  elseOperand = pushDownNotForIn(scope, reg(scope, not));
864  }
865 
866  return reg(scope,
867  SqlStdOperatorTable.CASE.createCall(SqlParserPos.ZERO,
868  caseNode.getValueOperand(),
869  caseNode.getWhenOperands(),
870  thenOperands,
871  elseOperand));
872 
873  case AND:
874  final List<SqlNode> orOperands = new ArrayList<>();
875  for (SqlNode operand : call.getOperandList()) {
876  orOperands.add(pushDownNotForIn(scope,
877  reg(scope,
878  SqlStdOperatorTable.NOT.createCall(
879  SqlParserPos.ZERO, operand))));
880  }
881  return reg(scope,
882  SqlStdOperatorTable.OR.createCall(SqlParserPos.ZERO, orOperands));
883 
884  case OR:
885  final List<SqlNode> andOperands = new ArrayList<>();
886  for (SqlNode operand : call.getOperandList()) {
887  andOperands.add(pushDownNotForIn(scope,
888  reg(scope,
889  SqlStdOperatorTable.NOT.createCall(
890  SqlParserPos.ZERO, operand))));
891  }
892  return reg(scope,
893  SqlStdOperatorTable.AND.createCall(SqlParserPos.ZERO, andOperands));
894 
895  case NOT:
896  assert call.operandCount() == 1;
897  return pushDownNotForIn(scope, call.operand(0));
898 
899  case NOT_IN:
900  return reg(scope,
901  SqlStdOperatorTable.IN.createCall(
902  SqlParserPos.ZERO, call.getOperandList()));
903 
904  case IN:
905  return reg(scope,
906  SqlStdOperatorTable.NOT_IN.createCall(
907  SqlParserPos.ZERO, call.getOperandList()));
908  }
909  }
910  return sqlNode;
911  }
static SqlNode pushDownNotForIn(SqlValidatorScope scope, SqlNode sqlNode)
int64_t const int32_t sz assert(dest)
static SqlNode reg(SqlValidatorScope scope, SqlNode e)

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

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

917  {
918  scope.getValidator().deriveType(scope, e);
919  return e;
920  }

+ 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 959 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.convertFrom(), org.apache.calcite.sql2rel.SqlToRelConverter.convertJoinCondition(), org.apache.calcite.sql2rel.SqlToRelConverter.convertMatchRecognize(), org.apache.calcite.sql2rel.SqlToRelConverter.convertUpdate(), org.apache.calcite.sql2rel.SqlToRelConverter.convertWhere(), and org.apache.calcite.sql2rel.SqlToRelConverter.createAggImpl().

960  {
961  findSubQueries(bb, expr, logic, false);
962  for (SubQuery node : bb.subQueryList) {
963  substituteSubQuery(bb, node);
964  }
965  }
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 602 of file SqlToRelConverter.java.

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

602  {
603  if (r instanceof Sort) {
604  return ((Sort) r).collation;
605  }
606  if (r instanceof Project) {
607  return requiredCollation(((Project) r).getInput());
608  }
609  if (r instanceof Delta) {
610  return requiredCollation(((Delta) r).getInput());
611  }
612  throw new AssertionError();
613  }

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

References assert(), and org.apache.calcite.sql2rel.SqlToRelConverter.explainParamCount.

421  {
422  assert config.isExplain();
423  this.explainParamCount = explainParamCount;
424  }
int64_t const int32_t sz assert(dest)

+ Here is the call graph for this function:

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

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

412  {
413  subQueryConverter = converter;
414  }
void org.apache.calcite.sql2rel.SqlToRelConverter.snapshotTemporalTable ( Blackboard  bb,
SqlCall  call 
)
inlineprivate

Definition at line 2297 of file SqlToRelConverter.java.

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

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

2297  {
2298  final SqlSnapshot snapshot = (SqlSnapshot) call;
2299  final RexNode period = bb.convertExpression(snapshot.getPeriod());
2300 
2301  // convert inner query, could be a table name or a derived table
2302  SqlNode expr = snapshot.getTableRef();
2303  convertFrom(bb, expr);
2304  final TableScan scan = (TableScan) bb.root;
2305 
2306  final RelNode snapshotRel = relBuilder.push(scan).snapshot(period).build();
2307 
2308  bb.setRoot(snapshotRel, false);
2309  }
void convertFrom(Blackboard bb, SqlNode from)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void org.apache.calcite.sql2rel.SqlToRelConverter.substituteSubQuery ( Blackboard  bb,
SubQuery  subQuery 
)
inlineprivate

Definition at line 967 of file SqlToRelConverter.java.

References run_benchmark_import.args, assert(), 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(), Double, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.getTopNode(), join(), org.apache.calcite.sql2rel.SqlToRelConverter.SubQuery.logic, org.apache.calcite.sql2rel.SqlToRelConverter.SubQuery.node, org.apache.calcite.sql2rel.SqlToRelConverter.relBuilder, org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard.root, 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().

967  {
968  final RexNode expr = subQuery.expr;
969  if (expr != null) {
970  // Already done.
971  return;
972  }
973 
974  final SqlBasicCall call;
975  final RelNode rel;
976  final SqlNode query;
977  final RelOptUtil.Exists converted;
978 
979  boolean isExpand = config.getExpandPredicate().test(bb.getTopNode(), subQuery.node);
980 
981  switch (subQuery.node.getKind()) {
982  case CURSOR:
983  convertCursor(bb, subQuery);
984  return;
985 
986  case MULTISET_QUERY_CONSTRUCTOR:
987  case MULTISET_VALUE_CONSTRUCTOR:
988  case ARRAY_QUERY_CONSTRUCTOR:
989  rel = convertMultisets(ImmutableList.of(subQuery.node), bb);
990  subQuery.expr = bb.register(rel, JoinRelType.INNER);
991  return;
992 
993  case IN:
994  case NOT_IN:
995  case SOME:
996  case ALL:
997  call = (SqlBasicCall) subQuery.node;
998  query = call.operand(1);
999  if (!isExpand && !(query instanceof SqlNodeList)) {
1000  return;
1001  }
1002  final SqlNode leftKeyNode = call.operand(0);
1003 
1004  final List<RexNode> leftKeys;
1005  switch (leftKeyNode.getKind()) {
1006  case ROW:
1007  leftKeys = new ArrayList<>();
1008  for (SqlNode sqlExpr : ((SqlBasicCall) leftKeyNode).getOperandList()) {
1009  leftKeys.add(bb.convertExpression(sqlExpr));
1010  }
1011  break;
1012  default:
1013  leftKeys = ImmutableList.of(bb.convertExpression(leftKeyNode));
1014  }
1015 
1016  if (query instanceof SqlNodeList) {
1017  SqlNodeList valueList = (SqlNodeList) query;
1018  if (!containsNullLiteral(valueList)
1019  && valueList.size() < config.getInSubQueryThreshold()) {
1020  // We're under the threshold, so convert to OR.
1021  subQuery.expr = convertInToOr(
1022  bb, leftKeys, valueList, (SqlInOperator) call.getOperator());
1023  return;
1024  }
1025 
1026  // Otherwise, let convertExists translate
1027  // values list into an inline table for the
1028  // reference to Q below.
1029  }
1030 
1031  // Project out the search columns from the left side
1032 
1033  // Q1:
1034  // "select from emp where emp.deptno in (select col1 from T)"
1035  //
1036  // is converted to
1037  //
1038  // "select from
1039  // emp inner join (select distinct col1 from T)) q
1040  // on emp.deptno = q.col1
1041  //
1042  // Q2:
1043  // "select from emp where emp.deptno not in (Q)"
1044  //
1045  // is converted to
1046  //
1047  // "select from
1048  // emp left outer join (select distinct col1, TRUE from T) q
1049  // on emp.deptno = q.col1
1050  // where emp.deptno <> null
1051  // and q.indicator <> TRUE"
1052  //
1053  // Note: Sub-query can be used as SqlUpdate#condition like below:
1054  //
1055  // UPDATE emp
1056  // SET empno = 1 WHERE emp.empno IN (
1057  // SELECT emp.empno FROM emp WHERE emp.empno = 2)
1058  //
1059  // In such case, when converting SqlUpdate#condition, bb.root is null
1060  // and it makes no sense to do the sub-query substitution.
1061  if (bb.root == null) {
1062  return;
1063  }
1064  final RelDataType targetRowType = SqlTypeUtil.promoteToRowType(
1065  typeFactory, validator.getValidatedNodeType(leftKeyNode), null);
1066  final boolean notIn = call.getOperator().kind == SqlKind.NOT_IN;
1067  converted = convertExists(
1068  query, RelOptUtil.SubQueryType.IN, subQuery.logic, notIn, targetRowType);
1069  if (converted.indicator) {
1070  // Generate
1071  // emp CROSS JOIN (SELECT COUNT(*) AS c,
1072  // COUNT(deptno) AS ck FROM dept)
1073  final RelDataType longType = typeFactory.createSqlType(SqlTypeName.BIGINT);
1074  final RelNode seek = converted.r.getInput(0); // fragile
1075  final int keyCount = leftKeys.size();
1076  final List<Integer> args = ImmutableIntList.range(0, keyCount);
1077  LogicalAggregate aggregate = LogicalAggregate.create(seek,
1078  ImmutableBitSet.of(),
1079  null,
1080  ImmutableList.of(AggregateCall.create(SqlStdOperatorTable.COUNT,
1081  false,
1082  false,
1083  false,
1084  ImmutableList.of(),
1085  -1,
1086  RelCollations.EMPTY,
1087  longType,
1088  null),
1089  AggregateCall.create(SqlStdOperatorTable.COUNT,
1090  false,
1091  false,
1092  false,
1093  args,
1094  -1,
1095  RelCollations.EMPTY,
1096  longType,
1097  null)));
1098  LogicalJoin join = LogicalJoin.create(bb.root,
1099  aggregate,
1100  rexBuilder.makeLiteral(true),
1101  ImmutableSet.of(),
1102  JoinRelType.INNER);
1103  bb.setRoot(join, false);
1104  }
1105  final RexNode rex = bb.register(converted.r,
1106  converted.outerJoin ? JoinRelType.LEFT : JoinRelType.INNER,
1107  leftKeys);
1108 
1109  RelOptUtil.Logic logic = subQuery.logic;
1110  switch (logic) {
1111  case TRUE_FALSE_UNKNOWN:
1112  case UNKNOWN_AS_TRUE:
1113  if (!converted.indicator) {
1114  logic = RelOptUtil.Logic.TRUE_FALSE;
1115  }
1116  }
1117  subQuery.expr = translateIn(logic, bb.root, rex);
1118  if (notIn) {
1119  subQuery.expr = rexBuilder.makeCall(SqlStdOperatorTable.NOT, subQuery.expr);
1120  }
1121  return;
1122 
1123  case EXISTS:
1124  // "select from emp where exists (select a from T)"
1125  //
1126  // is converted to the following if the sub-query is correlated:
1127  //
1128  // "select from emp left outer join (select AGG_TRUE() as indicator
1129  // from T group by corr_var) q where q.indicator is true"
1130  //
1131  // If there is no correlation, the expression is replaced with a
1132  // boolean indicating whether the sub-query returned 0 or >= 1 row.
1133  call = (SqlBasicCall) subQuery.node;
1134  query = call.operand(0);
1135  if (!isExpand) {
1136  return;
1137  }
1138  final SqlValidatorScope seekScope = (query instanceof SqlSelect)
1139  ? validator.getSelectScope((SqlSelect) query)
1140  : null;
1141  final Blackboard seekBb = createBlackboard(seekScope, null, false);
1142  final RelNode seekRel = convertQueryOrInList(seekBb, query, null);
1143  // An EXIST sub-query whose inner child has at least 1 tuple
1144  // (e.g. an Aggregate with no grouping columns or non-empty Values
1145  // node) should be simplified to a Boolean constant expression.
1146  final RelMetadataQuery mq = seekRel.getCluster().getMetadataQuery();
1147  final Double minRowCount = mq.getMinRowCount(seekRel);
1148  if (minRowCount != null && minRowCount >= 1D) {
1149  subQuery.expr = rexBuilder.makeLiteral(true);
1150  return;
1151  }
1152  converted = RelOptUtil.createExistsPlan(seekRel,
1153  RelOptUtil.SubQueryType.EXISTS,
1154  subQuery.logic,
1155  true,
1156  relBuilder);
1157  assert !converted.indicator;
1158  if (convertNonCorrelatedSubQuery(subQuery, bb, converted.r, true)) {
1159  return;
1160  }
1161  subQuery.expr = bb.register(converted.r, JoinRelType.LEFT);
1162  return;
1163 
1164  case SCALAR_QUERY:
1165  // Convert the sub-query. If it's non-correlated, convert it
1166  // to a constant expression.
1167  if (!isExpand) {
1168  return;
1169  }
1170  call = (SqlBasicCall) subQuery.node;
1171  query = call.operand(0);
1172  converted = convertExists(
1173  query, RelOptUtil.SubQueryType.SCALAR, subQuery.logic, true, null);
1174  assert !converted.indicator;
1175  if (convertNonCorrelatedSubQuery(subQuery, bb, converted.r, false)) {
1176  return;
1177  }
1178  rel = convertToSingleValueSubq(query, converted.r);
1179  subQuery.expr = bb.register(rel, JoinRelType.LEFT);
1180  return;
1181 
1182  case SELECT:
1183  // This is used when converting multiset queries:
1184  //
1185  // select * from unnest(select multiset[deptno] from emps);
1186  //
1187  converted = convertExists(subQuery.node,
1188  RelOptUtil.SubQueryType.SCALAR,
1189  subQuery.logic,
1190  true,
1191  null);
1192  assert !converted.indicator;
1193  subQuery.expr = bb.register(converted.r, JoinRelType.LEFT);
1194  return;
1195 
1196  default:
1197  throw new AssertionError("unexpected kind of sub-query: " + subQuery.node);
1198  }
1199  }
RexNode translateIn(RelOptUtil.Logic logic, RelNode root, final RexNode rex)
RelNode convertCursor(Blackboard bb, SubQuery subQuery)
RelNode convertQueryOrInList(Blackboard bb, SqlNode seek, RelDataType targetRowType)
std::string join(T const &container, std::string const &delim)
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)
int64_t const int32_t sz assert(dest)
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)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.toRel ( final RelOptTable  table)
inline

Definition at line 3172 of file SqlToRelConverter.java.

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

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

3172  {
3173  final RelNode scan = table.toRel(createToRelContext());
3174 
3175  final InitializerExpressionFactory ief =
3176  Util.first(table.unwrap(InitializerExpressionFactory.class),
3177  NullInitializerExpressionFactory.INSTANCE);
3178 
3179  // Lazily create a blackboard that contains all non-generated columns.
3180  final Supplier<Blackboard> bb = () -> {
3181  RexNode sourceRef = rexBuilder.makeRangeReference(scan);
3182  return createInsertBlackboard(table, sourceRef, table.getRowType().getFieldNames());
3183  };
3184 
3185  int virtualCount = 0;
3186  final List<RexNode> list = new ArrayList<>();
3187  for (RelDataTypeField f : table.getRowType().getFieldList()) {
3188  final ColumnStrategy strategy = ief.generationStrategy(table, f.getIndex());
3189  switch (strategy) {
3190  case VIRTUAL:
3191  list.add(ief.newColumnDefaultValue(table, f.getIndex(), bb.get()));
3192  ++virtualCount;
3193  break;
3194  default:
3195  list.add(rexBuilder.makeInputRef(
3196  scan, RelOptTableImpl.realOrdinal(table, f.getIndex())));
3197  }
3198  }
3199  if (virtualCount > 0) {
3200  relBuilder.push(scan);
3201  relBuilder.project(list);
3202  return relBuilder.build();
3203  }
3204  return scan;
3205  }
Blackboard createInsertBlackboard(RelOptTable targetTable, RexNode sourceRef, List< String > targetColumnNames)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RexNode org.apache.calcite.sql2rel.SqlToRelConverter.translateIn ( RelOptUtil.Logic  logic,
RelNode  root,
final RexNode  rex 
)
inlineprivate

Definition at line 1201 of file SqlToRelConverter.java.

References run_benchmark_import.args, assert(), join(), and org.apache.calcite.sql2rel.SqlToRelConverter.rexBuilder.

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

1201  {
1202  switch (logic) {
1203  case TRUE:
1204  return rexBuilder.makeLiteral(true);
1205 
1206  case TRUE_FALSE:
1207  case UNKNOWN_AS_FALSE:
1208  assert rex instanceof RexRangeRef;
1209  final int fieldCount = rex.getType().getFieldCount();
1210  RexNode rexNode = rexBuilder.makeFieldAccess(rex, fieldCount - 1);
1211  rexNode = rexBuilder.makeCall(SqlStdOperatorTable.IS_TRUE, rexNode);
1212 
1213  // Then append the IS NOT NULL(leftKeysForIn).
1214  //
1215  // RexRangeRef contains the following fields:
1216  // leftKeysForIn,
1217  // rightKeysForIn (the original sub-query select list),
1218  // nullIndicator
1219  //
1220  // The first two lists contain the same number of fields.
1221  final int k = (fieldCount - 1) / 2;
1222  for (int i = 0; i < k; i++) {
1223  rexNode = rexBuilder.makeCall(SqlStdOperatorTable.AND,
1224  rexNode,
1225  rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
1226  rexBuilder.makeFieldAccess(rex, i)));
1227  }
1228  return rexNode;
1229 
1230  case TRUE_FALSE_UNKNOWN:
1231  case UNKNOWN_AS_TRUE:
1232  // select e.deptno,
1233  // case
1234  // when ct.c = 0 then false
1235  // when dt.i is not null then true
1236  // when e.deptno is null then null
1237  // when ct.ck < ct.c then null
1238  // else false
1239  // end
1240  // from e
1241  // cross join (select count(*) as c, count(deptno) as ck from v) as ct
1242  // left join (select distinct deptno, true as i from v) as dt
1243  // on e.deptno = dt.deptno
1244  final Join join = (Join) root;
1245  final Project left = (Project) join.getLeft();
1246  final RelNode leftLeft = ((Join) left.getInput()).getLeft();
1247  final int leftLeftCount = leftLeft.getRowType().getFieldCount();
1248  final RelDataType longType = typeFactory.createSqlType(SqlTypeName.BIGINT);
1249  final RexNode cRef = rexBuilder.makeInputRef(root, leftLeftCount);
1250  final RexNode ckRef = rexBuilder.makeInputRef(root, leftLeftCount + 1);
1251  final RexNode iRef =
1252  rexBuilder.makeInputRef(root, root.getRowType().getFieldCount() - 1);
1253 
1254  final RexLiteral zero = rexBuilder.makeExactLiteral(BigDecimal.ZERO, longType);
1255  final RexLiteral trueLiteral = rexBuilder.makeLiteral(true);
1256  final RexLiteral falseLiteral = rexBuilder.makeLiteral(false);
1257  final RexNode unknownLiteral = rexBuilder.makeNullLiteral(trueLiteral.getType());
1258 
1259  final ImmutableList.Builder<RexNode> args = ImmutableList.builder();
1260  args.add(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, cRef, zero),
1261  falseLiteral,
1262  rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, iRef),
1263  trueLiteral);
1264  final JoinInfo joinInfo = join.analyzeCondition();
1265  for (int leftKey : joinInfo.leftKeys) {
1266  final RexNode kRef = rexBuilder.makeInputRef(root, leftKey);
1267  args.add(
1268  rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, kRef), unknownLiteral);
1269  }
1270  args.add(rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN, ckRef, cRef),
1271  unknownLiteral,
1272  falseLiteral);
1273 
1274  return rexBuilder.makeCall(SqlStdOperatorTable.CASE, args.build());
1275 
1276  default:
1277  throw new AssertionError(logic);
1278  }
1279  }
std::string join(T const &container, std::string const &delim)
int64_t const int32_t sz assert(dest)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelNode org.apache.calcite.sql2rel.SqlToRelConverter.trimUnusedFields ( boolean  ordered,
RelNode  rootRel 
)
inline

Walks over a tree of relational expressions, replacing each RelNode with a 'slimmed down' relational expression that projects only the fields required by its consumer.

This may make things easier for the optimizer, by removing crud that would expand the search space, but is difficult for the optimizer itself to do it, because optimizer rules must preserve the number and type of fields. Hence, this transform that operates on the entire tree, similar to the type-flattening transform.

Currently this functionality is disabled in farrago/luciddb; the default implementation of this method does nothing.

Parameters
orderedWhether the relational expression must produce results in a particular order (typically because it has an ORDER BY at top level)
rootRelRelational expression that is at the root of the tree
Returns
Trimmed relational expression

Definition at line 507 of file SqlToRelConverter.java.

References org.apache.calcite.sql2rel.SqlToRelConverter.isTrimUnusedFields(), org.apache.calcite.sql2rel.SqlToRelConverter.newFieldTrimmer(), and org.apache.calcite.sql2rel.SqlToRelConverter.SQL2REL_LOGGER.

507  {
508  // Trim fields that are not used by their consumer.
509  if (isTrimUnusedFields()) {
510  final RelFieldTrimmer trimmer = newFieldTrimmer();
511  final List<RelCollation> collations =
512  rootRel.getTraitSet().getTraits(RelCollationTraitDef.INSTANCE);
513  rootRel = trimmer.trim(rootRel);
514  if (!ordered && collations != null && !collations.isEmpty()
515  && !collations.equals(ImmutableList.of(RelCollations.EMPTY))) {
516  final RelTraitSet traitSet =
517  rootRel.getTraitSet().replace(RelCollationTraitDef.INSTANCE, collations);
518  rootRel = rootRel.copy(traitSet, rootRel.getInputs());
519  }
520  if (SQL2REL_LOGGER.isDebugEnabled()) {
521  SQL2REL_LOGGER.debug(RelOptUtil.dumpPlan("Plan after trimming unused fields",
522  rootRel,
523  SqlExplainFormat.TEXT,
524  SqlExplainLevel.EXPPLAN_ATTRIBUTES));
525  }
526  }
527  return rootRel;
528  }

+ Here is the call graph for this function:

static <T> T org.apache.calcite.sql2rel.SqlToRelConverter.unwrap ( Object  o,
Class< T >  clazz 
)
inlinestaticprivate

Definition at line 3324 of file SqlToRelConverter.java.

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

3324  {
3325  if (o instanceof Wrapper) {
3326  return ((Wrapper) o).unwrap(clazz);
3327  }
3328  return null;
3329  }

+ Here is the caller graph for this function:

Member Data Documentation

final Deque<String> org.apache.calcite.sql2rel.SqlToRelConverter.datasetStack = new ArrayDeque<>()
private

Stack of names of datasets requested by the TABLE(SAMPLE(<datasetName>, <query>)) construct.

Definition at line 258 of file SqlToRelConverter.java.

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

final int org.apache.calcite.sql2rel.SqlToRelConverter.DEFAULT_IN_SUB_QUERY_THRESHOLD = 20
static

Size of the smallest IN list that will be converted to a semijoin to a static table.

Definition at line 228 of file SqlToRelConverter.java.

final int org.apache.calcite.sql2rel.SqlToRelConverter.DEFAULT_IN_SUBQUERY_THRESHOLD = DEFAULT_IN_SUB_QUERY_THRESHOLD
static

Definition at line 231 of file SqlToRelConverter.java.

final List<SqlDynamicParam> org.apache.calcite.sql2rel.SqlToRelConverter.dynamicParamSqlNodes = new ArrayList<>()
private

Definition at line 241 of file SqlToRelConverter.java.

int org.apache.calcite.sql2rel.SqlToRelConverter.explainParamCount
private
final SqlNodeToRexConverter org.apache.calcite.sql2rel.SqlToRelConverter.exprConverter
private

Definition at line 244 of file SqlToRelConverter.java.

final List<RelNode> org.apache.calcite.sql2rel.SqlToRelConverter.leaves = new ArrayList<>()
protected

Definition at line 240 of file SqlToRelConverter.java.

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

Mapping of non-correlated sub-queries that have been converted to their equivalent constants. Used to avoid re-evaluating the sub-query if it's already been evaluated.

Definition at line 265 of file SqlToRelConverter.java.

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

final Map<CorrelationId, DeferredLookup> org.apache.calcite.sql2rel.SqlToRelConverter.mapCorrelToDeferred = new HashMap<>()
private

Fields used in name resolution for correlated sub-queries.

Definition at line 252 of file SqlToRelConverter.java.

final SqlOperatorTable org.apache.calcite.sql2rel.SqlToRelConverter.opTab
private

Definition at line 242 of file SqlToRelConverter.java.

final Logger org.apache.calcite.sql2rel.SqlToRelConverter.SQL2REL_LOGGER = CalciteTrace.getSqlToRelTracer()
staticprotected
SubQueryConverter org.apache.calcite.sql2rel.SqlToRelConverter.subQueryConverter
private
final BigDecimal org.apache.calcite.sql2rel.SqlToRelConverter.TWO = BigDecimal.valueOf(2L)
staticprivate

Definition at line 222 of file SqlToRelConverter.java.

final RelOptTable.ViewExpander org.apache.calcite.sql2rel.SqlToRelConverter.viewExpander

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