Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate boolean encoding in vectors #7

Closed
jhorstmann opened this issue May 30, 2024 · 4 comments
Closed

Investigate boolean encoding in vectors #7

jhorstmann opened this issue May 30, 2024 · 4 comments

Comments

@jhorstmann
Copy link
Owner

The spec says

Field values are encoded directly in the field header. Element values of type bool are sent as an int8; true as 1 and false as 0.

and

The following element-types are used (see note below):

BOOL, encoded as 2

But it seems the ColumnIndex::null_pages field in alltypes_tiny_pages.parquet, written by parquet-mr version 1.12.0-SNAPSHOT (build 6901a2040848c6b37fa61f4b0a76246445f396db) encodes the element type as 1 and contains elements with value 2.

We probably need to be lenient and support both, decoding element values as byte_value == 1.

@jhorstmann
Copy link
Owner Author

The java implementation seems to clearly contradict the spec here

  public void writeBool(boolean b) throws TException {
    if (booleanField_ != null) {
      // we haven't written the field header yet
      writeFieldBeginInternal(booleanField_, b ? Types.BOOLEAN_TRUE : Types.BOOLEAN_FALSE);
      booleanField_ = null;
    } else {
      // we're not part of a field, so just write the value.
      writeByteDirect(b ? Types.BOOLEAN_TRUE : Types.BOOLEAN_FALSE);
    }
  }

And here, called via getCompactType and writeCollectionBegin.

  private static final byte[] ttypeToCompactType = new byte[18];

  static {
    ttypeToCompactType[TType.STOP] = TType.STOP;
    ttypeToCompactType[TType.BOOL] = Types.BOOLEAN_TRUE;
    ...
  }

@jhorstmann
Copy link
Owner Author

jhorstmann commented Jan 31, 2025

The documentation was updated in apache/thrift@2c29c56

Element values of type bool are sent as an int8; true as 1 and false as 2.

Similarly for collections:

  • BOOL, encoded as 1 or '2' (see note 2 below)
    ...
  1. For historical and compatibility reasons, a reader should be capable to deal with both cases. The only valid value in the original spec was '2', but due to an widespread implementation bug the defacto
    standard across large parts of the library became '1' instead. As a result, both values are now allowed.

@jhorstmann
Copy link
Owner Author

The write_thrift method for bool needs to be adjusted to match the updated documentation.

@jhorstmann
Copy link
Owner Author

Fixed in commit c54a4d7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant