sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

byte arrays are not handled correctly with unnest

Open dziegler opened this issue 2 years ago • 1 comments

Version

1.23.0

What happened?

sqlc doesn't generate byte arrays when using unnest with bytea[]. The generated query below results in:

const insertTradesTemp = `-- name: InsertTradesTemp :exec
INSERT INTO transactions.trades_temp (
    id,
    trade
)
SELECT
    unnest($1::text[]),
    unnest($2::bytea[])
`

type InsertTradesTempParams struct {
	Column1 []string
	Column2 [][]byte
}

func (q *Queries) InsertTradesTemp(ctx context.Context, arg InsertTradesTempParams) error {
	_, err := q.exec(ctx, q.insertTradesTempStmt, insertTradesTemp, pq.Array(arg.Column1), pq.Array(arg.Column2))
	return err
}

when it should use pq.ByteArray for bytea[], e.g.

const insertTradesTemp = `-- name: InsertTradesTemp :exec
INSERT INTO transactions.trades_temp (
    id,
    trade
)
SELECT
    unnest($1::text[]),
    unnest($2::bytea[])
`

type InsertTradesTempParams struct {
	Column1 []string
	Column2 [][]byte
}

func (q *Queries) InsertTradesTemp(ctx context.Context, arg InsertTradesTempParams) error {
	_, err := q.exec(ctx, q.insertTradesTempStmt, insertTradesTemp, pq.Array(arg.Column1), pq.ByteaArray(arg.Column2))
	return err
}

Relevant log output

No response

Database schema

CREATE TABLE transactions.trades_temp (
	id text NOT NULL,
	trade bytea NOT NULL,
	CONSTRAINT trades_pk PRIMARY KEY (id)
);

SQL queries

-- name: InsertTradesTemp :exec
INSERT INTO transactions.trades_temp (
    id,
    trade
)
SELECT
    unnest($1::text[]),
    unnest($2::bytea[]);

Configuration

version: 2
sql:
  - engine: "postgresql"
    schema: "model.sql"
    queries: "queries.sql"
    gen:
      go:
        package: "txprocessor"
        out: "../../internal/models/txprocessor"
        omit_unused_structs: true
        emit_prepared_queries: true
        overrides:
          - db_type: "decimal"
            go_type: "github.com/shopspring/decimal.Decimal"

Playground URL

https://play.sqlc.dev/p/ebe6505fec44971788a308a20c3d209da706c261956f09a6279ca266c8dddd1d

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

dziegler avatar Nov 28 '23 21:11 dziegler

Same

stepan-romankov avatar Dec 29 '23 08:12 stepan-romankov