stru.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/apache/arrow/go/arrow"
  5. "github.com/apache/arrow/go/arrow/array"
  6. "github.com/apache/arrow/go/arrow/memory"
  7. )
  8. func main() {
  9. pool := memory.NewGoAllocator()
  10. dtype := arrow.StructOf([]arrow.Field{
  11. {Name: "f1", Type: arrow.ListOf(arrow.PrimitiveTypes.Uint8)},
  12. {Name: "f2", Type: arrow.PrimitiveTypes.Int32},
  13. }...)
  14. sb := array.NewStructBuilder(pool, dtype)
  15. defer sb.Release()
  16. f1b := sb.FieldBuilder(0).(*array.ListBuilder)
  17. defer f1b.Release()
  18. f1vb := f1b.ValueBuilder().(*array.Uint8Builder)
  19. defer f1vb.Release()
  20. f2b := sb.FieldBuilder(1).(*array.Int32Builder)
  21. defer f2b.Release()
  22. sb.Reserve(4)
  23. f1vb.Reserve(7)
  24. f2b.Reserve(3)
  25. sb.Append(true)
  26. f1b.Append(true)
  27. f1vb.AppendValues([]byte("joe"), nil)
  28. f2b.Append(1)
  29. sb.Append(true)
  30. f1b.AppendNull()
  31. f2b.Append(2)
  32. sb.AppendNull()
  33. sb.Append(true)
  34. f1b.Append(true)
  35. f1vb.AppendValues([]byte("mark"), nil)
  36. f2b.Append(4)
  37. arr := sb.NewArray().(*array.Struct)
  38. defer arr.Release()
  39. fmt.Printf("NullN() = %d\n", arr.NullN())
  40. fmt.Printf("Len() = %d\n", arr.Len())
  41. list := arr.Field(0).(*array.List)
  42. defer list.Release()
  43. offsets := list.Offsets()
  44. varr := list.ListValues().(*array.Uint8)
  45. defer varr.Release()
  46. ints := arr.Field(1).(*array.Int32)
  47. defer ints.Release()
  48. for i := 0; i < arr.Len(); i++ {
  49. if !arr.IsValid(i) {
  50. fmt.Printf("Struct[%d] = (null)\n", i)
  51. continue
  52. }
  53. fmt.Printf("Struct[%d] = [", i)
  54. pos := int(offsets[i])
  55. switch {
  56. case list.IsValid(pos):
  57. fmt.Printf("[")
  58. for j := offsets[i]; j < offsets[i+1]; j++ {
  59. if j != offsets[i] {
  60. fmt.Printf(", ")
  61. }
  62. fmt.Printf("%v", string(varr.Value(int(j))))
  63. }
  64. fmt.Printf("], ")
  65. default:
  66. fmt.Printf("(null), ")
  67. }
  68. fmt.Printf("%d]\n", ints.Value(i))
  69. }
  70. }