diff --git a/velox/functions/lib/Re2Functions.cpp b/velox/functions/lib/Re2Functions.cpp index 6939a62bce32a..f843b76cd10a5 100644 --- a/velox/functions/lib/Re2Functions.cpp +++ b/velox/functions/lib/Re2Functions.cpp @@ -470,6 +470,18 @@ class OptimizedLikeWithMemcmp final : public VectorFunction { vector_size_t reducedPatternLength_; }; +// This functions is constructed when the input is a constant null. +class AlwaysNull final : public VectorFunction { + void apply( + const SelectivityVector&, + std::vector&, + const TypePtr&, + EvalCtx&, + VectorPtr&) const final { + VELOX_UNREACHABLE("Not expected to be called.") + } +}; + class LikeWithRe2 final : public VectorFunction { public: LikeWithRe2(StringView pattern, std::optional escapeChar) { @@ -947,6 +959,9 @@ std::shared_ptr makeLike( inputArgs[2].type->toString()); auto constantEscape = escape->as>(); + if (constantEscape->isNullAt(0)) { + return std::make_shared(); + } try { VELOX_USER_CHECK_EQ( @@ -966,6 +981,10 @@ std::shared_ptr makeLike( "{} requires second argument to be a constant of type VARCHAR", name, inputArgs[1].type->toString()); + if (constantPattern->isNullAt(0)) { + return std::make_shared(); + } + auto pattern = constantPattern->as>()->valueAt(0); if (!escapeChar) { PatternKind patternKind; diff --git a/velox/functions/lib/tests/Re2FunctionsTest.cpp b/velox/functions/lib/tests/Re2FunctionsTest.cpp index 1c640d57d612c..59a6e947d8447 100644 --- a/velox/functions/lib/tests/Re2FunctionsTest.cpp +++ b/velox/functions/lib/tests/Re2FunctionsTest.cpp @@ -593,6 +593,16 @@ TEST_F(Re2FunctionsTest, likePatternSuffix) { EXPECT_TRUE(like(input, generateString(kAnyWildcardCharacter) + input)); } +TEST_F(Re2FunctionsTest, nullConstantPatternOrEscape) { + // Test null pattern. + ASSERT_TRUE( + !evaluateOnce("like('a', cast (null as varchar))").has_value()); + + // Test null escape. + ASSERT_TRUE( + !evaluateOnce("like('a', 'a', cast(null as varchar))").has_value()); +} + TEST_F(Re2FunctionsTest, likePatternAndEscape) { auto like = ([&](std::optional str, std::optional pattern,