You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by cb...@apache.org on 2020/03/23 09:12:37 UTC
[calcite] 02/02: [CALCITE-3840] Re-aliasing of VALUES that has
column aliases produces wrong SQL in the JDBC adapter
This is an automated email from the ASF dual-hosted git repository.
cbeikov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git
commit 893d41fa4b79dbac6b613f214de6b4fbab9e6773
Author: Christian Beikov <ch...@gmail.com>
AuthorDate: Wed Mar 4 11:49:40 2020 +0100
[CALCITE-3840] Re-aliasing of VALUES that has column aliases produces wrong SQL in the JDBC adapter
---
.../apache/calcite/rel/rel2sql/SqlImplementor.java | 13 +++++++++++--
.../calcite/rel/rel2sql/RelToSqlConverterTest.java | 19 +++++++++++++++++++
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
index 31927a0..31857e4 100644
--- a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
+++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
@@ -1499,8 +1499,17 @@ public abstract class SqlImplementor {
* equivalent to "SELECT * FROM emp AS emp".) */
public SqlNode asFrom() {
if (neededAlias != null) {
- return SqlStdOperatorTable.AS.createCall(POS, node,
- new SqlIdentifier(neededAlias, POS));
+ if (node.getKind() == SqlKind.AS) {
+ // If we already have an AS node, we need to replace the alias
+ // This is especially relevant for the VALUES clause rendering
+ SqlCall sqlCall = (SqlCall) node;
+ SqlNode[] operands = sqlCall.getOperandList().toArray(SqlNode.EMPTY_ARRAY);
+ operands[1] = new SqlIdentifier(neededAlias, POS);
+ return SqlStdOperatorTable.AS.createCall(POS, operands);
+ } else {
+ return SqlStdOperatorTable.AS.createCall(POS, node,
+ new SqlIdentifier(neededAlias, POS));
+ }
}
return node;
}
diff --git a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 234323f..30f096b 100644
--- a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -4212,6 +4212,25 @@ public class RelToSqlConverterTest {
.ok(expectedPostgresql);
}
+ /** Test case fo
+ * <a href="https://issues.apache.org/jira/browse/CALCITE-3840">[CALCITE-3840]
+ * Re-aliasing of VALUES that has column aliases produces wrong SQL in the JDBC adapter</a>*/
+ @Test public void testValuesReAlias() {
+ final RelBuilder builder = relBuilder();
+ final RelNode root = builder
+ .values(new String[]{ "a", "b" }, 1, "x ", 2, "yy")
+ .values(new String[]{ "a", "b" }, 1, "x ", 2, "yy")
+ .join(JoinRelType.FULL)
+ .project(builder.field("a"))
+ .build();
+ final String expectedSql = "SELECT \"t\".\"a\"\n"
+ + "FROM (VALUES (1, 'x '),\n"
+ + " (2, 'yy')) AS \"t\" (\"a\", \"b\")\n"
+ + "FULL JOIN (VALUES (1, 'x '),\n"
+ + " (2, 'yy')) AS \"t0\" (\"a\", \"b\") ON TRUE";
+ assertThat(toSql(root), isLinux(expectedSql));
+ }
+
/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-2118">[CALCITE-2118]
* RelToSqlConverter should only generate "*" if field names match</a>. */