Skip to content

Commit 6a71e76

Browse files
committed
added tests for writer statement with lightweight parser
1 parent e6af963 commit 6a71e76

File tree

2 files changed

+90
-60
lines changed

2 files changed

+90
-60
lines changed

jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -860,66 +860,6 @@ void testClearParameters() throws Exception {
860860
}
861861
}
862862

863-
@DataProvider
864-
Object[][] testBatchInsertWithRowBinary_dp() {
865-
return new Object[][]{
866-
{"INSERT INTO \n `%s` \nVALUES (?, ?, abs(?), ?)", PreparedStatementImpl.class}, // only string possible (because of abs(?))
867-
{"INSERT INTO\n `%s` \nVALUES (?, ?, ?, ?)", WriterStatementImpl.class}, // row binary writer
868-
{" INSERT INTO %s (ts, v1, v2, v3) VALUES (?, ?, ?, ?)", WriterStatementImpl.class}, // only string supported now
869-
{"INSERT INTO %s SELECT ?, ?, ?, ?", PreparedStatementImpl.class}, // only string possible (because of SELECT)
870-
};
871-
}
872-
873-
@Test(dataProvider = "testBatchInsertWithRowBinary_dp")
874-
void testBatchInsertWithRowBinary(String sql, Class implClass) throws Exception {
875-
String table = "test_batch";
876-
long seed = System.currentTimeMillis();
877-
Random rnd = new Random(seed);
878-
System.out.println("testBatchInsert seed" + seed);
879-
Properties properties = new Properties();
880-
properties.put(DriverProperties.BETA_ROW_BINARY_WRITER.getKey(), "true");
881-
try (Connection conn = getJdbcConnection(properties)) {
882-
883-
try (Statement stmt = conn.createStatement()) {
884-
stmt.execute("CREATE TABLE IF NOT EXISTS " + table +
885-
" ( ts DateTime, v1 Int32, v2 Float32, v3 Int32) Engine MergeTree ORDER BY ()");
886-
}
887-
888-
final int nBatches = 10;
889-
try (PreparedStatement stmt = conn.prepareStatement(String.format(sql, table))) {
890-
Assert.assertEquals(stmt.getClass(), implClass);
891-
for (int bI = 0; bI < nBatches; bI++) {
892-
stmt.setTimestamp(1, Timestamp.valueOf(LocalDateTime.now()));
893-
stmt.setInt(2, rnd.nextInt());
894-
stmt.setFloat(3, rnd.nextFloat());
895-
stmt.setInt(4, rnd.nextInt());
896-
stmt.addBatch();
897-
}
898-
899-
int[] result = stmt.executeBatch();
900-
for (int r : result) {
901-
Assert.assertTrue(r == 1 || r == PreparedStatement.SUCCESS_NO_INFO);
902-
}
903-
}
904-
905-
try (Statement stmt = conn.createStatement();
906-
ResultSet rs = stmt.executeQuery("SELECT * FROM " + table);) {
907-
908-
int count = 0;
909-
while (rs.next()) {
910-
Timestamp ts = rs.getTimestamp(1);
911-
assertNotNull(ts);
912-
assertTrue(rs.getInt(2) != 0);
913-
assertTrue(rs.getFloat(3) != 0.0f);
914-
assertTrue(rs.getInt(4) != 0);
915-
count++;
916-
}
917-
assertEquals(count, nBatches);
918-
919-
stmt.execute("TRUNCATE " + table);
920-
}
921-
}
922-
}
923863

924864
@DataProvider
925865
Object[][] testBatchInsertTextStatement_dp() {

jdbc-v2/src/test/java/com/clickhouse/jdbc/WriterStatementImplTest.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
package com.clickhouse.jdbc;
22

3+
import com.clickhouse.jdbc.internal.SqlParserFacade;
34
import org.testng.Assert;
5+
import org.testng.annotations.DataProvider;
46
import org.testng.annotations.Test;
57

68
import java.sql.Connection;
79
import java.sql.JDBCType;
810
import java.sql.PreparedStatement;
11+
import java.sql.ResultSet;
912
import java.sql.SQLException;
13+
import java.sql.Statement;
14+
import java.sql.Timestamp;
15+
import java.time.LocalDateTime;
16+
import java.util.EnumSet;
1017
import java.util.Properties;
18+
import java.util.Random;
19+
import java.util.Set;
20+
21+
import static org.testng.Assert.assertEquals;
22+
import static org.testng.Assert.assertNotNull;
23+
import static org.testng.Assert.assertTrue;
1124

1225
@Test(groups = {"integration"})
1326
public class WriterStatementImplTest extends JdbcIntegrationTest {
@@ -28,4 +41,81 @@ public void testTargetTypeMethodThrowException() throws SQLException {
2841
Assert.expectThrows(SQLException.class, () -> stmt.setObject(1, "", JDBCType.DECIMAL, 3));
2942
}
3043
}
44+
45+
@DataProvider
46+
Object[][] testBatchInsertWithRowBinary_dp() {
47+
48+
Object[][] template = new Object[][]{
49+
{"INSERT INTO \n `%s` \nVALUES (?, ?, abs(?), ?)", PreparedStatementImpl.class, null}, // only string possible (because of abs(?))
50+
{"INSERT INTO\n `%s` \nVALUES (?, ?, ?, ?)", WriterStatementImpl.class, null}, // row binary writer
51+
{" INSERT INTO %s (ts, v1, v2, v3) VALUES (?, ?, ?, ?)", WriterStatementImpl.class, null}, // only string supported now
52+
{"INSERT INTO %s SELECT ?, ?, ?, ?", PreparedStatementImpl.class, null}, // only string possible (because of SELECT)
53+
};
54+
55+
Set<SqlParserFacade.SQLParser> parsers = EnumSet.of(SqlParserFacade.SQLParser.ANTLR4_LIGHT, SqlParserFacade.SQLParser.JAVACC);
56+
Object[][] dataset = new Object[template.length * parsers.size()][];
57+
58+
int i = 0;
59+
for (SqlParserFacade.SQLParser p : parsers) {
60+
for (Object[] t : template) {
61+
Object[] test = new Object[t.length];
62+
System.arraycopy(t, 0, test, 0, t.length);
63+
test[t.length - 1] = p;
64+
dataset[i++] = test;
65+
}
66+
}
67+
68+
return dataset;
69+
}
70+
71+
@Test(dataProvider = "testBatchInsertWithRowBinary_dp")
72+
void testBatchInsertWithRowBinary(String sql, Class implClass, SqlParserFacade.SQLParser parser) throws Exception {
73+
String table = "test_batch";
74+
long seed = System.currentTimeMillis();
75+
Random rnd = new Random(seed);
76+
System.out.println("testBatchInsert seed" + seed);
77+
Properties properties = new Properties();
78+
properties.put(DriverProperties.BETA_ROW_BINARY_WRITER.getKey(), "true");
79+
try (Connection conn = getJdbcConnection(properties)) {
80+
81+
try (Statement stmt = conn.createStatement()) {
82+
stmt.execute("CREATE TABLE IF NOT EXISTS " + table +
83+
" ( ts DateTime, v1 Int32, v2 Float32, v3 Int32) Engine MergeTree ORDER BY ()");
84+
}
85+
86+
final int nBatches = 10;
87+
try (PreparedStatement stmt = conn.prepareStatement(String.format(sql, table))) {
88+
Assert.assertEquals(stmt.getClass(), implClass);
89+
for (int bI = 0; bI < nBatches; bI++) {
90+
stmt.setTimestamp(1, Timestamp.valueOf(LocalDateTime.now()));
91+
stmt.setInt(2, rnd.nextInt());
92+
stmt.setFloat(3, rnd.nextFloat());
93+
stmt.setInt(4, rnd.nextInt());
94+
stmt.addBatch();
95+
}
96+
97+
int[] result = stmt.executeBatch();
98+
for (int r : result) {
99+
Assert.assertTrue(r == 1 || r == PreparedStatement.SUCCESS_NO_INFO);
100+
}
101+
}
102+
103+
try (Statement stmt = conn.createStatement();
104+
ResultSet rs = stmt.executeQuery("SELECT * FROM " + table);) {
105+
106+
int count = 0;
107+
while (rs.next()) {
108+
Timestamp ts = rs.getTimestamp(1);
109+
assertNotNull(ts);
110+
assertTrue(rs.getInt(2) != 0);
111+
assertTrue(rs.getFloat(3) != 0.0f);
112+
assertTrue(rs.getInt(4) != 0);
113+
count++;
114+
}
115+
assertEquals(count, nBatches);
116+
117+
stmt.execute("TRUNCATE " + table);
118+
}
119+
}
120+
}
31121
}

0 commit comments

Comments
 (0)