github.com/apache/arrow/go/v14@v14.0.1/parquet/schema/helpers_test.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  You may obtain a copy of the License at
     8  //
     9  // http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package schema_test
    18  
    19  import (
    20  	"bytes"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/apache/arrow/go/v14/parquet"
    25  	"github.com/apache/arrow/go/v14/parquet/schema"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestListOf(t *testing.T) {
    30  	n := schema.NewByteArrayNode("str", parquet.Repetitions.Required, 1)
    31  	list, err := schema.ListOf(n, parquet.Repetitions.Optional, 2)
    32  
    33  	assert.NoError(t, err)
    34  	assert.Equal(t, "str", list.Name())
    35  	assert.Equal(t, parquet.Repetitions.Optional, list.RepetitionType())
    36  	assert.Equal(t, 1, list.NumFields())
    37  	assert.EqualValues(t, 2, list.FieldID())
    38  	assert.IsType(t, &schema.GroupNode{}, list.Field(0))
    39  	assert.Equal(t, "list", list.Field(0).Name())
    40  	assert.Equal(t, 1, list.Field(0).(*schema.GroupNode).NumFields())
    41  	assert.Same(t, n, list.Field(0).(*schema.GroupNode).Field(0))
    42  	assert.Equal(t, "element", list.Field(0).(*schema.GroupNode).Field(0).Name())
    43  }
    44  
    45  func TestListOfNested(t *testing.T) {
    46  	n, err := schema.ListOf(schema.NewInt32Node("arrays", parquet.Repetitions.Required, -1), parquet.Repetitions.Required, -1)
    47  	assert.NoError(t, err)
    48  	final, err := schema.ListOf(n, parquet.Repetitions.Required, -1)
    49  	assert.NoError(t, err)
    50  
    51  	var buf bytes.Buffer
    52  	schema.PrintSchema(final, &buf, 4)
    53  	assert.Equal(t,
    54  		`required group field_id=-1 arrays (List) {
    55      repeated group field_id=-1 list {
    56          required group field_id=-1 element (List) {
    57              repeated group field_id=-1 list {
    58                  required int32 field_id=-1 element;
    59              }
    60          }
    61      }
    62  }`, strings.TrimSpace(buf.String()))
    63  }
    64  
    65  func TestListOfWithNameNested(t *testing.T) {
    66  	n, err := schema.ListOfWithName("arrays", schema.NewInt32Node("element", parquet.Repetitions.Required, -1), parquet.Repetitions.Required, -1)
    67  	assert.NoError(t, err)
    68  	final, err := schema.ListOf(n, parquet.Repetitions.Required, -1)
    69  	assert.NoError(t, err)
    70  
    71  	var buf bytes.Buffer
    72  	schema.PrintSchema(final, &buf, 4)
    73  	assert.Equal(t,
    74  		`required group field_id=-1 arrays (List) {
    75      repeated group field_id=-1 list {
    76          required group field_id=-1 element (List) {
    77              repeated group field_id=-1 list {
    78                  required int32 field_id=-1 element;
    79              }
    80          }
    81      }
    82  }`, strings.TrimSpace(buf.String()))
    83  }
    84  func TestMapOfNestedTypes(t *testing.T) {
    85  	n, err := schema.NewGroupNode("student", parquet.Repetitions.Required, schema.FieldList{
    86  		schema.NewByteArrayNode("name", parquet.Repetitions.Required, -1),
    87  		schema.NewInt32Node("age", parquet.Repetitions.Optional, -1),
    88  	}, -1)
    89  	assert.NoError(t, err)
    90  
    91  	grp, err := schema.NewGroupNode("classes", parquet.Repetitions.Optional, schema.FieldList{
    92  		schema.NewInt32Node("a", parquet.Repetitions.Repeated, -1),
    93  		schema.NewFloat32Node("b", parquet.Repetitions.Repeated, -1),
    94  	}, -1)
    95  	assert.NoError(t, err)
    96  
    97  	classes, err := schema.ListOf(grp, parquet.Repetitions.Optional, -1)
    98  	assert.NoError(t, err)
    99  
   100  	m, err := schema.MapOf("studentmap", n, classes, parquet.Repetitions.Required, 1)
   101  	assert.NoError(t, err)
   102  
   103  	var buf bytes.Buffer
   104  	schema.PrintSchema(m, &buf, 4)
   105  	assert.Equal(t,
   106  		`required group field_id=1 studentmap (Map) {
   107      repeated group field_id=-1 key_value {
   108          required group field_id=-1 key {
   109              required byte_array field_id=-1 name;
   110              optional int32 field_id=-1 age;
   111          }
   112          optional group field_id=-1 value (List) {
   113              repeated group field_id=-1 list {
   114                  optional group field_id=-1 element {
   115                      repeated int32 field_id=-1 a;
   116                      repeated float field_id=-1 b;
   117                  }
   118              }
   119          }
   120      }
   121  }`, strings.TrimSpace(buf.String()))
   122  }