Repository: hive Updated Branches: refs/heads/branch-3 2df5165fe -> e20592cb2
HIVE-19984 Backport HIVE-15976 (CURRENT_SCHEMA and CURRENT_CATALOG) to branch-3 (Lazlo Bodor via Alan Gates) Project: http://git-wip-us.apache.org/repos/asf/hive/repo Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/e20592cb Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/e20592cb Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/e20592cb Branch: refs/heads/branch-3 Commit: e20592cb27c0556b61771e73997f9c1855bff575 Parents: 2df5165 Author: Alan Gates <ga...@hortonworks.com> Authored: Wed Jul 11 13:36:20 2018 -0700 Committer: Alan Gates <ga...@hortonworks.com> Committed: Wed Jul 11 13:36:20 2018 -0700 ---------------------------------------------------------------------- .../hadoop/hive/ql/exec/FunctionRegistry.java | 4 +- .../udf/generic/GenericUDFCurrentCatalog.java | 52 +++++++++++++++ .../udf/generic/GenericUDFCurrentDatabase.java | 53 +++++++++++++++ .../ql/udf/generic/GenericUDFCurrentSchema.java | 37 +++++++++++ .../hive/ql/udf/generic/UDFCurrentDB.java | 68 -------------------- .../clientpositive/current_catalog_and_schema.q | 10 +++ .../current_catalog_and_schema.q.out | 54 ++++++++++++++++ .../results/clientpositive/show_functions.q.out | 4 ++ 8 files changed, 213 insertions(+), 69 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hive/blob/e20592cb/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java ---------------------------------------------------------------------- diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java index 9ddfc06..10517ad 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java @@ -352,7 +352,9 @@ public final class FunctionRegistry { system.registerGenericUDF("grouping", GenericUDFGrouping.class); - system.registerGenericUDF("current_database", UDFCurrentDB.class); + system.registerGenericUDF("current_database", GenericUDFCurrentDatabase.class); + system.registerGenericUDF("current_schema", GenericUDFCurrentSchema.class); + system.registerGenericUDF("current_catalog", GenericUDFCurrentCatalog.class); system.registerGenericUDF("current_date", GenericUDFCurrentDate.class); system.registerGenericUDF("current_timestamp", GenericUDFCurrentTimestamp.class); system.registerGenericUDF("current_user", GenericUDFCurrentUser.class); http://git-wip-us.apache.org/repos/asf/hive/blob/e20592cb/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentCatalog.java ---------------------------------------------------------------------- diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentCatalog.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentCatalog.java new file mode 100644 index 0000000..e2684c8 --- /dev/null +++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentCatalog.java @@ -0,0 +1,52 @@ +/* + * 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.udf.generic; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.session.SessionState; +import org.apache.hadoop.hive.ql.udf.UDFType; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory; +import org.apache.hadoop.io.Text; + +// This function is not a deterministic function, but a runtime constant. +// The return value is constant within a query but can be different between queries. +@UDFType(deterministic = false, runtimeConstant = true) +@Description(name = "current_catalog", value = "_FUNC_() - returns currently used catalog name") +@NDV(maxNdv = 1) +public class GenericUDFCurrentCatalog extends GenericUDF { + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + return PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector( + TypeInfoFactory.stringTypeInfo, new Text(SessionState.get().getCurrentCatalog())); + } + + @Override + public Object evaluate(DeferredObject[] arguments) throws HiveException { + return SessionState.get().getCurrentCatalog(); + } + + @Override + public String getDisplayString(String[] children) { + return "current_catalog()"; + } +} http://git-wip-us.apache.org/repos/asf/hive/blob/e20592cb/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentDatabase.java ---------------------------------------------------------------------- diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentDatabase.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentDatabase.java new file mode 100644 index 0000000..29d3629 --- /dev/null +++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentDatabase.java @@ -0,0 +1,53 @@ +/* + * 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.udf.generic; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.session.SessionState; +import org.apache.hadoop.hive.ql.udf.UDFType; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory; +import org.apache.hadoop.io.Text; + +// This function is not a deterministic function, but a runtime constant. +// The return value is constant within a query but can be different between queries. +@UDFType(deterministic = false, runtimeConstant = true) +@Description(name = "current_database", + value = "_FUNC_() - returns currently using database name") +@NDV(maxNdv = 1) +public class GenericUDFCurrentDatabase extends GenericUDF { + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + return PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector( + TypeInfoFactory.stringTypeInfo, new Text(SessionState.get().getCurrentDatabase())); + } + + @Override + public Object evaluate(DeferredObject[] arguments) throws HiveException { + return SessionState.get().getCurrentDatabase(); + } + + @Override + public String getDisplayString(String[] children) { + return "current_database()"; + } +} http://git-wip-us.apache.org/repos/asf/hive/blob/e20592cb/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentSchema.java ---------------------------------------------------------------------- diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentSchema.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentSchema.java new file mode 100644 index 0000000..9e7895b --- /dev/null +++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCurrentSchema.java @@ -0,0 +1,37 @@ +/* + * 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.udf.generic; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.session.SessionState; +import org.apache.hadoop.hive.ql.udf.UDFType; + +// This function is not a deterministic function, but a runtime constant. +// The return value is constant within a query but can be different between queries. +@UDFType(deterministic = false, runtimeConstant = true) +@Description(name = "current_schema", value = "_FUNC_() - returns currently used schema(database) name") +@NDV(maxNdv = 1) +public class GenericUDFCurrentSchema extends GenericUDFCurrentDatabase { + @Override + public String getDisplayString(String[] children) { + return "current_schema()"; + } +} http://git-wip-us.apache.org/repos/asf/hive/blob/e20592cb/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/UDFCurrentDB.java ---------------------------------------------------------------------- diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/UDFCurrentDB.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/UDFCurrentDB.java deleted file mode 100644 index 1eacc25..0000000 --- a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/UDFCurrentDB.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.udf.generic; - -import org.apache.hadoop.hive.ql.exec.Description; -import org.apache.hadoop.hive.ql.exec.MapredContext; -import org.apache.hadoop.hive.ql.exec.UDFArgumentException; -import org.apache.hadoop.hive.ql.metadata.HiveException; -import org.apache.hadoop.hive.ql.session.SessionState; -import org.apache.hadoop.hive.ql.udf.UDFType; -import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; -import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory; -import org.apache.hadoop.io.Text; - -// This function is not a deterministic function, but a runtime constant. -// The return value is constant within a query but can be different between queries. -@UDFType(deterministic = false, runtimeConstant = true) -@Description(name = "current_database", - value = "_FUNC_() - returns currently using database name") -@NDV(maxNdv = 1) -public class UDFCurrentDB extends GenericUDF { - - private MapredContext context; - - @Override - public void configure(MapredContext context) { - this.context = context; - } - - @Override - public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { - String database; - if (context != null) { - database = context.getJobConf().get("hive.current.database"); - } else { - database = SessionState.get().getCurrentDatabase(); - } - return PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector( - TypeInfoFactory.stringTypeInfo, new Text(database)); - } - - @Override - public Object evaluate(DeferredObject[] arguments) throws HiveException { - return SessionState.get().getCurrentDatabase(); - } - - @Override - public String getDisplayString(String[] children) { - return "current_database()"; - } -} http://git-wip-us.apache.org/repos/asf/hive/blob/e20592cb/ql/src/test/queries/clientpositive/current_catalog_and_schema.q ---------------------------------------------------------------------- diff --git a/ql/src/test/queries/clientpositive/current_catalog_and_schema.q b/ql/src/test/queries/clientpositive/current_catalog_and_schema.q new file mode 100644 index 0000000..b4e4855 --- /dev/null +++ b/ql/src/test/queries/clientpositive/current_catalog_and_schema.q @@ -0,0 +1,10 @@ +use default; + +select current_catalog(); +select current_schema(); + +create database test_current_database; +use test_current_database; + +select current_catalog(); +select current_schema(); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hive/blob/e20592cb/ql/src/test/results/clientpositive/current_catalog_and_schema.q.out ---------------------------------------------------------------------- diff --git a/ql/src/test/results/clientpositive/current_catalog_and_schema.q.out b/ql/src/test/results/clientpositive/current_catalog_and_schema.q.out new file mode 100644 index 0000000..145845c --- /dev/null +++ b/ql/src/test/results/clientpositive/current_catalog_and_schema.q.out @@ -0,0 +1,54 @@ +PREHOOK: query: use default +PREHOOK: type: SWITCHDATABASE +PREHOOK: Input: database:default +POSTHOOK: query: use default +POSTHOOK: type: SWITCHDATABASE +POSTHOOK: Input: database:default +PREHOOK: query: select current_catalog() +PREHOOK: type: QUERY +PREHOOK: Input: _dummy_database@_dummy_table +#### A masked pattern was here #### +POSTHOOK: query: select current_catalog() +POSTHOOK: type: QUERY +POSTHOOK: Input: _dummy_database@_dummy_table +#### A masked pattern was here #### +hive +PREHOOK: query: select current_schema() +PREHOOK: type: QUERY +PREHOOK: Input: _dummy_database@_dummy_table +#### A masked pattern was here #### +POSTHOOK: query: select current_schema() +POSTHOOK: type: QUERY +POSTHOOK: Input: _dummy_database@_dummy_table +#### A masked pattern was here #### +default +PREHOOK: query: create database test_current_database +PREHOOK: type: CREATEDATABASE +PREHOOK: Output: database:test_current_database +POSTHOOK: query: create database test_current_database +POSTHOOK: type: CREATEDATABASE +POSTHOOK: Output: database:test_current_database +PREHOOK: query: use test_current_database +PREHOOK: type: SWITCHDATABASE +PREHOOK: Input: database:test_current_database +POSTHOOK: query: use test_current_database +POSTHOOK: type: SWITCHDATABASE +POSTHOOK: Input: database:test_current_database +PREHOOK: query: select current_catalog() +PREHOOK: type: QUERY +PREHOOK: Input: _dummy_database@_dummy_table +#### A masked pattern was here #### +POSTHOOK: query: select current_catalog() +POSTHOOK: type: QUERY +POSTHOOK: Input: _dummy_database@_dummy_table +#### A masked pattern was here #### +hive +PREHOOK: query: select current_schema() +PREHOOK: type: QUERY +PREHOOK: Input: _dummy_database@_dummy_table +#### A masked pattern was here #### +POSTHOOK: query: select current_schema() +POSTHOOK: type: QUERY +POSTHOOK: Input: _dummy_database@_dummy_table +#### A masked pattern was here #### +test_current_database http://git-wip-us.apache.org/repos/asf/hive/blob/e20592cb/ql/src/test/results/clientpositive/show_functions.q.out ---------------------------------------------------------------------- diff --git a/ql/src/test/results/clientpositive/show_functions.q.out b/ql/src/test/results/clientpositive/show_functions.q.out index 91d3660..e91cffd 100644 --- a/ql/src/test/results/clientpositive/show_functions.q.out +++ b/ql/src/test/results/clientpositive/show_functions.q.out @@ -64,9 +64,11 @@ crc32 create_union cume_dist current_authorizer +current_catalog current_database current_date current_groups +current_schema current_timestamp current_user date_add @@ -321,9 +323,11 @@ crc32 create_union cume_dist current_authorizer +current_catalog current_database current_date current_groups +current_schema current_timestamp current_user PREHOOK: query: SHOW FUNCTIONS '.*e$'