|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "strings" |
| 6 | + "unicode" |
| 7 | + "unicode/utf8" |
| 8 | + |
| 9 | + "github.com/segmentio/parquet-go" |
| 10 | + "github.com/segmentio/parquet-go/compress" |
| 11 | + "github.com/segmentio/parquet-go/deprecated" |
| 12 | + "github.com/segmentio/parquet-go/encoding" |
| 13 | + "github.com/segmentio/parquet-go/format" |
| 14 | +) |
| 15 | + |
| 16 | +type Group []*groupField |
| 17 | + |
| 18 | +func (g Group) String() string { |
| 19 | + s := new(strings.Builder) |
| 20 | + parquet.PrintSchema(s, "", g) |
| 21 | + return s.String() |
| 22 | +} |
| 23 | + |
| 24 | +func (g Group) Type() parquet.Type { return &groupType{} } |
| 25 | + |
| 26 | +func (g Group) Optional() bool { return false } |
| 27 | + |
| 28 | +func (g Group) Repeated() bool { return false } |
| 29 | + |
| 30 | +func (g Group) Required() bool { return true } |
| 31 | + |
| 32 | +func (g Group) Leaf() bool { return false } |
| 33 | + |
| 34 | +func (g Group) Fields() []parquet.Field { |
| 35 | + fields := make([]parquet.Field, len(g)) |
| 36 | + for pos := range g { |
| 37 | + fields[pos] = g[pos] |
| 38 | + } |
| 39 | + return fields |
| 40 | +} |
| 41 | + |
| 42 | +func (g Group) Encoding() encoding.Encoding { return nil } |
| 43 | + |
| 44 | +func (g Group) Compression() compress.Codec { return nil } |
| 45 | + |
| 46 | +func (g Group) GoType() reflect.Type { return goTypeOfGroup(g) } |
| 47 | + |
| 48 | +func exportedStructFieldName(name string) string { |
| 49 | + firstRune, size := utf8.DecodeRuneInString(name) |
| 50 | + return string([]rune{unicode.ToUpper(firstRune)}) + name[size:] |
| 51 | +} |
| 52 | + |
| 53 | +func goTypeOfGroup(node parquet.Node) reflect.Type { |
| 54 | + fields := node.Fields() |
| 55 | + structFields := make([]reflect.StructField, len(fields)) |
| 56 | + for i, field := range fields { |
| 57 | + structFields[i].Name = exportedStructFieldName(field.Name()) |
| 58 | + structFields[i].Type = field.GoType() |
| 59 | + // TODO: can we reconstruct a struct tag that would be valid if a value |
| 60 | + // of this type were passed to SchemaOf? |
| 61 | + } |
| 62 | + return reflect.StructOf(structFields) |
| 63 | +} |
| 64 | + |
| 65 | +type groupField struct { |
| 66 | + parquet.Node |
| 67 | + name string |
| 68 | +} |
| 69 | + |
| 70 | +type groupType struct{} |
| 71 | + |
| 72 | +func (groupType) String() string { return "group" } |
| 73 | + |
| 74 | +func (groupType) Kind() parquet.Kind { |
| 75 | + panic("cannot call Kind on parquet group") |
| 76 | +} |
| 77 | + |
| 78 | +func (groupType) Compare(parquet.Value, parquet.Value) int { |
| 79 | + panic("cannot compare values on parquet group") |
| 80 | +} |
| 81 | + |
| 82 | +func (groupType) NewColumnIndexer(int) parquet.ColumnIndexer { |
| 83 | + panic("cannot create column indexer from parquet group") |
| 84 | +} |
| 85 | + |
| 86 | +func (groupType) NewDictionary(int, int, []byte) parquet.Dictionary { |
| 87 | + panic("cannot create dictionary from parquet group") |
| 88 | +} |
| 89 | + |
| 90 | +func (t groupType) NewColumnBuffer(int, int) parquet.ColumnBuffer { |
| 91 | + panic("cannot create column buffer from parquet group") |
| 92 | +} |
| 93 | + |
| 94 | +func (t groupType) NewPage(int, int, []byte) parquet.Page { |
| 95 | + panic("cannot create page from parquet group") |
| 96 | +} |
| 97 | + |
| 98 | +func (groupType) Encode(_, _ []byte, _ encoding.Encoding) ([]byte, error) { |
| 99 | + panic("cannot encode parquet group") |
| 100 | +} |
| 101 | + |
| 102 | +func (groupType) Decode(_, _ []byte, _ encoding.Encoding) ([]byte, error) { |
| 103 | + panic("cannot decode parquet group") |
| 104 | +} |
| 105 | + |
| 106 | +func (groupType) Length() int { return 0 } |
| 107 | + |
| 108 | +func (groupType) EstimateSize(int) int64 { return 0 } |
| 109 | + |
| 110 | +func (groupType) ColumnOrder() *format.ColumnOrder { return nil } |
| 111 | + |
| 112 | +func (groupType) PhysicalType() *format.Type { return nil } |
| 113 | + |
| 114 | +func (groupType) LogicalType() *format.LogicalType { return nil } |
| 115 | + |
| 116 | +func (groupType) ConvertedType() *deprecated.ConvertedType { return nil } |
| 117 | + |
| 118 | +func (f *groupField) Name() string { return f.name } |
| 119 | + |
| 120 | +func (f *groupField) Value(base reflect.Value) reflect.Value { |
| 121 | + return base.MapIndex(reflect.ValueOf(&f.name).Elem()) |
| 122 | +} |
| 123 | + |
| 124 | +func ProfilesSchema() *parquet.Schema { |
| 125 | + stringRef := parquet.Encoded(parquet.Int(64), &parquet.DeltaBinaryPacked) |
| 126 | + sampleType := parquet.Group{ |
| 127 | + "Type": stringRef, |
| 128 | + "Unit": stringRef, |
| 129 | + } |
| 130 | + |
| 131 | + externalLabels := parquet.Repeated(Group{ |
| 132 | + {name: "Name", Node: stringRef}, |
| 133 | + {name: "Value", Node: stringRef}, |
| 134 | + }) |
| 135 | + |
| 136 | + pprofLabels := parquet.Repeated(Group{ |
| 137 | + {name: "Key", Node: stringRef}, |
| 138 | + {name: "Str", Node: parquet.Optional(stringRef)}, |
| 139 | + {name: "Num", Node: parquet.Optional(parquet.Int(64))}, |
| 140 | + {name: "NumUnit", Node: parquet.Optional(stringRef)}, |
| 141 | + }) |
| 142 | + |
| 143 | + s := parquet.NewSchema("Profile", Group{ |
| 144 | + {name: "ID", Node: parquet.UUID()}, |
| 145 | + {name: "ExternalLabels", Node: externalLabels}, |
| 146 | + {name: "Types", Node: parquet.Repeated(sampleType)}, |
| 147 | + {name: "Samples", Node: parquet.Repeated(Group{ |
| 148 | + {name: "LocationIds", Node: parquet.Repeated(parquet.Uint(64))}, |
| 149 | + {name: "Values", Node: parquet.Repeated(parquet.Encoded(parquet.Int(64), &parquet.DeltaBinaryPacked))}, |
| 150 | + {name: "Labels", Node: pprofLabels}, |
| 151 | + })}, |
| 152 | + {name: "DropFrames", Node: stringRef}, |
| 153 | + {name: "KeepFrames", Node: stringRef}, |
| 154 | + {name: "TimeNanos", Node: parquet.Timestamp(parquet.Nanosecond)}, |
| 155 | + {name: "DurationNanos", Node: parquet.Int(64)}, |
| 156 | + {name: "PeriodType", Node: parquet.Optional(sampleType)}, |
| 157 | + {name: "Period", Node: parquet.Int(64)}, |
| 158 | + {name: "Comments", Node: parquet.Repeated(stringRef)}, |
| 159 | + {name: "DefaultSampleType", Node: parquet.Int(64)}, |
| 160 | + }) |
| 161 | + return s |
| 162 | +} |
0 commit comments