array.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. lb := array.NewListBuilder(pool, arrow.PrimitiveTypes.Int64)
  11. defer lb.Release()
  12. vb := lb.ValueBuilder().(*array.Int64Builder)
  13. defer vb.Release()
  14. vb.Reserve(10)
  15. lb.Append(true)
  16. vb.Append(0)
  17. vb.Append(1)
  18. vb.Append(2)
  19. lb.AppendNull()
  20. lb.Append(true)
  21. vb.Append(3)
  22. lb.Append(true)
  23. vb.Append(4)
  24. vb.Append(5)
  25. lb.Append(true)
  26. vb.Append(6)
  27. vb.Append(7)
  28. vb.Append(8)
  29. lb.AppendNull()
  30. lb.Append(true)
  31. vb.Append(9)
  32. arr := lb.NewArray().(*array.List)
  33. defer arr.Release()
  34. fmt.Printf("NullN() = %d\n", arr.NullN())
  35. fmt.Printf("Len() = %d\n", arr.Len())
  36. fmt.Printf("Offsets() = %v\n", arr.Offsets())
  37. offsets := arr.Offsets()[1:]
  38. varr := arr.ListValues().(*array.Int64)
  39. pos := 0
  40. for i := 0; i < arr.Len(); i++ {
  41. if !arr.IsValid(i) {
  42. fmt.Printf("List[%d] = (null)\n", i)
  43. continue
  44. }
  45. fmt.Printf("List[%d] = [", i)
  46. for j := pos; j < int(offsets[i]); j++ {
  47. if j != pos {
  48. fmt.Printf(", ")
  49. }
  50. fmt.Printf("%v", varr.Value(j))
  51. }
  52. pos = int(offsets[i])
  53. fmt.Printf("]\n")
  54. }
  55. fmt.Printf("List = %v\n", arr)
  56. }