cashapp / sqldelight

SQLDelight - Generates typesafe Kotlin APIs from SQL
https://cashapp.github.io/sqldelight/
Apache License 2.0
6.13k stars 513 forks source link

COALESCE not inferring `AS custom-type` inside of `UPDATE` #5363

Open milanvdm opened 1 month ago

milanvdm commented 1 month ago

Using sqldelight version 2.0.2 for the JVM with postgressql-dialect.

It looks like COALESCE is losing the custom-type.

import com.example.Latitude;
import com.example.Longitude;
import com.example.NodeId;

CREATE TABLE nodes(
  node_id TEXT AS NodeId PRIMARY KEY NOT NULL,
  latitude FLOAT AS Latitude NOT NULL,
  longitude FLOAT AS Longitude NOT NULL,
);

updateNode:
UPDATE nodes
SET
    latitude = COALESCE(:latitude, latitude),
    longitude = COALESCE(:longitude, longitude)
WHERE node_id = :node_id;

This generates:

public fun updateNode(
    latitude: Double?,
    longitude: Double?,
    node_id: NodeId,
  )
griffio commented 1 month ago

😞 Sadly, custom type adapters are not generated when used with functions like this (e.g = COALESCE(:longitude, longitude)) - it is a general issue and requires a fix maybe starting here: https://github.com/cashapp/sqldelight/blob/4bbaa08e16bba5cc7c2145544fd2a1613b0d41db/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/lang/SqlDelightQueriesFile.kt#L108

Looking at this, the current implementation only works when referencing query columns directly (e.g = :node_id) and a specific case for insert statements.

Somewhere the query column type is being lost 👻 when referencing bind arguments used in functions