-
Notifications
You must be signed in to change notification settings - Fork 461
/
Copy pathSubstraitToVeloxPlanValidator.cc
1385 lines (1251 loc) · 49.5 KB
/
SubstraitToVeloxPlanValidator.cc
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SubstraitToVeloxPlanValidator.h"
#include <google/protobuf/wrappers.pb.h>
#include <re2/re2.h>
#include <string>
#include "TypeUtils.h"
#include "udf/UdfLoader.h"
#include "utils/Common.h"
#include "velox/exec/Aggregate.h"
#include "velox/expression/Expr.h"
#include "velox/expression/SignatureBinder.h"
namespace gluten {
namespace {
const char* extractFileName(const char* file) {
return strrchr(file, '/') ? strrchr(file, '/') + 1 : file;
}
#define LOG_VALIDATION_MSG_FROM_EXCEPTION(err) \
logValidateMsg(fmt::format( \
"Validation failed due to exception caught at file:{} line:{} function:{}, thrown from file:{} line:{} function:{}, reason:{}", \
extractFileName(__FILE__), \
__LINE__, \
__FUNCTION__, \
extractFileName(err.file()), \
err.line(), \
err.function(), \
err.message()))
#define LOG_VALIDATION_MSG(reason) \
logValidateMsg(fmt::format( \
"Validation failed at file:{}, line:{}, function:{}, reason:{}", \
extractFileName(__FILE__), \
__LINE__, \
__FUNCTION__, \
reason))
const std::unordered_set<std::string> kRegexFunctions = {
"regexp_extract",
"regexp_extract_all",
"regexp_replace",
"rlike"};
const std::unordered_set<std::string> kBlackList = {
"split_part",
"factorial",
"from_json",
"json_array_length",
"trunc",
"sequence",
"approx_percentile",
"get_array_struct_fields",
"map_from_arrays"};
} // namespace
bool SubstraitToVeloxPlanValidator::parseVeloxType(
const ::substrait::extensions::AdvancedExtension& extension,
TypePtr& out) {
::substrait::Type substraitType;
// The input type is wrapped in enhancement.
if (!extension.has_enhancement()) {
LOG_VALIDATION_MSG("Input type is not wrapped in enhancement.");
return false;
}
const auto& enhancement = extension.enhancement();
if (!enhancement.UnpackTo(&substraitType)) {
LOG_VALIDATION_MSG("Enhancement can't be unpacked to inputType.");
return false;
}
out = SubstraitParser::parseType(substraitType);
return true;
}
bool SubstraitToVeloxPlanValidator::flattenSingleLevel(const TypePtr& type, std::vector<TypePtr>& out) {
if (type->kind() != TypeKind::ROW) {
LOG_VALIDATION_MSG("Type is not a RowType.");
return false;
}
auto rowType = std::dynamic_pointer_cast<const RowType>(type);
if (!rowType) {
LOG_VALIDATION_MSG("Failed to cast to RowType.");
return false;
}
for (const auto& field : rowType->children()) {
out.emplace_back(field);
}
return true;
}
bool SubstraitToVeloxPlanValidator::flattenDualLevel(const TypePtr& type, std::vector<std::vector<TypePtr>>& out) {
if (type->kind() != TypeKind::ROW) {
LOG_VALIDATION_MSG("Type is not a RowType.");
return false;
}
auto rowType = std::dynamic_pointer_cast<const RowType>(type);
if (!rowType) {
LOG_VALIDATION_MSG("Failed to cast to RowType.");
return false;
}
for (const auto& field : rowType->children()) {
std::vector<TypePtr> inner;
if (!flattenSingleLevel(field, inner)) {
return false;
}
out.emplace_back(inner);
}
return true;
}
bool SubstraitToVeloxPlanValidator::validateRound(
const ::substrait::Expression::ScalarFunction& scalarFunction,
const RowTypePtr& inputType) {
const auto& arguments = scalarFunction.arguments();
if (arguments.size() < 2) {
return false;
}
if (!arguments[1].value().has_literal()) {
LOG_VALIDATION_MSG("Round scale is expected.");
return false;
}
// Velox has different result with Spark on negative scale.
auto typeCase = arguments[1].value().literal().literal_type_case();
switch (typeCase) {
case ::substrait::Expression_Literal::LiteralTypeCase::kI32:
return (arguments[1].value().literal().i32() >= 0);
case ::substrait::Expression_Literal::LiteralTypeCase::kI64:
return (arguments[1].value().literal().i64() >= 0);
default:
LOG_VALIDATION_MSG("Round scale validation is not supported for type case " + std::to_string(typeCase));
return false;
}
}
bool SubstraitToVeloxPlanValidator::validateExtractExpr(const std::vector<core::TypedExprPtr>& params) {
if (params.size() != 2) {
LOG_VALIDATION_MSG("Value expected in variant in ExtractExpr.");
return false;
}
auto functionArg = std::dynamic_pointer_cast<const core::ConstantTypedExpr>(params[0]);
if (functionArg) {
// Get the function argument.
const auto& variant = functionArg->value();
if (!variant.hasValue()) {
LOG_VALIDATION_MSG("Value expected in variant in ExtractExpr.");
return false;
}
return true;
}
LOG_VALIDATION_MSG("Constant is expected to be the first parameter in extract.");
return false;
}
bool SubstraitToVeloxPlanValidator::validateRegexExpr(
const std::string& name,
const ::substrait::Expression::ScalarFunction& scalarFunction) {
if (scalarFunction.arguments().size() < 2) {
LOG_VALIDATION_MSG("Wrong number of arguments for " + name);
}
const auto& patternArg = scalarFunction.arguments()[1].value();
if (!patternArg.has_literal() || !patternArg.literal().has_string()) {
LOG_VALIDATION_MSG("Pattern is not string literal for " + name);
return false;
}
const auto& pattern = patternArg.literal().string();
std::string error;
if (!validatePattern(pattern, error)) {
LOG_VALIDATION_MSG(name + " due to " + error);
return false;
}
return true;
}
bool SubstraitToVeloxPlanValidator::validateScalarFunction(
const ::substrait::Expression::ScalarFunction& scalarFunction,
const RowTypePtr& inputType) {
std::vector<core::TypedExprPtr> params;
params.reserve(scalarFunction.arguments().size());
for (const auto& argument : scalarFunction.arguments()) {
if (argument.has_value() && !validateExpression(argument.value(), inputType)) {
return false;
}
params.emplace_back(exprConverter_->toVeloxExpr(argument.value(), inputType));
}
const auto& function =
SubstraitParser::findFunctionSpec(planConverter_.getFunctionMap(), scalarFunction.function_reference());
const auto& name = SubstraitParser::getNameBeforeDelimiter(function);
std::vector<std::string> types = SubstraitParser::getSubFunctionTypes(function);
if (name == "round") {
return validateRound(scalarFunction, inputType);
} else if (name == "extract") {
return validateExtractExpr(params);
} else if (name == "concat") {
for (const auto& type : types) {
if (type.find("struct") != std::string::npos || type.find("map") != std::string::npos ||
type.find("list") != std::string::npos) {
LOG_VALIDATION_MSG(type + " is not supported in concat.");
return false;
}
}
}
// Validate regex functions.
if (kRegexFunctions.find(name) != kRegexFunctions.end()) {
return validateRegexExpr(name, scalarFunction);
}
if (kBlackList.find(name) != kBlackList.end()) {
LOG_VALIDATION_MSG("Function is not supported: " + name);
return false;
}
return true;
}
bool SubstraitToVeloxPlanValidator::validateLiteral(
const ::substrait::Expression_Literal& literal,
const RowTypePtr& inputType) {
if (literal.has_list()) {
for (auto child : literal.list().values()) {
if (!validateLiteral(child, inputType)) {
// the error msg has been set, so do not need to set it again.
return false;
}
}
} else if (literal.has_map()) {
for (auto child : literal.map().key_values()) {
if (!validateLiteral(child.key(), inputType) || !validateLiteral(child.value(), inputType)) {
// the error msg has been set, so do not need to set it again.
return false;
}
}
}
return true;
}
bool SubstraitToVeloxPlanValidator::validateCast(
const ::substrait::Expression::Cast& castExpr,
const RowTypePtr& inputType) {
if (!validateExpression(castExpr.input(), inputType)) {
return false;
}
const auto& toType = SubstraitParser::parseType(castExpr.type());
core::TypedExprPtr input = exprConverter_->toVeloxExpr(castExpr.input(), inputType);
// Only support cast from date to timestamp
if (toType->kind() == TypeKind::TIMESTAMP && !input->type()->isDate()) {
LOG_VALIDATION_MSG(
"Casting from " + input->type()->toString() + " to " + toType->toString() + " is not supported.");
return false;
}
if (toType->isIntervalYearMonth()) {
LOG_VALIDATION_MSG("Casting to " + toType->toString() + " is not supported.");
return false;
}
// Casting from some types is not supported. See CastExpr::applyPeeled.
if (input->type()->isDate()) {
// Only support cast date to varchar & timestamp
if (toType->kind() != TypeKind::VARCHAR && toType->kind() != TypeKind::TIMESTAMP) {
LOG_VALIDATION_MSG("Casting from DATE to " + toType->toString() + " is not supported.");
return false;
}
} else if (input->type()->isIntervalYearMonth()) {
LOG_VALIDATION_MSG("Casting from INTERVAL_YEAR_MONTH is not supported.");
return false;
}
switch (input->type()->kind()) {
case TypeKind::ARRAY:
case TypeKind::MAP:
case TypeKind::ROW:
case TypeKind::VARBINARY:
LOG_VALIDATION_MSG("Invalid input type in casting: ARRAY/MAP/ROW/VARBINARY.");
return false;
case TypeKind::TIMESTAMP:
// Only support casting timestamp to date or varchar.
if (!toType->isDate() && toType->kind() != TypeKind::VARCHAR) {
LOG_VALIDATION_MSG(
"Casting from TIMESTAMP to " + toType->toString() + " is not supported or has incorrect result.");
return false;
}
default: {
}
}
return true;
}
bool SubstraitToVeloxPlanValidator::validateIfThen(
const ::substrait::Expression_IfThen& ifThen,
const RowTypePtr& inputType) {
for (const auto& subIfThen : ifThen.ifs()) {
if (!validateExpression(subIfThen.if_(), inputType) || !validateExpression(subIfThen.then(), inputType)) {
return false;
}
}
if (ifThen.has_else_() && !validateExpression(ifThen.else_(), inputType)) {
return false;
}
return true;
}
bool SubstraitToVeloxPlanValidator::validateSingularOrList(
const ::substrait::Expression::SingularOrList& singularOrList,
const RowTypePtr& inputType) {
for (const auto& option : singularOrList.options()) {
if (!option.has_literal()) {
LOG_VALIDATION_MSG("Option is expected as Literal.");
return false;
}
if (!validateLiteral(option.literal(), inputType)) {
return false;
}
}
return validateExpression(singularOrList.value(), inputType);
}
bool SubstraitToVeloxPlanValidator::validateExpression(
const ::substrait::Expression& expression,
const RowTypePtr& inputType) {
auto typeCase = expression.rex_type_case();
switch (typeCase) {
case ::substrait::Expression::RexTypeCase::kScalarFunction:
return validateScalarFunction(expression.scalar_function(), inputType);
case ::substrait::Expression::RexTypeCase::kLiteral:
return validateLiteral(expression.literal(), inputType);
case ::substrait::Expression::RexTypeCase::kCast:
return validateCast(expression.cast(), inputType);
case ::substrait::Expression::RexTypeCase::kIfThen:
return validateIfThen(expression.if_then(), inputType);
case ::substrait::Expression::RexTypeCase::kSingularOrList:
return validateSingularOrList(expression.singular_or_list(), inputType);
default:
return true;
}
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::WriteRel& writeRel) {
if (writeRel.has_input() && !validate(writeRel.input())) {
LOG_VALIDATION_MSG("Validation failed for input type validation in WriteRel.");
return false;
}
// Validate input data type.
TypePtr inputRowType;
std::vector<TypePtr> types;
if (writeRel.has_named_table()) {
const auto& extension = writeRel.named_table().advanced_extension();
if (!parseVeloxType(extension, inputRowType) || !flattenSingleLevel(inputRowType, types)) {
LOG_VALIDATION_MSG("Validation failed for input type validation in WriteRel.");
return false;
}
}
// Validate partition key type.
if (writeRel.has_table_schema()) {
const auto& tableSchema = writeRel.table_schema();
std::vector<ColumnType> columnTypes;
SubstraitParser::parseColumnTypes(tableSchema, columnTypes);
for (auto i = 0; i < types.size(); i++) {
if (columnTypes[i] == ColumnType::kPartitionKey) {
switch (types[i]->kind()) {
case TypeKind::BOOLEAN:
case TypeKind::TINYINT:
case TypeKind::SMALLINT:
case TypeKind::INTEGER:
case TypeKind::BIGINT:
case TypeKind::VARCHAR:
case TypeKind::VARBINARY:
break;
default:
LOG_VALIDATION_MSG(
"Validation failed for input type validation in WriteRel, not support partition column type: " +
mapTypeKindToName(types[i]->kind()));
return false;
}
}
}
}
return true;
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::FetchRel& fetchRel) {
// Get and validate the input types from extension.
if (fetchRel.has_advanced_extension()) {
const auto& extension = fetchRel.advanced_extension();
TypePtr inputRowType;
std::vector<TypePtr> types;
if (!parseVeloxType(extension, inputRowType) || !flattenSingleLevel(inputRowType, types)) {
LOG_VALIDATION_MSG("Unsupported input types in FetchRel.");
return false;
}
int32_t inputPlanNodeId = 0;
std::vector<std::string> names;
names.reserve(types.size());
for (auto colIdx = 0; colIdx < types.size(); colIdx++) {
names.emplace_back(SubstraitParser::makeNodeName(inputPlanNodeId, colIdx));
}
}
if (fetchRel.offset() < 0 || fetchRel.count() < 0) {
LOG_VALIDATION_MSG("Offset and count should be valid in FetchRel.");
return false;
}
return true;
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::TopNRel& topNRel) {
RowTypePtr rowType = nullptr;
// Get and validate the input types from extension.
if (topNRel.has_advanced_extension()) {
const auto& extension = topNRel.advanced_extension();
TypePtr inputRowType;
std::vector<TypePtr> types;
if (!parseVeloxType(extension, inputRowType) || !flattenSingleLevel(inputRowType, types)) {
LOG_VALIDATION_MSG("Unsupported input types in TopNRel.");
return false;
}
int32_t inputPlanNodeId = 0;
std::vector<std::string> names;
names.reserve(types.size());
for (auto colIdx = 0; colIdx < types.size(); colIdx++) {
names.emplace_back(SubstraitParser::makeNodeName(inputPlanNodeId, colIdx));
}
rowType = std::make_shared<RowType>(std::move(names), std::move(types));
}
if (topNRel.n() < 0) {
LOG_VALIDATION_MSG("N should be valid in TopNRel.");
return false;
}
auto [sortingKeys, sortingOrders] = planConverter_.processSortField(topNRel.sorts(), rowType);
folly::F14FastSet<std::string> sortingKeyNames;
for (const auto& sortingKey : sortingKeys) {
auto result = sortingKeyNames.insert(sortingKey->name());
if (!result.second) {
LOG_VALIDATION_MSG("Duplicate sort keys were found in TopNRel.");
return false;
}
}
return true;
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::GenerateRel& generateRel) {
if (generateRel.has_input() && !validate(generateRel.input())) {
LOG_VALIDATION_MSG("Input validation fails in GenerateRel.");
return false;
}
// Get and validate the input types from extension.
if (!generateRel.has_advanced_extension()) {
LOG_VALIDATION_MSG("Input types are expected in GenerateRel.");
return false;
}
const auto& extension = generateRel.advanced_extension();
TypePtr inputRowType;
std::vector<TypePtr> types;
if (!parseVeloxType(extension, inputRowType) || !flattenSingleLevel(inputRowType, types)) {
LOG_VALIDATION_MSG("Validation failed for input types in GenerateRel.");
return false;
}
int32_t inputPlanNodeId = 0;
// Create the fake input names to be used in row type.
std::vector<std::string> names;
names.reserve(types.size());
for (uint32_t colIdx = 0; colIdx < types.size(); colIdx++) {
names.emplace_back(SubstraitParser::makeNodeName(inputPlanNodeId, colIdx));
}
auto rowType = std::make_shared<RowType>(std::move(names), std::move(types));
if (generateRel.has_generator() && !validateExpression(generateRel.generator(), rowType)) {
LOG_VALIDATION_MSG("Input validation fails in GenerateRel.");
return false;
}
return true;
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::ExpandRel& expandRel) {
if (expandRel.has_input() && !validate(expandRel.input())) {
LOG_VALIDATION_MSG("Input validation fails in ExpandRel.");
return false;
}
RowTypePtr rowType = nullptr;
// Get and validate the input types from extension.
if (expandRel.has_advanced_extension()) {
const auto& extension = expandRel.advanced_extension();
TypePtr inputRowType;
std::vector<TypePtr> types;
if (!parseVeloxType(extension, inputRowType) || !flattenSingleLevel(inputRowType, types)) {
LOG_VALIDATION_MSG("Unsupported input types in ExpandRel.");
return false;
}
int32_t inputPlanNodeId = 0;
std::vector<std::string> names;
names.reserve(types.size());
for (auto colIdx = 0; colIdx < types.size(); colIdx++) {
names.emplace_back(SubstraitParser::makeNodeName(inputPlanNodeId, colIdx));
}
rowType = std::make_shared<RowType>(std::move(names), std::move(types));
}
int32_t projectSize = 0;
// Validate fields.
for (const auto& fields : expandRel.fields()) {
std::vector<core::TypedExprPtr> expressions;
if (fields.has_switching_field()) {
auto projectExprs = fields.switching_field().duplicates();
expressions.reserve(projectExprs.size());
if (projectSize == 0) {
projectSize = projectExprs.size();
} else if (projectSize != projectExprs.size()) {
LOG_VALIDATION_MSG("SwitchingField expressions size should be constant in ExpandRel.");
return false;
}
for (const auto& projectExpr : projectExprs) {
const auto& typeCase = projectExpr.rex_type_case();
switch (typeCase) {
case ::substrait::Expression::RexTypeCase::kSelection:
case ::substrait::Expression::RexTypeCase::kLiteral:
break;
default:
LOG_VALIDATION_MSG("Only field or literal is supported in project of ExpandRel.");
return false;
}
if (rowType) {
expressions.emplace_back(exprConverter_->toVeloxExpr(projectExpr, rowType));
}
}
if (rowType) {
// Try to compile the expressions. If there is any unregistered
// function or mismatched type, exception will be thrown.
exec::ExprSet exprSet(std::move(expressions), execCtx_);
}
} else {
LOG_VALIDATION_MSG("Only SwitchingField is supported in ExpandRel.");
return false;
}
}
return true;
}
bool validateBoundType(::substrait::Expression_WindowFunction_Bound boundType) {
switch (boundType.kind_case()) {
case ::substrait::Expression_WindowFunction_Bound::kUnboundedFollowing:
case ::substrait::Expression_WindowFunction_Bound::kUnboundedPreceding:
case ::substrait::Expression_WindowFunction_Bound::kCurrentRow:
case ::substrait::Expression_WindowFunction_Bound::kFollowing:
case ::substrait::Expression_WindowFunction_Bound::kPreceding:
break;
default:
return false;
}
return true;
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::WindowRel& windowRel) {
if (windowRel.has_input() && !validate(windowRel.input())) {
LOG_VALIDATION_MSG("WindowRel input fails to validate.");
return false;
}
// Get and validate the input types from extension.
if (!windowRel.has_advanced_extension()) {
LOG_VALIDATION_MSG("Input types are expected in WindowRel.");
return false;
}
const auto& extension = windowRel.advanced_extension();
TypePtr inputRowType;
std::vector<TypePtr> types;
if (!parseVeloxType(extension, inputRowType) || !flattenSingleLevel(inputRowType, types)) {
LOG_VALIDATION_MSG("Validation failed for input types in WindowRel.");
return false;
}
if (types.empty()) {
// See: https://github.com/apache/incubator-gluten/issues/7600.
LOG_VALIDATION_MSG("Validation failed for empty input schema in WindowRel.");
return false;
}
int32_t inputPlanNodeId = 0;
std::vector<std::string> names;
names.reserve(types.size());
for (auto colIdx = 0; colIdx < types.size(); colIdx++) {
names.emplace_back(SubstraitParser::makeNodeName(inputPlanNodeId, colIdx));
}
auto rowType = std::make_shared<RowType>(std::move(names), std::move(types));
// Validate WindowFunction
std::vector<std::string> funcSpecs;
funcSpecs.reserve(windowRel.measures().size());
for (const auto& smea : windowRel.measures()) {
const auto& windowFunction = smea.measure();
funcSpecs.emplace_back(planConverter_.findFuncSpec(windowFunction.function_reference()));
SubstraitParser::parseType(windowFunction.output_type());
for (const auto& arg : windowFunction.arguments()) {
auto typeCase = arg.value().rex_type_case();
switch (typeCase) {
case ::substrait::Expression::RexTypeCase::kSelection:
case ::substrait::Expression::RexTypeCase::kLiteral:
break;
default:
LOG_VALIDATION_MSG("Only field or constant is supported in window functions.");
return false;
}
}
// Validate BoundType and Frame Type
switch (windowFunction.window_type()) {
case ::substrait::WindowType::ROWS:
case ::substrait::WindowType::RANGE:
break;
default:
LOG_VALIDATION_MSG(
"the window type only support ROWS and RANGE, and the input type is " +
std::to_string(windowFunction.window_type()));
return false;
}
bool boundTypeSupported =
validateBoundType(windowFunction.upper_bound()) && validateBoundType(windowFunction.lower_bound());
if (!boundTypeSupported) {
LOG_VALIDATION_MSG(
"Found unsupported Bound Type: upper " + std::to_string(windowFunction.upper_bound().kind_case()) +
", lower " + std::to_string(windowFunction.lower_bound().kind_case()));
return false;
}
}
// Validate groupby expression
const auto& groupByExprs = windowRel.partition_expressions();
std::vector<core::TypedExprPtr> expressions;
expressions.reserve(groupByExprs.size());
for (const auto& expr : groupByExprs) {
auto expression = exprConverter_->toVeloxExpr(expr, rowType);
auto exprField = dynamic_cast<const core::FieldAccessTypedExpr*>(expression.get());
if (exprField == nullptr) {
LOG_VALIDATION_MSG("Only field is supported for partition key in Window Operator!");
return false;
} else {
expressions.emplace_back(expression);
}
}
// Try to compile the expressions. If there is any unregistred funciton or
// mismatched type, exception will be thrown.
exec::ExprSet exprSet(std::move(expressions), execCtx_);
// Validate Sort expression
const auto& sorts = windowRel.sorts();
for (const auto& sort : sorts) {
switch (sort.direction()) {
case ::substrait::SortField_SortDirection_SORT_DIRECTION_ASC_NULLS_FIRST:
case ::substrait::SortField_SortDirection_SORT_DIRECTION_ASC_NULLS_LAST:
case ::substrait::SortField_SortDirection_SORT_DIRECTION_DESC_NULLS_FIRST:
case ::substrait::SortField_SortDirection_SORT_DIRECTION_DESC_NULLS_LAST:
break;
default:
LOG_VALIDATION_MSG("in windowRel, unsupported Sort direction " + std::to_string(sort.direction()));
return false;
}
if (sort.has_expr()) {
auto expression = exprConverter_->toVeloxExpr(sort.expr(), rowType);
auto exprField = dynamic_cast<const core::FieldAccessTypedExpr*>(expression.get());
if (!exprField) {
LOG_VALIDATION_MSG("in windowRel, the sorting key in Sort Operator only support field.");
return false;
}
exec::ExprSet exprSet1({std::move(expression)}, execCtx_);
}
}
return true;
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::WindowGroupLimitRel& windowGroupLimitRel) {
if (windowGroupLimitRel.has_input() && !validate(windowGroupLimitRel.input())) {
LOG_VALIDATION_MSG("WindowGroupLimitRel input fails to validate.");
return false;
}
// Get and validate the input types from extension.
if (!windowGroupLimitRel.has_advanced_extension()) {
LOG_VALIDATION_MSG("Input types are expected in WindowGroupLimitRel.");
return false;
}
const auto& extension = windowGroupLimitRel.advanced_extension();
TypePtr inputRowType;
std::vector<TypePtr> types;
if (!parseVeloxType(extension, inputRowType) || !flattenSingleLevel(inputRowType, types)) {
LOG_VALIDATION_MSG("Validation failed for input types in WindowGroupLimitRel.");
return false;
}
int32_t inputPlanNodeId = 0;
std::vector<std::string> names;
names.reserve(types.size());
for (auto colIdx = 0; colIdx < types.size(); colIdx++) {
names.emplace_back(SubstraitParser::makeNodeName(inputPlanNodeId, colIdx));
}
auto rowType = std::make_shared<RowType>(std::move(names), std::move(types));
// Validate groupby expression
const auto& groupByExprs = windowGroupLimitRel.partition_expressions();
std::vector<core::TypedExprPtr> expressions;
expressions.reserve(groupByExprs.size());
for (const auto& expr : groupByExprs) {
auto expression = exprConverter_->toVeloxExpr(expr, rowType);
auto exprField = dynamic_cast<const core::FieldAccessTypedExpr*>(expression.get());
if (exprField == nullptr) {
LOG_VALIDATION_MSG("Only field is supported for partition key in Window Group Limit Operator!");
return false;
} else {
expressions.emplace_back(expression);
}
}
// Try to compile the expressions. If there is any unregistered function or
// mismatched type, exception will be thrown.
exec::ExprSet exprSet(std::move(expressions), execCtx_);
// Validate Sort expression
const auto& sorts = windowGroupLimitRel.sorts();
for (const auto& sort : sorts) {
switch (sort.direction()) {
case ::substrait::SortField_SortDirection_SORT_DIRECTION_ASC_NULLS_FIRST:
case ::substrait::SortField_SortDirection_SORT_DIRECTION_ASC_NULLS_LAST:
case ::substrait::SortField_SortDirection_SORT_DIRECTION_DESC_NULLS_FIRST:
case ::substrait::SortField_SortDirection_SORT_DIRECTION_DESC_NULLS_LAST:
break;
default:
LOG_VALIDATION_MSG("in windowGroupLimitRel, unsupported Sort direction " + std::to_string(sort.direction()));
return false;
}
if (sort.has_expr()) {
auto expression = exprConverter_->toVeloxExpr(sort.expr(), rowType);
auto exprField = dynamic_cast<const core::FieldAccessTypedExpr*>(expression.get());
if (!exprField) {
LOG_VALIDATION_MSG("in windowGroupLimitRel, the sorting key in Sort Operator only support field.");
return false;
}
exec::ExprSet exprSet1({std::move(expression)}, execCtx_);
}
}
return true;
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::SetRel& setRel) {
switch (setRel.op()) {
case ::substrait::SetRel_SetOp::SetRel_SetOp_SET_OP_UNION_ALL: {
for (int32_t i = 0; i < setRel.inputs_size(); ++i) {
const auto& input = setRel.inputs(i);
if (!validate(input)) {
LOG_VALIDATION_MSG("ProjectRel input");
return false;
}
}
if (!setRel.has_advanced_extension()) {
LOG_VALIDATION_MSG("Input types are expected in SetRel.");
return false;
}
const auto& extension = setRel.advanced_extension();
TypePtr inputRowType;
std::vector<std::vector<TypePtr>> childrenTypes;
if (!parseVeloxType(extension, inputRowType) || !flattenDualLevel(inputRowType, childrenTypes)) {
LOG_VALIDATION_MSG("Validation failed for input types in SetRel.");
return false;
}
std::vector<RowTypePtr> childrenRowTypes;
for (auto i = 0; i < childrenTypes.size(); ++i) {
auto& types = childrenTypes.at(i);
std::vector<std::string> names;
names.reserve(types.size());
for (auto colIdx = 0; colIdx < types.size(); colIdx++) {
names.emplace_back(SubstraitParser::makeNodeName(i, colIdx));
}
childrenRowTypes.push_back(std::make_shared<RowType>(std::move(names), std::move(types)));
}
for (auto i = 1; i < childrenRowTypes.size(); ++i) {
if (!(childrenRowTypes[i]->equivalent(*childrenRowTypes[0]))) {
LOG_VALIDATION_MSG(
"All sources of the Set operation must have the same output type: " + childrenRowTypes[i]->toString() +
" vs. " + childrenRowTypes[0]->toString());
return false;
}
}
return true;
}
default:
LOG_VALIDATION_MSG("Unsupported SetRel op: " + std::to_string(setRel.op()));
return false;
}
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::SortRel& sortRel) {
if (sortRel.has_input() && !validate(sortRel.input())) {
return false;
}
// Get and validate the input types from extension.
if (!sortRel.has_advanced_extension()) {
LOG_VALIDATION_MSG("Input types are expected in SortRel.");
return false;
}
const auto& extension = sortRel.advanced_extension();
TypePtr inputRowType;
std::vector<TypePtr> types;
if (!parseVeloxType(extension, inputRowType) || !flattenSingleLevel(inputRowType, types)) {
LOG_VALIDATION_MSG("Validation failed for input types in SortRel.");
return false;
}
int32_t inputPlanNodeId = 0;
std::vector<std::string> names;
names.reserve(types.size());
for (auto colIdx = 0; colIdx < types.size(); colIdx++) {
names.emplace_back(SubstraitParser::makeNodeName(inputPlanNodeId, colIdx));
}
auto rowType = std::make_shared<RowType>(std::move(names), std::move(types));
const auto& sorts = sortRel.sorts();
for (const auto& sort : sorts) {
switch (sort.direction()) {
case ::substrait::SortField_SortDirection_SORT_DIRECTION_ASC_NULLS_FIRST:
case ::substrait::SortField_SortDirection_SORT_DIRECTION_ASC_NULLS_LAST:
case ::substrait::SortField_SortDirection_SORT_DIRECTION_DESC_NULLS_FIRST:
case ::substrait::SortField_SortDirection_SORT_DIRECTION_DESC_NULLS_LAST:
break;
default:
LOG_VALIDATION_MSG("unsupported Sort direction " + std::to_string(sort.direction()));
return false;
}
if (sort.has_expr()) {
auto expression = exprConverter_->toVeloxExpr(sort.expr(), rowType);
auto exprField = dynamic_cast<const core::FieldAccessTypedExpr*>(expression.get());
if (!exprField) {
LOG_VALIDATION_MSG("in SortRel, the sorting key in Sort Operator only support field.");
return false;
}
exec::ExprSet exprSet({std::move(expression)}, execCtx_);
}
}
return true;
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::ProjectRel& projectRel) {
if (projectRel.has_input() && !validate(projectRel.input())) {
LOG_VALIDATION_MSG("ProjectRel input");
return false;
}
// Get and validate the input types from extension.
if (!projectRel.has_advanced_extension()) {
LOG_VALIDATION_MSG("Input types are expected in ProjectRel.");
return false;
}
const auto& extension = projectRel.advanced_extension();
TypePtr inputRowType;
std::vector<TypePtr> types;
if (!parseVeloxType(extension, inputRowType) || !flattenSingleLevel(inputRowType, types)) {
LOG_VALIDATION_MSG("Validation failed for input types in ProjectRel.");
return false;
}
int32_t inputPlanNodeId = 0;
// Create the fake input names to be used in row type.
std::vector<std::string> names;
names.reserve(types.size());
for (uint32_t colIdx = 0; colIdx < types.size(); colIdx++) {
names.emplace_back(SubstraitParser::makeNodeName(inputPlanNodeId, colIdx));
}
auto rowType = std::make_shared<RowType>(std::move(names), std::move(types));
// Validate the project expressions.
const auto& projectExprs = projectRel.expressions();
std::vector<core::TypedExprPtr> expressions;
expressions.reserve(projectExprs.size());
for (const auto& expr : projectExprs) {
if (!validateExpression(expr, rowType)) {
return false;
}
expressions.emplace_back(exprConverter_->toVeloxExpr(expr, rowType));
}
// Try to compile the expressions. If there is any unregistered function or
// mismatched type, exception will be thrown.
exec::ExprSet exprSet(std::move(expressions), execCtx_);
return true;
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::FilterRel& filterRel) {
if (filterRel.has_input() && !validate(filterRel.input())) {
LOG_VALIDATION_MSG("input of FilterRel validation fails");
return false;
}
// Get and validate the input types from extension.
if (!filterRel.has_advanced_extension()) {
LOG_VALIDATION_MSG("Input types are expected in FilterRel.");
return false;
}
const auto& extension = filterRel.advanced_extension();
TypePtr inputRowType;
std::vector<TypePtr> types;
if (!parseVeloxType(extension, inputRowType) || !flattenSingleLevel(inputRowType, types)) {
LOG_VALIDATION_MSG("Validation failed for input types in FilterRel.");
return false;
}
int32_t inputPlanNodeId = 0;
// Create the fake input names to be used in row type.
std::vector<std::string> names;
names.reserve(types.size());
for (uint32_t colIdx = 0; colIdx < types.size(); colIdx++) {
names.emplace_back(SubstraitParser::makeNodeName(inputPlanNodeId, colIdx));
}
auto rowType = std::make_shared<RowType>(std::move(names), std::move(types));
std::vector<core::TypedExprPtr> expressions;
if (!validateExpression(filterRel.condition(), rowType)) {
return false;
}
expressions.emplace_back(exprConverter_->toVeloxExpr(filterRel.condition(), rowType));
// Try to compile the expressions. If there is any unregistered function
// or mismatched type, exception will be thrown.
exec::ExprSet exprSet(std::move(expressions), execCtx_);
return true;
}
bool SubstraitToVeloxPlanValidator::validate(const ::substrait::JoinRel& joinRel) {
if (joinRel.has_left() && !validate(joinRel.left())) {
LOG_VALIDATION_MSG("Validation fails for join left input.");
return false;
}
if (joinRel.has_right() && !validate(joinRel.right())) {
LOG_VALIDATION_MSG("Validation fails for join right input.");
return false;
}
if (joinRel.has_advanced_extension() &&
SubstraitParser::configSetInOptimization(joinRel.advanced_extension(), "isSMJ=")) {
switch (joinRel.type()) {
case ::substrait::JoinRel_JoinType_JOIN_TYPE_INNER:
case ::substrait::JoinRel_JoinType_JOIN_TYPE_OUTER:
case ::substrait::JoinRel_JoinType_JOIN_TYPE_LEFT:
case ::substrait::JoinRel_JoinType_JOIN_TYPE_RIGHT:
case ::substrait::JoinRel_JoinType_JOIN_TYPE_LEFT_SEMI:
case ::substrait::JoinRel_JoinType_JOIN_TYPE_RIGHT_SEMI:
case ::substrait::JoinRel_JoinType_JOIN_TYPE_LEFT_ANTI:
break;
default:
LOG_VALIDATION_MSG("Sort merge join type is not supported: " + std::to_string(joinRel.type()));
return false;
}
}
switch (joinRel.type()) {
case ::substrait::JoinRel_JoinType_JOIN_TYPE_INNER:
case ::substrait::JoinRel_JoinType_JOIN_TYPE_OUTER:
case ::substrait::JoinRel_JoinType_JOIN_TYPE_LEFT:
case ::substrait::JoinRel_JoinType_JOIN_TYPE_RIGHT:
case ::substrait::JoinRel_JoinType_JOIN_TYPE_LEFT_SEMI: