@@ -242,6 +242,11 @@ private String stripTerminalDelimiter(String sql, boolean noBackslashEscapes, bo
242242 char c = sql .charAt (i );
243243 if (Character .isWhitespace (c )) {
244244 i ++;
245+ } else if (jdbcType == TOdbcTableType .CLICKHOUSE
246+ && (sql .startsWith ("//" , i ) || sql .startsWith ("#!" , i ) || sql .startsWith ("# " , i ))) {
247+ // ClickHouse comments may contain unmatched quotes; their contents end only at LF.
248+ int newline = sql .indexOf ('\n' , i + 2 );
249+ i = newline < 0 ? sql .length () : newline + 1 ;
245250 // MySQL's second dash needs a following whitespace/control character; --1 is arithmetic.
246251 } else if ((sql .startsWith ("--" , i ) && (!mysql || (i + 2 < sql .length ()
247252 && (Character .isWhitespace (sql .charAt (i + 2 )) || Character .isISOControl (sql .charAt (i + 2 ))))))
@@ -252,13 +257,18 @@ private String stripTerminalDelimiter(String sql, boolean noBackslashEscapes, bo
252257 }
253258 } else if (sql .startsWith ("/*" , i )) {
254259 i = skipBlockComment (sql , i , jdbcType == TOdbcTableType .CLICKHOUSE );
260+ } else if (jdbcType == TOdbcTableType .CLICKHOUSE && c == '$' ) {
261+ i = skipClickHouseHeredoc (sql , i );
262+ delimiters .clear ();
255263 } else if (c == '\'' || c == '"' || c == '`' ) {
256264 char quote = c ;
257265 for (i ++; i < sql .length (); i ++) {
258266 // ANSI_QUOTES makes double quotes identifiers, where backslashes never escape the closing quote.
259267 // Both quoting modes belong to the remote session.
268+ // ClickHouse also permits backslash escapes inside backtick-quoted identifiers.
260269 if (sql .charAt (i ) == '\\' && !(mysql && noBackslashEscapes )
261- && !(mysql && ansiQuotes && quote == '"' ) && quote != '`'
270+ && !(mysql && ansiQuotes && quote == '"' )
271+ && (quote != '`' || jdbcType == TOdbcTableType .CLICKHOUSE )
262272 && jdbcType != TOdbcTableType .TRINO && jdbcType != TOdbcTableType .PRESTO ) {
263273 i ++;
264274 } else if (sql .charAt (i ) == quote ) {
@@ -292,6 +302,28 @@ private String stripTerminalDelimiter(String sql, boolean noBackslashEscapes, bo
292302 return result .toString ();
293303 }
294304
305+ private static int skipClickHouseHeredoc (String sql , int start ) {
306+ // A dollar sign inside a bare identifier is not the beginning of a heredoc.
307+ if (start > 0 && (isClickHouseWordCharacter (sql .charAt (start - 1 )) || sql .charAt (start - 1 ) == '$' )) {
308+ return start + 1 ;
309+ }
310+ int tagEnd = start + 1 ;
311+ while (tagEnd < sql .length () && isClickHouseWordCharacter (sql .charAt (tagEnd ))) {
312+ tagEnd ++;
313+ }
314+ if (tagEnd == sql .length () || sql .charAt (tagEnd ) != '$' ) {
315+ return start + 1 ;
316+ }
317+ // Heredocs contain raw text: embedded quotes, comments and delimiters must not affect scanning.
318+ String tag = sql .substring (start , tagEnd + 1 );
319+ int end = sql .indexOf (tag , tagEnd + 1 );
320+ return end < 0 ? start + 1 : end + tag .length ();
321+ }
322+
323+ private static boolean isClickHouseWordCharacter (char c ) {
324+ return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' ;
325+ }
326+
295327 private static int skipBlockComment (String sql , int start , boolean nested ) {
296328 // Trino, Presto and MySQL end at the first closing marker; ClickHouse permits nested comments.
297329 int depth = 1 ;
0 commit comments