-
Notifications
You must be signed in to change notification settings - Fork 151
/
table.go
172 lines (145 loc) · 4.05 KB
/
table.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package objects
import (
"bytes"
"fmt"
"regexp"
"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/memory"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
// A Table is an object with a schema.
// Schema is the only attribute that the table exposes. To extract records or columns
// one must use the provided functions (getRecord/getColumn).
// TODO(affo): we decided not to expose the schema, for now.
var (
SchemaMonoType = semantic.NewObjectType([]semantic.PropertyType{
{
Key: []byte("label"),
Value: semantic.BasicString,
},
{
Key: []byte("grouped"),
Value: semantic.BasicBool,
},
{
Key: []byte("type"),
Value: semantic.BasicString,
},
})
TableMonoType = semantic.NewObjectType([]semantic.PropertyType{{
Key: []byte("schema"),
Value: semantic.NewArrayType(SchemaMonoType),
}})
)
// Table is a values.Value that represents a table in Flux.
// (Unlike flux.TableObject which represents a stream of tables.)
type Table struct {
flux.BufferedTable
schema values.Array
}
func NewTable(tbl flux.Table) (*Table, error) {
bt, err := execute.CopyTable(tbl)
if err != nil {
return nil, err
}
t := &Table{BufferedTable: bt}
t.schema = values.NewArray(semantic.NewArrayType(SchemaMonoType))
for _, c := range tbl.Cols() {
t.schema.Append(values.NewObjectWithValues(map[string]values.Value{
"label": values.New(c.Label),
"grouped": values.New(tbl.Key().HasCol(c.Label)),
"type": values.New(c.Type.String()),
}))
}
return t, nil
}
func (t *Table) Get(name string) (values.Value, bool) {
// TODO(affo): uncomment this block and remove the return statement once we decide to expose the schema.
/*
if name != "schema" {
return nil, false
}
return t.schema, true
*/
return values.Null, false
}
func (t *Table) Set(name string, v values.Value) {
// immutable
}
func (t *Table) Len() int {
return 1
}
func (t *Table) Range(fn func(name string, v values.Value)) {
fn("schema", t.schema)
}
func (t *Table) Type() semantic.MonoType {
return TableMonoType
}
func (t *Table) IsNull() bool {
return false
}
func (t *Table) Str() string {
panic(values.UnexpectedKind(semantic.Object, semantic.String))
}
func (t *Table) Bytes() []byte {
panic(values.UnexpectedKind(semantic.Object, semantic.Bytes))
}
func (t *Table) Int() int64 {
panic(values.UnexpectedKind(semantic.Object, semantic.Int))
}
func (t *Table) UInt() uint64 {
panic(values.UnexpectedKind(semantic.Object, semantic.UInt))
}
func (t *Table) Float() float64 {
panic(values.UnexpectedKind(semantic.Object, semantic.Float))
}
func (t *Table) Bool() bool {
panic(values.UnexpectedKind(semantic.Object, semantic.Bool))
}
func (t *Table) Time() values.Time {
panic(values.UnexpectedKind(semantic.Object, semantic.Time))
}
func (t *Table) Duration() values.Duration {
panic(values.UnexpectedKind(semantic.Object, semantic.Duration))
}
func (t *Table) Regexp() *regexp.Regexp {
panic(values.UnexpectedKind(semantic.Object, semantic.Regexp))
}
func (t *Table) Array() values.Array {
panic(values.UnexpectedKind(semantic.Object, semantic.Array))
}
func (t *Table) Object() values.Object {
return t
}
func (t *Table) Function() values.Function {
panic(values.UnexpectedKind(semantic.Object, semantic.Function))
}
func (t *Table) Dict() values.Dictionary {
panic(values.UnexpectedKind(semantic.Object, semantic.Dictionary))
}
// Table returns a copy of the Table that can be called
// with Do. Either Do or Done must be called on the
// returned Table.
func (t *Table) Table() flux.Table {
return t.Copy()
}
func (t *Table) Equal(other values.Value) bool {
ot, ok := other.(*Table)
if !ok || !t.schema.Equal(ot.schema) {
return false
}
eq, err := execute.TablesEqual(t.BufferedTable, ot.BufferedTable, &memory.Allocator{})
if err != nil || !eq {
return false
}
return true
}
func (t *Table) String() string {
w := bytes.NewBuffer([]byte{})
if _, err := execute.NewFormatter(t, nil).WriteTo(w); err != nil {
return fmt.Sprintf("error while formatting table: %v", err)
}
return w.String()
}