HIVE-29241: Distinguish the default location of the database by catalog#6267
HIVE-29241: Distinguish the default location of the database by catalog#6267zhangbutao wants to merge 2 commits intoapache:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request introduces catalog-aware warehouse paths for Hive metastore, allowing databases and tables under non-default catalogs to have distinct storage locations. The changes ensure that catalog locations are properly isolated while maintaining backward compatibility for the default Hive catalog.
Changes:
- Added new configuration parameters (
WAREHOUSE_CATALOGandWAREHOUSE_CATALOG_EXTERNAL) for catalog-specific warehouse directories - Modified catalog creation to require a 'type' parameter and made location optional with automatic defaults
- Updated database path resolution throughout the codebase to use catalog-aware logic via new
Warehousemethods that accept catalog names
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 21 comments.
Show a summary per file
| File | Description |
|---|---|
| MetastoreConf.java | Added WAREHOUSE_CATALOG and WAREHOUSE_CATALOG_EXTERNAL configuration variables |
| HiveConf.java | Added corresponding Hive configuration variables for catalog warehouses |
| Warehouse.java | Enhanced path resolution methods to accept catalog names; added deprecated markers for old methods |
| CatalogUtil.java | New utility class for catalog type validation (NATIVE, ICEBERG) |
| CreateCatalogAnalyzer.java | Updated to validate catalog type property and make location optional |
| CreateCatalogOperation.java | Modified to generate default location from WAREHOUSE_CATALOG config |
| CreateDatabaseAnalyzer.java | Added validation to check if catalog supports database creation |
| CreateDatabaseOperation.java | Updated path building logic to include catalog names for non-default catalogs |
| DDLUtils.java | Added helper method to check catalog support for database creation |
| HiveParser.g | Modified grammar to make catalog location optional and properties required |
| EximUtil.java | Updated to use catalog-aware warehouse paths for replication |
| Multiple test files | Updated test queries to include required 'type' property in CREATE CATALOG statements |
| HMSHandler.java | Updated default catalog and database creation to use catalog-aware paths |
| MetastoreDefaultTransformer.java | Updated to pass Database object instead of just name |
| Migration/Authorization files | Updated to use new Database-based path methods |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
...one-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/Warehouse.java
Outdated
Show resolved
Hide resolved
...one-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/Warehouse.java
Show resolved
Hide resolved
...one-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/Warehouse.java
Outdated
Show resolved
Hide resolved
ql/src/java/org/apache/hadoop/hive/ql/ddl/catalog/create/CreateCatalogAnalyzer.java
Outdated
Show resolved
Hide resolved
...one-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/Warehouse.java
Show resolved
Hide resolved
...e-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/CatalogUtil.java
Show resolved
Hide resolved
ql/src/java/org/apache/hadoop/hive/ql/queryhistory/repository/AbstractRepository.java
Show resolved
Hide resolved
...one-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/Warehouse.java
Show resolved
Hide resolved
5940a60 to
49c343a
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 28 out of 28 changed files in this pull request and generated 9 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ql/src/java/org/apache/hadoop/hive/ql/ddl/catalog/create/CreateCatalogAnalyzer.java
Show resolved
Hide resolved
...tore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/conf/MetastoreConf.java
Outdated
Show resolved
Hide resolved
ql/src/java/org/apache/hadoop/hive/ql/ddl/database/create/CreateDatabaseAnalyzer.java
Show resolved
Hide resolved
...tore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/conf/MetastoreConf.java
Show resolved
Hide resolved
Changes have been made to keep the type optional when creating a catalog. Thanks for the advice. |
Let's go :) |
| // Create a fake fs root for local fs | ||
| Path localFsRoot = new Path(path, "localfs"); | ||
| warehousePath = new Path(localFsRoot, "warehouse"); | ||
| warehouseCatPath = new Path(localFsRoot, "catalog"); |
There was a problem hiding this comment.
shouldn't the catalog be under the warehousePath?
Warehouse Root
└── Catalog (default or named)
└── Database (.db)
└── Table
/warehouse/<catalog_name>/<database_name>.db/<table_name>/ # non default catalog
/warehouse/hive/<database_name>.db/<table_name>/ # default catalog
/warehouse/hive/default.db/<table_name>/ # default catalog & database
There was a problem hiding this comment.
shouldn't the catalog be under the warehousePath?
No. The warehousePath is the path of the default catalog, under which the paths of Hive databases reside. If the catalog path is placed under the warehousePath, it will cause confusion with the path information of Hive databases under the default catalog.
The path information for the default catalog's databases is controlled by the property metastore.warehouse.dir, which defaults to /user/hive/warehouse. The path for non-default catalogs is controlled by the new added propertymetastore.warehouse.catalog.dir, which defaults to /user/hive/catalog/warehouse.
/user/hive/catalog/warehouse/<catalog_name>/<database_name>.db/<table_name>/ # non default catalog
/user/hive/warehouse/<database_name>.db/<table_name>/ # default catalog
/user/hive/warehouse/default.db/<table_name>/ # default catalog & database
|
|
||
| // Initialize props if null, and set default type to native if not specified | ||
| if (props == null) { | ||
| props = new HashMap<>(); |
There was a problem hiding this comment.
could we do this in the var initialization?
| } | ||
|
|
||
| private static void checkCatalogType(Map<String, String> props) throws SemanticException { | ||
| String catalogType = props.get("type"); |
There was a problem hiding this comment.
could we please create constant for that
There was a problem hiding this comment.
Sure. Create constan TYPE in CatalogUtil
| } | ||
| // Normalize and validate catalog type (fail fast on invalid input) | ||
| try { | ||
| props.put("type", CatalogUtil.normalizeCatalogType(catalogType)); |
There was a problem hiding this comment.
Could we simplify by handling isNullOrEmpty in normalizeCatalogType
| @Override | ||
| public int execute() throws Exception { | ||
| Catalog catalog = new Catalog(desc.getName(), desc.getLocationUri()); | ||
| String catLocationUri = Optional.ofNullable(desc.getLocationUri()) |
There was a problem hiding this comment.
minor. i think cat prefix in local var is redundant here since we are inside CreateCatalog class
There was a problem hiding this comment.
Sorry, i didn't get you here. Could you please describe it in detail? Thank you.
| @@ -51,10 +52,9 @@ public CreateDatabaseAnalyzer(QueryState queryState) throws SemanticException { | |||
| @Override | |||
| public void analyzeInternal(ASTNode root) throws SemanticException { | |||
| Pair<String, String> catDbNamePair = DDLUtils.getCatDbNamePair((ASTNode) root.getChild(0)); | |||
There was a problem hiding this comment.
How about record CatalogDb(String catalog, String database) {}
please see #6379 (comment)
There was a problem hiding this comment.
I think using a Pair here is already simple and clear enough, there's no need to use the record CatalogDb.
| } | ||
|
|
||
| private void makeLocationQualified(Database database) throws HiveException { | ||
| String catalogName = database.getCatalogName().toLowerCase(); |
There was a problem hiding this comment.
Shouldn't the names be normalized here: special chars/encoding lowercase?
There was a problem hiding this comment.
At this stage, the database is being created, and the prerequisite is that a valid catalog already exists — there is no need to double validate the catalog here.
| private void makeLocationQualified(Database database) throws HiveException { | ||
| String catalogName = database.getCatalogName().toLowerCase(); | ||
| String dbName = database.getName().toLowerCase(); | ||
| boolean isDefaultCatalog = Warehouse.DEFAULT_CATALOG_NAME.equalsIgnoreCase(catalogName); |
There was a problem hiding this comment.
Fixed. Added a util method:
HiveUtils.isDefaultCatalog(String catName, Configuration conf)
|
|
||
| // managed warehouse root | ||
| String whManagedLocatoion = MetastoreConf.getVar(conf, | ||
| isDefaultCatalog ? MetastoreConf.ConfVars.WAREHOUSE |
| if (db == null) { | ||
| LOG.warn("Database ({}) for query history table hasn't been found, auto-creating one", QUERY_HISTORY_DB_NAME); | ||
| String location = getDatabaseLocation(QUERY_HISTORY_DB_NAME); | ||
| db = new Database(); |
There was a problem hiding this comment.
looks a bit weird creating 2 Database objects in same method
| Path root = null; | ||
| try { | ||
| initWh(); | ||
| // TODO catalog. Need to determine auth root path based on catalog name. |
There was a problem hiding this comment.
could you please add the ticket to the EPIC
cc @saihemanth-cloudera, as he is the most experienced in this area
| set hive.support.concurrency=true; | ||
|
|
||
| CREATE CATALOG testcat LOCATION '/tmp/testcat' COMMENT 'Hive test catalog'; | ||
| CREATE CATALOG testcat LOCATION '/tmp/testcat' COMMENT 'Hive test catalog' PROPERTIES('type'='native'); |
| -- CREATE with comment | ||
| CREATE CATALOG test_cat LOCATION 'hdfs:///tmp/test_cat' COMMENT 'Hive test catalog'; | ||
| -- CREATE with comment and default location | ||
| CREATE CATALOG test_cat COMMENT 'Hive test catalog' PROPERTIES('type'='NATIVE'); |
There was a problem hiding this comment.
same 'type'='NATIVE' is redundant
There was a problem hiding this comment.
Removed.
I also left a few DDL statements with 'type' = 'hive' to indicate that this parameter can be omitted for the Hive internal catalog.
|
|
||
| -- CREATE INE already exists | ||
| CREATE CATALOG IF NOT EXISTS test_cat LOCATION 'hdfs:///tmp/test_cat'; | ||
| CREATE CATALOG IF NOT EXISTS test_cat LOCATION 'hdfs:///tmp/test_cat' PROPERTIES('type'='native'); |
There was a problem hiding this comment.
do we have tests for external catalog?
There was a problem hiding this comment.
Currently, this PR is specifically targeting the internal/native catalog. If we were to implement a new external catalog, such as a JDBC catalog, I believe it would still require a lot of additional preliminary work(such as #6252), so we can consider doing it later.
|
|
||
| public class CatalogUtil { | ||
| // Catalog type constants | ||
| public static final String NATIVE = "native"; |
There was a problem hiding this comment.
no need for Strings, please use enum values.
| FileSystem cmFs = cmroot.getFileSystem(conf); | ||
| cmFs.setPermission(cmroot, new FsPermission("770")); | ||
| try { | ||
| // TODO catalog. The Repl function is currently only available for the default catalog path ConfVars.WAREHOUSE. |
There was a problem hiding this comment.
should we add the ticket to the EPIC?
| } | ||
|
|
||
| whRootString = getRequiredVar(conf, ConfVars.WAREHOUSE); | ||
| whCatRootString = getRequiredVar(conf, ConfVars.WAREHOUSE_CATALOG); |
There was a problem hiding this comment.
please see the comment on default FS structure
There was a problem hiding this comment.
Already repied above.
| return getWhRoot(); | ||
| } | ||
| return new Path(getWhRoot(), dbName.toLowerCase() + DATABASE_WAREHOUSE_SUFFIX); | ||
| Database db = new Database(); |
There was a problem hiding this comment.
are you using Database only as a DTO object? instead you could use a record CatalogDb as mentioned earlier
There was a problem hiding this comment.
I think this should work well. Regarding the changes made here, could you help me point out the more prominent advantages of using record CatalogDb?
Thank you.
| if (configUri != null && !configUri.isEmpty()) { | ||
| properties.put("uri", configUri); | ||
| } | ||
| // TODO catalog. Consider adding new created native catalog warehouse(MetastoreConf.ConfVars.WAREHOUSE_CATALOG) later. |
There was a problem hiding this comment.
could you please elaborate, i don't quite get what you are suggesting here
There was a problem hiding this comment.
Fixed. This just maybe a question. I created a ticket HIVE-28879 for this.
|



What changes were proposed in this pull request?
The main objectives of this PR are:
Allow location to be omitted when creating a catalog. At the same time, it is necessary to determine catalogs that do not require a location, such as JDBC catalogs.
For newly created catalogs, the location of their databases will be determined by the two newly added parameters:
metastore.warehouse.catalog.dirandmetastore.warehouse.catalog.external.dir. This helps better ensure that the location of the default Hive catalog's databases and tables remains unaffected.For example, if metastore.warehouse.catalog.dir is hdfs://ns1/testdir, then the location for a newly created catalog named testcat would be hdfs://ns1/testdir/testcat. Consequently, the default path for a database like testdb created under this catalog would be hdfs://ns1/testdir/testcat/testdb.
typeparameter must be specified when creating a catalog to distinguish its type. Based on this type, it will be determined whether the catalog's databases and tables require a location, or whether the catalog type supports creating databases and tables.CREATE CATALOG test_cat COMMENT 'Hive test catalog' PROPERTIES('type'='hive');BTW, While working on this PR, some tasks need to be carried forward. For these follow-up tasks, I have created sub-tickets. You can also search for the
TODO catalogcomments in the catalog for more details on these follow-up tasks.HIVE-29564
HIVE-29562
HIVE-29561
HIVE-29278
Why are the changes needed?
The paths of databases and tables created under a non-default catalog may interfere with those under the default Hive catalog. We need to introduce parameters to distinguish the paths for databases and tables under non-default catalogs, ensuring that the paths of the default Hive catalog remain unaffected.
Does this PR introduce any user-facing change?
No
How was this patch tested?
Existing tests.