Commit cb2d824
authored
Use leastRestrictive for mvappend element-type widening (#5424)
* Use leastRestrictive for mvappend element-type widening
`MVAppendFunctionImpl.updateMostGeneralType` used strict {@code Object.equals}
to compare each operand's component type against the running "most general"
type, falling back to Calcite's {@code ANY} on any mismatch. That's too
aggressive: {@code Object.equals} returns false for type pairs that differ
only in nullability tag (e.g. {@code array(1, 2)} synthesizes INTEGER NULLABLE
for its component while literal {@code 3} is INTEGER NOT NULL), and for
straightforwardly-widenable numerics like INTEGER + DOUBLE. The PPL UDF result
would then be {@code ARRAY<ANY>}.
The Calcite engine's enumerable runtime tolerates {@code ANY} because
{@code MVAppendImplementor.eval} processes elements through {@code Object} —
the declared element type is unused at execution time. The analytics-engine
route is stricter: substrait can't serialize {@code ANY}, so isthmus throws
{@code UnsupportedOperationException: Unable to convert the type ANY} during
the substrait conversion phase.
Widen with {@link RelDataTypeFactory#leastRestrictive} — the same routine
{@code SqlLibraryOperators.ARRAY} uses for its return-type inference. Falls
back to ANY only when {@code leastRestrictive} returns null (genuinely
incompatible operand types like INT + VARCHAR), preserving the original
behavior on those queries.
# Test plan
* {@code :core:test --tests "*MVAppend*"} — passes (no existing test asserted
on the {@code ANY} fallback).
* Companion to opensearch-project/OpenSearch#21554 — unblocks 8+ tests in
{@code CalciteMVAppendFunctionIT} force-routed through the analytics-engine
path that previously failed with "Unable to convert the type ANY".
Signed-off-by: Kai Huang <ahkcs@amazon.com>
* Restrict mvappend widening to nullability-only mismatch
Calling Calcite's leastRestrictive widens mixed numerics like INT + DECIMAL
to a single common numeric type (e.g. DECIMAL(11,1)). The Calcite engine then
casts each operand to that type at codegen — Integer(1) becomes BigDecimal
with scale 1, which renders as 0.1 (or 0 after JSON round-trip), breaking
testMvappendWithIntAndDouble that expects mvappend(1, 2.5) to return [1, 2.5].
The original goal was just to bridge the nullability-tag mismatch that
synthesizes an array's component as INTEGER NULLABLE versus a bare literal's
INTEGER NOT NULL. Limit the widening to that case via equalSansNullability
and keep the ANY fallback for genuinely different types — preserving the
Calcite engine's heterogeneous-Object[] runtime semantics that pre-existing
tests rely on.
Signed-off-by: Kai Huang <huangkaics@gmail.com>
Signed-off-by: Kai Huang <ahkcs@amazon.com>
* Widen mvappend element type via leastRestrictive + pre-cast operands
The previous nullability-only bridge fixed `array(1, 2) + literal 3` but left
`mvappend(1, 2.5)` falling back to ARRAY<ANY>. ARRAY<ANY> is not
substrait-serializable, so any analytics-engine query through that call
fails at substrait conversion. Aggressive `leastRestrictive` widening was
the obvious next step but earlier triggered a runtime corruption — Integer 1
showed up as 0 in the response — because the Avatica result-set's
ArrayAccessor uses element-type-specific accessors (e.g.
`DoubleAccessor.getDouble` does `(Double) value`), and an Integer cell in a
declared-DOUBLE list triggered a ClassCastException that the error path
masked as `[0, 2.5]`.
Fix the corruption by pre-casting each scalar operand to the call's element
Java class in `MVAppendImplementor` via `EnumUtils.convert`. The result list
is now homogeneously typed at codegen, so Avatica's per-element cast
succeeds. Promote DECIMAL → DOUBLE on the way through `updateMostGeneralType`
because `RowResponseCodec` maps DECIMAL cells to FloatingPoint(DOUBLE)
anyway; an explicit DECIMAL element type triggers Calcite's element coercion
to BigDecimal, which the JSON formatter renders inconsistently across paths.
For genuinely incompatible operand pairs (INT + VARCHAR, …)
`leastRestrictive` returns null and the existing `ANY` fallback stands —
heterogeneous mvappend output stays on the Calcite engine path; only the
analytics-engine route can't emit substrait for those.
Local verification:
- :core:test --tests *MVAppend* — green
- :integ-test:integTest --tests CalciteMVAppendFunctionIT — 15/15
- :integ-test:integTest --tests CalciteArrayFunctionIT — 60/60
Signed-off-by: Kai Huang <huangkaics@gmail.com>
Signed-off-by: Kai Huang <ahkcs@amazon.com>
---------
Signed-off-by: Kai Huang <ahkcs@amazon.com>
Signed-off-by: Kai Huang <huangkaics@gmail.com>1 parent f1f39ef commit cb2d824
1 file changed
Lines changed: 57 additions & 5 deletions
File tree
- core/src/main/java/org/opensearch/sql/expression/function/CollectionUDF
Lines changed: 57 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
| |||
78 | 81 | | |
79 | 82 | | |
80 | 83 | | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
| 84 | + | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
87 | 105 | | |
88 | 106 | | |
89 | 107 | | |
90 | 108 | | |
91 | 109 | | |
92 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
93 | 130 | | |
94 | 131 | | |
95 | | - | |
| 132 | + | |
96 | 133 | | |
97 | 134 | | |
98 | 135 | | |
99 | 136 | | |
100 | 137 | | |
101 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
102 | 154 | | |
0 commit comments