Skip to content

Commit

Permalink
fix error in same name column reference "id" is ambiguous
Browse files Browse the repository at this point in the history
example:
CREATE TABLE IF NOT EXISTS `app`
(
    `id`                        INT          NOT NULL AUTO_INCREMENT,
    `parent_app_id`             INT          NOT NULL DEFAULT 0 COMMENT '合并父应用id',
    PRIMARY KEY (`id`) USING BTREE
) AUTO_INCREMENT = 105 COMMENT = 'API所属的应用信息';

CREATE TABLE IF NOT EXISTS `user_account` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `name` VARCHAR(255) COMMENT '账号名称',
    PRIMARY KEY (`id`) USING BTREE
) AUTO_INCREMENT = 1 COMMENT='账号表,存储账号的基本信息和安全评分';

-- name: GetUserAccountAllChildIDs :exec
WITH app_user_account_id AS (
    SELECT tua.id,
           tua.name,
           CAST(IFNULL(tb.id, ta.id) AS SIGNED) AS parent_app_id
    FROM user_account AS tua
             LEFT JOIN app AS ta ON ta.id = tua.app_id
             LEFT JOIN app as tb ON ta.parent_app_id = tb.id
    WHERE tua.masked_sign = 1 AND tua.deleted_sign = 1
      AND ta.masked_sign = 1
      AND ta.deleted_sign = 1
)
SELECT a.name
FROM app_user_account_id AS a
LEFT JOIN app_user_account_id AS b ON b.id IN (sqlc.slice('account_id_set')) AND a.name = b.name AND a.parent_app_id = b.parent_app_id;
  • Loading branch information
dingyuanhong committed Nov 28, 2024
1 parent 60f1b88 commit 88b10e2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
30 changes: 30 additions & 0 deletions internal/compiler/query_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 88b10e2

Please sign in to comment.