emkornfield commented on a change in pull request #10951: URL: https://github.com/apache/arrow/pull/10951#discussion_r699814982
########## File path: go/parquet/metadata/column_chunk.go ########## @@ -0,0 +1,385 @@ +// 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. + +package metadata + +import ( + "bytes" + "context" + "io" + "reflect" + + "github.com/apache/arrow/go/arrow/memory" + "github.com/apache/arrow/go/parquet" + "github.com/apache/arrow/go/parquet/compress" + "github.com/apache/arrow/go/parquet/internal/encryption" + format "github.com/apache/arrow/go/parquet/internal/gen-go/parquet" + "github.com/apache/arrow/go/parquet/internal/thrift" + "github.com/apache/arrow/go/parquet/schema" + "golang.org/x/xerrors" +) + +// PageEncodingStats is used for counting the number of pages of specific +// types with the given internal encoding. +type PageEncodingStats struct { + Encoding parquet.Encoding + PageType format.PageType +} + +func makeColumnStats(metadata *format.ColumnMetaData, descr *schema.Column, mem memory.Allocator) TypedStatistics { + if descr.ColumnOrder() == parquet.ColumnOrders.TypeDefinedOrder { + return NewStatisticsFromEncoded(descr, mem, metadata.Statistics.MinValue, metadata.Statistics.MaxValue, + metadata.NumValues-metadata.Statistics.GetNullCount(), + metadata.Statistics.GetNullCount(), metadata.Statistics.GetDistinctCount(), + metadata.Statistics.IsSetMaxValue() || metadata.Statistics.IsSetMinValue(), + metadata.Statistics.IsSetNullCount(), metadata.Statistics.IsSetDistinctCount()) + } + return NewStatisticsFromEncoded(descr, mem, metadata.Statistics.Min, metadata.Statistics.Max, + metadata.NumValues-metadata.Statistics.GetNullCount(), + metadata.Statistics.GetNullCount(), metadata.Statistics.GetDistinctCount(), + metadata.Statistics.IsSetMax() || metadata.Statistics.IsSetMin(), + metadata.Statistics.IsSetNullCount(), metadata.Statistics.IsSetDistinctCount()) +} + +// ColumnChunkMetaData is a proxy around format.ColumnChunkMetaData +// containing all of the information and metadata for a given column chunk +// and it's associated Column +type ColumnChunkMetaData struct { + column *format.ColumnChunk + columnMeta *format.ColumnMetaData + decryptedMeta format.ColumnMetaData + descr *schema.Column + writerVersion *AppVersion + encodings []parquet.Encoding + encodingStats []format.PageEncodingStats + possibleStats TypedStatistics + mem memory.Allocator +} + +// NewColumnChunkMetaData creates an instance of the metadata from a column chunk and descriptor +// +// this is primarily used internally or between the subpackages. ColumnChunkMetaDataBuilder should +// be used by consumers instead of using this directly. +func NewColumnChunkMetaData(column *format.ColumnChunk, descr *schema.Column, writerVersion *AppVersion, rowGroupOrdinal, columnOrdinal int16, fileDecryptor encryption.FileDecryptor) (*ColumnChunkMetaData, error) { + c := &ColumnChunkMetaData{ + column: column, + columnMeta: column.GetMetaData(), + descr: descr, + writerVersion: writerVersion, + mem: memory.DefaultAllocator, + } + if column.IsSetCryptoMetadata() { + ccmd := column.CryptoMetadata + + if ccmd.IsSetENCRYPTION_WITH_COLUMN_KEY() { + if fileDecryptor != nil && fileDecryptor.Properties() != nil { + // should decrypt metadata + path := parquet.ColumnPath(ccmd.ENCRYPTION_WITH_COLUMN_KEY.GetPathInSchema()) + keyMetadata := ccmd.ENCRYPTION_WITH_COLUMN_KEY.GetKeyMetadata() + aadColumnMetadata := encryption.CreateModuleAad(fileDecryptor.FileAad(), encryption.ColumnMetaModule, rowGroupOrdinal, columnOrdinal, -1) + decryptor := fileDecryptor.GetColumnMetaDecryptor(path.String(), string(keyMetadata), aadColumnMetadata) + thrift.DeserializeThrift(&c.decryptedMeta, decryptor.Decrypt(column.GetEncryptedColumnMetadata())) + c.columnMeta = &c.decryptedMeta + } else { + return nil, xerrors.New("cannot decrypt column metadata. file decryption not setup correctly") + } + } + } + for _, enc := range c.columnMeta.Encodings { + c.encodings = append(c.encodings, parquet.Encoding(enc)) + } + for _, enc := range c.columnMeta.EncodingStats { + c.encodingStats = append(c.encodingStats, *enc) + } + return c, nil +} + +// CryptoMetadata returns the cryptographic metadata for how this column was +// encrypted and how to decrypt it. +func (c *ColumnChunkMetaData) CryptoMetadata() *format.ColumnCryptoMetaData { + return c.column.GetCryptoMetadata() +} + +// FileOffset is the location in the file where the column data begins +func (c *ColumnChunkMetaData) FileOffset() int64 { return c.column.FileOffset } + +// FilePath gives the name of the parquet file if provided in the metadata +func (c *ColumnChunkMetaData) FilePath() string { return c.column.GetFilePath() } + +// Type is the physical storage type used in the parquet file for this column chunk. +func (c *ColumnChunkMetaData) Type() parquet.Type { return parquet.Type(c.columnMeta.Type) } + +// NumValues is the number of values stored in just this chunk +func (c *ColumnChunkMetaData) NumValues() int64 { return c.columnMeta.NumValues } + +// PathInSchema is the full path to this column from the root of the schema including +// any nested columns +func (c *ColumnChunkMetaData) PathInSchema() parquet.ColumnPath { + return c.columnMeta.GetPathInSchema() +} + +// Compression provides the type of compression used for this particular chunk. +func (c *ColumnChunkMetaData) Compression() compress.Compression { + return compress.Compression(c.columnMeta.Codec) +} + +// Encodings returns the list of different encodings used in this chunk +func (c *ColumnChunkMetaData) Encodings() []parquet.Encoding { return c.encodings } + +// EncodingStats connects the order of encodings based on the list of pages and types +func (c *ColumnChunkMetaData) EncodingStats() []PageEncodingStats { + ret := make([]PageEncodingStats, len(c.encodingStats)) + for idx := range ret { + ret[idx].Encoding = parquet.Encoding(c.encodingStats[idx].Encoding) + ret[idx].PageType = c.encodingStats[idx].PageType + } + return ret +} + +// HasDictionaryPage returns true if there is a dictionary page offset set in +// this metadata. +func (c *ColumnChunkMetaData) HasDictionaryPage() bool { + return c.columnMeta.IsSetDictionaryPageOffset() +} + +// DictionaryPageOffset returns the location in the file where the dictionary page starts +func (c *ColumnChunkMetaData) DictionaryPageOffset() int64 { + return c.columnMeta.GetDictionaryPageOffset() +} + +// DataPageOffset returns the location in the file where the data pages begin for this column +func (c *ColumnChunkMetaData) DataPageOffset() int64 { return c.columnMeta.GetDataPageOffset() } + +// HasIndexPage returns true if the offset for the index page is set in the metadata +func (c *ColumnChunkMetaData) HasIndexPage() bool { return c.columnMeta.IsSetIndexPageOffset() } + +// IndexPageOffset is the location in the file where the index page starts. +func (c *ColumnChunkMetaData) IndexPageOffset() int64 { return c.columnMeta.GetIndexPageOffset() } + +// TotalCompressedSize will be equal to TotalUncompressedSize if the data is not compressed. +// Otherwise this will be the size of the actual data in the file. +func (c *ColumnChunkMetaData) TotalCompressedSize() int64 { + return c.columnMeta.GetTotalCompressedSize() +} + +// TotalUncompressedSize is the total size of the raw data after uncompressing the chunk +func (c *ColumnChunkMetaData) TotalUncompressedSize() int64 { Review comment: nit: should there be bloom filter info here as well? ########## File path: go/parquet/metadata/column_chunk.go ########## @@ -0,0 +1,385 @@ +// 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. + +package metadata + +import ( + "bytes" + "context" + "io" + "reflect" + + "github.com/apache/arrow/go/arrow/memory" + "github.com/apache/arrow/go/parquet" + "github.com/apache/arrow/go/parquet/compress" + "github.com/apache/arrow/go/parquet/internal/encryption" + format "github.com/apache/arrow/go/parquet/internal/gen-go/parquet" + "github.com/apache/arrow/go/parquet/internal/thrift" + "github.com/apache/arrow/go/parquet/schema" + "golang.org/x/xerrors" +) + +// PageEncodingStats is used for counting the number of pages of specific +// types with the given internal encoding. +type PageEncodingStats struct { + Encoding parquet.Encoding + PageType format.PageType +} + +func makeColumnStats(metadata *format.ColumnMetaData, descr *schema.Column, mem memory.Allocator) TypedStatistics { + if descr.ColumnOrder() == parquet.ColumnOrders.TypeDefinedOrder { + return NewStatisticsFromEncoded(descr, mem, metadata.Statistics.MinValue, metadata.Statistics.MaxValue, + metadata.NumValues-metadata.Statistics.GetNullCount(), + metadata.Statistics.GetNullCount(), metadata.Statistics.GetDistinctCount(), + metadata.Statistics.IsSetMaxValue() || metadata.Statistics.IsSetMinValue(), + metadata.Statistics.IsSetNullCount(), metadata.Statistics.IsSetDistinctCount()) + } + return NewStatisticsFromEncoded(descr, mem, metadata.Statistics.Min, metadata.Statistics.Max, + metadata.NumValues-metadata.Statistics.GetNullCount(), + metadata.Statistics.GetNullCount(), metadata.Statistics.GetDistinctCount(), + metadata.Statistics.IsSetMax() || metadata.Statistics.IsSetMin(), + metadata.Statistics.IsSetNullCount(), metadata.Statistics.IsSetDistinctCount()) +} + +// ColumnChunkMetaData is a proxy around format.ColumnChunkMetaData +// containing all of the information and metadata for a given column chunk +// and it's associated Column +type ColumnChunkMetaData struct { + column *format.ColumnChunk + columnMeta *format.ColumnMetaData + decryptedMeta format.ColumnMetaData + descr *schema.Column + writerVersion *AppVersion + encodings []parquet.Encoding + encodingStats []format.PageEncodingStats + possibleStats TypedStatistics + mem memory.Allocator +} + +// NewColumnChunkMetaData creates an instance of the metadata from a column chunk and descriptor +// +// this is primarily used internally or between the subpackages. ColumnChunkMetaDataBuilder should +// be used by consumers instead of using this directly. +func NewColumnChunkMetaData(column *format.ColumnChunk, descr *schema.Column, writerVersion *AppVersion, rowGroupOrdinal, columnOrdinal int16, fileDecryptor encryption.FileDecryptor) (*ColumnChunkMetaData, error) { + c := &ColumnChunkMetaData{ + column: column, + columnMeta: column.GetMetaData(), + descr: descr, + writerVersion: writerVersion, + mem: memory.DefaultAllocator, + } + if column.IsSetCryptoMetadata() { + ccmd := column.CryptoMetadata + + if ccmd.IsSetENCRYPTION_WITH_COLUMN_KEY() { + if fileDecryptor != nil && fileDecryptor.Properties() != nil { + // should decrypt metadata + path := parquet.ColumnPath(ccmd.ENCRYPTION_WITH_COLUMN_KEY.GetPathInSchema()) + keyMetadata := ccmd.ENCRYPTION_WITH_COLUMN_KEY.GetKeyMetadata() + aadColumnMetadata := encryption.CreateModuleAad(fileDecryptor.FileAad(), encryption.ColumnMetaModule, rowGroupOrdinal, columnOrdinal, -1) + decryptor := fileDecryptor.GetColumnMetaDecryptor(path.String(), string(keyMetadata), aadColumnMetadata) + thrift.DeserializeThrift(&c.decryptedMeta, decryptor.Decrypt(column.GetEncryptedColumnMetadata())) + c.columnMeta = &c.decryptedMeta + } else { + return nil, xerrors.New("cannot decrypt column metadata. file decryption not setup correctly") + } + } + } + for _, enc := range c.columnMeta.Encodings { + c.encodings = append(c.encodings, parquet.Encoding(enc)) + } + for _, enc := range c.columnMeta.EncodingStats { + c.encodingStats = append(c.encodingStats, *enc) + } + return c, nil +} + +// CryptoMetadata returns the cryptographic metadata for how this column was +// encrypted and how to decrypt it. +func (c *ColumnChunkMetaData) CryptoMetadata() *format.ColumnCryptoMetaData { + return c.column.GetCryptoMetadata() +} + +// FileOffset is the location in the file where the column data begins +func (c *ColumnChunkMetaData) FileOffset() int64 { return c.column.FileOffset } + +// FilePath gives the name of the parquet file if provided in the metadata +func (c *ColumnChunkMetaData) FilePath() string { return c.column.GetFilePath() } + +// Type is the physical storage type used in the parquet file for this column chunk. +func (c *ColumnChunkMetaData) Type() parquet.Type { return parquet.Type(c.columnMeta.Type) } + +// NumValues is the number of values stored in just this chunk +func (c *ColumnChunkMetaData) NumValues() int64 { return c.columnMeta.NumValues } + +// PathInSchema is the full path to this column from the root of the schema including +// any nested columns +func (c *ColumnChunkMetaData) PathInSchema() parquet.ColumnPath { + return c.columnMeta.GetPathInSchema() +} + +// Compression provides the type of compression used for this particular chunk. +func (c *ColumnChunkMetaData) Compression() compress.Compression { + return compress.Compression(c.columnMeta.Codec) +} + +// Encodings returns the list of different encodings used in this chunk +func (c *ColumnChunkMetaData) Encodings() []parquet.Encoding { return c.encodings } + +// EncodingStats connects the order of encodings based on the list of pages and types +func (c *ColumnChunkMetaData) EncodingStats() []PageEncodingStats { + ret := make([]PageEncodingStats, len(c.encodingStats)) + for idx := range ret { + ret[idx].Encoding = parquet.Encoding(c.encodingStats[idx].Encoding) + ret[idx].PageType = c.encodingStats[idx].PageType + } + return ret +} + +// HasDictionaryPage returns true if there is a dictionary page offset set in +// this metadata. +func (c *ColumnChunkMetaData) HasDictionaryPage() bool { + return c.columnMeta.IsSetDictionaryPageOffset() +} + +// DictionaryPageOffset returns the location in the file where the dictionary page starts +func (c *ColumnChunkMetaData) DictionaryPageOffset() int64 { + return c.columnMeta.GetDictionaryPageOffset() +} + +// DataPageOffset returns the location in the file where the data pages begin for this column +func (c *ColumnChunkMetaData) DataPageOffset() int64 { return c.columnMeta.GetDataPageOffset() } + +// HasIndexPage returns true if the offset for the index page is set in the metadata +func (c *ColumnChunkMetaData) HasIndexPage() bool { return c.columnMeta.IsSetIndexPageOffset() } + +// IndexPageOffset is the location in the file where the index page starts. +func (c *ColumnChunkMetaData) IndexPageOffset() int64 { return c.columnMeta.GetIndexPageOffset() } + +// TotalCompressedSize will be equal to TotalUncompressedSize if the data is not compressed. +// Otherwise this will be the size of the actual data in the file. +func (c *ColumnChunkMetaData) TotalCompressedSize() int64 { + return c.columnMeta.GetTotalCompressedSize() +} + +// TotalUncompressedSize is the total size of the raw data after uncompressing the chunk +func (c *ColumnChunkMetaData) TotalUncompressedSize() int64 { Review comment: nit: should there be bloom filter info in this file as well? -- 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]
