Skip to content

Commit b4eee66

Browse files
committed
fix: table alias can not find column
1 parent e4e1371 commit b4eee66

File tree

21 files changed

+588
-7
lines changed

21 files changed

+588
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
__pycache__
33
.DS_Store
44
.*.swp
5+
.vscode
56

67
# Devenv
78
.envrc

internal/compiler/output_columns.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,10 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
620620
}
621621
if n.Alias != nil {
622622
table.Rel = &ast.TableName{
623-
Catalog: table.Rel.Catalog,
624-
Schema: table.Rel.Schema,
625-
Name: *n.Alias.Aliasname,
623+
Catalog: table.Rel.Catalog,
624+
Schema: table.Rel.Schema,
625+
Name: *n.Alias.Aliasname,
626+
OriginalName: table.Rel.Name,
626627
}
627628
}
628629
tables = append(tables, table)
@@ -656,7 +657,7 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
656657
if schema != "" && t.Rel.Schema != schema {
657658
continue
658659
}
659-
if alias != "" && t.Rel.Name != alias {
660+
if alias != "" && t.Rel.Name != alias && t.Rel.OriginalName != alias {
660661
continue
661662
}
662663
for _, c := range t.Columns {

internal/endtoend/testdata/alias_join/mysql/go/db.go

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/alias_join/mysql/go/models.go

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/alias_join/mysql/go/query.sql.go

+88
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- name: RetrieveAllWithDeprecation :many
2+
select
3+
arn,
4+
name,
5+
aws_rds_databases.engine
6+
from
7+
aws_rds_databases r
8+
natural join aws_rds_databases_engines
9+
;
10+
11+
-- name: RetrieveAllWithDeprecationOther :many
12+
select
13+
arn,
14+
name,
15+
r.engine
16+
from
17+
aws_rds_databases r
18+
natural join aws_rds_databases_engines
19+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
CREATE TABLE IF NOT EXISTS aws_rds_databases (
2+
account_id VARCHAR(20) NOT NULL,
3+
region VARCHAR(20) NOT NULL,
4+
arn VARCHAR(20) NOT NULL,
5+
name TEXT NOT NULL,
6+
engine TEXT NOT NULL,
7+
engine_version TEXT NOT NULL,
8+
9+
-- tags is a JSON object
10+
tags TEXT NOT NULL,
11+
12+
UNIQUE (account_id, region, arn)
13+
);
14+
15+
CREATE TABLE IF NOT EXISTS aws_rds_databases_engines (
16+
engine VARCHAR(20) NOT NULL,
17+
engine_version VARCHAR(20) NOT NULL,
18+
deprecation TEXT NOT NULL,
19+
20+
UNIQUE (engine, engine_version)
21+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "mysql",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/endtoend/testdata/alias_join/postgresql/go/db.go

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/alias_join/postgresql/go/models.go

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/alias_join/postgresql/go/query.sql.go

+88
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- name: RetrieveAllWithDeprecation :many
2+
select
3+
arn,
4+
name,
5+
aws_rds_databases.engine
6+
from
7+
aws_rds_databases r
8+
natural join aws_rds_databases_engines
9+
;
10+
11+
-- name: RetrieveAllWithDeprecationOther :many
12+
select
13+
arn,
14+
name,
15+
r.engine
16+
from
17+
aws_rds_databases r
18+
natural join aws_rds_databases_engines
19+
;

0 commit comments

Comments
 (0)