github.com/apache/arrow/go/v7@v7.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/v7/parquet"
    25  	"github.com/apache/arrow/go/v7/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 TestMapOfNestedTypes(t *testing.T) {
    66  	n, err := schema.NewGroupNode("student", parquet.Repetitions.Required, schema.FieldList{
    67  		schema.NewByteArrayNode("name", parquet.Repetitions.Required, -1),
    68  		schema.NewInt32Node("age", parquet.Repetitions.Optional, -1),
    69  	}, -1)
    70  	assert.NoError(t, err)
    71  
    72  	grp, err := schema.NewGroupNode("classes", parquet.Repetitions.Optional, schema.FieldList{
    73  		schema.NewInt32Node("a", parquet.Repetitions.Repeated, -1),
    74  		schema.NewFloat32Node("b", parquet.Repetitions.Repeated, -1),
    75  	}, -1)
    76  	assert.NoError(t, err)
    77  
    78  	classes, err := schema.ListOf(grp, parquet.Repetitions.Optional, -1)
    79  	assert.NoError(t, err)
    80  
    81  	m, err := schema.MapOf("studentmap", n, classes, parquet.Repetitions.Required, 1)
    82  	assert.NoError(t, err)
    83  
    84  	var buf bytes.Buffer
    85  	schema.PrintSchema(m, &buf, 4)
    86  	assert.Equal(t,
    87  		`required group field_id=1 studentmap (Map) {
    88      repeated group field_id=-1 key_value {
    89          required group field_id=-1 key {
    90              required byte_array field_id=-1 name;
    91              optional int32 field_id=-1 age;
    92          }
    93          optional group field_id=-1 value (List) {
    94              repeated group field_id=-1 list {
    95                  optional group field_id=-1 element {
    96                      repeated int32 field_id=-1 a;
    97                      repeated float field_id=-1 b;
    98                  }
    99              }
   100          }
   101      }
   102  }`, strings.TrimSpace(buf.String()))
   103  }