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

Return custom types by SUM, COALESCE #3715

Closed
Knoxvillekm opened this issue Dec 2, 2022 · 3 comments
Closed

Return custom types by SUM, COALESCE #3715

Knoxvillekm opened this issue Dec 2, 2022 · 3 comments
Labels

Comments

@Knoxvillekm
Copy link

Description

CREATE TABLE SomeTable (
    ...
    field TEXT AS CustomType NOT NULL 
);

selectSum:
SELECT SUM(field) FROM SomeTable;

selectSum:
SELECT COALESCE(field, CustomType(defaultValue)) FROM SomeTable;

For now SUM return Double value and cannot set default value to COALESCE.
Idea:
Add some override fun to Adapder

ColumnAdapter<CustomType, String> {
  override fun decode(databaseValue: String)
  override fun encode(value: CustomType)
  override fun sum(value: CustomType, value2: CustomType)
  override fun coalesce(value: CustomType, defaultValue: CustomType)
}
@hfhbd
Copy link
Collaborator

hfhbd commented Dec 4, 2022

I don't think we want to introduce custom sql handling. If you have a valid SQL syntax calculating the sum of text, I am open. Same for coalescing.

@Knoxvillekm
Copy link
Author

How can I return CustomType from SUM and COALESCE?

selectSum:
SELECT SUM(field) AS CustomType FROM SomeTable;

selectSum:
SELECT COALESCE(field, "") AS CustomType FROM SomeTable;

AS CustomType - just rename returned field.

@Knoxvillekm Knoxvillekm reopened this Dec 9, 2022
@hfhbd
Copy link
Collaborator

hfhbd commented Dec 9, 2022

We really try to avoid changing the sql dialect because this make many thinks way harder, sql itself is often ambiguous. There is a similar issue and maybe we will add it somehow: #3421 (comment)

If you really want this functionallity in sql and not in Kotlin, there is a workound through, but requires a little overhead: you must create your own dialect to override this function: https://github.com/cashapp/sqldelight/tree/master/sqldelight-gradle-plugin/src/test/custom-dialect

@AlecKazakova AlecKazakova closed this as not planned Won't fix, can't repro, duplicate, stale Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants