Repository navigation
Fix CAST/CONVERT of NULL returning 0, false or "undefined" to close #1208 - #2576
Open
joshuablac wants to merge 1 commit into
Open
joshuablac wants to merge 1 commit into
joshuablac wants to merge 1 commit into
Conversation
…laSQL#1208 CAST(NULL AS <type>) and CONVERT(<type>, NULL) now return undefined (AlaSQL's NULL) for every target type, as in standard SQL. Previously the value was coerced: INT gave 0, NUMBER gave 0 for a null column, STRING gave "undefined"/"null", BOOLEAN gave false, DATE gave "aN.aN.aN" and JSON threw. The coerced 0 was then counted by AVG/SUM/COUNT, which is the wrong AVG reported in AlaSQL#1208. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Closes #1208
Problem
CAST/CONVERTcoerceNULLinstead of passing it through, soAVG(CAST(col AS NUMBER))over NULL values returns0instead ofundefined(the example in #1208). The same root cause shows up for every target type:developCAST(NULL AS INT)/SMALLINT/BIGINT0undefinedCAST(a AS NUMBER)whereaisnull0undefinedCAST(NULL AS VARCHAR)"undefined"("null"for a null column)undefinedCAST(NULL AS BOOLEAN)falseundefinedCAST(NULL AS DATE)"aN.aN.aN"("70.01.01"for a null column)undefinedCAST(NULL AS JSON)Cannot convert string to JSONundefinedAVG / SUM / COUNT(CAST(a AS NUMBER))over[null, 4, missing]1.33 / 4 / 34 / 4 / 1Cause
alasql.stdfn.CONVERT(used by bothCASTandCONVERT) never checks for NULL, sonullgoes throughval | 0,+val,String(val),!!valor the date formatting.Fix
Return
undefinedat the top ofCONVERTwhen the value isnullorundefined. That matches standard SQL (casting NULL to any type gives NULL) and AlaSQL's convention of returningundefinedfor NULL. Non-null values are converted as before.Behavior change to note: anything that relied on
CAST(NULL AS INT)being0(orBOOLEANbeingfalse, etc.) now getsundefined. The same applies toDECLARE/SETon a typed variable assignedNULL, since they also go throughCONVERT. No existing test depended on the old behavior.Tests
test/test1208.js: both queries from the issue,CAST(NULL AS ...)for 17 types, null and missing columns,CONVERTwith a style, aggregates over cast NULLs, and a guard that non-null values still convert. 6 of the 7 cases fail ondevelopand all 7 pass with this change.bun test, same command asyarn test-only): 2256 pass, 213 skip. The only 2 failures are intest2149.js(CLI). They fail identically on unmodifieddevelopin my local setup: one because my checkout path contains spaces, and one because the version-injection step ofbuild.shdidn't run.Note: while testing I noticed that plain
AVG(col)(withoutCAST) returnsnullfor a single-row group whose value isnull, butundefinedfor a multi-row all-null group. This PR doesn't touch that.This fix was written with AI assistance (Claude Code) and verified locally as described above.
🤖 Generated with Claude Code