Skip to content

Commit

Permalink
PARQUET-2249: Add nan_count to handle NaNs in Statistics
Browse files Browse the repository at this point in the history
This commit proposes an improvement for handling of NaN values in FLOAT
and DOUBLE type columns. The goal is to allow reading engines,
regardless of how they order NaN w.r.t. other values, to efficiently use
statistics for scan pruning while NaN values are present, which
currently is impossible in most cases. This is to be accomplished in a
fully backward compatible manner, so that existing readers and writers
do not need to be updated immediatly but can migrate over time to make
use of the improved semantics.

There was already [work on improving the handling of float and double
columns](apache#185) which laid
good ground work for backward compatible improvements, but it wasn't
sufficient to fix all the problems with NaN values, which are laid out
hereinafter.

Currently, the way NaN values are to be handled in statistics inhibits
most scan pruning once NaN values are present in DOUBLE or FLOAT columns.
Concretely the following problems exist:

As NaN values are not to be incorporated in min/max bounds, a reader
cannot know whether NaN values are present. This might seem to be not
too problematic, as most queries will not filter for NaNs. However,
NaN is ordered in most database systems. For example,
Postgres, DB2, and Oracle treat NaN as greater than any other value,
while MSSQL and MySQL treat it as less than any other value. An
overview over what different systems are doing can be found
[here](apache/arrow-rs#264 (comment)).
The gist of it is that different systems with different semantics exist
w.r.t. NaNs and most of the systems do order NaNs; either less than or
greater than all other values.

For example, if the semantics of the reading query engine mandate that
NaN is to be treated greater than all other values, the predicate
`x > 1.0` *should* include NaN values. If a page has `max = 0.0` now, the
engine would *not* be able to skip the page, as the page might contain
NaNs which would need to be included in the query result.

Likewise, the predicate `x < 1.0` should include NaN if NaN is treated
to be less than all other values by the reading engine. Again, a page
with `min = 2.0` couldn't be skipped in this case by the reader.

Thus, even if a user doesn't query for NaN explicitly, they might use
other predictes that need to filter or retain NaNs in the semantics of
the reading engine, so the fact that we currently can't know whether a
page or row group contains NaN is a bigger problem than it might seem on
first sight.

Currently, any predicate that needs to retain NaNs cannot use min and
max bounds in Parquet and therefore cannot be used for scan pruning at
all. Conversely, it would be nice if Parquet would enable scan pruning
in these cases, regardless of whether the reader and writer agree upon
whether NaN is smaller, greater, or incomparible to all other values.

Note that the problem exist especially if the Parquet file *doesn't*
include any NaNs, so this is not only a problem in the case where NaNs
are present; it is a problem for the way more common case of NaNs not
being present.

There is currently no well-defined way to write a spec-conforming
ColumnIndex once a page has only NaN (and possibly null) values.
NaN values should not be
included in min/max bounds, but if a page contains only NaN values, then
there is no other value to put into the min/max bounds. However, bounds
in a ColumnIndex are non-optional, so we *have to* put something in here.
The spec does not describe what engines should do in this case.
Parquet-mr takes the safe route and does not write a column index once
NaNs are present. But this is a huge pessimization, as a single page
containing NaNs will prevent the emission for a column index for that
column chunk, so even pages in that chunk that don't contain NaNs will
not be indexed.

It would be nice if there was a defined way of writing the `ColumnIndex`
when NaNs (and especially only-NaN pages) are present.

The `Statistics` objects stored in page headers and in the file footer
have a similar, albeit smaller problem: `min_value` and `max_value` are
optional here, so it is easier to not include NaNs in the min/max in
case of an only-NaN page or column chunk: Simply omit these optional
fields. However, this brings a semantic ambiguity with it, as it is now
unclear whether the min/max value wasn't written because there were only
NaNs, or simply because the writing engine did decide to omit them for
whatever other reason, which is allowed by the spec as the field is
optional.

Consequently, a reader cannot know whether missing `min_value` and
`max_value` means "only NaNs, you can skip this page if you are looking
for only non-NaN values" or "no stats written, you have to read this
page as it is undefined what values it contains".

It would be nice if we could handle NaNs in a way that would allow scan
pruning for these only-NaN pages.

The proposed solution for handling NaNs in statistics is akin to what
[Apache Iceberg](https://iceberg.apache.org/spec/) does: add an
*optional* `nan_count` field to `Statistics` and an *optional*
`nan_counts` list to `ColumnIndex`. This way, all places where
statistics are being retained can specify whether NaN values are
present. This already solves the first problem, as now queries wanting
to retain NaNs can check whether the count is > 0 to see whether a page
or column chunk contains NaNs.

Adding `nan_count`/`nan_counts` fields does not solve the problem of
only-NaN pages, yet. But since we have a new optional field in both the
`Statistics` object and the `ColumnIndex` object, we can tie a stricter
semantics to the existence of this field. I.e., we can mandate that writers
who write this optional field have to treat NaNs in a specific way.

We basically have two options for treating only-NaN pages or column
chunks:
* Order the writer to write NaN as min/max in this case.
* Order the writer to write nothing, i.e.,
    * omit the `min_value` / `max_value` in `Statistics`
    * write byte[0] in the min_values/max_values entry of the
      `ColumnIndex`

I propose to go with the first option of writing NaN as min/max in case
of only-Nan pages & column chunks. A section depicting the
decision process for this follows below.

Thus, to solve the problem of only-NaN pages, the comments in the spec
are extended to mandate the following behavior:

* Once a writer writes the `nan_count`/`nan_counts` fields, they have
  to: a) never write NaN into min/max if there are non-NaN non-Null
  values and b) always write min=max=NaN if the only non-null values in
  a page are NaN
* A reader observing that `nan_count`/`nan_counts` field was written can
  then rely on that if min or max are Nan, then both have to be NaN and
  this means that the only non-NULL values are NaN.

Here are the cons of each approach and how to mitigate them:

CONs for writing NaN in this case:
* Writing NaN breaks with the "don't write NaN into min and max bounds"
  rule.
    * However, one could argue that breaking the rule in this edge case
      is okay, as since if NaN is the only value in a page, then it
      doesn't matter where to sort NaN w.r.t. other values, as there are
      no other values. It is the only value in the page, so it is the
      min and max of the page
    * Breaking this rule has no consequences on existing readers, as they
      should ignore NaN anyway, i.e., treat it as if it wasn't written,
      so legacy readers should treat both cases the same anyway.
* There might be existing writers that have written NaN for min & max
  for pages that do not only contain NaN but also other values, so a
  reader couldn't rely on min=max=NaN to mean that the only non-null
  value is NaN
    * However, as specified, we can enforce a stricter semantics once
      the `nan_count` field is written: We could define that once a
      writing engine writes this field, it has to a) never write NaN
      into min/max if there are non-NaN non-Null values and b) always
      write min=max=NaN if the only non-null values in a page are NaN.
      Then, readers could rely on the semantics once they observe that
      the `nan_count` field was written.
* NaNs take more space than not writing the field or writing byte[0] in
  the column index. This space overhead should usually be negligible.

In conclusion, there is no big con for writing NaN. It can be
implemented in a fully backward compatible way that still allows future
writers and readers to apply a more strict semantics.

CONs for writing nothing in this case:
* Writing byte[0] to a ColumnIndex might break older readers who expect
  the `min_values`/`max_values` field to be a value with correct length
  unless `null_pages` is true for the entry.
  * Although readers should be lenient enough and handle wrongly sized
    min/max values gracefully by ignoring them we cannot be sure this is
    the case for any reader. Thus, we might legacy spec-conforming readers
    to reject the new Parquet file, which is bad.
* Omit the `min_value` / `max_value` in `Statistics` is suboptimal, as
  it first looks as if the writing engine has decided to not write them
  for whatever reason. In this case, the page could never be pruned by a
  reader, as the reader couldn't know which values are in there. Yes, we
  could define that a writer may not omit min/max if they write
  `null_count` and must only omit them if a page has only NaNs, but this
  seems to be quite fishy semancially.

In conclusion, the cons for the NaN approach have mitigations and
can be handled in a backward compatible way, while the cons for writing
nothing might be non-backward-compatible. Therefore, I propose to write
NaN as min/max for only-nan pages & column chunks.

The suggested change is fully backward compatible both in the read and
write direction:

Older readers not supporting `nan_count`/`nan_counts` yet can stay as is.
As the fields are optional, readers not supporting them will simply
ignore them.

The spec already today mandates that if a reader sees `NaN` in min or
max fields they should ignore it. They can continue doing so.

No older reader will have regressed performance; any page that an older
reader would have skipped before can still be skipped.

Older writers can continue writing files without
`nan_count`/`nan_counts` and `nans_first`. Even if an old reader sets
min=max=NaN for a page that does contain non-NaN values, readers
supporting this new semantics will not misinterpret these bounds, as the
writer will not write `nan_count`/`nan_counts`, so the new more strict
semantics does not apply when reading.

As `nan_count`/`nan_counts` are local to the
scopes where they apply (column index, page, column chunk), even
stiching together row groups from a writer that didn't write them and a
writer that does write them works. This would result in a file where
some pages / column indexes / column chunks would have them set while
others wouldn't.

This proposal definitly does not require a *major* version bump, as it
is fully backward compatible.

I do not fully understand the versioning policy of parquet, so I don't
know whether this change would require a minor version bump. One could
argue that it is not necessary as the mere existence of the
`nan_count`/`nan_counts` field would be the "feature flag" that would
indicate whether a writer supported this change or not. There wouldn't
be a version check necessary in a reader; they would just need to
check for the existence of the `nan_count`/`nan_counts` fields.

As thrift encodes missing optional fields with zero bytes in the compact
protocol, non-FLOAT/DOUBLE columns will not incur any overhead for the
new optional fields.

We could simply define NaN to be smaller or greater than all other
values and then allow NaN in the respective bound.

This however has many drawbacks:
* NaN is the widest bound possible, so adding NaNs to min and max isn't
  very useful, as it makes pruning for non-NaN values almost impossible
  in the respective direction.
* As mentioned, not all systems agree on whether NaN is larger or
  smaller than all other values. If we decided for one, systems that
  choose the other semantics couldn't filter effectively.
* This contradicts the current spec of not writing NaN to min/max, so it
  would make older readers no longer skip pages they could skip before.

We could add a new ColumnOrder that specifies NaN to be smaller or
greater than all other values, or even supports -NaN and +NaN ordering
them as smaller and larger than all values, respectively. For example,
Iceberg mandates the following sort order:

-NaN < -Infinity < -value < -0 < 0 < value < Infinity < NaN

Once we define such an order, we could again allow NaN (and potentially
-NaN) in bounds again.

This however has the following drawbacks:
* As with the previous alternative: NaN is the widest bound possible, so
  adding NaNs to min and max isn't very useful, as it makes pruning for
  non-NaN values almost impossible in the respective direction. If we
  even allow -NaN and +NaN, a page containing both would have no
  meaningful min and max and wouldn't allow any pruning at all.
* Most systems don't support -NaN, as mathematically speaking, it is
  nonsense. The only reason why it exists is that the physical
  reprsentation of floats has a sign bit that also exists for NaN
  representations.
* The fact that NaNs being so unuseful for min/max bounds is displayed
  by the fact that even though Iceberg has such a well defined sort
  order,
  they still do what is proposed in this proposal and *do not* include
  -NaN/NaN into min/max bounds and rather track them through nan_counts.

All in all, any alternative putting NaN into min/max bounds (except for
only-NaN-pages) suffers from the problem that NaN bounds are too wide
and therefore not useful for pruning.

The column index does allow `byte[0]` values already, in case a page
contains only nulls. We could enable the same for only-NaN pages by not
storing only the `nan_counts`, but also the `value_counts` of a page.
Then, one could check whether a page in the column index contains only
NaNs by checking  `nan_count + nulls_count = value_count`.
Hoewever, this would mean yet another list in the column
index, making the column index bigger and more expensive to deserialize.
And while the `nan_counts` list only exists for FLOAT/DOUBLE columns,
the `value_counts` list would exist for all columns and therefore take
up considerably more space.
Also, this would not be backward compatible, as an older reader wouldn't
know of the new lists, so it would see a `byte[0]` and would need to treat
it as invalid.

All in all, the extra list doesn't seem to add enough value for its cost
and reduced backward compatibility.

As long as we don't do anything, columns containing NaNs will almost be
useless for scan pruning. The problems outlined will persist, making double
columns almost unprunable for some predicates. That is not satisfactory.

Even with this improvement which fixes the semantics of NaN values in statistics,
NaN values are still a problem in some other places as there is still not a
defined order for them, so the `boundary_order` in a column index and
the `SortingColumn` would still have undefined placement for `NaN`.

This mainly wasn't tackled for two reasons:
* It is an orthogonal issue. This improvement is about enabling NaNs in statistics,
  so after this change all statistics can handle NaN in a well-defined way.
  Sort odering of columns to leverage `boundary_order` or `SortingColumn` needs
  to be solved in a different way anyway, as the mere information about whether
  (only or some) NaNs are present isn't enough here but it needs to be defined
  whether they come before or after all values.
  * Even though we could fix both statistics and sort order by just defining NaN
    to be smaller or greater than other values, doing so for statistics is *not*
    a good idea, as having NaN in bounds makes too wide bounds that aren't helpful
    for filtering.
  * If sort ordering will be fixed by a different commit one day, the design of this
    commit shouldn't have a (negative) influence on that future design, as NaN counts
    and not including NaNs into bounds is a good thing to do anyway.
* While fixing statistics with NaN counts is pretty uncontested w.r.t. design
  alternatives (see the respective section for a discussion why), the design to be
  chosen for sort orders isn't that clear:
  * We could define a new `ColumnOrder` with well defined NaN ordering. This
    would be the cleanest fix, but also a "very big gun", as this would be the first
    non-default column order in existence.
  * We could define a `nans_first` fields which tells whether NaNs are to be sorted
    before or after other values, akin to the already existing field `nulls_first`.
    This would be a more micro-invasive change, but it would be less clean IMHO, as
    there is a tool for defining column ordering--the `ColumnOrder`--and not using
    that tool but working around it feels hacky.

Thus, sort ordering of NaNs wasn't tackled in this commit. They can be tackled by a
follow-up change if necessary.
  • Loading branch information
JFinis committed Mar 22, 2023
1 parent 230711f commit 63c7b5d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,25 @@ following rules:
[Thrift definition](src/main/thrift/parquet.thrift) in the
`ColumnOrder` union. They are summarized here but the Thrift definition
is considered authoritative:
* NaNs should not be written to min or max statistics fields.
* If the computed max value is zero (whether negative or positive),
`+0.0` should be written into the max statistics field.
* If the computed min value is zero (whether negative or positive),
`-0.0` should be written into the min statistics field.

For backwards compatibility when reading files:
* If the min is a NaN, it should be ignored.
* If the max is a NaN, it should be ignored.
* If the min is +0, the row group may contain -0 values as well.
* If the max is -0, the row group may contain +0 values as well.
* When looking for NaN values, min and max should be ignored.
* The following compatibility rules should be applied when reading statistics:
* If the nan_count field is set to > 0 and both min and max are
NaN, a reader can rely on that all non-NULL values are NaN
* Otherwise, if the min or the max is a NaN, it should be ignored.
* When looking for NaN values, min and max should be ignored;
if the nan_count field is set, it should be used to check whether
NaNs are present.
* If the min is +0, the row group may contain -0 values as well.
* If the max is -0, the row group may contain +0 values as well.
* When writing statistics the following rules should be followed:
* The nan_count fields should always be set for FLOAT and DOUBLE columns.
* NaNs should not be written to min or max statistics fields except
when all non-NULL values are NaN, in which case min and max should
both be written as NaN. If the nan_count field is set, this semantics
is mandated and readers may rely on it.
* If the computed max value is zero (whether negative or positive),
`+0.0` should be written into the max statistics field.
* If the computed min value is zero (whether negative or positive),
`-0.0` should be written into the min statistics field.

* BYTE_ARRAY and FIXED_LEN_BYTE_ARRAY - Lexicographic unsigned byte-wise
comparison.
Expand Down
30 changes: 23 additions & 7 deletions src/main/thrift/parquet.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ struct Statistics {
*/
1: optional binary max;
2: optional binary min;
/** count of null value in the column */
/** count of null values in the column */
3: optional i64 null_count;
/** count of distinct values occurring */
4: optional i64 distinct_count;
Expand All @@ -223,6 +223,8 @@ struct Statistics {
*/
5: optional binary max_value;
6: optional binary min_value;
/** count of NaN values in the column; only present if type is FLOAT or DOUBLE */
7: optional i64 nan_count;
}

/** Empty structs to use as logical type annotations */
Expand Down Expand Up @@ -886,16 +888,23 @@ union ColumnOrder {
* FIXED_LEN_BYTE_ARRAY - unsigned byte-wise comparison
*
* (*) Because the sorting order is not specified properly for floating
* point values (relations vs. total ordering) the following
* compatibility rules should be applied when reading statistics:
* - If the min is a NaN, it should be ignored.
* - If the max is a NaN, it should be ignored.
* point values (relations vs. total ordering), the following compatibility
* rules should be applied when reading statistics:
* - If the nan_count field is set to > 0 and both min and max are
* NaN, a reader can rely on that all non-NULL values are NaN
* - Otherwise, if the min or the max is a NaN, it should be ignored.
* - When looking for NaN values, min and max should be ignored;
* if the nan_count field is set, it can be used to check whether
* NaNs are present.
* - If the min is +0, the row group may contain -0 values as well.
* - If the max is -0, the row group may contain +0 values as well.
* - When looking for NaN values, min and max should be ignored.
*
* When writing statistics the following rules should be followed:
* - NaNs should not be written to min or max statistics fields.
* - The nan_count fields should always be set for FLOAT and DOUBLE columns.
* - NaNs should not be written to min or max statistics fields except
* when all non-NULL values are NaN, in which case min and max should
* both be written as NaN. If the nan_count field is set, this semantics
* is mandated and readers may rely on it.
* - If the computed max value is zero (whether negative or positive),
* `+0.0` should be written into the max statistics field.
* - If the computed min value is zero (whether negative or positive),
Expand Down Expand Up @@ -952,6 +961,9 @@ struct ColumnIndex {
* Such more compact values must still be valid values within the column's
* logical type. Readers must make sure that list entries are populated before
* using them by inspecting null_pages.
* For columns of type FLOAT and DOUBLE, NaN values are not to be included
* in these bounds unless all non-null values in a page are NaN, in which
* case min and max are to be set to NaN.
*/
2: required list<binary> min_values
3: required list<binary> max_values
Expand All @@ -966,6 +978,10 @@ struct ColumnIndex {

/** A list containing the number of null values for each page **/
5: optional list<i64> null_counts

/** A list containing the number of NaN values for each page. Only present
* for columns of type FLOAT and DOUBLE. **/
6: optional list<i64> nan_counts
}

struct AesGcmV1 {
Expand Down

0 comments on commit 63c7b5d

Please sign in to comment.