Skip to content

[BugFix] Fix type mismatch in map literal when explicit key/value types are declared#71316

Merged
stephen-shelby merged 11 commits intoStarRocks:mainfrom
trueeyu:case_type
Apr 7, 2026
Merged

[BugFix] Fix type mismatch in map literal when explicit key/value types are declared#71316
stephen-shelby merged 11 commits intoStarRocks:mainfrom
trueeyu:case_type

Conversation

@trueeyu
Copy link
Copy Markdown
Contributor

@trueeyu trueeyu commented Apr 6, 2026

Why I'm doing:

Fix https://github.com/StarRocks/StarRocksTest/issues/11202

When a map literal is written with an explicit type annotation (e.g., map<smallint, smallint>{1: 2, 3: 4}), the FE analyzer failed to cast the child expressions (key/value literals) to the declared key and value types.
Integer literals like 1, 2, 3, 4 are inferred as TINYINT by default. Because visitMapExpr only handled the implicit-type case (AnyMapType.ANY_MAP) and had no logic for the explicit-type case, the children retained their inferred TINYINT types. These mistyped expressions were then serialized and sent to the BE.
On the BE side, MapExpr::evaluate_checked cloned the key column as a FixedLengthColumn<int8_t> (1 byte), but later attempted to append it into a FixedLengthColumnBase<int16_t> (2 bytes), causing a heap-buffer-overflow detected by
AddressSanitizer.

A typical reproducer:

  ALTER TABLE t ADD COLUMN c MAP<SMALLINT, SMALLINT>
  DEFAULT map<smallint, smallint>{1: 2, 3: 4};                                                                                                                                                                                          
                                              
  SELECT id, c FROM t ORDER BY id;  -- crashes BE       

Root Cause

In ExpressionAnalyzer.visitMapExpr, when the map has an explicit MapType (not AnyMapType.ANY_MAP), no casting was applied to the child expressions. The declared key/value types were simply ignored, and children kept their own inferred types.

The same issue did not exist for ArrayExpr — visitArrayExpr already correctly casts children to the declared item type when an explicit array type is present.

Fix

When the map literal has an explicit type, extract the declared keyType and valueType from the MapType and cast each key/value child expression to the corresponding type if it doesn't already match — exactly mirroring the existing pattern in visitArrayExpr.

What I'm doing:

Fix type mismatch in map literal when explicit key/value types are declared

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
    • This pr needs auto generate documentation
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 4.1
    • 4.0
    • 3.5
    • 3.4

@trueeyu trueeyu requested a review from a team as a code owner April 6, 2026 07:05
@mergify mergify bot assigned trueeyu Apr 6, 2026
@CelerData-Reviewer
Copy link
Copy Markdown

@codex review

stdpain
stdpain previously approved these changes Apr 6, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2ef6bb13e3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a FE analysis bug where map literals with an explicit MAP<k,v> type annotation did not cast their key/value child expressions to the declared types, which could lead to a BE heap-buffer-overflow due to mismatched serialized types.

Changes:

  • Update ExpressionAnalyzer.visitMapExpr to cast map literal children to the inferred/common types (implicit) or declared key/value types (explicit).
  • Add a regression unit test covering map<smallint, smallint>{1: 2, 3: 4} analysis behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
fe/fe-core/src/main/java/com/starrocks/sql/analyzer/ExpressionAnalyzer.java Cast map-literal children to key/value target types (handles explicit MapType as well as AnyMapType.ANY_MAP).
fe/fe-core/src/test/java/com/starrocks/sql/analyzer/ExpressionAnalyzerTest.java Adds regression test to ensure map literal children are analyzed with the expected target types.

Signed-off-by: trueeyu <[email protected]>
Signed-off-by: trueeyu <[email protected]>
stdpain
stdpain previously approved these changes Apr 6, 2026
Signed-off-by: trueeyu <[email protected]>
Signed-off-by: trueeyu <[email protected]>
@trueeyu trueeyu marked this pull request as draft April 6, 2026 09:37
trueeyu added 4 commits April 6, 2026 17:41
Signed-off-by: trueeyu <[email protected]>
Signed-off-by: trueeyu <[email protected]>
Signed-off-by: trueeyu <[email protected]>
Signed-off-by: trueeyu <[email protected]>
@trueeyu trueeyu marked this pull request as ready for review April 6, 2026 10:12
stdpain
stdpain previously approved these changes Apr 6, 2026
@alvin-celerdata
Copy link
Copy Markdown
Contributor

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 943ca1b270

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Signed-off-by: trueeyu <[email protected]>
Signed-off-by: trueeyu <[email protected]>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

[FE Incremental Coverage Report]

pass : 18 / 18 (100.00%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/analyzer/ExpressionAnalyzer.java 18 18 100.00% []

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@stephen-shelby stephen-shelby merged commit fdc3329 into StarRocks:main Apr 7, 2026
61 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

@Mergifyio backport branch-4.1

@github-actions github-actions bot removed the 4.1 label Apr 7, 2026
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Apr 7, 2026

backport branch-4.1

✅ Backports have been created

Details

Cherry-pick of fdc3329 has failed:

On branch mergify/bp/branch-4.1/pr-71316
Your branch is up to date with 'origin/branch-4.1'.

You are currently cherry-picking commit fdc3329597.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   fe/fe-core/src/main/java/com/starrocks/sql/analyzer/ExpressionAnalyzer.java
	modified:   fe/fe-core/src/test/java/com/starrocks/sql/analyzer/AnalyzeExprTest.java
	modified:   test/sql/test_default_value/R/test_complex_default_all_paths.sql
	modified:   test/sql/test_default_value/T/test_complex_default_all_paths.sql

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   fe/fe-core/src/test/java/com/starrocks/sql/analyzer/ExpressionAnalyzerTest.java
	both modified:   test/sql/test_map/R/test_map

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

mergify bot pushed a commit that referenced this pull request Apr 7, 2026
…es are declared (#71316)

Signed-off-by: trueeyu <[email protected]>
(cherry picked from commit fdc3329)

# Conflicts:
#	fe/fe-core/src/test/java/com/starrocks/sql/analyzer/ExpressionAnalyzerTest.java
#	test/sql/test_map/R/test_map
wanpengfei-git pushed a commit that referenced this pull request Apr 7, 2026
…es are declared (backport #71316) (#71345)

Signed-off-by: trueeyu <[email protected]>
Co-authored-by: trueeyu <[email protected]>
mergify bot added a commit that referenced this pull request Apr 7, 2026
…es are declared (backport #71316) (#71345)

Signed-off-by: trueeyu <[email protected]>
Co-authored-by: trueeyu <[email protected]>
(cherry picked from commit 329bb2f)
wanpengfei-git pushed a commit that referenced this pull request Apr 7, 2026
…es are declared (backport #71316) (backport #71345) (#71373)

Signed-off-by: trueeyu <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: trueeyu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants