|
15 | 15 | */ |
16 | 16 | #include <gtest/gtest.h> |
17 | 17 |
|
| 18 | +#include "velox/common/base/tests/GTestUtils.h" |
18 | 19 | #include "velox/common/memory/Memory.h" |
19 | 20 | #include "velox/core/Expressions.h" |
20 | 21 | #include "velox/functions/prestosql/types/HyperLogLogType.h" |
@@ -74,11 +75,10 @@ class ConstantTypedExprTest : public ::testing::Test, |
74 | 75 | } |
75 | 76 |
|
76 | 77 | // Helper functions |
77 | | - template <typename T> |
78 | 78 | std::shared_ptr<ConstantTypedExpr> createVariantExpr( |
79 | 79 | const TypePtr& type, |
80 | | - const T& value) { |
81 | | - return std::make_shared<ConstantTypedExpr>(type, variant(value)); |
| 80 | + const Variant& value) { |
| 81 | + return std::make_shared<ConstantTypedExpr>(type, value); |
82 | 82 | } |
83 | 83 |
|
84 | 84 | std::shared_ptr<ConstantTypedExpr> createNullVariantExpr( |
@@ -579,4 +579,34 @@ TEST_F(ConstantTypedExprTest, toStringComplexTypes) { |
579 | 579 | << "toString mismatch for OPAQUE variant vs vector"; |
580 | 580 | } |
581 | 581 |
|
| 582 | +TEST_F(ConstantTypedExprTest, variantTypeCheck) { |
| 583 | + auto testVariantExpr = |
| 584 | + [&](Variant value, const TypePtr type, const TypePtr expectedType) { |
| 585 | + VELOX_ASSERT_THROW( |
| 586 | + createVariantExpr(type, value), |
| 587 | + fmt::format( |
| 588 | + "Expression type {} does not match variant type {}", |
| 589 | + type->toString(), |
| 590 | + expectedType->toString())); |
| 591 | + if (type->isPrimitiveType()) { |
| 592 | + VELOX_ASSERT_THROW( |
| 593 | + createVariantExpr(type, Variant::null(expectedType->kind())), |
| 594 | + fmt::format( |
| 595 | + "Expression type {} does not match variant type {}", |
| 596 | + type->toString(), |
| 597 | + expectedType->toString())); |
| 598 | + } |
| 599 | + }; |
| 600 | + |
| 601 | + testVariantExpr("abc", INTEGER(), VARCHAR()); |
| 602 | + testVariantExpr(variant(123LL), INTEGER(), BIGINT()); |
| 603 | + testVariantExpr(2.0, BIGINT(), DOUBLE()); |
| 604 | + testVariantExpr( |
| 605 | + variant::array({1, 2, 3}), ARRAY(VARCHAR()), ARRAY(INTEGER())); |
| 606 | + testVariantExpr( |
| 607 | + variant::map({{2.0, "xyz"}}), |
| 608 | + MAP(INTEGER(), VARCHAR()), |
| 609 | + MAP(DOUBLE(), VARCHAR())); |
| 610 | +} |
| 611 | + |
582 | 612 | } // namespace facebook::velox::core::test |
0 commit comments