-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ListViewArray: Buffers validation, creation from JSON, and basic tests
- Loading branch information
Showing
10 changed files
with
399 additions
and
6 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,104 @@ | ||
// 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/array/list_view.h" | ||
#include "arrow/array/util.h" | ||
#include "arrow/testing/gtest_util.h" | ||
#include "arrow/type_fwd.h" | ||
#include "arrow/util/checked_cast.h" | ||
|
||
namespace arrow { | ||
|
||
using internal::checked_cast; | ||
|
||
// ---------------------------------------------------------------------- | ||
// List-view array tests | ||
|
||
namespace { | ||
|
||
class TestListViewArray : public ::testing::Test { | ||
public: | ||
std::shared_ptr<Array> string_values; | ||
std::shared_ptr<Array> int32_values; | ||
std::shared_ptr<Array> int16_values; | ||
|
||
void SetUp() override { | ||
string_values = ArrayFromJSON(utf8(), R"(["Hello", "World", null])"); | ||
int32_values = ArrayFromJSON(int32(), "[1, 20, 3]"); | ||
int16_values = ArrayFromJSON(int16(), "[10, 2, 30]"); | ||
} | ||
|
||
static std::shared_ptr<Array> Offsets(std::string_view json) { | ||
return ArrayFromJSON(int32(), json); | ||
} | ||
|
||
static std::shared_ptr<Array> Sizes(std::string_view json) { | ||
return ArrayFromJSON(int32(), json); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST_F(TestListViewArray, MakeArray) { | ||
ASSERT_OK_AND_ASSIGN( | ||
auto list_view_array, | ||
ListViewArray::FromArrays(list_view(utf8()), Offsets("[0, 0, 1, 2]"), | ||
Sizes("[2, 1, 1, 1]"), string_values)); | ||
auto array_data = list_view_array->data(); | ||
auto new_array = MakeArray(array_data); | ||
ASSERT_ARRAYS_EQUAL(*new_array, *list_view_array); | ||
// Should be the exact same ArrayData object | ||
ASSERT_EQ(new_array->data(), array_data); | ||
ASSERT_NE(std::dynamic_pointer_cast<ListViewArray>(new_array), NULLPTR); | ||
} | ||
|
||
TEST_F(TestListViewArray, FromOffsetsAndSizes) { | ||
std::shared_ptr<ListViewArray> list_view_array; | ||
|
||
ASSERT_OK_AND_ASSIGN( | ||
list_view_array, | ||
ListViewArray::FromArrays(list_view(int32()), Offsets("[0, 0, 1, 1000]"), | ||
Sizes("[2, 1, 1, null]"), int32_values)); | ||
ASSERT_EQ(list_view_array->length(), 4); | ||
ASSERT_ARRAYS_EQUAL(*list_view_array->values(), *int32_values); | ||
ASSERT_EQ(list_view_array->offset(), 0); | ||
ASSERT_EQ(list_view_array->data()->GetNullCount(), 1); | ||
ASSERT_EQ(list_view_array->data()->buffers.size(), 3); | ||
|
||
// Passing a non-zero logical offset | ||
ASSERT_OK_AND_ASSIGN( | ||
list_view_array, | ||
ListViewArray::FromArrays(list_view(utf8()), Offsets("[0, 0, 1, 2]")->Slice(1), | ||
Sizes("[2, 1, 1, 1]")->Slice(1), string_values)); | ||
ASSERT_EQ(list_view_array->length(), 3); | ||
ASSERT_ARRAYS_EQUAL(*list_view_array->values(), *string_values); | ||
ASSERT_EQ(list_view_array->data()->GetNullCount(), 0); | ||
ASSERT_EQ(list_view_array->offset(), 1); | ||
|
||
ASSERT_RAISES_WITH_MESSAGE( | ||
Invalid, "Invalid: values type must match int32", | ||
ListViewArray::FromArrays(list_view(int32()), Offsets("[0, 0, 1, 1000]"), | ||
Sizes("[2, 1, 1, null]"), int16_values)); | ||
ASSERT_RAISES_WITH_MESSAGE( | ||
Invalid, "Invalid: offsets and sizes must have the same length", | ||
ListViewArray::FromArrays(list_view(int32()), Offsets("[0, 0, 1]"), | ||
Sizes("[2, 1, 1, null]"), int32_values)); | ||
} | ||
|
||
} // 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
Oops, something went wrong.