Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HIVE-26227: Add support of catalog related statements for Hive ql #3288

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/src/java/org/apache/hadoop/hive/ql/ErrorMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ public enum ErrorMsg {
ICEBERG_COMPACTION_WITH_PART_SPEC_AND_FILTER_NOT_SUPPORTED(10441, "Compaction command with both partition spec and filter is not supported on Iceberg table {0}.{1}", true),
COMPACTION_THREAD_INITIALIZATION(10442, "Compaction thread failed during initialization", false),
ALTER_TABLE_COMPACTION_NON_PARTITIONED_COLUMN_NOT_ALLOWED(10443, "Filter expression can contain only partition columns."),
CATALOG_ALREADY_EXISTS(10444, "Catalog {0} already exists", true),
CATALOG_NOT_EXISTS(10445, "Catalog {0} does not exists:", true),

//========================== 20000 range starts here ========================//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public class PerfLogger {
public static final String LOAD_DYNAMIC_PARTITIONS = "LoadDynamicPartitions";

public static final String HIVE_GET_TABLE = "getTablesByType";
public static final String HIVE_GET_CATALOG = "getCatalog";
public static final String HIVE_GET_DATABASE = "getDatabase";
public static final String HIVE_GET_DATABASE_2 = "getDatabase2";
public static final String HIVE_GET_PARTITIONS = "getPartitions";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ alterStatement
| KW_ALTER (KW_DATABASE|KW_SCHEMA) alterDatabaseStatementSuffix -> alterDatabaseStatementSuffix
| KW_ALTER KW_DATACONNECTOR alterDataConnectorStatementSuffix -> alterDataConnectorStatementSuffix
| KW_OPTIMIZE KW_TABLE tableName optimizeTableStatementSuffix -> ^(TOK_ALTERTABLE tableName optimizeTableStatementSuffix)
| KW_ALTER KW_CATALOG alterCatalogStatementSuffix -> alterCatalogStatementSuffix
;

alterTableStatementSuffix
Expand Down Expand Up @@ -155,6 +156,19 @@ alterMaterializedViewSuffixRebuild[CommonTree tableNameTree]
: KW_REBUILD -> ^(TOK_ALTER_MATERIALIZED_VIEW_REBUILD {$tableNameTree})
;

alterCatalogStatementSuffix
@init { gParent.pushMsg("alter catalog statement", state); }
@after { gParent.popMsg(state); }
: alterCatalogSuffixSetLocation
;

alterCatalogSuffixSetLocation
@init { gParent.pushMsg("alter catalog set location", state); }
@after { gParent.popMsg(state); }
: catName=identifier KW_SET KW_LOCATION newLocation=StringLiteral
-> ^(TOK_ALTERCATALOG_LOCATION $catName $newLocation)
;

alterDatabaseStatementSuffix
@init { gParent.pushMsg("alter database statement", state); }
@after { gParent.popMsg(state); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ KW_INTERSECT: 'INTERSECT';
KW_VIEW: 'VIEW';
KW_VIEWS: 'VIEWS';
KW_IN: 'IN';
KW_CATALOG: 'CATALOG';
KW_CATALOGS: 'CATALOGS';
KW_DATABASE: 'DATABASE';
KW_DATABASES: 'DATABASES';
KW_MATERIALIZED: 'MATERIALIZED';
Expand Down
48 changes: 46 additions & 2 deletions parser/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ TOK_STRUCT;
TOK_MAP;
TOK_UNIONTYPE;
TOK_COLTYPELIST;
TOK_CREATECATALOG;
TOK_CREATEDATABASE;
TOK_CREATEDATACONNECTOR;
TOK_CREATETABLE;
Expand Down Expand Up @@ -229,6 +230,7 @@ TOK_RETAIN;
TOK_WITH_SNAPSHOT_RETENTION;
TOK_ALTERTABLE_CONVERT;
TOK_MSCK;
TOK_SHOWCATALOGS;
TOK_SHOWDATABASES;
TOK_SHOWDATACONNECTORS;
TOK_SHOWTABLES;
Expand All @@ -246,6 +248,7 @@ TOK_UNLOCKTABLE;
TOK_LOCKDB;
TOK_UNLOCKDB;
TOK_SWITCHDATABASE;
TOK_DROPCATALOG;
TOK_DROPDATABASE;
TOK_DROPTABLE;
TOK_DATABASECOMMENT;
Expand Down Expand Up @@ -369,6 +372,10 @@ TOK_SHOW_ROLES;
TOK_SHOW_CURRENT_ROLE;
TOK_SHOW_ROLE_PRINCIPALS;
TOK_SHOWDBLOCKS;
TOK_DESCCATALOG;
TOK_CATALOGLOCATION;
TOK_CATALOGCOMMENT;
TOK_ALTERCATALOG_LOCATION;
TOK_DESCDATABASE;
TOK_DATABASEPROPERTIES;
TOK_DATABASELOCATION;
Expand Down Expand Up @@ -997,7 +1004,9 @@ importStatement
ddlStatement
@init { pushMsg("ddl statement", state); }
@after { popMsg(state); }
: createDatabaseStatement
: createCatalogStatement
| dropCatalogStatement
| createDatabaseStatement
| switchDatabaseStatement
| dropDatabaseStatement
| createTableStatement
Expand Down Expand Up @@ -1099,6 +1108,38 @@ orReplace
-> ^(TOK_ORREPLACE)
;

createCatalogStatement
@init { pushMsg("create catalog statement", state); }
@after { popMsg(state); }
: KW_CREATE KW_CATALOG
ifNotExists?
name=identifier
catLocation
catalogComment?
-> ^(TOK_CREATECATALOG $name catLocation ifNotExists? catalogComment?)
;

catLocation
@init { pushMsg("catalog location specification", state); }
@after { popMsg(state); }
:
KW_LOCATION locn=StringLiteral -> ^(TOK_CATALOGLOCATION $locn)
;

catalogComment
@init { pushMsg("catalog's comment", state); }
@after { popMsg(state); }
: KW_COMMENT comment=StringLiteral
-> ^(TOK_CATALOGCOMMENT $comment)
;

dropCatalogStatement
@init { pushMsg("drop catalog statement", state); }
@after { popMsg(state); }
: KW_DROP KW_CATALOG ifExists? identifier
-> ^(TOK_DROPCATALOG identifier ifExists?)
;

createDatabaseStatement
@init { pushMsg("create database statement", state); }
@after { popMsg(state); }
Expand Down Expand Up @@ -1230,6 +1271,8 @@ descStatement
:
(KW_DESCRIBE|KW_DESC)
(
(KW_CATALOG) => (KW_CATALOG) KW_EXTENDED? (catName=identifier) -> ^(TOK_DESCCATALOG $catName KW_EXTENDED?)
|
(KW_DATABASE|KW_SCHEMA) => (KW_DATABASE|KW_SCHEMA) KW_EXTENDED? (dbName=identifier) -> ^(TOK_DESCDATABASE $dbName KW_EXTENDED?)
|
(KW_DATACONNECTOR) => (KW_DATACONNECTOR) KW_EXTENDED? (dcName=identifier) -> ^(TOK_DESCDATACONNECTOR $dcName KW_EXTENDED?)
Expand Down Expand Up @@ -1258,7 +1301,8 @@ analyzeStatement
showStatement
@init { pushMsg("show statement", state); }
@after { popMsg(state); }
: KW_SHOW (KW_DATABASES|KW_SCHEMAS) (KW_LIKE showStmtIdentifier)? -> ^(TOK_SHOWDATABASES showStmtIdentifier?)
: KW_SHOW KW_CATALOGS (KW_LIKE showStmtIdentifier)? -> ^(TOK_SHOWCATALOGS showStmtIdentifier?)
| KW_SHOW (KW_DATABASES|KW_SCHEMAS) (KW_LIKE showStmtIdentifier)? -> ^(TOK_SHOWDATABASES showStmtIdentifier?)
| KW_SHOW (isExtended=KW_EXTENDED)? KW_TABLES ((KW_FROM|KW_IN) db_name=identifier)? (filter=showTablesFilterExpr)?
-> ^(TOK_SHOWTABLES (TOK_FROM $db_name)? $filter? $isExtended?)
| KW_SHOW KW_VIEWS ((KW_FROM|KW_IN) db_name=identifier)? (KW_LIKE showStmtIdentifier|showStmtIdentifier)? -> ^(TOK_SHOWVIEWS (TOK_FROM $db_name)? showStmtIdentifier?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ nonReserved
:
KW_ABORT | KW_ADD | KW_ADMIN | KW_AFTER | KW_ANALYZE | KW_ARCHIVE | KW_ASC | KW_BEFORE | KW_BUCKET | KW_BUCKETS
| KW_CASCADE | KW_CBO | KW_CHANGE | KW_CHECK | KW_CLUSTER | KW_CLUSTERED | KW_CLUSTERSTATUS | KW_COLLECTION | KW_COLUMNS
| KW_COMMENT | KW_COMPACT | KW_COMPACTIONS | KW_COMPUTE | KW_CONCATENATE | KW_CONTINUE | KW_COST | KW_DATA | KW_DAY
| KW_COMMENT | KW_COMPACT | KW_COMPACTIONS | KW_COMPUTE | KW_CONCATENATE | KW_CONTINUE | KW_COST | KW_DATA | KW_DAY | KW_CATALOG | KW_CATALOGS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed CATALOG is not a reserved word in SQL:2023 👍

| KW_DATABASES | KW_DATETIME | KW_DBPROPERTIES | KW_DCPROPERTIES | KW_DEFERRED | KW_DEFINED | KW_DELIMITED | KW_DEPENDENCY
| KW_DESC | KW_DIRECTORIES | KW_DIRECTORY | KW_DISABLE | KW_DISTRIBUTE | KW_DISTRIBUTED | KW_DOW | KW_ELEM_TYPE
| KW_ENABLE | KW_ENFORCED | KW_ESCAPED | KW_EXCLUSIVE | KW_EXPLAIN | KW_EXPORT | KW_FIELDS | KW_FILE | KW_FILEFORMAT
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.hive.ql.ddl.catalog.alter;

import org.apache.hadoop.hive.metastore.api.Catalog;
import org.apache.hadoop.hive.ql.QueryState;
import org.apache.hadoop.hive.ql.ddl.DDLWork;
import org.apache.hadoop.hive.ql.exec.TaskFactory;
import org.apache.hadoop.hive.ql.hooks.WriteEntity;
import org.apache.hadoop.hive.ql.parse.ASTNode;
import org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer;
import org.apache.hadoop.hive.ql.parse.SemanticException;

/**
* Analyzer for database alteration commands.
*/
public abstract class AbstractAlterCatalogAnalyzer extends BaseSemanticAnalyzer {
public AbstractAlterCatalogAnalyzer(QueryState queryState) throws SemanticException {
super(queryState);
}

@Override
public void analyzeInternal(ASTNode root) throws SemanticException {
AbstractAlterCatalogDesc alterDesc = buildAlterCatalogDesc(root);
Catalog catalog = getCatalog(alterDesc.getCatalogName());
outputs.add(new WriteEntity(catalog, WriteEntity.WriteType.DDL_NO_LOCK));
rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(), alterDesc)));
}

protected abstract AbstractAlterCatalogDesc buildAlterCatalogDesc(ASTNode root)
throws SemanticException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.hive.ql.ddl.catalog.alter;

import org.apache.hadoop.hive.ql.ddl.DDLDesc;
import org.apache.hadoop.hive.ql.plan.Explain;

import java.io.Serializable;

/**
* DDL task description for ALTER CATALOG commands.
*/
public abstract class AbstractAlterCatalogDesc implements DDLDesc, Serializable {
private static final long serialVersionUID = 1L;

private final String catalogName;

public AbstractAlterCatalogDesc(String catalogName) {
this.catalogName = catalogName;
}

@Explain(displayName="name", explainLevels = { Explain.Level.USER, Explain.Level.DEFAULT, Explain.Level.EXTENDED })
public String getCatalogName() {
return catalogName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.hive.ql.ddl.catalog.alter;

import org.apache.hadoop.hive.metastore.api.Catalog;
import org.apache.hadoop.hive.ql.ErrorMsg;
import org.apache.hadoop.hive.ql.ddl.DDLOperation;
import org.apache.hadoop.hive.ql.ddl.DDLOperationContext;
import org.apache.hadoop.hive.ql.metadata.HiveException;

/**
* Operation process of altering a catalog.
*/
public abstract class AbstractAlterCatalogOperation<T extends AbstractAlterCatalogDesc> extends DDLOperation<T> {
public AbstractAlterCatalogOperation(DDLOperationContext context, T desc) {
super(context, desc);
}

@Override
public int execute() throws Exception {
String catalogName = desc.getCatalogName();
Catalog catalog = context.getDb().getMSC().getCatalog(catalogName);
if (catalog == null) {
throw new HiveException(ErrorMsg.CATALOG_NOT_EXISTS, catalogName);
}
doAlteration(catalog);

context.getDb().alterCatalog(catalogName, catalog);
return 0;
}

protected abstract void doAlteration(Catalog catalog) throws HiveException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.hive.ql.ddl.catalog.alter.location;

import org.apache.hadoop.hive.ql.QueryState;
import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
import org.apache.hadoop.hive.ql.ddl.catalog.alter.AbstractAlterCatalogAnalyzer;
import org.apache.hadoop.hive.ql.ddl.catalog.alter.AbstractAlterCatalogDesc;
import org.apache.hadoop.hive.ql.parse.ASTNode;
import org.apache.hadoop.hive.ql.parse.HiveParser;
import org.apache.hadoop.hive.ql.parse.SemanticException;

/**
* Analyzer for catalog set location commands.
*/
@DDLType(types = HiveParser.TOK_ALTERCATALOG_LOCATION)
public class AlterCatalogSetLocationAnalyzer extends AbstractAlterCatalogAnalyzer {
public AlterCatalogSetLocationAnalyzer(QueryState queryState) throws SemanticException {
super(queryState);
}

@Override
protected AbstractAlterCatalogDesc buildAlterCatalogDesc(ASTNode root) throws SemanticException {
String catalogName = getUnescapedName((ASTNode) root.getChild(0));
String newLocation = unescapeSQLString(root.getChild(1).getText());

outputs.add(toWriteEntity(newLocation));

return new AlterCatalogSetLocationDesc(catalogName, newLocation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.hive.ql.ddl.catalog.alter.location;

import org.apache.hadoop.hive.ql.ddl.catalog.alter.AbstractAlterCatalogDesc;
import org.apache.hadoop.hive.ql.plan.Explain;

/**
* DDL task description for ALTER CATALOG ... SET LOCATION ... commands.
*/
@Explain(displayName = "Set Catalog Location", explainLevels = { Explain.Level.USER, Explain.Level.DEFAULT, Explain.Level.EXTENDED })
public class AlterCatalogSetLocationDesc extends AbstractAlterCatalogDesc {
private final String location;

public AlterCatalogSetLocationDesc(String catalogName, String location) {
super(catalogName);
this.location = location;
}

@Explain(displayName="location")
public String getLocation() {
return location;
}
}
Loading
Loading