Repository: sqlc-dev/sqlc
Pull request #2956 (2023-11-07 19:42:45)

fix(compiler): No panic on full-qualified column names
Fixes https://github.com/sqlc-dev/sqlc/issues/2948


Patch:
diff --git a/internal/compiler/resolve.go b/internal/compiler/resolve.go
index eb4315a47..1947558b2 100644
--- a/internal/compiler/resolve.go
+++ b/internal/compiler/resolve.go
@@ -173,6 +173,10 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
 				case 2:
 					alias = items[0]
 					key = items[1]
+				case 3:
+					// schema := items[0]
+					alias = items[1]
+					key = items[2]
 				default:
 					panic("too many field items: " + strconv.Itoa(len(items)))
 				}


Test patch:
diff --git a/internal/endtoend/testdata/schema_table_column_ref/issue.md b/internal/endtoend/testdata/schema_table_column_ref/issue.md
new file mode 100644
index 000000000..f58a7d02d
--- /dev/null
+++ b/internal/endtoend/testdata/schema_table_column_ref/issue.md
@@ -0,0 +1,1 @@
+https://github.com/sqlc-dev/sqlc/issues/2948
diff --git a/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/go/db.go b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/go/db.go
new file mode 100644
index 000000000..2568532b6
--- /dev/null
+++ b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/go/db.go
@@ -0,0 +1,32 @@
+// Code generated by sqlc. DO NOT EDIT.
+// versions:
+//   sqlc v1.23.0
+
+package querytest
+
+import (
+	"context"
+
+	"github.com/jackc/pgx/v5"
+	"github.com/jackc/pgx/v5/pgconn"
+)
+
+type DBTX interface {
+	Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
+	Query(context.Context, string, ...interface{}) (pgx.Rows, error)
+	QueryRow(context.Context, string, ...interface{}) pgx.Row
+}
+
+func New(db DBTX) *Queries {
+	return &Queries{db: db}
+}
+
+type Queries struct {
+	db DBTX
+}
+
+func (q *Queries) WithTx(tx pgx.Tx) *Queries {
+	return &Queries{
+		db: tx,
+	}
+}
diff --git a/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/go/models.go b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/go/models.go
new file mode 100644
index 000000000..a2cda3521
--- /dev/null
+++ b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/go/models.go
@@ -0,0 +1,23 @@
+// Code generated by sqlc. DO NOT EDIT.
+// versions:
+//   sqlc v1.23.0
+
+package querytest
+
+import (
+	"github.com/jackc/pgx/v5/pgtype"
+)
+
+type AstoriaSlackFeedback struct {
+	ID          int64
+	WorkspaceID int64
+	CreatedAt   pgtype.Timestamp
+	IssueRaised pgtype.Bool
+}
+
+type AstoriaTicket struct {
+	ID          int64
+	WorkspaceID int64
+	CreatedAt   pgtype.Timestamp
+	Source      string
+}
diff --git a/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/go/query.sql.go b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/go/query.sql.go
new file mode 100644
index 000000000..890576c24
--- /dev/null
+++ b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/go/query.sql.go
@@ -0,0 +1,77 @@
+// Code generated by sqlc. DO NOT EDIT.
+// versions:
+//   sqlc v1.23.0
+// source: query.sql
+
+package querytest
+
+import (
+	"context"
+
+	"github.com/jackc/pgx/v5/pgtype"
+)
+
+const getTotalSlackQueries = `-- name: GetTotalSlackQueries :one
+SELECT
+    COUNT(*) AS count
+FROM astoria.slack_feedback
+WHERE astoria.slack_feedback.workspace_id = $1
+AND created_at BETWEEN $2::date AND $3::date
+`
+
+type GetTotalSlackQueriesParams struct {
+	WorkspaceID int64
+	Column2     pgtype.Date
+	Column3     pgtype.Date
+}
+
+func (q *Queries) GetTotalSlackQueries(ctx context.Context, arg GetTotalSlackQueriesParams) (int64, error) {
+	row := q.db.QueryRow(ctx, getTotalSlackQueries, arg.WorkspaceID, arg.Column2, arg.Column3)
+	var count int64
+	err := row.Scan(&count)
+	return count, err
+}
+
+const getTotalSlackQueriesRequestsCreated = `-- name: GetTotalSlackQueriesRequestsCreated :one
+SELECT
+    COUNT(*) AS count
+FROM astoria.tickets
+WHERE astoria.tickets.workspace_id = $1
+  AND source = 'RAISED_FROM_BOT'
+  AND created_at BETWEEN $2::date AND $3::date
+`
+
+type GetTotalSlackQueriesRequestsCreatedParams struct {
+	WorkspaceID int64
+	Column2     pgtype.Date
+	Column3     pgtype.Date
+}
+
+func (q *Queries) GetTotalSlackQueriesRequestsCreated(ctx context.Context, arg GetTotalSlackQueriesRequestsCreatedParams) (int64, error) {
+	row := q.db.QueryRow(ctx, getTotalSlackQueriesRequestsCreated, arg.WorkspaceID, arg.Column2, arg.Column3)
+	var count int64
+	err := row.Scan(&count)
+	return count, err
+}
+
+const getTotalSlackQueriesResolved = `-- name: GetTotalSlackQueriesResolved :one
+SELECT
+    COUNT(*) AS count
+FROM astoria.slack_feedback
+WHERE astoria.slack_feedback.workspace_id = $1
+  AND (issue_raised = false OR issue_raised IS NULL)
+  AND created_at BETWEEN $2::date AND $3::date
+`
+
+type GetTotalSlackQueriesResolvedParams struct {
+	WorkspaceID int64
+	Column2     pgtype.Date
+	Column3     pgtype.Date
+}
+
+func (q *Queries) GetTotalSlackQueriesResolved(ctx context.Context, arg GetTotalSlackQueriesResolvedParams) (int64, error) {
+	row := q.db.QueryRow(ctx, getTotalSlackQueriesResolved, arg.WorkspaceID, arg.Column2, arg.Column3)
+	var count int64
+	err := row.Scan(&count)
+	return count, err
+}
diff --git a/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/query.sql b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/query.sql
new file mode 100644
index 000000000..3aa8ae1ca
--- /dev/null
+++ b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/query.sql
@@ -0,0 +1,22 @@
+-- name: GetTotalSlackQueries :one
+SELECT
+    COUNT(*) AS count
+FROM astoria.slack_feedback
+WHERE astoria.slack_feedback.workspace_id = $1
+AND created_at BETWEEN $2::date AND $3::date;
+
+-- name: GetTotalSlackQueriesResolved :one
+SELECT
+    COUNT(*) AS count
+FROM astoria.slack_feedback
+WHERE astoria.slack_feedback.workspace_id = $1
+  AND (issue_raised = false OR issue_raised IS NULL)
+  AND created_at BETWEEN $2::date AND $3::date;
+
+-- name: GetTotalSlackQueriesRequestsCreated :one
+SELECT
+    COUNT(*) AS count
+FROM astoria.tickets
+WHERE astoria.tickets.workspace_id = $1
+  AND source = 'RAISED_FROM_BOT'
+  AND created_at BETWEEN $2::date AND $3::date;
diff --git a/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/schema.sql b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/schema.sql
new file mode 100644
index 000000000..48ccc12b8
--- /dev/null
+++ b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/schema.sql
@@ -0,0 +1,15 @@
+CREATE SCHEMA astoria;
+
+CREATE TABLE astoria.slack_feedback (
+  id            BIGSERIAL PRIMARY KEY,
+  workspace_id  BIGINT NOT NULL, 
+  created_at    TIMESTAMP NOT NULL,
+  issue_raised  BOOLEAN
+);
+
+CREATE TABLE astoria.tickets (
+  id            BIGSERIAL PRIMARY KEY,
+  workspace_id  BIGINT NOT NULL, 
+  created_at    TIMESTAMP NOT NULL,
+  source text   NOT NULL
+);
diff --git a/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/sqlc.yaml b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/sqlc.yaml
new file mode 100644
index 000000000..5dc63e3f9
--- /dev/null
+++ b/internal/endtoend/testdata/schema_table_column_ref/postgresql/pgx/sqlc.yaml
@@ -0,0 +1,10 @@
+version: "2"
+sql:
+  - engine: "postgresql"
+    schema: "schema.sql"
+    queries: "query.sql"
+    gen:
+      go:
+        package: "querytest"
+        out: "go"
+        sql_package: "pgx/v5"