storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/s3select/internal/parquet-go/reader_test.go (about)

     1  /*
     2   * Minio Cloud Storage, (C) 2018 Minio, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package parquet
    18  
    19  import (
    20  	"io"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/minio/minio-go/v7/pkg/set"
    25  )
    26  
    27  func getReader(name string, offset int64, length int64) (io.ReadCloser, error) {
    28  	file, err := os.Open(name)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	fi, err := file.Stat()
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	if offset < 0 {
    39  		offset = fi.Size() + offset
    40  	}
    41  
    42  	if _, err = file.Seek(offset, io.SeekStart); err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	return file, nil
    47  }
    48  
    49  func TestReader(t *testing.T) {
    50  	name := "example.parquet"
    51  	reader, err := NewReader(
    52  		func(offset, length int64) (io.ReadCloser, error) {
    53  			return getReader(name, offset, length)
    54  		},
    55  		set.CreateStringSet("one", "two", "three"),
    56  	)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	expectedRecords := []string{
    62  		`map[one:{-1 DOUBLE SchemaElement({Type:DOUBLE TypeLength:<nil> RepetitionType:OPTIONAL Name:one NumChildren:<nil> ConvertedType:<nil> Scale:<nil> Precision:<nil> FieldID:<nil> LogicalType:<nil>})} three:{true BOOLEAN SchemaElement({Type:BOOLEAN TypeLength:<nil> RepetitionType:OPTIONAL Name:three NumChildren:<nil> ConvertedType:<nil> Scale:<nil> Precision:<nil> FieldID:<nil> LogicalType:<nil>})} two:{[102 111 111] BYTE_ARRAY SchemaElement({Type:BYTE_ARRAY TypeLength:<nil> RepetitionType:OPTIONAL Name:two NumChildren:<nil> ConvertedType:<nil> Scale:<nil> Precision:<nil> FieldID:<nil> LogicalType:<nil>})}]`,
    63  		`map[one:{<nil> DOUBLE SchemaElement({Type:DOUBLE TypeLength:<nil> RepetitionType:OPTIONAL Name:one NumChildren:<nil> ConvertedType:<nil> Scale:<nil> Precision:<nil> FieldID:<nil> LogicalType:<nil>})} three:{false BOOLEAN SchemaElement({Type:BOOLEAN TypeLength:<nil> RepetitionType:OPTIONAL Name:three NumChildren:<nil> ConvertedType:<nil> Scale:<nil> Precision:<nil> FieldID:<nil> LogicalType:<nil>})} two:{[98 97 114] BYTE_ARRAY SchemaElement({Type:BYTE_ARRAY TypeLength:<nil> RepetitionType:OPTIONAL Name:two NumChildren:<nil> ConvertedType:<nil> Scale:<nil> Precision:<nil> FieldID:<nil> LogicalType:<nil>})}]`,
    64  		`map[one:{2.5 DOUBLE SchemaElement({Type:DOUBLE TypeLength:<nil> RepetitionType:OPTIONAL Name:one NumChildren:<nil> ConvertedType:<nil> Scale:<nil> Precision:<nil> FieldID:<nil> LogicalType:<nil>})} three:{true BOOLEAN SchemaElement({Type:BOOLEAN TypeLength:<nil> RepetitionType:OPTIONAL Name:three NumChildren:<nil> ConvertedType:<nil> Scale:<nil> Precision:<nil> FieldID:<nil> LogicalType:<nil>})} two:{[98 97 122] BYTE_ARRAY SchemaElement({Type:BYTE_ARRAY TypeLength:<nil> RepetitionType:OPTIONAL Name:two NumChildren:<nil> ConvertedType:<nil> Scale:<nil> Precision:<nil> FieldID:<nil> LogicalType:<nil>})}]`,
    65  	}
    66  
    67  	i := 0
    68  	for {
    69  		record, err := reader.Read()
    70  		if err != nil {
    71  			if err != io.EOF {
    72  				t.Error(err)
    73  			}
    74  
    75  			break
    76  		}
    77  
    78  		if i == len(expectedRecords) {
    79  			t.Errorf("read more than expected record count %v", len(expectedRecords))
    80  		}
    81  
    82  		if record.String() != expectedRecords[i] {
    83  			t.Errorf("record%v: expected: %v, got: %v", i+1, expectedRecords[i], record.String())
    84  		}
    85  
    86  		i++
    87  	}
    88  
    89  	reader.Close()
    90  }