-
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.
Summary: In Presto, `flatten` ignores NULL array element in the input. In Spark, `flatten` returns NULL if any array element of the input is NULL. Spark 3.5 ref: [Flatten function](https://github.com/apache/spark/blob/v3.5.1/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala#L2796) Pull Request resolved: #9593 Reviewed By: pedroerp Differential Revision: D56580116 Pulled By: kagamiori fbshipit-source-id: 535f8112e3d37921f46ee41a8a05401f76c092cf
- Loading branch information
1 parent
1daeb9d
commit 32289f9
Showing
5 changed files
with
135 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,57 @@ | ||
/* | ||
* 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. | ||
*/ | ||
#pragma once | ||
|
||
#include "velox/functions/Udf.h" | ||
|
||
namespace facebook::velox::functions::sparksql { | ||
|
||
/// flatten(array(array(E))) → array(E) | ||
/// Flattens nested array by concatenating the contained arrays. | ||
template <typename T> | ||
struct ArrayFlattenFunction { | ||
VELOX_DEFINE_FUNCTION_TYPES(T) | ||
|
||
// INT_MAX - 15, keep the same limit with spark. | ||
static constexpr int32_t kMaxNumberOfElements = 2147483632; | ||
|
||
FOLLY_ALWAYS_INLINE bool call( | ||
out_type<Array<Generic<T1>>>& out, | ||
const arg_type<Array<Array<Generic<T1>>>>& arrays) { | ||
int64_t elementCount = 0; | ||
for (const auto& array : arrays) { | ||
if (array.has_value()) { | ||
elementCount += array.value().size(); | ||
} else { | ||
// Return NULL if any of the nested arrays is NULL. | ||
return false; | ||
} | ||
} | ||
|
||
VELOX_USER_CHECK_LE( | ||
elementCount, | ||
kMaxNumberOfElements, | ||
"array flatten result exceeds the max array size limit {}", | ||
kMaxNumberOfElements); | ||
|
||
out.reserve(elementCount); | ||
for (const auto& array : arrays) { | ||
out.add_items(array.value()); | ||
}; | ||
return true; | ||
} | ||
}; | ||
} // namespace facebook::velox::functions::sparksql |
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,62 @@ | ||
/* | ||
* 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/sparksql/tests/SparkFunctionBaseTest.h" | ||
|
||
using namespace facebook::velox::test; | ||
|
||
namespace facebook::velox::functions::sparksql::test { | ||
namespace { | ||
|
||
class ArrayFlattenTest : public SparkFunctionBaseTest { | ||
protected: | ||
void testExpression( | ||
const std::string& expression, | ||
const std::vector<VectorPtr>& input, | ||
const VectorPtr& expected) { | ||
const auto result = evaluate(expression, makeRowVector(input)); | ||
assertEqualVectors(expected, result); | ||
} | ||
}; | ||
|
||
// Flatten integer arrays. | ||
TEST_F(ArrayFlattenTest, intArrays) { | ||
const auto arrayOfArrays = makeNestedArrayVectorFromJson<int64_t>({ | ||
"[[1, 1], [2, 2], [3, 3]]", | ||
"[[4, 4]]", | ||
"[[5, 5], [6, 6]]", | ||
}); | ||
|
||
const auto expected = makeArrayVectorFromJson<int64_t>( | ||
{"[1, 1, 2, 2, 3, 3]", "[4, 4]", "[5, 5, 6, 6]"}); | ||
|
||
testExpression("flatten(c0)", {arrayOfArrays}, expected); | ||
} | ||
|
||
// Flatten arrays with null. | ||
TEST_F(ArrayFlattenTest, nullArray) { | ||
const auto arrayOfArrays = makeNestedArrayVectorFromJson<int64_t>({ | ||
"[[1, 1], null, [3, 3]]", | ||
"null", | ||
"[[5, null], [null, 6], [null, null], []]", | ||
}); | ||
|
||
const auto expected = makeArrayVectorFromJson<int64_t>( | ||
{"null", "null", "[5, null, null, 6, null, null]"}); | ||
|
||
testExpression("flatten(c0)", {arrayOfArrays}, expected); | ||
} | ||
} // namespace | ||
} // namespace facebook::velox::functions::sparksql::test |
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