Repository navigation
Key recursive CTE UNION rows by value type to close #2881 - #2940
Draft
kuldeepsinh19 wants to merge 1 commit into
Draft
kuldeepsinh19 wants to merge 1 commit into
kuldeepsinh19 wants to merge 1 commit into
Conversation
A recursive CTE with UNION built the key of each row with JSON.stringify, which throws on BigInt and counts NaN and NULL, or a Date and its ISO string, as the same row. Each column value is now keyed with its type. Fixes AlaSQL#2881
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2881
Problem
A recursive CTE with
UNION(notUNION ALL) throws on BigInt, and counts NaN and NULL, or a Date and its ISO string, as one row:Cause
Since #2697,
executeRecursiveCTEinsrc/67withselect.js(the issue mentionssrc/statements/with.js, which isn't on develop) builds the key of each row for theUNIONcheck withJSON.stringify.JSON.stringifythrows on BigInt, writes NaN asnull, and writes a Date as its ISO string.Fix
A new
cteValueKeyhelper in the same file builds the key of each column value with its type, and the row key is the JSON of those value keys:nullandundefinedare both NULL, as beforegetValueOf, so it still matches the plain value, as beforetypeofplusString(value), so NaN equals NaN (a cycle through NaN still ends), and1,1nand'1'stay apartUNION ALLdoesn't change.Existing helpers weren't reused:
deepEqual(used by plainUNION) treats any two Dates as equal, treats NaN as not equal to NaN, and compares every pair of rows.distinctArraywrites every value as a string, so1,1nand'1'collide.Tests
New
test/test2881.js:A, B, C, E and F fail without the change in
src/67withselect.jsand pass with it. D and G pass both ways. The cycle tests intest/test2273-B.jsstill pass.Full suite with Bun 1.4.4 on Node 24: 2531 pass, 213 skip, 11 fail. The 11 failures are the tests that load files from the internet, which had no network access here. The same 11 fail without this change.
prettier --list-differentis clean on the changed files.Not in this PR
Two related problems, left for separate changes:
DISTINCT ONbuilds its key withJSON.stringifytoo (src/424select.js):SELECT DISTINCT ON (n) n FROM ?with[{n: NaN}, {n: null}]returns 1 row, and with BigInt values it throws.UNIONkeeps only one of two different Dates:SELECT d FROM ? UNION SELECT d FROM ?with[{d: new Date(0)}]and[{d: new Date(86400000)}]returns 1 row, becausedeepEqualtreats any two Dates as equal.This change was written with help from an AI coding assistant (Claude Code).