Skip to content

cmd/sqlc: pre-allocate slices for queries with LIMIT clauses #4031

@thatnealpatel

Description

@thatnealpatel

What do you want to change?

Hi,

I attempted to look around the issue tracker and website to find a duplicate and was not successful.

I believe that generated functions that have LIMIT clauses can pre-allocate memory so that calls to append are asymptotically cheaper. I cannot imagine that this is so widely desirable that it merits a change to the core library. However, I figured that the discussion might be worth having.

Of course, the example below is quite hacky; however, I am interested in whether or not this has been considered? Perhaps there is a more idiomatic way?

Example:

const getFoo = `--name: GetFoo :many
SELECT bar, baz
FROM foo as f
WHERE f.bar = ?1 AND f.baz <= ?2
LIMIT 5
`

func (q *Queries) GetFoo(ctx context.Context, arg FooParams) ([]Foo, error) {
       rows, err := q.db.QueryContext(ctx, getFoo, ...) // LIMIT present here
       if err != nil {
               return nil, err
       }
       defer rows.Close()
       var items []Foo
       // naively:
       // regex `LIMIT {0,9}*` out, parse it?
       // items := make([]Foo, 0, limitN)
       for rows.Next() {
               var i Foo
               if err := rows.Scan(&i.Bar, &i.Baz); err != nil {
                       return nil, err
               }
               items = append(items, i)
       }
       if err := rows.Close(); err != nil {
               return nil, err
       }
       if err := rows.Err(); err != nil {
               return nil, err
       }
       return items, nil
 }

What database engines need to be changed?

No response

What programming language backends need to be changed?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions