Skip to content

Commit 8cfa830

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

32 files changed

+919
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,11 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
656656
if schema != "" && t.Rel.Schema != schema {
657657
continue
658658
}
659-
if alias != "" && t.Rel.Name != alias {
659+
originTableName := ""
660+
if len(t.Columns) > 0 && t.Columns[0] != nil && t.Columns[0].Table != nil {
661+
originTableName = t.Columns[0].Table.Name
662+
}
663+
if alias != "" && t.Rel.Name != alias && originTableName != alias {
660664
continue
661665
}
662666
for _, c := range t.Columns {

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

Lines changed: 31 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
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

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
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+
;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
CREATE TABLE IF NOT EXISTS aws_rds_databases (
2+
account_id TEXT NOT NULL,
3+
region TEXT NOT NULL,
4+
arn TEXT 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 TEXT NOT NULL,
17+
engine_version TEXT NOT NULL,
18+
deprecation TEXT NOT NULL,
19+
20+
UNIQUE (engine, engine_version)
21+
);
Lines changed: 12 additions & 0 deletions
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/pgx/v4/go/db.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
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+
;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
CREATE TABLE IF NOT EXISTS aws_rds_databases (
2+
account_id TEXT NOT NULL,
3+
region TEXT NOT NULL,
4+
arn TEXT 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 TEXT NOT NULL,
17+
engine_version TEXT NOT NULL,
18+
deprecation TEXT NOT NULL,
19+
20+
UNIQUE (engine, engine_version)
21+
);

0 commit comments

Comments
 (0)