zeroshade commented on a change in pull request #10951:
URL: https://github.com/apache/arrow/pull/10951#discussion_r700442503



##########
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

Review comment:
       updated comment.




-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to