hcrosse commented on code in PR #1592: URL: https://github.com/apache/arrow-go/pull/1592#discussion_r4603039448
########## parquet/pqarrow/parallel_unsupported_encoding_test.go: ########## @@ -0,0 +1,80 @@ +// 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 pqarrow_test + +import ( + "bytes" + "context" + "encoding/binary" + "testing" + + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/arrow-go/v18/parquet" + "github.com/apache/arrow-go/v18/parquet/compress" + "github.com/apache/arrow-go/v18/parquet/file" + "github.com/apache/arrow-go/v18/parquet/internal/utils" + "github.com/apache/arrow-go/v18/parquet/metadata" + "github.com/apache/arrow-go/v18/parquet/pqarrow" + "github.com/apache/arrow-go/v18/parquet/schema" + "github.com/stretchr/testify/require" +) + +// The parallel record reader decodes on goroutines that do not recover, so a +// panic for an unsupported encoding ends the process rather than failing a test. +func TestParallelRecordReaderUnsupportedEncodingReturnsError(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.DefaultAllocator) + defer mem.AssertSize(t, 0) + + sc := schema.NewSchema(schema.MustGroup(schema.NewGroupNode("schema", parquet.Repetitions.Required, + schema.FieldList{schema.NewFloat64Node("v", parquet.Repetitions.Required, -1)}, -1))) + metaBldr := metadata.NewFileMetadataBuilder(sc, nil, nil) + rgMeta := metaBldr.AppendRowGroup() + rgMeta.SetNumRows(3) + + var buf bytes.Buffer + sink := &utils.TellWrapper{Writer: &buf} + _, err := sink.Write([]byte("PAR1")) + require.NoError(t, err) + pager, err := file.NewPageWriter(sink, compress.Codecs.Uncompressed, compress.DefaultCompressionLevel, + rgMeta.NextColumnChunk(), 0, 0, mem, false, nil, nil) + require.NoError(t, err) + body := memory.NewBufferBytes(make([]byte, 24)) + _, err = pager.WriteDataPage(file.NewDataPageV1(body, 3, parquet.Encodings.DeltaBinaryPacked, Review Comment: The page body is fixture data for the writer. `mem.AssertSize` checks what the writer and reader allocate through `mem`, and a buffer that wraps a Go slice takes nothing from it. ########## parquet/file/column_reader.go: ########## @@ -527,6 +519,30 @@ func (c *columnChunkReader) initDataDecoder(page DataPage, lvlByteLen int64) err return c.setDecoderData(page, lvlByteLen) } +// checkEncodingType rejects a physical type that the encoding does not apply +// to. The decoder traits panic on one, so this must run before they are called. +func checkEncodingType(enc format.Encoding, pt parquet.Type) error { Review Comment: `initDataDecoder` is the only place that builds a decoder for a data page's encoding. The dictionary paths build `PLAIN` decoders, which accept every physical type, and the comment on `checkEncodingType` already says it must run before the traits. ########## parquet/file/column_reader_test.go: ########## @@ -674,14 +674,45 @@ func (p *PrimitiveReaderSuite) TestDictionaryEncodedPages() { p.pages = p.pages[:0] }) - p.Run("Unsupported encoding", func() { - dataPage := testutils.MakeDataPage(p.dataPageVersion, descr, nil, 0, parquet.Encodings.DeltaByteArray, dummy, nil, nil, 0, 0, 0) - p.pages = append(p.pages, dataPage) - p.initReader(descr) - p.Panics(func() { p.reader.HasNext() }) - // p.Error(p.reader.Err()) - p.pages = p.pages[:0] - }) + // The decoder is chosen before the page body is read, so the body is empty. + unsupported := []struct { + enc parquet.Encoding + want string + types []parquet.Type + }{ + {parquet.Encodings.RLE, "RLE encoding only supports BOOLEAN", + []parquet.Type{parquet.Types.Int32}}, + {parquet.Encodings.DeltaBinaryPacked, "DELTA_BINARY_PACKED encoding only supports INT32 and INT64", + []parquet.Type{parquet.Types.Boolean, parquet.Types.Int96, parquet.Types.Float, parquet.Types.Double, + parquet.Types.ByteArray, parquet.Types.FixedLenByteArray}}, + {parquet.Encodings.DeltaLengthByteArray, "DELTA_LENGTH_BYTE_ARRAY encoding only supports BYTE_ARRAY", + []parquet.Type{parquet.Types.Boolean, parquet.Types.Int32, parquet.Types.Int64, parquet.Types.Int96, + parquet.Types.Float, parquet.Types.Double, parquet.Types.FixedLenByteArray}}, + {parquet.Encodings.DeltaByteArray, "DELTA_BYTE_ARRAY encoding only supports BYTE_ARRAY and FIXED_LEN_BYTE_ARRAY", + []parquet.Type{parquet.Types.Boolean, parquet.Types.Int32, parquet.Types.Int64, parquet.Types.Int96, + parquet.Types.Float, parquet.Types.Double}}, + {parquet.Encodings.ByteStreamSplit, "BYTE_STREAM_SPLIT encoding only supports INT32, INT64, FLOAT, DOUBLE and FIXED_LEN_BYTE_ARRAY", + []parquet.Type{parquet.Types.Boolean, parquet.Types.Int96, parquet.Types.ByteArray}}, + } + for _, tt := range unsupported { + for _, typ := range tt.types { + p.Run(fmt.Sprintf("Unsupported encoding %s for %s", tt.enc, typ), func() { + typeLen := int32(-1) + if typ == parquet.Types.FixedLenByteArray { + typeLen = 4 + } + node, err := schema.NewPrimitiveNode("a", parquet.Repetitions.Required, typ, -1, typeLen) + p.Require().NoError(err) + descr := schema.NewColumn(node, p.maxDefLvl, p.maxRepLvl) + dataPage := testutils.MakeDataPage(p.dataPageVersion, descr, nil, 0, tt.enc, dummy, nil, nil, 0, 0, 0) + p.pages = append(p.pages, dataPage) + p.initReader(descr) + p.NotPanics(func() { p.False(p.reader.HasNext()) }) + p.ErrorContains(p.reader.Err(), fmt.Sprintf("%s, got %s", tt.want, typ)) Review Comment: The test asserts that with `p.False(p.reader.HasNext())` inside `NotPanics`, then checks the error. -- 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]
