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().
This added check in ConstantVector and also make sure like function
handles null explicitly.

Differential Revision: D49917874
  • Loading branch information
laithsakka authored and facebook-github-bot committed Oct 4, 2023
1 parent 04da9ea commit 71d9d6d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions velox/functions/lib/Re2Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<VectorPtr>&,
const TypePtr&,
EvalCtx&,
VectorPtr&) const final {
VELOX_UNREACHABLE("Not expected to be called.")
}
};

class LikeWithRe2 final : public VectorFunction {
public:
LikeWithRe2(StringView pattern, std::optional<char> escapeChar) {
Expand Down Expand Up @@ -947,6 +959,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<AlwaysNull>();
}

try {
VELOX_USER_CHECK_EQ(
Expand All @@ -966,6 +981,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<AlwaysNull>();
}

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
1 change: 1 addition & 0 deletions velox/vector/ConstantVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class ConstantVector final : public SimpleVector<T> {
}

virtual const T valueAt(vector_size_t /*idx*/) const override {
VELOX_DCHECK(!isNullAt(0));
VELOX_DCHECK(initialized_);
SimpleVector<T>::checkElementSize();
return value();
Expand Down

0 comments on commit 71d9d6d

Please sign in to comment.