You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by kr...@apache.org on 2023/09/07 04:41:31 UTC

[hive] branch master updated: HIVE-27665: Change HMS PartitionFilter to allow backticks surrounding the column name. (Stephen Carlin, reviewed by Krisztian Kasa)

This is an automated email from the ASF dual-hosted git repository.

krisztiankasa pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new 0318fcf4bd0 HIVE-27665: Change HMS PartitionFilter to allow backticks surrounding the column name. (Stephen Carlin, reviewed by Krisztian Kasa)
0318fcf4bd0 is described below

commit 0318fcf4bd086825a19358b6beab032be676d5ba
Author: scarlin-cloudera <55...@users.noreply.github.com>
AuthorDate: Wed Sep 6 21:41:24 2023 -0700

    HIVE-27665: Change HMS PartitionFilter to allow backticks surrounding the column name. (Stephen Carlin, reviewed by Krisztian Kasa)
---
 .../hive/metastore/parser/PartFilterVisitor.java     | 20 ++++++++++++++++----
 .../hadoop/hive/metastore/parser/PartitionFilter.g4  |  6 ++++++
 .../hive/metastore/TestPartFilterExprUtil.java       | 14 ++++++++++++++
 3 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/PartFilterVisitor.java b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/PartFilterVisitor.java
index 28f74fb8635..f5fe429b7c2 100644
--- a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/PartFilterVisitor.java
+++ b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/PartFilterVisitor.java
@@ -32,6 +32,7 @@ import java.util.stream.Collectors;
 import org.antlr.v4.runtime.misc.ParseCancellationException;
 import org.antlr.v4.runtime.tree.RuleNode;
 import org.antlr.v4.runtime.tree.TerminalNode;
+import org.apache.commons.lang3.StringUtils;
 
 import static org.apache.hadoop.hive.metastore.parser.ExpressionTree.LeafNode;
 import static org.apache.hadoop.hive.metastore.parser.ExpressionTree.LogicalOperator;
@@ -110,7 +111,7 @@ public class PartFilterVisitor extends PartitionFilterBaseVisitor<Object> {
   @Override
   public TreeNode visitComparison(PartitionFilterParser.ComparisonContext ctx) {
     LeafNode leafNode = new LeafNode();
-    leafNode.keyName = ctx.key.getText();
+    leafNode.keyName = (String) visit(ctx.key);
     leafNode.value = visit(ctx.value);
     leafNode.operator = visitComparisonOperator(ctx.comparisonOperator());
     return leafNode;
@@ -119,7 +120,7 @@ public class PartFilterVisitor extends PartitionFilterBaseVisitor<Object> {
   @Override
   public Object visitReverseComparison(PartitionFilterParser.ReverseComparisonContext ctx) {
     LeafNode leafNode = new LeafNode();
-    leafNode.keyName = ctx.key.getText();
+    leafNode.keyName = (String) visit(ctx.key);
     leafNode.value = visit(ctx.value);
     leafNode.operator = visitComparisonOperator(ctx.comparisonOperator());
     leafNode.isReverseOrder = true;
@@ -130,7 +131,7 @@ public class PartFilterVisitor extends PartitionFilterBaseVisitor<Object> {
   public TreeNode visitBetweenCondition(PartitionFilterParser.BetweenConditionContext ctx) {
     LeafNode left = new LeafNode();
     LeafNode right = new LeafNode();
-    left.keyName = right.keyName = ctx.key.getText();
+    left.keyName = right.keyName = (String) visit(ctx.key);
     left.value = visit(ctx.lower);
     right.value = visit(ctx.upper);
 
@@ -147,7 +148,7 @@ public class PartFilterVisitor extends PartitionFilterBaseVisitor<Object> {
   public TreeNode visitInCondition(PartitionFilterParser.InConditionContext ctx) {
     List<Object> values = visitConstantSeq(ctx.constantSeq());
     boolean isPositive = ctx.NOT() == null;
-    String keyName = ctx.key.getText();
+    String keyName = (String) visit(ctx.key);
     List<LeafNode> nodes = values.stream()
         .map(value -> {
           LeafNode leafNode = new LeafNode();
@@ -264,4 +265,15 @@ public class PartFilterVisitor extends PartitionFilterBaseVisitor<Object> {
       throw new ParseCancellationException(e.getMessage());
     }
   }
+
+  @Override
+  public String visitUnquotedIdentifer(PartitionFilterParser.UnquotedIdentiferContext ctx) {
+    return ctx.getText();
+  }
+
+  @Override
+  public String visitQuotedIdentifier(PartitionFilterParser.QuotedIdentifierContext ctx) {
+    return StringUtils.replace(ctx.getText().substring(1, ctx.getText().length() -1 ), "``", "`");
+  }
+
 }
diff --git a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/PartitionFilter.g4 b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/PartitionFilter.g4
index 6ff85a3e061..d30f2057256 100644
--- a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/PartitionFilter.g4
+++ b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/PartitionFilter.g4
@@ -47,6 +47,7 @@ comparisonOperator
 
 identifier
     : IDENTIFIER #unquotedIdentifer
+    | QUOTEDIDENTIFIER #quotedIdentifier
     ;
 
 identifierList
@@ -132,6 +133,11 @@ IDENTIFIER
     : (LETTER | DIGIT | '_')+
     ;
 
+QUOTEDIDENTIFIER
+    :
+    ('`'  ( '``' | ~('`') )* '`')
+    ;
+
 fragment LETTER
     : [A-Z]
     ;
diff --git a/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestPartFilterExprUtil.java b/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestPartFilterExprUtil.java
index c3c504c2e14..df1666ae530 100644
--- a/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestPartFilterExprUtil.java
+++ b/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestPartFilterExprUtil.java
@@ -69,6 +69,20 @@ public class TestPartFilterExprUtil {
     "TreeNode{lhs=TreeNode{lhs=LeafNode{keyName='j', operator='=', value=1990-11-10}, andOr='OR', rhs=LeafNode{keyName='j', operator='=', value=1990-11-11}}, andOr='OR', rhs=LeafNode{keyName='j', operator='=', value=1990-11-12}}");
   }
 
+  @Test
+  public void testDateColumnNameKeyword() throws MetaException {
+    MetaException exception = assertThrows(MetaException.class,
+            () -> PartFilterExprUtil.parseFilterTree("date='1990-11-10'"));
+
+    assertTrue(exception.getMessage().contains("Error parsing partition filter"));
+  }
+
+  @Test
+  public void testDateColumnNameKeywordWithBackTicks() throws MetaException {
+    checkFilter("`date`='1990-11-10'",
+    "LeafNode{keyName='date', operator='=', value=1990-11-10}");
+  }
+
   @Test
   public void testSingleColInExpressionWhenDateLiteralTypeIsSpecified() throws MetaException {
     checkFilter("(j) IN (DATE'1990-11-10', DATE'1990-11-11', DATE'1990-11-12')",