@@ -19,14 +19,21 @@ func init() {
19
19
}
20
20
}
21
21
22
- func write (ctx context.Context , client ceresdb.Client , ts int64 ) error {
22
+ func write (ctx context.Context , client ceresdb.Client , ts int64 , addNewColumn bool ) error {
23
23
points := make ([]ceresdb.Point , 0 , 2 )
24
24
for i := 0 ; i < 2 ; i ++ {
25
- point , err := ceresdb .NewPointBuilder (table ).
25
+ builder := ceresdb .NewPointBuilder (table ).
26
26
SetTimestamp (ts ).
27
27
AddTag ("name" , ceresdb .NewStringValue (fmt .Sprintf ("tag-%d" , i ))).
28
- AddField ("value" , ceresdb .NewInt64Value (int64 (i ))).
29
- Build ()
28
+ AddField ("value" , ceresdb .NewInt64Value (int64 (i )))
29
+
30
+ if addNewColumn {
31
+ builder = builder .AddTag ("new_tag" , ceresdb .NewStringValue (fmt .Sprintf ("new-tag-%d" , i ))).
32
+ AddField ("new_field" , ceresdb .NewInt64Value (int64 (i )))
33
+ }
34
+
35
+ point , err := builder .Build ()
36
+
30
37
if err != nil {
31
38
return err
32
39
}
@@ -57,10 +64,10 @@ func ensureRow(expectedVals []ceresdb.Value, actualRow []ceresdb.Column) error {
57
64
58
65
}
59
66
60
- func query (ctx context.Context , client ceresdb.Client , ts int64 ) error {
67
+ func query (ctx context.Context , client ceresdb.Client , ts int64 , addNewColumn bool ) error {
61
68
resp , err := client .SQLQuery (ctx , ceresdb.SQLQueryRequest {
62
69
Tables : []string {table },
63
- SQL : fmt .Sprintf ("select * from %s where timestamp = %d" , table , ts ),
70
+ SQL : fmt .Sprintf ("select * from %s where timestamp = %d order by name " , table , ts ),
64
71
})
65
72
if err != nil {
66
73
return err
@@ -70,21 +77,32 @@ func query(ctx context.Context, client ceresdb.Client, ts int64) error {
70
77
return fmt .Errorf ("expect 2 rows, current: %+v" , len (resp .Rows ))
71
78
}
72
79
73
- if err := ensureRow ( []ceresdb.Value {
80
+ row0 := []ceresdb.Value {
74
81
ceresdb .NewUint64Value (4024844655630594205 ),
75
82
ceresdb .NewInt64Value (ts ),
76
83
ceresdb .NewStringValue ("tag-0" ),
77
- ceresdb .NewInt64Value (0 ),
78
- }, resp .Rows [0 ].Columns ()); err != nil {
79
- return err
80
- }
84
+ ceresdb .NewInt64Value (0 )}
81
85
82
- return ensureRow ( []ceresdb.Value {
86
+ row1 := []ceresdb.Value {
83
87
ceresdb .NewUint64Value (14230010170561829440 ),
84
88
ceresdb .NewInt64Value (ts ),
85
89
ceresdb .NewStringValue ("tag-1" ),
86
90
ceresdb .NewInt64Value (1 ),
87
- }, resp .Rows [1 ].Columns ())
91
+ }
92
+
93
+ if addNewColumn {
94
+ row0 [0 ] = ceresdb .NewUint64Value (8341999341185504339 )
95
+ row1 [0 ] = ceresdb .NewUint64Value (4452331151453582498 )
96
+ row0 = append (row0 , ceresdb .NewInt64Value (0 ), ceresdb .NewStringValue ("new-tag-0" ))
97
+ row1 = append (row1 , ceresdb .NewInt64Value (1 ), ceresdb .NewStringValue ("new-tag-1" ))
98
+ }
99
+
100
+ if err := ensureRow (row0 ,
101
+ resp .Rows [0 ].Columns ()); err != nil {
102
+ return err
103
+ }
104
+
105
+ return ensureRow (row1 , resp .Rows [1 ].Columns ())
88
106
}
89
107
90
108
func ddl (ctx context.Context , client ceresdb.Client , sql string ) (uint32 , error ) {
@@ -99,6 +117,48 @@ func ddl(ctx context.Context, client ceresdb.Client, sql string) (uint32, error)
99
117
return resp .AffectedRows , nil
100
118
}
101
119
120
+ func checkAutoCreateTable (ctx context.Context , client ceresdb.Client ) error {
121
+ if _ , err := ddl (ctx , client , "drop table if exists " + table ); err != nil {
122
+ return err
123
+ }
124
+
125
+ ts := currentMS ()
126
+ if err := write (ctx , client , ts , false ); err != nil {
127
+ return err
128
+ }
129
+
130
+ if err := query (ctx , client , ts , false ); err != nil {
131
+ return err
132
+ }
133
+
134
+ return nil
135
+ }
136
+
137
+ func checkAutoAddColumns (ctx context.Context , client ceresdb.Client ) error {
138
+ ts := currentMS ()
139
+ if err := write (ctx , client , ts , true ); err != nil {
140
+ return err
141
+ }
142
+
143
+ if err := query (ctx , client , ts , true ); err != nil {
144
+ return err
145
+ }
146
+
147
+ return nil
148
+ }
149
+
150
+ func dropTable (ctx context.Context , client ceresdb.Client ) error {
151
+ affected , err := ddl (ctx , client , "drop table " + table )
152
+ if err != nil {
153
+ return err
154
+ }
155
+
156
+ if affected != 0 {
157
+ panic (fmt .Sprintf ("drop table expected 0, actual is %d" , affected ))
158
+ }
159
+ return nil
160
+ }
161
+
102
162
func main () {
103
163
fmt .Printf ("Begin test, endpoint %s...\n " , endpoint )
104
164
@@ -110,28 +170,18 @@ func main() {
110
170
}
111
171
112
172
ctx := context .TODO ()
113
- if _ , err := ddl (ctx , client , "drop table if exists " + table ); err != nil {
114
- panic (err )
115
- }
116
-
117
- ts := currentMS ()
118
- if err := write (ctx , client , ts ); err != nil {
173
+ if err = checkAutoCreateTable (ctx , client ); err != nil {
119
174
panic (err )
120
175
}
121
176
122
- if err := query (ctx , client , ts ); err != nil {
177
+ if err = checkAutoAddColumns (ctx , client ); err != nil {
123
178
panic (err )
124
179
}
125
180
126
- affected , err := ddl (ctx , client , "drop table " + table )
127
- if err != nil {
181
+ if err = dropTable (ctx , client ); err != nil {
128
182
panic (err )
129
183
}
130
184
131
- if affected != 0 {
132
- panic (fmt .Sprintf ("drop table expected 0, actual is %d" , affected ))
133
- }
134
-
135
185
fmt .Println ("Test done" )
136
186
}
137
187
0 commit comments