hello-stephen commented on code in PR #69304:
URL: https://github.com/apache/doris/pull/69304#discussion_r4784320043
##########
be/src/exprs/aggregate/aggregate_function_state_union.h:
##########
@@ -104,18 +164,58 @@ class AggregateStateUnion : public
AggregateFunctionNonFinalBase,
bool is_trivial() const override { return false; }
- size_t size_of_data() const override { return _function->size_of_data(); }
+ size_t size_of_data() const override {
+ if (tracks_input()) {
+ // Keep the nested state at offset zero. Reserve one suffix byte
and
+ // round the stride up so each state in a batch stays aligned.
+ const size_t alignment = align_of_data();
+ return (_nested_state_size + alignment) / alignment * alignment;
+ }
+ return _nested_state_size;
+ }
size_t align_of_data() const override { return _function->align_of_data();
}
private:
+ // The original aggregate signature cannot describe outer SQL NULL. A
+ // BITMAP NOT NULL state, for example, has no nested nullable flag. Record
+ // whether UNION/MERGE received any SQL value in their own intermediate
state.
+ // Query version 21 adds this flag; old queries retain their wire format
and
+ // reject outer nullable input in AggFnEvaluator, as old BEs already do.
+ // insert_result_into() still writes only the nested persistent state
bytes.
Review Comment:
[P2] Preserve no input when a bitmap UNION emits its final state
At version 21, group two rows separately with `s = CASE WHEN p = 0 THEN NULL
ELSE bitmap_intersect_state(to_bitmap(p)) END` and `p` values 0 and 7. The
nested signature is `BITMAP NOT NULL` because `to_bitmap` is nonnullable.
Compute `bitmap_intersect_union(s)` per group, then feed those results to
`bitmap_intersect_merge`. A flat merge skips the SQL NULL and returns `{7}`,
but the grouped form returns the empty set.
The new flag survives partial merges of the same aggregate. At finalization,
`insert_result_into` discards it and the plain bitmap serializer writes a real
empty bitmap for the NULL-only group. The existing partial-merge test completes
the merge before this boundary. Preserve an explicit no-input state across
finalized UNION results, or reject signatures that cannot represent it, and
cover a grouped UNION followed by a separate MERGE.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/types/AggStateType.java:
##########
@@ -130,21 +146,22 @@ public AggStateType resolveDeclaration() {
return new AggStateType(functionName,
nested.children().stream().map(Expression::getDataType).collect(Collectors.toList()),
nested.children().stream().map(Expression::nullable).collect(Collectors.toList()),
- returnNullable, nested.getDataType());
+ returnNullable, nested.getDataType(), beExecVersion);
Review Comment:
[P2] Bind a prepared CAST to the execution codec
Server-prepare the following SELECT at v20 and execute it after raising the
FE setting to v21, with nullable bitmap values NULL and `{7}` in `src`.
```sql
SELECT bitmap_to_string(bitmap_intersect_merge(
CAST(bitmap_intersect_state(b) AS AGG_STATE<bitmap_intersect(BITMAP
NULL)>)))
FROM src;
```
`ExecuteCommand` reuses the parsed plan but starts a fresh statement
context, so the fragment and the STATE producer use v21. The parsed CAST target
still carries v20, and `resolveDeclaration` keeps it here. The CAST turns the
no-input marker into a real empty bitmap before MERGE, returning empty instead
of the `{7}` that the same freshly parsed SQL returns at v21. Defer the codec
choice for unbound declarations until analysis of each execution. Keep explicit
codecs on stored types.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/types/AggStateType.java:
##########
@@ -84,18 +88,29 @@ public class AggStateType extends DataType {
*/
public AggStateType(String functionName, List<DataType> subTypes,
List<Boolean> subTypeNullables, boolean returnNullable) {
- this(functionName, subTypes, subTypeNullables, returnNullable, null);
+ this(functionName, subTypes, subTypeNullables, returnNullable, null,
getCurrentBeExecVersion());
}
/** Constructor retaining the producer's result signature. */
public AggStateType(String functionName, List<DataType> subTypes,
List<Boolean> subTypeNullables, boolean returnNullable, DataType
resultType) {
- this(functionName, subTypes, subTypeNullables, returnNullable,
resultType, false);
+ this(functionName, subTypes, subTypeNullables, returnNullable,
resultType, getCurrentBeExecVersion());
Review Comment:
[P2] Keep the source codec when cloning a table schema
Create a v21 table with `b AGG_STATE<bitmap_intersect(BITMAP NULL)> GENERIC`
and store a no-input state and a `{7}` state under different keys. Run `CREATE
TABLE clone LIKE source` while the FE is pinned to v20, then copy the rows with
`INSERT INTO clone SELECT * FROM source` at v21. The source merge returns
`{7}`. The clone merge returns empty.
`CreateTableLikeCommand` captures the original aggregate state type, but
`ColumnDefinition.copyAggStateType` calls this constructor without the source
codec. The clone silently becomes v20, so the later copy converts the v21
no-input marker into a real empty bitmap. Carry the source codec along with its
effective result signature, as the same-signature MODIFY path already does.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/types/AggStateType.java:
##########
@@ -187,19 +218,72 @@ public String getFunctionName() {
return functionName;
}
+ public int getBeExecVersion() {
+ return beExecVersion;
+ }
+
+ /** Keep the aggregate signature when choosing a producer's output codec.
*/
+ public AggStateType withBeExecVersion(int version) {
+ return new AggStateType(functionName, subTypes, subTypeNullables,
returnNullable,
+ resultType, resultTypeInferredFromLegacy, version);
+ }
+
+ /** Compare the aggregate signature, without asserting that its state
bytes are compatible. */
+ public boolean hasSameAggregateSignature(AggStateType other) {
+ return functionName.equals(other.functionName)
+ && subTypes.equals(other.subTypes)
+ && subTypeNullables.equals(other.subTypeNullables)
+ && returnNullable == other.returnNullable
+ && Objects.equals(resultType, other.resultType);
+ }
+
+ /**
+ * Select a common stored type for UNION and conditional expressions.
+ * Use the same codec contract as explicit CAST, but only select a newer
version.
+ * Do not select the older type: its Bitmap cast can discard the no-input
distinction.
+ * Use canCastStoredStateFrom for an explicit conversion to a sink type.
+ */
+ public boolean canUpgradeStoredStateFrom(AggStateType source) {
+ return source.beExecVersion < beExecVersion &&
canCastStoredStateFrom(source);
+ }
+
+ /**
+ * Convert a stored state between the known storage codecs, without
changing its signature.
+ * The BE reuses byte-compatible columns. For nullable BITMAP_INTERSECT, a
cast from
+ * version 21 to 19 or 20 converts the no-input marker to the legacy empty
Bitmap.
+ * That conversion uses the target codec's merge behavior. It is not a
byte-only relabel.
+ * Keep version-aware identity so the planner retains this CAST at the
output boundary.
+ */
+ public boolean canCastStoredStateFrom(AggStateType source) {
+ // SUM stores its numeric result without a versioned frame. Nullable
SUM
+ // adds the same validity byte in versions 8 through 21. For example, a
+ // stored SUM(BIGINT NULL) at version 18 can be read at 21 and copied
back.
+ // Keep the proven 19..21 range for other functions. Some complex
states
+ // embed column frames: ANY_VALUE over a nonnullable SUM state embeds a
+ // FixedLengthObject frame, which changes at version 10. Do not infer
its
+ // compatibility from SUM's bytes or from equal aggregate signatures.
+ int minimumVersion = functionName.equals("sum") ? 8 : 19;
Review Comment:
[P2] Allow copies of byte-compatible version-18 AVG states
Create an `AGG_STATE<AVG(DOUBLE NULL)>` column at supported execution
version 18. After moving the FE to 21, copying that column into a
same-signature v21 sink now fails. The reduced plan is `Sink(s@21) →
Project(Cast(old.s@18 AS state@21)) → Scan(old)`. `CheckCast` rejects the
stored-slot cast at this version floor, and the direct STATE producer rewrite
cannot apply to a stored slot.
For this signature, the base and head both write the nullable validity byte
followed by a Double sum and UInt64 count. The AVG payload has no frame change
at 19. Extend the compatibility check for this proven layout and test a stored
AVG copy across the versions. Keep the stricter rule for functions whose frames
really differ.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/util/TypeCoercionUtils.java:
##########
@@ -1294,6 +1294,18 @@ private static Optional<DataType>
getCommonDataTypeWithFixedNumericType(
return Optional.empty();
}
+ private static Optional<DataType> findCommonAggStateType(AggStateType
left, AggStateType right) {
+ // A UNION of stored version 20 and version 21 states needs version 21.
+ // Reuse the storage-cast contract; a larger version alone does not
prove
+ // compatibility. Keep type equality version-aware so the cast is
inserted.
+ if (left.equals(right) || left.canUpgradeStoredStateFrom(right)) {
Review Comment:
[P2] Coerce compatible nested states when the outer codecs match
At query version 21, let `old.s` be a NOT NULL codec-20
`AGG_STATE<SUM(BIGINT NOT NULL)>` slot. This reduced plan no longer binds.
```text
UnionAll
Project(any_value_state(old.s@20)@21)
Project(any_value_state(sum_state(CAST(1 AS BIGINT))@21)@21)
```
Both outer ANY_VALUE states use codec 21. Equality sees the different inner
SUM versions, but neither `canUpgradeStoredStateFrom` considers a conversion
because the outer versions match. The nested SUM states use the same
FixedLengthObject layout at 20 and 21, so the planner rejects a UNION whose
bytes are compatible. Check proven compatibility recursively and choose a
common type when only nested codecs differ. Keep rejecting nested formats that
would require a real payload conversion, such as a bitmap-marker downgrade.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]