diff --git a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java index e4618c808b37..074471d9f256 100644 --- a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java +++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java @@ -398,16 +398,7 @@ private SqlNode groupItem(List groupKeys, /** @see #dispatch */ public Result visit(TableScan e) { - final SqlIdentifier identifier; - final JdbcTable jdbcTable = e.getTable().unwrap(JdbcTable.class); - if (jdbcTable != null) { - // Use the foreign catalog, schema and table names, if they exist, - // rather than the qualified name of the shadow table in Calcite. - identifier = jdbcTable.tableName(); - } else { - final List qualifiedName = e.getTable().getQualifiedName(); - identifier = new SqlIdentifier(qualifiedName, SqlParserPos.ZERO); - } + final SqlIdentifier identifier = getSqlTargetTable(e); return result(identifier, ImmutableList.of(Clause.FROM), e, null); } @@ -652,14 +643,28 @@ public boolean hasTrickyRollup(Sort e, Aggregate aggregate) { fc.getFieldIndex() < aggregate.getGroupSet().cardinality()); } + private SqlIdentifier getSqlTargetTable(RelNode e) { + final SqlIdentifier sqlTargetTable; + final JdbcTable jdbcTable = e.getTable().unwrap(JdbcTable.class); + if (jdbcTable != null) { + // Use the foreign catalog, schema and table names, if they exist, + // rather than the qualified name of the shadow table in Calcite. + sqlTargetTable = jdbcTable.tableName(); + } else { + final List qualifiedName = e.getTable().getQualifiedName(); + sqlTargetTable = new SqlIdentifier(qualifiedName, SqlParserPos.ZERO); + } + + return sqlTargetTable; + } + /** @see #dispatch */ public Result visit(TableModify modify) { final Map pairs = ImmutableMap.of(); final Context context = aliasContext(pairs, false); // Target Table Name - final SqlIdentifier sqlTargetTable = - new SqlIdentifier(modify.getTable().getQualifiedName(), POS); + final SqlIdentifier sqlTargetTable = getSqlTargetTable(modify); switch (modify.getOperation()) { case INSERT: {