Skip to content

Commit 5f413fc

Browse files
ericyuliufacebook-github-bot
authored andcommitted
test: Add unit tests for expression and fix ExpressionEvaluator due to clean up of mapTypeKindToName (prestodb#26414)
Summary: X-link: facebookincubator/velox#15264 add more unit tests for expression Reviewed By: bigfootjon Differential Revision: D85356832
1 parent cc46adb commit 5f413fc

File tree

1 file changed

+239
-5
lines changed

1 file changed

+239
-5
lines changed

presto-native-execution/presto_cpp/main/types/tests/RowExpressionTest.cpp

Lines changed: 239 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,28 @@ TEST_F(RowExpressionTest, booleanTrue) {
307307
testConstantExpression(str, "BOOLEAN", "true");
308308
}
309309

310+
TEST_F(RowExpressionTest, booleanFalse) {
311+
std::string str = R"(
312+
{
313+
"@type": "constant",
314+
"valueBlock": "CgAAAEJZVEVfQVJSQVkBAAAAAAA=",
315+
"type": "boolean"
316+
}
317+
)";
318+
testConstantExpression(str, "BOOLEAN", "false");
319+
}
320+
321+
TEST_F(RowExpressionTest, booleanNull) {
322+
std::string str = R"(
323+
{
324+
"@type": "constant",
325+
"valueBlock": "AwAAAFJMRQEAAAAKAAAAQllURV9BUlJBWQEAAAABgA==",
326+
"type": "boolean"
327+
}
328+
)";
329+
testConstantExpression(str, "BOOLEAN", "null");
330+
}
331+
310332
TEST_F(RowExpressionTest, varchar1) {
311333
std::string str = R"##(
312334
{
@@ -419,7 +441,8 @@ TEST_F(RowExpressionTest, varbinary5) {
419441
}
420442

421443
TEST_F(RowExpressionTest, char) {
422-
SystemConfig::instance()->setValue(std::string(SystemConfig::kCharNToVarcharImplicitCast), "true");
444+
SystemConfig::instance()->setValue(
445+
std::string(SystemConfig::kCharNToVarcharImplicitCast), "true");
423446
std::string str = R"##(
424447
{
425448
"@type": "constant",
@@ -656,8 +679,8 @@ TEST_F(RowExpressionTest, castToVarchar) {
656679
ASSERT_NE(returnExpr, nullptr);
657680
ASSERT_EQ(returnExpr->name(), "presto.default.substr");
658681

659-
auto returnArg1 = std::dynamic_pointer_cast<const CastTypedExpr>(
660-
returnExpr->inputs()[0]);
682+
auto returnArg1 =
683+
std::dynamic_pointer_cast<const CastTypedExpr>(returnExpr->inputs()[0]);
661684
auto returnArg2 = std::dynamic_pointer_cast<const ConstantTypedExpr>(
662685
returnExpr->inputs()[1]);
663686
auto returnArg3 = std::dynamic_pointer_cast<const ConstantTypedExpr>(
@@ -799,6 +822,218 @@ TEST_F(RowExpressionTest, special) {
799822
}
800823
}
801824

825+
TEST_F(RowExpressionTest, specialOr) {
826+
std::string str = R"##(
827+
{
828+
"@type": "special",
829+
"arguments": [
830+
{
831+
"@type": "call",
832+
"arguments": [
833+
{
834+
"@type": "variable",
835+
"name": "custkey",
836+
"type": "bigint"
837+
},
838+
{
839+
"@type": "constant",
840+
"type": "bigint",
841+
"valueBlock": "CgAAAExPTkdfQVJSQVkBAAAAAAoAAAAAAAAA"
842+
}
843+
],
844+
"displayName": "EQUAL",
845+
"functionHandle": {
846+
"@type": "$static",
847+
"signature": {
848+
"argumentTypes": [
849+
"bigint",
850+
"bigint"
851+
],
852+
"kind": "SCALAR",
853+
"longVariableConstraints": [],
854+
"name": "presto.default.$operator$equal",
855+
"returnType": "boolean",
856+
"typeVariableConstraints": [],
857+
"variableArity": false
858+
},
859+
"builtInFunctionKind": "ENGINE"
860+
},
861+
"returnType": "boolean"
862+
},
863+
{
864+
"@type": "call",
865+
"arguments": [
866+
{
867+
"@type": "variable",
868+
"name": "name",
869+
"type": "varchar(25)"
870+
},
871+
{
872+
"@type": "constant",
873+
"type": "varchar(25)",
874+
"valueBlock": "DgAAAFZBUklBQkxFX1dJRFRIAQAAAAMAAAAAAwAAAGZvbw=="
875+
}
876+
],
877+
"displayName": "EQUAL",
878+
"functionHandle": {
879+
"@type": "$static",
880+
"signature": {
881+
"argumentTypes": [
882+
"varchar(25)",
883+
"varchar(25)"
884+
],
885+
"kind": "SCALAR",
886+
"longVariableConstraints": [],
887+
"name": "presto.default.$operator$equal",
888+
"returnType": "boolean",
889+
"typeVariableConstraints": [],
890+
"variableArity": false
891+
},
892+
"builtInFunctionKind": "ENGINE"
893+
},
894+
"returnType": "boolean"
895+
}
896+
],
897+
"form": "OR",
898+
"returnType": "boolean"
899+
}
900+
)##";
901+
902+
json j = json::parse(str);
903+
std::shared_ptr<protocol::RowExpression> p = j;
904+
905+
auto callexpr =
906+
std::static_pointer_cast<const CallTypedExpr>(converter_->toVeloxExpr(p));
907+
908+
ASSERT_EQ(callexpr->type()->toString(), "BOOLEAN");
909+
ASSERT_EQ(callexpr->name(), "or");
910+
911+
{
912+
auto arg0expr =
913+
std::static_pointer_cast<const CallTypedExpr>(callexpr->inputs()[0]);
914+
915+
ASSERT_EQ(arg0expr->type()->toString(), "BOOLEAN");
916+
ASSERT_EQ(arg0expr->name(), "presto.default.eq");
917+
{
918+
auto cexpr = std::static_pointer_cast<const FieldAccessTypedExpr>(
919+
arg0expr->inputs()[0]);
920+
ASSERT_EQ(cexpr->type()->toString(), "BIGINT");
921+
ASSERT_EQ(cexpr->name(), "custkey");
922+
}
923+
{
924+
auto cexpr = std::static_pointer_cast<const ConstantTypedExpr>(
925+
arg0expr->inputs()[1]);
926+
ASSERT_EQ(cexpr->type()->toString(), "BIGINT");
927+
ASSERT_EQ(cexpr->value().toJson(cexpr->type()), "10");
928+
}
929+
}
930+
931+
{
932+
auto arg1expr =
933+
std::static_pointer_cast<const CallTypedExpr>(callexpr->inputs()[1]);
934+
935+
ASSERT_EQ(arg1expr->type()->toString(), "BOOLEAN");
936+
ASSERT_EQ(arg1expr->name(), "presto.default.eq");
937+
{
938+
auto cexpr = std::static_pointer_cast<const FieldAccessTypedExpr>(
939+
arg1expr->inputs()[0]);
940+
ASSERT_EQ(cexpr->type()->toString(), "VARCHAR");
941+
ASSERT_EQ(cexpr->name(), "name");
942+
}
943+
{
944+
auto cexpr = std::static_pointer_cast<const ConstantTypedExpr>(
945+
arg1expr->inputs()[1]);
946+
ASSERT_EQ(cexpr->type()->toString(), "VARCHAR");
947+
ASSERT_EQ(cexpr->value().toJson(cexpr->type()), "\"foo\"");
948+
}
949+
}
950+
}
951+
952+
TEST_F(RowExpressionTest, specialNot) {
953+
std::string str = R"##(
954+
{
955+
"@type": "call",
956+
"displayName": "NOT",
957+
"functionHandle": {
958+
"@type": "$static",
959+
"signature": {
960+
"name": "presto.default.$operator$not",
961+
"kind": "SCALAR",
962+
"typeVariableConstraints": [],
963+
"longVariableConstraints": [],
964+
"returnType": "boolean",
965+
"argumentTypes": ["boolean"],
966+
"variableArity": false
967+
},
968+
"builtInFunctionKind": "ENGINE"
969+
},
970+
"returnType": "boolean",
971+
"arguments": [
972+
{
973+
"@type": "call",
974+
"arguments": [
975+
{
976+
"@type": "variable",
977+
"name": "custkey",
978+
"type": "bigint"
979+
},
980+
{
981+
"@type": "constant",
982+
"type": "bigint",
983+
"valueBlock": "CgAAAExPTkdfQVJSQVkBAAAAAAoAAAAAAAAA"
984+
}
985+
],
986+
"displayName": "EQUAL",
987+
"functionHandle": {
988+
"@type": "$static",
989+
"signature": {
990+
"argumentTypes": [
991+
"bigint",
992+
"bigint"
993+
],
994+
"kind": "SCALAR",
995+
"longVariableConstraints": [],
996+
"name": "presto.default.$operator$equal",
997+
"returnType": "boolean",
998+
"typeVariableConstraints": [],
999+
"variableArity": false
1000+
},
1001+
"builtInFunctionKind": "ENGINE"
1002+
},
1003+
"returnType": "boolean"
1004+
}
1005+
]
1006+
}
1007+
)##";
1008+
1009+
json j = json::parse(str);
1010+
std::shared_ptr<protocol::RowExpression> p = j;
1011+
1012+
auto callexpr =
1013+
std::static_pointer_cast<const CallTypedExpr>(converter_->toVeloxExpr(p));
1014+
1015+
ASSERT_EQ(callexpr->type()->toString(), "BOOLEAN");
1016+
ASSERT_EQ(callexpr->name(), "presto.default.$operator$not");
1017+
1018+
auto arg0expr =
1019+
std::static_pointer_cast<const CallTypedExpr>(callexpr->inputs()[0]);
1020+
1021+
ASSERT_EQ(arg0expr->type()->toString(), "BOOLEAN");
1022+
ASSERT_EQ(arg0expr->name(), "presto.default.eq");
1023+
{
1024+
auto cexpr = std::static_pointer_cast<const FieldAccessTypedExpr>(
1025+
arg0expr->inputs()[0]);
1026+
ASSERT_EQ(cexpr->type()->toString(), "BIGINT");
1027+
ASSERT_EQ(cexpr->name(), "custkey");
1028+
}
1029+
{
1030+
auto cexpr = std::static_pointer_cast<const ConstantTypedExpr>(
1031+
arg0expr->inputs()[1]);
1032+
ASSERT_EQ(cexpr->type()->toString(), "BIGINT");
1033+
ASSERT_EQ(cexpr->value().toJson(cexpr->type()), "10");
1034+
}
1035+
}
1036+
8021037
TEST_F(RowExpressionTest, bind) {
8031038
std::string str = R"##(
8041039
{
@@ -1024,8 +1259,7 @@ TEST_F(RowExpressionTest, likeWithEscape) {
10241259
ASSERT_NE(callExpr, nullptr);
10251260

10261261
auto callExprToString = callExpr->toString();
1027-
ASSERT_EQ(
1028-
callExpr->toString(), "presto.default.like(\"type\",%BRASS,#)");
1262+
ASSERT_EQ(callExpr->toString(), "presto.default.like(\"type\",%BRASS,#)");
10291263
}
10301264

10311265
TEST_F(RowExpressionTest, dereference) {

0 commit comments

Comments
 (0)