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


##########
be/src/exec/common/variant_util.cpp:
##########
@@ -1297,11 +1372,15 @@ Status 
VariantCompactionUtil::get_extended_compaction_schema(
                                                     
extended_info.sparse_paths, output_schema);
         }
         // variant_max_subcolumns_count == 0 and no typed paths materialized
-        // it means that all subcolumns are materialized, may be from old data
-        else {
+        // under V1 it means that all subcolumns are materialized, may be from 
old data
+        else if (column->variant_serialize_type() == 
VariantSerializeTypePB::VARIANT_SERIALIZE_V1) {
             get_compaction_subcolumns_from_data_types(paths_set_info, column, 
target,
                                                       
extended_info.path_to_data_types,
                                                       output_schema);
+        } else {
+            // under V2 it means no subcolumn: every dynamic path stays 
sparse, and a path chosen to

Review Comment:
   [P2] Account for capped input statistics during V2 sparse compaction
   
   On a single-shard DUPLICATE KEY table with `variant_max_subcolumns_count=0` 
and `variant_max_sparse_column_statistics_size=1`, load 
`(1,{"a":1}),(3,{"a":2})` in one rowset and `(2,{"b":2}),(4,{"a":3})` in 
another. The input segments report `a:2` and `b:1` because each tracks its 
first sparse path. Full compaction keeps the paths sparse and writes `a:3` in 
key order. `check_path_stats` takes its exact comparison branch when the output 
map size equals the cap, compares `a:3` against `a:2`, and rejects the valid 
output with `Path stats not match`. Use input segment and shard statistics 
completeness to decide when exact comparison is valid, and cover this 
two-rowset case.



##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPlanUtil.java:
##########
@@ -1223,6 +1263,24 @@ private static List<Column> 
buildExpectedPhysicalSchema(MTMV mtmv,
     private static boolean isTypeLike(Type type, Type typeOther) {
         if (type.isStringType()) {
             return typeOther.isStringType();
+        } else if (type instanceof org.apache.doris.catalog.VariantType
+                && typeOther instanceof org.apache.doris.catalog.VariantType) {
+            // Storage layout knobs (serialize type, subcolumn budget, 
doc/sparse mode, shard count,
+            // row threshold, stats size) only govern how each table shreds 
its own physical column;
+            // they can differ across upgrades, internal reanalysis contexts, 
or light schema changes
+            // without changing the query's logical VARIANT type.
+            org.apache.doris.catalog.VariantType storedVariant = 
(org.apache.doris.catalog.VariantType) type;
+            org.apache.doris.catalog.VariantType analyzedVariant = 
(org.apache.doris.catalog.VariantType) typeOther;
+            if (storedVariant.getVariantSerializeType() == 
TVariantSerializeType.V1
+                    && analyzedVariant.getVariantSerializeType() == 
TVariantSerializeType.V2
+                    && analyzedVariant.getPredefinedFields().isEmpty()
+                    && !analyzedVariant.getEnableNestedGroup()) {
+                // Pre-4.2 MVs persisted parent-column templates or 
NestedGroup onto pruned sub-path
+                // columns (stored as V1), whereas 4.2 resolves derived 
expressions to fresh V2 defaults.
+                return true;

Review Comment:
   [P2] Keep template checks for direct columns when refreshing upgraded MVs
   
   Before upgrade, create an async MV `SELECT k, v FROM vw` over a view that 
directly projects `v VARIANT<'x': INT>`. After upgrade, alter `vw` to project 
the same names from a table with plain V2 `VARIANT` and the same dotted-key 
setting. `ALTER VIEW` puts the MV in `SCHEMA_CHANGE`. On complete refresh, this 
shortcut accepts the new `Project(k, v: plain V2) → Scan(new_source)` even 
though the stored output still has the V1 integer template. Refresh writes into 
that existing schema, so an input such as `{"x":"001"}` is materialized with a 
numeric `x` under the old template. The previous comparison rejected this 
logical type change. Limit the exception to legacy inferred outputs that 
carried a parent template and retain template checks for direct projections.



##########
regression-test/suites/variant_p0/predefine/variant_different_max_subcolumns_count.groovy:
##########
@@ -42,18 +43,21 @@ suite("variant_different_max_subcolumns_count", "p0") {
     qt_sql "select v['a'], v['b'], v['c'], * from ${table_name} order by k"
 
     sql "DROP TABLE IF EXISTS ${table_name}"
+    // typed paths to sparse is internal: only the session variable sets it
+    sql "set debug_variant_enable_typed_paths_to_sparse = true"

Review Comment:
   [P3] Keep one materialized typed column in the compaction comparison
   
   This setting now applies to both `v2` and `v4` during the second `CREATE 
TABLE`. They have the same templates, a budget of one, and identical input 
values, so the before-and-after queries compare the same storage layout twice. 
Previously `v2` kept typed subcolumns and `v4` let them enter sparse storage. 
Create one of the columns under each debug setting, for example by adding `v4` 
after changing the setting, so the compaction checks still exercise both 
layouts.



##########
regression-test/suites/variant_p0/test_variant_tuning_properties.groovy:
##########
@@ -0,0 +1,149 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("test_variant_tuning_properties") {
+    sql """ set describe_extend_variant_column = true """
+    sql "set debug_variant_max_subcolumns_count = 2048"
+    sql "set debug_variant_enable_doc_mode = false"
+    sql "set debug_variant_doc_materialization_min_rows = 0"
+    sql "set debug_variant_doc_hash_shard_count = 0"
+    sql "set debug_variant_max_sparse_column_statistics_size = 10000"
+    sql "set debug_variant_enable_nested_group = false"
+
+    // when extraction starts: a data file below 
variant_doc_materialization_min_rows keeps its dynamic paths in the
+    // sparse encoding, and compaction extracts them once it writes enough 
rows; doc mode is a separate switch
+    sql "DROP TABLE IF EXISTS test_variant_deferred_subcolumns"
+    sql """
+        CREATE TABLE test_variant_deferred_subcolumns (
+            k INT,
+            payload VARIANT<PROPERTIES(
+                "variant_max_subcolumns_count" = "128",
+                "variant_doc_materialization_min_rows" = "5",
+                "variant_doc_hash_shard_count" = "1",
+                "variant_enable_doc_mode" = "false"
+            )>
+        )
+        DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1
+        PROPERTIES ("replication_num" = "1", "disable_auto_compaction" = 
"true")
+    """
+    sql """INSERT INTO test_variant_deferred_subcolumns VALUES
+        (1, parse_to_variant('{"user_id": 101, "action": "click", 
"campaign_a": 7}')),
+        (2, parse_to_variant('{"user_id": 102, "action": "view", "os_name": 
"iOS"}')),
+        (3, parse_to_variant('{"user_id": 103, "action": "click", 
"source_page": "home"}'))"""
+    sql """INSERT INTO test_variant_deferred_subcolumns VALUES
+        (4, parse_to_variant('{"user_id": 104, "action": "view", "tag_a": 
"new"}')),
+        (5, parse_to_variant('{"user_id": 105, "action": "click"}')),
+        (6, parse_to_variant('{"user_id": 106, "action": "view", 
"device_flag": true}'))"""
+    qt_deferred_load "DESC test_variant_deferred_subcolumns"
+    order_qt_deferred_load_read """SELECT k, payload, payload['user_id'], 
payload['action'], payload['os_name']
+        FROM test_variant_deferred_subcolumns"""
+    trigger_and_wait_compaction("test_variant_deferred_subcolumns", "full")
+    qt_deferred_compacted "DESC test_variant_deferred_subcolumns"
+    order_qt_deferred_compacted_read """SELECT k, payload, payload['user_id'], 
payload['action'], payload['os_name']
+        FROM test_variant_deferred_subcolumns"""
+
+    // the row threshold can change: new data files follow the new one
+    sql """ALTER TABLE test_variant_deferred_subcolumns MODIFY COLUMN payload 
VARIANT<PROPERTIES(
+        "variant_max_subcolumns_count" = "128",
+        "variant_doc_materialization_min_rows" = "1000",
+        "variant_doc_hash_shard_count" = "1",
+        "variant_enable_doc_mode" = "false")>"""
+    waitForSchemaChangeDone {
+        sql """SHOW ALTER TABLE COLUMN WHERE TableName = 
'test_variant_deferred_subcolumns'
+            ORDER BY CreateTime DESC LIMIT 1"""
+        time 600
+    }
+    qt_deferred_altered "DESC test_variant_deferred_subcolumns"
+
+    // doc mode keeps the whole document and still extracts up to the count, 
the most frequent paths first
+    sql "DROP TABLE IF EXISTS test_variant_doc_mode_count"
+    sql """
+        CREATE TABLE test_variant_doc_mode_count (
+            event_id BIGINT NOT NULL,
+            payload VARIANT<PROPERTIES(
+                "variant_max_subcolumns_count" = "2",
+                "variant_doc_materialization_min_rows" = "0",
+                "variant_doc_hash_shard_count" = "1",
+                "variant_enable_doc_mode" = "true"
+            )>
+        )
+        DUPLICATE KEY(event_id) DISTRIBUTED BY HASH(event_id) BUCKETS 1
+        PROPERTIES ("replication_num" = "1", "disable_auto_compaction" = 
"true")
+    """
+    sql """INSERT INTO test_variant_doc_mode_count VALUES
+        (1, parse_to_variant('{"user_id": 101, "action": "click", 
"campaign_a": 7, "experiment_a": "A", "tag_a": "new"}')),
+        (2, parse_to_variant('{"user_id": 102, "action": "view", 
"device_flag": true, "os_name": "iOS", "tag_b": "returning"}')),
+        (3, parse_to_variant('{"user_id": 103, "action": "click", 
"experiment_b": "B", "source_page": "home", "device_model": "M1"}'))"""
+    qt_doc_mode_load "DESC test_variant_doc_mode_count"
+    order_qt_doc_mode_document "SELECT payload FROM 
test_variant_doc_mode_count WHERE CAST(payload['user_id'] AS BIGINT) = 101"
+    order_qt_doc_mode_paths """SELECT event_id, payload['action'], 
payload['os_name'], payload['tag_a']
+        FROM test_variant_doc_mode_count"""
+    trigger_and_wait_compaction("test_variant_doc_mode_count", "full")

Review Comment:
   [P2] Make the DOC regression merge multiple data rowsets
   
   The single `INSERT` leaves one data rowset after the empty initial rowset. 
`FullCompaction::pick_rowsets_to_compact` skips this layout, and the helper can 
finish when the failed preparation updates the timestamp. Cloud full compaction 
declines the same layout with the ignored `-808` status. Both of the 
post-compaction assertions can therefore pass against the original load. Split 
the data across two `INSERT`s and verify that the rowset merge happened before 
checking the DOC budget and values.



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