storesafe / cordova-sqlite-storage

A Cordova/PhoneGap plugin to open and use sqlite databases on Android, iOS and Windows with HTML5/Web SQL API
Other
2.15k stars 716 forks source link

unable to execute window function #1001

Open CodeWithOz opened 2 years ago

CodeWithOz commented 2 years ago

I'm trying to execute a window function but I'm getting this error:

near "(": syntax error (code 1): , while compiling: WITH cte AS (
            SELECT *,
                COUNT(*) OVER (PARTITION BY grp) count,
                ROW_NUMBER() OVER (PARTITION BY grp ORDER BY date) rn
            FROM (
                SELECT *, SUM(flag) OVER (ORDER BY date) grp
                FROM (
                    SELECT *, (type <> LAG(type, 1, '') OVER (ORDER BY date)) flag
                    FROM chatMessages WHERE chatID = 'alternateChat'
                )
            )
        )
        SELECT id, type, date, message,
            CASE WHEN type = 'groupedMsgs' THEN count END numConsecutiveItems
        FROM cte
        WHERE numConsecutiveItems IS NULL OR rn = 1;

The setup is:

CREATE TABLE chatMessages (`id` TEXT, `chatID` TEXT, `date` TEXT, `message` TEXT, `senderID` TEXT, `type` TEXT);

INSERT INTO chatMessages ('chatID', 'date', 'id', 'message', 'senderID', 'type') VALUES
                    ('initialChat', '0001-01-01T01:01:11.001Z', '18c9bb49-09f4-4dc4-b0ba-a3c45c324347', 'first message', 'first_sender_id', 'txt'),
                    ('alternateChat', '0001-02-01T01:01:11.001Z', '18c9bb49-09f4-4dc4-b0ba-a3c45c324348', 'second message', 'first_sender_id', 'groupedMsgs'),
                    ('alternateChat', '0001-03-01T01:01:11.001Z', '28c9bb49-09f4-4dc4-b0ba-a3c45c324348', 'third message', 'first_sender_id', 'groupedMsgs'),
                    ('alternateChat', '0001-04-01T01:01:11.001Z', '18c9bb49-09f5-4dc4-b0ba-a3c45c324348', 'fourth message', 'first_sender_id', 'txt'),
                    ('alternateChat', '0001-05-01T01:01:11.001Z', '18c9bb49-19f4-4dc4-b0ba-a3c45c324348', 'fifth message', 'first_sender_id', 'groupedMsgs');

And the operation that uses window functions is this:

WITH cte AS (
                    SELECT *,
                        COUNT(*) OVER (PARTITION BY grp) count,
                        ROW_NUMBER() OVER (PARTITION BY grp ORDER BY date) rn
                    FROM (
                        SELECT *, SUM(flag) OVER (ORDER BY date) grp
                        FROM (
                            SELECT *, (type <> LAG(type, 1, '') OVER (ORDER BY date)) flag
                            FROM chatMessages WHERE chatID = 'alternateChat'
                        )
                    )
                )
                SELECT id, type, date, message,
                    CASE WHEN type = 'groupedMsgs' THEN count END numConsecutiveItems
                FROM cte
                WHERE numConsecutiveItems IS NULL OR rn = 1;

I've created a sample app that reproduces the problem here, and the code can be viewed here. For good measure I reproduced the exact same operation using this fiddle and you can see that it works there. So I don't understand why it's not working using this plugin. The README makes it clear that window functions were added since #837 so I expect this operation to work.

The full context of what I want to achieve is explained in this stack overflow post, but nevertheless the summary is that I want to group and count consecutive rows based on some matching and ranking criteria. It turns out that window functions can be used to do exactly what I want, but they're not working as shown above. Am I doing something wrong?