File: DateTimeTest.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 (1870 lines) | stat: -rw-r--r-- 75,406 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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
/*

   Derby - Class org.apache.derbyTesting.functionTests.tests.lang.DateTimeTest

       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.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.TimeZone;
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;

/**
 * Test the builtin date/time types assumes these builtin types 
 * exist: int, smallint, char, varchar, real other things we might test: 
 * interaction with UUID and other user defined types 
 * compatibility with dynamic parameters and JDBC getDate etc. methods.
 */
public final class DateTimeTest extends BaseJDBCTestCase {

    /**
     * Public constructor required for running test as standalone JUnit.
     */
    public DateTimeTest(String name) {
        super(name);
    }

    public static Test suite() {
        BaseTestSuite suite = new BaseTestSuite("DateTimeTest");
        suite.addTest(baseSuite("DateTimeTest:Embedded"));
        suite.addTest(TestConfiguration
                .clientServerDecorator(baseSuite("DateTimeTest:Client")));
        return suite;
    }

    protected static Test baseSuite(String name) {
        BaseTestSuite suite = new BaseTestSuite(name);
        suite.addTestSuite(DateTimeTest.class);
        return new CleanDatabaseTestSetup(suite) {
            /**
             * Creates the tables used in the test cases.
             * @exception SQLException if a database error occurs
             */
            protected void decorateSQL(Statement stmt) throws SQLException {
                createTableForArithmeticTest(stmt);
                createTableForSyntaxTest(stmt);
                createTableForConversionTest(stmt);
                createTableForISOFormatTest(stmt);
            }
        };
    }
    
    private static void createTableForISOFormatTest(Statement st)
    throws SQLException{
        st.executeUpdate(" create table ts (ts1 timestamp, ts2 timestamp)");   
    }
    
    
    private static void createTableForConversionTest(Statement st) throws SQLException{
        st.executeUpdate(
                " create table convtest(d date, t time, ts timestamp)");
        st.executeUpdate(" insert into convtest values(date('1932-03-21'),  "
                + "time('23:49:52'), timestamp('1832-09-24 10:11:43.32'))");
        st.executeUpdate(" insert into convtest values(date('0001-03-21'),  "
                + "time('5:22:59'), timestamp('9999-12-31 23:59:59.999999'))");
        st.executeUpdate(" insert into convtest values(null, null, null)");
    }
    
    private static void createTableForSyntaxTest(Statement stmt) 
    throws SQLException{
        stmt.executeUpdate("create table source (i int, s smallint, c char(10), "
                + "v varchar(50), d double precision, r real, e date, "
                + "t time, p timestamp)");

        stmt.executeUpdate(" create table target (e date not null, t time not "
                + "null, p timestamp not null)");
    }
        
    private static void createTableForArithmeticTest(Statement stmt)
    throws SQLException{
        stmt.executeUpdate("create table t (i int, s smallint, " +
                "c char(10), v varchar(50), d double precision," +
                " r real, e date, t time, p timestamp)");

        stmt.executeUpdate(" insert into t values (null, null, " +
                "null, null, null, null, null, null, null)");

        stmt.executeUpdate(" insert into t values (0, 100, 'hello', " +
                "'everyone is here', 200.0e0, 300.0e0, " +
                "date('1992-01-01'), time('12:30:30'), " +
                "timestamp('1992-01-01 12:30:30'))");

        stmt.executeUpdate(" insert into t values (-1, -100, " +
                "'goodbye', 'everyone is there', -200.0e0, " +
                "-300.0e0, date('1992-01-01'), time('12:30:30'), " +
                "timestamp('1992-01-01 12:30:45'))");
    }
    
    /**
     * date/times don't support math, show each combination.
     */
    public void testArithOpers_math() throws SQLException{
        Statement st = createStatement();
        
        assertStatementError("42Y95", st, "select e + e from t");

        assertStatementError("42Y95", st, " select i + e from t");

        assertStatementError("42Y95", st, " select p / p from t");

        assertStatementError("42Y95", st, " select p * s from t");

        assertStatementError("42Y95", st, " select t - t from t");

        assertStatementError("42X37", st, " select -t from t");

        assertStatementError("42X37", st, " select +e from t");
        
        st.close();
    }
    
    public void testArithOpers_Comarision() throws SQLException{
        ResultSet rs = null;
        Statement st = createStatement();

        rs = st.executeQuery("select e from t where e = date('1992-01-01')");
        JDBC.assertColumnNames(rs, new String[] { "E" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1992-01-01" }, { "1992-01-01" } }, true);

        rs = st.executeQuery(" select e from t where date('1992-01-01') = e");
        JDBC.assertColumnNames(rs, new String[] { "E" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1992-01-01" }, { "1992-01-01" } }, true);

        rs = st.executeQuery(" select t from t where t > time('09:30:15')");
        JDBC.assertColumnNames(rs, new String[] { "T" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "12:30:30" }, { "12:30:30" } }, true);

        rs = st.executeQuery(" select t from t where time('09:30:15') < t");
        JDBC.assertColumnNames(rs, new String[] { "T" });
        JDBC.assertFullResultSet(rs,
                new String[][] { { "12:30:30" }, { "12:30:30" } }, true);

        rs = st.executeQuery(
                "select p from t where p < timestamp('1997-06-30 01:01:01')");
        JDBC.assertColumnNames(rs, new String[] { "P" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1992-01-01 12:30:30.0" },
                { "1992-01-01 12:30:45.0" } }, true);

        rs = st.executeQuery(
                "select p from t where timestamp('1997-06-30 01:01:01' )> p");
        JDBC.assertColumnNames(rs, new String[] { "P" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1992-01-01 12:30:30.0" },
                { "1992-01-01 12:30:45.0" } }, true);
        
        rs = st.executeQuery("select e from t where e >= date('1990-01-01')");
        JDBC.assertColumnNames(rs, new String[] { "E" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1992-01-01" }, { "1992-01-01" } }, true);

        rs = st.executeQuery(" select e from t where date('1990-01-01')<= e");
        JDBC.assertColumnNames(rs, new String[] { "E" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1992-01-01" }, { "1992-01-01" } }, true);

        rs = st.executeQuery(" select t from t where t <= time('09:30:15')");
        JDBC.assertColumnNames(rs, new String[] { "T" });
        JDBC.assertDrainResults(rs, 0);

        rs = st.executeQuery(" select t from t where time('09:30:15') >= t");
        JDBC.assertColumnNames(rs, new String[] { "T" });
        JDBC.assertDrainResults(rs, 0);

        rs = st.executeQuery(
                "select p from t where p <> timestamp('1997-06-30 01:01:01')");
        JDBC.assertColumnNames(rs, new String[] { "P" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1992-01-01 12:30:30.0" },
                { "1992-01-01 12:30:45.0" } }, true);

        rs = st.executeQuery(
                "select p from t where timestamp('1997-06-30 01:01:01' )<> p");
        JDBC.assertColumnNames(rs, new String[] { "P" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1992-01-01 12:30:30.0" },
                { "1992-01-01 12:30:45.0" } }, true);
        
        st.close();
    }
    
    /**
     * Show comparisons with mixed types don't work.
     */
    public void testArithOpers_CompraionOnMixedTypes() throws SQLException{
        Statement st = createStatement();
        
        assertStatementError("42818", st, "select e from t where e <= i");

        assertStatementError("42818", st, " select e from t where t < s");

        assertStatementError("42818", st, " select e from t where p > d");

        assertStatementError("42818", st, " select e from t where e >= t");

        assertStatementError("42818", st, " select e from t where t <> p");

        assertStatementError("42818", st, " select e from t where p = e");

        st.close();
    }
    
    /**
     * Look for a value that isn't in the table.
     */
    public void testArithOpers_CompraionOnNotExistingValue() 
    throws SQLException{
        ResultSet rs = null;
        Statement st = createStatement();
        
        rs = st.executeQuery("select e from t where e <> date('1992-01-01')");
        JDBC.assertColumnNames(rs, new String[] { "E" });
        JDBC.assertDrainResults(rs, 0);

        rs = st.executeQuery("select e from t where date('1992-01-01') <> e");
        JDBC.assertColumnNames(rs, new String[] { "E" });
        JDBC.assertDrainResults(rs, 0);

        st.close();
    }
    
    /**
     * Show garbage in == errors out
     */
    public void testArithOpers_ComparisonOnGarbage() throws SQLException {
        Statement st = createStatement();
        
        assertStatementError("22008", st, 
                "select date( 'xxxx') from t where p is null");

        assertStatementError("22007", st, 
                " select time( '') from t where p is null");

        assertStatementError("22008", st,
                " select timestamp( 'is there anything here?' )from " +
                "t where p is null");

        assertStatementError("22008", st,
                " select timestamp( '1992-01- there anything here?')" +
                "from t where p is null");

        assertStatementError("22008", st,
                " select timestamp( '--::' )from t where p is null");

        assertStatementError("22007", st, 
                " select time('::::') from t where p is null");
        
        st.close();
    }
    
    /**
     * Check limit values.
     */
    public void testArithOpers_ComparisonOnLimits() throws SQLException {
        ResultSet rs = null;
        Statement st = createStatement();

        rs = st.executeQuery("values( date('0001-1-1'), date('9999-12-31'), "
                + "date('2/29/2000'), date('29.2.2004'))");
        JDBC.assertColumnNames(rs, new String[] { "1", "2", "3", "4" });
        JDBC.assertFullResultSet(rs, new String[][] { { "0001-01-01", 
                "9999-12-31", "2000-02-29", "2004-02-29" } }, true);

        rs = st.executeQuery(" values( time('00:00:00'), time('23:59:59'))");
        JDBC.assertColumnNames(rs, new String[] { "1", "2" });
        JDBC.assertFullResultSet(rs,
                new String[][] { { "00:00:00", "23:59:59" } }, true);

        rs = st.executeQuery(" values( time('00 AM'), time( '12:59 AM')," +
        		" time('1 PM'), time('12:59 PM'))");
        JDBC.assertColumnNames(rs, new String[] { "1", "2", "3", "4" });
        JDBC.assertFullResultSet(rs, new String[][] { { "00:00:00", 
                "00:59:00", "13:00:00", "12:59:00" } }, true);

        rs = st.executeQuery(" values( time('00.00.00'), time('23.59.59'), " +
                "time('24.00.00'))");
        JDBC.assertColumnNames(rs, new String[] { "1", "2", "3" });
        JDBC.assertFullResultSet(rs, new String[][] { { "00:00:00", 
                "23:59:59", "00:00:00" } }, true);

        rs = st.executeQuery(" values( timestamp('0001-1-1 00:00:00'), "
                + "timestamp('9999-12-31 23:59:59.999999'))");
        JDBC.assertColumnNames(rs, new String[] { "1", "2" });
        JDBC.assertFullResultSet(rs, new String[][] { { 
            "0001-01-01 00:00:00.0", "9999-12-31 23:59:59.999999" } }, true);
        
        st.close();
    }
    
    /**
     * Show that overflow and underflow are not allowed
     *  (SQL92 would have these report errors).
     */
    public void testArithOpers_ComparisonOnBeyondLimits() throws SQLException {
        Statement st = createStatement();
        
        assertStatementError("22008", st , "values( date('0000-01-01'))");

        assertStatementError("22008", st, " values( date('2000-00-01'))");

        assertStatementError("22008", st, " values( date('2000-01-00'))");

        assertStatementError("22008", st, " values( date('10000-01-01'))");

        assertStatementError("22008", st, " values( date('2000-13-01'))");

        assertStatementError("22008", st, " values( date('2000-01-32'))");

        assertStatementError("22008", st, " values( date('1900-02-29'))");

        assertStatementError("22008", st, " values( date('2001-02-29'))");

        assertStatementError("22007", st, " values( time('25.00.00'))");

        assertStatementError("22007", st, " values( time('24.00.01'))");

        assertStatementError("22007", st, " values( time('0:60:00'))");

        assertStatementError("22007", st, " values( time('00:00:60'))");

        st.close();
    }
    
    public void testArithOpers_ComparisonOnNullAndNonNull() 
    throws SQLException {
        ResultSet rs = null;
        Statement st = createStatement();
        
        rs = st.executeQuery("select e, t, p from t " +
        		"where e = e or t = t or p = p");
        JDBC.assertColumnNames(rs, new String[] { "E", "T", "P" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1992-01-01", 
                "12:30:30", "1992-01-01 12:30:30.0" }, { "1992-01-01",
                "12:30:30", "1992-01-01 12:30:45.0" } }, true);
        
        rs = st.executeQuery("select * from t where e is not null " +
        		"and t is not " + "null and p is not null");
        JDBC.assertColumnNames(rs, new String[] { "I", "S", "C", "V",
                "D", "R", "E", "T", "P" });
        JDBC.assertFullResultSet(rs,  new String[][] {
                { "0", "100", "hello", "everyone is here", "200.0", "300.0",
                    "1992-01-01", "12:30:30", "1992-01-01 12:30:30.0" },
                { "-1", "-100", "goodbye", "everyone is there", "-200.0",
                    "-300.0", "1992-01-01", "12:30:30", 
                    "1992-01-01 12:30:45.0" } }, true);
        
        st.close();
    }

    /**
     * Test =SQ .
     */
    public void testArithOpers_ComparisonOnEqualSQ() throws SQLException{
        ResultSet rs = null;
        Statement st = createStatement();

        assertStatementError("21000", st,
                "select 'fail' from t where e = (select e from t)");

        rs = st.executeQuery("select 'pass' from t " +
        		"where e = (select e from t where d=200)");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs,
                new String[][] { { "pass" }, { "pass" } }, true);

        assertStatementError("21000", st,
                "select 'fail' from t where t = (select t from t)");

        rs = st.executeQuery("select 'pass' from t " +
        		"where t = (select t from t where d=200)");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "pass" }, { "pass" } }, true);

        assertStatementError("21000", st,
                "select 'fail' from t where p = (select p from t)");

        rs = st.executeQuery("select 'pass' from t " +
        		"where p = (select p from t where d=200)");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "pass" } }, true);

        st.close();
    }
    
    /**
     * Test syntax: precision cannot be specified.
     */
    public void testSyntax_SpecifiedPrecision() throws SQLException{        
        Statement st = createStatement();
        
        assertStatementError("42X01", st , 
                "create table wrong (t time(-100))");

        assertStatementError("42X01", st, 
                " create table wrong (t time(0))");

        assertStatementError("42X01", st, 
                " create table wrong (t time(23))");

        assertStatementError("42X01", st,
                " create table wrong (t timestamp(-100))");

        assertStatementError("42X01", st,
                " create table wrong (t timestamp(0))");

        assertStatementError("42X01", st,
                " create table wrong (t timestamp(6))");

        assertStatementError("42X01", st,
                " create table wrong (t timestamp(9))");

        assertStatementError("42X01", st,
                " create table wrong (t timestamp(23))");
        
        st.close();
    }
    

    /**
     * Test a variety of inserts.
     */
    public void testSyntax_Insert() throws SQLException{
        Statement st = createStatement();
        
        st.executeUpdate("insert into source values (1, 2, '3', '4', 5, 6, "
                + "date('1997-07-07'), "
                + "time('08:08:08'),timestamp('1999-09-09 09:09:09'))");
        
        st.executeUpdate("insert into target select e,t,p from source");
        
        //wrong columns should fail
        assertStatementError("42821", st,
                "insert into target select p,e,t from source");
        
        assertStatementError("42821", st,
        " insert into target select i,s,d from source");

        assertStatementError("42821", st,
            " insert into target (t,p) select c,r from source");

        assertUpdateCount(st, 1, " delete from source");
        
        
        st.executeUpdate(" insert into source values (null, null, null, null, "
                + "null, null, null, null, null)");

        // these fail because the target won't take a null -- of any type
        assertStatementError("23502", st,
                "insert into target values(null, null, null)");

        assertStatementError("23502", st,
                " insert into target select e,t,p from source");
        
        //these still fail with type errors:
        assertStatementError("42821", st,
                "insert into target select p,e,t from source");

        assertStatementError("42821", st,
                " insert into target select i,s,d from source");

        assertStatementError("42821", st,
                " insert into target (t,p)select c,r from source");

        //expect 1 row in target.
        ResultSet rs = st.executeQuery("select * from target");
        JDBC.assertColumnNames(rs, new String[] { "E", "T", "P" });
        JDBC.assertFullResultSet(rs, new String[][] { {
                "1997-07-07", "08:08:08", "1999-09-09 09:09:09.0" } }, true);
        
        st.close();
    }
    
    /**
     * Test a variety of updates.
     */
    public void testSyntax_Update() throws SQLException{
        Statement st = createStatement();
        
        //expect 1 row in target.
        ResultSet rs = st.executeQuery("select * from target");
        JDBC.assertColumnNames(rs, new String[] { "E", "T", "P" });
        JDBC.assertFullResultSet(rs, new String[][] { {
                "1997-07-07", "08:08:08", "1999-09-09 09:09:09.0" } }, true);
        
        // unchanged:
        assertUpdateCount(st, 1, "update target set e = e, t = t, p = p");
        rs = st.executeQuery(" select * from target");
        JDBC.assertColumnNames(rs, new String[] { "E", "T", "P" });
        JDBC.assertFullResultSet(rs, new String[][] { { 
                "1997-07-07", "08:08:08", "1999-09-09 09:09:09.0" } }, true);

        // alters the row:
        assertUpdateCount(st, 1, "update target set e = date('1990-01-01')");
        rs = st.executeQuery(" select * from target");
        JDBC.assertColumnNames(rs, new String[] { "E", "T", "P" });
        JDBC.assertFullResultSet(rs, new String[][] { {
                "1990-01-01", "08:08:08", "1999-09-09 09:09:09.0" } }, true);
        
        //not settable to null
        assertStatementError("23502", st, "update target set e = null");
        rs = st.executeQuery(" select * from target");
        JDBC.assertColumnNames(rs, new String[] { "E", "T", "P" });
        JDBC.assertFullResultSet(rs, new String[][] { { 
                "1990-01-01", "08:08:08", "1999-09-09 09:09:09.0" } }, true);
        
        // nullable col can be set to null:
        assertUpdateCount(st, 1, "update source set e = date('1492-10-01')");
        rs = st.executeQuery(" select e from source");
        JDBC.assertColumnNames(rs, new String[] { "E" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1492-10-01" } }, true);

        assertUpdateCount(st, 1, " update source set e = null");
        rs = st.executeQuery(" select e from source");
        JDBC.assertColumnNames(rs,  new String[] { "E" });
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);
        
        // these should get type errors
        assertStatementError("42821", st, "update target set e = 1");
        assertStatementError("42821", st, " update source set p = 1.4e10");
        assertStatementError("42821", st,
                " update source set i = date('1001-01-01')");
        
        st.close();
    }
    
    public void testSyntax_CurrentFunctions() throws SQLException{
        ResultSet rs = null;
        Statement st = createStatement();
        
        // tests with current functions:
        assertUpdateCount(st, 1, "delete from source");
        assertUpdateCount(st, 1, " delete from target");

        st.executeUpdate(" insert into source values (1, 2, '3', " +
        		"'4', 5, 6, date('1997-06-07'), time('08:08:08'), " +
        		"timestamp('9999-09-09 09:09:09'))");

        // these tests are 'funny' so that the masters won't show 
        // a diff every time.
        rs = st.executeQuery("select 'pass' from source where current_date = "
                + "current_date and current_time = current_time and "
                + "current_timestamp = current_timestamp");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "pass" } }, true);

        rs = st.executeQuery(" select 'pass' from source where current_date > "
                + "date('1996-12-31') and current_time <= "
                + "time(        '23:59:59') -- may oopsie on leap second days "
                + "and current_timestamp <> timestamp( -- this comment "
                + "is just more whitespace '1996-12-31 00:00:00')");
        JDBC.assertColumnNames(rs, new String[] { "1" });        
        JDBC.assertFullResultSet(rs, new String[][] { { "pass" } }, true);

        // test with DB2 compatible syntax
        rs = st.executeQuery("select 'pass' from source where current date = "
                + "current date and current time = current time and "
                + "current timestamp = current timestamp");
        JDBC.assertFullResultSet(rs, new String[][] { { "pass" } }, true);

        rs = st.executeQuery(" select 'pass' from source where current date > "
                + "date('1996-12-31') and current time <= "
                + "time(        '23:59:59') -- may oopsie on leap second days "
                + "and current timestamp <> timestamp( -- this comment "
                + "is just more whitespace '1996-12-31 00:00:00')");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "pass" } }, true);
    }
    
    public void testSyntax_EscapedFunctions() throws SQLException{
        ResultSet rs = null;
        Statement st = createStatement();
        
        //CURRENT_DATE escaped function not supported in DB2 UDB 
        //CURRENT_TIME escaped function not supported in DB2 UDB
        assertStatementError("42X01", st, "select 'pass' from source " +
        		"where current_date = {fn current_date()} " +
        		"and current_time = {fn current_time()} " +
        		"and current_timestamp = current_timestamp");

        rs = st.executeQuery(" select 'pass' from source " +
        		"where current_date = {fn curdate()} " +
        		"and current_time = {fn curtime()} " +
        		"and current_timestamp = current_timestamp");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "pass" } }, true);

        
        // current_date() and current_time() not valid in DB2. 
        // curdate() and curtime() are as escaped functions only.
        assertStatementError("42Y03", st, "values curdate()");

        assertStatementError("42Y03", st, " values curtime()");

        assertStatementError("42X01", st, " values current_date()");

        assertStatementError("42X01", st, " values current_time()");

        assertStatementError("42X01", st, " values {fn current_date()}");

        assertStatementError("42X01", st, " values {fn current_time()}");

        
        // DB2 UDB compatible test for escaped functions
        rs = st.executeQuery("select 'pass' from source " +
        		"where hour(current_time) = {fn hour(current_time)} " +
        		"and minute(current_time) = {fn minute(current_time)}" +
        		" and second(current_time) = {fn second(current_time)} " +
        		"and year(current_date)   = {fn year(current_date)}");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "pass" } }, true);

        
        // valid jdbc date and time escaped functions
        rs = st.executeQuery("values {fn hour('23:38:10')}");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "23" } }, true);

        rs = st.executeQuery(" values {fn minute('23:38:10')}");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "38" } }, true);

        rs = st.executeQuery(" values {fn second('23:38:10')}");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "10" } }, true);

        rs = st.executeQuery(" values {fn year('2004-03-22')}");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "2004" } }, true);

        
        // currents do have types, these inserts fail:
        assertStatementError("42821", st,
                "insert into source values (0, 0, '0', '0', 0, 0, "
                        + "current_time, current_time, current_timestamp)");

        assertStatementError("42821", st, " insert into source values" +
        		" (0, 0, '0', '0', 0, 0, current_date, " +
        		"current_timestamp, current_timestamp)");

        assertStatementError("42821", st,
                " insert into source values (0, 0, '0', '0', 0, 0, "
                        + "current_date, current_time, current_date)");

        // this insert works
        st.executeUpdate("insert into source values (0, 0, '0', '0', 0, 0, "
                + "current_date, current_time, current_timestamp)");

        // test with DB2 syntax this insert works
        st.executeUpdate("insert into source values (0, 0, '0', '0', 0, 0, "
                + "current date, current time, current timestamp)");

        // this test will diff if the select is run just after 
        // midnight, and the insert above was run just before 
        // midnight...
        rs = st.executeQuery("select * from source " +
        		"where e <> current_date and p <> current_timestamp");
        JDBC.assertColumnNames(rs, new String[] { "I", "S", "C", "V", "D", 
                "R", "E", "T", "P" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1", "2", 
            "3", "4", "5.0", "6.0", "1997-06-07", "08:08:08",
            "9999-09-09 09:09:09.0" } }, true);

        
        // test with DB2 syntax
        rs = st.executeQuery("select * from source " +
        		"where e <> current date and p <> current timestamp");
        JDBC.assertColumnNames(rs, new String[] { "I", "S", "C", "V", 
                "D", "R", "E", "T", "P" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1", "2", "3",
            "4", "5.0", "6.0", "1997-06-07", "08:08:08", 
            "9999-09-09 09:09:09.0" } }, true);

        rs = st.executeQuery(" select 'pass' from source " +
        		"where e <= current_date and p <= current_timestamp");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "pass" }, { "pass" } }, true);

        // reduce it back to one row
        assertUpdateCount(st, 2, "delete from source where i=0");
        
        st.close();
    }
    
    public void testSyntax_Extract() throws SQLException{
        Statement st = createStatement();
        
        ResultSet rs = st.executeQuery("select year( e), month( e), day( date( "
                + "'1997-01-15')), hour( t), minute( t), second( time( "
                + "'01:01:42')), year( p), month( p), day( p), hour( "
                + "timestamp( '1992-01-01 14:11:23')), minute( p), "
                + "second( p) from source");
        JDBC.assertColumnNames(rs, new String[] { "1", "2", "3", "4", 
                "5", "6", "7", "8", "9", "10", "11", "12" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1997", "6", "15", 
            "8", "8", "42", "9999", "9", "9", "14", "9", "9.0" } }, true);

        // extract won't work on other types
        assertStatementError("42X25", st, "select month( i) from source");

        assertStatementError("42X25", st, " select hour( d) from source");

        // extract won't work on certain field/type combos
        assertStatementError("42X25", st, "select month( t) from source");

        assertStatementError("42X25", st, " select day( t) from source");

        assertStatementError("42X25", st, " select year( t) from source");

        assertStatementError("42X25", st, " select hour( e) from source");

        assertStatementError("42X25", st, " select minute( e) from source");

        assertStatementError("42X25", st, " select second( e) from source");

        assertUpdateCount(st, 1,
                " update source set i=month( e), s=minute( t), d=second( p)");

        // should be true and atomics should match field named as 
        // label in date/times
        rs = st.executeQuery("select i,e as \"month\",s,t " +
                "as \"minute\",d,p as \"second\" from source " +
                "where (i = month(e)) and (s = minute(t)) " +
                "and (d = second(p))");
        JDBC.assertColumnNames(rs, new String[] { "I", "month", 
                "S", "minute", "D", "second" });
        JDBC.assertFullResultSet(rs, new String[][] { { "6", "1997-06-07", 
                "8", "08:08:08", "9.0", "9999-09-09 09:09:09.0" } }, true);

        // fields should match the fields in the date (in order)
        rs = st.executeQuery("select p, year( p) as \"year\", month( p) as "
                + "\"month\", day( p) as \"day\", hour( p) as "
                + "\"hour\", minute( p) as \"minute\", second( p) as "
                + "\"second\" from source");
        JDBC.assertColumnNames(rs, new String[] { "P", "year", "month", 
                "day", "hour", "minute", "second" });
        JDBC.assertFullResultSet(rs, new String[][] { { 
            "9999-09-09 09:09:09.0", "9999", "9", "9",
            "9", "9", "9.0" } }, true);

        
        // jdbc escape sequences
        rs = st.executeQuery("values ({d '1999-01-12'}, {t '11:26:35'}, {ts "
                + "'1999-01-12 11:26:51'})");
        JDBC.assertColumnNames(rs, new String[] { "1", "2", "3" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1999-01-12", 
                "11:26:35", "1999-01-12 11:26:51.0" } }, true);

        rs = st.executeQuery(" values year( {d '1999-01-12'})");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1999" } }, true);

        rs = st.executeQuery(" values hour( {t '11:28:10'})");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "11" } }, true);

        rs = st.executeQuery(" values day( {ts '1999-01-12 11:28:23'})");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "12" } }, true);
        
        st.close();
    }
    
    /**
     * Random tests for date.
     */
    public void testRandom() throws SQLException{        
        Statement st = createStatement();
        
        st.executeUpdate("create table sertest(d date, s Date, o Date)");
        st.executeUpdate(" insert into sertest values (date('1992-01-03'), " +
        		"null, null)");
        
        ResultSet rs = st.executeQuery(" select * from sertest");
        JDBC.assertColumnNames(rs, new String[] { "D", "S", "O" });
        JDBC.assertFullResultSet(rs,
                new String[][] { { "1992-01-03", null, null } }, true);
        
        assertUpdateCount(st, 1, " update sertest set s=d");
        assertUpdateCount(st, 1, " update sertest set o=d");
        st.executeUpdate(" insert into sertest values (date( '3245-09-09'), "
                + "date( '1001-06-07'), date( '1999-01-05'))");
        
        rs = st.executeQuery(" select * from sertest");
        JDBC.assertColumnNames(rs, new String[] { "D", "S", "O" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1992-01-03", 
                "1992-01-03", "1992-01-03" }, { "3245-09-09", "1001-06-07", 
                    "1999-01-05" } }, true);

        rs = st.executeQuery(" select * from sertest where d > s");
        JDBC.assertColumnNames(rs, new String[] { "D", "S", "O" });
        JDBC.assertFullResultSet(rs, new String[][] { { "3245-09-09", 
                "1001-06-07", "1999-01-05" } }, true);

        assertUpdateCount(st, 2, " update sertest set d=s");

        // should get type errors:
        assertStatementError("42821", st,
                "insert into sertest values (date('3245-09-09'), "
                        + "time('09:30:25'), null)");

        assertStatementError("42821", st,
                " insert into sertest values (null, null, time('09:30:25'))");

        assertStatementError("42821", st,
                " insert into sertest values (null, null, "
                        + "timestamp('1745-01-01 09:30:25'))");

        assertUpdateCount(st, 2, "update sertest set d=o");

        rs = st.executeQuery(" select * from sertest where s is null " +
        		"and o is not null");
        JDBC.assertColumnNames(rs, new String[] { "D", "S", "O" });
        JDBC.assertDrainResults(rs, 0);

        rs = st.executeQuery("select month(s) from sertest " +
        		"where s is not null");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1" }, { "6" } },
                true);

        rs = st.executeQuery(" select day(o) from sertest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "3" }, { "5" } }, 
                true);

        dropTable("sertest");
    }
    
    public void testConvertFromString() throws SQLException{        
        Statement st = createStatement();
        
        st.executeUpdate("create table convstrtest(d varchar(30), t char(30), "
                + "ts long varchar)");
        st.executeUpdate(" insert into convstrtest values('1932-03-21',  "
                + "'23:49:52', '1832-09-24 10:11:43.32')");
        st.executeUpdate(" insert into convstrtest values(null, null, null)");

        assertStatementError("22007", st,
                "select CAST (d AS time) from convstrtest");

        assertStatementError("22007", st,
                " select CAST (t AS date) from convstrtest");

        assertStatementError("42846", st,
                " select CAST (ts AS time) from convstrtest");

        assertStatementError("42846", st,
                " select CAST (ts AS date) from convstrtest");


        ResultSet rs = st.executeQuery("select CAST (t AS time) from convstrtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "23:49:52" }, 
                { null } }, true);

        rs = st.executeQuery(" select CAST (d AS date) from convstrtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1932-03-21" }, { null } }, true);

        // show time and date separately as timestamp will be 
        // filtered out
        assertStatementError("42846", st, "select " +
                "CAST(CAST (ts AS timestamp) AS date), " +
                "CAST(CAST (ts AS timestamp) AS time) from convstrtest");

        dropTable("convstrtest");
        
        st.close();
    }

    /**
     * Test that conversion from timestamp to string is correct when a
     * calendar is specified. Specifically, verify that the full nanosecond
     * resolution is used and that the converted timestamp is not rounded to
     * microsecond resolution. Regression test case for DERBY-4626.
     */
    public void testConvertToStringWithCalendar() throws SQLException {
        PreparedStatement ps =
                prepareStatement("values cast(? as varchar(29))");

        // Generate a timestamp representing 2010-09-01 20:31:40.123456789 GMT
        Timestamp ts = new Timestamp(1283373100000L);
        ts.setNanos(123456789);

        // Array of (timezone, timestamp string) pairs representing a timezone
        // with which to test and the expected timestamp string produced.
        String[][] testData = {
            { "GMT", "2010-09-01 20:31:40.123456789" },
            { "Europe/Oslo", "2010-09-01 22:31:40.123456789" },
        };

        for (int i = 0; i < testData.length; i++) {
            Calendar cal = Calendar.getInstance(
                    TimeZone.getTimeZone(testData[i][0]));
            ps.setTimestamp(1, ts, cal);
            JDBC.assertSingleValueResultSet(ps.executeQuery(), testData[i][1]);
        }
    }

    /**
     * Regression test case for DERBY-4621, which caused the conversion of
     * timestamp and time values to varchar to generate wrong results when
     * a Calendar object was supplied.
     */
    public void testConversionToString() throws SQLException {
        String timestampString = "2010-04-20 15:17:36.0";
        String timeString = "15:17:36";
        String dateString = "2010-04-20";

        Timestamp ts = Timestamp.valueOf(timestampString);
        Time t = Time.valueOf(timeString);
        Date d = Date.valueOf(dateString);

        PreparedStatement ps =
                prepareStatement("VALUES CAST(? AS VARCHAR(40))");

        ps.setTimestamp(1, ts);
        JDBC.assertSingleValueResultSet(ps.executeQuery(), timestampString);

        // Used to give wrong result - 2010-04-20 03:17:36
        ps.setTimestamp(1, ts, Calendar.getInstance());
        JDBC.assertSingleValueResultSet(ps.executeQuery(), timestampString);

        ps.setTime(1, t);
        JDBC.assertSingleValueResultSet(ps.executeQuery(), timeString);

        // Used to give wrong result - 03:17:36
        ps.setTime(1, t, Calendar.getInstance());
        JDBC.assertSingleValueResultSet(ps.executeQuery(), timeString);

        ps.setDate(1, d);
        JDBC.assertSingleValueResultSet(ps.executeQuery(), dateString);

        ps.setDate(1, d, Calendar.getInstance());
        JDBC.assertSingleValueResultSet(ps.executeQuery(), dateString);
    }

    /**
     * Test that trailing zeros in the nanoseconds component of a timestamp
     * are handled the same way by
     * {@code PreparedStatement.setTimestamp(int,Timestamp)} and
     * {@code PreparedStatement.setTimestamp(int,Timestamp, Calendar)}
     * when converting the timestamp to a VARCHAR. (DERBY-4810)
     */
    public void testTrailingZeros() throws SQLException {
        PreparedStatement ps =
                prepareStatement("values cast(? as varchar(29))");

        String[] tsStrings = {
            "2010-09-22 14:40:33.000000000",
            "2010-09-22 14:40:33.012000000",
            "2010-09-22 14:40:33.123456000",
            "2010-09-22 14:40:33.139990900",
            "2010-09-22 14:40:33.139990983",
        };

        for (int i = 0; i < tsStrings.length; i++) {
            Timestamp ts = Timestamp.valueOf(tsStrings[i]);

            // We expect the converted value to have the same format as
            // what Timestamp.toString() returns.
            String expected = ts.toString();

            ps.setTimestamp(1, ts);
            JDBC.assertSingleValueResultSet(ps.executeQuery(), expected);

            ps.setTimestamp(1, ts, Calendar.getInstance());
            JDBC.assertSingleValueResultSet(ps.executeQuery(), expected);
        }
    }

    public void testConversion_Aggregates() throws SQLException{
        Statement st = createStatement();
        
        //test aggregates sum should fail
        assertStatementError("42Y22", st, "select sum(d) from convtest");

        assertStatementError("42Y22", st, " select sum(t) from convtest");

        assertStatementError("42Y22", st, " select sum(ts) from convtest");

        // these should work
        ResultSet rs = st.executeQuery("select count(d) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "2" }, }, true);

        rs = st.executeQuery(" select count(t) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "2" }, }, true);

        rs = st.executeQuery(" select count(ts) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "2" }, }, true);

        st.executeUpdate(" insert into convtest values(date('0001-03-21'),  "
                + "time('5:22:59'), timestamp('9999-12-31 23:59:59.999999'))");

        // distinct count should be 2 not 3
        rs = st.executeQuery("select count(distinct d) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "2" }, }, true);

        rs = st.executeQuery(" select count(distinct t) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "2" }, }, true);

        rs = st.executeQuery(" select count(distinct ts) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "2" }, }, true);

        // min should not be null!!!!!!!!
        rs = st.executeQuery("select min(d) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "0001-03-21" }, }, 
                true);

        rs = st.executeQuery(" select min(t) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "05:22:59" }, }, true);

        // show time and date separately as timestamp will be 
        // filtered out
        rs = st.executeQuery("select " +
                "CAST(CAST (min(ts) AS timestamp) AS date), " +
                "CAST(CAST (min(ts) AS timestamp) AS time) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1", "2" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1832-09-24", 
            "10:11:43" }, }, true);

        rs = st.executeQuery(" select max(d) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1932-03-21" }, }, true);

        rs = st.executeQuery(" select max(t) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs,
                new String[][] { { "23:49:52" }, }, true);

        // show time and date separately as timestamp will be 
        // filtered out
        rs = st.executeQuery("select " +
                "CAST(CAST (max(ts) AS timestamp) AS date), " +
                "CAST(CAST (max(ts) AS timestamp) AS time) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1", "2" });
        JDBC.assertFullResultSet(rs,
                new String[][] { { "9999-12-31", "23:59:59" }, }, true);
        
        //just to recover the test environment
        dropTable("convtest");
        createTableForConversionTest(st);

        st.close();
    }
    
    public void testConversion() throws SQLException{        
        Statement st = createStatement();
        
        // these should fail
        assertStatementError("42846", st, 
                "select CAST (d AS time) from convtest");

        assertStatementError("42846", st,
                " select CAST (t AS date) from convtest");

        // these should work
        ResultSet rs = st.executeQuery(
                "select CAST (t AS time) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "23:49:52" }, 
                { "05:22:59" }, { null } }, true);

        rs = st.executeQuery(" select CAST (d AS date) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1932-03-21" },
                { "0001-03-21" }, { null } }, true);

        rs = st.executeQuery(" select CAST (ts AS time) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "10:11:43" }, 
                { "23:59:59" }, { null } }, true);

        rs = st.executeQuery(" select CAST (ts AS date) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1832-09-24" }, 
                { "9999-12-31" }, { null } }, true);

        // show time and date separately as timestamp will be 
        // filtered out
        rs = st.executeQuery("select CAST(CAST (ts AS timestamp) AS date), " +
        		"CAST(CAST (ts AS timestamp) AS time) from convtest");
        JDBC.assertColumnNames(rs, new String[] { "1", "2" });

        JDBC.assertFullResultSet(rs, new String[][] { { "1832-09-24", 
                "10:11:43" }, { "9999-12-31", "23:59:59" }, 
                { null, null } }, true);

        // casting from a time to a timestamp sets the date to 
        // current date
        assertStatementError("42846", st, "select 'pass', " +
        		"CAST (CAST(t AS timestamp) AS time) from convtest " +
        		"where CAST(CAST(t AS timestamp) AS date)=current_date");

        // time should be 0
        assertStatementError("42846", st, "select " +
        		"CAST (CAST (d AS timestamp) AS date), " +
        		"CAST(CAST(d AS timestamp) AS time) from convtest");
        
        st.close();
    }
    
    /**
     * leading zeros may be omitted from the month, 
     * day and part of the timestamp.
     */
    public void testISOFormat_OmitLeadingZero() throws SQLException{
        Statement st = createStatement();

        assertEquals(1, st.executeUpdate(
                "insert into ts values ('2003-03-05-17.05.43.111111', " +
                "'2003-03-05 17:05:43.111111')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2003-3-03-17.05.43.111111', '2003-3-03 17:05:43.111111')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2003-3-2-17.05.43.111111', '2003-3-2 17:05:43.111111')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2003-03-2-17.05.43.111111', '2003-03-2 17:05:43.111111')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2003-3-1-17.05.43.1', '2003-3-1 17:05:43.1')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2003-3-1-17.05.43.12', '2003-3-1 17:05:43.12')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2003-3-1-17.05.43.123', '2003-3-1 17:05:43.123')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2003-3-1-17.05.43.1234', '2003-3-1 17:05:43.1234')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2003-3-1-17.05.43.12345', '2003-3-1 17:05:43.12345')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2003-3-1-17.05.43.123456', '2003-3-1 17:05:43.123456')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2003-3-1-17.05.43', '2003-3-1 17:05:43')"));
        
        st.close();
    }    
    
    /**
     *Trailing blanks are allowed.
     */
    public void testISOFormat_TrailingBlanks() throws SQLException{
        Statement st = createStatement();
        
        assertEquals(1, st.executeUpdate("insert into ts values " +
        		"('2002-03-05-17.05.43.111111  ', " +
        		"'2002-03-05 17:05:43.111111   ')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2002-03-05-17.05.43.1   ', '2002-03-05 17:05:43.1   ')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2002-03-05-17.05.43    ', '2002-03-05 17:05:43    ')"));
        
        st.close();
    }
    
    /**
     *  UDB allows this by "appending a zero"; so, cloudscape follows.
     */
    public void testISOFormat_TrailingZero() throws SQLException{
        Statement st = createStatement();
        
        assertEquals(1, st.executeUpdate("insert into ts values " +
        		"('2003-3-1-17.05.43.', '2003-3-1 17:05:43')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('2003-3-1-17.05.43.0', '2003-3-1 17:05:43.0')"));

        assertEquals(1, st.executeUpdate(" insert into ts values " +
        		"('0003-03-05-17.05.43.111111'," +
        		" '0003-03-05 17:05:43.111111')"));
        
        ResultSet rs = st.executeQuery(" select count(*) from ts " +
        		"where ts1=ts2");
        rs.next();
        int rows = rs.getInt(1);
        rs = st.executeQuery(" select count(*) from ts ");
        rs.next();
        assertEquals(rows, rs.getInt(1));
        
        st.executeUpdate(" delete from ts");
        
        st.close();
    }
    
    /**
     * Should be rejected because leading zero in year is missing.
     */
    public void testISOFormat_LeadingZero() throws SQLException{
        Statement st = createStatement();
                
        assertStatementError("22007", st,
                "insert into ts (ts1) values ('03-03-05-17.05.43.111111')");

        assertStatementError("22007", st,
                " insert into ts (ts1) values ('103-03-05-17.05.43.111111')");

        assertStatementError("22007", st,
                " insert into ts (ts1) values ('3-03-05-17.05.43.111111')");
        
        st.close();
    }
    
    /**
     * not valid Time format in the timestamp strings:  cloudscape rejects
     */
    public void testISOFormat_WrongTimestampFormat() throws SQLException{
        Statement st = createStatement();
        
        assertStatementError("22007", st,
                "insert into ts (ts1) values ('2003-3-24-13.1.02.566999')");

        assertStatementError("22007", st,
                " insert into ts (ts1) values ('2003-3-24-13.1.1.569')");

        assertStatementError("22007", st,
                " insert into ts (ts1) values ('2003-3-24-1.1.1.56')");

        assertStatementError("22007", st,
                " insert into ts (ts1) values ('2003-3-24-1.1.1')");

        assertStatementError("22007", st,
                " insert into ts (ts1) values ('2003-3-1-17.05.4.')");

        assertEquals(1, st.executeUpdate(" insert into ts (ts1) values " +
        		"('2003-03-05-7.05.43.111111')"));

        // invalid ISO format: cloudscape rejects

        assertStatementError("22007", st,
                "insert into ts (ts1) values ('2003-3-1 17.05.43.123456')");
        
        st.close();
    }
    
    /**
     * Don't allow more than nanoseconds in ISO format.
     */
    public void testISOFormat_MoreThanNanoseconds() throws SQLException{
        Statement st = createStatement();
        
        assertStatementError("22007", st, "insert into ts (ts1) values "
                + "('2003-03-05-17.05.43.999999999999')");

        assertStatementError("22007", st, " insert into ts (ts1) values "
                + "('2003-03-05-17.05.43.999999999000')");

        st.close();
    }
    
    /**
     * Test the timestamp( d, t) function.
     */
    public void testTimeStampFunction() throws SQLException{
        Statement st = createStatement();
        
        st .executeUpdate("create table tt " +
        		"(datecol date, dateStr varchar(16), timecol time, " +
        		"timeStr varchar(16), expected timestamp)");

        st.executeUpdate(" insert into tt ( dateStr, timeStr) " +
        		"values( '2004-03-04', '12:01:02')");

        st.executeUpdate(" insert into tt ( dateStr, timeStr) " +
        		"values( null, '12:01:03')");

        st.executeUpdate(" insert into tt ( dateStr, timeStr) " +
        		"values( '2004-03-05', null)");

        assertUpdateCount(st, 3, " update tt  set datecol = date( dateStr), " +
        		"timecol = time( timeStr)");

        assertUpdateCount(st, 1, " update tt  set expected = " +
        		"timestamp( dateStr || ' ' || timeStr) " +
        		"where dateStr is not null and timeStr is not null");

        ResultSet rs = st.executeQuery(" select dateStr, timeStr from tt " +
        		"where (expected is not null and (expected <> " +
        		"timestamp( dateCol, timeCol) or " +
        		"timestamp( dateCol, timeCol) is null)) or " +
        		"(expected is null and " +
        		"timestamp( dateCol, timeCol) is not null)");
        JDBC.assertColumnNames(rs, new String[] { "DATESTR", "TIMESTR" });
        JDBC.assertDrainResults(rs, 0);

        rs = st.executeQuery(" select dateStr, timeStr from tt " +
        		"where (expected is not null and (expected <> " +
        		"timestamp( dateStr, timeStr) or " +
        		"timestamp( dateStr, timeStr) is null)) " +
        		"or (expected is null and " +
        		"timestamp( dateStr, timeStr) is not null)");

        JDBC.assertColumnNames(rs, new String[] { "DATESTR", "TIMESTR" });
        JDBC.assertDrainResults(rs, 0);

        rs = st.executeQuery(" select dateStr, timeStr from tt " +
        		"where (expected is not null and " +
        		"timestamp( dateStr, timeStr) <> timestamp( dateCol, timeCol))" +
        		" or (expected is null and " +
        		"timestamp( dateStr, timeStr) is not null)");

        JDBC.assertColumnNames(rs, new String[] { "DATESTR", "TIMESTR" });
        JDBC.assertDrainResults(rs, 0);

        rs = st.executeQuery(" select dateStr, timeStr from tt " +
        		"where expected is not null and " +
        		"date( timestamp( dateCol, timeCol)) <> dateCol");
        JDBC.assertColumnNames(rs, new String[] { "DATESTR", "TIMESTR" });
        JDBC.assertDrainResults(rs, 0);

        rs = st.executeQuery(" select dateStr, timeStr from tt " +
        		"where expected is not null and " +
        		"time( timestamp( dateCol, timeCol)) <> timeCol");
        JDBC.assertColumnNames(rs, new String[] { "DATESTR", "TIMESTR" });
        JDBC.assertDrainResults(rs, 0);

        // Error cases
        assertStatementError("42Y95", st,
                "select timestamp( dateCol, dateCol) from tt where "
                        + "dateCol is not null");

        assertStatementError("42Y95", st,
                " select timestamp( timeCol, timeCol) from tt where "
                        + "timeCol is not null");

        assertStatementError("22007", st,
                "values timestamp( 'xyz', '12:01:02')");

        assertStatementError("22007", st,
                " values timestamp( '2004-03-04', 'xyz')");

        dropTable("tt");
        
        st.close();
    }
    
    public void testFormat() throws SQLException{
        Statement st = createStatement();
        
        st.executeUpdate(" create table t_format (t time)");

        // ISO format: UDB is okay.
        assertEquals(1, 
                st.executeUpdate("insert into t_format values ('17.05.44')"));

        assertEquals(1, 
                st.executeUpdate(" insert into t_format values ('17.05.00')"));

        assertEquals(1, 
                st.executeUpdate(" insert into t_format values ('00.05.43')"));

        assertEquals(1, 
                st.executeUpdate(" insert into t_format values ('00.00.00')"));

        // DB2 keeps '24:00:00' but Cloudcape returns '00:00:00'
        assertEquals(1, 
                st.executeUpdate("insert into t_format values ('24.00.00')"));

        // trailing blanks are allowed
        assertEquals(1, 
                st.executeUpdate("insert into t_format values ('17.05.11  ')"));

        assertEquals(1, 
                st.executeUpdate(" insert into t_format values ('17:05:11  ')"));

        // seconds can be omitted
        assertEquals(1, 
                st.executeUpdate("insert into t_format values ('1:01')"));

        assertEquals(1, 
                st.executeUpdate(" insert into t_format values ('1:02 ')"));

        assertEquals(1, 
                st.executeUpdate(" insert into t_format values ('2.01')"));

        assertEquals(1, 
                st.executeUpdate(" insert into t_format values ('2.02 ')"));

        // 11 rows
        ResultSet rs = st.executeQuery("select * from t_format");
        JDBC.assertColumnNames(rs, new String[] { "T" });

        JDBC.assertFullResultSet(rs, 
                new String[][] { { "17:05:44" }, { "17:05:00" },
                { "00:05:43" }, { "00:00:00" }, { "00:00:00" }, { "17:05:11" },
                { "17:05:11" }, { "01:01:00" }, { "01:02:00" }, { "02:01:00" },
                { "02:02:00" } }, true);
        assertUpdateCount(st, 11, " delete from t_format");

        // end value tests...
        assertStatementError("22007", st, "insert into t_format values ('24.60.60')");

        assertStatementError("22007", st, " insert into t_format values ('04.00.60')");

        assertStatementError("22007", st, " insert into t_format values ('03.60.00')");

        // not valid Time string ISO format: HH.MM.SS
        assertStatementError("22007", st, 
                "insert into t_format values ('07.5.44')");

        assertStatementError("22007", st, 
                " insert into t_format values ('07.05.4')");

        assertStatementError("22007", st, 
                " insert into t_format values ('7.5.44')");

        assertStatementError("22007", st, 
                " insert into t_format values ('7.5.4')");

        assertStatementError("22007", st, 
                " insert into t_format values ('7.5.0')");

        assertStatementError("22007", st, 
                " insert into t_format values ('-4.00.00')");

        assertStatementError("22007", st, 
                " insert into t_format values ('A4.00.00')");

        assertStatementError("22007", st, 
                " insert into t_format values ('7.5.999')");

        assertStatementError("22007", st, 
                " insert into t_format values ('07.05.111')");

        assertStatementError("22007", st, 
                " insert into t_format values ('111.05.11')");

        assertStatementError("22007", st, 
                " insert into t_format values ('11.115.00')");

        // no row
        rs = st.executeQuery("select * from t_format");
        JDBC.assertColumnNames(rs, new String[] { "T" });
        JDBC.assertDrainResults(rs, 0);

        dropTable("t_format");
        
        st.close();
    }

    public void testFormat_Additional() throws SQLException{
        Statement st = createStatement();
        
        ResultSet rs = st .executeQuery(
        		"values time('2004-04-15 16:15:32.387')");
        JDBC.assertFullResultSet(rs, new String[][] { { "16:15:32" } }, true);

        rs = st.executeQuery(" values time('2004-04-15-16.15.32.387')");
        JDBC.assertFullResultSet(rs, new String[][] { { "16:15:32" } }, true);

        assertStatementError("22007", st,
                " values time('2004-04-15-16.15.32.387 zz')");

        assertStatementError("22007", st,
                " values time('x-04-15-16.15.32.387')");

        rs = st.executeQuery(" values date('2004-04-15 16:15:32.387')");
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "2004-04-15" } }, true);

        rs = st.executeQuery(" values date('2004-04-15-16.15.32.387')");
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "2004-04-15" } }, true);

        assertStatementError("22008", st,
                " values date('2004-04-15-16.15.32.387 zz')");

        assertStatementError("22008", st,
                " values date('2004-04-15-16.15.32.y')");

        rs = st.executeQuery(" values time('13:59')");
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "13:59:00" } }, true);

        rs = st.executeQuery(" values time('1:00')");
        JDBC.assertFullResultSet(rs, new String[][] { { "01:00:00" } }, true);
        
        st.close();
    }
    
    /**
     * Test unary date and datetime functions. 
     * Test with both constant and variable arguments.
     */
    public void test_DateAndDatetimeFunctionsMore() throws Exception {
        ResultSet rs = null;
        ResultSetMetaData rsmd;
        PreparedStatement pSt;
        Statement st = createStatement();

        String[][] expRS;
        String[] expColNames;      

        setAutoCommit(false);
        // test date(integer)
        st.executeUpdate("create table t_func( i int, d date)");

        commit();
        st.executeUpdate(" insert into t_func values( 1, date(1)),(10, " + 
                "date(10.1)),(365,date(365.1e0)),(366,date(366)),(789" + 
                ",date(789)),(790,date(790)),(791,date(791))");

        // should fail
        assertStatementError("22008", st, "insert into t_func values( 0, date(0))");

        assertStatementError("22008", st,
                " insert into t_func values( -1, date(-1))");

        assertStatementError("22008", st,
                " insert into t_func values( 3652060, date( 3652060))");

        rs = st.executeQuery(" select i,d,date(i),date(d) from t_func order by i");
        JDBC.assertColumnNames(rs, new String[] { "I", "D", "3", "4" });
        JDBC.assertFullResultSet(rs, new String[][] {
                { "1", "1970-01-01", "1970-01-01", "1970-01-01" },
                { "10", "1970-01-10", "1970-01-10", "1970-01-10" },
                { "365", "1970-12-31", "1970-12-31", "1970-12-31" },
                { "366", "1971-01-01", "1971-01-01", "1971-01-01" },
                { "789", "1972-02-28", "1972-02-28", "1972-02-28" },
                { "790", "1972-02-29", "1972-02-29", "1972-02-29" },
                { "791", "1972-03-01", "1972-03-01", "1972-03-01" } }, true);

        rollback();

        assertEquals(1, st.executeUpdate(" insert into t_func(i) values( 0)"));
        
        assertStatementError("22008", st, " select date(i) from t_func");

        rollback();

        st.executeUpdate(" insert into t_func(i) values( -1)");

        assertStatementError("22008", st, " select date(i) from t_func");
        rollback();

        st.executeUpdate(" insert into t_func(i) values( 3652060)");
        assertStatementError("22008", st, " select date(i) from t_func");
        rollback();

        st.executeUpdate(" drop table t_func");
        
        st.executeUpdate(" create table t_func( s varchar(32), d date)");

        commit();
        assertEquals(6, st.executeUpdate(" insert into t_func " +
        		"values('1900060', date('1900060')), ('1904060', " +
        		"date('1904060')), ('1904366', date('1904366')), " +
        		"('2000060', date('2000060')), ('2001060'," +
        		" date('2001060')), ('2001365', date('2001365'))"));

        rs = st.executeQuery(" select s,d,date(s) from t_func order by s");
        JDBC.assertColumnNames(rs, new String[] { "S", "D", "3" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1900060", "1900-03-01", "1900-03-01" },
                { "1904060", "1904-02-29", "1904-02-29" },
                { "1904366", "1904-12-31", "1904-12-31" },
                { "2000060", "2000-02-29", "2000-02-29" },
                { "2001060", "2001-03-01", "2001-03-01" },
                { "2001365", "2001-12-31", "2001-12-31" } }, true);

        rollback();

        // failure cases
        assertStatementError("22008", st, "values( date('2001000'))");

        assertStatementError("22008", st, " values( date('2001366'))");

        assertStatementError("22008", st, " values( date('2000367'))");

        assertStatementError("22008", st, " values( date('xxxxxxx'))");

        st.executeUpdate(" insert into t_func(s) values( '2001000')");

        assertStatementError("22008", st, "select date(s) from t_func");
        rollback();

        st.executeUpdate(" insert into t_func(s) values( '2001366')");

        assertStatementError("22008", st, "select date(s) from t_func");
        rollback();

        st.executeUpdate(" insert into t_func(s) values( '2000367')");

        assertStatementError("22008", st, "select date(s) from t_func");
        rollback();

        st.executeUpdate(" insert into t_func(s) values( 'xxxxxxx')");

        assertStatementError("22008", st, "select date(s) from t_func");
        rollback();

        // test parameter
        pSt = prepareStatement("values( date(cast(? as integer))," +
        		"timestamp(cast(? as varchar(32))))");
        rs = st.executeQuery("values(cast(1 as integer), " +
        		"'2003-03-05-17.05.43.111111')");

        rs.next();
        rsmd = rs.getMetaData();
        for (int i = 1; i <= rsmd.getColumnCount(); i++)
            pSt.setObject(i, rs.getObject(i));
        
        rs = pSt.executeQuery();
        JDBC.assertColumnNames(rs, new String[] { "1", "2" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1970-01-01",
            "2003-03-05 17:05:43.111111" } }, true);

        rs = st.executeQuery("values(2, '20030422190200')");
        rs.next();
        rsmd = rs.getMetaData();
        for (int i = 1; i <= rsmd.getColumnCount(); i++)
            pSt.setObject(i, rs.getObject(i));

        rs = pSt.executeQuery();
        JDBC.assertColumnNames(rs, new String[] { "1", "2" });
        JDBC.assertFullResultSet(rs, new String[][] { { "1970-01-02", 
                "2003-04-22 19:02:00.0" } }, true);

        rs = st.executeQuery(" values( date(date(1)), "
                + "date(timestamp('2003-03-05-17.05.43.111111')))");
        JDBC.assertColumnNames(rs, new String[] { "1", "2" });
        JDBC.assertFullResultSet(rs, 
                new String[][] { { "1970-01-01", "2003-03-05" } }, true);

        st.executeUpdate(" drop table t_func");
        
        
        st.executeUpdate(" create table t_func( s varchar(32), ts timestamp, " + 
                "expected timestamp)");

        commit();
        st.executeUpdate(" insert into t_func(ts) values( " +
                "timestamp('2003-03-05-17.05.43.111111'))");

        rs = st.executeQuery(" select date(ts) from t_func");

        expColNames = new String[] { "1" };
        JDBC.assertColumnNames(rs, expColNames);

        expRS = new String[][] { { "2003-03-05" } };

        JDBC.assertFullResultSet(rs, expRS, true);

        rollback();

        // Test special unary timestamp function rules: yyyyxxddhhmmss
        st.executeUpdate("insert into t_func values('20000228235959', " + 
                "timestamp('20000228235959'), '2000-02-28-23.59.59'), " +
                "('20000229000000', timestamp('20000229000000'), " +
                "'2000-02-29-00.00.00')");

        rs = st.executeQuery(" select s from t_func where ts <> expected or " + 
                "timestamp(s) <> expected or timestamp(ts) <> expected");
        JDBC.assertColumnNames(rs, new String[] { "S" });
        JDBC.assertDrainResults(rs, 0);

        rollback();

        // invalid
        assertStatementError("22008", st,
                "values( timestamp('2000 1 1 0 0 0'))");

        assertStatementError("22008", st,
                " values( timestamp('aaaaaaaaaaaaaa'))");

        assertEquals(1, st.executeUpdate(
                " insert into t_func(s) values('2000 1 1 0 0 0')"));
        
        assertStatementError("22008", st, " select timestamp(s) from t_func");

        rollback();
        assertEquals(1, st.executeUpdate(
                " insert into t_func(s) values('aaaaaaaaaaaaaa')"));
        assertStatementError("22008", st, " select timestamp(s) from t_func");
        rollback();

        commit();

        getConnection().rollback();
        st.close();
    }
    
    /**
     *  Null values in datetime scalar functions.
     */
    public void testNulls() throws SQLException{
        Statement st = createStatement();
        
        st.executeUpdate("create table nulls (t time, d date, ts timestamp)");

        st.executeUpdate(" insert into nulls values (null,null,null)");

        commit();
        assertStatementError("42X25", st, " select year(t) from nulls");

        assertStatementError("42X25", st, " select month(t) from nulls");

        assertStatementError("42X25", st, " select day(t) from nulls");

        ResultSet rs = st.executeQuery(" select hour(t) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        rs = st.executeQuery(" select minute(t) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        rs = st.executeQuery(" select second(t) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        rs = st.executeQuery(" select year(d) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        rs = st.executeQuery(" select month(d) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        rs = st.executeQuery(" select day(d) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        assertStatementError("42X25", st, " select hour(d) from nulls");

        assertStatementError("42X25", st, " select minute(d) from nulls");

        assertStatementError("42X25", st, " select second(d) from nulls");

        rs = st.executeQuery(" select year(ts) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        rs = st.executeQuery(" select month(ts) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        rs = st.executeQuery(" select day(ts) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        rs = st.executeQuery(" select hour(ts) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        rs = st.executeQuery(" select minute(ts) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        rs = st.executeQuery(" select second(ts) from nulls");
        JDBC.assertFullResultSet(rs, new String[][] { { null } }, true);

        st.executeUpdate(" drop table nulls");
        
        st.close();
    }

    /**
     * Regression test case for DERBY-3856. The embedded driver sometimes
     * returned an unnormalized datetime string when getString() was used
     * to retrieve the result from an invocation of the unary TIMESTAMP or
     * DATE function.
     */
    public void testDerby3856() throws SQLException {
        // TIMESTAMP called on literal has always a returned normalized value.
        assertSingleValue(
                "values timestamp('2003-03-05-17.05.43.111111')",
                "2003-03-05 17:05:43.111111");

        // This one used to return the original unnormalized input with the
        // embedded driver.
        assertSingleValue(
                "values timestamp(cast('2003-03-05-17.05.43.111111' " +
                "as varchar(32)))",
                "2003-03-05 17:05:43.111111");

        // DATE called on literal has always a returned normalized value.
        assertSingleValue("values date('10/07/2008')", "2008-10-07");

        // This one used to return the original unnormalized input with the
        // embedded driver.
        assertSingleValue(
                "values date(cast('10/07/2008' as varchar(10)))",
                "2008-10-07");

        // TIME called on literal has always a returned normalized string.
        assertSingleValue("values time('10.00.00')", "10:00:00");

        // TIME has also always returned a normalized value for the variant
        // with a CAST.
        assertSingleValue(
                "values time(cast('10.00.00' as varchar(10)))",
                "10:00:00");
    }

    /**
     * Test case to show that timestamp function accepts nanoseconds
     * resolution (DERBY-4625).
     */
    public void testNanosecondResolution() throws SQLException{
    	assertSingleValue("values timestamp('2010-04-21 12:00:00.123456789')",
    			"2010-04-21 12:00:00.123456789");
    }

    /**
     * Execute an SQL statement and check that it returns a single, specific
     * value.
     *
     * @param sql the statement to execute
     * @param expectedValue the expected value
     */
    private void assertSingleValue(String sql, String expectedValue)
            throws SQLException {
        JDBC.assertSingleValueResultSet(
                createStatement().executeQuery(sql), expectedValue);
    }
}