Thanks to visit codestin.com
Credit goes to github.com

Skip to content

COALESCE regression #4870

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

Closed
vanniktech opened this issue Dec 2, 2023 · 6 comments · Fixed by #4882
Closed

COALESCE regression #4870

vanniktech opened this issue Dec 2, 2023 · 6 comments · Fixed by #4882

Comments

@vanniktech
Copy link
Contributor

SQLDelight Version

2.0.1

Operating System

Mac

Gradle Version

8.5

Kotlin Version

1.9.21

Dialect

sqlite

AGP Version

8.1.4

Describe the Bug

Table:

CREATE TABLE meditationPhase (
  id TEXT NOT NULL PRIMARY KEY,
  meditationId TEXT NOT NULL REFERENCES meditation(id),
  number INTEGER AS Int NOT NULL,
  duration INTEGER NOT NULL,
  sound TEXT AS Sound NOT NULL
);

and my query:

totalDuration:
SELECT COALESCE(SUM(duration), 0) AS totalDuration
  FROM meditationPhase;

With 2.0.0 it simply returned the value as Long?, now it is wrapped in a class:

public data class TotalDuration(
  public val totalDuration: Long?,
)

Is there any reason for this change? In addition, the type should really be Long since the 0 will always match and make the expression non-nullable.

Stacktrace

No response

Gradle Build Script

No response

@vanniktech vanniktech added the bug label Dec 2, 2023
@vanniktech
Copy link
Contributor Author

Here's one more example where I think the same underlying failure is happening:

COALESCE(SUM(CASE WHEN shots = 1 THEN 1 ELSE 0 END), 0) AS numberOfHoleInOne,
CREATE TABLE minigolfPlayerGame (
  id TEXT NOT NULL PRIMARY KEY,
  playerId TEXT NOT NULL,
  gameId TEXT NOT NULL,
  hole INTEGER AS Int NOT NULL,
  shots INTEGER AS Int NOT NULL,
  total INTEGER AS Int NOT NULL,
  penalties INTEGER AS Int NOT NULL DEFAULT 0,
  FOREIGN KEY (playerId) REFERENCES minigolfPlayer(id),
  FOREIGN KEY (gameId) REFERENCES minigolfGame(id)
);

I used to get a Long but now inside my query I'm getting:

public val numberOfHoleInOne: Long?,

@vanniktech
Copy link
Contributor Author

My intuition is that the following PR introduced this regression: #4517

@eygraber
Copy link
Contributor

eygraber commented Dec 5, 2023

I have to look at the first case closer to see what's going on.

The second case is likely caused because SUM is always assumed to return null.. That could probably be updated to return non null if none of the inputs are nullable.

@eygraber
Copy link
Contributor

eygraber commented Dec 5, 2023

OK I found the first case in one of my projects as well. I believe I have a fix for it, gonna test it out and make a PR (probably tomorrow though)

@eygraber
Copy link
Contributor

eygraber commented Dec 5, 2023

Have the fix for both, thanks for reporting!

@vanniktech
Copy link
Contributor Author

Just checked with the latest snapshot and indeed all of the cases have been fixed in my project. Nicely done @eygraber

@JakeWharton JakeWharton removed the bug label Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants