forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARROW-6775: [C++][Python] Implement list_value_lengths and list_paren…
…t_indices functions This adds two functions that operate on list types: * list_value_lengths: returns an int32 (for List) or int64 (for LargeList) array with the number of elements in each list value slot * list_parent_indices: returns an int32/int64 array with the same length as the child values array of a List type where each value is the index of the list "slot" containing each child value Closes apache#7632 from wesm/some-list-functions Lead-authored-by: Wes McKinney <[email protected]> Co-authored-by: Antoine Pitrou <[email protected]> Signed-off-by: Antoine Pitrou <[email protected]>
- Loading branch information
Showing
12 changed files
with
244 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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. | ||
|
||
// Vector kernels involving nested types | ||
|
||
#include "arrow/array/array_base.h" | ||
#include "arrow/compute/kernels/common.h" | ||
#include "arrow/result.h" | ||
#include "arrow/visitor_inline.h" | ||
|
||
namespace arrow { | ||
namespace compute { | ||
namespace internal { | ||
namespace { | ||
|
||
template <typename Type, typename offset_type = typename Type::offset_type> | ||
void ListValueLengths(KernelContext* ctx, const ExecBatch& batch, Datum* out) { | ||
using ScalarType = typename TypeTraits<Type>::ScalarType; | ||
using OffsetScalarType = typename TypeTraits<Type>::OffsetScalarType; | ||
|
||
if (batch[0].kind() == Datum::ARRAY) { | ||
typename TypeTraits<Type>::ArrayType list(batch[0].array()); | ||
ArrayData* out_arr = out->mutable_array(); | ||
auto out_values = out_arr->GetMutableValues<offset_type>(1); | ||
const offset_type* offsets = list.raw_value_offsets(); | ||
::arrow::internal::detail::VisitBitBlocksVoid( | ||
list.data()->buffers[0], list.offset(), list.length(), | ||
[&](int64_t position) { | ||
*out_values++ = offsets[position + 1] - offsets[position]; | ||
}, | ||
[&]() { *out_values++ = 0; }); | ||
} else { | ||
const auto& arg0 = batch[0].scalar_as<ScalarType>(); | ||
if (arg0.is_valid) { | ||
checked_cast<OffsetScalarType*>(out->scalar().get())->value = | ||
static_cast<offset_type>(arg0.value->length()); | ||
} | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
void RegisterScalarNested(FunctionRegistry* registry) { | ||
auto list_value_lengths = | ||
std::make_shared<ScalarFunction>("list_value_lengths", Arity::Unary()); | ||
DCHECK_OK(list_value_lengths->AddKernel({InputType(Type::LIST)}, int32(), | ||
ListValueLengths<ListType>)); | ||
DCHECK_OK(list_value_lengths->AddKernel({InputType(Type::LARGE_LIST)}, int64(), | ||
ListValueLengths<LargeListType>)); | ||
DCHECK_OK(registry->AddFunction(std::move(list_value_lengths))); | ||
} | ||
|
||
} // namespace internal | ||
} // namespace compute | ||
} // namespace arrow |
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,40 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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 <gtest/gtest.h> | ||
|
||
#include "arrow/compute/api.h" | ||
#include "arrow/compute/kernels/test_util.h" | ||
#include "arrow/result.h" | ||
#include "arrow/testing/gtest_util.h" | ||
|
||
namespace arrow { | ||
namespace compute { | ||
|
||
static std::shared_ptr<DataType> GetOffsetType(const DataType& type) { | ||
return type.id() == Type::LIST ? int32() : int64(); | ||
} | ||
|
||
TEST(TestScalarNested, ListValueLengths) { | ||
for (auto ty : {list(int32()), large_list(int32())}) { | ||
CheckScalarUnary("list_value_lengths", ty, "[[0, null, 1], null, [2, 3], []]", | ||
GetOffsetType(*ty), "[3, null, 2, 0]"); | ||
} | ||
} | ||
|
||
} // namespace compute | ||
} // namespace arrow |
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
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