airborne12 opened a new pull request, #71130:
URL: https://github.com/apache/doris/pull/71130

   ### Background
   
   An index added to a table that already holds data takes one of two paths. A 
light index change only adds the index to the metadata: new data is indexed, 
and old data waits for `BUILD INDEX idx ON t [PARTITION (...)]` (one index 
change job per partition) or compaction. A heavy schema change rewrites all 
existing data, so the index covers every row when the job finishes. Today the 
path depends on the FE config `enable_light_index_change`, the session variable 
`enable_add_index_for_new_data` (#52251), the index type and the deployment 
mode.
   
   ### What problem does this PR solve?
   
   Issue Number: close #71129
   Related PR: #52251 (added enable_add_index_for_new_data)
   PR Type: Feature
   
   Problem Summary:
   
   Users adding an index to a large table want to choose per statement whether 
old data is indexed now or later. The session variable is ignored by local 
INVERTED and ANN indexes, so local mode has no single statement that adds an 
index and indexes old data, and an ALTER that cannot take the light path 
silently rewrites all data. This PR adds `BUILD IMMEDIATE | DEFERRED` to `ALTER 
TABLE ... ADD INDEX` and `CREATE INDEX`. Statements without the clause behave 
as before, so existing users see no change.
   
   ### Details
   
   - **Change map:** about 190 lines of production code, 380 of FE unit tests, 
120 of regression test and output. Parser (`DorisParser.g4`, 
`LogicalPlanBuilder`, `CreateIndexOp`, about 35 lines): parse and print the 
clause. `SchemaChangeHandler` (about 115): resolve light, light plus build 
jobs, or heavy for each ADD INDEX before metadata changes. 
`Index.isBuildIndexSupported` with `BuildIndexOp` (about 25): one rule for 
which index BUILD INDEX can build. `SessionVariable`: marked deprecated.
   - **Scope:** the clause follows the index definition of ADD INDEX and CREATE 
INDEX, not CREATE TABLE.
     - `BUILD DEFERRED` only changes metadata. It fails, before any change, for 
a tokenized INVERTED or an ANN index in cloud mode, when 
`enable_light_index_change` is false, and next to a clause other than a light 
ADD INDEX.
     - `BUILD IMMEDIATE` indexes old data by the time the statement's jobs 
finish. For INVERTED and ANN in local mode it adds the index and submits one 
build job per partition (visible in `SHOW BUILD INDEX`, cancellable with 
`CANCEL BUILD INDEX`); otherwise, or next to other clauses, it runs the heavy 
schema change. It fails for a VARIANT values index and on a table that has one, 
where BUILD INDEX is not supported.
   - **Design:** reuses `buildMode` (`BUILD IMMEDIATE | DEFERRED`) of `CREATE 
MATERIALIZED VIEW`, so no keyword is added, and pairs with the existing `BUILD 
INDEX` verb, like Db2 `DEFER YES` + `REBUILD INDEX` and ClickHouse `ADD INDEX` 
+ `MATERIALIZE INDEX`. Rejected: an index property, because properties are 
persisted and sent to BE while the mode belongs to one statement; always 
new-data-only, because some types cannot be added light or built in some modes 
and the cloud default would change; keeping only the session variable, because 
the SQL text, and so the binlog, would still not say which path ran. Proposed 
in #71129, opened with this PR; not discussed yet.
   - **Usage:**
   
     ```sql
     ALTER TABLE logs ADD INDEX idx_msg (msg) USING INVERTED PROPERTIES 
("parser" = "english") BUILD DEFERRED;
     BUILD INDEX idx_msg ON logs PARTITION (p20261128);
     CREATE INDEX idx_uid ON events (uid) USING INVERTED BUILD IMMEDIATE;
     ```
   
   - **Default behavior:** without the clause, `enable_add_index_for_new_data` 
(default false) and the type rules decide as before; the variable is read only 
for such statements.
   - **Compatibility:** FE only; no Thrift, Protobuf, EditLog or image change, 
and replay does not parse the SQL. Older FEs cannot parse the clause during a 
rolling upgrade, and a CCR downstream that replays the binlog SQL must run a 
version that parses it.
   - **Persistence and failure handling:** IMMEDIATE on the light path logs the 
index metadata, then one `IndexChangeJob` per partition, under the table write 
lock, as a light DROP INDEX does. A master failure between them leaves the 
index as if added with DEFERRED; `BUILD INDEX` completes it. Build jobs wait 
for loads that started before them.
   - **Deployment modes:** local: DEFERRED is light for every type; IMMEDIATE 
builds INVERTED and ANN with jobs and runs heavy for BLOOMFILTER and NGRAM_BF. 
Cloud: DEFERRED is light for non-tokenized INVERTED, NGRAM_BF and BLOOMFILTER 
and fails for others; IMMEDIATE runs heavy.
   - **Follow-up plan:** doris-website docs; light plus build jobs for 
IMMEDIATE in cloud mode; remove the session variable in a later major version.
   
   ### Testing
   
   - New FE UT, all passing locally: `NereidsParserTest` (both statements, two 
clauses in one ALTER, `toSql` round trip, rejected in CREATE TABLE); 
`IndexTest#testBuildIndexSupported`; `SchemaChangeHandlerTest` (local DEFERRED 
light for INVERTED, NGRAM_BF, BLOOMFILTER with the variable off; DEFERRED 
rejected next to ADD COLUMN, a heavy ADD INDEX, and with the config off, 
leaving the table unchanged; IMMEDIATE build jobs per partition for the 
IMMEDIATE index only, heavy for NGRAM_BF, next to ADD COLUMN, and with the 
config off); `CloudIndexTest` (DEFERRED rejected for tokenized INVERTED, light 
for NGRAM_BF; IMMEDIATE heavy with the variable on); 
`VariantValuesIndexChangeTest`. The new handler cases fail on the unmodified 
`SchemaChangeHandler`.
   - Existing index DDL classes pass unchanged (`SchemaChangeHandlerTest`, 
`CloudIndexTest`, `IndexChangeJobTest`, `CreateTableWithBloomFilterIndexTest`, 
`GramDdlValidationTest` and others, 305 tests).
   - Regression `inverted_index_p0/index_change/test_add_index_build_mode` with 
`enable_match_without_inverted_index = false`: after DEFERRED, MATCH on old 
data fails and on new data succeeds, and old data succeeds after BUILD INDEX; 
after IMMEDIATE, old data succeeds once its two build jobs finish. Run twice on 
a local shared-nothing cluster; cloud mode is left to cloud_p0.
   - `build.sh --fe`: 0 checkstyle violations.
   
   ### Release note
   
   `ALTER TABLE ... ADD INDEX` and `CREATE INDEX` accept `BUILD DEFERRED` 
(index only new data; build old data later with `BUILD INDEX`) and `BUILD 
IMMEDIATE` (index old data as part of the statement). Without the clause 
nothing changes; `enable_add_index_for_new_data` is deprecated.
   
   ### Check List (For Author)
   
   - Behavior changed: No
   - Does this need documentation: Yes, the ADD INDEX, CREATE INDEX and BUILD 
INDEX pages of apache/doris-website, in a follow-up PR
   - Pre-submission status: Partially verified: the regression test has not run 
in cloud mode
   
   ### AI Disclosure
   
   - Tool: Claude Code (Claude Opus 5.5)
   - Generated: code, tests, this description
   - Human review: pending author review
   


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