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

Support sqlite stdlib functions #1712

Merged
merged 4 commits into from
Jul 10, 2022

Conversation

hakobera
Copy link
Contributor

Add SQLite function catalogs listed in below pages.

Task

  • Move existing builtins end to end test to builtins/postgresql
  • Add function catalogs for SQLite
    • Aggregate functions
    • Mathematical functions
    • Scalar functions
  • Add end to end test

Remarks

Due to current SQLite parser limitation, we can't get correct type of arguments of function, so we can't overload function based on argument type like below.

SELECT max(1); => integer
SELECT max(1.0); => real
SELECT max('text'); => text

So arguments and return types of these functions like max are defined as any type due to this limitation.

@hakobera hakobera force-pushed the support-sqlite-stdlib-functions branch from 20c6abe to 579cf78 Compare June 26, 2022 15:48
{
Name: "COUNT",
Args: []*catalog.Argument{},
ReturnType: &ast.TypeName{Name: "bigint"},
ReturnType: &ast.TypeName{Name: "integer"},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will change quite a bit of generated code. Is it documented that SQLite will always return an int here?

Copy link
Contributor Author

@hakobera hakobera Jun 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In SQLite world, bigint and integer is totally same. SQLite can understand bigint but it's just a alias.
Each column in an SQLite 3 database is assigned one of the following type affinities:

  • TEXT
  • NUMERIC
  • INTEGER
  • REAL
  • BLOB

And bigint is translate it to integer.

https://www.sqlite.org/datatype3.html

Type definition in sqlc is same. Both are translated to int64 in go world, so this change doesn't any impact for generated code.

https://github.com/kyleconroy/sqlc/blob/0dc2c81f73534c15120f5711f2aa662a5689e02b/internal/codegen/golang/sqlite_type.go#L17-L21

I confirmed generated code is not changed.

https://github.com/hakobera/sqlc/blob/support-sqlite-stdlib-functions/internal/endtoend/testdata/count_star/sqlite/go/query.sql.go#L27-L32

So to keep consistency, I want to use integer for all integer value for SQLite, because it's a true type for integer value. What do you think?

@kyleconroy kyleconroy merged commit 9f6a6a7 into sqlc-dev:main Jul 10, 2022
jlisthood pushed a commit to jlisthood/sqlc that referenced this pull request Apr 28, 2023
* move existing builtins to postgresql folder

* [sqlite] support built-in aggregate functions

https://www.sqlite.org/lang_aggfunc.html

* [sqlite] support built-in mathematical functions

https://www.sqlite.org/lang_mathfunc.html

* [sqlite] support built-in scalar functions

https://www.sqlite.org/lang_corefunc.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants