hello-stephen commented on code in PR #70408:
URL: https://github.com/apache/doris/pull/70408#discussion_r4751143182


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/types/VariantType.java:
##########
@@ -130,14 +142,61 @@ public DataType conversion() {
                                 .collect(Collectors.toList()), 
variantMaxSubcolumnsCount, enableTypedPathsToSparse,
                                     variantMaxSparseColumnStatisticsSize, 
variantSparseHashShardCount,
                                     enableVariantDocMode, 
variantDocMaterializationMinRows,
-                                    variantDocShardCount, enableNestedGroup);
+                                    variantDocShardCount, enableNestedGroup, 
variantSerializeType);
+    }
+
+    /** This type with the given serialize type, as a new instance. */
+    public VariantType withSerializeType(TVariantSerializeType serializeType) {
+        return new VariantType(predefinedFields, variantMaxSubcolumnsCount, 
enableTypedPathsToSparse,
+                variantMaxSparseColumnStatisticsSize, 
variantSparseHashShardCount, enableVariantDocMode,
+                variantDocMaterializationMinRows, variantDocShardCount, 
enableNestedGroup, serializeType);
+    }
+
+    /**
+     * Resolves a query expression's type for a newly created CTAS, sync MV, 
or async MTMV column.
+     *
+     * <p>A direct whole-column reference to a managed table column keeps its 
storage layout (and its
+     * V1/V2 serialize type when {@code keepManagedSerializeType} is true for 
MVs, whereas CTAS
+     * upgrades it to V2 and strips source-table templates). An explicit 
{@code CAST(... AS VARIANT<...>)}
+     * preserves its SQL-declared V2 properties and templates, while every 
other VARIANT expression
+     * (sub-paths, {@code parse_to_variant}, {@code coalesce}, {@code UNION}, 
external catalogs, TVFs)
+     * uses fresh default V2 storage properties.
+     */
+    public static DataType resolveForNewColumn(Expression expr, @Nullable 
ConnectContext ctx,
+            boolean keepManagedSerializeType) {
+        Expression unwrapped = expr;
+        while (unwrapped instanceof Alias) {
+            unwrapped = ((Alias) unwrapped).child();
+        }
+        DataType convertedType = unwrapped.getDataType().conversion();
+        if (!(convertedType instanceof VariantType)) {
+            return convertedType;
+        }
+        VariantType converted = (VariantType) convertedType;
+        boolean managedRootColumn = unwrapped instanceof SlotReference
+                && ((SlotReference) unwrapped).getOriginalColumn().isPresent()
+                && ((SlotReference) unwrapped).getSubPath().isEmpty()
+                && ((SlotReference) 
unwrapped).getOriginalTable().filter(TableIf::isManagedTable).isPresent();
+        if (managedRootColumn) {
+            return keepManagedSerializeType
+                    ? converted
+                    : 
converted.withoutPredefinedFields().withSerializeType(TVariantSerializeType.V2);
+        }
+        boolean isSubPathOrNonManaged = unwrapped instanceof SlotReference
+                && (!((SlotReference) unwrapped).getSubPath().isEmpty()
+                        || ((SlotReference) unwrapped).getOriginalTable()
+                                .filter(t -> !t.isManagedTable()).isPresent());
+        if (isSubPathOrNonManaged || converted.getVariantSerializeType() == 
TVariantSerializeType.V1) {
+            return LogicalPlanBuilder.defaultStorageVariantType(ctx);
+        }
+        return converted.withSerializeType(TVariantSerializeType.V2);

Review Comment:
   [P2] Reset storage properties for V2 window outputs
   
   With a populated DUPLICATE KEY source whose `v` is `VARIANT<'x':INT, 
PROPERTIES("variant_max_subcolumns_count"="0")>`, CTAS `SELECT k, 
first_value(v) OVER (ORDER BY k) AS fv FROM src` produces this reduced plan:
   
   ```text
   Project(k, fv)
     Window(first_value(v) OVER (ORDER BY k) AS fv)
       Scan(src[k, v])
   ```
   
   `FirstValue.getDataType()` forwards the source's V2 type, while 
`Alias.toSlot()` creates a window-output slot without its original 
table/column. Neither the managed-column branch nor the subpath/nonmanaged/V1 
reset matches, so this return preserves count zero and the `x` template. The 
computed target consequently skips the default 2048 budget and unexpectedly 
inherits source templates; base CTAS stripped templates unconditionally. The 
inherited template also makes a later comment-only `MODIFY COLUMN` fail the 
schema-template restriction. Distinguish an explicit declared CAST from other 
V2 computed outputs, and cover a window result in the inferred-column tests.



-- 
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]

Reply via email to