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
|
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeDate32.h>
#include <DataTypes/DataTypeDateTime.h>
#include <DataTypes/DataTypeDateTime64.h>
#include <DataTypes/NumberTraits.h>
#include <Columns/ColumnString.h>
#include <Functions/DateTimeTransforms.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/FunctionsConversion.h>
#include <Functions/IFunction.h>
#include <Functions/castTypeToEither.h>
#include <Functions/extractTimeZoneFromFunctionArguments.h>
#include <Functions/numLiteralChars.h>
#include <IO/WriteHelpers.h>
#include <Common/Concepts.h>
#include <Common/DateLUTImpl.h>
#include <base/find_symbols.h>
#include <Core/DecimalFunctions.h>
#include <type_traits>
#include <concepts>
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int NOT_IMPLEMENTED;
extern const int ILLEGAL_COLUMN;
extern const int BAD_ARGUMENTS;
}
namespace
{
using Pos = const char *;
enum class SupportInteger
{
Yes,
No
};
enum class FormatSyntax
{
MySQL,
Joda
};
template <typename DataType> struct InstructionValueTypeMap {};
template <> struct InstructionValueTypeMap<DataTypeInt8> { using InstructionValueType = UInt32; };
template <> struct InstructionValueTypeMap<DataTypeUInt8> { using InstructionValueType = UInt32; };
template <> struct InstructionValueTypeMap<DataTypeInt16> { using InstructionValueType = UInt32; };
template <> struct InstructionValueTypeMap<DataTypeUInt16> { using InstructionValueType = UInt32; };
template <> struct InstructionValueTypeMap<DataTypeInt32> { using InstructionValueType = UInt32; };
template <> struct InstructionValueTypeMap<DataTypeUInt32> { using InstructionValueType = UInt32; };
template <> struct InstructionValueTypeMap<DataTypeInt64> { using InstructionValueType = UInt32; };
template <> struct InstructionValueTypeMap<DataTypeUInt64> { using InstructionValueType = UInt32; };
template <> struct InstructionValueTypeMap<DataTypeDate> { using InstructionValueType = UInt16; };
template <> struct InstructionValueTypeMap<DataTypeDate32> { using InstructionValueType = Int32; };
template <> struct InstructionValueTypeMap<DataTypeDateTime> { using InstructionValueType = UInt32; };
template <> struct InstructionValueTypeMap<DataTypeDateTime64> { using InstructionValueType = Int64; };
/// Cast value from integer to string, making sure digits number in result string is no less than total_digits by padding leading '0'.
String padValue(UInt32 val, size_t min_digits)
{
String str = std::to_string(val);
auto length = str.size();
if (length >= min_digits)
return str;
String paddings(min_digits - length, '0');
return str.insert(0, paddings);
}
constexpr std::string_view weekdaysFull[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
constexpr std::string_view weekdaysShort[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
constexpr std::string_view monthsFull[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
constexpr std::string_view monthsShort[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
/** formatDateTime(time, 'format')
* Performs formatting of time, according to provided format.
*
* This function is optimized with an assumption, that the resulting strings are fixed width.
* (This assumption is fulfilled for currently supported formatting options).
*
* It is implemented in two steps.
* At first step, it creates a template of zeros, literal characters, whitespaces, etc.
* and quickly fills resulting character array (string column) with this format.
* At second step, it walks across the resulting character array and modifies/replaces specific characters,
* by calling some functions by pointers and shifting cursor by specified amount.
*
* Advantages:
* - memcpy is mostly unrolled;
* - low number of arithmetic ops due to pre-filled template;
* - for somewhat reason, function by pointer call is faster than switch/case.
*
* Possible further optimization options:
* - slightly interleave first and second step for better cache locality
* (but it has no sense when character array fits in L1d cache);
* - avoid indirect function calls and inline functions with JIT compilation.
*
* Performance on Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz:
*
* WITH formatDateTime(now() + number, '%H:%i:%S') AS x SELECT count() FROM system.numbers WHERE NOT ignore(x);
* - 97 million rows per second per core;
*
* WITH formatDateTime(toDateTime('2018-01-01 00:00:00') + number, '%F %T') AS x SELECT count() FROM system.numbers WHERE NOT ignore(x)
* - 71 million rows per second per core;
*
* select count() from (select formatDateTime(t, '%m/%d/%Y %H:%i:%S') from (select toDateTime('2018-01-01 00:00:00')+number as t from numbers(100000000)));
* - 53 million rows per second per core;
*
* select count() from (select formatDateTime(t, 'Hello %Y World') from (select toDateTime('2018-01-01 00:00:00')+number as t from numbers(100000000)));
* - 138 million rows per second per core;
*
* PS. We can make this function to return FixedString. Currently it returns String.
*/
template <typename Name, SupportInteger support_integer, FormatSyntax format_syntax>
class FunctionFormatDateTimeImpl : public IFunction
{
private:
/// Time is either UInt32 for DateTime or UInt16 for Date.
template <typename F>
static bool castType(const IDataType * type, F && f)
{
return castTypeToEither<
DataTypeInt8,
DataTypeUInt8,
DataTypeInt16,
DataTypeUInt16,
DataTypeInt32,
DataTypeUInt32,
DataTypeInt64,
DataTypeUInt64>(type, std::forward<F>(f));
}
template <typename Time>
class Instruction
{
public:
/// Joda format generally requires capturing extra variables (i.e. holding state) which is more convenient with
/// std::function and std::bind. Unfortunately, std::function causes a performance degradation by 0.45x compared to raw function
/// pointers. For MySQL format, we generally prefer raw function pointers. Because of the special case that not all formatters are
/// fixed-width formatters (see mysqlLiteral instruction), we still need to be able to store state. For that reason, we use member
/// function pointers instead of static function pointers.
using FuncMysql = size_t (Instruction<Time>::*)(char *, Time, UInt64, UInt32, const DateLUTImpl &);
FuncMysql func_mysql = nullptr;
using FuncJoda = std::function<size_t(char *, Time, UInt64, UInt32, const DateLUTImpl &)>;
FuncJoda func_joda = nullptr;
/// extra_shift is only used in MySQL format syntax. It is always 0 in Joda format syntax.
size_t extra_shift = 0;
// Holds literal characters that will be copied into the output. Used by the mysqlLiteral instruction.
String literal;
Instruction() = default;
void setMysqlFunc(FuncMysql && func) { func_mysql = std::move(func); }
void setJodaFunc(FuncJoda && func) { func_joda = std::move(func); }
void setLiteral(std::string_view literal_) { literal = literal_; }
void perform(char *& dest, Time source, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & timezone)
{
size_t shift = func_mysql
? std::invoke(func_mysql, this, dest, source, fractional_second, scale, timezone)
: std::invoke(func_joda, dest, source, fractional_second, scale, timezone);
dest += shift + extra_shift;
}
private:
template <typename T>
static size_t writeNumber2(char * p, T v)
{
memcpy(p, &digits100[v * 2], 2);
return 2;
}
template <typename T>
static size_t writeNumber3(char * p, T v)
{
writeNumber2(p, v / 10);
p[2] = '0' + v % 10;
return 3;
}
template <typename T>
static size_t writeNumber4(char * p, T v)
{
writeNumber2(p, v / 100);
writeNumber2(p + 2, v % 100);
return 4;
}
/// Cast content from integer to string, and append result string to buffer.
/// Make sure digits number in result string is no less than total_digits by padding leading '0'
/// Notice: '-' is not counted as digit.
/// For example:
/// val = -123, total_digits = 2 => dest = "-123"
/// val = -123, total_digits = 3 => dest = "-123"
/// val = -123, total_digits = 4 => dest = "-0123"
static size_t writeNumberWithPadding(char * dest, std::integral auto val, size_t min_digits)
{
using T = decltype(val);
using WeightType = typename NumberTraits::Construct<is_signed_v<T>, /*is_floating*/ false, sizeof(T)>::Type;
WeightType w = 1;
WeightType n = val;
size_t digits = 0;
while (n)
{
w *= 10;
n /= 10;
++digits;
}
/// Possible sign
size_t pos = 0;
n = val;
if constexpr (is_signed_v<T>)
if (val < 0)
{
n = (~n) + 1;
dest[pos] = '-';
++pos;
}
/// Possible leading paddings
if (min_digits > digits)
{
memset(dest, '0', min_digits - digits);
pos += min_digits - digits;
}
/// Digits
while (w >= 100)
{
w /= 100;
writeNumber2(dest + pos, n / w);
pos += 2;
n = n % w;
}
if (n)
{
dest[pos] = '0' + n;
++pos;
}
return pos;
}
public:
size_t mysqlNoop(char *, Time, UInt64, UInt32, const DateLUTImpl &)
{
return 0;
}
size_t mysqlLiteral(char * dest, Time, UInt64, UInt32, const DateLUTImpl &)
{
memcpy(dest, literal.data(), literal.size());
return literal.size();
}
size_t mysqlCentury(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto year = ToYearImpl::execute(source, timezone);
auto century = year / 100;
return writeNumber2(dest, century);
}
size_t mysqlDayOfMonth(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
return writeNumber2(dest, ToDayOfMonthImpl::execute(source, timezone));
}
size_t mysqlAmericanDate(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
writeNumber2(dest, ToMonthImpl::execute(source, timezone));
writeNumber2(dest + 3, ToDayOfMonthImpl::execute(source, timezone));
writeNumber2(dest + 6, ToYearImpl::execute(source, timezone) % 100);
return 8;
}
size_t mysqlDayOfMonthSpacePadded(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto day = ToDayOfMonthImpl::execute(source, timezone);
if (day < 10)
dest[1] = '0' + day;
else
writeNumber2(dest, day);
return 2;
}
size_t mysqlISO8601Date(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
writeNumber4(dest, ToYearImpl::execute(source, timezone));
writeNumber2(dest + 5, ToMonthImpl::execute(source, timezone));
writeNumber2(dest + 8, ToDayOfMonthImpl::execute(source, timezone));
return 10;
}
size_t mysqlDayOfYear(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
return writeNumber3(dest, ToDayOfYearImpl::execute(source, timezone));
}
size_t mysqlMonth(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
return writeNumber2(dest, ToMonthImpl::execute(source, timezone));
}
static size_t monthOfYearText(char * dest, Time source, bool abbreviate, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto month = ToMonthImpl::execute(source, timezone);
std::string_view str_view = abbreviate ? monthsShort[month - 1] : monthsFull[month - 1];
memcpy(dest, str_view.data(), str_view.size());
return str_view.size();
}
size_t mysqlMonthOfYearTextShort(char * dest, Time source, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & timezone)
{
return monthOfYearText(dest, source, true, fractional_second, scale, timezone);
}
size_t mysqlMonthOfYearTextLong(char * dest, Time source, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & timezone)
{
return monthOfYearText(dest, source, false, fractional_second, scale, timezone);
}
size_t mysqlDayOfWeek(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
*dest = '0' + ToDayOfWeekImpl::execute(source, 0, timezone);
return 1;
}
static size_t dayOfWeekText(char * dest, Time source, bool abbreviate, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto week_day = ToDayOfWeekImpl::execute(source, 0, timezone);
if (week_day == 7)
week_day = 0;
std::string_view str_view = abbreviate ? weekdaysShort[week_day] : weekdaysFull[week_day];
memcpy(dest, str_view.data(), str_view.size());
return str_view.size();
}
size_t mysqlDayOfWeekTextShort(char * dest, Time source, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & timezone)
{
return dayOfWeekText(dest, source, true, fractional_second, scale, timezone);
}
size_t mysqlDayOfWeekTextLong(char * dest, Time source, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & timezone)
{
return dayOfWeekText(dest, source, false, fractional_second, scale, timezone);
}
size_t mysqlDayOfWeek0To6(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto day = ToDayOfWeekImpl::execute(source, 0, timezone);
*dest = '0' + (day == 7 ? 0 : day);
return 1;
}
size_t mysqlISO8601Week(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
return writeNumber2(dest, ToISOWeekImpl::execute(source, timezone));
}
size_t mysqlISO8601Year2(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
return writeNumber2(dest, ToISOYearImpl::execute(source, timezone) % 100);
}
size_t mysqlISO8601Year4(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
return writeNumber4(dest, ToISOYearImpl::execute(source, timezone));
}
size_t mysqlYear2(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
return writeNumber2(dest, ToYearImpl::execute(source, timezone) % 100);
}
size_t mysqlYear4(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
return writeNumber4(dest, ToYearImpl::execute(source, timezone));
}
size_t mysqlHour24(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
return writeNumber2(dest, ToHourImpl::execute(source, timezone));
}
size_t mysqlHour12(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto x = ToHourImpl::execute(source, timezone);
return writeNumber2(dest, x == 0 ? 12 : (x > 12 ? x - 12 : x));
}
size_t mysqlMinute(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
return writeNumber2(dest, ToMinuteImpl::execute(source, timezone));
}
static size_t AMPM(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone) // NOLINT
{
auto hour = ToHourImpl::execute(source, timezone);
dest[0] = hour >= 12 ? 'P' : 'A';
dest[1] = 'M';
return 2;
}
size_t mysqlAMPM(char * dest, Time source, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & timezone)
{
return AMPM(dest, source, fractional_second, scale, timezone);
}
size_t mysqlHHMM24(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
writeNumber2(dest, ToHourImpl::execute(source, timezone));
writeNumber2(dest + 3, ToMinuteImpl::execute(source, timezone));
return 5;
}
size_t mysqlHHMM12(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto hour = ToHourImpl::execute(source, timezone);
writeNumber2(dest, hour == 0 ? 12 : (hour > 12 ? hour - 12 : hour));
writeNumber2(dest + 3, ToMinuteImpl::execute(source, timezone));
dest[6] = hour >= 12 ? 'P' : 'A';
return 8;
}
size_t mysqlSecond(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
return writeNumber2(dest, ToSecondImpl::execute(source, timezone));
}
size_t mysqlFractionalSecond(char * dest, Time /*source*/, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & /*timezone*/)
{
if (scale == 0)
scale = 6;
for (Int64 i = scale, value = fractional_second; i > 0; --i)
{
dest[i - 1] += value % 10;
value /= 10;
}
return scale;
}
/// Same as mysqlFractionalSecond but prints a single zero if the value has no fractional seconds
size_t mysqlFractionalSecondSingleZero(char * dest, Time /*source*/, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & /*timezone*/)
{
if (scale == 0)
scale = 1;
for (Int64 i = scale, value = fractional_second; i > 0; --i)
{
dest[i - 1] += value % 10;
value /= 10;
}
return scale;
}
size_t mysqlISO8601Time(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone) // NOLINT
{
writeNumber2(dest, ToHourImpl::execute(source, timezone));
writeNumber2(dest + 3, ToMinuteImpl::execute(source, timezone));
writeNumber2(dest + 6, ToSecondImpl::execute(source, timezone));
return 8;
}
size_t mysqlTimezoneOffset(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto offset = TimezoneOffsetImpl::execute(source, timezone);
if (offset < 0)
{
*dest = '-';
offset = -offset;
}
writeNumber2(dest + 1, offset / 3600);
writeNumber2(dest + 3, offset % 3600 / 60);
return 5;
}
size_t mysqlQuarter(char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
*dest = '0' + ToQuarterImpl::execute(source, timezone);
return 1;
}
template <typename Literal>
static size_t jodaLiteral(const Literal & literal, char * dest, Time, UInt64, UInt32, const DateLUTImpl &)
{
memcpy(dest, literal.data(), literal.size());
return literal.size();
}
static size_t jodaEra(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto year = static_cast<Int32>(ToYearImpl::execute(source, timezone));
String res;
if (min_represent_digits <= 3)
res = static_cast<Int32>(year) > 0 ? "AD" : "BC";
else
res = static_cast<Int32>(year) > 0 ? "Anno Domini" : "Before Christ";
memcpy(dest, res.data(), res.size());
return res.size();
}
static size_t jodaCenturyOfEra(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto year = static_cast<Int32>(ToYearImpl::execute(source, timezone));
year = (year < 0 ? -year : year);
return writeNumberWithPadding(dest, year / 100, min_represent_digits);
}
static size_t jodaYearOfEra(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto year = static_cast<Int32>(ToYearImpl::execute(source, timezone));
if (min_represent_digits == 2)
return writeNumberWithPadding(dest, std::abs(year) % 100, 2);
else
{
year = year <= 0 ? std::abs(year - 1) : year;
return writeNumberWithPadding(dest, year, min_represent_digits);
}
}
static size_t jodaDayOfWeek1Based(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto week_day = ToDayOfWeekImpl::execute(source, 0, timezone);
return writeNumberWithPadding(dest, week_day, min_represent_digits);
}
static size_t jodaDayOfWeekText(size_t min_represent_digits, char * dest, Time source, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & timezone)
{
bool abbreviate = min_represent_digits <= 3;
return dayOfWeekText(dest, source, abbreviate, fractional_second, scale, timezone);
}
static size_t jodaYear(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto year = static_cast<Int32>(ToYearImpl::execute(source, timezone));
if (min_represent_digits == 2)
{
year = std::abs(year);
auto two_digit_year = year % 100;
return writeNumberWithPadding(dest, two_digit_year, 2);
}
else
return writeNumberWithPadding(dest, year, min_represent_digits);
}
static size_t jodaWeekYear(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto week_year = ToWeekYearImpl::execute(source, timezone);
return writeNumberWithPadding(dest, week_year, min_represent_digits);
}
static size_t jodaWeekOfWeekYear(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto week_of_weekyear = ToWeekOfWeekYearImpl::execute(source, timezone);
return writeNumberWithPadding(dest, week_of_weekyear, min_represent_digits);
}
static size_t jodaDayOfYear(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto day_of_year = ToDayOfYearImpl::execute(source, timezone);
return writeNumberWithPadding(dest, day_of_year, min_represent_digits);
}
static size_t jodaMonthOfYear(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto month_of_year = ToMonthImpl::execute(source, timezone);
return writeNumberWithPadding(dest, month_of_year, min_represent_digits);
}
static size_t jodaMonthOfYearText(size_t min_represent_digits, char * dest, Time source, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & timezone)
{
bool abbreviate = min_represent_digits <= 3;
return monthOfYearText(dest, source, abbreviate, fractional_second, scale, timezone);
}
static size_t jodaDayOfMonth(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto day_of_month = ToDayOfMonthImpl::execute(source, timezone);
return writeNumberWithPadding(dest, day_of_month, min_represent_digits);
}
static size_t jodaHalfDayOfDay(
size_t /*min_represent_digits*/, char * dest, Time source, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & timezone)
{
return AMPM(dest, source, fractional_second, scale, timezone);
}
static size_t jodaHourOfHalfDay(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto hour = ToHourImpl::execute(source, timezone) % 12;
return writeNumberWithPadding(dest, hour, min_represent_digits);
}
static size_t jodaClockHourOfHalfDay(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto hour = ToHourImpl::execute(source, timezone) ;
hour = (hour + 11) % 12 + 1;
return writeNumberWithPadding(dest, hour, min_represent_digits);
}
static size_t jodaHourOfDay(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto hour = ToHourImpl::execute(source, timezone) ;
return writeNumberWithPadding(dest, hour, min_represent_digits);
}
static size_t jodaClockHourOfDay(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto hour = ToHourImpl::execute(source, timezone);
hour = (hour + 23) % 24 + 1;
return writeNumberWithPadding(dest, hour, min_represent_digits);
}
static size_t jodaMinuteOfHour(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto minute_of_hour = ToMinuteImpl::execute(source, timezone);
return writeNumberWithPadding(dest, minute_of_hour, min_represent_digits);
}
static size_t jodaSecondOfMinute(size_t min_represent_digits, char * dest, Time source, UInt64, UInt32, const DateLUTImpl & timezone)
{
auto second_of_minute = ToSecondImpl::execute(source, timezone);
return writeNumberWithPadding(dest, second_of_minute, min_represent_digits);
}
static size_t jodaFractionOfSecond(size_t min_represent_digits, char * dest, Time /*source*/, UInt64 fractional_second, UInt32 scale, const DateLUTImpl & /*timezone*/)
{
if (min_represent_digits > 9)
min_represent_digits = 9;
if (fractional_second == 0)
{
for (UInt64 i = 0; i < min_represent_digits; ++i)
dest[i] = '0';
return min_represent_digits;
}
auto str = toString(fractional_second);
if (min_represent_digits > scale)
{
for (UInt64 i = 0; i < min_represent_digits - scale; ++i)
str += '0';
}
else if (min_represent_digits < scale)
{
str = str.substr(0, min_represent_digits);
}
memcpy(dest, str.data(), str.size());
return min_represent_digits;
}
static size_t jodaTimezone(size_t min_represent_digits, char * dest, Time /*source*/, UInt64, UInt32, const DateLUTImpl & timezone)
{
if (min_represent_digits <= 3)
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Short name time zone is not yet supported");
auto str = timezone.getTimeZone();
memcpy(dest, str.data(), str.size());
return str.size();
}
};
[[noreturn]] static void throwLastCharacterIsPercentException()
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "'%' must not be the last character in the format string, use '%%' instead");
}
static bool containsOnlyFixedWidthMySQLFormatters(std::string_view format, bool mysql_M_is_month_name)
{
static constexpr std::array variable_width_formatter = {'W'};
static constexpr std::array variable_width_formatter_M_is_month_name = {'W', 'M'};
for (size_t i = 0; i < format.size(); ++i)
{
switch (format[i])
{
case '%':
if (i + 1 >= format.size())
throwLastCharacterIsPercentException();
if (mysql_M_is_month_name)
{
if (std::any_of(
variable_width_formatter_M_is_month_name.begin(), variable_width_formatter_M_is_month_name.end(),
[&](char c){ return c == format[i + 1]; }))
return false;
}
else
{
if (std::any_of(
variable_width_formatter.begin(), variable_width_formatter.end(),
[&](char c){ return c == format[i + 1]; }))
return false;
}
i += 1;
continue;
default:
break;
}
}
return true;
}
const bool mysql_M_is_month_name;
const bool mysql_f_prints_single_zero;
public:
static constexpr auto name = Name::name;
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionFormatDateTimeImpl>(context); }
explicit FunctionFormatDateTimeImpl(ContextPtr context)
: mysql_M_is_month_name(context->getSettings().formatdatetime_parsedatetime_m_is_month_name)
, mysql_f_prints_single_zero(context->getSettings().formatdatetime_f_prints_single_zero)
{
}
String getName() const override
{
return name;
}
bool useDefaultImplementationForConstants() const override { return true; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1, 2}; }
bool isVariadic() const override { return true; }
size_t getNumberOfArguments() const override { return 0; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
if constexpr (support_integer == SupportInteger::Yes)
{
if (arguments.size() != 1 && arguments.size() != 2 && arguments.size() != 3)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
"Number of arguments for function {} doesn't match: passed {}, should be 1, 2 or 3",
getName(), arguments.size());
if (arguments.size() == 1 && !isInteger(arguments[0].type))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of first argument of function {} when arguments size is 1. Should be integer",
arguments[0].type->getName(), getName());
if (arguments.size() > 1 && !(isInteger(arguments[0].type) || isDate(arguments[0].type) || isDateTime(arguments[0].type) || isDate32(arguments[0].type) || isDateTime64(arguments[0].type)))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of first argument of function {} when arguments size is 2 or 3. "
"Should be a integer or a date with time",
arguments[0].type->getName(), getName());
}
else
{
if (arguments.size() != 2 && arguments.size() != 3)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
"Number of arguments for function {} doesn't match: passed {}, should be 2 or 3",
getName(), arguments.size());
if (!isDate(arguments[0].type) && !isDateTime(arguments[0].type) && !isDate32(arguments[0].type) && !isDateTime64(arguments[0].type))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of first argument of function {}. Should be a date or a date with time",
arguments[0].type->getName(), getName());
}
if (arguments.size() == 2 && !WhichDataType(arguments[1].type).isString())
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of second argument of function {}. Must be String.",
arguments[1].type->getName(), getName());
if (arguments.size() == 3 && !WhichDataType(arguments[2].type).isString())
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of third argument of function {}. Must be String.",
arguments[2].type->getName(), getName());
if (arguments.size() == 1)
return std::make_shared<DataTypeDateTime>();
return std::make_shared<DataTypeString>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, [[maybe_unused]] size_t input_rows_count) const override
{
ColumnPtr res;
if constexpr (support_integer == SupportInteger::Yes)
{
if (arguments.size() == 1)
{
if (!castType(arguments[0].type.get(), [&](const auto & type)
{
using FromDataType = std::decay_t<decltype(type)>;
res = ConvertImpl<FromDataType, DataTypeDateTime, Name>::execute(arguments, result_type, input_rows_count);
return true;
}))
{
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
"Illegal column {} of function {}, must be Integer, Date, Date32, DateTime "
"or DateTime64 when arguments size is 1.",
arguments[0].column->getName(), getName());
}
}
else
{
if (!castType(arguments[0].type.get(), [&](const auto & type)
{
using FromDataType = std::decay_t<decltype(type)>;
if (!(res = executeType<FromDataType>(arguments, result_type)))
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
"Illegal column {} of function {}, must be Integer, Date, Date32, DateTime or DateTime64.",
arguments[0].column->getName(), getName());
return true;
}))
{
if (!((res = executeType<DataTypeDate>(arguments, result_type))
|| (res = executeType<DataTypeDate32>(arguments, result_type))
|| (res = executeType<DataTypeDateTime>(arguments, result_type))
|| (res = executeType<DataTypeDateTime64>(arguments, result_type))))
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
"Illegal column {} of function {}, must be Integer or DateTime.",
arguments[0].column->getName(), getName());
}
}
}
else
{
if (!((res = executeType<DataTypeDate>(arguments, result_type))
|| (res = executeType<DataTypeDate32>(arguments, result_type))
|| (res = executeType<DataTypeDateTime>(arguments, result_type))
|| (res = executeType<DataTypeDateTime64>(arguments, result_type))))
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
"Illegal column {} of function {}, must be Date or DateTime.",
arguments[0].column->getName(), getName());
}
return res;
}
template <typename DataType>
ColumnPtr executeType(const ColumnsWithTypeAndName & arguments, const DataTypePtr &) const
{
auto * times = checkAndGetColumn<typename DataType::ColumnType>(arguments[0].column.get());
if (!times)
return nullptr;
const ColumnConst * format_column = checkAndGetColumnConst<ColumnString>(arguments[1].column.get());
if (!format_column)
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
"Illegal column {} of second ('format') argument of function {}. Must be constant string.",
arguments[1].column->getName(), getName());
String format = format_column->getValue<String>();
UInt32 scale [[maybe_unused]] = 0;
if constexpr (std::is_same_v<DataType, DataTypeDateTime64>)
scale = times->getScale();
/// For MySQL, we support two modes of execution:
///
/// - All formatters in the format string are fixed-width. As a result, all output rows will have the same width and structure. We
/// take advantage of this and
/// 1. create a "template" with placeholders from the format string,
/// 2. allocate a result column large enough to store the template on each row,
/// 3. copy the template into each result row,
/// 4. run instructions which replace the formatter placeholders. All other parts of the template (e.g. whitespaces) are already
/// as desired and instructions skip over them (see 'extra_shift' in the formatters).
///
/// - The format string contains at least one variable-width formatter. Output rows will potentially be of different size.
/// Steps 1. and 2. are performed as above (the result column is allocated based on a worst-case size estimation). The result
/// column rows are NOT populated with the template and left uninitialized. We run the normal instructions for formatters AND
/// instructions that copy literal characters before/between/after formatters. As a result, each byte of each result row is
/// written which is obviously slow.
bool mysql_with_only_fixed_length_formatters = (format_syntax == FormatSyntax::MySQL) ? containsOnlyFixedWidthMySQLFormatters(format, mysql_M_is_month_name) : false;
using T = typename InstructionValueTypeMap<DataType>::InstructionValueType;
std::vector<Instruction<T>> instructions;
String out_template;
size_t out_template_size = parseFormat(format, instructions, scale, mysql_with_only_fixed_length_formatters, out_template);
const DateLUTImpl * time_zone_tmp = nullptr;
if (castType(arguments[0].type.get(), [&]([[maybe_unused]] const auto & type) { return true; }))
time_zone_tmp = &extractTimeZoneFromFunctionArguments(arguments, 2, 0);
else if (std::is_same_v<DataType, DataTypeDateTime64> || std::is_same_v<DataType, DataTypeDateTime>)
time_zone_tmp = &extractTimeZoneFromFunctionArguments(arguments, 2, 0);
else
time_zone_tmp = &DateLUT::instance();
const DateLUTImpl & time_zone = *time_zone_tmp;
const auto & vec = times->getData();
auto col_res = ColumnString::create();
auto & res_data = col_res->getChars();
auto & res_offsets = col_res->getOffsets();
res_data.resize(vec.size() * (out_template_size + 1));
res_offsets.resize(vec.size());
if constexpr (format_syntax == FormatSyntax::MySQL)
{
if (mysql_with_only_fixed_length_formatters)
{
/// Fill result with template.
{
const UInt8 * const begin = res_data.data();
const UInt8 * const end = res_data.data() + res_data.size();
UInt8 * pos = res_data.data();
if (pos < end)
{
memcpy(pos, out_template.data(), out_template_size + 1); /// With zero terminator. mystring[mystring.size()] = '\0' is guaranteed since C++11.
pos += out_template_size + 1;
}
/// Copy exponentially growing ranges.
while (pos < end)
{
size_t bytes_to_copy = std::min(pos - begin, end - pos);
memcpy(pos, begin, bytes_to_copy);
pos += bytes_to_copy;
}
}
}
}
auto * begin = reinterpret_cast<char *>(res_data.data());
auto * pos = begin;
for (size_t i = 0; i < vec.size(); ++i)
{
if constexpr (std::is_same_v<DataType, DataTypeDateTime64>)
{
auto c = DecimalUtils::split(vec[i], scale);
// -1.123 splits to -1 / 0.123
if (vec[i].value < 0 && c.fractional)
{
using F = typename DataType::FieldType;
c.fractional = DecimalUtils::scaleMultiplier<F>(scale) + (c.whole ? F(-1) : F(1)) * c.fractional;
--c.whole;
}
for (auto & instruction : instructions)
instruction.perform(pos, static_cast<Int64>(c.whole), c.fractional, scale, time_zone);
}
else
{
for (auto & instruction : instructions)
instruction.perform(pos, static_cast<UInt32>(vec[i]), 0, 0, time_zone);
}
*pos++ = '\0';
res_offsets[i] = pos - begin;
}
res_data.resize(pos - begin);
return col_res;
}
template <typename T>
size_t parseFormat(const String & format, std::vector<Instruction<T>> & instructions, UInt32 scale, bool mysql_with_only_fixed_length_formatters, String & out_template) const
{
static_assert(format_syntax == FormatSyntax::MySQL || format_syntax == FormatSyntax::Joda);
if constexpr (format_syntax == FormatSyntax::MySQL)
return parseMySQLFormat(format, instructions, scale, mysql_with_only_fixed_length_formatters, out_template);
else
return parseJodaFormat(format, instructions, scale, mysql_with_only_fixed_length_formatters, out_template);
}
template <typename T>
size_t parseMySQLFormat(const String & format, std::vector<Instruction<T>> & instructions, UInt32 scale, bool mysql_with_only_fixed_length_formatters, String & out_template) const
{
auto add_extra_shift = [&](size_t amount)
{
if (instructions.empty())
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlNoop);
instructions.push_back(std::move(instruction));
}
instructions.back().extra_shift += amount;
};
auto add_literal_instruction = [&](std::string_view literal)
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlLiteral);
instruction.setLiteral(literal);
instructions.push_back(std::move(instruction));
};
auto add_extra_shift_or_literal_instruction = [&](std::string_view literal)
{
if (mysql_with_only_fixed_length_formatters)
add_extra_shift(literal.size());
else
add_literal_instruction(literal);
};
auto add_time_instruction = [&]([[maybe_unused]] typename Instruction<T>::FuncMysql && func, [[maybe_unused]] std::string_view literal)
{
/// DateTime/DateTime64 --> insert instruction
/// Other types cannot provide the requested data --> write out template
if constexpr (is_any_of<T, UInt32, Int64>)
{
Instruction<T> instruction;
instruction.setMysqlFunc(std::move(func));
instructions.push_back(std::move(instruction));
}
else
add_extra_shift_or_literal_instruction(literal);
};
Pos pos = format.data();
Pos const end = format.data() + format.size();
while (true)
{
Pos const percent_pos = find_first_symbols<'%'>(pos, end);
if (percent_pos < end)
{
if (pos < percent_pos)
{
/// Handle characters before next %
add_extra_shift_or_literal_instruction(std::string_view(pos, percent_pos - pos));
out_template += String(pos, percent_pos - pos);
}
pos = percent_pos + 1;
if (pos >= end)
throwLastCharacterIsPercentException();
switch (*pos)
{
// Abbreviated weekday [Mon-Sun]
case 'a':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlDayOfWeekTextShort);
instructions.push_back(std::move(instruction));
out_template += "Mon";
break;
}
// Abbreviated month [Jan-Dec]
case 'b':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlMonthOfYearTextShort);
instructions.push_back(std::move(instruction));
out_template += "Jan";
break;
}
// Month as a integer number (01-12)
case 'c':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlMonth);
instructions.push_back(std::move(instruction));
out_template += "00";
break;
}
// Year, divided by 100, zero-padded
case 'C':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlCentury);
instructions.push_back(std::move(instruction));
out_template += "00";
break;
}
// Day of month, zero-padded (01-31)
case 'd':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlDayOfMonth);
instructions.push_back(std::move(instruction));
out_template += "00";
break;
}
// Short MM/DD/YY date, equivalent to %m/%d/%y
case 'D':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlAmericanDate);
instructions.push_back(std::move(instruction));
out_template += "00/00/00";
break;
}
// Day of month, space-padded ( 1-31) 23
case 'e':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlDayOfMonthSpacePadded);
instructions.push_back(std::move(std::move(instruction)));
out_template += " 0";
break;
}
// Depending on a setting
// - Full month [January-December] OR
// - Minute of hour range [0, 59]
case 'M':
{
Instruction<T> instruction;
if (mysql_M_is_month_name)
{
instruction.setMysqlFunc(&Instruction<T>::mysqlMonthOfYearTextLong);
instructions.push_back(std::move(instruction));
out_template += "September"; /// longest possible month name
}
else
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlMinute, val);
out_template += val;
}
break;
}
// Fractional seconds
case 'f':
{
/// If the time data type has no fractional part, we print (default) '000000' or (deprecated) '0' as fractional part.
if (mysql_f_prints_single_zero)
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlFractionalSecondSingleZero);
instructions.push_back(std::move(instruction));
out_template += String(scale == 0 ? 1 : scale, '0');
}
else
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlFractionalSecond);
instructions.push_back(std::move(instruction));
out_template += String(scale == 0 ? 6 : scale, '0');
}
break;
}
// Short YYYY-MM-DD date, equivalent to %Y-%m-%d 2001-08-23
case 'F':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlISO8601Date);
instructions.push_back(std::move(instruction));
out_template += "0000-00-00";
break;
}
// Last two digits of year of ISO 8601 week number (see %G)
case 'g':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlISO8601Year2);
instructions.push_back(std::move(instruction));
out_template += "00";
break;
}
// Year of ISO 8601 week number (see %V)
case 'G':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlISO8601Year4);
instructions.push_back(std::move(instruction));
out_template += "0000";
break;
}
// Day of the year (001-366) 235
case 'j':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlDayOfYear);
instructions.push_back(std::move(instruction));
out_template += "000";
break;
}
// Month as a integer number (01-12)
case 'm':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlMonth);
instructions.push_back(std::move(instruction));
out_template += "00";
break;
}
// ISO 8601 weekday as number with Monday as 1 (1-7)
case 'u':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlDayOfWeek);
instructions.push_back(std::move(instruction));
out_template += "0";
break;
}
// ISO 8601 week number (01-53)
case 'V':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlISO8601Week);
instructions.push_back(std::move(instruction));
out_template += "00";
break;
}
// Weekday as a decimal number with Sunday as 0 (0-6) 4
case 'w':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlDayOfWeek0To6);
instructions.push_back(std::move(instruction));
out_template += "0";
break;
}
// Full weekday [Monday-Sunday]
case 'W':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlDayOfWeekTextLong);
instructions.push_back(std::move(instruction));
out_template += "Wednesday"; /// longest possible weekday name
break;
}
// Two digits year
case 'y':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlYear2);
instructions.push_back(std::move(instruction));
out_template += "00";
break;
}
// Four digits year
case 'Y':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlYear4);
instructions.push_back(std::move(instruction));
out_template += "0000";
break;
}
// Quarter (1-4)
case 'Q':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlQuarter);
instructions.push_back(std::move(instruction));
out_template += "0";
break;
}
// Offset from UTC timezone as +hhmm or -hhmm
case 'z':
{
Instruction<T> instruction;
instruction.setMysqlFunc(&Instruction<T>::mysqlTimezoneOffset);
instructions.push_back(std::move(instruction));
out_template += "+0000";
break;
}
/// Time components. If the argument is Date, not a DateTime, then this components will have default value.
// AM or PM
case 'p':
{
static constexpr std::string_view val = "AM";
add_time_instruction(&Instruction<T>::mysqlAMPM, val);
out_template += val;
break;
}
// 12-hour HH:MM time, equivalent to %h:%i %p 2:55 PM
case 'r':
{
static constexpr std::string_view val = "12:00 AM";
add_time_instruction(&Instruction<T>::mysqlHHMM12, val);
out_template += val;
break;
}
// 24-hour HH:MM time, equivalent to %H:%i 14:55
case 'R':
{
static constexpr std::string_view val = "00:00";
add_time_instruction(&Instruction<T>::mysqlHHMM24, val);
out_template += val;
break;
}
// Seconds
case 's':
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlSecond, val);
out_template += val;
break;
}
// Seconds
case 'S':
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlSecond, val);
out_template += val;
break;
}
// ISO 8601 time format (HH:MM:SS), equivalent to %H:%i:%S 14:55:02
case 'T':
{
static constexpr std::string_view val = "00:00:00";
add_time_instruction(&Instruction<T>::mysqlISO8601Time, val);
out_template += val;
break;
}
// Hour in 12h format (01-12)
case 'h':
{
static constexpr std::string_view val = "12";
add_time_instruction(&Instruction<T>::mysqlHour12, val);
out_template += val;
break;
}
// Hour in 24h format (00-23)
case 'H':
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlHour24, val);
out_template += val;
break;
}
// Minute of hour range [0, 59]
case 'i':
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlMinute, val);
out_template += val;
break;
}
// Hour in 12h format (01-12)
case 'I':
{
static constexpr std::string_view val = "12";
add_time_instruction(&Instruction<T>::mysqlHour12, val);
out_template += val;
break;
}
// Hour in 24h format (00-23)
case 'k':
{
static constexpr std::string_view val = "00";
add_time_instruction(&Instruction<T>::mysqlHour24, val);
out_template += val;
break;
}
// Hour in 12h format (01-12)
case 'l':
{
static constexpr std::string_view val = "12";
add_time_instruction(&Instruction<T>::mysqlHour12, val);
out_template += val;
break;
}
case 't':
{
static constexpr std::string_view val = "\t";
add_extra_shift_or_literal_instruction(val);
out_template += val;
break;
}
case 'n':
{
static constexpr std::string_view val = "\n";
add_extra_shift_or_literal_instruction(val);
out_template += val;
break;
}
// Escaped literal characters.
case '%':
{
static constexpr std::string_view val = "%";
add_extra_shift_or_literal_instruction(val);
out_template += val;
break;
}
// Unimplemented
case 'U':
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "format is not supported for WEEK (Sun-Sat)");
case 'v':
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "format is not supported for WEEK (Mon-Sun)");
case 'x':
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "format is not supported for YEAR for week (Mon-Sun)");
case 'X':
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "format is not supported for YEAR for week (Sun-Sat)");
default:
throw Exception(
ErrorCodes::BAD_ARGUMENTS,
"Incorrect syntax '{}', symbol is not supported '{}' for function {}",
format,
*pos,
getName());
}
++pos;
}
else
{
/// Handle characters after last %
add_extra_shift_or_literal_instruction(std::string_view(pos, end - pos));
out_template += String(pos, end - pos);
break;
}
}
return out_template.size();
}
template <typename T>
size_t parseJodaFormat(const String & format, std::vector<Instruction<T>> & instructions, UInt32, bool, String &) const
{
/// If the argument was DateTime, add instruction for printing. If it was date, just append default literal
auto add_instruction = [&]([[maybe_unused]] typename Instruction<T>::FuncJoda && func, [[maybe_unused]] const String & default_literal)
{
if constexpr (is_any_of<T, UInt32, Int64>)
{
Instruction<T> instruction;
instruction.setJodaFunc(std::move(func));
instructions.push_back(std::move(instruction));
}
else
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::template jodaLiteral<String>, default_literal));
instructions.push_back(std::move(instruction));
}
};
size_t reserve_size = 0;
Pos pos = format.data();
Pos end = format.data() + format.size();
while (pos < end)
{
Pos cur_token = pos;
// Literal case
if (*cur_token == '\'')
{
// Case 1: 2 consecutive single quote
if (pos + 1 < end && *(pos + 1) == '\'')
{
Instruction<T> instruction;
std::string_view literal(cur_token, 1);
instruction.setJodaFunc(std::bind_front(&Instruction<T>::template jodaLiteral<decltype(literal)>, literal));
instructions.push_back(std::move(instruction));
++reserve_size;
pos += 2;
}
else
{
// Case 2: find closing single quote
Int64 count = numLiteralChars(cur_token + 1, end);
if (count == -1)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "No closing single quote for literal");
else
{
for (Int64 i = 1; i <= count; i++)
{
Instruction<T> instruction;
std::string_view literal(cur_token + i, 1);
instruction.setJodaFunc(std::bind_front(&Instruction<T>::template jodaLiteral<decltype(literal)>, literal));
instructions.push_back(std::move(instruction));
++reserve_size;
if (*(cur_token + i) == '\'')
i += 1;
}
pos += count + 2;
}
}
}
else
{
int repetitions = 1;
++pos;
while (pos < end && *cur_token == *pos)
{
++repetitions;
++pos;
}
switch (*cur_token)
{
case 'G':
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaEra, repetitions));
instructions.push_back(std::move(instruction));
reserve_size += repetitions <= 3 ? 2 : 13;
break;
}
case 'C':
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaCenturyOfEra, repetitions));
instructions.push_back(std::move(instruction));
/// Year range [1900, 2299]
reserve_size += std::max(repetitions, 2);
break;
}
case 'Y':
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaYearOfEra, repetitions));
instructions.push_back(std::move(instruction));
/// Year range [1900, 2299]
reserve_size += repetitions == 2 ? 2 : std::max(repetitions, 4);
break;
}
case 'x':
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaWeekYear, repetitions));
instructions.push_back(std::move(instruction));
/// weekyear range [1900, 2299]
reserve_size += std::max(repetitions, 4);
break;
}
case 'w':
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaWeekOfWeekYear, repetitions));
instructions.push_back(std::move(instruction));
/// Week of weekyear range [1, 52]
reserve_size += std::max(repetitions, 2);
break;
}
case 'e':
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaDayOfWeek1Based, repetitions));
instructions.push_back(std::move(instruction));
/// Day of week range [1, 7]
reserve_size += std::max(repetitions, 1);
break;
}
case 'E':
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaDayOfWeekText, repetitions));
instructions.push_back(std::move(instruction));
/// Maximum length of short name is 3, maximum length of full name is 9.
reserve_size += repetitions <= 3 ? 3 : 9;
break;
}
case 'y':
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaYear, repetitions));
instructions.push_back(std::move(instruction));
/// Year range [1900, 2299]
reserve_size += repetitions == 2 ? 2 : std::max(repetitions, 4);
break;
}
case 'D':
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaDayOfYear, repetitions));
instructions.push_back(std::move(instruction));
/// Day of year range [1, 366]
reserve_size += std::max(repetitions, 3);
break;
}
case 'M':
{
if (repetitions <= 2)
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaMonthOfYear, repetitions));
instructions.push_back(std::move(instruction));
/// Month of year range [1, 12]
reserve_size += 2;
}
else
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaMonthOfYearText, repetitions));
instructions.push_back(std::move(instruction));
/// Maximum length of short name is 3, maximum length of full name is 9.
reserve_size += repetitions <= 3 ? 3 : 9;
}
break;
}
case 'd':
{
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaDayOfMonth, repetitions));
instructions.push_back(std::move(instruction));
/// Day of month range [1, 3]
reserve_size += std::max(repetitions, 3);
break;
}
case 'a':
/// Default half day of day is "AM"
add_instruction(std::bind_front(&Instruction<T>::jodaHalfDayOfDay, repetitions), "AM");
reserve_size += 2;
break;
case 'K':
/// Default hour of half day is 0
add_instruction(
std::bind_front(&Instruction<T>::jodaHourOfHalfDay, repetitions), padValue(0, repetitions));
/// Hour of half day range [0, 11]
reserve_size += std::max(repetitions, 2);
break;
case 'h':
/// Default clock hour of half day is 12
add_instruction(
std::bind_front(&Instruction<T>::jodaClockHourOfHalfDay, repetitions),
padValue(12, repetitions));
/// Clock hour of half day range [1, 12]
reserve_size += std::max(repetitions, 2);
break;
case 'H':
/// Default hour of day is 0
add_instruction(std::bind_front(&Instruction<T>::jodaHourOfDay, repetitions), padValue(0, repetitions));
/// Hour of day range [0, 23]
reserve_size += std::max(repetitions, 2);
break;
case 'k':
/// Default clock hour of day is 24
add_instruction(std::bind_front(&Instruction<T>::jodaClockHourOfDay, repetitions), padValue(24, repetitions));
/// Clock hour of day range [1, 24]
reserve_size += std::max(repetitions, 2);
break;
case 'm':
/// Default minute of hour is 0
add_instruction(std::bind_front(&Instruction<T>::jodaMinuteOfHour, repetitions), padValue(0, repetitions));
/// Minute of hour range [0, 59]
reserve_size += std::max(repetitions, 2);
break;
case 's':
/// Default second of minute is 0
add_instruction(std::bind_front(&Instruction<T>::jodaSecondOfMinute, repetitions), padValue(0, repetitions));
/// Second of minute range [0, 59]
reserve_size += std::max(repetitions, 2);
break;
case 'S':
{
/// Default fraction of second is 0
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaFractionOfSecond, repetitions));
instructions.push_back(std::move(instruction));
/// 'S' repetitions range [0, 9]
reserve_size += repetitions <= 9 ? repetitions : 9;
break;
}
case 'z':
{
if (repetitions <= 3)
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Short name time zone is not yet supported");
Instruction<T> instruction;
instruction.setJodaFunc(std::bind_front(&Instruction<T>::jodaTimezone, repetitions));
instructions.push_back(std::move(instruction));
/// Longest length of full name of time zone is 32.
reserve_size += 32;
break;
}
case 'Z':
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "format is not supported for TIMEZONE_OFFSET_ID");
default:
{
if (isalpha(*cur_token))
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "format is not supported for {}", String(cur_token, repetitions));
Instruction<T> instruction;
std::string_view literal(cur_token, pos - cur_token);
instruction.setJodaFunc(std::bind_front(&Instruction<T>::template jodaLiteral<decltype(literal)>, literal));
instructions.push_back(std::move(instruction));
reserve_size += pos - cur_token;
break;
}
}
}
}
return reserve_size;
}
};
struct NameFormatDateTime
{
static constexpr auto name = "formatDateTime";
};
struct NameFromUnixTime
{
static constexpr auto name = "fromUnixTimestamp";
};
struct NameFormatDateTimeInJodaSyntax
{
static constexpr auto name = "formatDateTimeInJodaSyntax";
};
struct NameFromUnixTimeInJodaSyntax
{
static constexpr auto name = "fromUnixTimestampInJodaSyntax";
};
using FunctionFormatDateTime = FunctionFormatDateTimeImpl<NameFormatDateTime, SupportInteger::No, FormatSyntax::MySQL>;
using FunctionFromUnixTimestamp = FunctionFormatDateTimeImpl<NameFromUnixTime, SupportInteger::Yes, FormatSyntax::MySQL>;
using FunctionFormatDateTimeInJodaSyntax = FunctionFormatDateTimeImpl<NameFormatDateTimeInJodaSyntax, SupportInteger::No, FormatSyntax::Joda>;
using FunctionFromUnixTimestampInJodaSyntax = FunctionFormatDateTimeImpl<NameFromUnixTimeInJodaSyntax, SupportInteger::Yes, FormatSyntax::Joda>;
}
REGISTER_FUNCTION(FormatDateTime)
{
factory.registerFunction<FunctionFormatDateTime>();
factory.registerAlias("DATE_FORMAT", FunctionFormatDateTime::name);
factory.registerFunction<FunctionFromUnixTimestamp>();
factory.registerAlias("FROM_UNIXTIME", FunctionFromUnixTimestamp::name);
factory.registerFunction<FunctionFormatDateTimeInJodaSyntax>();
factory.registerFunction<FunctionFromUnixTimestampInJodaSyntax>();
}
}
|