github.com/vc42/parquet-go@v0.0.0-20240320194221-1a9adb5f23f5/sort_test.go (about)

     1  package parquet_test
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/google/uuid"
     8  	"github.com/vc42/parquet-go"
     9  )
    10  
    11  func TestSortFunc(t *testing.T) {
    12  	sortFunc := parquet.SortFuncOf(parquet.String().Type(),
    13  		parquet.SortMaxDefinitionLevel(1),
    14  		parquet.SortDescending(true),
    15  		parquet.SortNullsFirst(true),
    16  	)
    17  
    18  	values := [][]parquet.Value{
    19  		{parquet.ValueOf("A")},
    20  		{parquet.ValueOf(nil)},
    21  		{parquet.ValueOf(nil)},
    22  		{parquet.ValueOf("C")},
    23  		{parquet.ValueOf("B")},
    24  		{parquet.ValueOf(nil)},
    25  	}
    26  
    27  	expect := [][]parquet.Value{
    28  		{parquet.ValueOf(nil)},
    29  		{parquet.ValueOf(nil)},
    30  		{parquet.ValueOf(nil)},
    31  		{parquet.ValueOf("C")},
    32  		{parquet.ValueOf("B")},
    33  		{parquet.ValueOf("A")},
    34  	}
    35  
    36  	sort.Slice(values, func(i, j int) bool {
    37  		return sortFunc(values[i], values[j]) < 0
    38  	})
    39  
    40  	for i := range values {
    41  		if !parquet.Equal(values[i][0], expect[i][0]) {
    42  			t.Errorf("value at index %d mismatch: got=%+v want=%+v\n%+v\n%+v", i, expect[i], values[i], expect, values)
    43  			break
    44  		}
    45  	}
    46  }
    47  
    48  func TestRepeatedUUIDSortFunc(t *testing.T) {
    49  	type testStruct struct {
    50  		List []uuid.UUID `parquet:"list"`
    51  	}
    52  
    53  	s := parquet.SchemaOf(&testStruct{})
    54  
    55  	a := s.Deconstruct(nil, &testStruct{
    56  		List: []uuid.UUID{
    57  			uuid.MustParse("00000000-0000-0000-0000-000000000001"),
    58  			uuid.MustParse("00000000-0000-0000-0000-000000000002"),
    59  		},
    60  	})
    61  
    62  	b := s.Deconstruct(nil, &testStruct{
    63  		List: []uuid.UUID{
    64  			uuid.MustParse("00000000-0000-0000-0000-000000000001"),
    65  			uuid.MustParse("00000000-0000-0000-0000-000000000002"),
    66  			uuid.MustParse("00000000-0000-0000-0000-000000000003"),
    67  		},
    68  	})
    69  
    70  	// a and b are equal up until the third element, then a ends so a < b.
    71  	f := parquet.SortFuncOf(
    72  		s.Fields()[0].Type(),
    73  		parquet.SortDescending(false),
    74  		parquet.SortNullsFirst(true),
    75  		parquet.SortMaxDefinitionLevel(1),
    76  		parquet.SortMaxRepetitionLevel(1),
    77  	)
    78  	cmp := f(a, b)
    79  	if cmp >= 0 {
    80  		t.Fatal("expected a < b, got compare value", cmp)
    81  	}
    82  }