Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ends_with Presto function #6908

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions velox/docs/functions/presto/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ String Functions
This function provides the same functionality as the
SQL-standard concatenation operator (``||``).

.. function:: ends_with(string, substring) -> boolean

Returns whether ``string`` ends_with with ``substring``.

.. function:: from_utf8(binary) -> varchar

Decodes a UTF-8 encoded string from ``binary``. Invalid UTF-8 sequences
Expand Down
18 changes: 18 additions & 0 deletions velox/functions/prestosql/StringFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,24 @@ struct StartsWithFunction {
}
};

template <typename T>
struct EndsWithFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);

FOLLY_ALWAYS_INLINE void call(
out_type<bool>& result,
const arg_type<Varchar>& x,
const arg_type<Varchar>& y) {
if (x.size() < y.size()) {
result = false;
return;
}

result =
(memcmp(x.data() + (x.size() - y.size()), y.data(), y.size()) == 0);
}
};

/// Pad functions
/// lpad(string, size, padString) → varchar
/// Left pads string to size characters with padString. If size is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ void registerSimpleFunctions(const std::string& prefix) {

registerFunction<StartsWithFunction, bool, Varchar, Varchar>(
{prefix + "starts_with"});
registerFunction<EndsWithFunction, bool, Varchar, Varchar>(
{prefix + "ends_with"});

registerFunction<SubstrFunction, Varchar, Varchar, int64_t>(
{prefix + "substr"});
Expand Down
18 changes: 18 additions & 0 deletions velox/functions/prestosql/tests/StringFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,24 @@ TEST_F(StringFunctionsTest, startsWith) {
ASSERT_FALSE(startsWith("", " "));
}

TEST_F(StringFunctionsTest, endsWith) {
auto endsWith = [&](const std::string& x, const std::string& y) {
return evaluateOnce<bool>(
"ends_with(c0, c1)", std::optional(x), std::optional(y))
.value();
};

ASSERT_TRUE(endsWith("", ""));
ASSERT_TRUE(endsWith("Hello world!", ""));
ASSERT_TRUE(endsWith("Hello world!", "world!"));
ASSERT_TRUE(endsWith("Hello world!", "lo world!"));
ASSERT_TRUE(endsWith("Hello world!", "Hello world!"));

ASSERT_FALSE(endsWith("Hello world!", " Hello world!"));
ASSERT_FALSE(endsWith("Hello world!", "hello"));
ASSERT_FALSE(endsWith("", " "));
}

// Test strpos function
template <typename TInstance>
void StringFunctionsTest::testStringPositionAllFlatVector(
Expand Down
Loading