Skip to content
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

fix error in same name column reference "id" is ambiguous #3727

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions internal/compiler/query_catalog.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package compiler

import (
"fmt"
"strings"

"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
@@ -77,6 +78,35 @@ func ConvertColumn(rel *ast.TableName, c *catalog.Column) *Column {
}
}

func RevertConvertColumn(c *Column) *catalog.Column {
out := &catalog.Column{
Name: c.Name,
IsNotNull: c.NotNull,
IsUnsigned: c.Unsigned,
IsArray: c.IsArray,
ArrayDims: c.ArrayDims,
Length: c.Length,
}
if c.Type != nil {
out.Type = *c.Type
}
dataTypes := strings.Split(c.DataType, ".")
if len(dataTypes) == 1 {
out.Type.Name = dataTypes[0]
} else if len(dataTypes) == 2 {
out.Type.Schema = dataTypes[0]
out.Type.Name = dataTypes[1]
}
return out
}

func RevertConvertColumns(columns []*Column) (out []*catalog.Column) {
for i := range columns {
out = append(out, RevertConvertColumn(columns[i]))
}
return
}

func (qc QueryCatalog) GetTable(rel *ast.TableName) (*Table, error) {
cte, exists := qc.ctes[rel.Name]
if exists {
13 changes: 12 additions & 1 deletion internal/compiler/resolve.go
Original file line number Diff line number Diff line change
@@ -67,9 +67,20 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
continue
}
// If the table name doesn't exist, first check if it's a CTE
if _, qcerr := qc.GetTable(fqn); qcerr != nil {
cteTable, qcerr := qc.GetTable(fqn)
if qcerr != nil {
return nil, err
}
err = indexTable(catalog.Table{
Rel: cteTable.Rel,
Columns: RevertConvertColumns(cteTable.Columns),
})
if err != nil {
return nil, err
}
if rv.Alias != nil {
aliasMap[*rv.Alias.Aliasname] = fqn
}
continue
}
err = indexTable(table)
Loading
Oops, something went wrong.