From b4eee668156655081040cd66d2eb40baa9314cbb Mon Sep 17 00:00:00 2001 From: Steve Xu Date: Tue, 26 Mar 2024 13:04:36 +0800 Subject: [PATCH] fix: table alias can not find column --- .gitignore | 1 + internal/compiler/output_columns.go | 9 +- .../testdata/alias_join/mysql/go/db.go | 31 +++++++ .../testdata/alias_join/mysql/go/models.go | 21 +++++ .../testdata/alias_join/mysql/go/query.sql.go | 88 ++++++++++++++++++ .../testdata/alias_join/mysql/query.sql | 19 ++++ .../testdata/alias_join/mysql/schema.sql | 21 +++++ .../testdata/alias_join/mysql/sqlc.json | 12 +++ .../testdata/alias_join/postgresql/go/db.go | 31 +++++++ .../alias_join/postgresql/go/models.go | 21 +++++ .../alias_join/postgresql/go/query.sql.go | 88 ++++++++++++++++++ .../testdata/alias_join/postgresql/query.sql | 19 ++++ .../testdata/alias_join/postgresql/schema.sql | 21 +++++ .../testdata/alias_join/postgresql/sqlc.json | 12 +++ .../testdata/alias_join/sqlite/go/db.go | 31 +++++++ .../testdata/alias_join/sqlite/go/models.go | 21 +++++ .../alias_join/sqlite/go/query.sql.go | 90 +++++++++++++++++++ .../testdata/alias_join/sqlite/query.sql | 19 ++++ .../testdata/alias_join/sqlite/schema.sql | 21 +++++ .../testdata/alias_join/sqlite/sqlc.json | 12 +++ internal/sql/ast/table_name.go | 7 +- 21 files changed, 588 insertions(+), 7 deletions(-) create mode 100644 internal/endtoend/testdata/alias_join/mysql/go/db.go create mode 100644 internal/endtoend/testdata/alias_join/mysql/go/models.go create mode 100644 internal/endtoend/testdata/alias_join/mysql/go/query.sql.go create mode 100644 internal/endtoend/testdata/alias_join/mysql/query.sql create mode 100644 internal/endtoend/testdata/alias_join/mysql/schema.sql create mode 100644 internal/endtoend/testdata/alias_join/mysql/sqlc.json create mode 100644 internal/endtoend/testdata/alias_join/postgresql/go/db.go create mode 100644 internal/endtoend/testdata/alias_join/postgresql/go/models.go create mode 100644 internal/endtoend/testdata/alias_join/postgresql/go/query.sql.go create mode 100644 internal/endtoend/testdata/alias_join/postgresql/query.sql create mode 100644 internal/endtoend/testdata/alias_join/postgresql/schema.sql create mode 100644 internal/endtoend/testdata/alias_join/postgresql/sqlc.json create mode 100644 internal/endtoend/testdata/alias_join/sqlite/go/db.go create mode 100644 internal/endtoend/testdata/alias_join/sqlite/go/models.go create mode 100644 internal/endtoend/testdata/alias_join/sqlite/go/query.sql.go create mode 100644 internal/endtoend/testdata/alias_join/sqlite/query.sql create mode 100644 internal/endtoend/testdata/alias_join/sqlite/schema.sql create mode 100644 internal/endtoend/testdata/alias_join/sqlite/sqlc.json diff --git a/.gitignore b/.gitignore index 39961ebb02..af2afc070c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__ .DS_Store .*.swp +.vscode # Devenv .envrc diff --git a/internal/compiler/output_columns.go b/internal/compiler/output_columns.go index dbdbe252b3..51d27a363a 100644 --- a/internal/compiler/output_columns.go +++ b/internal/compiler/output_columns.go @@ -620,9 +620,10 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro } if n.Alias != nil { table.Rel = &ast.TableName{ - Catalog: table.Rel.Catalog, - Schema: table.Rel.Schema, - Name: *n.Alias.Aliasname, + Catalog: table.Rel.Catalog, + Schema: table.Rel.Schema, + Name: *n.Alias.Aliasname, + OriginalName: table.Rel.Name, } } tables = append(tables, table) @@ -656,7 +657,7 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef) if schema != "" && t.Rel.Schema != schema { continue } - if alias != "" && t.Rel.Name != alias { + if alias != "" && t.Rel.Name != alias && t.Rel.OriginalName != alias { continue } for _, c := range t.Columns { diff --git a/internal/endtoend/testdata/alias_join/mysql/go/db.go b/internal/endtoend/testdata/alias_join/mysql/go/db.go new file mode 100644 index 0000000000..df0488f428 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/mysql/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/alias_join/mysql/go/models.go b/internal/endtoend/testdata/alias_join/mysql/go/models.go new file mode 100644 index 0000000000..8b63403515 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/mysql/go/models.go @@ -0,0 +1,21 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 + +package querytest + +type AwsRdsDatabase struct { + AccountID string + Region string + Arn string + Name string + Engine string + EngineVersion string + Tags string +} + +type AwsRdsDatabasesEngine struct { + Engine string + EngineVersion string + Deprecation string +} diff --git a/internal/endtoend/testdata/alias_join/mysql/go/query.sql.go b/internal/endtoend/testdata/alias_join/mysql/go/query.sql.go new file mode 100644 index 0000000000..49150edc44 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/mysql/go/query.sql.go @@ -0,0 +1,88 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 +// source: query.sql + +package querytest + +import ( + "context" +) + +const retrieveAllWithDeprecation = `-- name: RetrieveAllWithDeprecation :many +select + arn, + name, + aws_rds_databases.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +` + +type RetrieveAllWithDeprecationRow struct { + Arn string + Name string + Engine string +} + +func (q *Queries) RetrieveAllWithDeprecation(ctx context.Context) ([]RetrieveAllWithDeprecationRow, error) { + rows, err := q.db.QueryContext(ctx, retrieveAllWithDeprecation) + if err != nil { + return nil, err + } + defer rows.Close() + var items []RetrieveAllWithDeprecationRow + for rows.Next() { + var i RetrieveAllWithDeprecationRow + if err := rows.Scan(&i.Arn, &i.Name, &i.Engine); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const retrieveAllWithDeprecationOther = `-- name: RetrieveAllWithDeprecationOther :many +select + arn, + name, + r.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +` + +type RetrieveAllWithDeprecationOtherRow struct { + Arn string + Name string + Engine string +} + +func (q *Queries) RetrieveAllWithDeprecationOther(ctx context.Context) ([]RetrieveAllWithDeprecationOtherRow, error) { + rows, err := q.db.QueryContext(ctx, retrieveAllWithDeprecationOther) + if err != nil { + return nil, err + } + defer rows.Close() + var items []RetrieveAllWithDeprecationOtherRow + for rows.Next() { + var i RetrieveAllWithDeprecationOtherRow + if err := rows.Scan(&i.Arn, &i.Name, &i.Engine); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/alias_join/mysql/query.sql b/internal/endtoend/testdata/alias_join/mysql/query.sql new file mode 100644 index 0000000000..9adc83573f --- /dev/null +++ b/internal/endtoend/testdata/alias_join/mysql/query.sql @@ -0,0 +1,19 @@ +-- name: RetrieveAllWithDeprecation :many +select + arn, + name, + aws_rds_databases.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +; + +-- name: RetrieveAllWithDeprecationOther :many +select + arn, + name, + r.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +; diff --git a/internal/endtoend/testdata/alias_join/mysql/schema.sql b/internal/endtoend/testdata/alias_join/mysql/schema.sql new file mode 100644 index 0000000000..e45ea91937 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/mysql/schema.sql @@ -0,0 +1,21 @@ +CREATE TABLE IF NOT EXISTS aws_rds_databases ( + account_id VARCHAR(20) NOT NULL, + region VARCHAR(20) NOT NULL, + arn VARCHAR(20) NOT NULL, + name TEXT NOT NULL, + engine TEXT NOT NULL, + engine_version TEXT NOT NULL, + + -- tags is a JSON object + tags TEXT NOT NULL, + + UNIQUE (account_id, region, arn) +); + +CREATE TABLE IF NOT EXISTS aws_rds_databases_engines ( + engine VARCHAR(20) NOT NULL, + engine_version VARCHAR(20) NOT NULL, + deprecation TEXT NOT NULL, + + UNIQUE (engine, engine_version) +); diff --git a/internal/endtoend/testdata/alias_join/mysql/sqlc.json b/internal/endtoend/testdata/alias_join/mysql/sqlc.json new file mode 100644 index 0000000000..e41c39e8b3 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/mysql/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "mysql", + "name": "querytest", + "schema": "schema.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/endtoend/testdata/alias_join/postgresql/go/db.go b/internal/endtoend/testdata/alias_join/postgresql/go/db.go new file mode 100644 index 0000000000..df0488f428 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/postgresql/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/alias_join/postgresql/go/models.go b/internal/endtoend/testdata/alias_join/postgresql/go/models.go new file mode 100644 index 0000000000..8b63403515 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/postgresql/go/models.go @@ -0,0 +1,21 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 + +package querytest + +type AwsRdsDatabase struct { + AccountID string + Region string + Arn string + Name string + Engine string + EngineVersion string + Tags string +} + +type AwsRdsDatabasesEngine struct { + Engine string + EngineVersion string + Deprecation string +} diff --git a/internal/endtoend/testdata/alias_join/postgresql/go/query.sql.go b/internal/endtoend/testdata/alias_join/postgresql/go/query.sql.go new file mode 100644 index 0000000000..49150edc44 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/postgresql/go/query.sql.go @@ -0,0 +1,88 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 +// source: query.sql + +package querytest + +import ( + "context" +) + +const retrieveAllWithDeprecation = `-- name: RetrieveAllWithDeprecation :many +select + arn, + name, + aws_rds_databases.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +` + +type RetrieveAllWithDeprecationRow struct { + Arn string + Name string + Engine string +} + +func (q *Queries) RetrieveAllWithDeprecation(ctx context.Context) ([]RetrieveAllWithDeprecationRow, error) { + rows, err := q.db.QueryContext(ctx, retrieveAllWithDeprecation) + if err != nil { + return nil, err + } + defer rows.Close() + var items []RetrieveAllWithDeprecationRow + for rows.Next() { + var i RetrieveAllWithDeprecationRow + if err := rows.Scan(&i.Arn, &i.Name, &i.Engine); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const retrieveAllWithDeprecationOther = `-- name: RetrieveAllWithDeprecationOther :many +select + arn, + name, + r.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +` + +type RetrieveAllWithDeprecationOtherRow struct { + Arn string + Name string + Engine string +} + +func (q *Queries) RetrieveAllWithDeprecationOther(ctx context.Context) ([]RetrieveAllWithDeprecationOtherRow, error) { + rows, err := q.db.QueryContext(ctx, retrieveAllWithDeprecationOther) + if err != nil { + return nil, err + } + defer rows.Close() + var items []RetrieveAllWithDeprecationOtherRow + for rows.Next() { + var i RetrieveAllWithDeprecationOtherRow + if err := rows.Scan(&i.Arn, &i.Name, &i.Engine); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/alias_join/postgresql/query.sql b/internal/endtoend/testdata/alias_join/postgresql/query.sql new file mode 100644 index 0000000000..9adc83573f --- /dev/null +++ b/internal/endtoend/testdata/alias_join/postgresql/query.sql @@ -0,0 +1,19 @@ +-- name: RetrieveAllWithDeprecation :many +select + arn, + name, + aws_rds_databases.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +; + +-- name: RetrieveAllWithDeprecationOther :many +select + arn, + name, + r.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +; diff --git a/internal/endtoend/testdata/alias_join/postgresql/schema.sql b/internal/endtoend/testdata/alias_join/postgresql/schema.sql new file mode 100644 index 0000000000..1b225d2273 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/postgresql/schema.sql @@ -0,0 +1,21 @@ +CREATE TABLE IF NOT EXISTS aws_rds_databases ( + account_id TEXT NOT NULL, + region TEXT NOT NULL, + arn TEXT NOT NULL, + name TEXT NOT NULL, + engine TEXT NOT NULL, + engine_version TEXT NOT NULL, + + -- tags is a JSON object + tags TEXT NOT NULL, + + UNIQUE (account_id, region, arn) +); + +CREATE TABLE IF NOT EXISTS aws_rds_databases_engines ( + engine TEXT NOT NULL, + engine_version TEXT NOT NULL, + deprecation TEXT NOT NULL, + + UNIQUE (engine, engine_version) +); diff --git a/internal/endtoend/testdata/alias_join/postgresql/sqlc.json b/internal/endtoend/testdata/alias_join/postgresql/sqlc.json new file mode 100644 index 0000000000..f717ca2e66 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/postgresql/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "postgresql", + "name": "querytest", + "schema": "schema.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/endtoend/testdata/alias_join/sqlite/go/db.go b/internal/endtoend/testdata/alias_join/sqlite/go/db.go new file mode 100644 index 0000000000..df0488f428 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/sqlite/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/alias_join/sqlite/go/models.go b/internal/endtoend/testdata/alias_join/sqlite/go/models.go new file mode 100644 index 0000000000..8b63403515 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/sqlite/go/models.go @@ -0,0 +1,21 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 + +package querytest + +type AwsRdsDatabase struct { + AccountID string + Region string + Arn string + Name string + Engine string + EngineVersion string + Tags string +} + +type AwsRdsDatabasesEngine struct { + Engine string + EngineVersion string + Deprecation string +} diff --git a/internal/endtoend/testdata/alias_join/sqlite/go/query.sql.go b/internal/endtoend/testdata/alias_join/sqlite/go/query.sql.go new file mode 100644 index 0000000000..3c02ea304e --- /dev/null +++ b/internal/endtoend/testdata/alias_join/sqlite/go/query.sql.go @@ -0,0 +1,90 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 +// source: query.sql + +package querytest + +import ( + "context" +) + +const retrieveAllWithDeprecation = `-- name: RetrieveAllWithDeprecation :many +select + arn, + name, + aws_rds_databases.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +` + +type RetrieveAllWithDeprecationRow struct { + Arn string + Name string + Engine string +} + +func (q *Queries) RetrieveAllWithDeprecation(ctx context.Context) ([]RetrieveAllWithDeprecationRow, error) { + rows, err := q.db.QueryContext(ctx, retrieveAllWithDeprecation) + if err != nil { + return nil, err + } + defer rows.Close() + var items []RetrieveAllWithDeprecationRow + for rows.Next() { + var i RetrieveAllWithDeprecationRow + if err := rows.Scan(&i.Arn, &i.Name, &i.Engine); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const retrieveAllWithDeprecationOther = `-- name: RetrieveAllWithDeprecationOther :many +; + +select + arn, + name, + r.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +` + +type RetrieveAllWithDeprecationOtherRow struct { + Arn string + Name string + Engine string +} + +func (q *Queries) RetrieveAllWithDeprecationOther(ctx context.Context) ([]RetrieveAllWithDeprecationOtherRow, error) { + rows, err := q.db.QueryContext(ctx, retrieveAllWithDeprecationOther) + if err != nil { + return nil, err + } + defer rows.Close() + var items []RetrieveAllWithDeprecationOtherRow + for rows.Next() { + var i RetrieveAllWithDeprecationOtherRow + if err := rows.Scan(&i.Arn, &i.Name, &i.Engine); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/alias_join/sqlite/query.sql b/internal/endtoend/testdata/alias_join/sqlite/query.sql new file mode 100644 index 0000000000..9adc83573f --- /dev/null +++ b/internal/endtoend/testdata/alias_join/sqlite/query.sql @@ -0,0 +1,19 @@ +-- name: RetrieveAllWithDeprecation :many +select + arn, + name, + aws_rds_databases.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +; + +-- name: RetrieveAllWithDeprecationOther :many +select + arn, + name, + r.engine +from + aws_rds_databases r + natural join aws_rds_databases_engines +; diff --git a/internal/endtoend/testdata/alias_join/sqlite/schema.sql b/internal/endtoend/testdata/alias_join/sqlite/schema.sql new file mode 100644 index 0000000000..db9d431489 --- /dev/null +++ b/internal/endtoend/testdata/alias_join/sqlite/schema.sql @@ -0,0 +1,21 @@ +CREATE TABLE IF NOT EXISTS aws_rds_databases ( + account_id TEXT NOT NULL, + region TEXT NOT NULL, + arn TEXT NOT NULL, + name TEXT NOT NULL, + engine TEXT NOT NULL, + engine_version TEXT NOT NULL, + + -- tags is a JSON object + tags TEXT NOT NULL, + + UNIQUE (account_id, region, arn) ON CONFLICT REPLACE +); + +CREATE TABLE IF NOT EXISTS aws_rds_databases_engines ( + engine TEXT NOT NULL, + engine_version TEXT NOT NULL, + deprecation TEXT NOT NULL, + + UNIQUE (engine, engine_version) ON CONFLICT REPLACE +); diff --git a/internal/endtoend/testdata/alias_join/sqlite/sqlc.json b/internal/endtoend/testdata/alias_join/sqlite/sqlc.json new file mode 100644 index 0000000000..d4963e751f --- /dev/null +++ b/internal/endtoend/testdata/alias_join/sqlite/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "sqlite", + "name": "querytest", + "schema": "schema.sql", + "queries": "query.sql" + } + ] +} \ No newline at end of file diff --git a/internal/sql/ast/table_name.go b/internal/sql/ast/table_name.go index a95a510c83..52eb826687 100644 --- a/internal/sql/ast/table_name.go +++ b/internal/sql/ast/table_name.go @@ -1,9 +1,10 @@ package ast type TableName struct { - Catalog string - Schema string - Name string + Catalog string + Schema string + Name string // table name, maybe alias name, maybe original name + OriginalName string // table original name } func (n *TableName) Pos() int {