Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions history/latest/3391.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- **[jdbc-v2]** The JavaCC SQL parser now accepts a heredoc (`$$...$$`, `$tag$...$tag$`) as the cluster name in
`ON CLUSTER` and as the string of `INTERVAL '...'`. Before, such a statement could not be parsed: `ALTER TABLE ... ON
CLUSTER $$c$$` and `UPDATE ... ON CLUSTER $$c$$` were reported as statements with a result set
(`Statement.execute()` returned `true`), and a batch of `INSERT ... VALUES (?, now() - INTERVAL $$1 day$$)` was sent
as one request per row. (https://github.com/ClickHouse/clickhouse-java/issues/3391)
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ public static String unescape(String str) {
return sb.toString();
}

/**
* Get the body of a heredoc string literal, {@code $$body$$} or {@code $tag$body$tag$}. A heredoc has no escape
* sequences, so the body is returned as is.
*
* @param str heredoc string literal
* @return body of the heredoc, or the given string when it is not a heredoc
*/
static String unwrapHeredoc(String str) {
if (str == null || str.length() < 4 || str.charAt(0) != '$' || str.charAt(str.length() - 1) != '$') {
return str;
}

int bodyStart = str.indexOf('$', 1) + 1;
int bodyEnd = str.lastIndexOf('$', str.length() - 2);
return bodyEnd < bodyStart ? str : str.substring(bodyStart, bodyEnd);
}

private ClickHouseSqlUtils() {
}
}
9 changes: 6 additions & 3 deletions jdbc-v2/src/main/javacc/ClickHouseSqlParser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,11 @@ void alterStmt(): {} {
}

void clusterClause(): { Token t; } {
<ON> <CLUSTER> (LOOKAHEAD(2) t = anyIdentifier() | t = <STRING_LITERAL>)
{ token_source.cluster = ClickHouseSqlUtils.unescape(t.image); }
<ON> <CLUSTER> (LOOKAHEAD(2) t = anyIdentifier() | t = <STRING_LITERAL> | t = <HEREDOC_LITERAL>)
{
token_source.cluster = t.kind == HEREDOC_LITERAL
? ClickHouseSqlUtils.unwrapHeredoc(t.image) : ClickHouseSqlUtils.unescape(t.image);
}
}

// https://clickhouse.tech/docs/en/sql-reference/statements/attach/
Expand Down Expand Up @@ -804,7 +807,7 @@ void nestedExpr(): {} {
LOOKAHEAD(2) (<DASH> | <NOT>) nestedExpr()
| LOOKAHEAD(2) <CASE> (LOOKAHEAD({ getToken(1).kind != WHEN }) nestedExpr())?
(<WHEN> nestedExpr() <THEN> nestedExpr())+ (<ELSE> nestedExpr())? <END>
| LOOKAHEAD(2) <INTERVAL> (LOOKAHEAD(2) <STRING_LITERAL> | nestedExpr() interval())
| LOOKAHEAD(2) <INTERVAL> (LOOKAHEAD(2) (<STRING_LITERAL> | <HEREDOC_LITERAL>) | nestedExpr() interval())
| columnExpr()
(LOOKAHEAD(2) <LBRACKET> anyExprList() <RBRACKET>)*
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ public static Object[][] heredocStatementsDP() {
"($$-- still data\nmore data$$, 1)"},
{"INSERT INTO t VALUES ($$/* still data\n*/$$, 1)", true, "t", "($$/* still data\n*/$$, 1)"},
{"SELECT $$line1\nline2$$ AS x FROM t", false, "t", null},
// A heredoc is an interval string, like a quoted string literal
{"SELECT now() + INTERVAL $$1 day$$ AS d FROM t", false, "t", null},
{"SELECT now() + INTERVAL $tag$1 day$tag$ AS d FROM t", false, "t", null},
{"INSERT INTO t VALUES (now() - INTERVAL $$1 day$$, 1)", true, "t", "(now() - INTERVAL $$1 day$$, 1)"},
};
}

Expand Down Expand Up @@ -641,6 +645,32 @@ public static Object[][] javaCcHeredocStatementsDP() {
};
}

@Test(dataProvider = "javaCcHeredocClusterNameDP")
public void testHeredocClusterNameJavaCcOnly(String sql, String expectedTable) {
// The ANTLR4 grammars do not accept a heredoc cluster name yet, so the expectations only hold
// for the JavaCC backend.
if (!javaCcBackend) {
return;
}
ParsedPreparedStatement stmt = parser.parsePreparedStatement(sql);
Assert.assertFalse(stmt.isHasErrors(), "Query should parse without errors: " + sql);
Assert.assertFalse(stmt.isInsert(), "Should not be an INSERT: " + sql);
Assert.assertFalse(stmt.isHasResultSet(), "Should have no result set: " + sql);
Assert.assertEquals(stmt.getTable(), expectedTable, "Table name mismatch for: " + sql);
}

@DataProvider
public static Object[][] javaCcHeredocClusterNameDP() {
return new Object[][] {
// A heredoc is a cluster name, like a quoted string literal
{"ALTER TABLE t ON CLUSTER $$c$$ DELETE WHERE 1", "t"},
{"ALTER TABLE t ON CLUSTER $tag$c$tag$ ADD COLUMN b Int", "t"},
{"UPDATE t ON CLUSTER $$c$$ SET a = 1 WHERE 1", "t"},
// Contrast: a quoted string literal keeps its existing handling
{"ALTER TABLE t ON CLUSTER 'c' DELETE WHERE 1", "t"},
};
}

@DataProvider
public static Object[][] antlr4HeredocStatementsDP() {
return new Object[][] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
package com.clickhouse.jdbc.internal;


import com.clickhouse.jdbc.internal.parser.javacc.ClickHouseSqlParser;
import com.clickhouse.jdbc.internal.parser.javacc.ClickHouseSqlStatement;
import com.clickhouse.jdbc.internal.parser.javacc.JdbcParseHandler;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Ignore;
import org.testng.annotations.Test;

//@Ignore
public class JavaCCParserTest extends BaseSqlParserFacadeTest {
public JavaCCParserTest() throws Exception {
super(SqlParserFacade.SQLParser.JAVACC.name());
}

@Test(dataProvider = "clusterNamesDP")
public void testClusterName(String sql, String expectedCluster) {
ClickHouseSqlStatement stmt = ClickHouseSqlParser.parse(sql, JdbcParseHandler.getInstance())[0];
Assert.assertEquals(stmt.getCluster(), expectedCluster, "Cluster mismatch for: " + sql);
}

@DataProvider
public static Object[][] clusterNamesDP() {
return new Object[][] {
{"ALTER TABLE t ON CLUSTER c DELETE WHERE 1", "c"},
{"ALTER TABLE t ON CLUSTER 'c''1' DELETE WHERE 1", "c'1"},
// A heredoc body has no escape sequences
{"ALTER TABLE t ON CLUSTER $$c''1$$ DELETE WHERE 1", "c''1"},
{"ALTER TABLE t ON CLUSTER $tag$c$tag$ DELETE WHERE 1", "c"},
};
}
}
Loading