Skip to content

Commit

Permalink
Fix: Explicit handle null pattern and escape in like function. (faceb…
Browse files Browse the repository at this point in the history
…ookincubator#6894)

Summary:


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.

Differential Revision: D49917874
  • Loading branch information
laithsakka authored and facebook-github-bot committed Oct 5, 2023
1 parent c00c806 commit a5792a9
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 AlwaysNullDefaultNull 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::AlwaysNullDefaultNull>();
}

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::AlwaysNullDefaultNull>();
}

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 a5792a9

Please sign in to comment.