Skip to content

Commit d9a0f5d

Browse files
authored
Merge pull request #387 from databendlabs/fix/regex-parse-err
fix: extractColumnTypes parse sql err
2 parents 02590b3 + ae8ef90 commit d9a0f5d

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

databend-jdbc/src/main/java/com/databend/jdbc/StatementUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public Optional<Pair<String, String>> extractParamFromSetStatement(@NonNull Stri
6868
*/
6969
public static Map<Integer, String> extractColumnTypes(String sql) {
7070
Map<Integer, String> columnTypes = new LinkedHashMap<>();
71+
if (isQuery(sql)) {
72+
return columnTypes;
73+
}
7174
Pattern pattern = Pattern.compile("\\((.*?)\\)");
7275
Matcher matcher = pattern.matcher(sql);
7376
if (matcher.find()) {

databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,48 @@ public void testBatchAndNoBatch() throws SQLException {
620620
}
621621

622622

623+
@Test(groups = {"IT"})
624+
public void testSelectCountStarWithPreparedStatement() throws SQLException {
625+
if (Compatibility.skipDriverBugLowerThen("0.4.5")) {
626+
return;
627+
}
628+
try (Connection conn = getConn();
629+
Statement s = conn.createStatement()) {
630+
s.execute("create or replace table t1(a int, b string)");
631+
s.execute("insert into t1 values(1, 'a')");
632+
s.execute("insert into t1 values(2, 'b')");
633+
634+
// COUNT(*) should not be parsed as a parameter
635+
String countSql = "SELECT COUNT(*) FROM t1";
636+
try (PreparedStatement ps = conn.prepareStatement(countSql)) {
637+
Assert.assertEquals(ps.getParameterMetaData().getParameterCount(), 0,
638+
"COUNT(*) should have 0 parameters");
639+
ResultSet rs = ps.executeQuery();
640+
Assert.assertTrue(rs.next());
641+
Assert.assertEquals(rs.getInt(1), 2);
642+
}
643+
644+
// SUM with column should not be parsed as a parameter
645+
String sumSql = "SELECT SUM(a) FROM t1";
646+
try (PreparedStatement ps = conn.prepareStatement(sumSql)) {
647+
Assert.assertEquals(ps.getParameterMetaData().getParameterCount(), 0,
648+
"SUM(a) should have 0 parameters");
649+
ResultSet rs = ps.executeQuery();
650+
Assert.assertTrue(rs.next());
651+
Assert.assertEquals(rs.getInt(1), 3);
652+
}
653+
654+
// SELECT with actual parameter marker should still work
655+
String selectWithParam = "SELECT COUNT(*) FROM t1 WHERE a = ?";
656+
try (PreparedStatement ps = conn.prepareStatement(selectWithParam)) {
657+
ps.setInt(1, 1);
658+
ResultSet rs = ps.executeQuery();
659+
Assert.assertTrue(rs.next());
660+
Assert.assertEquals(rs.getInt(1), 1);
661+
}
662+
}
663+
}
664+
623665
@Test(groups = {"IT"})
624666
public void testSelectWithPreparedStatement()
625667
throws SQLException {

databend-jdbc/src/test/java/com/databend/jdbc/TestStatementUtil.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,25 @@ public void testExtractColumnTypes() {
1919
assertEquals("Int8", columnTypes.get(1));
2020
assertEquals("VARIANT", columnTypes.get(2));
2121
}
22+
23+
@Test(groups = {"UNIT"})
24+
public void testExtractColumnTypesSelectCountStar() {
25+
String sql = "SELECT COUNT(*) FROM 'szps_dwd'.'DWD_WSC_WS_GYYX_LJSLLJ_1H'";
26+
Map<Integer, String> columnTypes = StatementUtil.extractColumnTypes(sql);
27+
assertEquals(0, columnTypes.size());
28+
}
29+
30+
@Test(groups = {"UNIT"})
31+
public void testExtractColumnTypesSelectWithFunctions() {
32+
String sql = "SELECT SUM(a), AVG(b) FROM test_table WHERE id = ?";
33+
Map<Integer, String> columnTypes = StatementUtil.extractColumnTypes(sql);
34+
assertEquals(0, columnTypes.size());
35+
}
36+
37+
@Test(groups = {"UNIT"})
38+
public void testExtractColumnTypesShowTables() {
39+
String sql = "SHOW TABLES";
40+
Map<Integer, String> columnTypes = StatementUtil.extractColumnTypes(sql);
41+
assertEquals(0, columnTypes.size());
42+
}
2243
}

0 commit comments

Comments
 (0)