Skip to content

Commit

Permalink
Fix: Explicit handle null pattern and escape in like function. (#6894)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #6894

When value at constant vector is null we shall not call valueAt().
When i added a check in ConstantVector valueAt that value is not null i got other faluires
I will address them in seperate PR then add the check.

Reviewed By: mbasmanova

Differential Revision: D49917874

fbshipit-source-id: db210ee2a777f1c4a1378d5d049a58389b08e646
  • Loading branch information
laithsakka authored and facebook-github-bot committed Oct 10, 2023
1 parent fefcd9f commit 37d8984
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions velox/expression/VectorFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ class AlwaysFailingVectorFunction final : public VectorFunction {
std::exception_ptr exceptionPtr_;
};

// This functions is used when we know a function will never be called because
// it is default null with a literal null input value. For example like(c0,
// null).
class ApplyNeverCalled final : public VectorFunction {
void apply(
const SelectivityVector&,
std::vector<VectorPtr>&,
const TypePtr&,
EvalCtx&,
VectorPtr&) const final {
VELOX_UNREACHABLE("Not expected to be called.")
}
};

// Factory for functions which are template generated from simple functions.
class SimpleFunctionAdapterFactory {
public:
Expand Down
7 changes: 7 additions & 0 deletions velox/functions/lib/Re2Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,9 @@ std::shared_ptr<exec::VectorFunction> makeLike(
inputArgs[2].type->toString());

auto constantEscape = escape->as<ConstantVector<StringView>>();
if (constantEscape->isNullAt(0)) {
return std::make_shared<exec::ApplyNeverCalled>();
}

try {
VELOX_USER_CHECK_EQ(
Expand All @@ -966,6 +969,10 @@ std::shared_ptr<exec::VectorFunction> makeLike(
"{} requires second argument to be a constant of type VARCHAR",
name,
inputArgs[1].type->toString());
if (constantPattern->isNullAt(0)) {
return std::make_shared<exec::ApplyNeverCalled>();
}

auto pattern = constantPattern->as<ConstantVector<StringView>>()->valueAt(0);
if (!escapeChar) {
PatternKind patternKind;
Expand Down
10 changes: 10 additions & 0 deletions velox/functions/lib/tests/Re2FunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>("like('a', cast (null as varchar))").has_value());

// Test null escape.
ASSERT_TRUE(
!evaluateOnce<bool>("like('a', 'a', cast(null as varchar))").has_value());
}

TEST_F(Re2FunctionsTest, likePatternAndEscape) {
auto like = ([&](std::optional<std::string> str,
std::optional<std::string> pattern,
Expand Down

0 comments on commit 37d8984

Please sign in to comment.