Skip to content

Commit

Permalink
[CALCITE-4724] In JDBC adapter for ClickHouse, implement Values by ge…
Browse files Browse the repository at this point in the history
…nerating SELECT without FROM (Liu Enze)

Fix by making SqlDialect.supportsAliasedValues return false
in the ClickHouse dialect, similar to MySQL.

Close apache#2480
  • Loading branch information
Enzo-Liu authored and julianhyde committed Aug 14, 2021
1 parent dff28d1 commit d64fd60
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public ClickHouseSqlDialect(Context context) {
return false;
}

@Override public boolean supportsAliasedValues() {
return false;
}

@Override public CalendarPolicy getCalendarPolicy() {
return CalendarPolicy.SHIFT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4817,6 +4817,10 @@ private void checkLiteral2(String expression, String expected) {
@Test void testValues() {
final String sql = "select \"a\"\n"
+ "from (values (1, 'x'), (2, 'yy')) as t(\"a\", \"b\")";
final String expectedClickHouse = "SELECT `a`\n"
+ "FROM (SELECT 1 AS `a`, 'x ' AS `b`\n"
+ "UNION ALL\n"
+ "SELECT 2 AS `a`, 'yy' AS `b`)"; // almost the same as MySQL
final String expectedHsqldb = "SELECT a\n"
+ "FROM (VALUES (1, 'x '),\n"
+ "(2, 'yy')) AS t (a, b)";
Expand Down Expand Up @@ -4844,6 +4848,7 @@ private void checkLiteral2(String expression, String expected) {
final String expectedSnowflake = expectedPostgresql;
final String expectedRedshift = expectedPostgresql;
sql(sql)
.withClickHouse().ok(expectedClickHouse)
.withBigQuery().ok(expectedBigQuery)
.withHive().ok(expectedHive)
.withHsqldb().ok(expectedHsqldb)
Expand All @@ -4869,24 +4874,33 @@ private void checkLiteral2(String expression, String expected) {
final String expectedPostgresql = "SELECT *\n"
+ "FROM (VALUES (NULL, NULL)) AS \"t\" (\"X\", \"Y\")\n"
+ "WHERE 1 = 0";
final String expectedClickHouse = expectedMysql;
sql(sql)
.optimize(rules, null)
.withClickHouse().ok(expectedClickHouse)
.withMysql().ok(expectedMysql)
.withOracle().ok(expectedOracle)
.withPostgresql().ok(expectedPostgresql);
}

/** Tests SELECT without FROM clause; effectively the same as a VALUES
* query. */
* query.
*
* <p>Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-4724">[CALCITE-4724]
* In JDBC adapter for ClickHouse, implement Values by generating SELECT
* without FROM</a>. */
@Test void testSelectWithoutFrom() {
final String query = "select 2 + 2";
final String expectedBigQuery = "SELECT 2 + 2";
final String expectedClickHouse = expectedBigQuery;
final String expectedHive = expectedBigQuery;
final String expectedMysql = "SELECT 2 + 2";
final String expectedMysql = expectedBigQuery;
final String expectedPostgresql = "SELECT 2 + 2\n"
+ "FROM (VALUES (0)) AS \"t\" (\"ZERO\")";
sql(query)
.withBigQuery().ok(expectedBigQuery)
.withClickHouse().ok(expectedClickHouse)
.withHive().ok(expectedHive)
.withMysql().ok(expectedMysql)
.withPostgresql().ok(expectedPostgresql);
Expand All @@ -4895,12 +4909,14 @@ private void checkLiteral2(String expression, String expected) {
@Test void testSelectOne() {
final String query = "select 1";
final String expectedBigQuery = "SELECT 1";
final String expectedClickHouse = expectedBigQuery;
final String expectedHive = expectedBigQuery;
final String expectedMysql = expectedBigQuery;
final String expectedPostgresql = "SELECT *\n"
+ "FROM (VALUES (1)) AS \"t\" (\"EXPR$0\")";
sql(query)
.withBigQuery().ok(expectedBigQuery)
.withClickHouse().ok(expectedClickHouse)
.withHive().ok(expectedHive)
.withMysql().ok(expectedMysql)
.withPostgresql().ok(expectedPostgresql);
Expand Down

0 comments on commit d64fd60

Please sign in to comment.