forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transform_keys and transform_values Presto functions (facebookinc…
…ubator#2245) Summary: Pull Request resolved: facebookincubator#2245 Differential Revision: D38565343 Pulled By: mbasmanova fbshipit-source-id: a6a670105d921cf6fd8d88f4d52e112f1a7c00a4
- Loading branch information
1 parent
4a86bd6
commit 12ba118
Showing
10 changed files
with
718 additions
and
2 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
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,126 @@ | ||
/* | ||
* 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/vector/FunctionVector.h" | ||
|
||
namespace facebook::velox::functions { | ||
namespace { | ||
|
||
// See documentation at https://prestodb.io/docs/current/functions/map.html | ||
class TransformKeysFunction : public exec::VectorFunction { | ||
public: | ||
bool isDefaultNullBehavior() const override { | ||
// transform_keys is null preserving for the map. But | ||
// since an expr tree with a lambda depends on all named fields, including | ||
// captures, a null in a capture does not automatically make a | ||
// null result. | ||
return false; | ||
} | ||
|
||
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); | ||
|
||
// Flatten input map. | ||
exec::LocalDecodedVector mapDecoder(context, *args[0], rows); | ||
auto& decodedMap = *mapDecoder.get(); | ||
|
||
auto flatMap = flattenMap(rows, args[0], decodedMap); | ||
|
||
std::vector<VectorPtr> lambdaArgs = { | ||
flatMap->mapKeys(), flatMap->mapValues()}; | ||
auto numKeys = flatMap->mapKeys()->size(); | ||
|
||
VectorPtr transformedKeys; | ||
|
||
// Loop over lambda functions and apply these to keys of the map. | ||
// In most cases there will be only one function and the loop will run once. | ||
auto it = args[1]->asUnchecked<FunctionVector>()->iterator(&rows); | ||
while (auto entry = it.next()) { | ||
auto keyRows = | ||
toElementRows<MapVector>(numKeys, *entry.rows, flatMap.get()); | ||
auto wrapCapture = toWrapCapture<MapVector>( | ||
numKeys, entry.callable, *entry.rows, flatMap); | ||
|
||
entry.callable->apply( | ||
keyRows, wrapCapture, context, lambdaArgs, &transformedKeys); | ||
} | ||
|
||
// TODO Check for duplicates in transformedKeys. | ||
|
||
auto localResult = std::make_shared<MapVector>( | ||
flatMap->pool(), | ||
outputType, | ||
flatMap->nulls(), | ||
flatMap->size(), | ||
flatMap->offsets(), | ||
flatMap->sizes(), | ||
transformedKeys, | ||
flatMap->mapValues()); | ||
|
||
checkDuplicateKeys(localResult, rows); | ||
|
||
context->moveOrCopyResult(localResult, rows, result); | ||
} | ||
|
||
static std::vector<std::shared_ptr<exec::FunctionSignature>> signatures() { | ||
// map(K1, V), function(K1, V) -> K2 -> map(K2, V) | ||
return {exec::FunctionSignatureBuilder() | ||
.typeVariable("K1") | ||
.typeVariable("K2") | ||
.typeVariable("V") | ||
.returnType("map(K2,V)") | ||
.argumentType("map(K1,V)") | ||
.argumentType("function(K1,V,K2)") | ||
.build()}; | ||
} | ||
|
||
private: | ||
void checkDuplicateKeys( | ||
const MapVectorPtr& mapVector, | ||
const SelectivityVector& rows) const { | ||
static const char* kDuplicateKey = "Duplicate map keys are not allowed"; | ||
|
||
MapVector::canonicalize(mapVector); | ||
|
||
auto offsets = mapVector->rawOffsets(); | ||
auto sizes = mapVector->rawSizes(); | ||
auto mapKeys = mapVector->mapKeys(); | ||
rows.applyToSelected([&](auto row) { | ||
auto offset = offsets[row]; | ||
auto size = sizes[row]; | ||
for (auto i = 1; i < size; i++) { | ||
if (mapKeys->equalValueAt(mapKeys.get(), offset + i, offset + i - 1)) { | ||
VELOX_USER_FAIL("{}", kDuplicateKey); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
} // namespace | ||
|
||
VELOX_DECLARE_VECTOR_FUNCTION( | ||
udf_transform_keys, | ||
TransformKeysFunction::signatures(), | ||
std::make_unique<TransformKeysFunction>()); | ||
|
||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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/vector/FunctionVector.h" | ||
|
||
namespace facebook::velox::functions { | ||
namespace { | ||
|
||
// See documentation at https://prestodb.io/docs/current/functions/map.html | ||
class TransformValuesFunction : public exec::VectorFunction { | ||
public: | ||
bool isDefaultNullBehavior() const override { | ||
// transform_values is null preserving for the map. But | ||
// since an expr tree with a lambda depends on all named fields, including | ||
// captures, a null in a capture does not automatically make a | ||
// null result. | ||
return false; | ||
} | ||
|
||
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); | ||
|
||
// Flatten input map. | ||
exec::LocalDecodedVector mapDecoder(context, *args[0], rows); | ||
auto& decodedMap = *mapDecoder.get(); | ||
|
||
auto flatMap = flattenMap(rows, args[0], decodedMap); | ||
|
||
std::vector<VectorPtr> lambdaArgs = { | ||
flatMap->mapKeys(), flatMap->mapValues()}; | ||
auto numValues = flatMap->mapValues()->size(); | ||
|
||
VectorPtr transformedValues; | ||
|
||
// Loop over lambda functions and apply these to keys of the map. | ||
// In most cases there will be only one function and the loop will run once. | ||
auto it = args[1]->asUnchecked<FunctionVector>()->iterator(&rows); | ||
while (auto entry = it.next()) { | ||
auto keyRows = | ||
toElementRows<MapVector>(numValues, *entry.rows, flatMap.get()); | ||
auto wrapCapture = toWrapCapture<MapVector>( | ||
numValues, entry.callable, *entry.rows, flatMap); | ||
|
||
entry.callable->apply( | ||
keyRows, wrapCapture, context, lambdaArgs, &transformedValues); | ||
} | ||
|
||
auto localResult = std::make_shared<MapVector>( | ||
flatMap->pool(), | ||
outputType, | ||
flatMap->nulls(), | ||
flatMap->size(), | ||
flatMap->offsets(), | ||
flatMap->sizes(), | ||
flatMap->mapKeys(), | ||
transformedValues); | ||
context->moveOrCopyResult(localResult, rows, result); | ||
} | ||
|
||
static std::vector<std::shared_ptr<exec::FunctionSignature>> signatures() { | ||
// map(K, V1), function(K, V1) -> V2 -> map(K, V2) | ||
return {exec::FunctionSignatureBuilder() | ||
.typeVariable("K") | ||
.typeVariable("V1") | ||
.typeVariable("V2") | ||
.returnType("map(K,V2)") | ||
.argumentType("map(K,V1)") | ||
.argumentType("function(K,V1,V2)") | ||
.build()}; | ||
} | ||
}; | ||
} // namespace | ||
|
||
VELOX_DECLARE_VECTOR_FUNCTION( | ||
udf_transform_values, | ||
TransformValuesFunction::signatures(), | ||
std::make_unique<TransformValuesFunction>()); | ||
|
||
} // 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
Oops, something went wrong.