You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2020/12/23 16:42:27 UTC

[GitHub] [hive] kasakrisz opened a new pull request #1810: [WIP] HIVE-24565: Implement standard trim function

kasakrisz opened a new pull request #1810:
URL: https://github.com/apache/hive/pull/1810


   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://cwiki.apache.org/confluence/display/Hive/HowToContribute
     2. Ensure that you have created an issue on the Hive project JIRA: https://issues.apache.org/jira/projects/HIVE/summary
     3. Ensure you have added or run the appropriate tests for your PR: 
     4. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP]HIVE-XXXXX:  Your PR title ...'.
     5. Be sure to keep the PR description updated to reflect all changes.
     6. Please write your PR title to summarize what this PR proposes.
     7. If possible, provide a concise example to reproduce the issue for a faster review.
   
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description, screenshot and/or a reproducable example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Hive versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   -->
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kasakrisz merged pull request #1810: HIVE-24565: Implement standard trim function

Posted by GitBox <gi...@apache.org>.
kasakrisz merged pull request #1810:
URL: https://github.com/apache/hive/pull/1810


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kasakrisz commented on a change in pull request #1810: HIVE-24565: Implement standard trim function

Posted by GitBox <gi...@apache.org>.
kasakrisz commented on a change in pull request #1810:
URL: https://github.com/apache/hive/pull/1810#discussion_r551761602



##########
File path: ql/src/test/queries/clientpositive/udf_trim.q
##########
@@ -1,2 +1,20 @@
 DESCRIBE FUNCTION trim;
 DESCRIBE FUNCTION EXTENDED trim;
+
+SELECT '"' || trim('   tech   ') || '"';
+
+SELECT '"' || TRIM(' '  FROM  '   tech   ') || '"';
+
+SELECT '"' || TRIM(LEADING '0' FROM '000123') || '"';
+
+SELECT '"' || TRIM(TRAILING '1' FROM 'Tech1') || '"';
+
+SELECT '"' || TRIM(BOTH '1' FROM '123Tech111') || '"';
+
+SELECT '"' || ltrim('   tech   ') || '"', '"' || rtrim('   tech   ') || '"';
+
+SELECT '"' || lTRIM('0'  FROM  '000123') || '"', '"' || rTRIM('0'  FROM  '000123') || '"';
+
+SELECT trim('000123', '0');

Review comment:
       `null` cannot be passed as a parameter to `trim`. It works in Postgres and Oracle but not supported in Hive.
   The following was executed on master without this patch:
   ```
   select trim(null);
   
    org.apache.hadoop.hive.ql.parse.SemanticException: Line 6:7 Wrong arguments 'TOK_NULL': trim takes only STRING/CHAR/VARCHAR types. Found VOID
   ```
   
   IMHO we can implement this in a follow-up patch.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kasakrisz commented on a change in pull request #1810: HIVE-24565: Implement standard trim function

Posted by GitBox <gi...@apache.org>.
kasakrisz commented on a change in pull request #1810:
URL: https://github.com/apache/hive/pull/1810#discussion_r551958137



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseTrim.java
##########
@@ -68,11 +82,24 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
     if (valObject == null) {
       return null;
     }
-    String val = ((Text) converter.convert(valObject)).toString();
+    String val = stringToTrimConverter.convert(valObject).toString();
     if (val == null) {
       return null;
     }
-    result.set(performOp(val.toString()));
+
+    String trimChars = " ";

Review comment:
       I add supporting vectorized two parameter version of trim functions when the first parameter is a column the second is a literal. Example: 
   ```
   create table t1 (col0 string);
   select trim(col0, 'xy') from t1 group by col0;
   ```
   In case of trim chars parameter is also column we fall back for non-vectorized version of trim. I guess this use case is not as common as the previous but it can be implemented in a follow-up patch.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] jcamachor commented on a change in pull request #1810: [WIP] HIVE-24565: Implement standard trim function

Posted by GitBox <gi...@apache.org>.
jcamachor commented on a change in pull request #1810:
URL: https://github.com/apache/hive/pull/1810#discussion_r548335382



##########
File path: parser/src/java/org/apache/hadoop/hive/ql/parse/HiveLexerParent.g
##########
@@ -373,6 +373,8 @@ KW_COST: 'COST';
 KW_JOINCOST: 'JOINCOST';
 KW_WITHIN: 'WITHIN';
 KW_PKFK_JOIN: 'PKFK_JOIN';
+KW_LEADING: 'LEADING';

Review comment:
       Since these two keywords are reserved, can you add a test to `TestSQL11ReservedKeyWordsNegative.java`?

##########
File path: parser/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g
##########
@@ -464,6 +473,7 @@ atomExpression
     | whenExpression
     | (subQueryExpression)=> (subQueryExpression)
         -> ^(TOK_SUBQUERY_EXPR TOK_SUBQUERY_OP subQueryExpression)
+    | (functionName LPAREN (leading=KW_LEADING | trailing=KW_TRAILING | KW_BOTH)? (trim_characters=selectExpression)? KW_FROM (str=selectExpression) RPAREN) => trimFunction

Review comment:
       I think this would match any function name, that then would be converted to `trim` (or any other variant) by the rules in `trimFunction`?
   
   I think one way of fixing this could be to declare `TRIM` as a keyword, and match directly on `TRIM` (TRIM is a reserved keyword in SQL standard) rather than `functionName`.
   
   Could you also add a negative test, i.e., a test where the trim construct is correct but the function name is different, and make sure it fails and it is not transformed by the parser?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kgyrtkirk commented on a change in pull request #1810: [WIP] HIVE-24565: Implement standard trim function

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on a change in pull request #1810:
URL: https://github.com/apache/hive/pull/1810#discussion_r551257267



##########
File path: ql/src/test/queries/clientpositive/udf_trim.q
##########
@@ -1,2 +1,20 @@
 DESCRIBE FUNCTION trim;
 DESCRIBE FUNCTION EXTENDED trim;
+
+SELECT '"' || trim('   tech   ') || '"';
+
+SELECT '"' || TRIM(' '  FROM  '   tech   ') || '"';
+
+SELECT '"' || TRIM(LEADING '0' FROM '000123') || '"';
+
+SELECT '"' || TRIM(TRAILING '1' FROM 'Tech1') || '"';
+
+SELECT '"' || TRIM(BOTH '1' FROM '123Tech111') || '"';
+
+SELECT '"' || ltrim('   tech   ') || '"', '"' || rtrim('   tech   ') || '"';
+
+SELECT '"' || lTRIM('0'  FROM  '000123') || '"', '"' || rTRIM('0'  FROM  '000123') || '"';
+
+SELECT trim('000123', '0');

Review comment:
       could you also add some`null` cases as well `trim(null,'x')` and `trim('x',null)`
   
   I know they will probably work okay; but its better to cover them

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseTrim.java
##########
@@ -68,11 +82,24 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
     if (valObject == null) {
       return null;
     }
-    String val = ((Text) converter.convert(valObject)).toString();
+    String val = stringToTrimConverter.convert(valObject).toString();
     if (val == null) {
       return null;
     }
-    result.set(performOp(val.toString()));
+
+    String trimChars = " ";

Review comment:
       there is also some vectorized implementations (see `StringRTrim` for example)
   
   the functionality is enhanced a bit here - those other implementations should be updated as well (and possibly also covered with tests)
   

##########
File path: parser/src/java/org/apache/hadoop/hive/ql/parse/HiveLexerParent.g
##########
@@ -373,6 +373,8 @@ KW_COST: 'COST';
 KW_JOINCOST: 'JOINCOST';
 KW_WITHIN: 'WITHIN';
 KW_PKFK_JOIN: 'PKFK_JOIN';
+KW_LEADING: 'LEADING';

Review comment:
       do we need to fully reserve these keywords - if not they could be added to: IdentifiersParser.g/nonReserved 

##########
File path: parser/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g
##########
@@ -464,6 +473,7 @@ atomExpression
     | whenExpression
     | (subQueryExpression)=> (subQueryExpression)
         -> ^(TOK_SUBQUERY_EXPR TOK_SUBQUERY_OP subQueryExpression)
+    | (functionName LPAREN (leading=KW_LEADING | trailing=KW_TRAILING | KW_BOTH)? (trim_characters=selectExpression)? KW_FROM (str=selectExpression) RPAREN) => trimFunction

Review comment:
       this is a syntetic predicate expression; narrowing it down furthere will help for sure ; but since `trimFunction` also matches the `functionName LPAREN` prefix I think it should be placed in the `function` rule




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kasakrisz merged pull request #1810: HIVE-24565: Implement standard trim function

Posted by GitBox <gi...@apache.org>.
kasakrisz merged pull request #1810:
URL: https://github.com/apache/hive/pull/1810


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org