File: BigDataTest.java

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (537 lines) | stat: -rw-r--r-- 18,964 bytes parent folder | download | duplicates (4)
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/**
 * Derby - Class org.apache.derbyTesting.functionTests.tests.lang.BigDataTest
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file to you under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package org.apache.derbyTesting.functionTests.tests.lang;

import java.sql.Clob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.Formatters;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;

/**
 * A test case for big.sql.
 */
public class BigDataTest extends BaseJDBCTestCase {
    private static final String BIG_TABLE_NAME = "big";

    /**
     * Constructor.
     * 
     * @param name
     * @throws SQLException
     */
    public BigDataTest(String name) throws SQLException {
        super(name);
    }

    /**
     * Get a String to select all records from the table defined.
     * 
     * @param tableName
     *            The table to fetch records from.
     * @return "select * from " + tableName.
     */
    public static String getSelectSql(String tableName) {
        return "select * from " + tableName;
    }

    /**
     * Create table by the name defined, and each column of the table has the type of
     * varchar or clob, which is determined by the element in useClob. At the same time,
     * each column is as big as identified by lengths. i.e. to call createTable(conn,
     * "big", {1000, 1000, }, {true, false,}) equals calling the sql sentence "create
     * table big(c1 clob(1000), c2 varchar(1000));"
     * 
     * @param tableName
     * @param lengths
     * @param useClob
     *            for element of useClob, if true, use clob as an element for a column;
     *            false, use varchar as an element for a column.
     * @throws SQLException
     */
    private void createTable(String tableName, int[] lengths, boolean[] useClob)
            throws SQLException {
        StringBuffer sqlSb = new StringBuffer();
        sqlSb.append("create table ");
        sqlSb.append(tableName);
        sqlSb.append(" (");
        for (int i = 0; i < lengths.length - 1; i++) {
            sqlSb.append("c" + (i + 1) + (useClob[i] ? " clob(" : " varchar(")
                    + lengths[i] + "),");
        }
        sqlSb.append("c" + lengths.length
                + (useClob[lengths.length - 1] ? " clob(" : " varchar(")
                + lengths[lengths.length - 1] + ")");
        sqlSb.append(")");
        String sql = sqlSb.toString();

        createTable(sql);
    }

    /**
     * Create a new table with defined sql sentence.
     * 
     * @param sql
     *            a sql sentence a create a table, which should use BIG_TABLE_NAME as new
     *            table's name.
     * @throws SQLException
     */
    private void createTable(String sql) throws SQLException {
        Statement ps = createStatement();
        ps.executeUpdate(sql);
        ps.close();
    }

    /**
     * Generate String array according to String array and int array defined. i.e. calling
     * getStringArray({"a", "b",}, {3, 4}) returns {"aaa", "bbbb",}.
     * 
     * @param sa
     *            the sort string array to use.
     * @param timesArray
     *            stores repeated times of String constructed by elements in sa.
     * @return A String array, whose elements is constructed by elements in sa, and each
     *         element repeated times defined by ia.
     */
    private String[] getStringArray(String[] sa, int[] timesArray) {
        String[] result = new String[sa.length];
        for (int i = 0; i < sa.length; i++) {
            result[i] = new String(Formatters.repeatChar(sa[i], timesArray[i]));
        }

        return result;
    }

    /**
     * Generate String array with two dimensions according to String array and int array
     * defined. i.e. calling getStringArray({"a", "b",}, {3, 4}) returns {"aaa", "bbbb",}.
     * 
     * @param sa
     *            the sort string array to use.
     * @param timesArray
     *            stores repeated times of String constructed by elements in sa.
     * @return A String array with two dimensions, whose elements is constructed by
     *         elements in sa, and each element repeated times defined by ia, for each
     *         row, it has only one column.
     */
    private String[][] getRowsWithOnlyOneColumn(String[] sa, int[] timesArray) {
        String[][] result = new String[sa.length][1];
        for (int i = 0; i < sa.length; i++) {
            result[i][0] = new String(Formatters.repeatChar(sa[i], timesArray[i]));
        }

        return result;
    }

    /**
     * Insert one row into a table named by tableName, with defined table, and String
     * array and int array to construct params.
     * 
     * @param tableName
     *            can not be null.
     * @param sa
     *            the string array to use.
     * @param timesArray
     *            stores repeated times of String constructed by elements in sa.
     * @throws SQLException
     */
    private void insertOneRow(String tableName, String[] sa, int[] timesArray)
            throws SQLException {
        String[] params = getStringArray(sa, timesArray);
        insertOneRow(tableName, params);
    }

    /**
     * Insert a row into a table named by tableName, with defined table and params.
     * 
     * @param tableName
     *            can not be null.
     * @param columns
     *            can not be null, and has a length bigger than 0.
     * @throws SQLException
     *             if SQLException occurs.
     */
    private void insertOneRow(String tableName, String[] columns) throws SQLException {
        StringBuffer sqlSb = new StringBuffer();
        sqlSb.append("insert into ");
        sqlSb.append(tableName);
        sqlSb.append(" values (");
        for (int i = 0; i < columns.length - 1; i++)
            sqlSb.append("?, ");
        sqlSb.append("?)");
        String sql = sqlSb.toString();

        PreparedStatement ps = prepareStatement(sql);
        for (int i = 1; i <= columns.length; i++)
            ps.setString(i, columns[i - 1]);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * Insert multiple rows into one table.
     * 
     * @param tableName
     *            the table will receive new rows.
     * @param rows
     *            new rows for the table. Each row has only one column.
     * @throws SQLException
     */
    private void insertMultipleRows(String tableName, String[][] rows)
            throws SQLException {
        for (int i = 0; i < rows.length; i++) {
            String[] row = rows[i];
            insertOneRow(tableName, row);
        }
    }

    /**
     * Valid content in defined table.
     * 
     * @param expected
     *            the values expected, it has the same order with the table.
     *            i.e.expected[0] means the expected values for the first row in rs.
     * @param tableName
     *            whose content will be compared.
     * @throws SQLException
     *             means invalid.
     */
    private void validTable(String[][] expected, String tableName) throws SQLException {
        String sql = getSelectSql(tableName);
        Statement st = createStatement();
        ResultSet rs = st.executeQuery(sql);
        JDBC.assertFullResultSet(rs, expected);
        st.close();
    }

    /**
     * Valid the current row record of passed ResultSet.
     * 
     * @param exected
     *            the values expected, it has the same order with the table.
     * @param useClob
     *            for each element of useColb, true means the column is Clob, false means
     *            varchar.
     * @param rs
     *            whose current row will be compared.
     * @throws SQLException
     *             means invalid.
     */
    private void validSingleRow(String[] exected, boolean[] useClob, ResultSet rs)
            throws SQLException {
        for (int i = 0; i < exected.length; i++) {
            String real;
            if (useClob[i]) {
                Clob c = rs.getClob(i + 1);
                real = c.getSubString(1, (int) c.length());
            } else {
                real = rs.getString(i + 1);
            }

            assertEquals("Compare column " + (i + 1), exected[i], real);
        }
    }

    public void tearDown() throws Exception {
        dropTable(BIG_TABLE_NAME);
        super.tearDown();
    }

    public static Test suite() {
        BaseTestSuite suite = new BaseTestSuite("BigDataTest");
        suite.addTest(TestConfiguration.defaultSuite(BigDataTest.class));
        return suite;
    }

    /**
     * Mix clob and varchar in the table. The commented part in big.sql has been revived
     * without DRDAProtocolException thrown.
     * 
     * @throws SQLException
     */
    public void testMixture() throws SQLException {
        int[] ia = { 32672, 32672, 32672, 32672, };
        boolean[] useClob = { true, false, false, true, };
        createTable(BIG_TABLE_NAME, ia, useClob);

        String[] sa = { "a", "b", "c", "d", };
        insertOneRow(BIG_TABLE_NAME, sa, ia);

        String[] row = getStringArray(sa, ia);
        String[][] expected = { row, };
        validTable(expected, BIG_TABLE_NAME);

        insertOneRow(BIG_TABLE_NAME, sa, ia);
        insertOneRow(BIG_TABLE_NAME, sa, ia);

        expected = new String[][] { row, row, row, };
        validTable(expected, BIG_TABLE_NAME);

        String sql1 = getSelectSql(BIG_TABLE_NAME);
        Statement st = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = st.executeQuery(sql1);
        assertEquals("Before operation, row No. is 0.", 0, rs.getRow());
        rs.first();
        assertEquals("After calling first(), row No. is 1.", 1, rs.getRow());
        validSingleRow(row, useClob, rs);
        rs.next();
        assertEquals("After calling next(), row No. is 2.", 2, rs.getRow());
        validSingleRow(row, useClob, rs);
        rs.previous();
        assertEquals("After calling previous(), row No. is 1.", 1, rs.getRow());
        validSingleRow(row, useClob, rs);
        rs.last();
        assertEquals("After calling last(), row No. is 3.", 3, rs.getRow());
        validSingleRow(row, useClob, rs);
        rs.close();

        st.close();
    }

    /**
     * let's try scrolling.
     * 
     * @throws SQLException
     */
    public void testScrolling() throws SQLException {
        int[] lens = { 10000, 10000, 10000, 10000, };
        boolean[] useClob = { false, false, false, false, };
        createTable(BIG_TABLE_NAME, lens, useClob);

        String[] sa1 = { "a", "b", "c", "d", };
        insertOneRow(BIG_TABLE_NAME, sa1, lens);
        String[] sa2 = new String[] { "e", "f", "g", "h", };
        insertOneRow(BIG_TABLE_NAME, sa2, lens);
        String[] sa3 = new String[] { "i", "j", "k", "l", };
        insertOneRow(BIG_TABLE_NAME, sa3, lens);
        String[] sa4 = new String[] { "m", "n", "o", "p", };
        insertOneRow(BIG_TABLE_NAME, sa4, lens);

        String[] row1 = getStringArray(sa1, lens);
        String[] row2 = getStringArray(sa2, lens);
        String[] row3 = getStringArray(sa3, lens);
        String[] row4 = getStringArray(sa4, lens);
        String[][] expected = { row1, row2, row3, row4, };
        validTable(expected, BIG_TABLE_NAME);

        String sql = getSelectSql(BIG_TABLE_NAME);
        Statement st = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = st.executeQuery(sql);

        rs.first();
        validSingleRow(row1, useClob, rs);
        rs.next();
        validSingleRow(row2, useClob, rs);
        rs.previous();
        validSingleRow(row1, useClob, rs);
        rs.last();
        validSingleRow(row4, useClob, rs);
        rs.close();

        rs = st.executeQuery(sql);
        rs.last();
        validSingleRow(row4, useClob, rs);
        rs.close();

        st.close();
    }

    /**
     * try a column which is &gt; 32767
     * 
     * @throws SQLException
     */
    public void testBigColumn() throws SQLException {
        int[] ia = { 40000, };
        boolean[] useClob = { true, };
        createTable(BIG_TABLE_NAME, ia, useClob);

        String[] sa = { "a", };
        insertOneRow(BIG_TABLE_NAME, sa, ia);

        String[][] expected = { getStringArray(sa, ia), };
        validTable(expected, BIG_TABLE_NAME);
    }

    /**
     * try several columns &gt; 32767.
     * 
     * @throws SQLException
     */
    public void testSeveralBigColumns() throws SQLException {
        int[] ia = { 40000, 40000, 40000, };
        boolean[] useClob = { true, true, true, };
        createTable(BIG_TABLE_NAME, ia, useClob);

        String[] sa = { "a", "b", "c", };
        insertOneRow(BIG_TABLE_NAME, sa, ia);

        String[] sa1 = new String[] { "d", "e", "f", };
        insertOneRow(BIG_TABLE_NAME, sa1, ia);

        String[][] expected = { getStringArray(sa, ia), getStringArray(sa1, ia), };
        validTable(expected, BIG_TABLE_NAME);
    }

    /**
     * create table with row greater than 32K.
     * 
     * @throws SQLException
     */
    public void testBigRow() throws SQLException {
        int[] ia = { 10000, 10000, 10000, 10000, };
        boolean[] useClob = { false, false, false, false, };
        createTable(BIG_TABLE_NAME, ia, useClob);

        String[] sa = { "a", "b", "c", "d", };
        insertOneRow(BIG_TABLE_NAME, sa, ia);

        String[][] expected = { getStringArray(sa, ia), };
        validTable(expected, BIG_TABLE_NAME);

        String[] sa1 = new String[] { "e", "f", "g", "h", };
        insertOneRow(BIG_TABLE_NAME, sa1, ia);

        expected = new String[][] { expected[0], getStringArray(sa1, ia), };
        validTable(expected, BIG_TABLE_NAME);
    }

    /**
     * the overhead for DSS on QRYDTA is 15 bytes let's try a row which is exactly 32767
     * (default client query block size).
     * 
     * @throws SQLException
     */
    public void testDefaultQueryBlock() throws SQLException {
        int[] lens = { 30000, 2752, };
        boolean[] useClob = { false, false, };
        createTable(BIG_TABLE_NAME, lens, useClob);

        String[] sa = { "a", "b", };
        insertOneRow(BIG_TABLE_NAME, sa, lens);

        String[][] expected = { getStringArray(sa, lens), };
        validTable(expected, BIG_TABLE_NAME);
    }

    /**
     * Various tests for JIRA-614: handling of rows which span QRYDTA blocks. What happens
     * when the SplitQRYDTA has to span 3+ blocks.
     * 
     * @throws SQLException
     */
    public void testSpanQRYDTABlocks() throws SQLException {
        int[] lens = { 32672, 32672, 32672, 32672, };
        boolean[] useClob = { false, false, false, false, };
        createTable(BIG_TABLE_NAME, lens, useClob);

        String[] sa = { "a", "b", "c", "d", };
        insertOneRow(BIG_TABLE_NAME, sa, lens);

        String[] row = getStringArray(sa, lens);
        String[][] expected = { row, };
        validTable(expected, BIG_TABLE_NAME);

        insertOneRow(BIG_TABLE_NAME, sa, lens);
        insertOneRow(BIG_TABLE_NAME, sa, lens);

        expected = new String[][] { row, row, row, };
        validTable(expected, BIG_TABLE_NAME);

        String sql1 = getSelectSql(BIG_TABLE_NAME);
        Statement st = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = st.executeQuery(sql1);
        assertEquals("Before operation, row No. is 0.", 0, rs.getRow());
        rs.first();
        assertEquals("After calling first(), row No. is 1.", 1, rs.getRow());
        validSingleRow(row, useClob, rs);
        rs.next();
        assertEquals("After calling next(), row No. is 2.", 2, rs.getRow());
        validSingleRow(row, useClob, rs);
        rs.previous();
        assertEquals("After calling previous(), row No. is 1.", 1, rs.getRow());
        validSingleRow(row, useClob, rs);
        rs.last();
        assertEquals("After calling last(), row No. is 3.", 3, rs.getRow());
        validSingleRow(row, useClob, rs);
        rs.close();
        st.close();
    }

    /**
     * What happens when the row + the ending SQLCARD is too big.
     * 
     * @throws SQLException
     */
    public void testTooBigSQLCARD() throws SQLException {
        int[] lens = { 30000, 2750, };
        boolean[] useClob = { false, false, };
        createTable(BIG_TABLE_NAME, lens, useClob);

        String[] sa = { "a", "b", };
        insertOneRow(BIG_TABLE_NAME, sa, lens);

        String[][] expected = { getStringArray(sa, lens), };
        validTable(expected, BIG_TABLE_NAME);
    }

    /**
     * Test a table just has only one column typed long varchar. This is a Test case
     * commented in big.sql, but revived partly now. When inserting a big row, a
     * SQLException is thrown with the prompt that 33000 is a invalid length.
     * 
     * @throws SQLException
     */
    public void testLongVarchar() throws SQLException {
        String sql = "create table " + BIG_TABLE_NAME + "(lvc long varchar )";
        createTable(sql);

        String[] sa = { "a", "a", "a", "a", "a", };
        int[] timesArray = { 1000, 2000, 3000, 32000, 32700, };
        String[][] rows = getRowsWithOnlyOneColumn(sa, timesArray);

        insertMultipleRows(BIG_TABLE_NAME, rows);
        validTable(rows, BIG_TABLE_NAME);
    }

    /**
     * Test a table just has only one column typed varchar. This is a Test case commented
     * in big.sql, but revived partly now.
     * 
     * @throws SQLException
     */
    public void testVarchar() throws SQLException {
        String sql = "create table " + BIG_TABLE_NAME + "(vc varchar(32672))";
        createTable(sql);

        String[] sa = { "a", "a", "a", "a", "a", };
        int[] timesArray = { 1000, 2000, 3000, 32000, 32672, };
        String[][] rows = getRowsWithOnlyOneColumn(sa, timesArray);

        insertMultipleRows(BIG_TABLE_NAME, rows);
        validTable(rows, BIG_TABLE_NAME);
    }
}