11package com .clickhouse .jdbc ;
22
3+ import com .clickhouse .jdbc .internal .SqlParserFacade ;
34import org .testng .Assert ;
5+ import org .testng .annotations .DataProvider ;
46import org .testng .annotations .Test ;
57
68import java .sql .Connection ;
79import java .sql .JDBCType ;
810import java .sql .PreparedStatement ;
11+ import java .sql .ResultSet ;
912import java .sql .SQLException ;
13+ import java .sql .Statement ;
14+ import java .sql .Timestamp ;
15+ import java .time .LocalDateTime ;
16+ import java .util .EnumSet ;
1017import 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" })
1326public 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` \n VALUES (?, ?, abs(?), ?)" , PreparedStatementImpl .class , null }, // only string possible (because of abs(?))
50+ {"INSERT INTO\n `%s` \n VALUES (?, ?, ?, ?)" , 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