CREATE TABLE LIKE fails for Hudi COW/MOR tables with "Creating ro/rt table need the existence of the base table"
- Dominant language
- Java
- Stars
- 6.2k
- Forks
- 2.5k
- Avg merge
- 2d 8h
- Merged PRs (30d)
- 111
Description
### Bug Description
**What happened:**
When using Hudi with Spark SQL (with Glue) and performing a CREATE TABLE ... LIKE ... operation, the command fails with the error for COW and MOR under certain conditions:
```
spark-sql (mansipp_demo_db)> create table hudi_demo_table_like like hudi_demo_table using hudi;
Creating ro/rt table need the existence of the base table.
```
Hudi’s CREATE TABLE LIKE implementation includes a check on the configuration property `hoodie.query.as.ro.table`
- If `hoodie.query.as.ro.table` is empty, Hudi allows the CREATE TABLE LIKE operation to create a table.
- If `hoodie.query.as.ro.table` is non-empty, the operation fails, expecting the base table to exist.
- Although this property is intended for MOR RO/RTtables to indicate read-optimized behavior, it is also populated for COW tables in Glue (set to false) and for MOR BASE table (set to false). Since the check only verifies whether the property is empty, a non-empty value, regardless of table type or relevance to RO/RT causes the operation to fail, preventing valid CREATE TABLE LIKE operations for both COW and MOR tables.
https://github.com/apache/hudi/blob/master/hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/hudi/command/CreateHoodieTableLikeCommand.scala#L91-L101
```
val queryAsProp = hoodieCatalogTable.catalogProperties.get(ConfigUtils.IS_QUERY_AS_RO_TABLE)
if (queryAsProp.isEmpty) {
// init hoodie table for a normal table (not a ro/rt table)
hoodieCatalogTable.initHoodieTable()
} else {
if (!hoodieCatalogTable.hoodieTableExists) {
throw new HoodieAnalysisException("Creating ro/rt table need the existence of the base table.")
}
if (HoodieTableType.MERGE_ON_READ != hoodieCatalogTable.tableType) {
throw new HoodieAnalysisException("Creating ro/rt table should only apply to a mor table.")
}
}
```
- When a new Hudi table is created and data is inserted, during catalog sync when an INSERT is performed, the table properties are automatically updated. This includes syncing the SerDe properties and adding `hoodie.query.as.ro.table` to the catalog table including COW and MOR base table. This property should only be set for RO/RT tables in MOR, and not for base tables (COW or MOR). https://github.com/apache/hudi/blob/master/hudi-sync/hudi-sync-common/src/main/java/org/apache/hudi/sync/common/util/SparkDataSourceTableUtils.java#L103
- Since this property is now non-empty, any subsequent CREATE TABLE ... LIKE ... operation against this table will fail with the above error for COW and MOR.
**What you expected:**
- The CREATE TABLE LIKE operation should succeed for both COW and MOR tables.
**Steps to reproduce:**
NOTE: `Performing a `CREATE TABLE LIKE` after a `CREATE TABLE` operation does not cause issues because `hoodie.query.as.ro.table` is populated in Glue only after the first set of data is inserted.`
**CASE 1: Failure**
CREATE -> INSERT -> CREATE LIKE
1.
```
CREATE TABLE hudi_demo_table (
id STRING,
name STRING,
ts BIGINT,
dt STRING
)
USING hudi
OPTIONS (
type = 'cow',
primaryKey = 'id',
preCombineField = 'ts'
)
PARTITIONED BY (dt)
LOCATION 's3:///hudi_demo_table';
```
2.
```
INSERT INTO hudi_demo_table VALUES
('1', 'Bob', 1739660000000, '2025-10-16'),
('2', 'Alice', 1739660100000, '2025-10-16');
```
3.
```
CREATE TABLE hudi_demo_table_like LIKE hudi_demo_table USING HUDI;
```
**CASE 2: Success**
CREATE -> CREATE LIKE
1.
```
CREATE TABLE hudi_demo_table_success (
id STRING,
name STRING,
ts BIGINT,
dt STRING
)
USING hudi
OPTIONS (
type = 'cow',
primaryKey = 'id',
preCombineField = 'ts'
)
PARTITIONED BY (dt)
LOCATION 's3:///hudi_demo_table_success';
```
2.
```
CREATE TABLE hudi_demo_table_success_like LIKE hudi_demo_table_success USING HUDI;
```
### Environment
**Hudi version:**
1.0.2
**Query engine:** (Spark/Flink/Trino etc)
Spark
**Relevant configs:**
### Logs and Stack Trace
```
spark-sql (mansipp_demo_db)> create table hudi_demo_table_like like hudi_demo_table using hudi;
Creating ro/rt table need the existence of the base table.
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/hudi/command/CreateHoodieTableLikeCommand.scala around lines 91-101, then inspect SparkDataSourceTableUtils.java around line 103. Reproduce the CREATE → INSERT → CREATE LIKE sequence for COW and MOR tables and compare it with CREATE → CREATE LIKE. Done means valid CREATE TABLE LIKE operations succeed for both table types without breaking RO/RT validation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, java, scala, sql
- Domain
- data-engineering, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100