Skip to content

Commit 873b154

Browse files
committed
[fix](jdbc) Preserve ClickHouse literals and isolate Oracle test drivers
1 parent 88625ed commit 873b154

3 files changed

Lines changed: 94 additions & 3 deletions

File tree

‎fe/be-java-extensions/jdbc-scanner/src/test/java/org/apache/doris/jdbc/JdbcZonedTimestampIntegrationTest.java‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,33 @@ private void verifyTimestampWriteRoundTrip(String prefix, String driver,
373373
});
374374
}
375375

376+
@Test
377+
void testOracleDriverIsIsolatedFromTestClasspath() throws Exception {
378+
URL jar = oracle.jdbc.OracleDriver.class.getProtectionDomain().getCodeSource().getLocation();
379+
try (URLClassLoader loader = createDriverClassLoader(jar, "oracle.jdbc.OracleDriver")) {
380+
Class<?> selected = loader.loadClass("oracle.jdbc.OracleDriver");
381+
Assertions.assertSame(loader, selected.getClassLoader());
382+
Assertions.assertNotSame(oracle.jdbc.OracleDriver.class, selected);
383+
Assertions.assertEquals(jar, selected.getProtectionDomain().getCodeSource().getLocation());
384+
Assertions.assertTrue(Driver.class.isAssignableFrom(selected));
385+
}
386+
}
387+
388+
private URLClassLoader createDriverClassLoader(URL jar, String driverClass) {
389+
// The Oracle mock dependency must not shadow the JAR selected for integration testing.
390+
ClassLoader parent = driverClass.startsWith("oracle.")
391+
? Driver.class.getClassLoader() : getClass().getClassLoader();
392+
return new URLClassLoader(new URL[] {jar}, parent);
393+
}
394+
376395
private void withDriver(String prefix, String driverClass, Check check) throws Exception {
377396
TimeZone original = TimeZone.getDefault();
378397
URL jar = new File(System.getProperty(prefix + ".integration.driverJar")).toURI().toURL();
379-
try (URLClassLoader loader = new URLClassLoader(new URL[] {jar}, getClass().getClassLoader())) {
380-
Driver driver = (Driver) loader.loadClass(driverClass).getDeclaredConstructor().newInstance();
398+
try (URLClassLoader loader = createDriverClassLoader(jar, driverClass)) {
399+
Class<?> selected = loader.loadClass(driverClass);
400+
Assertions.assertEquals(jar, selected.getProtectionDomain().getCodeSource().getLocation(),
401+
"The integration test must load the configured driver JAR");
402+
Driver driver = (Driver) selected.getDeclaredConstructor().newInstance();
381403
for (String zone : new String[] {"UTC", "Asia/Shanghai", "America/New_York"}) {
382404
TimeZone.setDefault(TimeZone.getTimeZone(zone));
383405
Properties properties = new Properties();

‎fe/fe-core/src/main/java/org/apache/doris/datasource/jdbc/source/JdbcScanNode.java‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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;

‎fe/fe-core/src/test/java/org/apache/doris/datasource/jdbc/source/JdbcQueryWrappingTest.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,43 @@ void mysqlProjectsOnlyInstantColumnsAsText() throws Exception {
176176
Assertions.assertEquals(Arrays.asList("CAST(`ts` AS CHAR) AS `ts`", "`dt`"), columns.get(node));
177177
}
178178

179+
@Test
180+
void clickHouseEscapedBackticksPreserveIdentifierContents() throws Exception {
181+
assertClickHouseWrapped("SELECT ts, 1 AS `a\\`b` FROM t");
182+
assertClickHouseWrapped("SELECT ts, 1 AS `a\\`;b` FROM t");
183+
}
184+
185+
@Test
186+
void clickHouseLineCommentsDoNotChangeQuoteState() throws Exception {
187+
for (String marker : Arrays.asList("//", "///", "#!", "# ")) {
188+
for (String newline : Arrays.asList("\n", "\r\n")) {
189+
assertClickHouseWrapped("SELECT ts FROM t " + marker + " 'note ` ;" + newline);
190+
}
191+
}
192+
}
193+
194+
@Test
195+
void clickHouseHeredocsPreserveRawContents() throws Exception {
196+
for (String literal : Arrays.asList("$h$foo'bar$h$", "$$'\\\"`; /* --$$",
197+
"$tag_1$'text; $other$tag_1$", "$h$$h$")) {
198+
assertClickHouseWrapped("SELECT ts FROM t WHERE length(" + literal + ") > 0");
199+
}
200+
// An interior statement delimiter must remain visible to the remote parser.
201+
assertClickHouseWrapped("SELECT ts, $h$'raw$h$ FROM t; SELECT ts FROM t");
202+
}
203+
204+
private void assertClickHouseWrapped(String query) throws Exception {
205+
JdbcScanNode node = org.mockito.Mockito.mock(JdbcScanNode.class, org.mockito.Mockito.CALLS_REAL_METHODS);
206+
set(node, "jdbcType", TOdbcTableType.CLICKHOUSE);
207+
set(node, "projectsTimestamps", true);
208+
set(node, "columns", Arrays.asList("toUnixTimestamp64Micro(ts) AS ts"));
209+
set(node, "query", query + ";");
210+
Method wrap = JdbcScanNode.class.getDeclaredMethod("getTvfQuery");
211+
wrap.setAccessible(true);
212+
Assertions.assertEquals("SELECT toUnixTimestamp64Micro(ts) AS ts FROM (" + query
213+
+ "\n) doris_jdbc_source", wrap.invoke(node));
214+
}
215+
179216
private static void set(Object target, String name, Object value) throws Exception {
180217
Field field = JdbcScanNode.class.getDeclaredField(name);
181218
field.setAccessible(true);

0 commit comments

Comments
 (0)