Class RelToSqlConverter
- All Implemented Interfaces:
ReflectiveVisitor
- Direct Known Subclasses:
JdbcImplementor,PigRelToSqlConverter
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.calcite.rel.rel2sql.SqlImplementor
SqlImplementor.AliasContext, SqlImplementor.BaseContext, SqlImplementor.Builder, SqlImplementor.Clause, SqlImplementor.Context, SqlImplementor.MatchRecognizeContext, SqlImplementor.Result, SqlImplementor.SimpleContext -
Field Summary
Fields inherited from class org.apache.calcite.rel.rel2sql.SqlImplementor
aliasSet, correlTableMap, dialect, POS -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSelect(List<SqlNode> selectList, SqlNode node, RelDataType rowType) protected voidbuildAggGroupList(Aggregate e, SqlImplementor.Builder builder, List<SqlNode> groupByList, List<SqlNode> selectList) Builds the group list for an Aggregate node.protected SqlImplementor.BuilderbuildAggregate(Aggregate e, SqlImplementor.Builder builder, List<SqlNode> selectList, List<SqlNode> groupByList) Builds an aggregate query.createAsFullOperands(RelDataType rowType, SqlNode leftOperand, String alias) Creates operands for a full AS operator.protected SqlImplementor.ResultDispatches a call to thevisit(Xxx e)method whereXxxmost closely matches the runtime type of the argument.booleanhasTrickyRollup(Sort e, Aggregate aggregate) protected booleanisAnon()Returns whether to remove trivial aliases such as "EXPR$0" when converting the current relational expression into a SELECT.protected SqlImplementor.Resultresult(SqlNode node, Collection<SqlImplementor.Clause> clauses, @Nullable String neededAlias, @Nullable RelDataType neededType, Map<String, RelDataType> aliases) Factory method forSqlImplementor.Result.Visits an Aggregate; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a Calc; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a Correlate; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a Filter; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits an Intersect; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a Join; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a Match; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a Minus; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a Sample; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a Sort; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.visit(TableModify modify) Visits a TableModify; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a TableScan; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a Union; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a Values; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a Window; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.Visits a RelNode; called bydispatch(org.apache.calcite.rel.RelNode)via reflection.protected SqlImplementor.ResultvisitInput(RelNode parent, int i, boolean anon, boolean ignoreClauses, Set<SqlImplementor.Clause> expectedClauses) Visits theith input ofe, the current relational expression.protected SqlImplementor.ResultwrapNestedJoin(SqlImplementor.Result input, RelNode inputRel, String outerAlias) Wraps a nested join into a subquery with explicit column aliases.Methods inherited from class org.apache.calcite.rel.rel2sql.SqlImplementor
aliasContext, as, convertConditionToSqlNode, getAliasContext, isStar, isStar, joinContext, joinType, matchRecognizeContext, result, result, selectListContext, setOpToSql, tableFunctionScanContext, toSql, toSql, visitChild, visitInput, visitInput, visitInput, visitRoot, windowTableFunctionScanContext
-
Constructor Details
-
RelToSqlConverter
Creates a RelToSqlConverter.
-
-
Method Details
-
dispatch
Dispatches a call to thevisit(Xxx e)method whereXxxmost closely matches the runtime type of the argument. -
visitInput
public SqlImplementor.Result visitInput(RelNode parent, int i, boolean anon, boolean ignoreClauses, Set<SqlImplementor.Clause> expectedClauses) Description copied from class:SqlImplementorVisits theith input ofe, the current relational expression.- Specified by:
visitInputin classSqlImplementor- Parameters:
parent- Current relational expressioni- Ordinal of input withineanon- Whether to remove trivial aliases such as "EXPR$0"ignoreClauses- Whether to ignore the expected clauses when deciding whether a sub-query is requiredexpectedClauses- Set of clauses that we expect the builder that consumes this result will create- Returns:
- Result
- See Also:
-
isAnon
protected boolean isAnon()Description copied from class:SqlImplementorReturns whether to remove trivial aliases such as "EXPR$0" when converting the current relational expression into a SELECT.For example, INSERT does not care about field names; we would prefer to generate without the "EXPR$0" alias:
INSERT INTO t1 SELECT x, y + 1 FROM t2rather than with it:
INSERT INTO t1 SELECT x, y + 1 AS EXPR$0 FROM t2But JOIN does care about field names; we have to generate the "EXPR$0" alias:
SELECT * FROM emp AS e JOIN (SELECT x, y + 1 AS EXPR$0) AS d ON e.deptno = d.EXPR$0because if we omit "AS EXPR$0" we do not know the field we are joining to, and the following is invalid:
SELECT * FROM emp AS e JOIN (SELECT x, y + 1) AS d ON e.deptno = d.EXPR$0- Overrides:
isAnonin classSqlImplementor
-
result
protected SqlImplementor.Result result(SqlNode node, Collection<SqlImplementor.Clause> clauses, @Nullable String neededAlias, @Nullable RelDataType neededType, Map<String, RelDataType> aliases) Description copied from class:SqlImplementorFactory method forSqlImplementor.Result.Call this method rather than creating a
Resultdirectly, because sub-classes may override.- Overrides:
resultin classSqlImplementor
-
visit
Visits a RelNode; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
wrapNestedJoin
protected SqlImplementor.Result wrapNestedJoin(SqlImplementor.Result input, RelNode inputRel, String outerAlias) Wraps a nested join into a subquery with explicit column aliases.This is specifically required for dialects like ClickHouse where the identifier resolver cannot resolve columns with internal table qualifiers (e.g., 'd1.loc') when they are wrapped in a subquery alias. By forcing an explicit 'AS' projection for every column, we ensure the identifiers are flattened and visible to the outer query block.
- Parameters:
input- The Result of the nested join branchinputRel- The RelNode representing the join branch to extract row typesouterAlias- The alias to be assigned to the wrapped subquery- Returns:
- A new Result containing the wrapped SQL with explicit aliases
-
visit
Visits a Join; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visitAntiOrSemiJoin
-
visit
Visits a Correlate; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
Visits a Filter; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
Visits aProjectand converts it to aSqlSelect.If the project defines correlation variables (e.g., via
$cor0), this method ensures that the input relation is assigned a stable alias (either the natural table name or a synthetic alias like 't'). This enables nested correlated subqueries to correctly qualify their column references back to this project's scope.For simple table scans, it avoids forcing an explicit 'AS' clause to maintain compatibility with DML statements (like UPDATE/DELETE) in certain SQL dialects.
-
visit
Visits a Window; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
Visits an Aggregate; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
buildAggGroupList
protected void buildAggGroupList(Aggregate e, SqlImplementor.Builder builder, List<SqlNode> groupByList, List<SqlNode> selectList) Builds the group list for an Aggregate node.- Parameters:
e- The Aggregate nodebuilder- The SQL buildergroupByList- output group listselectList- output select list
-
buildAggregate
protected SqlImplementor.Builder buildAggregate(Aggregate e, SqlImplementor.Builder builder, List<SqlNode> selectList, List<SqlNode> groupByList) Builds an aggregate query.- Parameters:
e- The Aggregate nodebuilder- The SQL builderselectList- The precomputed group listgroupByList- The precomputed select list- Returns:
- The aggregate query result
-
visit
Visits a TableScan; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
Visits a Union; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
Visits an Intersect; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
Visits a Minus; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
Visits a Calc; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
Visits a Values; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
Visits a Sample; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
Visits a Sort; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
hasTrickyRollup
-
visit
Visits a TableModify; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
Visits a Match; called bydispatch(org.apache.calcite.rel.RelNode)via reflection. -
visit
-
visit
-
createAsFullOperands
Creates operands for a full AS operator. Format SqlNode AS alias(col_1, col_2,... ,col_n).- Parameters:
rowType- Row type of the SqlNodeleftOperand- SqlNodealias- alias
-
addSelect
- Overrides:
addSelectin classSqlImplementor
-