-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "velox/expression/Expr.h" | ||
#include "velox/expression/VectorFunction.h" | ||
#include "velox/functions/lib/LambdaFunctionUtil.h" | ||
#include "velox/functions/lib/RowsTranslationUtil.h" | ||
#include "velox/vector/FunctionVector.h" | ||
|
||
namespace facebook::velox::functions { | ||
namespace { | ||
|
||
class AllMatchFunction : public exec::VectorFunction { | ||
public: | ||
void apply( | ||
const SelectivityVector& rows, | ||
std::vector<VectorPtr>& args, | ||
const TypePtr& outputType, | ||
exec::EvalCtx& context, | ||
VectorPtr& result) const override { | ||
VELOX_CHECK_EQ(args.size(), 2); | ||
|
||
exec::LocalDecodedVector arrayDecoder(context, *args[0], rows); | ||
auto& decodedArray = *arrayDecoder.get(); | ||
|
||
auto flatArray = flattenArray(rows, args[0], decodedArray); | ||
auto inputOffsets = flatArray->rawOffsets(); | ||
auto inputSizes = flatArray->rawSizes(); | ||
|
||
std::vector<VectorPtr> lambdaArgs = {flatArray->elements()}; | ||
auto newNumElements = flatArray->elements()->size(); | ||
|
||
SelectivityVector finalSelection; | ||
if (!context.isFinalSelection()) { | ||
finalSelection = toElementRows<ArrayVector>( | ||
newNumElements, *context.finalSelection(), flatArray.get()); | ||
} | ||
|
||
VectorPtr matchBits; | ||
auto elementToTopLevelRows = getElementToTopLevelRows( | ||
newNumElements, rows, flatArray.get(), context.pool()); | ||
|
||
// loop over lambda functions and apply these to elements of the base array; | ||
// in most cases there will be only one function and the loop will run once | ||
context.ensureWritable(rows, BOOLEAN(), result); | ||
auto flatResult = result->asFlatVector<bool>(); | ||
exec::LocalDecodedVector bitsDecoder(context); | ||
auto it = args[1]->asUnchecked<FunctionVector>()->iterator(&rows); | ||
while (auto entry = it.next()) { | ||
auto elementRows = toElementRows<ArrayVector>( | ||
newNumElements, *entry.rows, flatArray.get()); | ||
auto wrapCapture = toWrapCapture<ArrayVector>( | ||
newNumElements, entry.callable, *entry.rows, flatArray); | ||
|
||
entry.callable->apply( | ||
elementRows, | ||
finalSelection, | ||
wrapCapture, | ||
&context, | ||
lambdaArgs, | ||
elementToTopLevelRows, | ||
&matchBits); | ||
|
||
bitsDecoder.get()->decode(*matchBits, elementRows); | ||
entry.rows->applyToSelected([&](vector_size_t row) { | ||
auto size = inputSizes[row]; | ||
auto offset = inputOffsets[row]; | ||
auto allMatch = true; | ||
auto hasNull = false; | ||
for (auto i = 0; i < size; ++i) { | ||
auto idx = offset + i; | ||
if (bitsDecoder->isNullAt(idx)) { | ||
hasNull = true; | ||
} else if (!bitsDecoder->valueAt<bool>(idx)) { | ||
allMatch = false; | ||
break; | ||
} | ||
} | ||
|
||
if (hasNull && allMatch) { | ||
flatResult->setNull(row, true); | ||
} else { | ||
flatResult->set(row, allMatch); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
static std::vector<std::shared_ptr<exec::FunctionSignature>> signatures() { | ||
// array(T), function(T) -> boolean | ||
return {exec::FunctionSignatureBuilder() | ||
.typeVariable("T") | ||
.returnType("boolean") | ||
.argumentType("array(T)") | ||
.argumentType("function(T, boolean)") | ||
.build()}; | ||
} | ||
}; | ||
} // namespace | ||
|
||
VELOX_DECLARE_VECTOR_FUNCTION( | ||
udf_all_match, | ||
AllMatchFunction::signatures(), | ||
std::make_unique<AllMatchFunction>()); | ||
|
||
} // namespace facebook::velox::functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h" | ||
|
||
using namespace facebook::velox; | ||
using namespace facebook::velox::test; | ||
|
||
class ArrayAllMatchTest : public functions::test::FunctionBaseTest {}; | ||
|
||
TEST_F(ArrayAllMatchTest, bigints) { | ||
auto input = makeNullableArrayVector<int64_t>( | ||
{{}, | ||
{2}, | ||
{std::numeric_limits<int64_t>::max()}, | ||
{std::numeric_limits<int64_t>::min()}, | ||
{std::nullopt, std::nullopt}, // return null if all is null | ||
{2, | ||
std::nullopt}, // return null if one or more is null and others matched | ||
{1, std::nullopt, 2}}); // return false if one is not matched | ||
auto result = evaluate<SimpleVector<bool>>( | ||
"all_match(c0, x -> (x % 2 = 0))", makeRowVector({input})); | ||
|
||
auto expectedResult = makeNullableFlatVector<bool>( | ||
{true, true, false, true, std::nullopt, std::nullopt, false}); | ||
assertEqualVectors(expectedResult, result); | ||
} | ||
|
||
TEST_F(ArrayAllMatchTest, strings) { | ||
auto input = makeNullableArrayVector<StringView>( | ||
{{}, {"abc"}, {"ab", "abc"}, {std::nullopt}}); | ||
auto result = evaluate<SimpleVector<bool>>( | ||
"all_match(c0, x -> (x == 'abc'))", makeRowVector({input})); | ||
|
||
auto expectedResult = | ||
makeNullableFlatVector<bool>({true, true, false, std::nullopt}); | ||
assertEqualVectors(expectedResult, result); | ||
} | ||
|
||
TEST_F(ArrayAllMatchTest, doubles) { | ||
auto input = | ||
makeNullableArrayVector<double>({{}, {1.2}, {3.0, 0}, {std::nullopt}}); | ||
auto result = evaluate<SimpleVector<bool>>( | ||
"all_match(c0, x -> (x > 1.1))", makeRowVector({input})); | ||
|
||
auto expectedResult = | ||
makeNullableFlatVector<bool>({true, true, false, std::nullopt}); | ||
assertEqualVectors(expectedResult, result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters