Skip to content

Commit 4341368

Browse files
authored
Support new data types (#107)
1 parent bb812b8 commit 4341368

File tree

12 files changed

+551
-172
lines changed

12 files changed

+551
-172
lines changed

client/field.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package client
2121

22+
import "time"
23+
2224
type Field struct {
2325
dataType TSDataType
2426
name string
@@ -89,6 +91,10 @@ func (f *Field) GetText() string {
8991
return float64ToString(f.value.(float64))
9092
case string:
9193
return f.value.(string)
94+
case []byte:
95+
return bytesToHexString(f.value.([]byte))
96+
case time.Time:
97+
return f.value.(time.Time).Format("2006-01-02")
9298
}
9399
return ""
94100
}

client/field_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package client
2222
import (
2323
"reflect"
2424
"testing"
25+
"time"
2526
)
2627

2728
func TestField_IsNull(t *testing.T) {
@@ -126,6 +127,38 @@ func TestField_GetDataType(t *testing.T) {
126127
value: nil,
127128
},
128129
want: TEXT,
130+
}, {
131+
name: "GetDataType-STRING",
132+
fields: fields{
133+
dataType: STRING,
134+
name: "",
135+
value: nil,
136+
},
137+
want: STRING,
138+
}, {
139+
name: "GetDataType-BLOB",
140+
fields: fields{
141+
dataType: BLOB,
142+
name: "",
143+
value: nil,
144+
},
145+
want: BLOB,
146+
}, {
147+
name: "GetDataType-TIMESTAMP",
148+
fields: fields{
149+
dataType: TIMESTAMP,
150+
name: "",
151+
value: nil,
152+
},
153+
want: TIMESTAMP,
154+
}, {
155+
name: "GetDataType-DATE",
156+
fields: fields{
157+
dataType: DATE,
158+
name: "",
159+
value: nil,
160+
},
161+
want: DATE,
129162
},
130163
}
131164
for _, tt := range tests {
@@ -201,6 +234,38 @@ func TestField_GetValue(t *testing.T) {
201234
value: "TEXT",
202235
},
203236
want: "TEXT",
237+
}, {
238+
name: "GetValue-STRING",
239+
fields: fields{
240+
dataType: STRING,
241+
name: "",
242+
value: "STRING",
243+
},
244+
want: "STRING",
245+
}, {
246+
name: "GetValue-BLOB",
247+
fields: fields{
248+
dataType: BLOB,
249+
name: "",
250+
value: []byte("BLOB"),
251+
},
252+
want: []byte("BLOB"),
253+
}, {
254+
name: "GetValue-TIMESTAMP",
255+
fields: fields{
256+
dataType: TIMESTAMP,
257+
name: "",
258+
value: int64(65535),
259+
},
260+
want: int64(65535),
261+
}, {
262+
name: "GetValue-DATE",
263+
fields: fields{
264+
dataType: DATE,
265+
name: "",
266+
value: time.Date(2024, time.Month(4), 1, 0, 0, 0, 0, time.UTC),
267+
},
268+
want: time.Date(2024, time.Month(4), 1, 0, 0, 0, 0, time.UTC),
204269
},
205270
}
206271
for _, tt := range tests {
@@ -408,6 +473,30 @@ func TestField_GetText(t *testing.T) {
408473
value: int32(1),
409474
},
410475
want: "1",
476+
}, {
477+
name: "GetText-04",
478+
fields: fields{
479+
dataType: STRING,
480+
name: "",
481+
value: "STRING",
482+
},
483+
want: "STRING",
484+
}, {
485+
name: "GetText-05",
486+
fields: fields{
487+
dataType: BLOB,
488+
name: "",
489+
value: []byte("BLOB"),
490+
},
491+
want: "0x424c4f42",
492+
}, {
493+
name: "GetText-06",
494+
fields: fields{
495+
dataType: DATE,
496+
name: "",
497+
value: time.Date(2024, time.Month(4), 1, 0, 0, 0, 0, time.UTC),
498+
},
499+
want: "2024-04-01",
411500
},
412501
}
413502
for _, tt := range tests {

client/protocol.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,42 @@ type TSEncoding uint8
2626
type TSCompressionType uint8
2727

2828
const (
29-
UNKNOWN TSDataType = -1
30-
BOOLEAN TSDataType = 0
31-
INT32 TSDataType = 1
32-
INT64 TSDataType = 2
33-
FLOAT TSDataType = 3
34-
DOUBLE TSDataType = 4
35-
TEXT TSDataType = 5
29+
UNKNOWN TSDataType = -1
30+
BOOLEAN TSDataType = 0
31+
INT32 TSDataType = 1
32+
INT64 TSDataType = 2
33+
FLOAT TSDataType = 3
34+
DOUBLE TSDataType = 4
35+
TEXT TSDataType = 5
36+
TIMESTAMP TSDataType = 8
37+
DATE TSDataType = 9
38+
BLOB TSDataType = 10
39+
STRING TSDataType = 11
3640
)
3741

3842
const (
39-
PLAIN TSEncoding = 0
40-
PLAIN_DICTIONARY TSEncoding = 1
41-
RLE TSEncoding = 2
42-
DIFF TSEncoding = 3
43-
TS_2DIFF TSEncoding = 4
44-
BITMAP TSEncoding = 5
45-
GORILLA_V1 TSEncoding = 6
46-
REGULAR TSEncoding = 7
47-
GORILLA TSEncoding = 8
48-
ZIGZAG TSEncoding = 9
49-
FREQ TSEncoding = 10
50-
CHIMP TSEncoding = 11
51-
SPRINTZ TSEncoding = 12
52-
RLBE TSEncoding = 13
43+
PLAIN TSEncoding = 0
44+
DICTIONARY TSEncoding = 1
45+
RLE TSEncoding = 2
46+
DIFF TSEncoding = 3
47+
TS_2DIFF TSEncoding = 4
48+
BITMAP TSEncoding = 5
49+
GORILLA_V1 TSEncoding = 6
50+
REGULAR TSEncoding = 7
51+
GORILLA TSEncoding = 8
52+
ZIGZAG TSEncoding = 9
53+
FREQ TSEncoding = 10
54+
CHIMP TSEncoding = 11
55+
SPRINTZ TSEncoding = 12
56+
RLBE TSEncoding = 13
5357
)
5458

5559
const (
5660
UNCOMPRESSED TSCompressionType = 0
5761
SNAPPY TSCompressionType = 1
5862
GZIP TSCompressionType = 2
59-
LZO TSCompressionType = 3
60-
SDT TSCompressionType = 4
61-
PAA TSCompressionType = 5
62-
PLA TSCompressionType = 6
6363
LZ4 TSCompressionType = 7
64-
ZSTD TSCompressionType = 8
64+
ZSTD TSCompressionType = 8
6565
LZMA2 TSCompressionType = 9
6666
)
6767

@@ -201,4 +201,4 @@ const (
201201
CqAlreadyActive int32 = 1401
202202
CqAlreadyExist int32 = 1402
203203
CqUpdateLastExecTimeError int32 = 1403
204-
)
204+
)

client/rpcdataset.go

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ const (
3939
var (
4040
errClosed error = errors.New("DataSet is Closed")
4141
tsTypeMap map[string]TSDataType = map[string]TSDataType{
42-
"BOOLEAN": BOOLEAN,
43-
"INT32": INT32,
44-
"INT64": INT64,
45-
"FLOAT": FLOAT,
46-
"DOUBLE": DOUBLE,
47-
"TEXT": TEXT,
42+
"BOOLEAN": BOOLEAN,
43+
"INT32": INT32,
44+
"INT64": INT64,
45+
"FLOAT": FLOAT,
46+
"DOUBLE": DOUBLE,
47+
"TEXT": TEXT,
48+
"TIMESTAMP": TIMESTAMP,
49+
"DATE": DATE,
50+
"BLOB": BLOB,
51+
"STRING": STRING,
4852
}
4953
)
5054

@@ -116,10 +120,10 @@ func (s *IoTDBRpcDataSet) constructOneRow() error {
116120
case BOOLEAN:
117121
s.values[i] = valueBuffer[:1]
118122
s.queryDataSet.ValueList[i] = valueBuffer[1:]
119-
case INT32:
123+
case INT32, DATE:
120124
s.values[i] = valueBuffer[:4]
121125
s.queryDataSet.ValueList[i] = valueBuffer[4:]
122-
case INT64:
126+
case INT64, TIMESTAMP:
123127
s.values[i] = valueBuffer[:8]
124128
s.queryDataSet.ValueList[i] = valueBuffer[8:]
125129
case FLOAT:
@@ -128,7 +132,7 @@ func (s *IoTDBRpcDataSet) constructOneRow() error {
128132
case DOUBLE:
129133
s.values[i] = valueBuffer[:8]
130134
s.queryDataSet.ValueList[i] = valueBuffer[8:]
131-
case TEXT:
135+
case TEXT, BLOB, STRING:
132136
length := bytesToInt32(valueBuffer[:4])
133137
s.values[i] = valueBuffer[4 : 4+length]
134138
s.queryDataSet.ValueList[i] = valueBuffer[4+length:]
@@ -178,16 +182,24 @@ func (s *IoTDBRpcDataSet) getString(columnIndex int, dataType TSDataType) string
178182
return "false"
179183
case INT32:
180184
return int32ToString(bytesToInt32(valueBytes))
181-
case INT64:
185+
case INT64, TIMESTAMP:
182186
return int64ToString(bytesToInt64(valueBytes))
183187
case FLOAT:
184188
bits := binary.BigEndian.Uint32(valueBytes)
185189
return float32ToString(math.Float32frombits(bits))
186190
case DOUBLE:
187191
bits := binary.BigEndian.Uint64(valueBytes)
188192
return float64ToString(math.Float64frombits(bits))
189-
case TEXT:
193+
case TEXT, STRING:
190194
return string(valueBytes)
195+
case BLOB:
196+
return bytesToHexString(valueBytes)
197+
case DATE:
198+
date, err := bytesToDate(valueBytes)
199+
if err != nil {
200+
return ""
201+
}
202+
return date.Format("2006-01-02")
191203
default:
192204
return ""
193205
}
@@ -206,19 +218,27 @@ func (s *IoTDBRpcDataSet) getValue(columnName string) interface{} {
206218
valueBytes := s.values[columnIndex]
207219
switch dataType {
208220
case BOOLEAN:
209-
return bool(valueBytes[0] != 0)
221+
return valueBytes[0] != 0
210222
case INT32:
211223
return bytesToInt32(valueBytes)
212-
case INT64:
224+
case INT64, TIMESTAMP:
213225
return bytesToInt64(valueBytes)
214226
case FLOAT:
215227
bits := binary.BigEndian.Uint32(valueBytes)
216228
return math.Float32frombits(bits)
217229
case DOUBLE:
218230
bits := binary.BigEndian.Uint64(valueBytes)
219231
return math.Float64frombits(bits)
220-
case TEXT:
232+
case TEXT, STRING:
221233
return string(valueBytes)
234+
case BLOB:
235+
return valueBytes
236+
case DATE:
237+
date, err := bytesToDate(valueBytes)
238+
if err != nil {
239+
return nil
240+
}
241+
return date
222242
default:
223243
return nil
224244
}
@@ -281,7 +301,7 @@ func (s *IoTDBRpcDataSet) scan(dest ...interface{}) error {
281301
case BOOLEAN:
282302
switch t := d.(type) {
283303
case *bool:
284-
*t = bool(valueBytes[0] != 0)
304+
*t = valueBytes[0] != 0
285305
case *string:
286306
if valueBytes[0] != 0 {
287307
*t = "true"
@@ -301,7 +321,7 @@ func (s *IoTDBRpcDataSet) scan(dest ...interface{}) error {
301321
default:
302322
return fmt.Errorf("dest[%d] types must be *int32 or *string", i)
303323
}
304-
case INT64:
324+
case INT64, TIMESTAMP:
305325
switch t := d.(type) {
306326
case *int64:
307327
*t = bytesToInt64(valueBytes)
@@ -332,12 +352,37 @@ func (s *IoTDBRpcDataSet) scan(dest ...interface{}) error {
332352
default:
333353
return fmt.Errorf("dest[%d] types must be *float64 or *string", i)
334354
}
335-
case TEXT:
355+
case TEXT, STRING:
336356
switch t := d.(type) {
357+
case *[]byte:
358+
*t = valueBytes
337359
case *string:
338360
*t = string(valueBytes)
339361
default:
340-
return fmt.Errorf("dest[%d] types must be *string", i)
362+
return fmt.Errorf("dest[%d] types must be *[]byte or *string", i)
363+
}
364+
case BLOB:
365+
switch t := d.(type) {
366+
case *[]byte:
367+
*t = valueBytes
368+
case *string:
369+
*t = bytesToHexString(valueBytes)
370+
default:
371+
return fmt.Errorf("dest[%d] types must be *[]byte or *string", i)
372+
}
373+
case DATE:
374+
switch t := d.(type) {
375+
case *time.Time:
376+
*t, _ = bytesToDate(valueBytes)
377+
case *string:
378+
*t = int32ToString(bytesToInt32(valueBytes))
379+
date, err := bytesToDate(valueBytes)
380+
if err != nil {
381+
*t = ""
382+
}
383+
*t = date.Format("2006-01-02")
384+
default:
385+
return fmt.Errorf("dest[%d] types must be *time.Time or *string", i)
341386
}
342387
default:
343388
return nil
@@ -412,7 +457,7 @@ func (s *IoTDBRpcDataSet) hasCachedResults() bool {
412457
if s.closed {
413458
return false
414459
}
415-
return (s.queryDataSet != nil && len(s.queryDataSet.Time) > 0)
460+
return s.queryDataSet != nil && len(s.queryDataSet.Time) > 0
416461
}
417462

418463
func (s *IoTDBRpcDataSet) next() (bool, error) {

0 commit comments

Comments
 (0)