Skip to content

byte arrays are not handled correctly with unnest #3021

Open
@dziegler

Description

@dziegler

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageNew issues that hasn't been reviewed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions