AlecKazakova / sql-psi

An extendable parsing environment for sql which outputs PSI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Group by and having don't work on columns from select clause

vanniktech opened this issue · comments

Dialect

SQLite

Failing SQL

unreadItems:
SELECT
  SUM(CASE WHEN item.read IS NULL AND item.deleted IS NULL THEN 1 ELSE 0 END) AS numberOfUnread,
  COALESCE(directory.name, channel.customTitle, channel.title) AS title,
  COALESCE(channel.directoryId, channel.id) AS identifier,
  GROUP_CONCAT(DISTINCT channel.id) AS channelIds
  FROM item
  JOIN channel
   ON item.channelId = channel.id
  LEFT JOIN directory
   ON channel.directoryId = directory.id
  GROUP BY identifier
  HAVING numberOfUnread > 0
  ORDER BY directory.name COLLATE NOCASE ASC, COALESCE(channel.customTitle, channel.title) COLLATE NOCASE ASC
;

Description

Screenshot 2022-02-11 at 11 18 13

Stacktrace:

/Users/niklas/dev/GitHub/vanniktech/feature-rss-reader/src/commonMain/sqldelight/com/vanniktech/feature/rssreader/Item.sq line 80:11 - No column found with name identifier
70    SELECT
71      SUM(CASE WHEN item.read IS NULL AND item.deleted IS NULL THEN 1 ELSE 0 END) AS numberOfUnread,
72      COALESCE(directory.name, channel.customTitle, channel.title) AS title,
73      COALESCE(channel.directoryId, channel.id) AS identifier,
74      GROUP_CONCAT(DISTINCT channel.id) AS channelIds
75      FROM item
76      JOIN channel
77       ON item.channelId = channel.id
78      LEFT JOIN directory
79       ON channel.directoryId = directory.id
80      GROUP BY identifier
                 ^^^^^^^^^^
81      HAVING numberOfUnread > 0
82      ORDER BY directory.name COLLATE NOCASE ASC, COALESCE(channel.customTitle, channel.title) COLLATE NOCASE ASC

/Users/niklas/dev/GitHub/vanniktech/feature-rss-reader/src/commonMain/sqldelight/com/vanniktech/feature/rssreader/Item.sq line 81:9 - No column found with name numberOfUnread
70    SELECT
71      SUM(CASE WHEN item.read IS NULL AND item.deleted IS NULL THEN 1 ELSE 0 END) AS numberOfUnread,
72      COALESCE(directory.name, channel.customTitle, channel.title) AS title,
73      COALESCE(channel.directoryId, channel.id) AS identifier,
74      GROUP_CONCAT(DISTINCT channel.id) AS channelIds
75      FROM item
76      JOIN channel
77       ON item.channelId = channel.id
78      LEFT JOIN directory
79       ON channel.directoryId = directory.id
80      GROUP BY identifier
81      HAVING numberOfUnread > 0
               ^^^^^^^^^^^^^^
82      ORDER BY directory.name COLLATE NOCASE ASC, COALESCE(channel.customTitle, channel.title) COLLATE NOCASE ASC

I can run the query fine on my phone using the Database Inspector.

I found an awkward workaround though:

-  GROUP BY identifier
-  HAVING numberOfUnread > 0
+  GROUP BY channel.id

and then on Kotlin side I can do the grouping:

queryWrapper.itemQueries.unreadItems().executeAsList()
    .groupBy { it.identifier }
    .map { (_, values) ->
      values.reduce { acc, unreadItems ->
        UnreadItems(
          numberOfUnread = acc.numberOfUnread + unreadItems.numberOfUnread,
          title = unreadItems.title,
          identifier = unreadItems.identifier,
          channelIds = "${acc.channelIds},${unreadItems.channelIds}"
        )
      }
    }
      .filter { it.numberOfUnread > 0 }

fixed on master