Skip to content

Commit 9ba03aa

Browse files
fix: Use PostgreSQL hex format for bytea literals (fixes #32) (#71)
Replace invalid b"..." format with PostgreSQL-compatible '\x...' hex format for bytea literals. The old format caused syntax errors when executing queries with byte values. Changes: - Add encoding/hex import to cel2sql.go - Update bytea literal generation to use '\x' + hex encoding - Remove unused bytesToOctets function from utils.go - Update test expectations to match new hex format 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6952938 commit 9ba03aa

File tree

4 files changed

+6
-17
lines changed

4 files changed

+6
-17
lines changed

cel2sql.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cel2sql
33

44
import (
55
"context"
6+
"encoding/hex"
67
"errors"
78
"fmt"
89
"log/slog"
@@ -1534,9 +1535,9 @@ func (con *converter) visitConst(expr *exprpb.Expr) error {
15341535
con.str.WriteString(fmt.Sprintf("$%d", con.paramCount))
15351536
con.parameters = append(con.parameters, b)
15361537
} else {
1537-
con.str.WriteString(`b"`)
1538-
con.str.WriteString(bytesToOctets(b))
1539-
con.str.WriteString(`"`)
1538+
con.str.WriteString("'\\x")
1539+
con.str.WriteString(hex.EncodeToString(b))
1540+
con.str.WriteString("'")
15401541
}
15411542
default:
15421543
return newConversionErrorf(errMsgUnsupportedExpression, "constant type: %T", c.ConstantKind)

edgecase_coverage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestConstEdgeCases(t *testing.T) {
115115
{
116116
name: "bytes_value",
117117
expression: `b"hello" != b""`,
118-
expectedSQL: `b"\150\145\154\154\157" != b""`,
118+
expectedSQL: `'\x68656c6c6f' != '\x'`,
119119
description: "Bytes constant",
120120
},
121121
{

final_coverage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func TestBinaryOperatorAdditionalCases(t *testing.T) {
287287
{
288288
name: "bytes_concatenation",
289289
expression: `b"hello" + b" world" != b""`,
290-
expectedSQL: `b"\150\145\154\154\157" || b"\040\167\157\162\154\144" != b""`,
290+
expectedSQL: `'\x68656c6c6f' || '\x20776f726c64' != '\x'`,
291291
description: "Bytes concatenation with || operator",
292292
},
293293
{

utils.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,6 @@ func extractFieldName(node *exprpb.Expr) (string, error) {
148148
return fieldName, nil
149149
}
150150

151-
// Byte conversion utilities
152-
153-
// bytesToOctets converts byte sequences to a string using a three digit octal encoded value
154-
// per byte.
155-
func bytesToOctets(byteVal []byte) string {
156-
var b strings.Builder
157-
for _, c := range byteVal {
158-
_, _ = fmt.Fprintf(&b, "\\%03o", c)
159-
}
160-
return b.String()
161-
}
162-
163151
// Numeric comparison utilities
164152

165153
// isNumericComparison checks if an operator is a numeric comparison

0 commit comments

Comments
 (0)