Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions cpp/src/parquet/arrow/arrow_reader_writer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2469,7 +2469,60 @@ TEST(ArrowReadWrite, ListOfStructOfList1) {
CheckSimpleRoundtrip(table, 2);
}

TEST(ArrowReadWrite, DISABLED_ListOfStructOfList2) {
TEST(ArrowReadWrite, Map) {
using ::arrow::field;
using ::arrow::map;

auto type = map(::arrow::int16(), ::arrow::utf8());

const char* json = R"([
[[1, "a"], [2, "b"]],
[[3, "c"]],
[],
null,
[[4, "d"], [5, "e"], [6, "f"]]
])";
auto array = ::arrow::ArrayFromJSON(type, json);
auto table = ::arrow::Table::Make(::arrow::schema({field("root", type)}), {array});
auto props_store_schema = ArrowWriterProperties::Builder().store_schema()->build();
CheckSimpleRoundtrip(table, 2, props_store_schema);
}

TEST(ArrowReadWrite, LargeList) {
using ::arrow::field;
using ::arrow::large_list;
using ::arrow::struct_;

auto type = large_list(::arrow::int16());

const char* json = R"([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])";
auto array = ::arrow::ArrayFromJSON(type, json);
auto table = ::arrow::Table::Make(::arrow::schema({field("root", type)}), {array});
auto props_store_schema = ArrowWriterProperties::Builder().store_schema()->build();
CheckSimpleRoundtrip(table, 2, props_store_schema);
}

TEST(ArrowReadWrite, FixedSizeList) {
using ::arrow::field;
using ::arrow::fixed_size_list;
using ::arrow::struct_;

auto type = fixed_size_list(::arrow::int16(), /*size=*/3);

const char* json = R"([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])";
auto array = ::arrow::ArrayFromJSON(type, json);
auto table = ::arrow::Table::Make(::arrow::schema({field("root", type)}), {array});
auto props_store_schema = ArrowWriterProperties::Builder().store_schema()->build();
CheckSimpleRoundtrip(table, 2, props_store_schema);
}

TEST(ArrowReadWrite, ListOfStructOfList2) {
using ::arrow::field;
using ::arrow::list;
using ::arrow::struct_;
Expand All @@ -2484,7 +2537,7 @@ TEST(ArrowReadWrite, DISABLED_ListOfStructOfList2) {
[{"a": 123, "b": [1, 2, 3]}],
null,
[],
[{"a": 456}, {"a": 789, "b": [null]}, {"a": 876, "b": [4, 5, 6]}]])";
[{"a": 456, "b": []}, {"a": 789, "b": [null]}, {"a": 876, "b": [4, 5, 6]}]])";
auto array = ::arrow::ArrayFromJSON(type, json);
auto table = ::arrow::Table::Make(::arrow::schema({field("root", type)}), {array});
CheckSimpleRoundtrip(table, 2);
Expand Down
86 changes: 86 additions & 0 deletions cpp/src/parquet/arrow/arrow_schema_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,44 @@ TEST_F(TestConvertParquetSchema, ParquetFlatDecimals) {
ASSERT_NO_FATAL_FAILURE(CheckFlatSchema(arrow_schema));
}

TEST_F(TestConvertParquetSchema, ParquetMaps) {
std::vector<NodePtr> parquet_fields;
std::vector<std::shared_ptr<Field>> arrow_fields;

// MAP encoding example taken from parquet-format/LogicalTypes.md

// Two column map.
{
auto key = PrimitiveNode::Make("key", Repetition::REQUIRED, ParquetType::BYTE_ARRAY,
ConvertedType::UTF8);
auto value = PrimitiveNode::Make("value", Repetition::OPTIONAL,
ParquetType::BYTE_ARRAY, ConvertedType::UTF8);

auto list = GroupNode::Make("key_value", Repetition::REPEATED, {key, value});
parquet_fields.push_back(
GroupNode::Make("my_map", Repetition::REQUIRED, {list}, LogicalType::Map()));
auto arrow_value = ::arrow::field("string", UTF8, /*nullable=*/true);
auto arrow_map = ::arrow::map(/*key=*/UTF8, arrow_value);
arrow_fields.push_back(::arrow::field("my_map", arrow_map, false));
}
// Single column map (i.e. set) gets converted to list of struct.
{
auto key = PrimitiveNode::Make("key", Repetition::REQUIRED, ParquetType::BYTE_ARRAY,
ConvertedType::UTF8);

auto list = GroupNode::Make("key_value", Repetition::REPEATED, {key});
parquet_fields.push_back(
GroupNode::Make("my_set", Repetition::REQUIRED, {list}, LogicalType::Map()));
auto arrow_list = ::arrow::list({::arrow::field("key", UTF8, /*nullable=*/false)});
arrow_fields.push_back(::arrow::field("my_set", arrow_list, false));
}

auto arrow_schema = ::arrow::schema(arrow_fields);
ASSERT_OK(ConvertSchema(parquet_fields));

ASSERT_NO_FATAL_FAILURE(CheckFlatSchema(arrow_schema));
}

TEST_F(TestConvertParquetSchema, ParquetLists) {
std::vector<NodePtr> parquet_fields;
std::vector<std::shared_ptr<Field>> arrow_fields;
Expand Down Expand Up @@ -1217,6 +1255,54 @@ TEST_F(TestLevels, TestPrimitive) {
/*ancestor_list_def_level*/ 1})); // primitive field
}

TEST_F(TestLevels, TestMaps) {
// Two column map.
auto key = PrimitiveNode::Make("key", Repetition::REQUIRED, ParquetType::BYTE_ARRAY,
ConvertedType::UTF8);
auto value = PrimitiveNode::Make("value", Repetition::OPTIONAL, ParquetType::BYTE_ARRAY,
ConvertedType::UTF8);

auto list = GroupNode::Make("key_value", Repetition::REPEATED, {key, value});
SetParquetSchema(
GroupNode::Make("my_map", Repetition::OPTIONAL, {list}, LogicalType::Map()));
ASSERT_OK_AND_ASSIGN(std::deque<LevelInfo> levels,
RootToTreeLeafLevels(*manifest_, /*column_number=*/0));
EXPECT_THAT(
levels,
ElementsAre(LevelInfo{/*null_slot_usage=*/1, /*def_level=*/2, /*rep_level=*/1,
/*ancestor_list_def_level*/ 0},
LevelInfo{/*null_slot_usage=*/1, /*def_level=*/2, /*rep_level=*/1,
/*ancestor_list_def_level*/ 2},
LevelInfo{/*null_slot_usage=*/1, /*def_level=*/2, /*rep_level=*/1,
/*ancestor_list_def_level*/ 2}));

ASSERT_OK_AND_ASSIGN(levels, RootToTreeLeafLevels(*manifest_, /*column_number=*/1));
EXPECT_THAT(
levels,
ElementsAre(LevelInfo{/*null_slot_usage=*/1, /*def_level=*/2, /*rep_level=*/1,
/*ancestor_list_def_level*/ 0},
LevelInfo{/*null_slot_usage=*/1, /*def_level=*/2, /*rep_level=*/1,
/*ancestor_list_def_level*/ 2},
LevelInfo{/*null_slot_usage=*/1, /*def_level=*/3, /*rep_level=*/1,
/*ancestor_list_def_level*/ 2}));

// single column map.
key = PrimitiveNode::Make("key", Repetition::REQUIRED, ParquetType::BYTE_ARRAY,
ConvertedType::UTF8);

list = GroupNode::Make("key_value", Repetition::REPEATED, {key});
SetParquetSchema(
GroupNode::Make("my_set", Repetition::REQUIRED, {list}, LogicalType::Map()));

ASSERT_OK_AND_ASSIGN(levels, RootToTreeLeafLevels(*manifest_, /*column_number=*/0));
EXPECT_THAT(
levels,
ElementsAre(LevelInfo{/*null_slot_usage=*/1, /*def_level=*/1, /*rep_level=*/1,
/*ancestor_list_def_level*/ 0},
LevelInfo{/*null_slot_usage=*/1, /*def_level=*/1, /*rep_level=*/1,
/*ancestor_list_def_level*/ 1}));
}

TEST_F(TestLevels, TestSimpleGroups) {
// Arrow schema: struct(child: struct(inner: boolean not null))
SetParquetSchema(GroupNode::Make(
Expand Down
10 changes: 7 additions & 3 deletions cpp/src/parquet/arrow/path_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -815,13 +815,17 @@ class PathBuilder {
Status Visit(const ::arrow::FixedSizeListArray& array) {
MaybeAddNullable(array);
int32_t list_size = array.list_type()->list_size();
if (list_size == 0) {
info_.max_def_level++;
}
// Technically we could encode fixed size lists with two level encodings
// but since we always use 3 level encoding we increment def levels as
// well.
info_.max_def_level++;
info_.max_rep_level++;
info_.path.push_back(FixedSizeListNode(FixedSizedRangeSelector{list_size},
info_.max_rep_level, info_.max_def_level));
nullable_in_parent_ = array.list_type()->value_field()->nullable();
if (array.offset() > 0) {
return VisitInline(*array.values()->Slice(array.value_offset(0)));
}
return VisitInline(*array.values());
}

Expand Down
22 changes: 21 additions & 1 deletion cpp/src/parquet/arrow/path_internal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,26 @@ TEST_F(MultipathLevelBuilderTest, TestStruct) {
/*def_levels=*/std::vector<int16_t>({2, 2, 0}));
}

TEST_F(MultipathLevelBuilderTest, TestFixedSizeListNullableElements) {
auto entries = field("Entries", ::arrow::int64());
auto list_type = fixed_size_list(entries, 2);
auto array = ::arrow::ArrayFromJSON(list_type, R"([null, [2, 3], [4, 5], null])");

ASSERT_OK(
MultipathLevelBuilder::Write(*array, /*nullable=*/true, &context_, callback_));

ASSERT_THAT(results_, SizeIs(1));
results_[0].CheckLevels(/*def_levels=*/std::vector<int16_t>{0, 3, 3, 3, 3, 0},
/*rep_levels=*/std::vector<int16_t>{0, 0, 1, 0, 1, 0});

// Null slots take up space in a fixed size list (they can in variable size
// lists as well) but the actual written values are only the "middle" elements
// in this case.
ASSERT_THAT(results_[0].post_list_elements, SizeIs(1));
EXPECT_THAT(results_[0].post_list_elements[0].start, Eq(2));
EXPECT_THAT(results_[0].post_list_elements[0].end, Eq(6));
}

TEST_F(MultipathLevelBuilderTest, TestFixedSizeList) {
auto entries = field("Entries", ::arrow::int64(), /*nullable=*/false);
auto list_type = fixed_size_list(entries, 2);
Expand All @@ -470,7 +490,7 @@ TEST_F(MultipathLevelBuilderTest, TestFixedSizeList) {
MultipathLevelBuilder::Write(*array, /*nullable=*/true, &context_, callback_));

ASSERT_THAT(results_, SizeIs(1));
results_[0].CheckLevels(/*def_levels=*/std::vector<int16_t>{0, 1, 1, 1, 1, 0},
results_[0].CheckLevels(/*def_levels=*/std::vector<int16_t>{0, 2, 2, 2, 2, 0},
/*rep_levels=*/std::vector<int16_t>{0, 0, 1, 0, 1, 0});

// Null slots take up space in a fixed size list (they can in variable size
Expand Down
64 changes: 59 additions & 5 deletions cpp/src/parquet/arrow/reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,20 @@ class ListReader : public ColumnReaderImpl {
return item_reader_->LoadBatch(number_of_records);
}

virtual ::arrow::Result<std::shared_ptr<ChunkedArray>> AssembleArray(
std::shared_ptr<ArrayData> data) {
std::shared_ptr<Array> result = ::arrow::MakeArray(data);
return std::make_shared<ChunkedArray>(result);
}

Status BuildArray(int64_t length_upper_bound,
std::shared_ptr<ChunkedArray>* out) override {
const int16_t* def_levels;
const int16_t* rep_levels;
int64_t num_levels;
RETURN_NOT_OK(item_reader_->GetDefLevels(&def_levels, &num_levels));
RETURN_NOT_OK(item_reader_->GetRepLevels(&rep_levels, &num_levels));

std::shared_ptr<ResizableBuffer> validity_buffer;
::parquet::internal::ValidityBitmapInputOutput validity_io;
validity_io.values_read_upper_bound = length_upper_bound;
Expand Down Expand Up @@ -576,6 +583,7 @@ class ListReader : public ColumnReaderImpl {
if (validity_buffer != nullptr) {
RETURN_NOT_OK(
validity_buffer->Resize(BitUtil::BytesForBits(validity_io.values_read)));
validity_buffer->ZeroPadding();
}
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<ArrayData> item_chunk, ChunksToSingle(**out));

Expand All @@ -586,8 +594,7 @@ class ListReader : public ColumnReaderImpl {
/*length=*/validity_io.values_read, std::move(buffers),
std::vector<std::shared_ptr<ArrayData>>{item_chunk}, validity_io.null_count);

std::shared_ptr<Array> result = ::arrow::MakeArray(data);
*out = std::make_shared<ChunkedArray>(result);
ARROW_ASSIGN_OR_RAISE(*out, AssembleArray(std::move(data)));
return Status::OK();
}

Expand All @@ -600,6 +607,32 @@ class ListReader : public ColumnReaderImpl {
std::unique_ptr<ColumnReaderImpl> item_reader_;
};

class PARQUET_NO_EXPORT FixedSizeListReader : public ListReader<int32_t> {
public:
FixedSizeListReader(std::shared_ptr<ReaderContext> ctx, std::shared_ptr<Field> field,
::parquet::internal::LevelInfo level_info,
std::unique_ptr<ColumnReaderImpl> child_reader)
: ListReader(std::move(ctx), std::move(field), level_info,
std::move(child_reader)) {}
::arrow::Result<std::shared_ptr<ChunkedArray>> AssembleArray(
std::shared_ptr<ArrayData> data) final {
DCHECK_EQ(data->buffers.size(), 2);
DCHECK_EQ(field()->type()->id(), ::arrow::Type::FIXED_SIZE_LIST);
const auto& type = checked_cast<::arrow::FixedSizeListType&>(*field()->type());
const int32_t* offsets = reinterpret_cast<const int32_t*>(data->buffers[1]->data());
for (int x = 1; x <= data->length; x++) {
int32_t size = offsets[x] - offsets[x - 1];
if (size != type.list_size()) {
return Status::Invalid("Expected all lists to be of size=", type.list_size(),
" but index ", x, " had size=", size);
}
}
data->buffers.resize(1);
std::shared_ptr<Array> result = ::arrow::MakeArray(data);
return std::make_shared<ChunkedArray>(result);
}
};

class PARQUET_NO_EXPORT StructReader : public ColumnReaderImpl {
public:
explicit StructReader(std::shared_ptr<ReaderContext> ctx,
Expand Down Expand Up @@ -714,6 +747,7 @@ Status StructReader::BuildArray(int64_t length_upper_bound,
// Ensure all values are initialized.
if (null_bitmap) {
RETURN_NOT_OK(null_bitmap->Resize(BitUtil::BytesForBits(validity_io.values_read)));
null_bitmap->ZeroPadding();
}

END_PARQUET_CATCH_EXCEPTIONS
Expand Down Expand Up @@ -770,16 +804,36 @@ Status GetReader(const SchemaField& field, const std::shared_ptr<Field>& arrow_f
std::unique_ptr<FileColumnIterator> input(
ctx->iterator_factory(field.column_index, ctx->reader));
out->reset(new LeafReader(ctx, arrow_field, std::move(input), field.level_info));
} else if (type_id == ::arrow::Type::LIST) {
} else if (type_id == ::arrow::Type::LIST || type_id == ::arrow::Type::MAP ||
type_id == ::arrow::Type::FIXED_SIZE_LIST ||
type_id == ::arrow::Type::LARGE_LIST) {
auto list_field = arrow_field;
auto child = &field.children[0];
std::unique_ptr<ColumnReaderImpl> child_reader;
RETURN_NOT_OK(GetReader(*child, ctx, &child_reader));
if (child_reader == nullptr) {
*out = nullptr;
return Status::OK();
}
out->reset(new ListReader<int32_t>(ctx, arrow_field, field.level_info,
std::move(child_reader)));
if (type_id == ::arrow::Type::LIST ||
type_id == ::arrow::Type::MAP) { // Map can be reconstructed as list of structs.
if (type_id == ::arrow::Type::MAP &&
child_reader->field()->type()->num_fields() != 2) {
// This case applies if either key or value is filtered.
list_field = list_field->WithType(::arrow::list(child_reader->field()));
}
out->reset(new ListReader<int32_t>(ctx, list_field, field.level_info,
std::move(child_reader)));
} else if (type_id == ::arrow::Type::LARGE_LIST) {
out->reset(new ListReader<int64_t>(ctx, list_field, field.level_info,
std::move(child_reader)));

} else if (type_id == ::arrow::Type::FIXED_SIZE_LIST) {
out->reset(new FixedSizeListReader(ctx, list_field, field.level_info,
std::move(child_reader)));
} else {
return Status::UnknownError("Unknown list type: ", field.field->ToString());
}
} else if (type_id == ::arrow::Type::STRUCT) {
std::vector<std::shared_ptr<Field>> child_fields;
std::vector<std::unique_ptr<ColumnReaderImpl>> child_readers;
Expand Down
Loading