airborne12 commented on code in PR #70352:
URL: https://github.com/apache/doris/pull/70352#discussion_r4750729537


##########
fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java:
##########
@@ -2200,37 +2218,84 @@ public int getAsInt() {
                     buildIndexChange = true;
                     lightSchemaChange = false;
                 } else if (alterClause instanceof DropIndexClause) {
-                    if (processDropIndex((DropIndexClause) alterClause, 
olapTable, newIndexes)) {
-                        return;
-                    }
-                    lightSchemaChange = false;
-
                     DropIndexClause dropIndexClause = (DropIndexClause) 
alterClause;
-                    List<Index> existedIndexes = olapTable.getIndexes();
-                    Index found = null;
-                    for (Index existedIdx : existedIndexes) {
-                        if 
(existedIdx.getIndexName().equalsIgnoreCase(dropIndexClause.getIndexName())) {
-                            found = existedIdx;
-                            break;
+
+                    if (dropIndexClause.hasPartitionSpec()) {
+                        // Delete the physical index files and keep the 
table-level index definition.
+                        List<Index> existedIndexes = olapTable.getIndexes();
+                        Index found = null;
+                        for (Index existedIdx : existedIndexes) {
+                            if 
(existedIdx.getIndexName().equalsIgnoreCase(dropIndexClause.getIndexName())) {
+                                found = existedIdx;
+                                break;
+                            }
                         }
-                    }
-                    // for inverted index, light schema change is supported in 
both cloud and local mode;
-                    // for ngram index, light schema change is supported only 
in cloud mode;
-                    boolean supportLightIndexChange = false;
-                    if (Config.isCloudMode()) {
-                        if (enableAddIndexForNewData) {
-                            supportLightIndexChange = (
-                                    found.getIndexType() == IndexType.NGRAM_BF
-                                            || found.getIndexType() == 
IndexDef.IndexType.INVERTED);
+                        if (found == null) {
+                            if (dropIndexClause.isSetIfExists()) {
+                                LOG.info("drop index[{}] which does not exist 
on table[{}]",
+                                        dropIndexClause.getIndexName(), 
olapTable.getName());
+                                return;
+                            }
+                            throw new DdlException("index " + 
dropIndexClause.getIndexName() + " does not exist");
                         }
-                    } else {
-                        supportLightIndexChange = found.getIndexType() == 
IndexDef.IndexType.INVERTED
-                                || found.getIndexType() == 
IndexDef.IndexType.ANN;
-                    }
-                    if (found != null && supportLightIndexChange) {
+                        if (found.getIndexType() != IndexType.INVERTED) {
+                            throw new DdlException(
+                                    "Only inverted index supports DROP INDEX 
ON PARTITION");
+                        }
+                        // For a VARIANT column the BE finds only values 
indexes under the root column, so a
+                        // partition drop of any other index on it would 
finish without deleting anything.
+                        for (String indexColumn : found.getColumns()) {
+                            Column column = olapTable.getColumn(indexColumn);
+                            if (column != null && 
column.getType().isVariantType()) {
+                                throw new DdlException("DROP INDEX ON 
PARTITION is not supported for index "
+                                        + found.getIndexName() + " on VARIANT 
column " + column.getName());
+                            }
+                        }
+                        if (!olapTable.isPartitionedTable()) {
+                            throw new DdlException("table " + 
olapTable.getName()
+                                    + " is not partitioned, cannot drop index 
with partitions");
+                        }
+                        Set<String> specifiedPartitions = new 
HashSet<>(dropIndexClause.getPartitionNames());
+                        for (String partName : specifiedPartitions) {
+                            if (olapTable.getPartition(partName) == null) {
+                                throw new DdlException("partition " + partName 
+ " does not exist");
+                            }
+                        }
+
+                        if (!Config.enable_light_index_change) {
+                            throw new DdlException(
+                                    "DROP INDEX ON PARTITION requires 
enable_light_index_change=true");
+                        }
+                        Set<Long> specifiedPartitionIds = 
specifiedPartitions.stream()
+                                .map(partName -> 
olapTable.getPartition(partName).getId())
+                                .collect(Collectors.toSet());
+                        checkNoIndexChangeJobOnPartitions(db, olapTable,
+                                ImmutableList.of(found), 
specifiedPartitionIds);
+
                         alterIndexes.add(found);
+                        indexOnPartitions.put(found.getIndexId(), 
specifiedPartitions);

Review Comment:
   Confirmed. This is at parity with master, where 
`fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java:2672` 
behaves the same, so it is not introduced by this backport. Fixed on master in 
#71216; I will pick it to branch-4.2 after it merges.



##########
fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java:
##########
@@ -3477,25 +3540,89 @@ public void buildOrDeleteTableInvertedIndices(Database 
db, OlapTable olapTable,
         }
     }
 
+    // Inverted index supports light schema change in both cloud and local 
mode.
+    // NGRAM_BF supports it only in cloud mode.
+    private static boolean isLightDropIndexSupported(Index index, boolean 
enableAddIndexForNewData) {
+        if (Config.isCloudMode()) {
+            return enableAddIndexForNewData
+                    && (index.getIndexType() == IndexType.NGRAM_BF
+                    || index.getIndexType() == IndexType.INVERTED);
+        }
+        return index.getIndexType() == IndexType.INVERTED || 
index.getIndexType() == IndexType.ANN;
+    }
+
+    // Reject conflicting jobs and mixed add/drop operations before publishing 
index metadata.
+    private void checkLightDropIndexPreconditions(Database db, OlapTable 
olapTable, List<AlterClause> alterClauses,
+            boolean enableAddIndexForNewData) throws UserException {
+        if (!Config.enable_light_index_change) {
+            return;
+        }
+        List<Index> droppedIndexes = new ArrayList<>();
+        for (AlterClause alterClause : alterClauses) {
+            if (!(alterClause instanceof DropIndexClause)
+                    || ((DropIndexClause) alterClause).hasPartitionSpec()) {
+                continue;
+            }
+            String indexName = ((DropIndexClause) alterClause).getIndexName();
+            olapTable.getIndexes().stream()
+                    .filter(index -> 
index.getIndexName().equalsIgnoreCase(indexName))
+                    .filter(index -> isLightDropIndexSupported(index, 
enableAddIndexForNewData))
+                    .findFirst()
+                    .ifPresent(droppedIndexes::add);
+        }
+        if (droppedIndexes.isEmpty()) {
+            return;
+        }
+        if (alterClauses.stream().anyMatch(alterClause -> alterClause 
instanceof CreateIndexClause
+                && ((CreateIndexClause) 
alterClause).getIndex().isLightAddIndexSupported(enableAddIndexForNewData))) {
+            throw new DdlException("Can not add and drop indexes in one ALTER 
statement,"
+                    + " please run them in separate statements.");
+        }
+        getCloudClusterName();
+        checkNoIndexChangeJobOnPartitions(db, olapTable, droppedIndexes, new 
HashSet<>(olapTable.getPartitionIds()));
+    }
+
+    // Throws if any of the partitions still has an unfinished index change 
job on the base index.
+    // Jobs are matched by partition ID because a partition can be renamed 
while its job is unfinished.
+    private void checkNoIndexChangeJobOnPartitions(Database db, OlapTable 
olapTable, List<Index> alterIndexes,
+            Set<Long> partitionIds) throws DdlException {
+        // Scan the retained job history once instead of once per partition, 
the table write lock is held here.
+        for (IndexChangeJob indexChangeJob : 
ImmutableList.copyOf(indexChangeJobs.values())) {
+            // Cheap partition check first, the predicate compares index lists.
+            if (partitionIds.contains(indexChangeJob.getPartitionId())
+                    && isConflictingIndexChangeJob(indexChangeJob, 
olapTable.getBaseIndexId(), db.getId(),
+                    olapTable.getId(), alterIndexes)) {
+                throw new DdlException("partition " + 
olapTable.getPartition(indexChangeJob.getPartitionId()).getName()
+                        + " of table " + olapTable.getName()
+                        + " has an unfinished index change job, please retry 
after it finishes.");
+            }
+        }
+    }
+
+    // Jobs are matched by partition ID because a partition can be renamed 
while its job is unfinished.
     public boolean hasIndexChangeJobOnPartition(
-            long originIndexId, long dbId, long tableId, String partitionName,
-            List<Index> alterIndexes, boolean isDrop) {
+            long originIndexId, long dbId, long tableId, long partitionId, 
List<Index> alterIndexes) {
         // TODO: this is temporary methods
         for (IndexChangeJob indexChangeJob : 
ImmutableList.copyOf(indexChangeJobs.values())) {
-            if (indexChangeJob.getOriginIndexId() == originIndexId
-                    && indexChangeJob.getDbId() == dbId
-                    && indexChangeJob.getTableId() == tableId
-                    && indexChangeJob.getPartitionName().equals(partitionName)
-                    && ((Config.isNotCloudMode() && 
indexChangeJob.hasSameAlterInvertedIndex(isDrop, alterIndexes))
-                    || Config.isCloudMode())
-                    && !indexChangeJob.isDone()) {
-                // if JobState is done (CANCELLED or FINISHED), also allow 
user to create job again
+            if (indexChangeJob.getPartitionId() == partitionId
+                    && isConflictingIndexChangeJob(indexChangeJob, 
originIndexId, dbId, tableId, alterIndexes)) {
                 return true;
             }
         }
         return false;
     }
 
+    private static boolean isConflictingIndexChangeJob(IndexChangeJob 
indexChangeJob, long originIndexId,
+            long dbId, long tableId, List<Index> alterIndexes) {
+        // if JobState is done (CANCELLED or FINISHED), also allow user to 
create job again
+        return indexChangeJob.getOriginIndexId() == originIndexId
+                && indexChangeJob.getDbId() == dbId
+                && indexChangeJob.getTableId() == tableId
+                && ((Config.isNotCloudMode() && 
indexChangeJob.hasAlterInvertedIndex(alterIndexes))
+                || Config.isCloudMode())
+                && !indexChangeJob.isDone();

Review Comment:
   Confirmed. This is at parity with master, where 
`fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java:4082` 
behaves the same, so it is not introduced by this backport. Fixed on master in 
#71217; I will pick it to branch-4.2 after it merges.



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