1 package com.mapd.tests;
3 import com.omnisci.thrift.server.TOmniSciException;
4 import com.omnisci.thrift.server.TTypeInfo;
6 import org.apache.commons.math3.util.Pair;
8 import java.time.LocalDateTime;
9 import java.time.ZoneOffset;
10 import java.time.format.DateTimeFormatter;
11 import java.time.temporal.ChronoField;
12 import java.time.temporal.ChronoUnit;
13 import java.util.Arrays;
14 import java.util.EnumSet;
15 import java.util.Random;
16 import java.util.function.Function;
23 dtYEAR(
"YEAR",
new Function<LocalDateTime, LocalDateTime>() {
25 public LocalDateTime apply(LocalDateTime t) {
27 t = t.withDayOfMonth(1);
28 t = t.truncatedTo(ChronoUnit.DAYS);
32 dtQUARTER(
"QUARTER",
new Function<LocalDateTime, LocalDateTime>() {
34 public LocalDateTime apply(LocalDateTime t) {
35 int month = t.getMonthValue();
60 t = t.withDayOfMonth(1);
61 t = t.truncatedTo(ChronoUnit.DAYS);
65 dtMONTH(
"MONTH",
new Function<LocalDateTime, LocalDateTime>() {
67 public LocalDateTime apply(LocalDateTime t) {
68 t = t.withDayOfMonth(1);
69 t = t.truncatedTo(ChronoUnit.DAYS);
73 dtDAY(
"DAY",
new Function<LocalDateTime, LocalDateTime>() {
75 public LocalDateTime apply(LocalDateTime t) {
76 t = t.truncatedTo(ChronoUnit.DAYS);
80 dtHOUR(
"HOUR",
new Function<LocalDateTime, LocalDateTime>() {
82 public LocalDateTime apply(LocalDateTime t) {
83 t = t.truncatedTo(ChronoUnit.HOURS);
87 dtMINUTE(
"MINUTE",
new Function<LocalDateTime, LocalDateTime>() {
89 public LocalDateTime apply(LocalDateTime t) {
90 t = t.truncatedTo(ChronoUnit.MINUTES);
94 dtSECOND(
"SECOND",
new Function<LocalDateTime, LocalDateTime>() {
96 public LocalDateTime apply(LocalDateTime t) {
97 t = t.truncatedTo(ChronoUnit.SECONDS);
118 dtCENTURY(
"CENTURY",
new Function<LocalDateTime, LocalDateTime>() {
120 public LocalDateTime apply(LocalDateTime t) {
121 int year = t.getYear();
123 int diff = year % range;
128 t = t.withYear(year + 1);
131 t = t.withDayOfMonth(1);
132 t = t.truncatedTo(ChronoUnit.DAYS);
136 dtDECADE(
"DECADE",
new Function<LocalDateTime, LocalDateTime>() {
138 public LocalDateTime apply(LocalDateTime t) {
139 int year = t.getYear();
141 int diff = year % range;
143 t = t.withYear(year);
145 t = t.withDayOfMonth(1);
146 t = t.truncatedTo(ChronoUnit.DAYS);
152 public LocalDateTime apply(LocalDateTime t) {
153 t = t.truncatedTo(ChronoUnit.MILLIS);
159 public LocalDateTime apply(LocalDateTime t) {
160 t = t.truncatedTo(ChronoUnit.MICROS);
164 dtNANOSECOND(
"NANOSECOND",
new Function<LocalDateTime, LocalDateTime>() {
166 public LocalDateTime apply(LocalDateTime t) {
167 t = t.truncatedTo(ChronoUnit.NANOS);
171 dtWEEK(
"WEEK",
new Function<LocalDateTime, LocalDateTime>() {
173 public LocalDateTime apply(LocalDateTime t) {
174 t = t.with(ChronoField.DAY_OF_WEEK, 1);
175 t = t.truncatedTo(ChronoUnit.DAYS);
194 Function<LocalDateTime, LocalDateTime>
trunc;
196 private DateTruncUnit(String token, Function<LocalDateTime, LocalDateTime> trunc) {
197 this.sqlToken = token;
204 daYEAR(
"YEAR",
new Function<LocalDateTime, Long>() {
205 public Long apply(LocalDateTime t) {
206 return (
long) t.get(ChronoField.YEAR);
209 daQUARTER(
"QUARTER",
new Function<LocalDateTime, Long>() {
211 public Long apply(LocalDateTime t) {
212 int month = t.get(ChronoField.MONTH_OF_YEAR);
234 daMONTH(
"MONTH",
new Function<LocalDateTime, Long>() {
236 public Long apply(LocalDateTime t) {
237 return (
long) t.get(ChronoField.MONTH_OF_YEAR);
240 daDAY(
"DAY",
new Function<LocalDateTime, Long>() {
242 public Long apply(LocalDateTime t) {
243 return (
long) t.get(ChronoField.DAY_OF_MONTH);
246 daHOUR(
"HOUR",
new Function<LocalDateTime, Long>() {
248 public Long apply(LocalDateTime t) {
249 return (
long) t.get(ChronoField.HOUR_OF_DAY);
252 daMINUTE(
"MINUTE",
new Function<LocalDateTime, Long>() {
254 public Long apply(LocalDateTime t) {
255 return (
long) t.get(ChronoField.MINUTE_OF_HOUR);
258 daSECOND(
"SECOND",
new Function<LocalDateTime, Long>() {
260 public Long apply(LocalDateTime t) {
261 return (
long) t.get(ChronoField.SECOND_OF_MINUTE);
269 public Long apply(LocalDateTime t) {
270 return t.get(ChronoField.MILLI_OF_SECOND)
271 + (1000L * t.get(ChronoField.SECOND_OF_MINUTE));
276 public Long apply(LocalDateTime t) {
277 return t.get(ChronoField.MICRO_OF_SECOND)
278 + (1000_000L * t.get(ChronoField.SECOND_OF_MINUTE));
283 public Long apply(LocalDateTime t) {
284 return t.get(ChronoField.NANO_OF_SECOND)
285 + (1000_000_000L * t.get(ChronoField.SECOND_OF_MINUTE));
288 daWEEK(
"WEEK",
new Function<LocalDateTime, Long>() {
290 public Long apply(LocalDateTime t) {
291 LocalDateTime year = DateTruncUnit.dtYEAR.trunc.apply(t);
293 year = year.plusDays(3);
296 LocalDateTime week = DateTruncUnit.dtWEEK.trunc.apply(year);
298 if (week.compareTo(t) > 0) {
299 year = year.minusYears(1);
300 week = DateTruncUnit.dtWEEK.trunc.apply(year);
304 while (week.compareTo(t) <= 0) {
306 week = week.plusWeeks(1);
326 public Long apply(LocalDateTime t) {
327 return (
long) t.get(ChronoField.DAY_OF_YEAR);
332 private Function<LocalDateTime, Long>
extract;
335 this.sqlToken = token;
342 daYEAR(
"YEAR",
new Function<Pair<LocalDateTime, LocalDateTime>, Long>() {
343 public Long apply(Pair<LocalDateTime, LocalDateTime> d) {
344 return d.getFirst().until(d.getSecond(), ChronoUnit.YEARS);
347 daQUARTER(
"QUARTER",
new Function<Pair<LocalDateTime, LocalDateTime>, Long>() {
348 private Long applyCorrect(Pair<LocalDateTime, LocalDateTime> d) {
349 LocalDateTime start = d.getFirst();
350 LocalDateTime end = d.getSecond();
353 if (start.compareTo(end) > 0) {
359 start = DateTruncUnit.dtQUARTER.trunc.apply(start);
363 while (start.compareTo(end) <= 0) {
365 start = start.plusMonths(3);
371 public Long apply(Pair<LocalDateTime, LocalDateTime> d) {
373 return d.getFirst().until(d.getSecond(), ChronoUnit.MONTHS) / 3;
376 daMONTH(
"MONTH",
new Function<Pair<LocalDateTime, LocalDateTime>, Long>() {
377 public Long apply(Pair<LocalDateTime, LocalDateTime> d) {
378 return d.getFirst().until(d.getSecond(), ChronoUnit.MONTHS);
381 daDAY(
"DAY",
new Function<Pair<LocalDateTime, LocalDateTime>, Long>() {
382 public Long apply(Pair<LocalDateTime, LocalDateTime> d) {
383 return d.getFirst().until(d.getSecond(), ChronoUnit.DAYS);
386 daHOUR(
"HOUR",
new Function<Pair<LocalDateTime, LocalDateTime>, Long>() {
387 public Long apply(Pair<LocalDateTime, LocalDateTime> d) {
388 return d.getFirst().until(d.getSecond(), ChronoUnit.HOURS);
391 daMINUTE(
"MINUTE",
new Function<Pair<LocalDateTime, LocalDateTime>, Long>() {
392 public Long apply(Pair<LocalDateTime, LocalDateTime> d) {
393 return d.getFirst().until(d.getSecond(), ChronoUnit.MINUTES);
396 daSECOND(
"SECOND",
new Function<Pair<LocalDateTime, LocalDateTime>, Long>() {
397 public Long apply(Pair<LocalDateTime, LocalDateTime> d) {
398 return d.getFirst().until(d.getSecond(), ChronoUnit.SECONDS);
405 "MILLISECOND",
new Function<Pair<LocalDateTime, LocalDateTime>, Long>() {
406 public Long apply(Pair<LocalDateTime, LocalDateTime> d) {
407 return d.getFirst().until(d.getSecond(), ChronoUnit.MILLIS);
411 "MICROSECOND",
new Function<Pair<LocalDateTime, LocalDateTime>, Long>() {
412 public Long apply(Pair<LocalDateTime, LocalDateTime> d) {
413 return d.getFirst().until(d.getSecond(), ChronoUnit.MICROS);
416 daNANOSECOND(
"NANOSECOND",
new Function<Pair<LocalDateTime, LocalDateTime>, Long>() {
417 public Long apply(Pair<LocalDateTime, LocalDateTime> d) {
418 return d.getFirst().until(d.getSecond(), ChronoUnit.NANOS);
421 daWEEK(
"WEEK",
new Function<Pair<LocalDateTime, LocalDateTime>, Long>() {
422 public Long apply(Pair<LocalDateTime, LocalDateTime> d) {
423 return d.getFirst().until(d.getSecond(), ChronoUnit.WEEKS);
441 private Function<Pair<LocalDateTime, LocalDateTime>, Long>
diff;
444 String token, Function<Pair<LocalDateTime, LocalDateTime>, Long> diff) {
445 this.sqlToken = token;
452 daYEAR(
"YEAR", 99,
new Function<Pair<LocalDateTime, Long>, LocalDateTime>() {
453 public LocalDateTime apply(Pair<LocalDateTime, Long> t) {
454 return t.getFirst().plus(t.getSecond(), ChronoUnit.YEARS);
458 "QUARTER", 10 * 3,
new Function<Pair<LocalDateTime, Long>, LocalDateTime>() {
460 public LocalDateTime apply(Pair<LocalDateTime, Long> t) {
461 return t.getFirst().plus(t.getSecond() * 3, ChronoUnit.MONTHS);
464 daMONTH(
"MONTH", 99,
new Function<Pair<LocalDateTime, Long>, LocalDateTime>() {
466 public LocalDateTime apply(Pair<LocalDateTime, Long> t) {
467 return t.getFirst().plus(t.getSecond(), ChronoUnit.MONTHS);
470 daDAY(
"DAY", 99,
new Function<Pair<LocalDateTime, Long>, LocalDateTime>() {
472 public LocalDateTime apply(Pair<LocalDateTime, Long> t) {
473 return t.getFirst().plus(t.getSecond(), ChronoUnit.DAYS);
476 daHOUR(
"HOUR", 99,
new Function<Pair<LocalDateTime, Long>, LocalDateTime>() {
478 public LocalDateTime apply(Pair<LocalDateTime, Long> t) {
479 return t.getFirst().plus(t.getSecond(), ChronoUnit.HOURS);
482 daMINUTE(
"MINUTE", 99,
new Function<Pair<LocalDateTime, Long>, LocalDateTime>() {
484 public LocalDateTime apply(Pair<LocalDateTime, Long> t) {
485 return t.getFirst().plus(t.getSecond(), ChronoUnit.MINUTES);
488 daSECOND(
"SECOND", 99,
new Function<Pair<LocalDateTime, Long>, LocalDateTime>() {
490 public LocalDateTime apply(Pair<LocalDateTime, Long> t) {
491 return t.getFirst().plus(t.getSecond(), ChronoUnit.SECONDS);
498 12 * 30 * 24 * 60 * 60 * 1000L,
499 new Function<Pair<LocalDateTime, Long>, LocalDateTime>() {
501 public LocalDateTime apply(Pair<LocalDateTime, Long> t) {
502 return t.getFirst().plus(t.getSecond(), ChronoUnit.MILLIS);
506 12 * 30 * 24 * 60 * 60 * 1000 * 1000,
507 new Function<Pair<LocalDateTime, Long>, LocalDateTime>() {
509 public LocalDateTime apply(Pair<LocalDateTime, Long> t) {
510 return t.getFirst().plus(t.getSecond(), ChronoUnit.MICROS);
514 12 * 30 * 24 * 60 * 60 * 1000 * 1000 * 1000,
515 new Function<Pair<LocalDateTime, Long>, LocalDateTime>() {
517 public LocalDateTime apply(Pair<LocalDateTime, Long> t) {
518 return t.getFirst().plus(t.getSecond(), ChronoUnit.NANOS);
521 daWEEK(
"WEEK", 53,
new Function<Pair<LocalDateTime, Long>, LocalDateTime>() {
523 public LocalDateTime apply(Pair<LocalDateTime, Long> t) {
524 return t.getFirst().plus(t.getSecond(), ChronoUnit.WEEKS);
542 private Function<Pair<LocalDateTime, Long>, LocalDateTime>
add;
547 Function<Pair<LocalDateTime, Long>, LocalDateTime> f) {
548 this.sqlToken = token;
557 int year = 1900 + r.nextInt(200);
558 int month = 1 + r.nextInt(12);
559 int dayOfMonth = 1 + r.nextInt(31);
560 int hour = r.nextInt(24);
561 int minute = r.nextInt(60);
562 int second = r.nextInt(60);
563 int nanoOfSecond = r.nextInt(1000 * 1000 * 1000);
565 return LocalDateTime.of(
566 year, month, dayOfMonth, hour, minute, second, nanoOfSecond);
567 }
catch (Exception e) {
573 TIMESTAMP(
"TIMESTAMP",
574 "'TIMESTAMP' ''yyyy-MM-dd HH:mm:ss''",
576 LocalDateTime.ofEpochSecond(-30610224000L, 0, ZoneOffset.UTC),
577 LocalDateTime.ofEpochSecond(29379542399L, 0, ZoneOffset.UTC)),
578 TIMESTAMP_0(
"TIMESTAMP(0)",
579 "'TIMESTAMP(0)' ''yyyy-MM-dd HH:mm:ss''",
581 LocalDateTime.ofEpochSecond(-30610224000L, 0, ZoneOffset.UTC),
582 LocalDateTime.ofEpochSecond(29379542399L, 0, ZoneOffset.UTC)),
583 TIMESTAMP_3(
"TIMESTAMP(3)",
584 "'TIMESTAMP(3)' ''yyyy-MM-dd HH:mm:ss.SSS''",
586 LocalDateTime.ofEpochSecond(-30610224000L, 0, ZoneOffset.UTC),
587 LocalDateTime.ofEpochSecond(29379542399L, 0, ZoneOffset.UTC)),
588 TIMESTAMP_6(
"TIMESTAMP(6)",
589 "'TIMESTAMP(6)' ''yyyy-MM-dd HH:mm:ss.SSSSSS''",
591 LocalDateTime.ofEpochSecond(-30610224000L, 0, ZoneOffset.UTC),
592 LocalDateTime.ofEpochSecond(29379542399L, 0, ZoneOffset.UTC)),
593 TIMESTAMP_9(
"TIMESTAMP(9)",
594 "'TIMESTAMP(9)' ''yyyy-MM-dd HH:mm:ss.SSSSSSSSS''",
596 LocalDateTime.ofEpochSecond(-9223372036L, 0, ZoneOffset.UTC),
597 LocalDateTime.ofEpochSecond(9223372036L, 0, ZoneOffset.UTC)),
598 TIMESTAMP_FIXED_32(
"TIMESTAMP ENCODING FIXED(32)",
599 "'TIMESTAMP' ''yyyy-MM-dd HH:mm:ss''",
601 LocalDateTime.ofEpochSecond(Integer.MIN_VALUE + 1, 0, ZoneOffset.UTC),
602 LocalDateTime.ofEpochSecond(Integer.MAX_VALUE, 0, ZoneOffset.UTC)),
604 "'DATE' ''yyyy-MM-dd''",
606 LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC).plusDays(-2147483648L),
607 LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC).plusDays(2147483647L)),
608 DATE_DAYS_16(
"DATE ENCODING DAYS(16)",
609 "'DATE' ''yyyy-MM-dd''",
611 LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC)
612 .plusDays(Short.MIN_VALUE + 1),
613 LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC).plusDays(Short.MAX_VALUE)),
614 DATE_DAYS_32(
"DATE ENCODING DAYS(32)",
615 "'DATE' ''yyyy-MM-dd''",
617 LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC).plusDays(-2147483648L),
618 LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC).plusDays(2147483647L));
631 this.sqlType = sqlType;
632 formatter = DateTimeFormatter.ofPattern(pattern);
639 if (null != val)
return prefx +
"_" +
name() +
" /* " + toSql(val) +
" */";
640 return prefx +
"_" +
name();
643 public String
toSql(LocalDateTime d) {
644 return formatter.format(d);
647 public LocalDateTime
clear(LocalDateTime d) {
648 if (null != toClear) {
649 d = d.truncatedTo(toClear);
656 if (null != toClear) {
657 if (toClear == ChronoUnit.DAYS) {
658 d = d.truncatedTo(ChronoUnit.SECONDS);
660 d = d.truncatedTo(toClear);
668 return t.isAfter(min) && t.isBefore(max);
675 com.omnisci.thrift.server.TQueryResult
res = client.runSql(sql);
676 LocalDateTime r = null;
677 if (res.row_set.is_columnar) {
678 TTypeInfo tt = res.row_set.row_desc.get(0).col_type;
679 int pow = (int) Math.pow(10, tt.precision);
680 long val = res.row_set.columns.get(0).data.int_col.get(0);
681 int nanosPow = (int) Math.pow(10, 9 - tt.precision);
682 long nanos = (val % pow);
687 r = LocalDateTime.ofEpochSecond(
688 Math.floorDiv(val, pow), (
int) nanos, ZoneOffset.UTC);
691 throw new RuntimeException(
"Unsupported!");
695 }
catch (TOmniSciException e) {
696 System.out.println(
"Query failed: " + sql +
" -- " + e.error_msg);
697 return LocalDateTime.MIN;
699 }
catch (Exception e) {
700 System.out.println(
"Query failed: " + sql +
" -- " + e.getMessage());
701 return LocalDateTime.MIN;
707 com.omnisci.thrift.server.TQueryResult
res = client.runSql(sql);
709 if (res.row_set.is_columnar) {
710 long val = res.row_set.columns.get(0).data.int_col.get(0);
713 throw new RuntimeException(
"Unsupported!");
716 }
catch (TOmniSciException e) {
717 System.out.println(
"Query failed: " + sql +
" -- " + e.error_msg);
718 return Long.MIN_VALUE;
719 }
catch (Exception e) {
720 System.out.println(
"Query failed: " + sql +
" -- " + e.getMessage());
721 return Long.MIN_VALUE;
728 if (!enc.isValid(d)) {
732 String sql =
"SELECT DATE_TRUNC('" + f.sqlToken +
"', " + enc.toSql(d) +
");";
734 LocalDateTime expected = f.trunc.apply(d);
735 expected = enc.clear(expected);
737 Fuzzy rc = Fuzzy.compare(expected, r, enc);
739 System.out.println(
"Query " + rc +
": " + sql
740 +
" -> expected: " + expected.toString() +
" got " + r.toString());
756 String sqlUpdate =
"UPDATE DateTimeTest set " + aEnc.toSqlColumn(
"a", null) +
" = "
760 sqlUpdate +=
", " + bEnc.toSqlColumn(
"b", null) +
" = " + bEnc.toSql(b);
766 client.runSql(sqlUpdate);
767 }
catch (TOmniSciException e) {
768 System.out.println(
"Update failed: " + sqlUpdate +
" " + e.error_msg);
776 String sql =
"SELECT DATE_TRUNC('" + f.sqlToken +
"', " + enc.toSqlColumn(
"a", d)
777 +
") FROM DateTimeTest;";
779 LocalDateTime expected = f.trunc.apply(d);
780 expected = enc.clear(expected);
782 Fuzzy rc = Fuzzy.compare(expected, r, enc);
784 System.out.println(
"Query " + rc +
": " + sql
785 +
" -> expected: " + expected.toString() +
" got " + r.toString());
794 String sql =
"SELECT EXTRACT(" + f.sqlToken +
" FROM " + enc.toSql(d) +
");";
798 long expected = f.extract.apply(d);
800 Fuzzy rc = Fuzzy.compare(expected, r);
803 "Query " + rc +
": " + sql +
" -> expected: " + expected +
" got " + r);
812 if (!enc.isValid(d)) {
817 String sql =
"SELECT EXTRACT(" + f.sqlToken +
" FROM " + enc.toSqlColumn(
"a", d)
818 +
") FROM DateTimeTest;";
822 long expected = f.extract.apply(d);
824 Fuzzy rc = Fuzzy.compare(expected, r);
827 "Query " + rc +
": " + sql +
" -> expected: " + expected +
" got " + r);
838 String sql =
"SELECT " + fn +
"(" + f.sqlToken +
", " + enc0.toSql(d0) +
", "
839 + enc1.toSql(d1) +
");";
844 long expected = f.diff.apply(Pair.create(d0, d1));
846 Fuzzy rc = Fuzzy.compare(expected, r);
849 "Query " + rc +
": " + sql +
" -> expected: " + expected +
" got " + r);
862 if (!enc0.isValid(d0) || !enc1.isValid(d1)) {
867 String sql =
"SELECT " + fn +
"(" + f.sqlToken +
", " + enc0.toSqlColumn(
"a", d0)
868 +
", " + enc1.toSqlColumn(
"b", d1) +
") FROM DateTimeTest;";
873 long expected = f.diff.apply(Pair.create(d0, d1));
875 Fuzzy rc = Fuzzy.compare(expected, r);
878 "Query " + rc +
": " + sql +
" -> expected: " + expected +
" got " + r);
889 "SELECT " + fn +
"(" + f.sqlToken +
", " + units +
", " + enc.toSql(d) +
");";
892 LocalDateTime expected = f.add.apply(Pair.create(enc.clear(d), units));
893 expected = enc.clearForDateAddResult(expected);
895 Fuzzy rc = Fuzzy.compareDateAdd(expected, r, enc);
897 System.out.println(
"Query " + rc +
": " + sql
898 +
" -> expected: " + expected.toString() +
" got " + r.toString());
910 if (!enc.isValid(d)) {
915 String sql =
"SELECT " + fn +
"(" + f.sqlToken +
", " + units +
", "
916 + enc.toSqlColumn(
"a", d) +
") FROM DateTimeTest;";
919 LocalDateTime expected = f.add.apply(Pair.create(enc.clear(d), units));
920 expected = enc.clearForDateAddResult(expected);
922 Fuzzy rc = Fuzzy.compareDateAdd(expected, r, enc);
924 System.out.println(
"Query " + rc +
": " + sql
925 +
" -> expected: " + expected.toString() +
" got " + r.toString());
931 static EnumSet
addAllowed = EnumSet.allOf(DateAddUnit.class);
949 "SELECT " + enc.toSql(d) +
" + INTERVAL '" + units +
"' " + f.sqlToken +
" ;";
952 LocalDateTime expected = f.add.apply(Pair.create(enc.clear(d), units));
953 expected = enc.clearForDateAddResult(expected);
955 Fuzzy rc = Fuzzy.compareDateAdd(expected, r, enc);
957 System.out.println(
"Query " + rc +
": " + sql
958 +
" -> expected: " + expected.toString() +
" got " + r.toString());
972 "SELECT " + enc.toSql(d) +
" - INTERVAL '" + toSub +
"' " + f.sqlToken +
" ;";
975 LocalDateTime expected = f.add.apply(Pair.create(enc.clear(d), units));
976 expected = enc.clearForDateAddResult(expected);
978 Fuzzy rc = Fuzzy.compareDateAdd(expected, r, enc);
980 System.out.println(
"Query " + rc +
": " + sql
981 +
" -> expected: " + expected.toString() +
" got " + r.toString());
991 if (expected.equals(result))
return ok;
993 LocalDateTime okish = result.minus(1, ChronoUnit.NANOS);
994 okish = enc.clear(okish);
996 if (expected.equals(okish))
return Fuzzy.
okish;
998 okish = result.plus(1, ChronoUnit.NANOS);
999 okish = enc.clear(okish);
1001 if (expected.equals(okish))
return Fuzzy.
okish;
1007 if (expected == result)
return ok;
1009 long okish = result - 1;
1017 if ((result == 59 && expected == 0) || (result == 0 && expected == 59)) {
1022 if ((result == 23 && expected == 0) || (result == 0 && expected == 23)) {
1032 if (expected.equals(result))
return ok;
1034 LocalDateTime okish = result.minus(1, ChronoUnit.NANOS);
1035 okish = enc.clearForDateAddResult(okish);
1037 if (expected.equals(okish))
return Fuzzy.
okish;
1039 okish = result.plus(1, ChronoUnit.NANOS);
1040 okish = enc.clearForDateAddResult(okish);
1042 if (expected.equals(okish))
return Fuzzy.
okish;
1049 client.runSql(
"DROP TABLE IF EXISTS DateTimeTest;");
1050 String sqlCreate =
"CREATE TABLE DateTimeTest(id int";
1051 String sqlInsert =
"INSERT INTO DateTimeTest VALUES(0";
1053 sqlCreate +=
", " + e.toSqlColumn(
"a", null) +
" " + e.sqlType;
1054 sqlCreate +=
", " + e.toSqlColumn(
"b", null) +
" " + e.sqlType;
1055 sqlInsert +=
", null, null";
1061 client.runSql(sqlCreate);
1062 client.runSql(sqlInsert);
1064 System.out.println(
"CREATE: " + sqlCreate);
1065 System.out.println(
"INSERT: " + sqlInsert);
1068 public static void main(String[]
args)
throws Exception {
1072 if (0 <
args.length) {
1073 seed = Long.parseLong(
args[0], 10);
1075 seed = System.currentTimeMillis();
1078 System.out.println(
"Seed: " + seed);
1079 Random r =
new Random(seed);
1082 "localhost", 6274,
"omnisci",
"admin",
"HyperInteractive");
1090 boolean testTrunc =
true;
1091 boolean testExtract =
true;
1099 e = e.minus(1, ChronoUnit.NANOS);
1102 e = e.minus(1, ChronoUnit.NANOS);
1125 for (String fn : Arrays.asList(
"TIMESTAMPDIFF" )) {
1126 testDiff(fn, d0, d1, f, su, enc0, enc1);
1127 testDiff(fn, d1, d0, f, su, enc0, enc1);
1128 testDiff(fn, d0, d0, f, su, enc0, enc1);
1129 testDiff(fn, d1, d1, f, su, enc0, enc1);
1138 long units = r.nextLong() % f.max;
1139 if (r.nextBoolean()) {
1143 for (String fn : Arrays.asList(
"TIMESTAMPADD",
"DATEADD")) {
1147 testAdd(d0, f, units, su, enc0);
1148 testSub(d0, f, units, su, enc0);
1149 testAdd(d1, f, units, su, enc0);
1150 testSub(d1, f, units, su, enc0);
static LocalDateTime createRandomDateTime(Random r)
DateTruncUnit(String token, Function< LocalDateTime, LocalDateTime > trunc)
static void testDiffTable(String fn, LocalDateTime d0, LocalDateTime d1, DateDiffUnit f, MapdTestClient client, Encoding enc0, Encoding enc1)
String toSqlColumn(String prefx, LocalDateTime val)
static void testDateExtract(LocalDateTime d, DateExtractUnit f, MapdTestClient client, Encoding enc)
DateAddUnit(String token, long max, Function< Pair< LocalDateTime, Long >, LocalDateTime > f)
static void updateValues(MapdTestClient client, LocalDateTime a, Encoding aEnc)
static Fuzzy compare(LocalDateTime expected, LocalDateTime result, Encoding enc)
String toSql(LocalDateTime d)
static void testDateAddTable(String fn, LocalDateTime d, DateAddUnit f, long units, MapdTestClient client, Encoding enc)
static void testDateExtractTable(LocalDateTime d, DateExtractUnit f, MapdTestClient client, Encoding enc)
static void testDiff(String fn, LocalDateTime d0, LocalDateTime d1, DateDiffUnit f, MapdTestClient client, Encoding enc0, Encoding enc1)
static EnumSet resultsToDump
static Fuzzy compare(long expected, long result)
static EnumSet addAllowed
static Fuzzy compareDateAdd(LocalDateTime expected, LocalDateTime result, Encoding enc)
Function< LocalDateTime, LocalDateTime > trunc
static void updateValues(MapdTestClient client, LocalDateTime a, Encoding aEnc, LocalDateTime b, Encoding bEnc)
Function< Pair< LocalDateTime, LocalDateTime >, Long > diff
Encoding(String sqlType, String pattern, ChronoUnit unit, LocalDateTime min, LocalDateTime max)
static void testDateAdd(String fn, LocalDateTime d, DateAddUnit f, long units, MapdTestClient client, Encoding enc)
static void main(String[] args)
DateDiffUnit(String token, Function< Pair< LocalDateTime, LocalDateTime >, Long > diff)
static void testAdd(LocalDateTime d, DateAddUnit f, long units, MapdTestClient client, Encoding enc)
Function< Pair< LocalDateTime, Long >, LocalDateTime > add
static LocalDateTime getDateTimeFromQuery(MapdTestClient client, String sql)
static LocalDateTime testDateTrunc(LocalDateTime d, DateTruncUnit f, MapdTestClient client, Encoding enc)
LocalDateTime clearForDateAddResult(LocalDateTime d)
DateTimeFormatter formatter
static void createTestTable(MapdTestClient client)
static long getLongFromQuery(MapdTestClient client, String sql)
LocalDateTime clear(LocalDateTime d)
boolean isValid(LocalDateTime t)
static void testSub(LocalDateTime d, DateAddUnit f, long units, MapdTestClient client, Encoding enc)
static LocalDateTime testDateTruncTable(LocalDateTime d, DateTruncUnit f, MapdTestClient client, Encoding enc)