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

INSERT with SELECT causes *ast.ResTarget has nil name error #1646

Closed
ryan-berger opened this issue May 27, 2022 · 4 comments
Closed

INSERT with SELECT causes *ast.ResTarget has nil name error #1646

ryan-berger opened this issue May 27, 2022 · 4 comments

Comments

@ryan-berger
Copy link
Contributor

Version

1.13.0

What happened?

When trying to compile an INSERT/SELECT with sqlc.narg, I received the following error message:

query.sql:5:1: *ast.ResTarget has nil name

Relevant log output

SQLCDEBUG=dumpast=1 sqlc-dev generate
([]interface {}) (len=1 cap=1) {
 (*ast.RawStmt)(0xc00003d020)({
  Stmt: (*ast.CreateTableStmt)(0xc00024c960)({
   IfNotExists: (bool) false,
   Name: (*ast.TableName)(0xc000254a50)({
    Catalog: (string) "",
    Schema: (string) "",
    Name: (string) (len=1) "a"
   }),
   Cols: ([]*ast.ColumnDef) (len=1 cap=1) {
    (*ast.ColumnDef)(0xc000258780)({
     Colname: (string) (len=6) "temp_a",
     TypeName: (*ast.TypeName)(0xc000849490)({
      Catalog: (string) "",
      Schema: (string) "",
      Name: (string) (len=4) "text",
      Names: (*ast.List)(<nil>),
      TypeOid: (ast.Oid) 0,
      Setof: (bool) false,
      PctType: (bool) false,
      Typmods: (*ast.List)(<nil>),
      Typemod: (int32) 0,
      ArrayBounds: (*ast.List)(<nil>),
      Location: (int) 0
     }),
     IsNotNull: (bool) false,
     IsArray: (bool) false,
     Vals: (*ast.List)(<nil>),
     Length: (*int)(<nil>),
     Inhcount: (int) 0,
     IsLocal: (bool) false,
     IsFromType: (bool) false,
     IsFromParent: (bool) false,
     Storage: (uint8) 0,
     RawDefault: (ast.Node) <nil>,
     CookedDefault: (ast.Node) <nil>,
     Identity: (uint8) 0,
     CollClause: (*ast.CollateClause)(<nil>),
     CollOid: (ast.Oid) 0,
     Constraints: (*ast.List)(<nil>),
     Fdwoptions: (*ast.List)(<nil>),
     Location: (int) 0,
     Comment: (string) ""
    })
   },
   ReferTable: (*ast.TableName)(<nil>),
   Comment: (string) "",
   Inherits: ([]*ast.TableName) <nil>
  }),
  StmtLocation: (int) 0,
  StmtLen: (int) 27
 })
}
([]interface {}) (len=1 cap=1) {
 (*ast.RawStmt)(0xc00003d040)({
  Stmt: (*ast.CreateTableStmt)(0xc00024c9c0)({
   IfNotExists: (bool) false,
   Name: (*ast.TableName)(0xc000254ab0)({
    Catalog: (string) "",
    Schema: (string) "",
    Name: (string) (len=1) "b"
   }),
   Cols: ([]*ast.ColumnDef) (len=2 cap=2) {
    (*ast.ColumnDef)(0xc000258820)({
     Colname: (string) (len=6) "temp_a",
     TypeName: (*ast.TypeName)(0xc000849500)({
      Catalog: (string) "",
      Schema: (string) "",
      Name: (string) (len=4) "text",
      Names: (*ast.List)(<nil>),
      TypeOid: (ast.Oid) 0,
      Setof: (bool) false,
      PctType: (bool) false,
      Typmods: (*ast.List)(<nil>),
      Typemod: (int32) 0,
      ArrayBounds: (*ast.List)(<nil>),
      Location: (int) 0
     }),
     IsNotNull: (bool) false,
     IsArray: (bool) false,
     Vals: (*ast.List)(<nil>),
     Length: (*int)(<nil>),
     Inhcount: (int) 0,
     IsLocal: (bool) false,
     IsFromType: (bool) false,
     IsFromParent: (bool) false,
     Storage: (uint8) 0,
     RawDefault: (ast.Node) <nil>,
     CookedDefault: (ast.Node) <nil>,
     Identity: (uint8) 0,
     CollClause: (*ast.CollateClause)(<nil>),
     CollOid: (ast.Oid) 0,
     Constraints: (*ast.List)(<nil>),
     Fdwoptions: (*ast.List)(<nil>),
     Location: (int) 0,
     Comment: (string) ""
    }),
    (*ast.ColumnDef)(0xc0002588c0)({
     Colname: (string) (len=6) "temp_b",
     TypeName: (*ast.TypeName)(0xc000849570)({
      Catalog: (string) "",
      Schema: (string) "",
      Name: (string) (len=4) "text",
      Names: (*ast.List)(<nil>),
      TypeOid: (ast.Oid) 0,
      Setof: (bool) false,
      PctType: (bool) false,
      Typmods: (*ast.List)(<nil>),
      Typemod: (int32) 0,
      ArrayBounds: (*ast.List)(<nil>),
      Location: (int) 0
     }),
     IsNotNull: (bool) false,
     IsArray: (bool) false,
     Vals: (*ast.List)(<nil>),
     Length: (*int)(<nil>),
     Inhcount: (int) 0,
     IsLocal: (bool) false,
     IsFromType: (bool) false,
     IsFromParent: (bool) false,
     Storage: (uint8) 0,
     RawDefault: (ast.Node) <nil>,
     CookedDefault: (ast.Node) <nil>,
     Identity: (uint8) 0,
     CollClause: (*ast.CollateClause)(<nil>),
     CollOid: (ast.Oid) 0,
     Constraints: (*ast.List)(<nil>),
     Fdwoptions: (*ast.List)(<nil>),
     Location: (int) 0,
     Comment: (string) ""
    })
   },
   ReferTable: (*ast.TableName)(<nil>),
   Comment: (string) "",
   Inherits: ([]*ast.TableName) <nil>
  }),
  StmtLocation: (int) 28,
  StmtLen: (int) 41
 })
}
([]interface {}) (len=1 cap=1) {
 (*ast.RawStmt)(0xc00003d160)({
  Stmt: (*ast.InsertStmt)(0xc0003ba340)({
   Relation: (*ast.RangeVar)(0xc000254b40)({
    Catalogname: (*string)(<nil>),
    Schemaname: (*string)(<nil>),
    Relname: (*string)(0xc0005985e0)((len=1) "b"),
    Inh: (bool) true,
    Relpersistence: (uint8) 112,
    Alias: (*ast.Alias)(<nil>),
    Location: (int) 109
   }),
   Cols: (*ast.List)(0xc0005a1308)({
    Items: ([]ast.Node) (len=2 cap=2) {
     (*ast.ResTarget)(0xc000254b70)({
      Name: (*string)(0xc0005985f0)((len=6) "temp_a"),
      Indirection: (*ast.List)(0xc0005a1320)({
       Items: ([]ast.Node) <nil>
      }),
      Val: (*ast.TODO)(0x22d91e0)({
      }),
      Location: (int) 111
     }),
     (*ast.ResTarget)(0xc000254ba0)({
      Name: (*string)(0xc000598610)((len=6) "temp_c"),
      Indirection: (*ast.List)(0xc0005a1338)({
       Items: ([]ast.Node) <nil>
      }),
      Val: (*ast.TODO)(0x22d91e0)({
      }),
      Location: (int) 119
     })
    }
   }),
   SelectStmt: (*ast.SelectStmt)(0xc00025a630)({
    DistinctClause: (*ast.List)(0xc0005a1350)({
     Items: ([]ast.Node) <nil>
    }),
    IntoClause: (*ast.IntoClause)(<nil>),
    TargetList: (*ast.List)(0xc0005a1368)({
     Items: ([]ast.Node) (len=2 cap=2) {
      (*ast.ResTarget)(0xc000254bd0)({
       Name: (*string)(<nil>),
       Indirection: (*ast.List)(0xc0005a1380)({
        Items: ([]ast.Node) <nil>
       }),
       Val: (*ast.ColumnRef)(0xc00003d080)({
        Name: (string) "",
        Fields: (*ast.List)(0xc0005a1398)({
         Items: ([]ast.Node) (len=1 cap=1) {
          (*ast.String)(0xc000598630)({
           Str: (string) (len=6) "temp_a"
          })
         }
        }),
        Location: (int) 134
       }),
       Location: (int) 134
      }),
      (*ast.ResTarget)(0xc000254c90)({
       Name: (*string)(<nil>),
       Indirection: (*ast.List)(0xc0005a13b0)({
        Items: ([]ast.Node) <nil>
       }),
       Val: (*ast.CoalesceExpr)(0xc000254c60)({
        Xpr: (*ast.TODO)(0x22d91e0)({
        }),
        Coalescetype: (ast.Oid) 0,
        Coalescecollid: (ast.Oid) 0,
        Args: (*ast.List)(0xc0005a13c8)({
         Items: ([]ast.Node) (len=2 cap=2) {
          (*ast.FuncCall)(0xc000250ff0)({
           Func: (*ast.FuncName)(0xc000254c30)({
            Catalog: (string) "",
            Schema: (string) (len=4) "sqlc",
            Name: (string) (len=4) "narg"
           }),
           Funcname: (*ast.List)(0xc0005a13e0)({
            Items: ([]ast.Node) (len=2 cap=2) {
             (*ast.String)(0xc000598680)({
              Str: (string) (len=4) "sqlc"
             }),
             (*ast.String)(0xc0005986a0)({
              Str: (string) (len=4) "narg"
             })
            }
           }),
           Args: (*ast.List)(0xc0005a13f8)({
            Items: ([]ast.Node) (len=1 cap=1) {
             (*ast.A_Const)(0xc0005a1410)({
              Val: (*ast.String)(0xc0005986b0)({
               Str: (string) (len=3) "arg"
              }),
              Location: (int) 168
             })
            }
           }),
           AggOrder: (*ast.List)(0xc0005a1428)({
            Items: ([]ast.Node) <nil>
           }),
           AggFilter: (*ast.TODO)(0x22d91e0)({
           }),
           AggWithinGroup: (bool) false,
           AggStar: (bool) false,
           AggDistinct: (bool) false,
           FuncVariadic: (bool) false,
           Over: (*ast.WindowDef)(<nil>),
           Location: (int) 158
          }),
          (*ast.ColumnRef)(0xc00003d0e0)({
           Name: (string) "",
           Fields: (*ast.List)(0xc0005a1440)({
            Items: ([]ast.Node) (len=1 cap=1) {
             (*ast.String)(0xc0005986e0)({
              Str: (string) (len=6) "temp_b"
             })
            }
           }),
           Location: (int) 176
          })
         }
        }),
        Location: (int) 149
       }),
       Location: (int) 149
      })
     }
    }),
    FromClause: (*ast.List)(0xc0005a1458)({
     Items: ([]ast.Node) (len=1 cap=1) {
      (*ast.RangeVar)(0xc000254cc0)({
       Catalogname: (*string)(<nil>),
       Schemaname: (*string)(<nil>),
       Relname: (*string)(0xc000598720)((len=1) "a"),
       Inh: (bool) true,
       Relpersistence: (uint8) 112,
       Alias: (*ast.Alias)(<nil>),
       Location: (int) 189
      })
     }
    }),
    WhereClause: (*ast.TODO)(0x22d91e0)({
    }),
    GroupClause: (*ast.List)(0xc0005a1470)({
     Items: ([]ast.Node) <nil>
    }),
    HavingClause: (*ast.TODO)(0x22d91e0)({
    }),
    WindowClause: (*ast.List)(0xc0005a1488)({
     Items: ([]ast.Node) <nil>
    }),
    ValuesLists: (*ast.List)(0xc0005a14a0)({
     Items: ([]ast.Node) <nil>
    }),
    SortClause: (*ast.List)(0xc0005a14b8)({
     Items: ([]ast.Node) <nil>
    }),
    LimitOffset: (*ast.TODO)(0x22d91e0)({
    }),
    LimitCount: (*ast.TODO)(0x22d91e0)({
    }),
    LockingClause: (*ast.List)(0xc0005a14d0)({
     Items: ([]ast.Node) <nil>
    }),
    WithClause: (*ast.WithClause)(<nil>),
    Op: (ast.SetOperation) None,
    All: (bool) false,
    Larg: (*ast.SelectStmt)(<nil>),
    Rarg: (*ast.SelectStmt)(<nil>)
   }),
   OnConflictClause: (*ast.OnConflictClause)(<nil>),
   ReturningList: (*ast.List)(0xc0005a14e8)({
    Items: ([]ast.Node) (len=1 cap=1) {
     (*ast.ResTarget)(0xc000254cf0)({
      Name: (*string)(<nil>),
      Indirection: (*ast.List)(0xc0005a1500)({
       Items: ([]ast.Node) <nil>
      }),
      Val: (*ast.ColumnRef)(0xc00003d140)({
       Name: (string) "",
       Fields: (*ast.List)(0xc0005a1518)({
        Items: ([]ast.Node) (len=1 cap=1) {
         (*ast.A_Star)(0x22d91e0)({
         })
        }
       }),
       Location: (int) 201
      }),
      Location: (int) 201
     })
    }
   }),
   WithClause: (*ast.WithClause)(<nil>),
   Override: (ast.OverridingKind) 1
  }),
  StmtLocation: (int) 70,
  StmtLen: (int) 132
 })
}
# package batch
query.sql:5:1: *ast.ResTarget has nil name

Database schema

No response

SQL queries

CREATE TABLE a(temp_a TEXT);
CREATE TABLE b(temp_a TEXT, temp_b TEXT);

-- name: TestQuery :many
INSERT INTO b(temp_a, temp_c)
SELECT temp_a,
       COALESCE(sqlc.narg('arg'), temp_b)
FROM a
RETURNING *;

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "postgresql",
      "name": "batch",
      "schema": "query.sql",
      "queries": "query.sql",
      "engine": "postgresql",
      "sql_package": "pgx/v4",
      "emit_json_tags": true,
      "emit_prepared_queries": true,
      "emit_interface": true
    }
  ]
}

Playground URL

No response

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@ryan-berger ryan-berger added bug Something isn't working triage New issues that hasn't been reviewed labels May 27, 2022
@kyleconroy kyleconroy added 📚 postgresql 💻 linux 🔧 golang and removed triage New issues that hasn't been reviewed labels Jun 4, 2022
@MS-24
Copy link

MS-24 commented Mar 23, 2023

Facing similar issue with below insert statement.

-- name: InsertOrders :exec
	insert into Orders (id,name)
	select id , CASE WHEN @name_do_update::BOOLEAN THEN @name ELSE s.name END 
	from Orders s ;

@lavaniatwinkle
Copy link

DO we have a resolution for this yet? I am facing a similar issue.

@kyleconroy
Copy link
Collaborator

kyleconroy added a commit that referenced this issue Oct 16, 2023
kyleconroy added a commit that referenced this issue Oct 18, 2023
* fix(compiler): Pull in array information from analyzer
Fixes #1532
* test(analyzer): Add testcase for #1574
* test: Added test for #1634
* test: Add test case for #1646
* test: Add test for #1714
* Fixes #1912
* test: Add case for #1916
* test: Add two test cases
#1917
#1545
* test: Add case for #1979
* test: Add case for #1990
@kyleconroy
Copy link
Collaborator

This is fixed in v1.23.0 by enabling the database-backed query analyzer. We added a test case for this issue so it won’t break in the future.

You can play around with the working example on the playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants