Apache XTable secondary index: faster Iceberg point lookups
A Hudi-backed index maps one column's values to files, so Spark reads 1 of 40 files for a lookup. Setup, every validation message, every reason the rule skips it, and what the runs showed.
- What is stored and where
- Configure and build
- Use it in Spark
- Every reason the index is skipped
- Maintenance
- Checked against the source
- Key takeaways
- References
- Trademarks
TL;DR
The index maps each value of one column to the data files that hold it. A sync builds it, and a Spark session with XTable’s extensions reads one file instead of many for an equality or
INlookup.
- It never changes a result. Every case the rule skips reads the table normally, and the log says why.
- It needs a Hudi target at table version 9 with the Spark execution engine, one top-level column, and a column that does not change later.
- Setting the column also turns on Hudi’s record index, which is why
record_indexappears besidesecondary_index_<column>.- The rule only fires for equality and
INfilters on literals. Range,LIKE, joins, branches, tags and older snapshots are left alone.- It is used only when the scan is big enough, by default 32 files or 1 GiB, and when the index is as new as the table.
Iceberg prunes files by partition and by each file’s min and max values. That
is excellent for a time column and poor for a high-cardinality column like an
email address or an id, where every file’s range overlaps. A lookup by such a
value reads far more files than it needs. The secondary index is XTable’s answer
for Spark readers. Everything here is from the 0.5.0-incubating source and from runs
on Spark 3.5.9.

What is stored and where
The index lives in the Hudi metadata table under .hoodie/ in the Iceberg
table’s data path. After a build on a table of 2,000 rows in 40 files, the
metadata folder held four partitions:
column_stats files record_index secondary_index_email
HudiTargetConfig explains the second one: setting the secondary index column
“also enables the global record index, which the secondary index depends on”.
The index is built when the metadata table is created, so changing the indexed
column of an existing table is not supported. Hudi’s Java engine cannot build a
secondary index yet, which is why the Spark engine is required.

Configure and build
The index is a property of the Hudi target of an Iceberg-source sync:
sourceFormat: ICEBERG
targetFormats:
- HUDI
datasets:
- tableBasePath: file:///warehouse/db/cust
tableDataPath: file:///warehouse/db/cust/data
tableName: cust
namespace: db
targetProperties:
HUDI:
xtable.hudi.target.table_version: '9'
xtable.hudi.target.execution.engine: spark
xtable.hudi.target.secondary.index.column: email
Three more properties tune it, and none is required:
| Property | Meaning |
|---|---|
xtable.hudi.target.metadata.record.index.min.filegroup.count |
Lower bound on the record index’s file groups. Must be set together with the max |
xtable.hudi.target.metadata.record.index.max.filegroup.count |
Upper bound. The min must not be greater than the max |
xtable.hudi.target.metadata.index.secondary.parallelism |
Parallelism when generating the secondary index records |
Bad settings fail at sync time with messages from HudiTargetConfig:
| Message | Cause |
|---|---|
xtable.hudi.target.secondary.index.column requires Hudi target table version 9, set xtable.hudi.target.table_version=9 |
Left at the default version 6 |
... requires the Spark execution engine, set xtable.hudi.target.execution.engine=spark |
Engine left at java |
... supports only one column. |
More than one column given |
... must be set together / ... must not be greater than ... |
The two file group bounds |
Unsupported Hudi target table version <v>. Only table versions 6 and 9 are supported |
Any other version |
Run the sync where Hudi’s Spark classes exist. java -jar on the bundled jar fails with
NoClassDefFoundError: org/apache/hudi/client/common/HoodieSparkEngineContext.
Two setups work, and both were run. On a Spark cluster the build is spread across the executors:
$SPARK_HOME/bin/spark-submit \
--packages org.apache.hudi:hudi-spark3.5-bundle_2.12:1.2.1,org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.9.2 \
--conf spark.serializer=org.apache.spark.serializer.KryoSerializer \
--class org.apache.xtable.spark.XTableSparkSync \
xtable-spark-runtime_2.12-0.5.0-incubating.jar \
--datasetconfig index_config.yaml
On one machine, RunSync takes the Hudi Spark 3.4 bundle after the bundled jar,
because that jar carries Spark 3.4:
java -cp "$XTABLE_JAR:hudi-spark3.4-bundle_2.12-1.2.1.jar" \
org.apache.xtable.utilities.RunSync --datasetConfig index_config.yaml
The build reads every data file, so use the cluster for a large table.
Use it in Spark
Add the XTable extensions after Iceberg’s, and put the jars on the class path
rather than --jars, so XTable and Hudi load from one class loader:
spark.sql.extensions=org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions,org.apache.xtable.spark.extensions.XTableSparkSessionExtensions
spark.serializer=org.apache.spark.serializer.KryoSerializer
spark.driver.extraClassPath=xtable-spark-runtime.jar:hudi-spark3.5-bundle.jar:iceberg-spark-runtime.jar
spark.executor.extraClassPath=xtable-spark-runtime.jar:hudi-spark3.5-bundle.jar:iceberg-spark-runtime.jar
The query does not change. The decision is logged by IndexPruningRule at INFO, so set the
org.apache.xtable logger to INFO to see it. Seven queries on the 2,000-row table,
with the thresholds lowered to 1 so the small table qualifies:
| Query | What the log said |
|---|---|
email = 'user1234@example.com' |
Using the index ... for 1 keys: reading 1 of 40 candidate files |
email IN (3 values) |
... for 3 keys: reading 3 of 40 candidate files |
email = ... AND id > 0 |
... for 1 keys: reading 1 of 40 candidate files |
email = 'nobody@example.com' |
Not using the index ... the scan reads only 0 files of 0 bytes, because Iceberg’s own pruning had already left nothing |
email > 'user19' |
No index line. A range filter is not a lookup |
email LIKE 'user12%' |
No index line |
email = ... with pruning disabled |
No index line, same row returned |
Every query returned the right rows. Only the number of files read changed. Latency was not measured, because on a table this small it would say nothing.
Every reason the index is skipped
These are the messages IndexPruningRule can log, with the source condition behind
each. A skipped index reads the table normally.

| Log message | Condition |
|---|---|
Using the index ...: no file of N candidate files holds them |
The index proves no file has the key, so the scan is replaced by an empty relation |
Not using the index ...: the scan reads only N files of B bytes |
Fewer files than minCandidateFiles and fewer bytes than minCandidateBytes. Either threshold being met is enough to use the index |
Not using the index ...: N keys |
More filter values than maxKeys |
WARN Not using the index ...: it is synced to snapshot X, but the table is at Y. Run the XTable sync of the table to update the index |
The index is behind the table |
Not using the index ...: it changed from A to B during the lookup |
A sync, rollback or rebuild ran during the query |
Not using the index ...: snapshot S has no schema id |
The snapshot lacks a schema id |
WARN Not using the index ...: too many staged scans are live |
Plans were optimised but not run, which leaves scans staged |
IndexedColumnRenamedException |
The indexed name now belongs to another field. This one fails the query |
The rule also leaves alone any query that reads an older snapshot, a branch, a
tag, the changes between snapshots or a metadata table, per the comment in the rule.
The filter stays in the plan, so the query returns the same rows either way. The defaults come from
XTableIndexConf: pruning enabled, 32 files, 1,073,741,824 bytes and 10,000 keys.
Three of these were run. With both thresholds raised, the 40-file table logged
the scan reads only 40 files of 61462 bytes. With maxKeys=2, a three-value IN
logged 3 keys and read normally. An Iceberg insert made the index stale, which logged
the warning above and still returned the right row. A new column given the old
name of the indexed one, after a rename, failed with:
IndexedColumnRenamedException: The index of local.db.cust on email was synced when email named another field. Rebuild the index after the next commit to the table, or set s...
Setting spark.xtable.index.pruning.enabled=false made the same filter run. A plain rename
did not fail, because a filter on the new name is simply not indexed.
Maintenance
Each later sync of the Iceberg table updates the index to the new snapshot, so the
index is only ever as fresh as the last sync. A sync interval longer than your commit
interval means the WARN above on most queries. To drop the index, remove the Hudi
target from the sync and delete .hoodie/ under the data path, which leaves the
data files alone. That removal comes from the documentation and was not run.
Only Spark sessions with the extensions use the index, and the documentation lists
joins, subqueries, MERGE, UPDATE and DELETE as not using it.
Checked against the source
| Claim | Where |
|---|---|
| Record index enabled with the secondary index, Spark engine needed, column fixed at creation | HudiTargetConfig.SECONDARY_INDEX_COLUMN comment |
| The six properties and their validation messages | HudiTargetConfig |
| Defaults and setting names | XTableIndexConf |
| Every skip reason and its log level | IndexPruningRule |
| The rename failure | IndexedColumnRenamedException in IndexPruningRule.scala |
The seven queries and the threshold, maxKeys and stale-index checks are from runs
on a 2,000-row, 40-file table in Docker.
Key takeaways
Use the index for equality and IN lookups on a column Iceberg’s own statistics
cannot prune, on tables big enough to pass the thresholds. Keep the sync frequent so
the index is not behind, never reuse an indexed column’s name, and read the
IndexPruningRule log lines when a lookup is slow, because each skip says why.
References
- Secondary index for Iceberg tables
- Apache Iceberg as an XTable source
- Apache XTable Spark runtime
- Apache Hudi as an XTable source
Trademarks
Apache XTable (incubating), Apache Iceberg, Apache Hudi, Apache Spark and Apache are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries. All other marks are the property of their respective owners.
Found this useful?
These posts and tools are free. If one saved you an afternoon, you can buy me a coffee.
Comments
Questions, corrections and reviews are welcome. Sign in with GitHub to post; comments are stored in this site's public GitHub Discussions.