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


##########
fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java:
##########
@@ -3914,6 +3929,97 @@ private String getCloudClusterName() throws 
UserException {
         return clusterName;
     }
 
+    // How one ADD INDEX runs: LIGHT only changes metadata, so only data 
written afterwards gets the index;
+    // LIGHT_AND_BUILD also builds the existing data of every partition 
through index change jobs;
+    // HEAVY rewrites the existing data in a schema change job.
+    private enum AddIndexPath { LIGHT, LIGHT_AND_BUILD, HEAVY }
+
+    // Resolves the path of every ADD INDEX in alterOps. An index added with 
BUILD DEFERRED keeps the existing
+    // data unindexed, so it can not share a statement with a clause that 
rewrites or changes that data.
+    private static Map<CreateIndexOp, AddIndexPath> 
resolveAddIndexPaths(List<AlterOp> alterOps,
+            OlapTable olapTable, boolean enableAddIndexForNewData) throws 
DdlException {
+        boolean onlyAddIndex = alterOps.stream().allMatch(alterOp -> alterOp 
instanceof CreateIndexOp);
+        boolean sniiTable = olapTable.getInvertedIndexFileStorageFormat() == 
TInvertedIndexFileStorageFormat.SNII;
+        boolean tableHasValuesIndex = 
olapTable.getIndexes().stream().anyMatch(Index::isVariantValuesIndex);
+        // A partition with a storage policy may hold rowsets that have cooled 
to remote storage.
+        boolean mayHaveCooledData = 
olapTable.getPartitionIds().stream().anyMatch(partitionId ->
+                
!Strings.isNullOrEmpty(olapTable.getPartitionInfo().getDataProperty(partitionId).getStoragePolicy()));
+        Map<CreateIndexOp, AddIndexPath> paths = new IdentityHashMap<>();
+        for (AlterOp alterOp : alterOps) {
+            if (alterOp instanceof CreateIndexOp) {
+                CreateIndexOp createIndexOp = (CreateIndexOp) alterOp;
+                Index index = createIndexOp.getIndex();
+                // BUILD INDEX can not build an index on a VARIANT column, see 
the BuildIndexOp branch of process.
+                Optional<Column> variantColumn = 
olapTable.getBaseSchema().stream()
+                        .filter(column -> column.getType().isVariantType())
+                        .filter(column -> 
index.getColumns().stream().anyMatch(column.getName()::equalsIgnoreCase))
+                        .findFirst();
+                paths.put(createIndexOp, resolveAddIndexPath(index, 
createIndexOp.getBuildMode().orElse(null),
+                        enableAddIndexForNewData, sniiTable, onlyAddIndex, 
mayHaveCooledData, tableHasValuesIndex,
+                        variantColumn));
+            }
+        }
+        boolean hasDeferred = paths.keySet().stream()
+                .anyMatch(createIndexOp -> 
createIndexOp.getBuildMode().orElse(null) == BuildMode.DEFERRED);
+        if (hasDeferred && (!onlyAddIndex || 
paths.containsValue(AddIndexPath.HEAVY))) {
+            throw new DdlException("BUILD DEFERRED can not be combined with 
other schema changes or with an index"
+                    + " that rewrites the existing data, please run them in 
separate statements.");
+        }
+        return paths;
+    }
+
+    private static AddIndexPath resolveAddIndexPath(Index index, BuildMode 
buildMode,
+            boolean enableAddIndexForNewData, boolean sniiTable, boolean 
onlyAddIndex, boolean mayHaveCooledData,
+            boolean tableHasValuesIndex, Optional<Column> variantColumn) 
throws DdlException {
+        boolean lightEnabled = Config.enable_light_index_change;
+        if (buildMode == null) {
+            return lightEnabled && 
index.isLightAddIndexSupported(enableAddIndexForNewData)
+                    ? AddIndexPath.LIGHT : AddIndexPath.HEAVY;
+        }
+        if (buildMode == BuildMode.DEFERRED) {
+            if (!lightEnabled) {
+                throw new DdlException("BUILD DEFERRED requires 
enable_light_index_change=true");
+            }
+            if (!index.isLightAddIndexSupported(true)) {
+                throw new DdlException("BUILD DEFERRED is not supported for " 
+ index.getIndexType() + " index "
+                        + index.getIndexName() + " in " + 
(Config.isCloudMode() ? "cloud" : "local") + " mode");
+            }
+            return AddIndexPath.LIGHT;
+        }
+        if (index.isVariantValuesIndex()) {
+            throw new DdlException("VARIANT values index supports only BUILD 
DEFERRED");
+        }
+        // Where adding the index is heavy by default, the heavy schema change 
already indexes the existing data.
+        // Next to other clauses, the statement runs as one heavy schema 
change, which indexes all data as well.
+        // BUILD INDEX builds only local rowsets, so data that may have cooled 
also needs the heavy schema change.
+        if (!lightEnabled || !index.isLightAddIndexSupported(false) || 
!onlyAddIndex || mayHaveCooledData) {
+            return AddIndexPath.HEAVY;

Review Comment:
   [P2] Make ANN IMMEDIATE force a rewriting schema change
   
   On a local DUP_KEYS table with historical local rows, no delete predicates, 
and `embedding ARRAY<FLOAT> NOT NULL`, `ALTER TABLE t ADD COLUMN x INT, ADD 
INDEX a(embedding) USING ANN PROPERTIES 
('index_type'='hnsw','metric_type'='l2_distance','dim'='4') BUILD IMMEDIATE` 
takes this HEAVY branch. However, `SchemaChangeJob::parse_request` detects 
index changes through `variant_util::has_schema_index_diff`, which compares 
bloom flags and INVERTED indexes only, so this ANN addition can select 
`LinkedSchemaChange`. That path only links the old files, and the job finishes 
without building the historical ANN index. Queries keep using brute force, and 
manual `BUILD INDEX` can skip these rowsets because their new schema already 
advertises the ANN ID. Ensure ANN differences force an actual rewrite (or 
reject this combination), and add a regression that checks historical ANN use 
after the job finishes.



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