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
|
#include "catalog.h"
#include <util/generic/utility.h>
#include <util/generic/hash.h>
#include <util/string/builder.h>
#include <util/string/cast.h>
#include <util/string/split.h>
#include <library/cpp/resource/resource.h>
namespace NYql::NPg {
constexpr ui32 AnyOid = 2276;
constexpr ui32 AnyArrayOid = 2277;
using TOperators = THashMap<ui32, TOperDesc>;
using TProcs = THashMap<ui32, TProcDesc>;
using TTypes = THashMap<ui32, TTypeDesc>;
using TCasts = THashMap<ui32, TCastDesc>;
using TAggregations = THashMap<ui32, TAggregateDesc>;
using TOpClasses = THashMap<std::pair<EOpClassMethod, ui32>, TOpClassDesc>;
using TAmOps = THashMap<std::tuple<TString, ui32, ui32, ui32>, TAmOpDesc>;
using TAmProcs = THashMap<std::tuple<TString, ui32, ui32, ui32>, TAmProcDesc>;
bool IsCompatibleTo(ui32 actualType, ui32 expectedType, const TTypes& types) {
if (!actualType) {
return true;
}
if (actualType == expectedType) {
return true;
}
if (expectedType == AnyOid) {
return true;
}
if (expectedType == AnyArrayOid) {
const auto& actualDescPtr = types.FindPtr(actualType);
Y_ENSURE(actualDescPtr);
return actualDescPtr->ArrayTypeId == actualDescPtr->TypeId;
}
return false;
}
TString ArgTypesList(const TVector<ui32>& ids) {
TStringBuilder str;
str << '(';
for (ui32 i = 0; i < ids.size(); ++i) {
if (i > 0) {
str << ',';
}
str << (ids[i] ? LookupType(ids[i]).Name : "NULL");
}
str << ')';
return str;
}
class TParser {
public:
virtual ~TParser() = default;
void Do(const TString& dat) {
enum class EState {
WaitBracket,
InsideBrackets,
WaitForEndOfKey,
WaitForValue,
WaitForEndOfValue
};
EState state = EState::WaitBracket;
bool AfterBackSlash = false;
TStringBuilder key;
TStringBuilder value;
for (char c : dat) {
switch (state) {
case EState::WaitBracket: {
if (c == '{') {
state = EState::InsideBrackets;
}
break;
}
case EState::InsideBrackets: {
if (c == '}') {
state = EState::WaitBracket;
OnFinish();
continue;
}
if (c == ' ' || c == ',' || c == '\n') {
continue;
}
key.clear();
key << c;
state = EState::WaitForEndOfKey;
break;
}
case EState::WaitForEndOfKey: {
if (c != ' ') {
key << c;
continue;
}
state = EState::WaitForValue;
break;
}
case EState::WaitForValue: {
if (c != '\'') {
continue;
}
state = EState::WaitForEndOfValue;
value.clear();
break;
}
case EState::WaitForEndOfValue: {
if (c == '\\' && !AfterBackSlash) {
AfterBackSlash = true;
continue;
}
if (AfterBackSlash) {
AfterBackSlash = false;
value << c;
continue;
}
if (c != '\'') {
value << c;
continue;
}
state = EState::InsideBrackets;
OnKey(key, value);
break;
}
}
}
}
virtual void OnFinish() = 0;
virtual void OnKey(const TString& key, const TString& value) = 0;
};
bool ValidateOperArgs(const TOperDesc& d, const TVector<ui32>& argTypeIds, const TTypes& types) {
ui32 size = d.Kind == EOperKind::Binary ? 2 : 1;
if (argTypeIds.size() != size) {
return false;
}
for (size_t i = 0; i < argTypeIds.size(); ++i) {
ui32 expectedArgType;
if (d.Kind == EOperKind::RightUnary || (d.Kind == EOperKind::Binary && i == 0)) {
expectedArgType = d.LeftType;
}
else {
expectedArgType = d.RightType;
}
if (!IsCompatibleTo(argTypeIds[i], expectedArgType, types)) {
return false;
}
}
return true;
}
class TOperatorsParser : public TParser {
public:
TOperatorsParser(TOperators& operators, const THashMap<TString, ui32>& typeByName, const TTypes& types,
const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs)
: Operators(operators)
, TypeByName(typeByName)
, Types(types)
, ProcByName(procByName)
, Procs(procs)
{}
void OnKey(const TString& key, const TString& value) override {
if (key == "oid") {
LastOperator.OperId = FromString<ui32>(value);
} else if (key == "oprname") {
LastOperator.Name = value;
} else if (key == "oprkind") {
if (value == "r") {
LastOperator.Kind = EOperKind::RightUnary;
} else if (value == "l") {
LastOperator.Kind = EOperKind::LeftUnary;
}
} else if (key == "oprleft") {
if (value != "0") {
auto typeIdPtr = TypeByName.FindPtr(value);
Y_ENSURE(typeIdPtr);
LastOperator.LeftType = *typeIdPtr;
}
} else if (key == "oprright") {
if (value != "0") {
auto typeIdPtr = TypeByName.FindPtr(value);
Y_ENSURE(typeIdPtr);
LastOperator.RightType = *typeIdPtr;
}
} else if (key == "oprresult") {
auto typeIdPtr = TypeByName.FindPtr(value);
Y_ENSURE(typeIdPtr);
LastOperator.ResultType = *typeIdPtr;
} else if (key == "oprcode") {
LastCode = value;
}
}
void OnFinish() override {
if (IsSupported) {
auto pos = LastCode.find('(');
auto code = LastCode.substr(0, pos);
auto procIdPtr = ProcByName.FindPtr(code);
// skip operator if proc isn't builtin, e.g. path_contain_pt
if (!procIdPtr) {
IsSupported = false;
} else {
for (auto procId : *procIdPtr) {
auto procPtr = Procs.FindPtr(procId);
Y_ENSURE(procPtr);
if (ValidateOperArgs(LastOperator, procPtr->ArgTypes, Types)) {
Y_ENSURE(!LastOperator.ProcId);
LastOperator.ProcId = procId;
}
}
// can be missing for example jsonb - _text
if (LastOperator.ProcId) {
Y_ENSURE(!LastOperator.Name.empty());
Operators[LastOperator.OperId] = LastOperator;
}
}
}
LastOperator = TOperDesc();
LastCode = "";
IsSupported = true;
}
private:
TOperators& Operators;
const THashMap<TString, ui32>& TypeByName;
const TTypes& Types;
const THashMap<TString, TVector<ui32>>& ProcByName;
const TProcs& Procs;
TOperDesc LastOperator;
bool IsSupported = true;
TString LastCode;
};
class TProcsParser : public TParser {
public:
TProcsParser(TProcs& procs, const THashMap<TString, ui32>& typeByName)
: Procs(procs)
, TypeByName(typeByName)
{}
void OnKey(const TString& key, const TString& value) override {
if (key == "oid") {
LastProc.ProcId = FromString<ui32>(value);
} else if (key == "provariadic") {
IsSupported = false;
} else if (key == "prokind") {
if (value == "f") {
LastProc.Kind = EProcKind::Function;
} else if (value == "a") {
LastProc.Kind = EProcKind::Aggregate;
} else if (value == "w") {
LastProc.Kind = EProcKind::Window;
} else {
IsSupported = false;
}
} else if (key == "prorettype") {
auto idPtr = TypeByName.FindPtr(value);
Y_ENSURE(idPtr);
LastProc.ResultType = *idPtr;
} else if (key == "proname") {
LastProc.Name = value;
} else if (key == "prosrc") {
LastProc.Src = value;
} else if (key == "prolang") {
IsSupported = false;
} else if (key == "proargtypes") {
TVector<TString> strArgs;
Split(value, " ", strArgs);
LastProc.ArgTypes.reserve(strArgs.size());
for (const auto& s : strArgs) {
auto idPtr = TypeByName.FindPtr(s);
Y_ENSURE(idPtr);
LastProc.ArgTypes.push_back(*idPtr);
}
} else if (key == "proisstrict") {
LastProc.IsStrict = (value == "t");
} else if (key == "proretset") {
LastProc.ReturnSet = (value == "t");
}
}
void OnFinish() override {
if (IsSupported) {
Y_ENSURE(!LastProc.Name.empty());
Procs[LastProc.ProcId] = LastProc;
}
IsSupported = true;
LastProc = TProcDesc();
}
private:
TProcs& Procs;
const THashMap<TString, ui32>& TypeByName;
TProcDesc LastProc;
bool IsSupported = true;
};
struct TLazyTypeInfo {
TString ElementType;
TString InFunc;
TString OutFunc;
TString SendFunc;
TString ReceiveFunc;
TString ModInFunc;
TString ModOutFunc;
};
class TTypesParser : public TParser {
public:
TTypesParser(TTypes& types, THashMap<ui32, TLazyTypeInfo>& lazyInfos)
: Types(types)
, LazyInfos(lazyInfos)
{}
void OnKey(const TString& key, const TString& value) override {
if (key == "oid") {
LastType.TypeId = FromString<ui32>(value);
} else if (key == "array_type_oid") {
LastType.ArrayTypeId = FromString<ui32>(value);
} else if (key == "typname") {
LastType.Name = value;
} else if (key == "typcategory") {
Y_ENSURE(value.size() == 1);
LastType.Category = value[0];
} else if (key == "typlen") {
if (value == "NAMEDATALEN") {
LastType.TypeLen = 64;
} else if (value == "SIZEOF_POINTER") {
LastType.TypeLen = 8;
} else {
LastType.TypeLen = FromString<i32>(value);
}
} else if (key == "typalign") {
if (value == "ALIGNOF_POINTER") {
LastType.TypeAlign = 'i'; // doesn't matter for pointers
} else {
Y_ENSURE(value.size() == 1);
LastType.TypeAlign = value[0];
}
} else if (key == "typdelim") {
Y_ENSURE(value.size() == 1);
LastType.TypeDelim = value[0];
} else if (key == "typelem") {
LastLazyTypeInfo.ElementType = value; // resolve later
} else if (key == "typinput") {
LastLazyTypeInfo.InFunc = value; // resolve later
} else if (key == "typoutput") {
LastLazyTypeInfo.OutFunc = value; // resolve later
} else if (key == "typsend") {
LastLazyTypeInfo.SendFunc = value; // resolve later
} else if (key == "typreceive") {
LastLazyTypeInfo.ReceiveFunc = value; // resolve later
} else if (key == "typmodin") {
LastLazyTypeInfo.ModInFunc = value; // resolve later
} else if (key == "typmodout") {
LastLazyTypeInfo.ModOutFunc = value; // resolve later
} else if (key == "typbyval") {
if (value == "f") {
LastType.PassByValue = false;
} else if (value == "t" || value == "FLOAT8PASSBYVAL") {
LastType.PassByValue = true;
} else {
ythrow yexception() << "Unknown typbyval value: " << value;
}
}
}
void OnFinish() override {
Y_ENSURE(!LastType.Name.empty());
if (LastType.TypeLen < 0 || LastType.TypeLen > 8) {
Y_ENSURE(!LastType.PassByValue);
}
Types[LastType.TypeId] = LastType;
if (LastType.ArrayTypeId) {
auto arrayType = LastType;
arrayType.Name = "_" + arrayType.Name;
arrayType.ElementTypeId = arrayType.TypeId;
arrayType.TypeId = LastType.ArrayTypeId;
arrayType.PassByValue = false;
Types[LastType.ArrayTypeId] = arrayType;
}
LazyInfos[LastType.TypeId] = LastLazyTypeInfo;
LastType = TTypeDesc();
LastLazyTypeInfo = TLazyTypeInfo();
}
private:
TTypes& Types;
THashMap<ui32, TLazyTypeInfo>& LazyInfos;
TTypeDesc LastType;
TLazyTypeInfo LastLazyTypeInfo;
};
class TCastsParser : public TParser {
public:
TCastsParser(TCasts& casts, const THashMap<TString, ui32>& typeByName, const TTypes& types,
const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs)
: Casts(casts)
, TypeByName(typeByName)
, Types(types)
, ProcByName(procByName)
, Procs(procs)
{}
void OnKey(const TString& key, const TString& value) override {
if (key == "castsource") {
auto typePtr = TypeByName.FindPtr(value);
Y_ENSURE(typePtr);
LastCast.SourceId = *typePtr;
} else if (key == "casttarget") {
auto typePtr = TypeByName.FindPtr(value);
Y_ENSURE(typePtr);
LastCast.TargetId = *typePtr;
} else if (key == "castfunc") {
if (value != "0") {
if (value.Contains(',')) {
// e.g. castfunc => 'bit(int8,int4)'
IsSupported = false;
} else if (value.Contains('(')) {
auto pos1 = value.find('(');
auto pos2 = value.find(')');
Y_ENSURE(pos1 != TString::npos);
Y_ENSURE(pos2 != TString::npos);
auto funcName = value.substr(0, pos1);
auto inputType = value.substr(pos1 + 1, pos2 - pos1 - 1);
auto inputTypeIdPtr = TypeByName.FindPtr(inputType);
Y_ENSURE(inputTypeIdPtr);
auto procIdPtr = ProcByName.FindPtr(funcName);
Y_ENSURE(procIdPtr);
bool found = false;
for (const auto& procId : *procIdPtr) {
auto procPtr = Procs.FindPtr(procId);
Y_ENSURE(procPtr);
if (procPtr->ArgTypes.size() != 1) {
continue;
}
if (IsCompatibleTo(*inputTypeIdPtr, procPtr->ArgTypes[0], Types)) {
LastCast.FunctionId = procPtr->ProcId;
found = true;
break;
}
}
if (!found) {
// e.g. convert circle to 12-vertex polygon, used sql proc
IsSupported = false;
}
} else {
auto procIdPtr = ProcByName.FindPtr(value);
Y_ENSURE(procIdPtr);
Y_ENSURE(procIdPtr->size() == 1);
LastCast.FunctionId = procIdPtr->at(0);
}
}
} else if (key == "castmethod") {
if (value == "f") {
LastCast.Method = ECastMethod::Function;
} else if (value == "i") {
LastCast.Method = ECastMethod::InOut;
} else if (value == "b") {
LastCast.Method = ECastMethod::Binary;
} else {
ythrow yexception() << "Unknown castmethod value: " << value;
}
}
}
void OnFinish() override {
if (IsSupported) {
auto id = 1 + Casts.size();
Casts[id] = LastCast;
}
LastCast = TCastDesc();
IsSupported = true;
}
private:
TCasts& Casts;
const THashMap<TString, ui32>& TypeByName;
const TTypes& Types;
const THashMap<TString, TVector<ui32>>& ProcByName;
const TProcs& Procs;
TCastDesc LastCast;
bool IsSupported = true;
};
class TAggregationsParser : public TParser {
public:
TAggregationsParser(TAggregations& aggregations, const THashMap<TString, ui32>& typeByName,
const TTypes& types, const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs)
: Aggregations(aggregations)
, TypeByName(typeByName)
, Types(types)
, ProcByName(procByName)
, Procs(procs)
{}
void OnKey(const TString& key, const TString& value) override {
Y_UNUSED(ProcByName);
if (key == "aggtranstype") {
auto typeId = TypeByName.FindPtr(value);
Y_ENSURE(typeId);
LastAggregation.TransTypeId = *typeId;
} else if (key == "aggfnoid") {
LastOid = value;
} else if (key == "aggtransfn") {
LastTransFunc = value;
} else if (key == "aggfinalfn") {
LastFinalFunc = value;
} else if (key == "aggcombinefn") {
LastCombineFunc = value;
} else if (key == "aggserialfn") {
LastSerializeFunc = value;
} else if (key == "aggdeserialfn") {
LastDeserializeFunc = value;
} else if (key == "aggkind") {
if (value == "n") {
LastAggregation.Kind = EAggKind::Normal;
} else if (value == "o") {
LastAggregation.Kind = EAggKind::OrderedSet;
} else if (value == "h") {
LastAggregation.Kind = EAggKind::Hypothetical;
} else {
ythrow yexception() << "Unknown aggkind value: " << value;
}
} else if (key == "agginitval") {
LastAggregation.InitValue = value;
}
}
void OnFinish() override {
if (IsSupported) {
if (FillSupported()) {
auto id = Aggregations.size() + 1;
Aggregations[id] = LastAggregation;
}
}
LastAggregation = TAggregateDesc();
IsSupported = true;
LastOid = "";
LastTransFunc = "";
LastFinalFunc = "";
LastCombineFunc = "";
LastSerializeFunc = "";
LastDeserializeFunc = "";
}
bool FillSupported() {
Y_ENSURE(LastAggregation.TransTypeId);
Y_ENSURE(LastOid);
Y_ENSURE(LastTransFunc);
auto transFuncIdsPtr = ProcByName.FindPtr(LastTransFunc);
if (!transFuncIdsPtr) {
// e.g. variadic ordered_set_transition_multi
return false;
}
for (const auto id : *transFuncIdsPtr) {
auto procPtr = Procs.FindPtr(id);
Y_ENSURE(procPtr);
if (procPtr->ArgTypes.size() >= 1 &&
IsCompatibleTo(LastAggregation.TransTypeId, procPtr->ArgTypes[0], Types)) {
Y_ENSURE(!LastAggregation.TransFuncId);
LastAggregation.TransFuncId = id;
}
}
Y_ENSURE(LastAggregation.TransFuncId);
// oid format: name(arg1,arg2...)
auto pos1 = LastOid.find('(');
if (pos1 != TString::npos) {
LastAggregation.Name = LastOid.substr(0, pos1);
auto pos = pos1 + 1;
for (;;) {
auto nextPos = Min(LastOid.find(',', pos), LastOid.find(')', pos));
Y_ENSURE(nextPos != TString::npos);
if (pos == nextPos) {
break;
}
auto arg = LastOid.substr(pos, nextPos - pos);
auto argTypeId = TypeByName.FindPtr(arg);
Y_ENSURE(argTypeId);
LastAggregation.ArgTypes.push_back(*argTypeId);
pos = nextPos;
if (LastOid[pos] == ')') {
break;
} else {
++pos;
}
}
} else {
// no signature in oid, use transfunc
LastAggregation.Name = LastOid;
auto procPtr = Procs.FindPtr(LastAggregation.TransFuncId);
Y_ENSURE(procPtr);
LastAggregation.ArgTypes = procPtr->ArgTypes;
Y_ENSURE(LastAggregation.ArgTypes.size() >= 1);
Y_ENSURE(IsCompatibleTo(LastAggregation.TransTypeId, LastAggregation.ArgTypes[0], Types));
LastAggregation.ArgTypes.erase(LastAggregation.ArgTypes.begin());
}
Y_ENSURE(!LastAggregation.Name.empty());
if (!ResolveFunc(LastFinalFunc, LastAggregation.FinalFuncId, 1)) {
return false;
}
if (!ResolveFunc(LastCombineFunc, LastAggregation.CombineFuncId, 2)) {
return false;
}
if (!ResolveFunc(LastSerializeFunc, LastAggregation.SerializeFuncId, 1)) {
return false;
}
if (!ResolveFunc(LastDeserializeFunc, LastAggregation.DeserializeFuncId, 0)) {
return false;
}
return true;
}
bool ResolveFunc(const TString& name, ui32& funcId, ui32 stateArgsCount) {
if (name) {
auto funcIdsPtr = ProcByName.FindPtr(name);
if (!funcIdsPtr) {
return false;
}
if (!stateArgsCount) {
Y_ENSURE(funcIdsPtr->size() == 1);
}
for (const auto id : *funcIdsPtr) {
auto procPtr = Procs.FindPtr(id);
Y_ENSURE(procPtr);
bool found = true;
if (stateArgsCount > 0 && procPtr->ArgTypes.size() == stateArgsCount) {
for (ui32 i = 0; i < stateArgsCount; ++i) {
if (!IsCompatibleTo(LastAggregation.TransTypeId, procPtr->ArgTypes[i], Types)) {
found = false;
break;
}
}
}
if (found) {
Y_ENSURE(!funcId);
funcId = id;
}
}
Y_ENSURE(funcId);
}
return true;
}
private:
TAggregations& Aggregations;
const THashMap<TString, ui32>& TypeByName;
const TTypes& Types;
const THashMap<TString, TVector<ui32>>& ProcByName;
const TProcs& Procs;
TAggregateDesc LastAggregation;
bool IsSupported = true;
TString LastOid;
TString LastTransFunc;
TString LastFinalFunc;
TString LastCombineFunc;
TString LastSerializeFunc;
TString LastDeserializeFunc;
};
class TOpClassesParser : public TParser {
public:
TOpClassesParser(TOpClasses& opClasses, const THashMap<TString, ui32>& typeByName)
: OpClasses(opClasses)
, TypeByName(typeByName)
{}
void OnKey(const TString& key, const TString& value) override {
if (key == "opcmethod") {
if (value == "btree") {
LastOpClass.Method = EOpClassMethod::Btree;
} else if (value == "hash") {
LastOpClass.Method = EOpClassMethod::Hash;
} else {
IsSupported = false;
}
} else if (key == "opcintype") {
auto idPtr = TypeByName.FindPtr(value);
Y_ENSURE(idPtr);
LastOpClass.TypeId = *idPtr;
} else if (key == "opcname") {
LastOpClass.Name = value;
} else if (key == "opcfamily") {
LastOpClass.Family = value;
}
}
void OnFinish() override {
if (IsSupported) {
Y_ENSURE(!LastOpClass.Name.empty());
OpClasses[std::make_pair(LastOpClass.Method, LastOpClass.TypeId)] = LastOpClass;
}
IsSupported = true;
LastOpClass = TOpClassDesc();
}
private:
TOpClasses& OpClasses;
const THashMap<TString, ui32>& TypeByName;
TOpClassDesc LastOpClass;
bool IsSupported = true;
};
class TAmOpsParser : public TParser {
public:
TAmOpsParser(TAmOps& amOps, const THashMap<TString, ui32>& typeByName, const TTypes& types,
const THashMap<TString, TVector<ui32>>& operatorsByName, const TOperators& operators)
: AmOps(amOps)
, TypeByName(typeByName)
, Types(types)
, OperatorsByName(operatorsByName)
, Operators(operators)
{}
void OnKey(const TString& key, const TString& value) override {
if (key == "amopfamily") {
LastAmOp.Family = value;
} else if (key == "amoplefttype") {
auto leftTypePtr = TypeByName.FindPtr(value);
Y_ENSURE(leftTypePtr);
LastAmOp.LeftType = *leftTypePtr;
} else if (key == "amoprighttype") {
auto rightTypePtr = TypeByName.FindPtr(value);
Y_ENSURE(rightTypePtr);
LastAmOp.RightType = *rightTypePtr;
} else if (key == "amopstrategy") {
LastAmOp.Strategy = FromString<ui32>(value);
} else if (key == "amopopr") {
auto pos = value.find('(');
Y_ENSURE(pos != TString::npos);
LastOp = value.substr(0, pos);
}
}
void OnFinish() override {
auto operIdPtr = OperatorsByName.FindPtr(LastOp);
Y_ENSURE(operIdPtr);
for (const auto& id : *operIdPtr) {
const auto& d = Operators.FindPtr(id);
Y_ENSURE(d);
if (d->Kind == EOperKind::Binary &&
IsCompatibleTo(LastAmOp.LeftType, d->LeftType, Types) &&
IsCompatibleTo(LastAmOp.RightType, d->RightType, Types)) {
Y_ENSURE(!LastAmOp.OperId);
LastAmOp.OperId = d->OperId;
}
}
Y_ENSURE(LastAmOp.OperId);
AmOps[std::make_tuple(LastAmOp.Family, LastAmOp.Strategy, LastAmOp.LeftType, LastAmOp.RightType)] = LastAmOp;
LastAmOp = TAmOpDesc();
LastOp = "";
}
private:
TAmOps& AmOps;
const THashMap<TString, ui32>& TypeByName;
const TTypes& Types;
const THashMap<TString, TVector<ui32>>& OperatorsByName;
const TOperators& Operators;
TAmOpDesc LastAmOp;
TString LastOp;
};
class TAmProcsParser : public TParser {
public:
TAmProcsParser(TAmProcs& amProcs, const THashMap<TString, ui32>& typeByName,
const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs)
: AmProcs(amProcs)
, TypeByName(typeByName)
, ProcByName(procByName)
, Procs(procs)
{}
void OnKey(const TString& key, const TString& value) override {
if (key == "amprocfamily") {
LastAmProc.Family = value;
} else if (key == "amproclefttype") {
auto leftTypePtr = TypeByName.FindPtr(value);
Y_ENSURE(leftTypePtr);
LastAmProc.LeftType = *leftTypePtr;
} else if (key == "amprocrighttype") {
auto rightTypePtr = TypeByName.FindPtr(value);
Y_ENSURE(rightTypePtr);
LastAmProc.RightType = *rightTypePtr;
} else if (key == "amprocnum") {
LastAmProc.ProcNum = FromString<ui32>(value);
} else if (key == "amproc") {
LastName = value;
}
}
void OnFinish() override {
if (LastName.find('(') == TString::npos) {
auto procIdPtr = ProcByName.FindPtr(LastName);
Y_ENSURE(procIdPtr);
for (const auto& id : *procIdPtr) {
const auto& d = Procs.FindPtr(id);
Y_ENSURE(d);
Y_ENSURE(!LastAmProc.ProcId);
LastAmProc.ProcId = d->ProcId;
}
Y_ENSURE(LastAmProc.ProcId);
AmProcs[std::make_tuple(LastAmProc.Family, LastAmProc.ProcNum, LastAmProc.LeftType, LastAmProc.RightType)] = LastAmProc;
}
LastAmProc = TAmProcDesc();
LastName = "";
}
private:
TAmProcs& AmProcs;
const THashMap<TString, ui32>& TypeByName;
const THashMap<TString, TVector<ui32>>& ProcByName;
const TProcs& Procs;
TAmProcDesc LastAmProc;
TString LastName;
};
TOperators ParseOperators(const TString& dat, const THashMap<TString, ui32>& typeByName,
const TTypes& types, const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs) {
TOperators ret;
TOperatorsParser parser(ret, typeByName, types, procByName, procs);
parser.Do(dat);
return ret;
}
TAggregations ParseAggregations(const TString& dat, const THashMap<TString, ui32>& typeByName,
const TTypes& types, const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs) {
TAggregations ret;
TAggregationsParser parser(ret, typeByName, types, procByName, procs);
parser.Do(dat);
return ret;
}
TProcs ParseProcs(const TString& dat, const THashMap<TString, ui32>& typeByName) {
TProcs ret;
TProcsParser parser(ret, typeByName);
parser.Do(dat);
return ret;
}
TTypes ParseTypes(const TString& dat, THashMap<ui32, TLazyTypeInfo>& lazyInfos) {
TTypes ret;
TTypesParser parser(ret, lazyInfos);
parser.Do(dat);
return ret;
}
TCasts ParseCasts(const TString& dat, const THashMap<TString, ui32>& typeByName, const TTypes& types,
const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs) {
TCasts ret;
TCastsParser parser(ret, typeByName, types, procByName, procs);
parser.Do(dat);
return ret;
}
TOpClasses ParseOpClasses(const TString& dat, const THashMap<TString, ui32>& typeByName) {
TOpClasses ret;
TOpClassesParser parser(ret, typeByName);
parser.Do(dat);
return ret;
}
TAmOps ParseAmOps(const TString& dat, const THashMap<TString, ui32>& typeByName, const TTypes& types,
const THashMap<TString, TVector<ui32>>& operatorsByName, const TOperators& operators) {
TAmOps ret;
TAmOpsParser parser(ret, typeByName, types, operatorsByName, operators);
parser.Do(dat);
return ret;
}
TAmProcs ParseAmProcs(const TString& dat, const THashMap<TString, ui32>& typeByName,
const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs) {
TAmProcs ret;
TAmProcsParser parser(ret, typeByName, procByName, procs);
parser.Do(dat);
return ret;
}
struct TCatalog {
TCatalog() {
TString typeData;
Y_ENSURE(NResource::FindExact("pg_type.dat", &typeData));
TString opData;
Y_ENSURE(NResource::FindExact("pg_operator.dat", &opData));
TString procData;
Y_ENSURE(NResource::FindExact("pg_proc.dat", &procData));
TString castData;
Y_ENSURE(NResource::FindExact("pg_cast.dat", &castData));
TString aggData;
Y_ENSURE(NResource::FindExact("pg_aggregate.dat", &aggData));
TString opClassData;
Y_ENSURE(NResource::FindExact("pg_opclass.dat", &opClassData));
TString amProcData;
Y_ENSURE(NResource::FindExact("pg_amproc.dat", &amProcData));
TString amOpData;
Y_ENSURE(NResource::FindExact("pg_amop.dat", &amOpData));
THashMap<ui32, TLazyTypeInfo> lazyTypeInfos;
Types = ParseTypes(typeData, lazyTypeInfos);
for (const auto& [k, v] : Types) {
if (k == v.TypeId) {
Y_ENSURE(TypeByName.insert(std::make_pair(v.Name, k)).second);
}
if (k == v.ArrayTypeId) {
Y_ENSURE(TypeByName.insert(std::make_pair("_" + v.Name, k)).second);
}
}
for (const auto& [k, v]: lazyTypeInfos) {
if (!v.ElementType) {
continue;
}
auto elemTypePtr = TypeByName.FindPtr(v.ElementType);
Y_ENSURE(elemTypePtr);
auto typePtr = Types.FindPtr(k);
Y_ENSURE(typePtr);
typePtr->ElementTypeId = *elemTypePtr;
}
Procs = ParseProcs(procData, TypeByName);
for (const auto& [k, v]: Procs) {
ProcByName[v.Name].push_back(k);
}
const ui32 cstringId = 2275;
const ui32 byteaId = 17;
const ui32 internalId = 2281;
for (const auto&[k, v] : lazyTypeInfos) {
auto typePtr = Types.FindPtr(k);
Y_ENSURE(typePtr);
auto inFuncIdPtr = ProcByName.FindPtr(v.InFunc);
Y_ENSURE(inFuncIdPtr);
Y_ENSURE(inFuncIdPtr->size() == 1);
auto inFuncPtr = Procs.FindPtr(inFuncIdPtr->at(0));
Y_ENSURE(inFuncPtr);
Y_ENSURE(inFuncPtr->ArgTypes.size() >= 1); // may have mods
Y_ENSURE(inFuncPtr->ArgTypes[0] == cstringId);
typePtr->InFuncId = inFuncIdPtr->at(0);
auto outFuncIdPtr = ProcByName.FindPtr(v.OutFunc);
Y_ENSURE(outFuncIdPtr);
Y_ENSURE(outFuncIdPtr->size() == 1);
auto outFuncPtr = Procs.FindPtr(outFuncIdPtr->at(0));
Y_ENSURE(outFuncPtr);
Y_ENSURE(outFuncPtr->ArgTypes.size() == 1);
Y_ENSURE(outFuncPtr->ResultType == cstringId);
typePtr->OutFuncId = outFuncIdPtr->at(0);
if (v.ReceiveFunc != "-") {
auto receiveFuncIdPtr = ProcByName.FindPtr(v.ReceiveFunc);
Y_ENSURE(receiveFuncIdPtr);
Y_ENSURE(receiveFuncIdPtr->size() == 1);
auto receiveFuncPtr = Procs.FindPtr(receiveFuncIdPtr->at(0));
Y_ENSURE(receiveFuncPtr);
Y_ENSURE(receiveFuncPtr->ArgTypes.size() >= 1);
Y_ENSURE(receiveFuncPtr->ArgTypes[0] == internalId); // mutable StringInfo
typePtr->ReceiveFuncId = receiveFuncIdPtr->at(0);
}
if (v.SendFunc != "-") {
auto sendFuncIdPtr = ProcByName.FindPtr(v.SendFunc);
Y_ENSURE(sendFuncIdPtr);
Y_ENSURE(sendFuncIdPtr->size() == 1);
auto sendFuncPtr = Procs.FindPtr(sendFuncIdPtr->at(0));
Y_ENSURE(sendFuncPtr);
Y_ENSURE(sendFuncPtr->ArgTypes.size() == 1);
Y_ENSURE(sendFuncPtr->ResultType == byteaId);
typePtr->SendFuncId = sendFuncIdPtr->at(0);
}
if (v.ModInFunc) {
auto modInFuncIdPtr = ProcByName.FindPtr(v.ModInFunc);
Y_ENSURE(modInFuncIdPtr);
Y_ENSURE(modInFuncIdPtr->size() == 1);
auto modInFuncPtr = Procs.FindPtr(modInFuncIdPtr->at(0));
Y_ENSURE(modInFuncPtr);
Y_ENSURE(modInFuncPtr->ArgTypes.size() == 1);
typePtr->TypeModInFuncId = modInFuncIdPtr->at(0);
}
if (v.ModOutFunc) {
auto modOutFuncIdPtr = ProcByName.FindPtr(v.ModOutFunc);
Y_ENSURE(modOutFuncIdPtr);
Y_ENSURE(modOutFuncIdPtr->size() == 1);
auto modOutFuncPtr = Procs.FindPtr(modOutFuncIdPtr->at(0));
Y_ENSURE(modOutFuncPtr);
Y_ENSURE(modOutFuncPtr->ArgTypes.size() == 1);
typePtr->TypeModOutFuncId = modOutFuncIdPtr->at(0);
}
}
Casts = ParseCasts(castData, TypeByName, Types, ProcByName, Procs);
for (const auto&[k, v] : Casts) {
Y_ENSURE(CastsByDir.insert(std::make_pair(std::make_pair(v.SourceId, v.TargetId), k)).second);
}
Operators = ParseOperators(opData, TypeByName, Types, ProcByName, Procs);
for (const auto&[k, v] : Operators) {
OperatorsByName[v.Name].push_back(k);
}
Aggregations = ParseAggregations(aggData, TypeByName, Types, ProcByName, Procs);
for (const auto&[k, v] : Aggregations) {
AggregationsByName[v.Name].push_back(k);
}
OpClasses = ParseOpClasses(opClassData, TypeByName);
AmOps = ParseAmOps(amOpData, TypeByName, Types, OperatorsByName, Operators);
AmProcs = ParseAmProcs(amProcData, TypeByName, ProcByName, Procs);
for (auto&[k, v] : Types) {
if (v.TypeId != v.ArrayTypeId) {
auto btreeOpClassPtr = OpClasses.FindPtr(std::make_pair(EOpClassMethod::Btree, v.TypeId));
if (btreeOpClassPtr) {
auto lessAmOpPtr = AmOps.FindPtr(std::make_tuple(btreeOpClassPtr->Family, ui32(EBtreeAmStrategy::Less), v.TypeId, v.TypeId));
Y_ENSURE(lessAmOpPtr);
auto equalAmOpPtr = AmOps.FindPtr(std::make_tuple(btreeOpClassPtr->Family, ui32(EBtreeAmStrategy::Equal), v.TypeId, v.TypeId));
Y_ENSURE(equalAmOpPtr);
auto lessOperPtr = Operators.FindPtr(lessAmOpPtr->OperId);
Y_ENSURE(lessOperPtr);
auto equalOperPtr = Operators.FindPtr(equalAmOpPtr->OperId);
Y_ENSURE(equalOperPtr);
v.LessProcId = lessOperPtr->ProcId;
v.EqualProcId = equalOperPtr->ProcId;
auto compareAmProcPtr = AmProcs.FindPtr(std::make_tuple(btreeOpClassPtr->Family, ui32(EBtreeAmProcNum::Compare), v.TypeId, v.TypeId));
Y_ENSURE(compareAmProcPtr);
v.CompareProcId = compareAmProcPtr->ProcId;
}
auto hashOpClassPtr = OpClasses.FindPtr(std::make_pair(EOpClassMethod::Hash, v.TypeId));
if (hashOpClassPtr) {
auto hashAmProcPtr = AmProcs.FindPtr(std::make_tuple(hashOpClassPtr->Family, ui32(EHashAmProcNum::Hash), v.TypeId, v.TypeId));
Y_ENSURE(hashAmProcPtr);
v.HashProcId = hashAmProcPtr->ProcId;
}
}
}
}
static const TCatalog& Instance() {
return *Singleton<TCatalog>();
}
TOperators Operators;
TProcs Procs;
TTypes Types;
TCasts Casts;
TAggregations Aggregations;
TOpClasses OpClasses;
TAmOps AmOps;
TAmProcs AmProcs;
THashMap<TString, TVector<ui32>> ProcByName;
THashMap<TString, ui32> TypeByName;
THashMap<std::pair<ui32, ui32>, ui32> CastsByDir;
THashMap<TString, TVector<ui32>> OperatorsByName;
THashMap<TString, TVector<ui32>> AggregationsByName;
};
bool ValidateArgs(const TVector<ui32>& descArgTypeIds, const TVector<ui32>& argTypeIds) {
if (argTypeIds.size() != descArgTypeIds.size()) {
return false;
}
for (size_t i = 0; i < argTypeIds.size(); ++i) {
if (!IsCompatibleTo(argTypeIds[i], descArgTypeIds[i])) {
return false;
}
}
return true;
}
bool ValidateProcArgs(const TProcDesc& d, const TVector<ui32>& argTypeIds) {
return ValidateArgs(d.ArgTypes, argTypeIds);
}
const TProcDesc& LookupProc(ui32 procId, const TVector<ui32>& argTypeIds) {
const auto& catalog = TCatalog::Instance();
auto procPtr = catalog.Procs.FindPtr(procId);
if (!procPtr) {
throw yexception() << "No such proc: " << procId;
}
if (!ValidateProcArgs(*procPtr, argTypeIds)) {
throw yexception() << "Unable to find an overload for proc with oid " << procId << " with given argument types: " <<
ArgTypesList(argTypeIds);
}
return *procPtr;
}
const TProcDesc& LookupProc(const TString& name, const TVector<ui32>& argTypeIds) {
const auto& catalog = TCatalog::Instance();
auto procIdPtr = catalog.ProcByName.FindPtr(name);
if (!procIdPtr) {
throw yexception() << "No such proc: " << name;
}
for (const auto& id : *procIdPtr) {
const auto& d = catalog.Procs.FindPtr(id);
Y_ENSURE(d);
if (!ValidateProcArgs(*d, argTypeIds)) {
continue;
}
return *d;
}
throw yexception() << "Unable to find an overload for proc " << name << " with given argument types: "
<< ArgTypesList(argTypeIds);
}
const TProcDesc& LookupProc(ui32 procId) {
const auto& catalog = TCatalog::Instance();
auto procPtr = catalog.Procs.FindPtr(procId);
if (!procPtr) {
throw yexception() << "No such proc: " << procId;
}
return *procPtr;
}
bool HasReturnSetProc(const TStringBuf& name) {
const auto& catalog = TCatalog::Instance();
auto procIdPtr = catalog.ProcByName.FindPtr(name);
if (!procIdPtr) {
return false;
}
for (const auto& id : *procIdPtr) {
const auto& d = catalog.Procs.FindPtr(id);
Y_ENSURE(d);
if (d->ReturnSet) {
return true;
}
}
return false;
}
bool HasType(const TStringBuf& name) {
const auto& catalog = TCatalog::Instance();
return catalog.TypeByName.contains(name);
}
const TTypeDesc& LookupType(const TString& name) {
const auto& catalog = TCatalog::Instance();
auto typeIdPtr = catalog.TypeByName.FindPtr(name);
if (!typeIdPtr) {
throw yexception() << "No such type: " << name;
}
auto typePtr = catalog.Types.FindPtr(*typeIdPtr);
Y_ENSURE(typePtr);
return *typePtr;
}
const TTypeDesc& LookupType(ui32 typeId) {
const auto& catalog = TCatalog::Instance();
auto typePtr = catalog.Types.FindPtr(typeId);
if (!typePtr) {
throw yexception() << "No such type: " << typeId;
}
return *typePtr;
}
bool HasCast(ui32 sourceId, ui32 targetId) {
const auto& catalog = TCatalog::Instance();
return catalog.CastsByDir.contains(std::make_pair(sourceId, targetId));
}
const TCastDesc& LookupCast(ui32 sourceId, ui32 targetId) {
const auto& catalog = TCatalog::Instance();
auto castByDirPtr = catalog.CastsByDir.FindPtr(std::make_pair(sourceId, targetId));
if (!castByDirPtr) {
throw yexception() << "No such cast";
}
auto castPtr = catalog.Casts.FindPtr(*castByDirPtr);
Y_ENSURE(castPtr);
return *castPtr;
}
const TOperDesc& LookupOper(const TString& name, const TVector<ui32>& argTypeIds) {
const auto& catalog = TCatalog::Instance();
auto operIdPtr = catalog.OperatorsByName.FindPtr(name);
if (!operIdPtr) {
throw yexception() << "No such operator: " << name;
}
for (const auto& id : *operIdPtr) {
const auto& d = catalog.Operators.FindPtr(id);
Y_ENSURE(d);
if (!ValidateOperArgs(*d, argTypeIds, catalog.Types)) {
continue;
}
return *d;
}
throw yexception() << "Unable to find an overload for operator " << name << " with given argument types: "
<< ArgTypesList(argTypeIds);
}
const TOperDesc& LookupOper(ui32 operId, const TVector<ui32>& argTypeIds) {
const auto& catalog = TCatalog::Instance();
auto operPtr = catalog.Operators.FindPtr(operId);
if (!operPtr) {
throw yexception() << "No such oper: " << operId;
}
if (!ValidateOperArgs(*operPtr, argTypeIds, catalog.Types)) {
throw yexception() << "Unable to find an overload for operator with oid " << operId << " with given argument types: "
<< ArgTypesList(argTypeIds);
}
return *operPtr;
}
const TOperDesc& LookupOper(ui32 operId) {
const auto& catalog = TCatalog::Instance();
auto operPtr = catalog.Operators.FindPtr(operId);
if (!operPtr) {
throw yexception() << "No such oper: " << operId;
}
return *operPtr;
}
bool HasAggregation(const TStringBuf& name) {
const auto& catalog = TCatalog::Instance();
return catalog.AggregationsByName.contains(name);
}
bool ValidateAggregateArgs(const TAggregateDesc& d, const TVector<ui32>& argTypeIds) {
return ValidateArgs(d.ArgTypes, argTypeIds);
}
const TAggregateDesc& LookupAggregation(const TStringBuf& name, const TVector<ui32>& argTypeIds) {
const auto& catalog = TCatalog::Instance();
auto aggIdPtr = catalog.AggregationsByName.FindPtr(name);
if (!aggIdPtr) {
throw yexception() << "No such aggregate: " << name;
}
for (const auto& id : *aggIdPtr) {
const auto& d = catalog.Aggregations.FindPtr(id);
Y_ENSURE(d);
if (!ValidateAggregateArgs(*d, argTypeIds)) {
continue;
}
return *d;
}
throw yexception() << "Unable to find an overload for aggregate " << name << " with given argument types: "
<< ArgTypesList(argTypeIds);
}
bool HasOpClass(EOpClassMethod method, ui32 typeId) {
const auto& catalog = TCatalog::Instance();
return catalog.OpClasses.contains(std::make_pair(method, typeId));
}
const TOpClassDesc& LookupOpClass(EOpClassMethod method, ui32 typeId) {
const auto& catalog = TCatalog::Instance();
auto opClassPtr = catalog.OpClasses.FindPtr(std::make_pair(method, typeId));
if (!opClassPtr) {
throw yexception() << "No such opclass";
}
return *opClassPtr;
}
const TAmOpDesc& LookupAmOp(const TString& family, ui32 strategy, ui32 leftType, ui32 rightType) {
const auto& catalog = TCatalog::Instance();
auto amOpPtr = catalog.AmOps.FindPtr(std::make_tuple(family, strategy, leftType, rightType));
if (!amOpPtr) {
throw yexception() << "No such amop";
}
return *amOpPtr;
}
const TAmProcDesc& LookupAmProc(const TString& family, ui32 num, ui32 leftType, ui32 rightType) {
const auto& catalog = TCatalog::Instance();
auto amProcPtr = catalog.AmProcs.FindPtr(std::make_tuple(family, num, leftType, rightType));
if (!amProcPtr) {
throw yexception() << "No such amproc";
}
return *amProcPtr;
}
bool IsCompatibleTo(ui32 actualType, ui32 expectedType) {
const auto& catalog = TCatalog::Instance();
return IsCompatibleTo(actualType, expectedType, catalog.Types);
}
}
|