Skip to content

fix: Search from Rexpr if not found from Lexpr #2056

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

Merged
merged 3 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
_, ok := node.(*ast.ColumnRef)
return ok
})
if len(list.Items) == 0 {
list = astutils.Search(n.Rexpr, func(node ast.Node) bool {
_, ok := node.(*ast.ColumnRef)
return ok
})
}

if len(list.Items) == 0 {
// TODO: Move this to database-specific engine package
Expand All @@ -135,9 +141,9 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
continue
}

switch left := list.Items[0].(type) {
switch node := list.Items[0].(type) {
case *ast.ColumnRef:
items := stringSlice(left.Fields)
items := stringSlice(node.Fields)
var key, alias string
switch len(items) {
case 1:
Expand Down Expand Up @@ -165,7 +171,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
return nil, &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("table alias \"%s\" does not exist", alias),
Location: left.Location,
Location: node.Location,
}
}
}
Expand Down Expand Up @@ -204,14 +210,14 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
return nil, &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("column \"%s\" does not exist", key),
Location: left.Location,
Location: node.Location,
}
}
if found > 1 {
return nil, &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("column reference \"%s\" is ambiguous", key),
Location: left.Location,
Location: node.Location,
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE users (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255)
) ENGINE=InnoDB;

-- name: FindByID :many
SELECT * FROM users WHERE ? = id;

-- name: FindByIDAndName :many
SELECT * FROM users WHERE ? = id AND ? = name;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "mysql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(255)
);

-- name: FindByID :many
SELECT * FROM users WHERE ? = id;

-- name: FindByIDAndName :many
SELECT * FROM users WHERE ? = id AND ? = name;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}