Skip to content

Commit

Permalink
Add support for Hive current_user function (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
losipiuk authored Jan 28, 2021
1 parent 33d1add commit cf00cf5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ public RelDataType inferReturnType(SqlOperatorBinding opBinding) {

// UDTFs
addFunctionEntry("explode", HiveExplodeOperator.EXPLODE);

// Context functions
addFunctionEntry("current_user", CURRENT_USER);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand Down Expand Up @@ -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);
}
}

0 comments on commit cf00cf5

Please sign in to comment.