File: AutomaticIndexStatisticsTest.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (795 lines) | stat: -rw-r--r-- 29,931 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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/*

   Derby - Class org.apache.derbyTesting.functionTests.tests.store.AutomaticIndexStatisticsTest

   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.store;

import java.io.File;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;
import javax.sql.DataSource;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.PrivilegedFileOpsForTests;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.IndexStatsUtil;
import org.apache.derbyTesting.junit.IndexStatsUtil.IdxStats;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.JDBCDataSource;
import org.apache.derbyTesting.junit.TestConfiguration;
import org.apache.derbyTesting.junit.TimeZoneTestSetup;
import org.apache.derbyTesting.junit.Utilities;

/**
 * Tests various aspects of the automatic index statistics update feature,
 * which is designed to run as a background task.
 * <p>
 * Some of the tests ensure that the daemon is able to recover after
 * encountering various kinds of "expected errors", for instance if the table
 * is dropped while being scanned by the daemon.
 */
public class AutomaticIndexStatisticsTest
    extends BaseJDBCTestCase {

    // used also by Derby5582AutomaticIndexStatisticsTest so just protected
    protected static final String MASTERDB = "masterDb";
    private static final String BIG_TABLE = "BIG_TABLE";
    private static final long DEFAULT_TIMEOUT = 20*1000;

    private static final String[] TYPES = new String[] {"TABLE", "VIEW"};

    private static boolean dbCreated;
    // Change this if running test cases in parallel...
    private static IndexStatsUtil stats;

    public AutomaticIndexStatisticsTest(String name) {

        super(name);
    }

    public static Test suite() {
        Test test = new BaseTestSuite(AutomaticIndexStatisticsTest.class);
        test = new CleanDatabaseTestSetup(test);
        test = TestConfiguration.additionalDatabaseDecorator(test, MASTERDB);

        // DERBY-5964: The test needs to check the timestamp stored in
        // SYSSTATISTICS, which is in the local timezone. Since those
        // timestamps may be ambiguous around the transition to or from DST,
        // run this test in a timezone that doesn't observe DST. The
        // TimeZoneTestSetup can probably be removed once DERBY-5974 is fixed.
        return new TimeZoneTestSetup(test, "GMT");
    }

    /** Initialize the default statistics helper object. */
    public void setUp()
            throws SQLException {
        if (stats != null) {
            stats.release();
        }
        stats = new IndexStatsUtil(openDefaultConnection(), DEFAULT_TIMEOUT);
    }

    /** Release the default statistics helper object. */
    public void tearDown()
            throws Exception {
        if (stats != null) {
            stats.release();
        }
        stats = null;
        super.tearDown();
    }

    /**
     * Make sure stats are created when the table grows, and that the database
     * can be deleted after automated stats create/update -
     * that is verify that there are no open file handles left behind in the
     * daemon.
     */
    public void testStatsCreatedOnGrowthThenDeleteDb()
            throws SQLException {
        String db = "singleUse/newCleanDb";
        DataSource ds = JDBCDataSource.getDataSource();
        JDBCDataSource.setBeanProperty(ds, "databaseName", db);
        JDBCDataSource.setBeanProperty(ds, "createDatabase", "create");
        Connection con = ds.getConnection();
        String TAB = "TEST_GROWTH_EMPTY";
        createAndInsertSimple(con, TAB, 300);
        // This should trigger creation of statistics.
        PreparedStatement ps = con.prepareStatement(
                "select * from " + TAB + " where id = ?");
        ps.close();

        // Get statistics for the non-unique index.
        IdxStats[] myStats = new IndexStatsUtil(
                ds.getConnection(), DEFAULT_TIMEOUT).getStatsTable(TAB, 1);
        assertEquals(1, myStats.length);
        assertTrue(myStats[0].rows == 300);

        // Shutdown database and try to delete it.
        JDBCDataSource.shutdownDatabase(ds);
        assertDirectoryDeleted(constructDbPath(db));
    }

    /** Make sure stats are updated when the table grows. */
    public void testStatsUpdatedOnGrowth()
            throws SQLException {
        String TAB = "TEST_GROWTH";
        createAndInsertSimple(TAB, 10000);
        // This should trigger creation of statistics.
        prepareStatement("select * from " + TAB + " where id = ?");

        // Get statistics
        IdxStats[] statsPre = stats.getStatsTable(TAB, 1);
        assertEquals(1, statsPre.length);

        // Insert more rows.
        setAutoCommit(false);
        insertSimple(TAB, 50000, 10000);
        // Force a checkpoint to update the row estimates.
        forceRowCountEstimateUpdate(TAB);
        prepareStatement("select * from " + TAB + " where 1=1");
        IdxStats[] statsPost = getFilteredTableStats(TAB, 1, statsPre);
        assertEquals(1, statsPost.length);
        assertFalse(statsPre[0].equals(statsPost[0]));
        assertFalse(statsPre[0].after(statsPost[0]));
        // Insert a few more rows, should not trigger a new update
        insertSimple(TAB, 1000, 60000);
        forceRowCountEstimateUpdate(TAB);
        prepareStatement("select * from " + TAB + " where 2=2");
        // Manual wait to see if the existing stats are replaced - they should
        // not be
        Utilities.sleep(1500);
        IdxStats[] statsPost1 = stats.getStatsTable(TAB, 1);
        assertTrue(statsPost[0].equals(statsPost1[0]));
        assertFalse(statsPost1[0].after(statsPost[0]));
    }

    /**
     * Shuts down database while the daemon is scanning a table, and then
     * makes sure the database directory can be deleted.
     */
    public void testShutdownWhileScanningThenDelete()
            throws IOException, SQLException {
        // Need a big enough table to get the timing right.
        String db = "singleUse/copyShutdown";
        copyDb(db);
        DataSource ds = JDBCDataSource.getDataSource();
        JDBCDataSource.setBeanProperty(ds, "databaseName", db);
        Connection con = ds.getConnection();
        String TAB = BIG_TABLE;

        // Trigger statistics creation.
        PreparedStatement ps = con.prepareStatement(
                "select * from " + TAB + " where id = ?");
        ps.close();

        // Wait to make sure the scan starts.
        Utilities.sleep(150);
        // Now shut down the database.
        JDBCDataSource.shutdownDatabase(ds);
        assertDirectoryDeleted(constructDbPath(db));
    }

    /**
     * Drops table while the daemon is scanning it, then triggers update of
     * statistics of a different table, and finally shuts down and deletes the
     * database.
     */
    public void testDropWhileScanningThenDelete()
            throws IOException, SQLException {
        // Need a big enough table to get the timing right.
        String TAB1 = BIG_TABLE;
        String TAB2 = "SECONDARY_TABLE";
        String db = "singleUse/copyDrop";
        copyDb(db);
        DataSource ds = JDBCDataSource.getDataSource();
        JDBCDataSource.setBeanProperty(ds, "databaseName", db);
        Connection con = ds.getConnection();
        // Create secondary table.
        createAndInsertSimple(con, TAB2, 20000);

        // Trigger statistics creation.
        PreparedStatement ps = con.prepareStatement(
                "select * from " + TAB1 + " where id = ?");
        ps.close();
        // Wait to make sure the scan starts.
        Utilities.sleep(150);
        println("dropping table...");
        Statement stmt = con.createStatement();
        stmt.executeUpdate("drop table " + TAB1);
        stmt.close();

        // Trigger stats update on secondary table.
        IndexStatsUtil myStats =
                new IndexStatsUtil(ds.getConnection(), DEFAULT_TIMEOUT);
        myStats.assertNoStatsTable(TAB2);
        con.prepareStatement("select * from " + TAB2 + " where id = ?");
        myStats.assertTableStats(TAB2, 1);
        myStats.release();

        // Shutdown, then delete database directory.
        JDBCDataSource.shutdownDatabase(ds);
        assertDirectoryDeleted(constructDbPath(db));
    }

    /**
     * Tests that compressing the table while scanned makes the daemon fail
     * gracefully, and that the daemon can do other work afterwards.
     */
    public void testCompressWhileScanning()
            throws IOException, SQLException {
        String TAB1 = BIG_TABLE;
        String TAB2 = "SECONDARY_TABLE";
        String db = "singleUse/copyCompress";
        copyDb(db);
        DataSource ds = JDBCDataSource.getDataSource();
        JDBCDataSource.setBeanProperty(ds, "databaseName", db);
        Connection con = ds.getConnection();
        // Create secondary table.
        createAndInsertSimple(con, TAB2, 20000);

        // Trigger statistics creation.
        PreparedStatement ps = con.prepareStatement(
                "select * from " + TAB1 + " where id = ?");
        ps.close();
        // Wait to make sure the scan starts.
        Utilities.sleep(150);
        println("compressing table...");
        Statement stmt = con.createStatement();
        stmt.executeUpdate("call SYSCS_UTIL.SYSCS_COMPRESS_TABLE('APP', '" +
                TAB1 + "', 0)");
        stmt.close();
        // There should still be a statistics object written during the
        // compress operation.
        IndexStatsUtil myStats =
                new IndexStatsUtil(ds.getConnection(), DEFAULT_TIMEOUT);
        myStats.assertTableStats(TAB1, 1);

        // Trigger stats update on secondary table, make sure the daemon can
        // still process work.
        myStats.assertNoStatsTable(TAB2);
        con.prepareStatement("select * from " + TAB2 + " where id = ?");
        myStats.assertTableStats(TAB2, 1);
        myStats.release();

        // Shutdown database and try to delete it.
        JDBCDataSource.shutdownDatabase(ds);
        assertDirectoryDeleted(constructDbPath(db));
    }

    /**
     * Tests that the statistics computed are correct.
     *
     * @throws SQLException if something goes wrong
     */
    public void testStatisticsCorrectness()
            throws SQLException {
        // Create table.
        String TAB = "STAT_CORR";
        dropTable(TAB);
        Statement stmt = createStatement();
        stmt.executeUpdate("create table " + TAB +
                " (id1 int, id2 int, id3 int, val int, " +
                "primary key (id1, id2, id3))");
        stats.assertNoStatsTable(TAB);
        // Insert rows.
        PreparedStatement ps = prepareStatement("insert into " + TAB +
                " values (?,?,?,?)");
        // # rows = 100 * 50 * 10 = 50'000
        setAutoCommit(false);
        final int rows = 100*50*10;
        for (int i=1; i <= 100; i++) {
            ps.setInt(1, i);
            for (int j=1; j <= 50; j++) {
                ps.setInt(2, j);
                for (int k=1; k <= 10; k++) {
                    ps.setInt(3, k);
                    ps.setInt(4, i*j*k % 750);
                    ps.executeUpdate();
                }
            }
        }
        commit();
        setAutoCommit(true);

        // Select to trigger statistics generation.
        forceRowCountEstimateUpdate(TAB);
        JDBC.assertDrainResults(
                prepareStatement("select * from " + TAB + " where id1 = 10")
                .executeQuery());

        // We expect three stats objects for the single index; one per
        // leading columns combination (c1, c1-c2, and c1-c2-c3).
        IdxStats statsObj[] = stats.getStatsTable(TAB, 3);
        assertEquals(3, statsObj.length);

        Timestamp now = new Timestamp(new Date().getTime());
        for (int i=0; i < statsObj.length; i++) {
            IdxStats s = statsObj[i];
            assertEquals(rows, s.rows);
            
            // DERBY-6144 
            // Stats cannot have been created after the current time (future).
            assertFalse(
                "expected stat created in past:now = " + now + 
                ";s.created = " + s.created,
                s.created.compareTo(now) > 0);

            switch (s.lcols) {
                case 1:
                    assertEquals(100, s.card);
                    break;
                case 2:
                    assertEquals(5000, s.card);
                    break;
                case 3:
                    assertEquals(50000, s.card);
                    break;
            default:
                fail("unexpected number of leading columns: " + s.lcols);
            }
        }

        // Now create a second index in the opposite order, check stats.
        stmt.executeUpdate("create index IDXREV on " + TAB + "(id3, id2, id1)");
        statsObj = stats.getStatsIndex("IDXREV", 3);
        assertEquals(3, statsObj.length);
        Timestamp earlier = now;
        now = new Timestamp(new Date().getTime());
        for (int i=0; i < statsObj.length; i++) {
            IdxStats s = statsObj[i];
            assertEquals(rows, s.rows);
            assertTrue("current stats created " + s.created +
                    ", previous stats created " + earlier,
                    s.created.after(earlier));
            // Stats cannot have been created after the current time (future).
            assertFalse(
                "expected stat created in past:now = " + now + 
                ";s.created = " + s.created,
                s.created.compareTo(now) > 0);

            switch (s.lcols) {
                case 1:
                    assertEquals(10, s.card);
                    break;
                case 2:
                    assertEquals(500, s.card);
                    break;
                case 3:
                    assertEquals(50000, s.card);
                    break;
            default:
                fail("unexpected number of leading columns: " + s.lcols);
            }
        }

        // Finally, create a non-unique index on the val column.
        stmt.executeUpdate("create index IDXVAL on " + TAB + "(val)");
        ResultSet rs = stmt.executeQuery(
                "select val from " + TAB + " order by val");
        // Calculate number of unique values.
        int uniqueVals = 0;
        int prev = -1;
        while (rs.next()) {
            int curr = rs.getInt(1);
            if (curr != prev) {
                uniqueVals++;
                prev = curr;
            }
        }
        rs.close();
        // Get stats and check the associated values.
        IdxStats[] valStat = stats.getStatsIndex("IDXVAL", 1);
        assertEquals(1, valStat.length);
        assertEquals(uniqueVals, valStat[0].card);
        assertEquals(rows, valStat[0].rows);
    }

    public void testSelectFromSimpleView()
            throws SQLException {
        // First create a table with a few columns.
        String table = "VIEW_BASE_TABLE";
        String view = "MY_VIEW";

        dropIfExists(getConnection(), view);
        dropIfExists(getConnection(), table);
        Statement s = createStatement();
        s.execute("create table " + table + " (" +
                "id int primary key, col1 int, col2 int)");
        s.execute("create index COL2_IDX on " + table + "(col2)");
        PreparedStatement ps = prepareStatement("insert into " + table +
                " values (?,?,?)");
        setAutoCommit(false);
        for (int i=0; i < 30000; i++) {
            ps.setInt(1, i);
            ps.setInt(2, i % 15);
            ps.setInt(3, i % 25);
            ps.executeUpdate();
            // Commit periodically
            if (i % 5000 == 0) {
                commit();
            }
        }
        commit();
        setAutoCommit(true);
        ps.close();

        // Create the view.
        s.execute("create view " + view + "(vcol_1, vcol2) " +
                "AS select id, col2 from " + table);

        // Select from the view, using index.
        stats.assertNoStatsTable(table);
        prepareStatement("select * from " + view + " where vcol2 = 7");
        stats.assertNoStatsTable(table);
        // Trigger update of the base table.
        prepareStatement("select * from " + table + " where col2 = 7");
        stats.assertTableStats(table, 1);
    }

    /**
     * Verifies that queries on a table with single-column unique indexes only
     * don't trigger a statistics update with the istat daemon.
     */
    public void testNoUpdateTriggeredBySingleColumnUniqueIndex()
            throws SQLException {
        // Create table.
        String TAB = "STAT_SCUI";
        dropTable(TAB);
        Statement stmt = createStatement();
        stmt.executeUpdate("create table " + TAB +
                " (id int primary key, val int unique not null)");
        stats.assertNoStatsTable(TAB);
        PreparedStatement ps = prepareStatement(
                "insert into " + TAB + " values (?,?)");
        setAutoCommit(false);
        for (int i=0; i < 2000; i++) {
            ps.setInt(1, i);
            ps.setInt(2, i);
            ps.executeUpdate();
        }
        commit();
        // The queries below would trigger a stats update in earlier releases.
        PreparedStatement psSel1 = prepareStatement(
                "select id from " + TAB + " where id = ?");
        psSel1.setInt(1, 98);
        JDBC.assertSingleValueResultSet(psSel1.executeQuery(), "98");
        PreparedStatement psSel2 = prepareStatement(
                "select val from " + TAB + " where val = ?");
        psSel2.setInt(1, 1573);
        JDBC.assertSingleValueResultSet(psSel2.executeQuery(), "1573");
        Utilities.sleep(100); 
        stats.assertNoStatsTable(TAB);

        // Try again after inserting more data.
        for (int i=2000; i < 4000; i++) {
            ps.setInt(1, i);
            ps.setInt(2, i);
            ps.executeUpdate();
        }
        commit();
        forceRowCountEstimateUpdate(TAB);
        psSel1 = prepareStatement(
                "select id from " + TAB + " where id = ?");
        psSel1.setInt(1, 117);
        JDBC.assertSingleValueResultSet(psSel1.executeQuery(), "117");
        psSel2 = prepareStatement(
                "select val from " + TAB + " where val = ?");
        psSel2.setInt(1, 1);
        JDBC.assertSingleValueResultSet(psSel2.executeQuery(), "1");
        Utilities.sleep(100); 
        stats.assertNoStatsTable(TAB);

        // Cleanup
        dropTable(TAB);
    }

    // Utility methods

    /**
     * Creates a copy of the master db.
     *
     * @param newDbName name of the copy
     * @throws IOException if something goes wrong
     * @throws SQLException if something goes wrong
     */
    private void copyDb(String newDbName)
            throws IOException, SQLException {
        if (!dbCreated) {
            createMasterDb();
        }

        File master = constructDbPath(
                TestConfiguration.getCurrent().getPhysicalDatabaseName(
                    MASTERDB));
        final File dest = constructDbPath(newDbName);

        // Make sure the directory containing the database directory exists.
        // We expect the security manager to stop us from creating directories
        // where we aren't supposed to.
        if (!PrivilegedFileOpsForTests.exists(dest.getParentFile())) {
            AccessController.doPrivileged(new PrivilegedAction<Void>() {

                public Void run() {
                    assertTrue(dest.getParentFile().mkdirs());
                    return null;
                }
            });
        }

        PrivilegedFileOpsForTests.copy(master, dest);
    }

    /**
     * Creates the default/master db with a larger number of rows.
     *
     * @throws SQLException if creating the database fails
     */
    private void createMasterDb()
            throws SQLException {
        long start = System.currentTimeMillis();
        String table = BIG_TABLE;
        int rows = 1*1000*1000;
        DataSource ds1 = JDBCDataSource.getDataSourceLogical(MASTERDB);
        JDBCDataSource.setBeanProperty(ds1, "createDatabase", "create");
        Connection con = ds1.getConnection();
        // Check if the table exists, if so, drop it.
        dropIfExists(con, table);
        // Create the table.
        Statement stmt = con.createStatement();
        stmt.executeUpdate("create table " + table + "(id int primary key)");
        stmt.close();

        // Insert data
        con.setAutoCommit(false);
        PreparedStatement ps = con.prepareStatement("insert into " + table +
                " values ?");
        for (int i=0; i < rows; i++) {
            ps.setInt(1, i);
            ps.addBatch();
            if (i % 5000 == 0) {
                ps.executeBatch();
                con.commit();
            }
        }
        ps.executeBatch();
        con.commit();
        con.close();
        println("created master db with " + rows + " rows in " +
                ((System.currentTimeMillis() - start) / 1000) + " seconds");

        // Shut down the master db, we will copy it later.
        JDBCDataSource.shutdownDatabase(
                JDBCDataSource.getDataSourceLogical(MASTERDB));

        dbCreated = true;

    }

    /**
     * Forces Derby to update the row count estimate by doing a full table
     * scan and then invoking a checkpoint.
     *
     * @param table target table
     * @throws SQLException if something goes wrong
     */
    private void forceRowCountEstimateUpdate(String table)
            throws SQLException {
        Statement stmt = createStatement();
        JDBC.assertDrainResults(
                stmt.executeQuery("select count(*) from " + table));
        stmt.execute("call SYSCS_UTIL.SYSCS_CHECKPOINT_DATABASE()");
        stmt.close();
    }

    /**
     * Constructs the path to the database base directory.
     *
     * @param relDbDirName the database name (relative)
     * @return The path to the database.
     */
    private File constructDbPath(String relDbDirName) {
        // Example:
        //     "singleUse/readOnly" -> "<derby.system.home>/singleUse/readOnly"
        File f = new File(getSystemProperty("derby.system.home"));
        return new File(f, relDbDirName);
    }

    /**
     * Default method to create and populate a simple test table.
     * <p>
     * The table consists of a two integer columns, where the first is the
     * primary key of the table and the second is a value with a non-unique
     * index on it.
     *
     * @param table target table
     * @param rows number of rows to insert
     * @throws SQLException if creating/populating the table fails
     */
    private void createAndInsertSimple(String table, int rows)
            throws SQLException {
        createAndInsertSimple(null, table, rows);
    }

    /**
     * Default method to create and populate a simple test table.
     * <p>
     * The table consists of a two integer columns, where the first is the
     * primary key of the table and the second is a value with a non-unique
     * index on it.
     *
     * @param con the connection to use (may be {@code null}, in which case
     *      the default connection will be used)
     * @param table target table
     * @param rows number of rows to insert
     * @throws SQLException if creating/populating the table fails
     */
    private void createAndInsertSimple(Connection con, String table, int rows)
            throws SQLException {
        Statement s;
        IndexStatsUtil myStats;
        if (con == null) {
            con = getConnection();
            s = createStatement();
            myStats = stats;
        } else {
            s = con.createStatement();
            myStats = new IndexStatsUtil(con);
        }
        // See if the table exists, and if so, drop it.
        dropIfExists(con, table);
        // Create table.
        s.executeUpdate(
                "create table " + table + "(id int primary key, val int)");
        s.executeUpdate("create index NON_UNIQUE_INDEX_" + table + " on " +
                table + "(val)");

        myStats.assertNoStatsTable(table);

        // Insert data
        long start = System.currentTimeMillis();
        println("created " + table + ", inserting " + rows + " rows");
        insertSimple(con, table, rows, 0);
        println("completed in " + (System.currentTimeMillis() - start) + " ms");
        myStats.assertNoStatsTable(table);
    }

    /**
     * Inserts the specified number of rows into the table, using an increasing
     * integer as the value.
     *
     * @param table target table
     * @param rows number of rows
     * @param start starting value for the first inserted row
     * @throws SQLException if something goes wrong
     */
    private void insertSimple(String table, int rows, int start)
            throws SQLException {
        // Use the default connection.
        insertSimple(getConnection(), table, rows, start);
    }

    /**
     * Inserts the specified number of rows into the table, using an increasing
     * integer as the value.
     *
     * @param con the connection to use
     * @param table target table
     * @param rows number of rows
     * @param start starting value for the first inserted row
     * @throws SQLException if something goes wrong
     * @throws NullPointerException if {@code con} is {@code null}
     */
    private void insertSimple(Connection con, String table, int rows, int start)
            throws SQLException {
        PreparedStatement ps = con.prepareStatement(
                                    "insert into " + table + " values (?,?)");
        boolean autoCommit = con.getAutoCommit();
        con.setAutoCommit(false);
        for (int i=start; i < start+rows; i++) {
            ps.setInt(1, i);
            ps.setInt(2, i % 20);
            ps.addBatch();
            if (i % 5000 == 0) {
                ps.executeBatch();
                con.commit();
            }
        }
        ps.executeBatch();
        con.commit();
        con.setAutoCommit(autoCommit);
    }

    /**
     * Obtains the statistics for all indexes associated with the given table in
     * the default database.
     *
     * @param table base table
     * @param expectedCount number of statistics objects to obtain
     * @param oldStats statistics objects to ignore
     * @return An array with the statistics objects obtained.
     * @throws SQLException if something goes wrong
     * @throws AssertionError if the number of statistics objects expected
     *      can't be obtained within the time limit
     *      ({@linkplain #DEFAULT_TIMEOUT})
     */
    private IdxStats[] getFilteredTableStats(String table, int expectedCount,
                                             IdxStats[] oldStats)
            throws SQLException {
        long start = System.currentTimeMillis();
        while (System.currentTimeMillis() - start < DEFAULT_TIMEOUT) {
            IdxStats[] ret = stats.getStatsTable(table, expectedCount);
            boolean doReturn = true;
            // Check if we have new stats (if filtering is asked for).
            if (oldStats != null) {
                for (int i=0; i < ret.length; i++) {
                    for (int j=0; j < oldStats.length; j++) {
                        if (ret[i].equals(oldStats[j])) {
                            doReturn = false;
                            break;
                        }
                    }
                }
            }
            if (doReturn) {
                return ret;
            }
            Utilities.sleep(250);
        }
        fail("getting stats for table " + table + " timed out (#expected=" +
                expectedCount + ", #oldStats=" +
                (oldStats == null ? 0 : oldStats.length) + ")");
        // Silence the compiler.
        return null;
    }

    // Static utility methods

    /**
     * Drops the specified entity if it exists.
     *
     * @param con connection to the database
     * @param entity the entity to drop (i.e. table or view)
     */
    private static void dropIfExists(Connection con, String entity)
            throws SQLException {
        ResultSet tables = con.getMetaData().getTables(
                null, null, entity, TYPES);
        while (tables.next()) {
            String type = tables.getString(4);
            if (type.equals("TABLE")) {
                dropTable(con, entity);
            } else if (type.equals("VIEW")) {
                con.createStatement().executeUpdate("drop view " + entity);
            } else {
                fail("entity " + entity + " of unsupported type: " + type);
            }
        }
        tables.close();
    }
}