Skip to content

sqlc crashes when calling a function with OUT arg #1654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
danielbprice opened this issue May 31, 2022 · 4 comments · Fixed by #2865
Closed

sqlc crashes when calling a function with OUT arg #1654

danielbprice opened this issue May 31, 2022 · 4 comments · Fixed by #2865

Comments

@danielbprice
Copy link
Contributor

danielbprice commented May 31, 2022

Version

1.13.0

What happened?

I tried to call a function which has an OUT argument. I am seeing sqlc crash a lot when I try to do this. I realize I might have botched the syntax or something somewhat, but the crash is making diagnosis of the problem harder.

Relevant log output

sqlc generate failed.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xe77599]

goroutine 1 [running]:
github.com/kyleconroy/sqlc/internal/compiler.sourceTables(0xc00051f0a0, {0x14205a0, 0xc00037dd90})
	/go/pkg/mod/github.com/kyleconroy/[email protected]/internal/compiler/output_columns.go:409 +0x639
github.com/kyleconroy/sqlc/internal/compiler.outputColumns(0xc00051f0a0, {0x14205a0, 0xc00037dd90})
	/go/pkg/mod/github.com/kyleconroy/[email protected]/internal/compiler/output_columns.go:54 +0x56
github.com/kyleconroy/sqlc/internal/compiler.(*Compiler).parseQuery(0xc000424000, {0x1420340, 0xc0002159a0}, {0xc000418480, 0x21a}, {0x0, {0x0, 0x0, {0x0, 0x0}}})
	/go/pkg/mod/github.com/kyleconroy/[email protected]/internal/compiler/parse.go:114 +0x8bf
github.com/kyleconroy/sqlc/internal/compiler.(*Compiler).parseQueries(0xc000424000, {0x20, {0x20, 0x45, {0x0, 0x1}}})
	/go/pkg/mod/github.com/kyleconroy/[email protected]/internal/compiler/compile.go:108 +0x513
github.com/kyleconroy/sqlc/internal/compiler.(*Compiler).ParseQueries(...)
	/go/pkg/mod/github.com/kyleconroy/[email protected]/internal/compiler/engine.go:49
github.com/kyleconroy/sqlc/internal/cmd.parse({_, _}, {_, _}, {_, _}, {_, _}, {{0xc0002294a6, 0xa}, ...}, ...)
	/go/pkg/mod/github.com/kyleconroy/[email protected]/internal/cmd/generate.go:262 +0x253
github.com/kyleconroy/sqlc/internal/cmd.Generate({0x14343d0, 0xc00004e040}, {0x80, 0x46}, {0xc00004a3c0, 0x57}, {0x0, 0x101e900}, {0x14250e0, 0xc000010020})
	/go/pkg/mod/github.com/kyleconroy/[email protected]/internal/cmd/generate.go:183 +0x7bb
github.com/kyleconroy/sqlc/internal/cmd.glob..func3(0x1d3bcc0, {0x116a2e3, 0x0, 0x0})
	/go/pkg/mod/github.com/kyleconroy/[email protected]/internal/cmd/cmd.go:144 +0x194
github.com/spf13/cobra.(*Command).execute(0x1d3bcc0, {0x2366b60, 0x0, 0x0})
	/go/pkg/mod/github.com/spf13/[email protected]/command.go:860 +0x5f8
github.com/spf13/cobra.(*Command).ExecuteC(0xc0002fea00)
	/go/pkg/mod/github.com/spf13/[email protected]/command.go:974 +0x3bc
github.com/spf13/cobra.(*Command).Execute(...)
	/go/pkg/mod/github.com/spf13/[email protected]/command.go:902
github.com/spf13/cobra.(*Command).ExecuteContext(...)
	/go/pkg/mod/github.com/spf13/[email protected]/command.go:895
github.com/kyleconroy/sqlc/internal/cmd.Do({0xc00003c050, 0x1, 0x1}, {0x14250a0, 0xc000010010}, {0x14250e0, 0xc000010018}, {0x14250e0, 0xc000010020})
	/go/pkg/mod/github.com/kyleconroy/[email protected]/internal/cmd/cmd.go:47 +0x4ba
main.main()
	/go/pkg/mod/github.com/kyleconroy/[email protected]/cmd/sqlc/main.go:10 +0x7a

Database schema

-- Example queries for sqlc
CREATE TABLE authors (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  bio  text
);

CREATE OR REPLACE FUNCTION add_author (name text, bio text, out id int)
AS $$
DECLARE
BEGIN
  id = 123;
END;
$$ LANGUAGE plpgsql;

SQL queries

-- name: CreateAuthor :one
SELECT * FROM add_author (
  sqlc.arg(name), sqlc.arg(bio)
);

Configuration

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

Playground URL

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

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@danielbprice danielbprice added bug Something isn't working triage New issues that hasn't been reviewed labels May 31, 2022
@danielbprice
Copy link
Contributor Author

I also reproduced this on HEAD of main, b0d6f13

@danielbprice
Copy link
Contributor Author

With a little more experimentation, I found that adding RETURNS void to the function avoids the crash, but then an error is thrown: query.sql:17:1: empty edit contents: https://play.sqlc.dev/p/50db6d7ed79a5b1cd22ae47ec5938ccb856fa1f89f4d83caad779bc6e6fbffcd

Modifying the code to SELECT add_author(...); instead of SELECT * FROM add_author(...)') further helps, but the out argument appears to be ignored: https://play.sqlc.dev/p/bb7c8c91e7e3f775c095e1905f890eea93c6cf9371c6a00ed9582cf9a0f16e87

@kyleconroy kyleconroy added 📚 postgresql 💻 linux 🔧 golang panic and removed triage New issues that hasn't been reviewed labels Jun 4, 2022
andrewmbenton added a commit that referenced this issue Oct 16, 2023
kyleconroy added a commit that referenced this issue Oct 17, 2023
* test: add a test case for #1654

* fix(compiler): Support functions with OUT params

* Add generated code

---------

Co-authored-by: Andrew Benton <[email protected]>
@mdigger
Copy link

mdigger commented May 26, 2024

Does not work in version 1.26.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x1030369a4]

goroutine 7 [running]:
github.com/sqlc-dev/sqlc/internal/compiler.dataType(...)
	github.com/sqlc-dev/sqlc/internal/compiler/resolve.go:17
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).outputColumns(0x14000663208, 0x14000a0c960, {0x103e1b5e0, 0x140008fdd90})
	github.com/sqlc-dev/sqlc/internal/compiler/output_columns.go:315 +0x454
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler)._analyzeQuery(0x14000663208, 0x14000514560, {0x140008f5ec2, 0xbd}, 0x1)
	github.com/sqlc-dev/sqlc/internal/compiler/analyze.go:180 +0x9ac
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).analyzeQuery(...)
	github.com/sqlc-dev/sqlc/internal/compiler/analyze.go:110
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQuery(0x14000663208, {0x103e1b7a0, 0x14000514560}, {0x140008f5800, 0x780}, {{0x0, 0x0, {0x0, 0x0}, 0x0, ...}})
	github.com/sqlc-dev/sqlc/internal/compiler/parse.go:103 +0x480
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQueries(0x14000663208, {{0x0, 0x0, {0x0, 0x0}, 0x0, 0x0, 0x0, 0x0}})
	github.com/sqlc-dev/sqlc/internal/compiler/compile.go:81 +0x3b0
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).ParseQueries(...)
	github.com/sqlc-dev/sqlc/internal/compiler/engine.go:76
github.com/sqlc-dev/sqlc/internal/cmd.parse({_, _}, {_, _}, {_, _}, {{0x0, 0x0}, {0x14000497070, 0xa}, ...}, ...)
	github.com/sqlc-dev/sqlc/internal/cmd/generate.go:322 +0x23c
github.com/sqlc-dev/sqlc/internal/cmd.processQuerySets.func1()
	github.com/sqlc-dev/sqlc/internal/cmd/process.go:107 +0x68c
golang.org/x/sync/errgroup.(*Group).Go.func1()
	golang.org/x/[email protected]/errgroup/errgroup.go:78 +0x58
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
	golang.org/x/[email protected]/errgroup/errgroup.go:75 +0x98

@wexder
Copy link

wexder commented Aug 18, 2024

Does not work in version 1.27.0

CREATE OR REPLACE FUNCTION next_id(IN seq regclass, OUT result bigint) AS $$
DECLARE
--  6/4/2013 00:00:00 GMT
our_epoch bigint := 1370304000000;
seq_id bigint;
now_millis bigint;
shard_id int := 1;
BEGIN
SELECT nextval(seq) % 1024 INTO seq_id;

SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis;
result := (now_millis - our_epoch) << 23;
result := result | (shard_id << 10);
result := result | (seq_id);
END;
$$ LANGUAGE PLPGSQL;
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x10bcbba]

goroutine 19 [running]:
github.com/sqlc-dev/sqlc/internal/compiler.dataType(...)
  /go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/resolve.go:17
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).outputColumns(0xc0003acf08, 0xc000599080, {0x1da9fa0, 0xc000576210})
  /go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/output_columns.go:315 +0x49a
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler)._analyzeQuery(0xc0003acf08, 0xc000164ea0, {0xc00012febc, 0x46}, 0x1)
  /go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/analyze.go:180 +0xccd
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).analyzeQuery(...)
  /go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/analyze.go:110
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQuery(0xc0003acf08, {0x1daa160, 0xc000164ea0}, {0xc00012fc00, 0x3c7}, {{0x0, 0x0, {0x0, 0x0}, 0x0, ...}})
  /go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/parse.go:103 +0x5d5
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQueries(0xc0003acf08, {{0x0, 0x0, {0x0, 0x0}, 0x0, 0x0, 0x0, 0x0}})
  /go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/compile.go:81 +0x467
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).ParseQueries(...)
  /go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/engine.go:72
github.com/sqlc-dev/sqlc/internal/cmd.parse({_, _}, {_, _}, {_, _}, {{0x0, 0x0}, {0xc0004243c0, 0xa}, ...}, ...)
  /go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/cmd/generate.go:322 +0x2eb
github.com/sqlc-dev/sqlc/internal/cmd.processQuerySets.func1()
  /go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/cmd/process.go:107 +0x81a
golang.org/x/sync/errgroup.(*Group).Go.func1()
  /go/pkg/mod/golang.org/x/[email protected]/errgroup/errgroup.go:78 +0x56
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
  /go/pkg/mod/golang.org/x/[email protected]/errgroup/errgroup.go:75 +0x96
cmd/main.go:3: running "sqlc": exit status 2

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

Successfully merging a pull request may close this issue.

4 participants