Skip to content

Commit 936c311

Browse files
authored
fix: Handling of NULL bytes in Postgresql Text fields (#5249)
Required to fix batch-write-failures due to postgresql TEXT fields containing NULL bytes. ``` 2022-11-30T12:00:11Z ERR failed to execute batch with pgerror error="ERROR: invalid byte sequence for encoding \"UTF8\": 0x00 (SQLSTATE 22021)" module=pg-dest ```
1 parent 1c9755d commit 936c311

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

plugins/destination/postgresql/client/transformer.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package client
22

33
import (
4+
"strings"
5+
46
"github.com/cloudquery/plugin-sdk/schema"
57
"github.com/jackc/pgtype"
68
)
@@ -54,15 +56,15 @@ func (*Client) TransformJSON(v *schema.JSON) interface{} {
5456

5557
func (*Client) TransformText(v *schema.Text) interface{} {
5658
return &pgtype.Text{
57-
String: v.Str,
59+
String: stripNulls(v.Str),
5860
Status: cqStatusToPgStatus[v.Status],
5961
}
6062
}
6163

6264
func (*Client) TransformTextArray(v *schema.TextArray) interface{} {
6365
r := pgtype.TextArray{}
6466
for _, e := range v.Elements {
65-
r.Elements = append(r.Elements, pgtype.Text{String: e.Str, Status: cqStatusToPgStatus[e.Status]})
67+
r.Elements = append(r.Elements, pgtype.Text{String: stripNulls(e.Str), Status: cqStatusToPgStatus[e.Status]})
6668
}
6769
r.Status = cqStatusToPgStatus[v.Status]
6870
for _, d := range v.Dimensions {
@@ -181,3 +183,7 @@ func (c *Client) TransformMacaddrArray(v *schema.MacaddrArray) interface{} {
181183
}
182184
return &r
183185
}
186+
187+
func stripNulls(s string) string {
188+
return strings.ReplaceAll(s, "\x00", "")
189+
}

plugins/destination/sqlite/client/transformer.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package client
22

3-
import "github.com/cloudquery/plugin-sdk/schema"
3+
import (
4+
"strings"
5+
6+
"github.com/cloudquery/plugin-sdk/schema"
7+
)
48

59
func (*Client) TransformBool(v *schema.Bool) interface{} {
610
return v.String()
@@ -27,11 +31,11 @@ func (*Client) TransformJSON(v *schema.JSON) interface{} {
2731
}
2832

2933
func (*Client) TransformText(v *schema.Text) interface{} {
30-
return v.String()
34+
return stripNulls(v.String())
3135
}
3236

3337
func (*Client) TransformTextArray(v *schema.TextArray) interface{} {
34-
return v.String()
38+
return stripNulls(v.String())
3539
}
3640

3741
func (*Client) TransformTimestamptz(v *schema.Timestamptz) interface{} {
@@ -69,3 +73,7 @@ func (*Client) TransformMacaddr(v *schema.Macaddr) interface{} {
6973
func (*Client) TransformMacaddrArray(v *schema.MacaddrArray) interface{} {
7074
return v.String()
7175
}
76+
77+
func stripNulls(s string) string {
78+
return strings.ReplaceAll(s, "\x00", "")
79+
}

0 commit comments

Comments
 (0)