-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatypes.go
112 lines (98 loc) · 2.72 KB
/
datatypes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package bonobo
import (
"fmt"
"github.com/substrait-io/substrait-go/v3/proto"
"github.com/substrait-io/substrait-go/v3/types"
)
type Type types.Type
type Field struct {
Name string
Type Type
}
func (f Field) String() string {
return fmt.Sprintf("%s::%s", f.Name, f.Type)
}
type Schema types.NamedStruct
func NewSchema(fields []Field) *Schema {
fieldNames := make([]string, len(fields))
fieldTypes := make([]types.Type, len(fields))
for i, field := range fields {
fieldNames[i] = field.Name
fieldTypes[i] = field.Type
}
return &Schema{
Names: fieldNames,
Struct: types.StructType{
Nullability: types.NullabilityRequired,
Types: fieldTypes,
},
}
}
func NewSchemaFromProto(n *proto.NamedStruct) *Schema {
schema := Schema(types.NewNamedStructFromProto(n))
return &schema
}
func (s *Schema) Fields() []Field {
fields := make([]Field, len(s.Names))
for i := range fields {
typ := s.Struct.Types[i]
fields[i] = Field{Name: s.Names[i], Type: typ}
}
return fields
}
func (s *Schema) Len() int {
return len(s.Names)
}
func (s *Schema) String() string {
return (*types.NamedStruct)(s).String()
}
var Types = struct {
BooleanType func(nullable bool) Type
Int8Type func(nullable bool) Type
Int16Type func(nullable bool) Type
Int32Type func(nullable bool) Type
Int64Type func(nullable bool) Type
FloatType func(nullable bool) Type
DoubleType func(nullable bool) Type
DateType func(nullable bool) Type
StringType func(nullable bool) Type
DecimalType func(p, s int32, nullable bool) Type
}{
BooleanType: func(nullable bool) Type {
return withNullability(&types.BooleanType{}, nullable)
},
Int8Type: func(nullable bool) Type {
return withNullability(&types.Int8Type{}, nullable)
},
Int16Type: func(nullable bool) Type {
return withNullability(&types.Int16Type{}, nullable)
},
Int32Type: func(nullable bool) Type {
return withNullability(&types.Int32Type{}, nullable)
},
Int64Type: func(nullable bool) Type {
return withNullability(&types.Int64Type{}, nullable)
},
FloatType: func(nullable bool) Type {
return withNullability(&types.Float32Type{}, nullable)
},
DoubleType: func(nullable bool) Type {
return withNullability(&types.Float64Type{}, nullable)
},
DateType: func(nullable bool) Type {
return withNullability(&types.DateType{}, nullable)
},
StringType: func(nullable bool) Type {
return withNullability(&types.StringType{}, nullable)
},
DecimalType: func(p, s int32, nullable bool) Type {
return withNullability(&types.DecimalType{Precision: p, Scale: s}, nullable)
},
}
func withNullability(typ Type, nullable bool) Type {
nullability := types.NullabilityRequired
if nullable {
nullability = types.NullabilityNullable
}
return typ.WithNullability(nullability)
}