Skip to content

COALESCE in INSERT results in missing arguments #2748

Closed

Description

@andrei-dascalu

Version

1.21.0

What happened?

When writing a query like

INSERT INTO authors (
    address,
    name,
    bio
) VALUES (
    ?,
    COALESCE(sqlc.narg("authName"), ""),
    COALESCE(sqlc.narg("authBio"), "")
);

Only the ? is recognised and add as a parameter to the insert query

Relevant log output

none - generate works

Database schema

CREATE TABLE authors (
  id   BIGINT AUTO_INCREMENT NOT NULL,
  address VARCHAR(200) NOT NULL,
  name VARCHAR(20) NOT NULL,
  bio  LONGTEXT NOT NULL
);

SQL queries

-- name: AddAuthor :execlastid
INSERT INTO authors (
    address,
    name,
    bio
) VALUES (
    ?,
    COALESCE(sqlc.narg("calName"), ""),
    COALESCE(sqlc.narg("calDescription"), "")
);

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "db",
      "engine": "mysql",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/8471d8c7965409b41c01510cf680b8026ae5268dc83ae2591e891c432aef0432

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Sep 20, 2023
  2. andrewmbenton commented on Sep 21, 2023

    @andrewmbenton
    Member

    I think the bug is sadly worse than your description, and I'll probably update the title. It appears that sqlc fails to generate input parameters for values passed to COALESCE in INSERT statements, even without sqlc.narg(). And I also see this behavior with PostgreSQL.

    This playground link updates the one you provided to demonstrate that things work fine in the context of UPDATE: https://play.sqlc.dev/p/0d614a07b58fbacdea078b51d5e6d338414dc6558059249294c29f74212f9104

    And here's a playground link demonstrating the bug using PostgreSQL as engine and without sqlc.narg(): https://play.sqlc.dev/p/28b5372fd509a34792e30e7eb62bbbe74d400ab7e7cb1652d58bc9f7ee1ec5e7

  3. changed the title [-]COALESCE and narg results in missing arguments[/-] [+]COALESCE in INSERT results in missing arguments[/+] on Sep 21, 2023
  4. andrei-dascalu commented on Sep 21, 2023

    @andrei-dascalu
    Author

    Interestingly this does indeed seem particular to COALESCE. I went back and tried a couple of other functions like substrings (both for postgresql and the mysql variant) but they seem ok.

  5. orisano commented on Sep 21, 2023

    @orisano
    Contributor

    The problem occurs because COALESCE is treated as CoalesceExpr instead of FuncCall on ast.

  6. added this to the 1.23.0 milestone on Oct 2, 2023
  7. kyleconroy commented on Oct 12, 2023

    @kyleconroy
    Collaborator

    Fix has been merged, will be included in v1.23.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions