Fokko commented on code in PR #7831: URL: https://github.com/apache/iceberg/pull/7831#discussion_r1229173359
########## python/pyiceberg/utils/file_stats.py: ########## @@ -0,0 +1,164 @@ +# 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. + +from pyiceberg.manifest import DataFile, FileFormat +import pyarrow.parquet as pq +import pyarrow.compute as pc +import pyarrow as pa +import struct +import datetime + +BOUND_TRUNCATED_LENGHT = 16 + +# Serialization rules: https://iceberg.apache.org/spec/#binary-single-value-serialization +# +# Type Binary serialization +# boolean 0x00 for false, non-zero byte for true +# int Stored as 4-byte little-endian +# long Stored as 8-byte little-endian +# float Stored as 4-byte little-endian +# double Stored as 8-byte little-endian +# date Stores days from the 1970-01-01 in an 4-byte little-endian int +# time Stores microseconds from midnight in an 8-byte little-endian long +# timestamp without zone Stores microseconds from 1970-01-01 00:00:00.000000 in an 8-byte little-endian long +# timestamp with zone Stores microseconds from 1970-01-01 00:00:00.000000 UTC in an 8-byte little-endian long +# string UTF-8 bytes (without length) +# uuid 16-byte big-endian value, see example in Appendix B +# fixed(L) Binary value +# binary Binary value (without length) +# +def serialize_to_binary(scalar: pa.Scalar) -> bytes: Review Comment: We could reuse existing logic by first converting the PyArrow DataType to an Iceberg type using `pyarrow_to_schema(datatype)`, and then reuse `to_bytes` in `conversion.py`. ########## python/pyiceberg/utils/file_stats.py: ########## @@ -0,0 +1,164 @@ +# 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. + +from pyiceberg.manifest import DataFile, FileFormat +import pyarrow.parquet as pq +import pyarrow.compute as pc +import pyarrow as pa +import struct +import datetime + +BOUND_TRUNCATED_LENGHT = 16 + +# Serialization rules: https://iceberg.apache.org/spec/#binary-single-value-serialization +# +# Type Binary serialization +# boolean 0x00 for false, non-zero byte for true +# int Stored as 4-byte little-endian +# long Stored as 8-byte little-endian +# float Stored as 4-byte little-endian +# double Stored as 8-byte little-endian +# date Stores days from the 1970-01-01 in an 4-byte little-endian int +# time Stores microseconds from midnight in an 8-byte little-endian long +# timestamp without zone Stores microseconds from 1970-01-01 00:00:00.000000 in an 8-byte little-endian long +# timestamp with zone Stores microseconds from 1970-01-01 00:00:00.000000 UTC in an 8-byte little-endian long +# string UTF-8 bytes (without length) +# uuid 16-byte big-endian value, see example in Appendix B +# fixed(L) Binary value +# binary Binary value (without length) +# +def serialize_to_binary(scalar: pa.Scalar) -> bytes: + value = scalar.as_py() + if isinstance(scalar, pa.BooleanScalar): + return struct.pack('?', value) Review Comment: For performance reasons, it is best to re-use the `struct`'s, see `conversions.py` ########## python/pyiceberg/utils/file_stats.py: ########## @@ -0,0 +1,164 @@ +# 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. + +from pyiceberg.manifest import DataFile, FileFormat +import pyarrow.parquet as pq +import pyarrow.compute as pc +import pyarrow as pa +import struct +import datetime + +BOUND_TRUNCATED_LENGHT = 16 + +# Serialization rules: https://iceberg.apache.org/spec/#binary-single-value-serialization +# +# Type Binary serialization +# boolean 0x00 for false, non-zero byte for true +# int Stored as 4-byte little-endian +# long Stored as 8-byte little-endian +# float Stored as 4-byte little-endian +# double Stored as 8-byte little-endian +# date Stores days from the 1970-01-01 in an 4-byte little-endian int +# time Stores microseconds from midnight in an 8-byte little-endian long +# timestamp without zone Stores microseconds from 1970-01-01 00:00:00.000000 in an 8-byte little-endian long +# timestamp with zone Stores microseconds from 1970-01-01 00:00:00.000000 UTC in an 8-byte little-endian long +# string UTF-8 bytes (without length) +# uuid 16-byte big-endian value, see example in Appendix B +# fixed(L) Binary value +# binary Binary value (without length) +# +def serialize_to_binary(scalar: pa.Scalar) -> bytes: + value = scalar.as_py() + if isinstance(scalar, pa.BooleanScalar): + return struct.pack('?', value) + elif isinstance(scalar, (pa.Int8Scalar, pa.UInt8Scalar)): + return struct.pack('<b', value) + elif isinstance(scalar, (pa.Int16Scalar, pa.UInt16Scalar)): + return struct.pack('<h', value) + elif isinstance(scalar, (pa.Int32Scalar, pa.UInt32Scalar)): + return struct.pack('<i', value) + elif isinstance(scalar, (pa.Int64Scalar, pa.UInt64Scalar)): + return struct.pack('<q', value) + elif isinstance(scalar, pa.FloatScalar): + return struct.pack('<f', value) + elif isinstance(scalar, pa.DoubleScalar): + return struct.pack('<d', value) + elif isinstance(scalar, pa.StringScalar): + return value.encode('utf-8') + elif isinstance(scalar, pa.BinaryScalar): + return value + elif isinstance(scalar, (pa.Date32Scalar, pa.Date64Scalar)): + epoch = datetime.date(1970, 1, 1) + days = (value - epoch).days + return struct.pack('<i', days) + elif isinstance(scalar, (pa.Time32Scalar, pa.Time64Scalar)): + microseconds = int(value.hour * 60 * 60 * 1e6 + + value.minute * 60 * 1e6 + + value.second * 1e6 + + value.microsecond) + return struct.pack('<q', microseconds) + elif isinstance(scalar, pa.TimestampScalar): + epoch = datetime.datetime(1970, 1, 1) + microseconds = int((value - epoch).total_seconds() * 1e6) + return struct.pack('<q', microseconds) + else: + raise TypeError('Unsupported type: {}'.format(type(scalar))) + + +def fill_parquet_file_metadata(df: DataFile, file_object: pa.NativeFile, table: pa.Table = None) -> None: + """ + Computes and fills the following fields of the DataFile object: + + - file_format + - record_count + - file_size_in_bytes + - column_sizes + - value_counts + - null_value_counts + - nan_value_counts + - lower_bounds + - upper_bounds + - split_offsets + + Args: + df (DataFile): A DataFile object representing the Parquet file for which metadata is to be filled. + file_object (pa.NativeFile): A pyarrow NativeFile object pointing to the location where the + Parquet file is stored. + table (pa.Table, optional): If the metadata is computed while writing a pyarrow Table to parquet + the table can be passed to compute the column statistics. If absent the table will be read + from file_object using pyarrow.parquet.read_table. + """ + + parquet_file = pq.ParquetFile(file_object) + metadata = parquet_file.metadata + + column_sizes = {} + value_counts = {} + + for r in range(metadata.num_row_groups): + for c in range(metadata.num_columns): + column_sizes[c+1] = column_sizes.get(c+1, 0) + metadata.row_group(r).column(c).total_compressed_size + value_counts[c+1] = value_counts.get(c+1, 0) + metadata.row_group(r).column(c).num_values + + + # References: + # https://github.com/apache/iceberg/blob/fc381a81a1fdb8f51a0637ca27cd30673bd7aad3/parquet/src/main/java/org/apache/iceberg/parquet/ParquetUtil.java#L232 + # https://github.com/apache/parquet-mr/blob/ac29db4611f86a07cc6877b416aa4b183e09b353/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/metadata/ColumnChunkMetaData.java#L184 + split_offsets = [] + for r in range(metadata.num_row_groups): + data_offset = metadata.row_group(r).column(0).data_page_offset + dictionary_offset = metadata.row_group(r).column(0).dictionary_page_offset + if metadata.row_group(r).column(0).has_dictionary_page and dictionary_offset < data_offset: + split_offsets.append(dictionary_offset) + else: + split_offsets.append(data_offset) + + split_offsets.sort() + + if table is None: + table = pa.parquet.read_table(file_object) + + null_value_counts = {} + nan_value_counts = {} + lower_bounds = {} + upper_bounds = {} + + for c in range(metadata.num_columns): + null_value_counts[c+1] = table.filter(pc.field(c).is_null(nan_is_null=False)).num_rows Review Comment: This combines comment re-using `to_bytes` and fetching the field-id above. Here we also have to index the field IDs. When we get the field from the Iceberg schema, we also know the type. We could invoke `to_bytes` directly with the Iceberg type. ########## python/pyiceberg/utils/file_stats.py: ########## @@ -0,0 +1,164 @@ +# 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. + +from pyiceberg.manifest import DataFile, FileFormat +import pyarrow.parquet as pq +import pyarrow.compute as pc +import pyarrow as pa +import struct +import datetime + +BOUND_TRUNCATED_LENGHT = 16 + +# Serialization rules: https://iceberg.apache.org/spec/#binary-single-value-serialization +# +# Type Binary serialization +# boolean 0x00 for false, non-zero byte for true +# int Stored as 4-byte little-endian +# long Stored as 8-byte little-endian +# float Stored as 4-byte little-endian +# double Stored as 8-byte little-endian +# date Stores days from the 1970-01-01 in an 4-byte little-endian int +# time Stores microseconds from midnight in an 8-byte little-endian long +# timestamp without zone Stores microseconds from 1970-01-01 00:00:00.000000 in an 8-byte little-endian long +# timestamp with zone Stores microseconds from 1970-01-01 00:00:00.000000 UTC in an 8-byte little-endian long +# string UTF-8 bytes (without length) +# uuid 16-byte big-endian value, see example in Appendix B +# fixed(L) Binary value +# binary Binary value (without length) +# +def serialize_to_binary(scalar: pa.Scalar) -> bytes: + value = scalar.as_py() + if isinstance(scalar, pa.BooleanScalar): + return struct.pack('?', value) + elif isinstance(scalar, (pa.Int8Scalar, pa.UInt8Scalar)): + return struct.pack('<b', value) + elif isinstance(scalar, (pa.Int16Scalar, pa.UInt16Scalar)): + return struct.pack('<h', value) + elif isinstance(scalar, (pa.Int32Scalar, pa.UInt32Scalar)): + return struct.pack('<i', value) + elif isinstance(scalar, (pa.Int64Scalar, pa.UInt64Scalar)): + return struct.pack('<q', value) + elif isinstance(scalar, pa.FloatScalar): + return struct.pack('<f', value) + elif isinstance(scalar, pa.DoubleScalar): + return struct.pack('<d', value) + elif isinstance(scalar, pa.StringScalar): + return value.encode('utf-8') + elif isinstance(scalar, pa.BinaryScalar): + return value + elif isinstance(scalar, (pa.Date32Scalar, pa.Date64Scalar)): + epoch = datetime.date(1970, 1, 1) + days = (value - epoch).days + return struct.pack('<i', days) + elif isinstance(scalar, (pa.Time32Scalar, pa.Time64Scalar)): + microseconds = int(value.hour * 60 * 60 * 1e6 + + value.minute * 60 * 1e6 + + value.second * 1e6 + + value.microsecond) + return struct.pack('<q', microseconds) + elif isinstance(scalar, pa.TimestampScalar): + epoch = datetime.datetime(1970, 1, 1) + microseconds = int((value - epoch).total_seconds() * 1e6) + return struct.pack('<q', microseconds) + else: + raise TypeError('Unsupported type: {}'.format(type(scalar))) + + +def fill_parquet_file_metadata(df: DataFile, file_object: pa.NativeFile, table: pa.Table = None) -> None: + """ + Computes and fills the following fields of the DataFile object: + + - file_format + - record_count + - file_size_in_bytes + - column_sizes + - value_counts + - null_value_counts + - nan_value_counts + - lower_bounds + - upper_bounds + - split_offsets + + Args: + df (DataFile): A DataFile object representing the Parquet file for which metadata is to be filled. + file_object (pa.NativeFile): A pyarrow NativeFile object pointing to the location where the + Parquet file is stored. + table (pa.Table, optional): If the metadata is computed while writing a pyarrow Table to parquet + the table can be passed to compute the column statistics. If absent the table will be read + from file_object using pyarrow.parquet.read_table. + """ + + parquet_file = pq.ParquetFile(file_object) + metadata = parquet_file.metadata + + column_sizes = {} + value_counts = {} + + for r in range(metadata.num_row_groups): + for c in range(metadata.num_columns): + column_sizes[c+1] = column_sizes.get(c+1, 0) + metadata.row_group(r).column(c).total_compressed_size Review Comment: FieldIDs are a fundamental concept of Iceberg. Instead of relying on column names (or positions), each of the fields gets a unique field-id assigned that is monotonically increasing when new fields are being added (and are never re-used). These field IDs are used for example: - To safely rename a column (you change the name, but the ID is still pointing at the existing field). - To drop a column, and create a new column with the same name. Give this a try in Spark/Hive on a plain Parquet table. The `column_sizes` is also indexed by the field-id. What we need to do is: ``` >>> metadata_collector[0].row_group(0).to_dict() { 'num_columns': 2, 'num_rows': 6, 'total_byte_size': 256, 'columns': [{ 'file_offset': 119, 'file_path': 'c569c5eaf90c4395885f31e012068b69-0.parquet', 'physical_type': 'INT64', 'num_values': 6, 'path_in_schema': 'n_legs', 'is_stats_set': True, 'statistics': { 'has_min_max': True, 'min': 2, 'max': 100, 'null_count': 0, 'distinct_count': 0, 'num_values': 6, 'physical_type': 'INT64' }, 'compression': 'SNAPPY', 'encodings': ('PLAIN_DICTIONARY', 'PLAIN', 'RLE'), 'has_dictionary_page': True, 'dictionary_page_offset': 4, 'data_page_offset': 46, 'total_compressed_size': 115, 'total_uncompressed_size': 117 } } ``` Take the `path_in_schema`, in this case, `n_legs`. Using the current table schema you can do `schema.find_field('n_legs')` and that will return the `Field` that contains the field-id `schema.field_id`. ########## python/pyiceberg/utils/file_stats.py: ########## @@ -0,0 +1,164 @@ +# 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. + +from pyiceberg.manifest import DataFile, FileFormat +import pyarrow.parquet as pq +import pyarrow.compute as pc +import pyarrow as pa +import struct +import datetime + +BOUND_TRUNCATED_LENGHT = 16 + +# Serialization rules: https://iceberg.apache.org/spec/#binary-single-value-serialization +# +# Type Binary serialization +# boolean 0x00 for false, non-zero byte for true +# int Stored as 4-byte little-endian +# long Stored as 8-byte little-endian +# float Stored as 4-byte little-endian +# double Stored as 8-byte little-endian +# date Stores days from the 1970-01-01 in an 4-byte little-endian int +# time Stores microseconds from midnight in an 8-byte little-endian long +# timestamp without zone Stores microseconds from 1970-01-01 00:00:00.000000 in an 8-byte little-endian long +# timestamp with zone Stores microseconds from 1970-01-01 00:00:00.000000 UTC in an 8-byte little-endian long +# string UTF-8 bytes (without length) +# uuid 16-byte big-endian value, see example in Appendix B +# fixed(L) Binary value +# binary Binary value (without length) +# +def serialize_to_binary(scalar: pa.Scalar) -> bytes: + value = scalar.as_py() + if isinstance(scalar, pa.BooleanScalar): + return struct.pack('?', value) + elif isinstance(scalar, (pa.Int8Scalar, pa.UInt8Scalar)): + return struct.pack('<b', value) + elif isinstance(scalar, (pa.Int16Scalar, pa.UInt16Scalar)): + return struct.pack('<h', value) + elif isinstance(scalar, (pa.Int32Scalar, pa.UInt32Scalar)): + return struct.pack('<i', value) + elif isinstance(scalar, (pa.Int64Scalar, pa.UInt64Scalar)): + return struct.pack('<q', value) + elif isinstance(scalar, pa.FloatScalar): + return struct.pack('<f', value) + elif isinstance(scalar, pa.DoubleScalar): + return struct.pack('<d', value) + elif isinstance(scalar, pa.StringScalar): + return value.encode('utf-8') + elif isinstance(scalar, pa.BinaryScalar): + return value + elif isinstance(scalar, (pa.Date32Scalar, pa.Date64Scalar)): + epoch = datetime.date(1970, 1, 1) + days = (value - epoch).days + return struct.pack('<i', days) + elif isinstance(scalar, (pa.Time32Scalar, pa.Time64Scalar)): + microseconds = int(value.hour * 60 * 60 * 1e6 + + value.minute * 60 * 1e6 + + value.second * 1e6 + + value.microsecond) + return struct.pack('<q', microseconds) + elif isinstance(scalar, pa.TimestampScalar): + epoch = datetime.datetime(1970, 1, 1) + microseconds = int((value - epoch).total_seconds() * 1e6) + return struct.pack('<q', microseconds) + else: + raise TypeError('Unsupported type: {}'.format(type(scalar))) + + +def fill_parquet_file_metadata(df: DataFile, file_object: pa.NativeFile, table: pa.Table = None) -> None: + """ + Computes and fills the following fields of the DataFile object: + + - file_format + - record_count + - file_size_in_bytes + - column_sizes + - value_counts + - null_value_counts + - nan_value_counts + - lower_bounds + - upper_bounds + - split_offsets + + Args: + df (DataFile): A DataFile object representing the Parquet file for which metadata is to be filled. + file_object (pa.NativeFile): A pyarrow NativeFile object pointing to the location where the + Parquet file is stored. + table (pa.Table, optional): If the metadata is computed while writing a pyarrow Table to parquet + the table can be passed to compute the column statistics. If absent the table will be read + from file_object using pyarrow.parquet.read_table. + """ + + parquet_file = pq.ParquetFile(file_object) Review Comment: This is my main concern, we read the file here, and I would like to see if we can collect the statistics while writing. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
