zeroshade commented on code in PR #14683: URL: https://github.com/apache/arrow/pull/14683#discussion_r1033700092
########## go/parquet/s3/s3_reader.go: ########## @@ -0,0 +1,305 @@ +// 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 s3 + +import ( + "bytes" + "context" + "encoding/binary" + "fmt" + "io" + "runtime" + "sync" + + "github.com/apache/arrow/go/v11/arrow/memory" + "github.com/apache/arrow/go/v11/parquet" + "github.com/apache/arrow/go/v11/parquet/internal/encryption" + "github.com/apache/arrow/go/v11/parquet/metadata" + "github.com/xitongsys/parquet-go-source/s3" + "github.com/xitongsys/parquet-go/source" + "golang.org/x/xerrors" +) + +const ( + footerSize uint32 = 8 +) + +var ( + magicBytes = []byte("PAR1") + magicEBytes = []byte("PARE") + errInconsistentFileMetadata = xerrors.New("parquet: file is smaller than indicated metadata size") +) + +type S3file struct { + source.ParquetFile +} + +func (rdr S3file) ReadAt(p []byte, off int64) (n int, err error) { + rdr.Seek(off, io.SeekCurrent) + return rdr.Read(p) +} + + +// Reader is the main interface for reading a parquet file +type Reader struct { + r parquet.ReaderAtSeeker + props *parquet.ReaderProperties + metadata *metadata.FileMetaData + footerOffset int64 + fileDecryptor encryption.FileDecryptor + + bufferPool sync.Pool +} + +type ReadOption func(*Reader) + +// WithReadProps specifies a specific reader properties instance to use, rather +// than using the default ReaderProperties. +func WithReadProps(props *parquet.ReaderProperties) ReadOption { + return func(r *Reader) { + r.props = props + } +} + +// WithMetadata allows providing a specific FileMetaData object rather than reading +// the file metadata from the file itself. +func WithMetadata(m *metadata.FileMetaData) ReadOption { + return func(r *Reader) { + r.metadata = m + } +} + +// OpenParquetFile will return a Reader for the given parquet file on the local file system. +// +// Optionally the file can be memory mapped for faster reading. If no read properties are provided +// then the default ReaderProperties will be used. The WithMetadata option can be used to provide +// a FileMetaData object rather than reading the file metadata from the file. +func OpenParquetFile(filename string, opts ...ReadOption) (*Reader, error) { + ctx := context.Background() + bucket := "writo-test-bucket" + + // var source source.ParquetFile + source, err := s3.NewS3FileReader(ctx, bucket, filename) + if err != nil { + return nil, err + } + src := S3file { source } + return NewParquetReader(src, opts...) +} + +// NewParquetReader returns a FileReader instance that reads a parquet file which can be read from r. +// This reader needs to support Read, ReadAt and Seeking. +// +// If no read properties are provided then the default ReaderProperties will be used. The WithMetadata +// option can be used to provide a FileMetaData object rather than reading the file metadata from the file. +func NewParquetReader(r parquet.ReaderAtSeeker, opts ...ReadOption) (*Reader, error) { + var err error + f := &Reader{r: r} + for _, o := range opts { + o(f) + } + + if f.footerOffset <= 0 { + f.footerOffset, err = r.Seek(0, io.SeekEnd) + if err != nil { + return nil, fmt.Errorf("parquet: could not retrieve footer offset: %w", err) + } + } + + if f.props == nil { + f.props = parquet.NewReaderProperties(memory.NewGoAllocator()) + } + + f.bufferPool = sync.Pool{ + New: func() interface{} { + buf := memory.NewResizableBuffer(f.props.Allocator()) + runtime.SetFinalizer(buf, func(obj *memory.Buffer) { + obj.Release() + }) + return buf + }, + } + + f.footerOffset = 0 + if f.metadata == nil { + return f, f.parseMetaData() + } + + return f, nil +} + +// BufferPool returns the internal buffer pool being utilized by this reader. +// This is primarily for use by the pqarrow.FileReader or anything that builds +// on top of the Reader and constructs their own ColumnReaders (like the +// RecordReader) +func (f *Reader) BufferPool() *sync.Pool { + return &f.bufferPool +} + +// Close will close the current reader, and if the underlying reader being used +// is an `io.Closer` then Close will be called on it too. +func (f *Reader) Close() error { + if r, ok := f.r.(io.Closer); ok { + return r.Close() + } + return nil +} + +// MetaData returns the underlying FileMetadata object +func (f *Reader) MetaData() *metadata.FileMetaData { return f.metadata } + +// parseMetaData handles parsing the metadata from the opened file. +func (f *Reader) parseMetaData() error { + if f.footerOffset <= int64(footerSize) { + return fmt.Errorf("parquet: file too small (size=%d)", f.footerOffset) + } + + buf := make([]byte, footerSize) + // backup 8 bytes to read the footer size (first four bytes) and the magic bytes (last 4 bytes) + n, err := f.r.ReadAt(buf, f.footerOffset-int64(footerSize)) + if err != nil { + return fmt.Errorf("parquet: could not read footer: %w", err) + } Review Comment: All of this logic already exists in the `file` package of the Parquet module and is unnecessary to reproduce. All you need to do is have something that meets the `parquet.ReaderAtSeeker` interface and everything else will be handled automatically. -- 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]
