Skip to content

Commit b507761

Browse files
authored
Always allow unquoted keywords as column names (#539)
* always allow unquote keywords as column names * add comment + update unit test
1 parent 6172733 commit b507761

File tree

5 files changed

+12
-54
lines changed

5 files changed

+12
-54
lines changed

coral-hive/src/main/antlr/roots/com/linkedin/coral/hive/hive2rel/parsetree/parser/HiveParser.g

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -657,17 +657,13 @@ import org.slf4j.LoggerFactory;
657657
this.hiveConf = hiveConf;
658658
}
659659
protected boolean useSQL11ReservedKeywordsForIdentifier() {
660-
try {
661-
/*
662-
* Use the config string hive.support.sql11.reserved.keywords directly as
663-
* HiveConf.ConfVars.HIVE_SUPPORT_SQL11_RESERVED_KEYWORDS might not be available in the hive-common present in the
664-
* classpath during translation triggering the exception path defaulting to false
665-
*/
666-
return !hiveConf.get("hive.support.sql11.reserved.keywords").equalsIgnoreCase("true");
667-
} catch (Throwable throwable) {
668-
LOG.warn(throwable.getMessage());
669-
return false;
670-
}
660+
/*
661+
* This enables the translation of keywords as column names without adding backquotes. This is required for translating views
662+
* created using spark engine as certain keywords in hive like timestamp are not keywords in spark. This will
663+
* result in creation of views without backquoting those keywords. This should return false when coral-spark becomes
664+
* a supported LHS for translations.
665+
*/
666+
return true;
671667
}
672668
}
673669

coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/parsetree/ParseTreeBuilder.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.ArrayList;
99
import java.util.Collections;
1010
import java.util.List;
11-
import java.util.Map;
1211
import java.util.Optional;
1312
import java.util.regex.Matcher;
1413
import java.util.regex.Pattern;
@@ -105,20 +104,8 @@ public SqlNode processSql(String sql) {
105104
return process(sql, null);
106105
}
107106

108-
/**
109-
* Returns true if the view is created using spark sql. This relies on the presence of the
110-
* spark.sql.create.version property in the views when created using spark sql.
111-
*
112-
* @param hiveView
113-
* @return true if the view is created using spark sql
114-
*/
115-
private static boolean isCreatedUsingSpark(Table hiveView) {
116-
Map<String, String> tableParams = hiveView.getParameters();
117-
return tableParams != null && tableParams.containsKey("spark.sql.create.version");
118-
}
119-
120107
public SqlNode process(String sql, @Nullable Table hiveView) {
121-
ParseDriver pd = new CoralParseDriver(hiveView != null && isCreatedUsingSpark(hiveView));
108+
ParseDriver pd = new CoralParseDriver();
122109
try {
123110
ASTNode root = pd.parse(sql);
124111
return processAST(root, hiveView);

coral-hive/src/main/java/com/linkedin/coral/hive/hive2rel/parsetree/parser/CoralParseDriver.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,13 @@
1313
import org.antlr.runtime.TokenRewriteStream;
1414
import org.apache.commons.logging.Log;
1515
import org.apache.commons.logging.LogFactory;
16-
import org.apache.hadoop.hive.conf.HiveConf;
1716

1817

1918
public class CoralParseDriver extends ParseDriver {
2019

2120
private static final Log LOG =
2221
LogFactory.getLog("com.linkedin.coral.hive.hive2rel.parsetree.parser.CoralParseDriver");
2322

24-
private boolean useSQL11ReservedKeywordsForIdentifier;
25-
26-
public CoralParseDriver(boolean useSQL11ReservedKeywordsForIdentifier) {
27-
super();
28-
this.useSQL11ReservedKeywordsForIdentifier = useSQL11ReservedKeywordsForIdentifier;
29-
}
30-
31-
public CoralParseDriver() {
32-
super();
33-
this.useSQL11ReservedKeywordsForIdentifier = false;
34-
}
35-
3623
@Override
3724
public ASTNode parse(String command) throws ParseException {
3825
if (LOG.isDebugEnabled()) {
@@ -42,17 +29,6 @@ public ASTNode parse(String command) throws ParseException {
4229
HiveLexerCoral lexer = new HiveLexerCoral(new ANTLRNoCaseStringStream(command));
4330
TokenRewriteStream tokens = new TokenRewriteStream(lexer);
4431
HiveParser parser = new HiveParser(tokens);
45-
HiveConf hiveConf = new HiveConf();
46-
/*
47-
* This enables usage of keywords as column names without adding backquotes. This is required for translating views
48-
* created using spark engine as certain keywords in hive like timestamp are not keywords in spark. This will
49-
* result in creation of views without backquoting those keywords. This will be removed when coral-spark becomes
50-
* a supported LHS for translations.
51-
*/
52-
if (useSQL11ReservedKeywordsForIdentifier) {
53-
hiveConf.set("hive.support.sql11.reserved.keywords", "false");
54-
parser.setHiveConf(hiveConf);
55-
}
5632
parser.setTreeAdaptor(adaptor);
5733
HiveParser.statement_return r = null;
5834
try {

coral-hive/src/test/java/com/linkedin/coral/hive/hive2rel/TestUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,8 @@ public static TestHive setupDefaultHive(HiveConf conf) throws IOException {
174174
driver.run("CREATE VIEW IF NOT EXISTS view_schema_evolve_wrapper AS SELECT * from view_schema_evolve");
175175
driver.run("ALTER TABLE schema_evolve CHANGE COLUMN b b array<struct<b1:string, b2:double, b3:int>>");
176176

177-
driver.run("CREATE OR REPLACE VIEW test.spark_created_view AS SELECT 1 AS `timestamp` FROM test.tableOne");
178-
// Simulate the creation of view using spark by setting the corresponding table property of the view.
179-
driver.run("ALTER VIEW test.spark_created_view SET TBLPROPERTIES ('spark.sql.create.version'='3.1.1')");
177+
driver.run(
178+
"CREATE OR REPLACE VIEW test.quoted_reserved_keyword_view AS SELECT 1 AS `timestamp` FROM test.tableOne");
180179

181180
CommandProcessorResponse response = driver
182181
.run("create function test_tableOneView_LessThanHundred as 'com.linkedin.coral.hive.hive2rel.CoralTestUDF'");

coral-hive/src/test/java/com/linkedin/coral/hive/hive2rel/parsetree/ParseTreeBuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ public void testUnsupportedOuterExplodeWithoutColumns() {
236236
}
237237

238238
/**
239-
* Validates if coral-hive can translate views with unquoted reserved keywords when the views are created using spark.
239+
* Validates if coral-hive can translate views with unquoted reserved keywords as column names.
240240
*/
241241
@Test
242242
public void testUnquotedKeywordAsColumnName() {
243243
HiveToRelConverter hiveToRelConverter = new HiveToRelConverter(msc);
244-
Table table = msc.getTable("test", "spark_created_view");
244+
Table table = msc.getTable("test", "quoted_reserved_keyword_view");
245245
// Remove the backquotes associated with the view text
246246
String input = table.getViewExpandedText().replaceAll("`", "");
247247
SqlNode sqlNode = hiveToRelConverter.toSqlNode(input, table);

0 commit comments

Comments
 (0)