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

Add postgreSql character length functions #4121

Merged
merged 8 commits into from
May 3, 2023

Conversation

griffio
Copy link
Contributor

@griffio griffio commented Apr 30, 2023

Add postgreSql character length functions
Add fixture test for use with constraint checks
Add integration test for use with client

Returns number of characters in the string or will return NULL if expression is NULL.

PostgreSQL supports Ansi CHAR_LENGTH, CHARACTER_LENGTH.
It also supports LENGTH, which is a PostgreSQL alias for CHAR_LENGTH.

  • length ( In MySql the length function is in bytes )
  • char_length
  • character_length

Multibyte characters count as a single character, not as bytes.

Examples

char_length ( text ) -> integer
char_length( 'jose' ) -> 4
char_length( '海豚' ) -> 2
char_length( NULL ) -> NULL

TODO

Add to MySql

❓ One question for the reviewer -- how to make the PostgreSqlTypeResolver.kt so that the char functions will type check string expressions and fail if passed a numeric?
e.g error function character_length(integer) does not exist

Test for length function can be executed standalone.

Test for Nullable type support is generated
Returns number of characters in the string or will return NULL if expression is NULL.
In PostgreSql the following are synonymous and are Ansi Sql supported.
length ( In MySql the length function is in bytes )
char_length
character_length

char_length is ANSI SQL Standard and the explicitly named character_length,
as multibyte character counts as a single character not as bytes.

Examples

char_length ( text ) -> integer
char_length('jose') -> 4
char_length('海豚') -> 2
char_length(NULL) -> NULL
Support for all character length functions can be used in check constraint.
lowercase to compile - not sure if this should matter
@griffio griffio force-pushed the add-postgresql-length-functions branch from 54494fd to 8ad37f7 Compare May 1, 2023 18:00
@hfhbd
Copy link
Collaborator

hfhbd commented May 1, 2023

AFAIK there is no sql type checker. While the compiler is able to get the expected Kotlin type, eg for SELECT length(?) to specify the Kotlin parameter (here String), there is no check, if the actual SQL function could handle this SQL type, like: SELECT length(42). And I don't think, there will be a SQL type checker in the future.

hfhbd
hfhbd previously approved these changes May 1, 2023
Copy link
Collaborator

@hfhbd hfhbd left a comment

Choose a reason for hiding this comment

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

Thanks!
Let's add MySql (and other dialects) in another PR.

@griffio
Copy link
Contributor Author

griffio commented May 3, 2023

Ok. To try and understand further - using a bind variable - would it be expected for the SqlFunctionExpr to generate code to express e.g string->int ?

Would this work if the bind parameter is not related to a SqlColumn?

Given

selectLength:
  SELECT length(?);
val length = queries.selectLength("abc").executeAsOne()

results in mismatch: inferred type is String but Long? was expected

Maybe this is not supported

@hfhbd
Copy link
Collaborator

hfhbd commented May 3, 2023

Yes, this should work! Could you add it as a test? I I will take a look later.

@hfhbd hfhbd self-requested a review May 3, 2023 12:27
@hfhbd hfhbd dismissed their stale review May 3, 2023 12:27

Found bug

Shows the error

Type mismatch: inferred type is String but Int? was expected
@griffio
Copy link
Contributor Author

griffio commented May 3, 2023

@hfhbd Thanks - Added failing test here 04ad12f

This problem already existed with calling functions

Type mismatch: inferred type is String but Int? was expected

Maybe it's related to this https://github.com/cashapp/sqldelight/pull/3431/files
Can only infer the same type? 🤔

@hfhbd
Copy link
Collaborator

hfhbd commented May 3, 2023

Thanks.
I think, there is a bigger root cause: we currently don't really have a good representation of a function, including the typed parameters. At the moment, we only specify the return type. The added Kotlin parameter type inference needs other typed sql parameters. In the past, to specify the kotlin type, you need to use a CAST. Ideally, we need a better function representation to specify the type of the parameters.

@hfhbd
Copy link
Collaborator

hfhbd commented May 3, 2023

I was just about to ask to revert the test.
I am fine with merging this.

Rewriting the function implementation is another issue and properly won't make it into 2.0.

@hfhbd hfhbd merged commit 32d6c51 into cashapp:master May 3, 2023
@griffio griffio deleted the add-postgresql-length-functions branch May 4, 2023 08:15
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.

None yet

2 participants