forked from pgjdbc/pgjdbc
/
BatchedInsertReWriteEnabledTest.java
448 lines (414 loc) · 13.4 KB
/
BatchedInsertReWriteEnabledTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* Copyright (c) 2003, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.test.jdbc2;
import org.postgresql.PGProperty;
import org.postgresql.test.TestUtil;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.sql.BatchUpdateException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;
@RunWith(Parameterized.class)
public class BatchedInsertReWriteEnabledTest extends BaseTest4 {
private final AutoCommit autoCommit;
public BatchedInsertReWriteEnabledTest(AutoCommit autoCommit,
BinaryMode binaryMode) {
this.autoCommit = autoCommit;
setBinaryMode(binaryMode);
}
@Parameterized.Parameters(name = "{index}: autoCommit={0}, binary={1}")
public static Iterable<Object[]> data() {
Collection<Object[]> ids = new ArrayList<Object[]>();
for (AutoCommit autoCommit : AutoCommit.values()) {
for (BinaryMode binaryMode : BinaryMode.values()) {
ids.add(new Object[]{autoCommit, binaryMode});
}
}
return ids;
}
/* Set up the fixture for this testcase: a connection to a database with
a table for this test. */
public void setUp() throws Exception {
super.setUp();
TestUtil.createTable(con, "testbatch", "pk INTEGER, col1 VARCHAR, col2 INTEGER");
con.setAutoCommit(autoCommit == AutoCommit.YES);
}
// Tear down the fixture for this test case.
public void tearDown() throws SQLException {
TestUtil.dropTable(con, "testbatch");
super.tearDown();
}
@Override
protected void updateProperties(Properties props) {
super.updateProperties(props);
PGProperty.REWRITE_BATCHED_INSERTS.set(props, true);
}
/**
* Check batching using two individual statements that are both the same type.
* Test to check the re-write optimisation behaviour.
*/
@Test
public void testBatchWithReWrittenRepeatedInsertStatementOptimizationEnabled()
throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("INSERT INTO testbatch VALUES (?,?)");
pstmt.setInt(1, 1);
pstmt.setInt(2, 2);
pstmt.addBatch();
pstmt.setInt(1, 3);
pstmt.setInt(2, 4);
pstmt.addBatch();
pstmt.setInt(1, 5);
pstmt.setInt(2, 6);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(3, pstmt.executeBatch());
TestUtil.assertNumberOfRows(con, "testbatch", 3, "3 rows inserted");
/*
* Now check the ps can be reused. The batched statement should be reset
* and have no knowledge of prior re-written batch. This test uses a
* different batch size. To test if the driver detects the different size
* and prepares the statement on with the backend. If not then an
* exception will be thrown for an unknown prepared statement.
*/
pstmt.setInt(1, 1);
pstmt.setInt(2, 2);
pstmt.addBatch();
pstmt.setInt(1, 3);
pstmt.setInt(2, 4);
pstmt.addBatch();
pstmt.setInt(1, 5);
pstmt.setInt(2, 6);
pstmt.addBatch();
pstmt.setInt(1, 7);
pstmt.setInt(2, 8);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(4, pstmt.executeBatch());
TestUtil.assertNumberOfRows(con, "testbatch", 7, "3+4 rows inserted");
pstmt.setInt(1, 1);
pstmt.setInt(2, 2);
pstmt.addBatch();
pstmt.setInt(1, 3);
pstmt.setInt(2, 4);
pstmt.addBatch();
pstmt.setInt(1, 5);
pstmt.setInt(2, 6);
pstmt.addBatch();
pstmt.setInt(1, 7);
pstmt.setInt(2, 8);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(4, pstmt.executeBatch());
TestUtil.assertNumberOfRows(con, "testbatch", 11, "3+4+4 rows inserted");
} finally {
TestUtil.closeQuietly(pstmt);
}
}
/**
* Check batching using a statement with fixed parameter.
*/
@Test
public void testBatchWithReWrittenBatchStatementWithFixedParameter()
throws SQLException {
String[] odd = new String[]{
"INSERT INTO testbatch VALUES (?, '1, (, $1234, a''n?d )' /*xxxx)*/, ?) -- xxx",
// "INSERT /*xxx*/INTO testbatch VALUES (?, '1, (, $1234, a''n?d )' /*xxxx)*/, ?) -- xxx",
};
for (String s : odd) {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(s);
pstmt.setInt(1, 1);
pstmt.setInt(2, 2);
pstmt.addBatch();
pstmt.setInt(1, 3);
pstmt.setInt(2, 4);
pstmt.addBatch();
pstmt.setInt(1, 5);
pstmt.setInt(2, 6);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(3, pstmt.executeBatch());
TestUtil.assertNumberOfRows(con, "testbatch", 3, "3 rows inserted");
} finally {
TestUtil.closeQuietly(pstmt);
}
}
}
/**
* Check batching using a statement with fixed parameters only.
*/
@Test
public void testBatchWithReWrittenBatchStatementWithFixedParametersOnly()
throws SQLException {
String[] odd = new String[]{
"INSERT INTO testbatch VALUES (9, '1, (, $1234, a''n?d )' /*xxxx)*/, 7) -- xxx",
// "INSERT /*xxx*/INTO testbatch VALUES (?, '1, (, $1234, a''n?d )' /*xxxx)*/, ?) -- xxx",
};
for (String s : odd) {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(s);
pstmt.addBatch();
pstmt.addBatch();
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(3, pstmt.executeBatch());
TestUtil.assertNumberOfRows(con, "testbatch", 3, "3 rows inserted");
} finally {
TestUtil.closeQuietly(pstmt);
}
}
}
/**
* Test to make sure a statement with a semicolon is not broken.
*/
private void simpleRewriteBatch(String values, String suffix)
throws SQLException {
PreparedStatement pstmt = null;
try {
PreparedStatement clean = con.prepareStatement("truncate table testbatch");
clean.execute();
clean.close();
pstmt = con.prepareStatement("INSERT INTO testbatch " + values + "(?,?,?)" + suffix);
pstmt.setInt(1, 1);
pstmt.setString(2, "a");
pstmt.setInt(3, 2);
pstmt.addBatch();
pstmt.setInt(1, 3);
pstmt.setString(2, "b");
pstmt.setInt(3, 4);
pstmt.addBatch();
pstmt.setInt(1, 5);
pstmt.setString(2, "c");
pstmt.setInt(3, 6);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(3, pstmt.executeBatch());
TestUtil.assertNumberOfRows(con, "testbatch", 3, "3 rows inserted");
} finally {
TestUtil.closeQuietly(pstmt);
}
}
/**
* Test to make sure a statement with a semicolon is not broken.
*/
@Test
public void testBatchWithReWrittenBatchStatementWithSemiColon()
throws SQLException {
simpleRewriteBatch("values", ";");
}
/**
* Test to make sure a statement with a semicolon is not broken.
*/
@Test
public void testBatchWithReWrittenSpaceAfterValues()
throws SQLException {
simpleRewriteBatch("values ", "");
simpleRewriteBatch("values ", "");
simpleRewriteBatch("values\t", "");
}
/**
* Test VALUES word with mixed case.
*/
@Test
public void testBatchWithReWrittenMixedCaseValues()
throws SQLException {
simpleRewriteBatch("vAlues", "");
simpleRewriteBatch("vaLUES", "");
simpleRewriteBatch("VALUES", "");
}
/**
* Test to make sure a statement with a semicolon is not broken.
*/
@Test
public void testBindsInNestedParens()
throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("INSERT INTO testbatch VALUES ((?),((?)),?);");
pstmt.setInt(1, 1);
pstmt.setString(2, "a");
pstmt.setInt(3, 2);
pstmt.addBatch();
pstmt.setInt(1, 3);
pstmt.setString(2, "b");
pstmt.setInt(3, 4);
pstmt.addBatch();
pstmt.setInt(1, 5);
pstmt.setString(2, "c");
pstmt.setInt(3, 6);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(3, pstmt.executeBatch());
} finally {
TestUtil.closeQuietly(pstmt);
}
}
/**
* Test to make sure a statement with a semicolon is not broken.
*/
@Test
public void testMultiValues1bind()
throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("INSERT INTO testbatch (pk) VALUES (?), (?)");
pstmt.setInt(1, 100);
pstmt.setInt(2, 200);
pstmt.addBatch();
pstmt.setInt(1, 300);
pstmt.setInt(2, 400);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(2, pstmt.executeBatch());
} finally {
TestUtil.closeQuietly(pstmt);
}
}
/**
* Test case to check the outcome for a batch with a single row/batch is
* consistent across calls to executeBatch. Especially after a batch
* has been re-written.
*/
@Test
public void testConsistentOutcome() throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("INSERT INTO testbatch VALUES (?,?,?);");
pstmt.setInt(1, 1);
pstmt.setString(2, "a");
pstmt.setInt(3, 2);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(1, pstmt.executeBatch());
pstmt.setInt(1, 1);
pstmt.setString(2, "b");
pstmt.setInt(3, 2);
pstmt.addBatch();
pstmt.setInt(1, 3);
pstmt.setString(2, "c");
pstmt.setInt(3, 4);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(2, pstmt.executeBatch());
pstmt.setInt(1, 1);
pstmt.setString(2, "d");
pstmt.setInt(3, 2);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(1, pstmt.executeBatch());
} finally {
TestUtil.closeQuietly(pstmt);
}
}
/**
* Test to check statement with named columns still work as expected.
*/
@Test
public void testINSERTwithNamedColumnsNotBroken() throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con
.prepareStatement("INSERT INTO testbatch (pk, col1, col2) VALUES (?,?,?);");
pstmt.setInt(1, 1);
pstmt.setString(2, "a");
pstmt.setInt(3, 2);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(1, pstmt.executeBatch());
} finally {
TestUtil.closeQuietly(pstmt);
}
}
@Test
public void testMixedCaseInSeRtStatement() throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement("InSeRt INTO testbatch VALUES (?,?,?);");
pstmt.setInt(1, 1);
pstmt.setString(2, "a");
pstmt.setInt(3, 2);
pstmt.addBatch();
pstmt.setInt(1, 3);
pstmt.setString(2, "b");
pstmt.setInt(3, 4);
pstmt.addBatch();
BatchExecuteTest.assertSimpleInsertBatch(2, pstmt.executeBatch());
} finally {
TestUtil.closeQuietly(pstmt);
}
}
@Test
public void testReWriteDisabledForPlainBatch() throws Exception {
Statement stmt = null;
try {
con = TestUtil.openDB(new Properties());
stmt = con.createStatement();
stmt.addBatch("INSERT INTO testbatch VALUES (100,'a',200);");
stmt.addBatch("INSERT INTO testbatch VALUES (300,'b',400);");
Assert.assertEquals(
"Expected outcome not returned by batch execution. The driver"
+ " allowed re-write in combination with plain statements.",
Arrays.toString(new int[]{1, 1}), Arrays.toString(stmt.executeBatch()));
} finally {
TestUtil.closeQuietly(stmt);
}
}
@Test
public void test32767Binds() throws Exception {
testNBinds(32767);
}
@Test
public void test32768Binds() throws Exception {
testNBinds(32768);
}
@Test
public void test65535Binds() throws Exception {
testNBinds(65535);
}
public void testNBinds(int nBinds) throws Exception {
PreparedStatement pstmt = null;
try {
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO testbatch(pk) VALUES (coalesce(?");
for (int i = 0; i < nBinds - 1 /* note one ? above */; i++) {
sb.append(",?");
}
sb.append("))");
pstmt = con.prepareStatement(sb.toString());
for (int k = 0; k < 2; k++) {
for (int i = 1; i <= nBinds; i++) {
pstmt.setInt(i, i + k * nBinds);
}
pstmt.addBatch();
}
if (nBinds * 2 <= 65535) {
Assert.assertEquals(
"Insert with " + nBinds + " binds should be rewritten into multi-value insert"
+ ", so expecting Statement.SUCCESS_NO_INFO == -2",
Arrays.toString(new int[]{Statement.SUCCESS_NO_INFO, Statement.SUCCESS_NO_INFO}),
Arrays.toString(pstmt.executeBatch()));
} else {
Assert.assertEquals(
"Insert with " + nBinds + " binds can't be rewritten into multi-value insert"
+ " since write format allows 65535 binds maximum"
+ ", so expecting batch to be executed as individual statements",
Arrays.toString(new int[]{1, 1}),
Arrays.toString(pstmt.executeBatch()));
}
} catch (BatchUpdateException be) {
SQLException e = be;
while (true) {
e.printStackTrace();
SQLException next = e.getNextException();
if (next == null) {
break;
}
e = next;
}
throw e;
} finally {
TestUtil.closeQuietly(pstmt);
}
}
}