From cf00cf5c753b8a2846ba9c62b2e6270ab0b7672f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Osipiuk?= Date: Thu, 28 Jan 2021 09:49:17 +0100 Subject: [PATCH] Add support for Hive current_user function (#40) --- .../hive/hive2rel/HiveSqlConformance.java | 24 +++++++++++++++++++ .../hive/hive2rel/RelContextProvider.java | 5 ++-- .../functions/StaticHiveFunctionRegistry.java | 3 +++ .../hive/hive2rel/HiveToRelConverterTest.java | 10 +++++++- .../rel2presto/RelToPrestoConverterTest.java | 9 ++++++- 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/HiveSqlConformance.java diff --git a/coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/HiveSqlConformance.java b/coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/HiveSqlConformance.java new file mode 100644 index 000000000..39128b516 --- /dev/null +++ b/coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/HiveSqlConformance.java @@ -0,0 +1,24 @@ +/** + * Copyright 2021 LinkedIn Corporation. All rights reserved. + * Licensed under the BSD-2 Clause license. + * See LICENSE in the project root for license information. + */ +package com.linkedin.coral.hive.hive2rel; + +import org.apache.calcite.sql.validate.SqlConformance; +import org.apache.calcite.sql.validate.SqlDelegatingConformance; + + +public class HiveSqlConformance extends SqlDelegatingConformance { + + public static SqlConformance HIVE_SQL = new HiveSqlConformance(); + + private HiveSqlConformance() { + super(PRAGMATIC_2003); + } + + @Override + public boolean allowNiladicParentheses() { + return true; + } +} diff --git a/coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/RelContextProvider.java b/coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/RelContextProvider.java index eb0e59afe..a792b713f 100644 --- a/coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/RelContextProvider.java +++ b/coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/RelContextProvider.java @@ -30,7 +30,6 @@ import org.apache.calcite.schema.SchemaPlus; import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.util.ChainedSqlOperatorTable; -import org.apache.calcite.sql.validate.SqlConformanceEnum; import org.apache.calcite.sql2rel.SqlToRelConverter; import org.apache.calcite.tools.FrameworkConfig; import org.apache.calcite.tools.Frameworks; @@ -42,6 +41,8 @@ import com.linkedin.coral.hive.hive2rel.functions.StaticHiveFunctionRegistry; import com.linkedin.coral.hive.hive2rel.parsetree.ParseTreeBuilder; +import static com.linkedin.coral.hive.hive2rel.HiveSqlConformance.HIVE_SQL; + /** * Calcite needs different objects that are not trivial to create. This class @@ -196,7 +197,7 @@ CalciteCatalogReader getCalciteCatalogReader() { HiveSqlValidator getHiveSqlValidator() { if (sqlValidator == null) { sqlValidator = new HiveSqlValidator(config.getOperatorTable(), getCalciteCatalogReader(), - ((JavaTypeFactory) relBuilder.getTypeFactory()), SqlConformanceEnum.PRAGMATIC_2003); + ((JavaTypeFactory) relBuilder.getTypeFactory()), HIVE_SQL); } return sqlValidator; } diff --git a/coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/functions/StaticHiveFunctionRegistry.java b/coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/functions/StaticHiveFunctionRegistry.java index c0e0c40b0..9f71cc4ae 100644 --- a/coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/functions/StaticHiveFunctionRegistry.java +++ b/coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/functions/StaticHiveFunctionRegistry.java @@ -401,6 +401,9 @@ public RelDataType inferReturnType(SqlOperatorBinding opBinding) { // UDTFs addFunctionEntry("explode", HiveExplodeOperator.EXPLODE); + + // Context functions + addFunctionEntry("current_user", CURRENT_USER); } /** diff --git a/coral-hive/src/test/java/com/linkedin/coral/hive/hive2rel/HiveToRelConverterTest.java b/coral-hive/src/test/java/com/linkedin/coral/hive/hive2rel/HiveToRelConverterTest.java index 946d00ace..f0fe7f95c 100644 --- a/coral-hive/src/test/java/com/linkedin/coral/hive/hive2rel/HiveToRelConverterTest.java +++ b/coral-hive/src/test/java/com/linkedin/coral/hive/hive2rel/HiveToRelConverterTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2020 LinkedIn Corporation. All rights reserved. + * Copyright 2017-2021 LinkedIn Corporation. All rights reserved. * Licensed under the BSD-2 Clause license. * See LICENSE in the project root for license information. */ @@ -316,6 +316,14 @@ public void testConversionWithLocalMetastore() { assertEquals(relToHql(rel), expectedSql); } + @Test + public void testCurrentUser() { + final String sql = "SELECT current_user() as cu"; + String generated = relToString(sql); + final String expected = "LogicalProject(cu=[CURRENT_USER])\n LogicalValues(tuples=[[{ 0 }]])\n"; + assertEquals(generated, expected); + } + private String relToString(String sql) { return RelOptUtil.toString(converter.convertSql(sql)); } diff --git a/coral-presto/src/test/java/com/linkedin/coral/presto/rel2presto/RelToPrestoConverterTest.java b/coral-presto/src/test/java/com/linkedin/coral/presto/rel2presto/RelToPrestoConverterTest.java index 74baa7969..1671547ee 100644 --- a/coral-presto/src/test/java/com/linkedin/coral/presto/rel2presto/RelToPrestoConverterTest.java +++ b/coral-presto/src/test/java/com/linkedin/coral/presto/rel2presto/RelToPrestoConverterTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2020 LinkedIn Corporation. All rights reserved. + * Copyright 2017-2021 LinkedIn Corporation. All rights reserved. * Licensed under the BSD-2 Clause license. * See LICENSE in the project root for license information. */ @@ -458,4 +458,11 @@ public void testDataTypeSpecRewrite() { String expectedSql3 = formatSql("SELECT CAST(varbinaryfield AS VARBINARY) FROM " + tableThree); testConversion(sql3, expectedSql3); } + + @Test + public void testCurrentUser() { + String sql = "SELECT current_user"; + String expected = formatSql("SELECT CURRENT_USER AS \"CURRENT_USER\"\nFROM (VALUES (0)) AS \"t\" (\"ZERO\")"); + testConversion(sql, expected); + } }