Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Open
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
69 changes: 68 additions & 1 deletion internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
if isUnion {
return c.outputColumns(qc, n.Larg)
}
if len(targets.Items) == 0 && n.ValuesLists != nil && len(n.ValuesLists.Items) > 0 {
return c.valuesColumns(qc, n.ValuesLists)
}
case *ast.UpdateStmt:
targets = n.ReturningList
}
Expand Down Expand Up @@ -419,6 +422,46 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
return cols, nil
}

func (c *Compiler) valuesColumns(qc *QueryCatalog, valuesLists *ast.List) ([]*Column, error) {
var cols []*Column
for _, item := range valuesLists.Items {
row, ok := item.(*ast.List)
if !ok {
continue
}
targets := make([]ast.Node, 0, len(row.Items))
for i, expr := range row.Items {
name := fmt.Sprintf("column%d", i+1)
targets = append(targets, &ast.ResTarget{
Name: &name,
Val: expr,
})
}
rowCols, err := c.outputColumns(qc, &ast.SelectStmt{
TargetList: &ast.List{Items: targets},
FromClause: &ast.List{},
})
if err != nil {
return nil, err
}
if cols == nil {
cols = rowCols
continue
}
for i := 0; i < len(cols) && i < len(rowCols); i++ {
if !rowCols[i].NotNull {
cols[i].NotNull = false
}
if cols[i].DataType == "any" && rowCols[i].DataType != "any" {
notNull := cols[i].NotNull
cols[i] = rowCols[i]
cols[i].NotNull = notNull
}
}
}
return cols, nil
}

const (
tableNotFound = iota
tableRequired
Expand Down Expand Up @@ -616,8 +659,32 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
}

var tableName string
var colNames []string
if n.Alias != nil {
tableName = *n.Alias.Aliasname
if n.Alias.Aliasname != nil {
tableName = *n.Alias.Aliasname
}
if n.Alias.Colnames != nil {
for _, item := range n.Alias.Colnames.Items {
if colName, ok := item.(*ast.String); ok {
colNames = append(colNames, colName.Str)
} else {
colNames = append(colNames, "")
}
}
}
}

if len(colNames) > 0 {
renamed := make([]*Column, len(cols))
for i, col := range cols {
colCopy := *col
if i < len(colNames) && colNames[i] != "" {
colCopy.Name = colNames[i]
}
renamed[i] = &colCopy
}
cols = renamed
}

tables = append(tables, &Table{
Expand Down
48 changes: 45 additions & 3 deletions internal/core/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,52 @@ func (a *analyzer) typeLimit(n ast.Node) error {
}

func (a *analyzer) typeValuesLists(l *ast.List) error {
for _, row := range listItems(l) {
if _, err := a.typeExpr(row); err != nil {
return err
var colTypes []exprType
for _, item := range listItems(l) {
row, ok := item.(*ast.List)
if !ok {
if _, err := a.typeExpr(item); err != nil {
return err
}
continue
}
cells := listItems(row)
if colTypes == nil {
colTypes = make([]exprType, len(cells))
}
for i, cell := range cells {
t, err := a.typeExpr(cell)
if err != nil {
return err
}
if i >= len(colTypes) {
continue
}
if colTypes[i].typeOID == 0 && colTypes[i].expr == nil {
nullable := colTypes[i].nullable || t.nullable
colTypes[i] = t
colTypes[i].nullable = nullable
} else if t.nullable {
colTypes[i].nullable = true
}
}
}

a.columns = make([]core.Column, len(colTypes))
for i, t := range colTypes {
if t.typeOID == 0 && t.expr == nil {
if oid, ok := a.cat.UntypedTypeOID(); ok {
t = exprType{typeOID: oid, nullable: true}
}
}
col := core.Column{
Name: fmt.Sprintf("column%d", i+1),
TypeOID: t.typeOID,
NotNull: !t.nullable,
}
col.DataType, col.IsArray = a.typeNameOf(t)
col.Type = a.typeExprOf(t)
a.columns[i] = col
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions internal/endtoend/testdata/cte_values/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/sqlc-dev/sqlc/issues/4198
32 changes: 32 additions & 0 deletions internal/endtoend/testdata/cte_values/postgresql/pgx/go/db.go

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

10 changes: 10 additions & 0 deletions internal/endtoend/testdata/cte_values/postgresql/pgx/go/models.go

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

166 changes: 166 additions & 0 deletions internal/endtoend/testdata/cte_values/postgresql/pgx/go/query.sql.go

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

27 changes: 27 additions & 0 deletions internal/endtoend/testdata/cte_values/postgresql/pgx/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- name: SaleStatus :many
WITH w_pending_sale_status AS (
SELECT * FROM
(VALUES ('SAVED'), ('IDLE'), ('IN PROGRESS'), ('HELD'))
AS pending_sale_status(status)
)
SELECT status FROM w_pending_sale_status;

-- name: StatusDetails :many
WITH status_details AS (
SELECT * FROM
(VALUES ('SAVED', 1, NULL), ('IDLE', NULL, true))
AS v(status, rank, active)
)
SELECT status, rank, active FROM status_details;

-- name: DirectValuesCTE :many
WITH codes(code, label) AS (
VALUES (1, 'one'), (2, 'two')
)
SELECT * FROM codes;

-- name: UnnamedValuesColumns :many
SELECT * FROM (VALUES ('alpha', 10), ('beta', 20)) AS v;

-- name: RenamedSubqueryColumns :many
SELECT * FROM (SELECT id, name FROM items) AS renamed(item_id, item_name);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE items (
id integer NOT NULL,
name text NOT NULL
);
Loading
Loading