airborne12 commented on code in PR #71130:
URL: https://github.com/apache/doris/pull/71130#discussion_r4734030580
##########
fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java:
##########
@@ -3914,6 +3929,92 @@ 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);
+ 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,
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 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.
+ if (!lightEnabled || !index.isLightAddIndexSupported(false) ||
!onlyAddIndex) {
+ return AddIndexPath.HEAVY;
+ }
+ Preconditions.checkState(index.isBuildIndexSupported(sniiTable),
index.getIndexName());
+ if (tableHasValuesIndex) {
+ throw new DdlException("BUILD IMMEDIATE is not supported on a
table with a VARIANT values index,"
+ + " because BUILD INDEX is not supported on it");
+ }
+ if (variantColumn.isPresent()) {
+ throw new DdlException("BUILD IMMEDIATE is not supported for index
" + index.getIndexName()
+ + " on VARIANT column " + variantColumn.get().getName()
+ + ", because BUILD INDEX can not build it; use BUILD
DEFERRED instead");
+ }
+ return AddIndexPath.LIGHT_AND_BUILD;
Review Comment:
Confirmed and fixed in b3a04b3c199.
`Tablet::pick_candidate_rowsets_to_build_inverted_index` only takes
`rs->is_local()` rowsets, so on a local table whose rowsets have cooled the
build jobs can finish while the remote rowsets stay unindexed. The heavy schema
change does read remote rowsets (`schema_change.cpp` uses their remote storage
resource), so path resolution now sends BUILD IMMEDIATE to the heavy schema
change whenever a partition of the table has a storage policy. FE can not tell
whether rows have already cooled, so any storage policy counts.
New FE UT
`SchemaChangeHandlerTest#testAddIndexBuildImmediateWithStoragePolicyRunsHeavy`
creates a table with a storage policy; on d56ed51bac1 the IMMEDIATE ADD INDEX
took the light path (table state NORMAL, no schema change job), with the fix it
runs as a heavy schema change job that finishes with the index.
`SchemaChangeHandlerTest`, `VariantValuesIndexChangeTest` and `CloudIndexTest`
pass (71 tests). BUILD DEFERRED and manual BUILD INDEX keep their existing
behavior; the local-only manual BUILD INDEX predates this PR.
##########
fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java:
##########
@@ -2740,6 +2752,9 @@ public int getAsInt() {
// For light index changes, directly modify table metadata
first.
modifyTableLightSchemaChange(rawSql, db, olapTable,
indexSchemaMap, newIndexes,
alterIndexes, isDropIndex, jobId, false, propertyMap);
+ if (!indexesToBuild.isEmpty()) {
+ submitBuildJobs(db, olapTable, indexSchemaMap,
indexesToBuild);
Review Comment:
The window is real: the index metadata and the per-partition build jobs are
separate journal records, so an FE crash between them leaves the index without
build jobs, and a retry with IF NOT EXISTS returns at the duplicate check.
This PR keeps it on purpose, with the same journal order as the existing
light DROP INDEX (metadata first, then its delete jobs). What it costs: queries
stay correct, since segments without the index fall back to the slow MATCH
path; nothing is lost; and `BUILD INDEX` (with `PARTITION (...)` if needed)
completes the build, while BE skips rowsets that already have the index. The PR
description states this under Persistence and failure handling.
Closing it means persisting the build intent with the metadata record and
having the master recreate missing jobs after replay, or a single journal
record for both, which is a new persisted contract. I would rather settle that
with a maintainer than add it to this PR on my own; leaving this thread open
for that decision.
--
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]