Skip to content

Commit

Permalink
Handle empty string correctly in Parquet statistics (#14257)
Browse files Browse the repository at this point in the history
An empty string should be a valid minimum value for a string column, but the current parquet writer considers an empty string to have no value when writing the column chunk statistics. This PR changes all fields in the Statistics struct to be `thrust::optional` to help distinguish between a valid empty string and no value.

Authors:
  - Ed Seidl (https://github.com/etseidl)

Approvers:
  - Vukasin Milovanovic (https://github.com/vuule)
  - Nghia Truong (https://github.com/ttnghia)

URL: #14257
  • Loading branch information
etseidl authored Oct 10, 2023
1 parent 0ed7725 commit aa8b0f8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 40 deletions.
15 changes: 9 additions & 6 deletions cpp/src/io/parquet/compact_protocol_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,15 @@ bool CompactProtocolReader::read(ColumnIndex* c)

bool CompactProtocolReader::read(Statistics* s)
{
auto op = std::make_tuple(parquet_field_binary(1, s->max),
parquet_field_binary(2, s->min),
parquet_field_int64(3, s->null_count),
parquet_field_int64(4, s->distinct_count),
parquet_field_binary(5, s->max_value),
parquet_field_binary(6, s->min_value));
using optional_binary = parquet_field_optional<std::vector<uint8_t>, parquet_field_binary>;
using optional_int64 = parquet_field_optional<int64_t, parquet_field_int64>;

auto op = std::make_tuple(optional_binary(1, s->max),
optional_binary(2, s->min),
optional_int64(3, s->null_count),
optional_int64(4, s->distinct_count),
optional_binary(5, s->max_value),
optional_binary(6, s->min_value));
return function_builder(this, op);
}

Expand Down
12 changes: 6 additions & 6 deletions cpp/src/io/parquet/compact_protocol_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ size_t CompactProtocolWriter::write(ColumnChunkMetaData const& s)
size_t CompactProtocolWriter::write(Statistics const& s)
{
CompactProtocolFieldWriter c(*this);
if (not s.max.empty()) { c.field_binary(1, s.max); }
if (not s.min.empty()) { c.field_binary(2, s.min); }
if (s.null_count != -1) { c.field_int(3, s.null_count); }
if (s.distinct_count != -1) { c.field_int(4, s.distinct_count); }
if (not s.max_value.empty()) { c.field_binary(5, s.max_value); }
if (not s.min_value.empty()) { c.field_binary(6, s.min_value); }
if (s.max.has_value()) { c.field_binary(1, s.max.value()); }
if (s.min.has_value()) { c.field_binary(2, s.min.value()); }
if (s.null_count.has_value()) { c.field_int(3, s.null_count.value()); }
if (s.distinct_count.has_value()) { c.field_int(4, s.distinct_count.value()); }
if (s.max_value.has_value()) { c.field_binary(5, s.max_value.value()); }
if (s.min_value.has_value()) { c.field_binary(6, s.min_value.value()); }
return c.value();
}

Expand Down
18 changes: 12 additions & 6 deletions cpp/src/io/parquet/parquet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,18 @@ struct SchemaElement {
* @brief Thrift-derived struct describing column chunk statistics
*/
struct Statistics {
std::vector<uint8_t> max; // deprecated max value in signed comparison order
std::vector<uint8_t> min; // deprecated min value in signed comparison order
int64_t null_count = -1; // count of null values in the column
int64_t distinct_count = -1; // count of distinct values occurring
std::vector<uint8_t> max_value; // max value for column determined by ColumnOrder
std::vector<uint8_t> min_value; // min value for column determined by ColumnOrder
// deprecated max value in signed comparison order
thrust::optional<std::vector<uint8_t>> max;
// deprecated min value in signed comparison order
thrust::optional<std::vector<uint8_t>> min;
// count of null values in the column
thrust::optional<int64_t> null_count;
// count of distinct values occurring
thrust::optional<int64_t> distinct_count;
// max value for column determined by ColumnOrder
thrust::optional<std::vector<uint8_t>> max_value;
// min value for column determined by ColumnOrder
thrust::optional<std::vector<uint8_t>> min_value;
};

/**
Expand Down
14 changes: 8 additions & 6 deletions cpp/src/io/parquet/predicate_pushdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ struct stats_caster {
{
}

void set_index(size_type index, std::vector<uint8_t> const& binary_value, Type const type)
void set_index(size_type index,
thrust::optional<std::vector<uint8_t>> const& binary_value,
Type const type)
{
if (!binary_value.empty()) {
val[index] = convert<T>(binary_value.data(), binary_value.size(), type);
if (binary_value.has_value()) {
val[index] = convert<T>(binary_value.value().data(), binary_value.value().size(), type);
}
if (binary_value.empty()) {
if (not binary_value.has_value()) {
clear_bit_unsafe(null_mask.data(), index);
null_count++;
}
Expand Down Expand Up @@ -210,10 +212,10 @@ struct stats_caster {
auto const& row_group = per_file_metadata[src_idx].row_groups[rg_idx];
auto const& colchunk = row_group.columns[col_idx];
// To support deprecated min, max fields.
auto const& min_value = colchunk.meta_data.statistics.min_value.size() > 0
auto const& min_value = colchunk.meta_data.statistics.min_value.has_value()
? colchunk.meta_data.statistics.min_value
: colchunk.meta_data.statistics.min;
auto const& max_value = colchunk.meta_data.statistics.max_value.size() > 0
auto const& max_value = colchunk.meta_data.statistics.max_value.has_value()
? colchunk.meta_data.statistics.max_value
: colchunk.meta_data.statistics.max;
// translate binary data to Type then to <T>
Expand Down
85 changes: 69 additions & 16 deletions cpp/tests/io/parquet_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4161,8 +4161,10 @@ TEST_P(ParquetV2Test, LargeColumnIndex)
// check trunc(page.min) <= stats.min && trun(page.max) >= stats.max
auto const ptype = fmd.schema[c + 1].type;
auto const ctype = fmd.schema[c + 1].converted_type;
EXPECT_TRUE(compare_binary(ci.min_values[0], stats.min_value, ptype, ctype) <= 0);
EXPECT_TRUE(compare_binary(ci.max_values[0], stats.max_value, ptype, ctype) >= 0);
ASSERT_TRUE(stats.min_value.has_value());
ASSERT_TRUE(stats.max_value.has_value());
EXPECT_TRUE(compare_binary(ci.min_values[0], stats.min_value.value(), ptype, ctype) <= 0);
EXPECT_TRUE(compare_binary(ci.max_values[0], stats.max_value.value(), ptype, ctype) >= 0);
}
}
}
Expand Down Expand Up @@ -4242,6 +4244,9 @@ TEST_P(ParquetV2Test, CheckColumnOffsetIndex)
auto const ci = read_column_index(source, chunk);
auto const stats = get_statistics(chunk);

ASSERT_TRUE(stats.min_value.has_value());
ASSERT_TRUE(stats.max_value.has_value());

// schema indexing starts at 1
auto const ptype = fmd.schema[c + 1].type;
auto const ctype = fmd.schema[c + 1].converted_type;
Expand All @@ -4250,10 +4255,10 @@ TEST_P(ParquetV2Test, CheckColumnOffsetIndex)
EXPECT_FALSE(ci.null_pages[p]);
// null_counts should always be 0
EXPECT_EQ(ci.null_counts[p], 0);
EXPECT_TRUE(compare_binary(stats.min_value, ci.min_values[p], ptype, ctype) <= 0);
EXPECT_TRUE(compare_binary(stats.min_value.value(), ci.min_values[p], ptype, ctype) <= 0);
}
for (size_t p = 0; p < ci.max_values.size(); p++)
EXPECT_TRUE(compare_binary(stats.max_value, ci.max_values[p], ptype, ctype) >= 0);
EXPECT_TRUE(compare_binary(stats.max_value.value(), ci.max_values[p], ptype, ctype) >= 0);
}
}
}
Expand Down Expand Up @@ -4344,7 +4349,10 @@ TEST_P(ParquetV2Test, CheckColumnOffsetIndexNulls)
auto const stats = get_statistics(chunk);

// should be half nulls, except no nulls in column 0
EXPECT_EQ(stats.null_count, c == 0 ? 0 : num_rows / 2);
ASSERT_TRUE(stats.min_value.has_value());
ASSERT_TRUE(stats.max_value.has_value());
ASSERT_TRUE(stats.null_count.has_value());
EXPECT_EQ(stats.null_count.value(), c == 0 ? 0 : num_rows / 2);

// schema indexing starts at 1
auto const ptype = fmd.schema[c + 1].type;
Expand All @@ -4356,10 +4364,10 @@ TEST_P(ParquetV2Test, CheckColumnOffsetIndexNulls)
} else {
EXPECT_EQ(ci.null_counts[p], 0);
}
EXPECT_TRUE(compare_binary(stats.min_value, ci.min_values[p], ptype, ctype) <= 0);
EXPECT_TRUE(compare_binary(stats.min_value.value(), ci.min_values[p], ptype, ctype) <= 0);
}
for (size_t p = 0; p < ci.max_values.size(); p++) {
EXPECT_TRUE(compare_binary(stats.max_value, ci.max_values[p], ptype, ctype) >= 0);
EXPECT_TRUE(compare_binary(stats.max_value.value(), ci.max_values[p], ptype, ctype) >= 0);
}
}
}
Expand Down Expand Up @@ -4436,7 +4444,12 @@ TEST_P(ParquetV2Test, CheckColumnOffsetIndexNullColumn)
auto const stats = get_statistics(chunk);

// there should be no nulls except column 1 which is all nulls
EXPECT_EQ(stats.null_count, c == 1 ? num_rows : 0);
if (c != 1) {
ASSERT_TRUE(stats.min_value.has_value());
ASSERT_TRUE(stats.max_value.has_value());
}
ASSERT_TRUE(stats.null_count.has_value());
EXPECT_EQ(stats.null_count.value(), c == 1 ? num_rows : 0);

// schema indexing starts at 1
auto const ptype = fmd.schema[c + 1].type;
Expand All @@ -4449,12 +4462,12 @@ TEST_P(ParquetV2Test, CheckColumnOffsetIndexNullColumn)
}
if (not ci.null_pages[p]) {
EXPECT_EQ(ci.null_counts[p], 0);
EXPECT_TRUE(compare_binary(stats.min_value, ci.min_values[p], ptype, ctype) <= 0);
EXPECT_TRUE(compare_binary(stats.min_value.value(), ci.min_values[p], ptype, ctype) <= 0);
}
}
for (size_t p = 0; p < ci.max_values.size(); p++) {
if (not ci.null_pages[p]) {
EXPECT_TRUE(compare_binary(stats.max_value, ci.max_values[p], ptype, ctype) >= 0);
EXPECT_TRUE(compare_binary(stats.max_value.value(), ci.max_values[p], ptype, ctype) >= 0);
}
}
}
Expand Down Expand Up @@ -4533,13 +4546,16 @@ TEST_P(ParquetV2Test, CheckColumnOffsetIndexStruct)
auto const ci = read_column_index(source, chunk);
auto const stats = get_statistics(chunk);

ASSERT_TRUE(stats.min_value.has_value());
ASSERT_TRUE(stats.max_value.has_value());

auto const ptype = fmd.schema[colidx].type;
auto const ctype = fmd.schema[colidx].converted_type;
for (size_t p = 0; p < ci.min_values.size(); p++) {
EXPECT_TRUE(compare_binary(stats.min_value, ci.min_values[p], ptype, ctype) <= 0);
EXPECT_TRUE(compare_binary(stats.min_value.value(), ci.min_values[p], ptype, ctype) <= 0);
}
for (size_t p = 0; p < ci.max_values.size(); p++) {
EXPECT_TRUE(compare_binary(stats.max_value, ci.max_values[p], ptype, ctype) >= 0);
EXPECT_TRUE(compare_binary(stats.max_value.value(), ci.max_values[p], ptype, ctype) >= 0);
}
}
}
Expand Down Expand Up @@ -4829,11 +4845,14 @@ TEST_F(ParquetWriterTest, CheckColumnIndexTruncation)
auto const ci = read_column_index(source, chunk);
auto const stats = get_statistics(chunk);

ASSERT_TRUE(stats.min_value.has_value());
ASSERT_TRUE(stats.max_value.has_value());

// check trunc(page.min) <= stats.min && trun(page.max) >= stats.max
auto const ptype = fmd.schema[c + 1].type;
auto const ctype = fmd.schema[c + 1].converted_type;
EXPECT_TRUE(compare_binary(ci.min_values[0], stats.min_value, ptype, ctype) <= 0);
EXPECT_TRUE(compare_binary(ci.max_values[0], stats.max_value, ptype, ctype) >= 0);
EXPECT_TRUE(compare_binary(ci.min_values[0], stats.min_value.value(), ptype, ctype) <= 0);
EXPECT_TRUE(compare_binary(ci.max_values[0], stats.max_value.value(), ptype, ctype) >= 0);

// check that truncated values == expected
EXPECT_EQ(memcmp(ci.min_values[0].data(), truncated_min[c], ci.min_values[0].size()), 0);
Expand Down Expand Up @@ -4890,8 +4909,10 @@ TEST_F(ParquetWriterTest, BinaryColumnIndexTruncation)
// check trunc(page.min) <= stats.min && trun(page.max) >= stats.max
auto const ptype = fmd.schema[c + 1].type;
auto const ctype = fmd.schema[c + 1].converted_type;
EXPECT_TRUE(compare_binary(ci.min_values[0], stats.min_value, ptype, ctype) <= 0);
EXPECT_TRUE(compare_binary(ci.max_values[0], stats.max_value, ptype, ctype) >= 0);
ASSERT_TRUE(stats.min_value.has_value());
ASSERT_TRUE(stats.max_value.has_value());
EXPECT_TRUE(compare_binary(ci.min_values[0], stats.min_value.value(), ptype, ctype) <= 0);
EXPECT_TRUE(compare_binary(ci.max_values[0], stats.max_value.value(), ptype, ctype) >= 0);

// check that truncated values == expected
EXPECT_EQ(ci.min_values[0], truncated_min[c]);
Expand Down Expand Up @@ -6737,6 +6758,38 @@ TEST_P(ParquetV2Test, CheckEncodings)
}
}

TEST_F(ParquetWriterTest, EmptyMinStringStatistics)
{
char const* const min_val = "";
char const* const max_val = "zzz";
std::vector<char const*> strings{min_val, max_val, "pining", "for", "the", "fjords"};

column_wrapper<cudf::string_view> string_col{strings.begin(), strings.end()};
auto const output = table_view{{string_col}};
auto const filepath = temp_env->get_temp_filepath("EmptyMinStringStatistics.parquet");
cudf::io::parquet_writer_options out_opts =
cudf::io::parquet_writer_options::builder(cudf::io::sink_info{filepath}, output);
cudf::io::write_parquet(out_opts);

auto const source = cudf::io::datasource::create(filepath);
cudf::io::parquet::detail::FileMetaData fmd;
read_footer(source, &fmd);

ASSERT_TRUE(fmd.row_groups.size() > 0);
ASSERT_TRUE(fmd.row_groups[0].columns.size() > 0);
auto const& chunk = fmd.row_groups[0].columns[0];
auto const stats = get_statistics(chunk);

ASSERT_TRUE(stats.min_value.has_value());
ASSERT_TRUE(stats.max_value.has_value());
auto const min_value = std::string{reinterpret_cast<char const*>(stats.min_value.value().data()),
stats.min_value.value().size()};
auto const max_value = std::string{reinterpret_cast<char const*>(stats.max_value.value().data()),
stats.max_value.value().size()};
EXPECT_EQ(min_value, std::string(min_val));
EXPECT_EQ(max_value, std::string(max_val));
}

TEST_F(ParquetReaderTest, RepeatedNoAnnotations)
{
constexpr unsigned char repeated_bytes[] = {
Expand Down

0 comments on commit aa8b0f8

Please sign in to comment.