Conversation
INNER JOIN ON predicates are flattened into WHERE. Cost-based AND reordering could evaluate a[i] before a cardinality/bounds guard, raising ARRAY_ELEMENT_ERROR for indexes the join would exclude. Prefer AND sides that cannot raise array element errors first so short-circuit skips out-of-range array access. JSON accessors still return null; bare out-of-range array[index] still errors. Fixes h2database#4364
katzyn
reviewed
Aug 1, 2026
katzyn
left a comment
Contributor
There was a problem hiding this comment.
Thank you for your contribution!
Unfortunately, this is not a proper solution, it's just a workaround.
The proper fix is to move a premature query transformation from Parser.parseSelectFromPart() into place when we can determine whether transformation is possible or not.
Determination of unsafe expressions also should be performed by a new type of ExpressionVisitor, because array element reference is not the only one dangerous operation, so we need more flexibility here.
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.
Summary
Fixes #4364.
H2 flattens
INNER JOIN ... ONpredicates into the queryWHEREclause.ConditionAndOrthen reorders AND sides by cost. A cheaperarray[i]comparison could therefore run before a more expensive cardinality/bounds guard from the join, raisingARRAY_ELEMENT_ERRORfor indexes the join would have excluded (e.g. Hibernate-style unnest withsystem_range).This change keeps cost-based reordering, then for AND only moves expressions that may raise SQL array element errors to the right so short-circuit can skip them when a safer guard already failed. JSON array accessors still return
NULLfor missing indexes; bare out-of-rangearray[index]still raises as documented/SQL-standard.Changes
ConditionAndOr.optimize: after cost reorder, if left may raise array element error and right does not, swapConditionAndOrN.optimize: for AND, stable-sort so may-raise expressions are lastdatatypes/array.sql(issue Unnecessary WHERE condition evaluation leading to error #4364 repro + related forms)Test plan
= 2returns rowARRAY[1,2,3][4]still raisesARRAY_ELEMENT_ERROR_2TestScript:datatypes/array.sql,indexes.sql,range_table.sql,queries/joins.sql,queries/query-optimisations.sql,other/conditions.sql,predicates/null.sql,functions/system/array-get.sql— green