Skip to content

Support SIGNED INTEGER and reject unrecognized CAST targets - #573

Merged
perNyfelt merged 2 commits into
mainfrom
fix/322-signed-unsigned-cast
Nov 17, 2026
Merged

perNyfelt merged 2 commits into
mainfrom
fix/322-signed-unsigned-cast

Conversation

@perNyfelt

Copy link
Copy Markdown
Member

Problem

CAST(... AS SIGNED INTEGER) and CAST(... AS UNSIGNED) were silent no-ops: the value came back unchanged with OTHER metadata. Space normalization turns the targets into signedinteger / unsigned, which match no NumericCastTarget alias and fall through DateTimeFunctions.castLiteral's default -> value. Any unrecognized target (e.g. FOOBAR, JSON, INTERVAL DAY) is a no-op the same way.

Reproduction (verified on main)

Query Before After
SELECT CAST('123' AS SIGNED INTEGER) 123 / java.lang.String, OTHER 123 / java.lang.Integer, INTEGER p=10
SELECT CAST(cyl AS SIGNED INTEGER) FROM mtcars column value unchanged, OTHER 6 / java.lang.Integer, INTEGER
SELECT CAST('3000000000' AS SIGNED INTEGER) "3000000000" / String, OTHER rejected, SQLState 22003 (like SIGNED)
SELECT CAST('3000000000' AS SIGNED) rejected, 22003 unchanged
SELECT CAST('123' AS UNSIGNED) "123" / String, OTHER rejected, SQLState 42000
SELECT CAST(cyl AS UNSIGNED INTEGER) FROM mtcars column value unchanged, OTHER rejected, 42000
SELECT CAST('x' AS FOOBAR) "x" / String, OTHER rejected, 42000
SELECT CONVERT(SIGNED INTEGER, '123') "123" / String, OTHER 123 / java.lang.Integer, INTEGER
SELECT CONVERT(UNSIGNED, '123') "123" / String, OTHER rejected, 42000
SELECT CAST(t AS TIMESTAMP WITH TIME ZONE) … (regression guard) OffsetDateTime unchanged

SIGNED semantics are intentionally kept at the established 32-bit INTEGER (tested since #238) rather than MySQL's 64-bit BIGINT; SIGNED INTEGER is a synonym of SIGNED. UNSIGNED [INTEGER] names no JParq target (there is no unsigned integer type), so it is rejected with 42000 instead of being accepted as a no-op.

Root cause

NumericCastTarget lists no alias for the space-joined SIGNED INTEGER spelling, and castLiteral's switch default returned the input for every name it does not convert, so both the value and the metadata (which resolves through the same name lists) silently reported the operand. SqlParser.isConvertTypeName likewise did not recognize the multi-word or unsigned spellings for CONVERT argument disambiguation.

Fix (metadata and value side share the name lists)

  • NumericCastTarget: INTEGER gains the signedinteger alias, so SIGNED INTEGER / SIGNED_INTEGER convert like SIGNED in CAST and CONVERT, numeric metadata, and set operations. A new isUnsupportedAlias names the unsigned spellings (underscores retained so column names like big_int keep their value role).
  • DateTimeFunctions.castLiteral: the default branch now throws SQLState 42000 (Invalid data type for CAST: <type>) for unrecognized targets, except the temporal aliases (DATETIME, TIME [WITHOUT] TIME ZONE, TIMESTAMP [WITHOUT] TIME ZONE) whose conversion the temporal promotion path applies; their pass-through is preserved and regression-tested.
  • SqlParser.isConvertTypeName: recognizes space-joined numeric spellings (spaces stripped, underscores not) and the unsigned spellings, so type-first CONVERT resolves them to a CAST that fails with 42000 instead of silently returning the operand.

Tests

jparq.SignedUnsignedCastTest (13 tests): SIGNED / SIGNED INTEGER / SIGNED_INTEGER declare INTEGER/Integer and convert literals and columns; truncation and 22003 overflow for oversized values, identical to SIGNED; UNSIGNED / UNSIGNED INTEGER rejected with 42000 for literal and column operands; FOOBAR / JSON / INTERVAL DAY rejected with 42000; a NULL operand keeps the signed declaration; unsigned inside a scalar subquery is rejected; cast-style CONVERT shares the signed declaration and the unsigned rejection for both argument orders; set operations see the declared INTEGER branch (empty signed branch + character peer rejected, numeric peer merges).

Verified: full mvn -o verify green (4048 tests, 0 failures), 0 Checkstyle/PMD/Spotless violations.

Fixes #322.

@perNyfelt perNyfelt left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the diff against main and verified every claim in the PR description with runtime probes on both main and the PR head, plus a full mvn -o verify. (Comment review: GitHub does not allow approving my own PR.)

Verified behavior (probed on main vs head):

  • CAST('123' AS SIGNED INTEGER) / SIGNED_INTEGER: main returned String/OTHER; head returns Integer/INTEGER p=10, column operands convert, 22003 overflow identical to SIGNED.
  • CAST('123' AS UNSIGNED) / UNSIGNED INTEGER / FOOBAR / JSON / INTERVAL DAY: main was a silent no-op (e.g. CAST(cyl AS FOOBAR) returned 6/Integer, CONVERT(UNSIGNED INTEGER, model) returned the row value); head rejects with 42000 "Invalid data type for CAST".
  • Temporal pass-throughs are sound: CAST(... AS DATETIME), TIME [WITHOUT] TIME ZONE, TIMESTAMP [WITHOUT] TIME ZONE still convert correctly (conversion is applied by the declared-type coercion path after castLiteral; e.g. CAST('2023-01-01T10:00:00Z' AS DATETIME) → Timestamp in local zone; the TIMESTAMP WITH TIME ZONE regression guard holds).
  • CONVERT disambiguation: CONVERT(SIGNED INTEGER, '123') → Integer 123, CONVERT(model, UNSIGNED) and CONVERT(UNSIGNED, '123') → 42000; underscored names keep their column role (only the exact unsigned spellings are type-positioned), consistent with the pre-existing type-beats-column precedence for e.g. int.
  • The navigationDefault (LAG/LEAD default) and LiteralConverter folding call sites cannot reach the new throw with unhandled names: navigation defaults are rejected at prepare time ("LAG default type X is not assignable to Y") before castLiteral, and verified LAG(ARRAY[model], 1, 5) / LAG(model, 1, 5) / LAG(NULL, 1, 'a') behave as before.

Existing feedback check: the PR has no prior review threads, reviews, or comments (GraphQL reviewThreads/reviews/comments all empty), so nothing outstanding to re-check.

Build: full mvn -o verify green on this head — 4051 tests, 0 failures/errors, 0 Checkstyle/PMD/Spotless violations, JaCoCo coverage check passed.

Out-of-scope findings (tracked, no new issue created):

  1. Value-first CONVERT with space-joined or parameterized targets is still a parse error (e.g. CONVERT(model, SIGNED INTEGER), CONVERT(model, VARCHAR(10)), CONVERT('123', SIGNED)). Verified identical on main, so pre-existing and out of scope here; it is the same JSqlParser source-first CONVERT grammar gap already tracked in #465.

Observations (non-blocking, no action requested):
2. Minor DRY note: NumericCastTarget.normalize duplicates DateTimeFunctions.normalizeTypeName (equivalent: lowercase, trim, strip parameters/spaces/underscores). Keeping the enum self-contained is defensible, but a shared helper could collapse the two.

@perNyfelt

Copy link
Copy Markdown
Member Author

Addressed observation #2 in 41b54a0: removed the private NumericCastTarget.normalize and had fromName delegate to the canonical DateTimeFunctions.normalizeTypeName (functionally identical: lowercase, trim, strip parameters/spaces/underscores). isUnsupportedAlias keeps its inline normalization because it deliberately retains underscores so column names like big_int keep their value role. Full mvn -o verify green on the new head (4048 tests, 0 violations).

@perNyfelt perNyfelt left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of the updated head (new commit 41b54a0). Comment review: GitHub does not allow approving my own PR.

Status of existing feedback:

  • My earlier review observation #2 (DRY: NumericCastTarget.normalize duplicated DateTimeFunctions.normalizeTypeName) — fixed in 41b54a0: the private normalize is removed and fromName now delegates to the canonical DateTimeFunctions.normalizeTypeName. I verified the delegation is functionally identical (both lowercase, trim, strip parameters, spaces, and underscores; the paren-strip/trim ordering difference is unobservable) and re-probed the head: CAST('123' AS SIGNED INTEGER) → Integer 123, CAST(cyl AS SIGNED_INTEGER) → 6/Integer, CAST('123' AS UNSIGNED) and CONVERT(model, UNSIGNED) → 42000, CONVERT(SIGNED INTEGER, '123') → Integer 123, CAST(... AS TIMESTAMP WITH TIME ZONE) → OffsetDateTime — all unchanged from the previously reviewed head. isUnsupportedAlias correctly keeps its inline normalization since it deliberately retains underscores.
  • My earlier review observation #1 (value-first CONVERT parse gap, e.g. CONVERT(model, SIGNED INTEGER)) — still open, not applicable to this PR: pre-existing JSqlParser grammar limitation, unchanged on this head, tracked in #465.
  • Your PR comment (16:32) claiming observation #2 addressed and full verify green — confirmed: the commit matches the description and I re-ran the full build.
  • No review threads exist (GraphQL reviewThreads empty), and there are no Copilot or bot (nordnet-claude-app) reviews or comments on this PR, so nothing else outstanding.

Build: full mvn -o verify green on the new head — 4048 tests + 20 integration tests, 0 failures/errors, 0 Checkstyle violations, PMD and Spotless passed.

The follow-up commit is minimal and surgical (one file, +2/−7); the engine → engine.function class reference already exists in both directions (e.g. ScalarExpressionSchemas → DateTimeFunctions, DateTimeFunctions → NumericCastTarget) with no static-initialization cycle, so the refactor introduces no new coupling concerns.

No remaining issues found in the PR.

@perNyfelt
perNyfelt merged commit bfbfe1d into main Nov 17, 2026
2 checks passed
@perNyfelt
perNyfelt deleted the fix/322-signed-unsigned-cast branch November 17, 2026 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CAST(... AS SIGNED INTEGER) and CAST(... AS UNSIGNED) are silent no-ops returning the input unchanged

1 participant