File: ExistsWithSubqueriesTest.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 (644 lines) | stat: -rw-r--r-- 27,147 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
/*
 
   Derby - Class org.apache.derbyTesting.functionTests.tests.lang.ExistsWithSubqueriesTest

   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.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;

/**
 * This test contains a variety of cases of EXISTS predicates with subqueries.
 *
 * Several tests ensure that an EXISTS predicate which wraps a set operation--
 * meaning a UNION, INTERSECT, or EXCEPT node--returns the correct results.
 * For example:
 *
 *   select * from ( values 'BAD' ) as T
 *     where exists ((values 1) intersect (values 2))
 *
 * should return zero rows. Prompted by DERBY-2370.
 *
 * A somewhat unrelated test verifies the DERBY-3033 behavior, which
 * involves flattening of subqueries with NOT EXISTS predicates. The
 * issue here is that a flattened NOT EXISTS subquery cannot be used
 * to perform equi-join transitive closure, because the implied predicate
 * that results from the flattening is a NOT EQUALS condition.
 */
public class ExistsWithSubqueriesTest extends BaseJDBCTestCase {
    
    private static final String EXISTS_PREFIX_1 =
        "select * from ( values 'GOT_A_ROW' ) as T where exists (";

    private static final String EXISTS_PREFIX_2 =
        "select j from onerow where exists (";

    /**
     * Create a test with the given name.
     * @param name name of the test.
     *
     */
    public ExistsWithSubqueriesTest(String name)
    {
        super(name);
    }
    
    /**
     * Return suite with all tests of the class.
     */
    public static Test suite()
    {
       BaseTestSuite suite = new BaseTestSuite("EXISTS with SET operations");

        /* This is a language/optimization test so behavior will be the
         * same for embedded and client/server.  Therefore we only need
         * to run the test against one or the other; we choose embedded.
         */
        suite.addTest(
            TestConfiguration.embeddedSuite(ExistsWithSubqueriesTest.class));

        /* Wrap the suite in a CleanDatabaseTestSetup that will create
         * and populate the test tables.
         */
        return new CleanDatabaseTestSetup(suite) 
        {
            /**
            * Create and populate the test table.
            */
            protected void decorateSQL(Statement s) throws SQLException
            {
                s.executeUpdate("create table empty (i int)"); 
                s.executeUpdate("create table onerow (j int)");
                s.executeUpdate("insert into onerow values 2");
                s.executeUpdate("create table diffrow (k int)");
                s.executeUpdate("insert into diffrow values 4");
                s.executeUpdate("create table tworows (p int)");
                s.executeUpdate("insert into tworows values 2, 4");
                s.executeUpdate("create table onerow2col (j1 int, j2 int)");
                s.executeUpdate("insert into onerow2col values (2, 2)");
            }
        };
    }

    /**
     * Test queries where the set operation just involves VALUES
     * expressions.
     */
    public void testSetOpsWithVALUES() throws Exception
    {
        Statement st = createStatement();
        String [][] expRS = new String [1][1];

        expRS[0][0] = "GOT_A_ROW";
        checkQuery(st, expRS, EXISTS_PREFIX_1 + "values 1 union values 1)");
        checkQuery(st, expRS, EXISTS_PREFIX_1 + "values 1 intersect values 1)");
        checkQuery(st, expRS, EXISTS_PREFIX_1 + "values 1 except values 0)");

        checkQuery(st, null, EXISTS_PREFIX_1 + "values 1 intersect values 0)");
        checkQuery(st, null, EXISTS_PREFIX_1 + "values 1 except values 1)");
        st.close();
    }

    /**
     * Test queries where the set operation has subqueries which are not
     * correlated to the outer query.  It's important to check for cases
     * where we have explicit columns _and_ cases where we have "*" because
     * the binding codepaths differ and we want to verify both.
     */
    public void testNonCorrelatedSetOps() throws Exception
    {
        Statement st = createStatement();
        String [][] expRS = new String [1][1];

        expRS[0][0] = "GOT_A_ROW";

        // Expect 1 row for the following.

        // Simple UNION with "*".
        checkQuery(st, expRS, EXISTS_PREFIX_1 +
            "select * from diffrow union select * from onerow)");

        // Simple UNION with explicit columns.
        checkQuery(st, expRS, EXISTS_PREFIX_1 +
            "select k from diffrow union select j from onerow)");

        // Simple INTERSECT with "*".
        checkQuery(st, expRS, EXISTS_PREFIX_1 +
            "select * from diffrow intersect select 4 from onerow)");

        // Simple INTERSECT with explicit columns.
        checkQuery(st, expRS, EXISTS_PREFIX_1 +
            "select k from diffrow intersect select 4 from onerow)");

        // Simple EXCEPT with "*".
        checkQuery(st, expRS, EXISTS_PREFIX_1 +
            "select * from diffrow except select * from onerow)");

        // Simple EXCEPT with explicit columns.
        checkQuery(st, expRS, EXISTS_PREFIX_1 +
            "select k from diffrow except select j from onerow)");

        // EXCEPT with "*" where left and right children have their
        // own preds.
        checkQuery(st, expRS, EXISTS_PREFIX_1 +
            "(select * from tworows where p = 2) except " +
            "(select * from tworows where p <> 2))");

        // INTERSECT with "*" where left and right children have their
        // own preds.
        checkQuery(st, expRS, EXISTS_PREFIX_1 +
            "(select * from tworows where p = 2) intersect " +
            "(select * from tworows where p = 2))");

        // Expect 0 rows for the following.  Similar queries to
        // above except modified to return no rows.

        checkQuery(st, null, EXISTS_PREFIX_1 +
            "select i from empty union select * from empty)");

        checkQuery(st, null, EXISTS_PREFIX_1 +
            "select * from onerow intersect select * from empty)");

        checkQuery(st, null, EXISTS_PREFIX_1 +
            "select j from onerow intersect select i from empty)");

        checkQuery(st, null, EXISTS_PREFIX_1 +
            "select * from empty except select * from onerow)");

        checkQuery(st, null, EXISTS_PREFIX_1 +
            "select i from empty except select j from onerow)");

        checkQuery(st, null, EXISTS_PREFIX_1 +
            "select * from onerow intersect select * from diffrow)");

        checkQuery(st, null, EXISTS_PREFIX_1 +
            "select j from onerow intersect select k from diffrow)");

        checkQuery(st, null, EXISTS_PREFIX_1 +
            "select * from onerow except select * from onerow)");

        checkQuery(st, null, EXISTS_PREFIX_1 +
            "select j from onerow except select j from onerow)");

        checkQuery(st, null, EXISTS_PREFIX_1 +
            "(select * from tworows where p = 2) intersect " +
            "(select * from tworows where p <> 2))");

        checkQuery(st, null, EXISTS_PREFIX_1 +
            "(select * from tworows where p = 2) except " +
            "(select * from tworows where p = 2))");

        // Negative cases.  These should fail because "oops" is not
        // a valid column in ONEROW.

        assertCompileError("42X04", EXISTS_PREFIX_1 +
            "(select * from onerow where j = 2) intersect " +
            "(select oops from onerow where j <> 2))");

        assertCompileError("42X04", EXISTS_PREFIX_1 +
            "(select * from onerow where j = 2) intersect " +
            "(select * from onerow where oops <> 2))");

        st.close();
    }

    /**
     * Test queries where the set operation has subqueries which are
     * correlated to the outer query.  Subqueries should still be able
     * reference the outer query table and execute without error.
     */
    public void testCorrelatedSetOps() throws Exception
    {
        Statement st = createStatement();
        String [][] expRS = new String [1][1];

        // "2" here is the value that was inserted into "onerow".
        expRS[0][0] = "2";
        
        // Expect 1 row for the following.

        // Right child of UNION has "*" for RCL and references table
        // from outer query.
        checkQuery(st, expRS, EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 1 union " +
            "select * from diffrow where onerow.j < k)");

        // Right child of UNION has qualified "*" for RCL and references
        // table from outer query.
        checkQuery(st, expRS, EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 1 union " +
            "select diffrow.* from diffrow where onerow.j < k)");

        // Right child of UNION has explicit RCL and references
        // table from outer query.
        checkQuery(st, expRS, EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 1 union " +
            "select k from diffrow where onerow.j < k)");

        /* Right child of UNION is itself another EXISTS query whose
         * child is another set operator (INTERSECT). The INTERSECT in
         * turn has a right child which references a table from the
         * outer-most query.
         */
        checkQuery(st, expRS, EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 0 union " +
            "select * from diffrow where exists " +
            "  (select 2 from diffrow intersect " +
            "     select 2 from diffrow where onerow.j < k))");

        /* Right child of UNION is itself another EXISTS query whose
         * child is another set operator (INTERSECT). The INTERSECT in
         * turn has a right child which references a table from the
         * outer-most query.  In this one the INTERSECT returns zero
         * rows.
         */
        checkQuery(st, expRS, EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 1 union " +
            "select * from diffrow where exists " +
            "  (select 2 from empty intersect " +
            "    select 3 from empty where onerow.j < i))");

        /* Right child of UNION is itself another EXISTS query whose
         * child is another set operator (INTERSECT). The INTERSECT in
         * turn has a right child which references 1) a table from the
         * outer-most query, and 2) a table from the INTERSECT node's
         * "parent" subquery (i.e. from the UNION's right subquery).
         */
        checkQuery(st, expRS, EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 1 union " +
            "select * from diffrow where exists " +
            "  (select 2 from onerow2col intersect " +
            "    select 3 from empty where onerow.j < diffrow.k))");

        /* Right child of UNION is itself another EXISTS query whose
         * child is another set operator (INTERSECT). The INTERSECT in
         * turn has a right child which references 1) a table from the
         * outer-most query, and 2) a table from the INTERSECT node's
         * "parent" query.  In addition, add another predicate to the
         * UNION's right subquery and make that predicate reference
         * both 1) a table from the outer-most query, and 2) a table
         * in the subquery's own FROM list.  All of this to ensure
         * that binding finds the correct columns at all levels of
         * the query.
         */
        checkQuery(st, expRS, EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 1 union " +
            "select * from diffrow where exists " +
            "  (select 2 from onerow2col intersect " +
            "    select 3 from empty where onerow.j < k) " +
            "  and (onerow.j < diffrow.k))");

        // Expect 0 rows for the following.  Similar queries to
        // above except modified to return no rows.

        checkQuery(st, null, EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 0 union " +
            "select * from diffrow where onerow.j > k)");

        checkQuery(st, null, EXISTS_PREFIX_2 +
              "select 1 from diffrow where 1 = 0 union " +
            "select * from diffrow where exists " +
            "  (select 2 from diffrow intersect " +
            "     select 3 from diffrow where onerow.j < k))");

        checkQuery(st, null, EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 0 union " +
            "select * from diffrow where exists " +
            "  (select 2 from empty intersect " +
            "    select 3 from empty where onerow.j < i))");

        checkQuery(st, null, EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 0 union " +
            "select * from diffrow where exists " +
            "  (select 2 from onerow2col intersect " +
            "    select 3 from empty where onerow.j < diffrow.k))");

        checkQuery(st, null, EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 0 union " +
            "select * from diffrow where exists " +
            "  (select 2 from onerow2col intersect " +
            "    select 3 from empty where onerow.j < k) " +
            "  and (onerow.j < diffrow.k))");

        // Negative cases.

        // Should fail because left and right children of the UNION
        // have different RCL sizes. (NOTE: Would have passed prior
        // to DERBY-2370, but that was incorrect).
        assertCompileError("42X58", EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 0 union " +
            "select * from onerow2col where onerow.j < j)");

        /* Should fail because there is an explicit subquery ("SELECT *")
         * within the EXISTS query and such a subquery is not allowed to
         * reference outer tables.  So we will be unable to find the
         * column "onerow.j" in this case.
         */
        assertCompileError("42X04", EXISTS_PREFIX_2 +
            "select * from (select 1 from diffrow where 1 = 0 " +
            "union select * from diffrow where onerow.j < k) x)");

        /* Should fail because the UNION's right subquery is trying to
         * select from an outer table.  While the subquery is allowed
         * to reference the outer table in expressions, it cannot
         * include the outer table in its RCL.
         */
        assertCompileError("42X10", EXISTS_PREFIX_2 +
            "select 1 from diffrow where 1 = 1 union " +
            "select onerow.* from diffrow where onerow.j < k)");

        st.close();
    }

    /**
     * Simple helper method to assert the results of the received
     * query.  If the array representing expected results is null
     * then we assert that the query returns no rows.
     */
    private void checkQuery(Statement st, String [][] expRS,
        String query) throws Exception
    {
        ResultSet rs = st.executeQuery(query);
        if (expRS == null)
            JDBC.assertEmpty(rs);
        else
            JDBC.assertFullResultSet(rs, expRS);
        rs.close();
    }

    /**
     * Regression test for Derby-3033.
     *
     * This method constructs a query with the property that it:
     * - contains a NOT EXISTS condition against a correlated subquery
     * - such that if that subquery is flattened, the result is 3 tables
     *   which all have join predicates on the same key.
     * The point of the test is that it is *not* correct to construct
     * a new equijoin predicate between table d3033_a and d3033_c via
     * transitive closure, because the join condition between d3033_b and
     * d3033_c is NOT EXISTS.
     *
     * In the original bug, the compiler/optimizer erroneously generated
     * the extra equijoin predicate, which caused NPE exceptions at
     * runtime due to attempts to reference the non-existent (NOT EXISTS) row.
     *
     * So this test succeeds if it gets the right results and no NPE.
     */
    public void testDerby3033()
        throws Exception
    {
        setupDerby3033();

        PreparedStatement pstmt = prepareStatement(
            "select c1, c2_b " +
            "from (select distinct st.c1,st.c2_b,dsr.c3_a,st.c3_b " +
            "      from " +
            "             d3033_a dsr, " +  // Table order matters here!
            "             d3033_b st " +
            "      where dsr.c4_a is null " +
            "      and   dsr.c2 = ? " +
            "      and   dsr.c1 = st.c1 " +
            "      and   not exists ( " +
            "              select 1 " +
            "              from d3033_c " +
            "              where d3033_c.c1 = st.c1 " +
            "              and   d3033_c.c2 = ? " +
            "              and   d3033_c.c3_c = ? " +
            "              ) " +
            ") temp "
        );
 
        pstmt.setInt(1, 4);
        pstmt.setInt(2, 4);
        pstmt.setInt(3, 100);
 
        String [][]expected = {
            { "1", "100" },
            { "2", "200" },
            { "3", "300" },
        };
        ResultSet rs = pstmt.executeQuery();
        JDBC.assertFullResultSet(rs, expected);
        pstmt.close();
    }

    /**
     * Ensure that the #rows statistics are updated
     */
    private void updateStats(Statement st, String tName)
        throws Exception
    {
        ResultSet rs = st.executeQuery("select * from " + tName);
        int numRows = 0;
        while (rs.next())
            numRows ++;
        rs.close();
    }

    private void setupDerby3033()
        throws Exception
    {
        // The pattern of inserting the data is fairly important, as we
        // are going to do a combination of joins between the three tables
        // and we want both matching data and non-matching data. We load:
        //
        // d3033_a      d3033_b       d3033_c
        // --------     --------      --------
        //    1            1             1
        //    2            2             3
        //    3            3
        //                 4
        //
        // We also load a whole pile of irrelevant data into tables a and c
        // so that the index becomes relevant in the optimizer's analysis,
        // then we create some constraints and indexes and delete the rows
        // from table d3033_c (the NOT EXISTS table).
        //
        Statement s = createStatement();

        s.executeUpdate("create table d3033_a "+
                        "(c1 int, c2 int, c3_a int, c4_a date)");
        s.executeUpdate("create table d3033_b "+
                        "(c1 int primary key not null, c2_b int, c3_b date)");
        s.executeUpdate("create table d3033_c (c1 int, c2 int, c3_c int)");
        s.executeUpdate("insert into d3033_a (c1,c2,c3_a) values(1, 4, 10)");
        s.executeUpdate("insert into d3033_a (c1,c2,c3_a) values(2, 4, 20)");
        s.executeUpdate("insert into d3033_a (c1,c2,c3_a) values(3, 4, 30)");
        s.executeUpdate("insert into d3033_b values(1, 100, CURRENT_DATE)");
        s.executeUpdate("insert into d3033_b values(2, 200, CURRENT_DATE)");
        s.executeUpdate("insert into d3033_b values(3, 300, CURRENT_DATE)");
        s.executeUpdate("insert into d3033_b values(4, 400, CURRENT_DATE)");
        s.executeUpdate("insert into d3033_c values(1, 4, 100)");
        s.executeUpdate("insert into d3033_c values(3, 4, 100)");
            
        PreparedStatement pstmt2 = prepareStatement(
                "insert into d3033_a (c1, c2, c3_a) values (?,?,?)");

        PreparedStatement pstmt = prepareStatement(
                "insert into d3033_b (c1, c2_b, c3_b) values (?,?,?)");

        java.util.Date now = new java.util.Date();
        java.sql.Timestamp nowTS = new java.sql.Timestamp(now.getTime());
        for (int i = 0; i < 15; i++)
        {
            pstmt.setInt(1, 100+i);
            pstmt.setInt(2, 100+i);
            pstmt.setTimestamp(3, nowTS);
            pstmt.executeUpdate();

            for (int j = 0; j < 200; j++)
            {
                pstmt2.setInt(1, 1000+j);
                pstmt2.setInt(2, 100+i); // note "i" here (FK)
                pstmt2.setInt(3, 1000 + (j+1)*10);
                pstmt2.executeUpdate();
            }
        }


        s.executeUpdate("alter table d3033_a add constraint " +
                "d3033_a_fk foreign key (c2) references d3033_b(c1) " +
                "on delete cascade on update no action");

        s.executeUpdate("alter table d3033_c add constraint " +
                "d3033_c_fk foreign key (c1) references d3033_b(c1) " +
                "on delete cascade on update no action");

        s.executeUpdate("delete from d3033_c");

        // Update the statistics on the 3 tables:
        updateStats(s, "d3033_a");
        updateStats(s, "d3033_b");
        updateStats(s, "d3033_c");

        s.close();
    }

    /**
     * Some EXISTS subqueries (and IN subqueries transformed to EXISTS)
     * returned NULL instead of TRUE or FALSE before DERBY-6408. This test
     * case verifies the fix.
     */
    public void testDerby6408() throws SQLException {
        setAutoCommit(false);
        Statement s = createStatement();

        // This statement used to return only NULLs.
        JDBC.assertFullResultSet(
                s.executeQuery("values (exists(select * from empty), "
                        + "not exists (select * from empty), "
                        + "not (exists (select * from empty)), "
                        + "not (not exists (select * from empty)))"),
                new String[][] {{"false", "true", "true", "false" }});

        // This similar statement worked even before the fix.
        JDBC.assertFullResultSet(
                s.executeQuery("values (exists(select * from onerow), "
                        + "not exists (select * from onerow), "
                        + "not (exists (select * from onerow)), "
                        + "not (not exists (select * from onerow)))"),
                new String[][] {{"true", "false", "false", "true" }});

        // Now put the same expressions in the SELECT list. Used to return
        // only NULLs.
        JDBC.assertFullResultSet(
                s.executeQuery("select exists(select * from empty), "
                        + "not exists (select * from empty), "
                        + "not (exists (select * from empty)), "
                        + "not (not exists (select * from empty)) from onerow"),
                new String[][] {{"false", "true", "true", "false" }});

        // Check the returned value when used in a WHERE predicate. All of
        // these queries returned one row, but they should return no rows
        // because EXISTS shouldn't return null.
        JDBC.assertEmpty(s.executeQuery("select * from onerow "
                + "where (exists (select * from empty)) is null"));
        JDBC.assertEmpty(s.executeQuery("select * from onerow "
                + "where (not exists (select * from empty)) is null"));
        JDBC.assertEmpty(s.executeQuery("select * from onerow "
                + "where (not (not exists (select * from empty))) is null"));

        // The results were correct even before the fix if the subquery
        // wasn't empty. Verify that they still are.
        JDBC.assertEmpty(s.executeQuery("select * from onerow "
                + "where (exists (select * from onerow)) is null"));
        JDBC.assertEmpty(s.executeQuery("select * from onerow "
                + "where (not exists (select * from onerow)) is null"));
        JDBC.assertEmpty(s.executeQuery("select * from onerow "
                + "where (not (not exists (select * from onerow))) is null"));

        // Similar problems were seen in IN subqueries that were rewritten
        // to EXISTS subqueries internally. For example, this query used
        // to return NULL.
        JDBC.assertSingleValueResultSet(
                s.executeQuery("values 1 in (select j from onerow)"), "false");

        // If it should evaluate to TRUE, it worked even before the fix.
        JDBC.assertSingleValueResultSet(
                s.executeQuery("values 2 in (select j from onerow)"), "true");

        // DERBY-6409: Quantified comparisons can also be rewritten to EXISTS,
        // and these two queries returned wrong results before the fix.
        JDBC.assertSingleValueResultSet(
                s.executeQuery("values 1 > all (select 2 from tworows)"), "false");
        JDBC.assertSingleValueResultSet(
                s.executeQuery("values 1 < all (select 2 from tworows)"), "true");

        // Verify that EXISTS works in INSERT and UPDATE.
        s.execute("create table d6408(id int generated by default as identity,"
                + " b boolean not null)");

        // This used to fail with
        // ERROR 23502: Column 'B'  cannot accept a NULL value.
        s.execute("insert into d6408(b) values exists (select * from empty), "
                + "not exists (select * from empty), "
                + "exists (select * from onerow), "
                + "not exists (select * from onerow)");

        JDBC.assertFullResultSet(
                s.executeQuery("select b from d6408 order by id"),
                new String[][] {{"false"}, {"true"}, {"true"}, {"false"}});

        // These used to fail with
        // ERROR 23502: Column 'B'  cannot accept a NULL value.
        s.execute("update d6408 set b = exists (select * from empty)");
        JDBC.assertSingleValueResultSet(
                s.executeQuery("select distinct b from d6408"), "false");
        s.execute("update d6408 set b = not exists (select * from empty)");
        JDBC.assertSingleValueResultSet(
                s.executeQuery("select distinct b from d6408"), "true");

        // These passed even before the fix.
        s.execute("update d6408 set b = exists (select * from onerow)");
        JDBC.assertSingleValueResultSet(
                s.executeQuery("select distinct b from d6408"), "true");
        s.execute("update d6408 set b = not exists (select * from onerow)");
        JDBC.assertSingleValueResultSet(
                s.executeQuery("select distinct b from d6408"), "false");
    }
}