Skip to content

Commit c2e84c3

Browse files
authored
feat: Add arrow support for timestamp and bytea (#724)
This should unblock cloudquery/filetypes#87 Add supports for timestamp to decode arrow string and for byte to decode base64 (backward compat is saved)
1 parent e675cf0 commit c2e84c3

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

schema/bytea.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package schema
33

44
import (
55
"bytes"
6+
"encoding/base64"
67
"encoding/hex"
78
)
89

@@ -41,7 +42,7 @@ func (dst *Bytea) Equal(src CQType) bool {
4142

4243
func (dst *Bytea) String() string {
4344
if dst.Status == Present {
44-
return hex.EncodeToString(dst.Bytes)
45+
return base64.StdEncoding.EncodeToString(dst.Bytes)
4546
} else {
4647
return ""
4748
}
@@ -72,7 +73,11 @@ func (dst *Bytea) Set(src any) error {
7273
b := make([]byte, hex.DecodedLen(len(value)))
7374
_, err := hex.Decode(b, []byte(value))
7475
if err != nil {
75-
return &ValidationError{Type: TypeByteArray, Msg: "hex decode failed", Err: err, Value: value}
76+
b = make([]byte, base64.StdEncoding.DecodedLen(len(value)))
77+
_, err := base64.StdEncoding.Decode(b, []byte(value))
78+
if err != nil {
79+
return &ValidationError{Type: TypeByteArray, Msg: "base64 and hex decode failed", Err: err, Value: value}
80+
}
7681
}
7782
*dst = Bytea{Status: Present, Bytes: b}
7883
} else {

schema/resource_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ var calculateCQIDPrimaryKeyTestCases = []struct {
438438
"MacAddressValue": "aa:bb:cc:dd:ee:ff",
439439
"MacAddressArrayValue": []string{"aa:bb:cc:dd:ee:ff", "11:22:33:44:55:66"},
440440
},
441-
ExpectedValue: &UUID{Bytes: [16]uint8{0xa6, 0x76, 0x76, 0xfb, 0x4c, 0x95, 0x51, 0x2d, 0x9e, 0xcd, 0xf4, 0xc4, 0xae, 0xc1, 0x2a, 0xf5}, Status: 0x2},
441+
ExpectedValue: &UUID{Bytes: [16]uint8{0x82, 0xaa, 0xa2, 0xc7, 0x75, 0x10, 0x5c, 0x6d, 0xb8, 0xef, 0x18, 0x49, 0x33, 0x99, 0x4a, 0x9d}, Status: 0x2},
442442
DeterministicCQID: true,
443443
},
444444
}

schema/timestamptz.go

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import (
1414
// this is the default format used by time.Time.String()
1515
const defaultStringFormat = "2006-01-02 15:04:05.999999999 -0700 MST"
1616

17+
// this is used by arrow string format (time is in UTC)
18+
const arrowStringFormat = "2006-01-02 15:04:05.999999999"
19+
1720
// const microsecFromUnixEpochToY2K = 946684800 * 1000000
1821

1922
const (
@@ -170,6 +173,11 @@ func (dst *Timestamptz) DecodeText(src []byte) error {
170173
*dst = Timestamptz{Time: tim.UTC(), Status: Present}
171174
return nil
172175
}
176+
tim, err = time.Parse(arrowStringFormat, sbuf)
177+
if err == nil {
178+
*dst = Timestamptz{Time: tim.UTC(), Status: Present}
179+
return nil
180+
}
173181
return &ValidationError{Type: TypeTimestamp, Msg: "cannot parse timestamp", Value: sbuf, Err: err}
174182
}
175183

testdata/testdata.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func GenTestData(table *schema.Table) schema.CQTypes {
183183
data[i] = uuidArrayColumn
184184
case schema.TypeInet:
185185
inetColumn := &schema.Inet{}
186-
if err := inetColumn.Set("192.0.2.1/24"); err != nil {
186+
if err := inetColumn.Set("192.0.2.0/24"); err != nil {
187187
panic(err)
188188
}
189189
data[i] = inetColumn

0 commit comments

Comments
 (0)