emkornfield commented on a change in pull request #10716:
URL: https://github.com/apache/arrow/pull/10716#discussion_r680026208



##########
File path: go/parquet/internal/encoding/levels.go
##########
@@ -0,0 +1,284 @@
+// 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 encoding
+
+import (
+       "bytes"
+       "encoding/binary"
+       "io"
+       "math/bits"
+
+       "github.com/JohnCGriffin/overflow"
+       "github.com/apache/arrow/go/arrow/bitutil"
+       "github.com/apache/arrow/go/parquet"
+       format "github.com/apache/arrow/go/parquet/internal/gen-go/parquet"
+       "github.com/apache/arrow/go/parquet/internal/utils"
+)
+
+// LevelEncoder is for handling the encoding of Definition and Repetition 
levels
+// to parquet files.
+type LevelEncoder struct {
+       bitWidth int
+       rleLen   int
+       encoding format.Encoding
+       rle      *utils.RleEncoder
+       bit      *utils.BitWriter
+}
+
+// LevelEncodingMaxBufferSize estimates the max number of bytes needed to 
encode data with the
+// specified encoding given the max level and number of buffered values 
provided.
+func LevelEncodingMaxBufferSize(encoding parquet.Encoding, maxLvl int16, 
nbuffered int) int {
+       bitWidth := bits.Len64(uint64(maxLvl))
+       nbytes := 0
+       switch encoding {
+       case parquet.Encodings.RLE:
+               nbytes = utils.MaxBufferSize(bitWidth, nbuffered) + 
utils.MinBufferSize(bitWidth)
+       case parquet.Encodings.BitPacked:
+               nbytes = int(bitutil.BytesForBits(int64(nbuffered * bitWidth)))
+       default:
+               panic("parquet: unknown encoding type for levels")
+       }
+       return nbytes
+}
+
+// Reset resets the encoder allowing it to be reused and updating the maxlevel 
to the new
+// specified value.
+func (l *LevelEncoder) Reset(maxLvl int16) {
+       l.bitWidth = bits.Len64(uint64(maxLvl))
+       switch l.encoding {
+       case format.Encoding_RLE:
+               l.rle.Clear()
+               l.rle.BitWidth = l.bitWidth
+       case format.Encoding_BIT_PACKED:
+               l.bit.Clear()
+       default:
+               panic("parquet: unknown encoding type")
+       }
+}
+
+// Init is called to set up the desired encoding type, max level and 
underlying writer for a
+// level encoder to control where the resulting encoded buffer will end up.
+func (l *LevelEncoder) Init(encoding parquet.Encoding, maxLvl int16, w 
io.WriterAt) {
+       l.bitWidth = bits.Len64(uint64(maxLvl))
+       l.encoding = format.Encoding(encoding)
+       switch l.encoding {
+       case format.Encoding_RLE:
+               l.rle = utils.NewRleEncoder(w, l.bitWidth)
+       case format.Encoding_BIT_PACKED:
+               l.bit = utils.NewBitWriter(w)
+       default:
+               panic("parquet: unknown encoding type for levels")
+       }
+}
+
+// EncodeNoFlush encodes the provided levels in the encoder, but doesn't flush
+// the buffer and return it yet, appending these encoded values. Returns the 
number

Review comment:
       does it ever return less than the values provided in lvls?  If so please 
document it (if not maybe still note that this is simply for the API consumer's 
convenience?)




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