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 2022/07/13 01:02:09 UTC
[GitHub] [hive] scarlin-cloudera opened a new pull request, #3433: HIVE-26294: Allow substr to take bigint params
scarlin-cloudera opened a new pull request, #3433:
URL: https://github.com/apache/hive/pull/3433
Impala allows a bigint as an argument for the substr function.
This will allow Hive substr function to take bigint arguments too.
<!--
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.
-->
Allow substr function to take bigint arguments
### 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.
-->
Extra functionality, in case the param passed in is a bigint instead of an int. No reason why we can't support this.
### 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'.
-->
Yes, we should change our user documentation to show that we allow bigint params
### 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.
-->
Added unit tests
--
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.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
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] scarlin-cloudera commented on a diff in pull request #3433: HIVE-26294: Allow substr to take bigint params
Posted by GitBox <gi...@apache.org>.
scarlin-cloudera commented on code in PR #3433:
URL: https://github.com/apache/hive/pull/3433#discussion_r929127785
##########
ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSubstr.java:
##########
@@ -85,6 +107,31 @@ public Text evaluate(Text t, IntWritable pos, IntWritable len) {
return r;
}
+ private int[] makeIndex(long pos, long len, int inputLen) {
+ if ((Math.abs(pos) > inputLen)) {
+ return null;
+ }
+
+ long start, end;
+
+ if (pos > 0) {
+ start = pos - 1;
+ } else if (pos < 0) {
+ start = inputLen + pos;
+ } else {
+ start = 0;
+ }
+
+ if ((inputLen - start) < len) {
+ end = inputLen;
+ } else {
+ end = start + len;
+ }
+ index[0] = (int) start;
+ index[1] = (int) end;
+ return index;
+ }
Review Comment:
Done
--
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.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
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] scarlin-cloudera commented on a diff in pull request #3433: HIVE-26294: Allow substr to take bigint params
Posted by GitBox <gi...@apache.org>.
scarlin-cloudera commented on code in PR #3433:
URL: https://github.com/apache/hive/pull/3433#discussion_r929128673
##########
ql/src/test/queries/clientpositive/udf_substr.q:
##########
@@ -76,3 +76,8 @@ SELECT
substr("abc 玩玩玩 abc", 5),
substr("abc 玩玩玩 abc", 5, 3)
FROM src tablesample (1 rows);
+
+SELECT
+ substr('ABC', cast(1 as bigint), cast(0 as bigint)),
+ substr('ABC', cast(4 as bigint))
+FROM src tablesample (1 rows);
Review Comment:
Added.
I'm not sure what is best. I wavered between returning NULL or using MAX_INT. I ultimately decided to use NULL since we use that in other functions when we can't return the correct value. What do you think?
--
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.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
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 diff in pull request #3433: HIVE-26294: Allow substr to take bigint params
Posted by GitBox <gi...@apache.org>.
kasakrisz commented on code in PR #3433:
URL: https://github.com/apache/hive/pull/3433#discussion_r934330702
##########
ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSubstr.java:
##########
@@ -64,19 +64,39 @@ public UDFSubstr() {
r = new Text();
}
- public Text evaluate(Text t, IntWritable pos, IntWritable len) {
+ public Text evaluate(Text t, LongWritable pos, LongWritable len) {
+ if ((t == null) || (pos == null) || (len == null)) {
+ return null;
+ }
+
+ long longPos = pos.get();
+ long longLen = len.get();
+ // If an unsupported value is seen, we don't want to return a string
+ // that doesn't match what the user expects, so we return NULL (still
+ // unexpected, of course, but probably better than a bad string).
+ if (longPos > Integer.MAX_VALUE || longLen > Integer.MAX_VALUE) {
Review Comment:
Should negative overflow also be handled?
```
long l = Integer.MIN_VALUE - 10L;
int i = (int)l;
```
I think `i` is going to be a positive integer.
--
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.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
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 #3433: HIVE-26294: Allow substr to take bigint params
Posted by GitBox <gi...@apache.org>.
kasakrisz merged PR #3433:
URL: https://github.com/apache/hive/pull/3433
--
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.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
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 diff in pull request #3433: HIVE-26294: Allow substr to take bigint params
Posted by GitBox <gi...@apache.org>.
kasakrisz commented on code in PR #3433:
URL: https://github.com/apache/hive/pull/3433#discussion_r923998397
##########
ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSubstr.java:
##########
@@ -64,6 +65,27 @@ public UDFSubstr() {
r = new Text();
}
+ public Text evaluate(Text t, LongWritable pos, LongWritable len) {
+
+ if ((t == null) || (pos == null) || (len == null)) {
+ return null;
+ }
+
+ r.clear();
+ if ((len.get() <= 0)) {
+ return r;
+ }
+
+ String s = t.toString();
+ int[] index = makeIndex(pos.get(), len.get(), s.length());
+ if (index == null) {
+ return r;
+ }
+
+ r.set(s.substring(index[0], index[1]));
+ return r;
+ }
Review Comment:
Java String accepts int parameters only
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-int-
How about creating a common `evaluate` which receives int parameters and both the `LongWritable` and `IntWritable` version call it?
```
public Text evaluate(Text t, IntWritable pos, IntWritable len) {
if ((t == null) || (pos == null) || (len == null)) {
return null;
}
return evaluate(t, pos.get(), len.get());
}
public Text evaluate(Text t, LongWritable pos, LongWritable len) {
if ((t == null) || (pos == null) || (len == null)) {
return null;
}
// Handle long int overflow
return evaluate(t, (int)pos.get(), (int)len.get());
}
private Text evaluate(Text t, int pos, int len) {
...
}
```
##########
ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSubstr.java:
##########
@@ -85,6 +107,31 @@ public Text evaluate(Text t, IntWritable pos, IntWritable len) {
return r;
}
+ private int[] makeIndex(long pos, long len, int inputLen) {
+ if ((Math.abs(pos) > inputLen)) {
+ return null;
+ }
+
+ long start, end;
+
+ if (pos > 0) {
+ start = pos - 1;
+ } else if (pos < 0) {
+ start = inputLen + pos;
+ } else {
+ start = 0;
+ }
+
+ if ((inputLen - start) < len) {
+ end = inputLen;
+ } else {
+ end = start + len;
+ }
+ index[0] = (int) start;
+ index[1] = (int) end;
+ return index;
+ }
Review Comment:
This is unnecessary if both `LongWritable` and `IntWritable` `evaluate` call the common one. See my previous comment.
##########
ql/src/test/queries/clientpositive/udf_substr.q:
##########
@@ -76,3 +76,8 @@ SELECT
substr("abc 玩玩玩 abc", 5),
substr("abc 玩玩玩 abc", 5, 3)
FROM src tablesample (1 rows);
+
+SELECT
+ substr('ABC', cast(1 as bigint), cast(0 as bigint)),
+ substr('ABC', cast(4 as bigint))
+FROM src tablesample (1 rows);
Review Comment:
Could you please add a test when one of the long params has a value which doesn't falls into the int range? How should we handle that?
##########
ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSubstr.java:
##########
@@ -112,10 +159,34 @@ private int[] makeIndex(int pos, int len, int inputLen) {
private final IntWritable maxValue = new IntWritable(Integer.MAX_VALUE);
+ private final LongWritable maxLongValue = new LongWritable(Long.MAX_VALUE);
+
public Text evaluate(Text s, IntWritable pos) {
return evaluate(s, pos, maxValue);
}
+ public Text evaluate(Text s, LongWritable pos) {
+ return evaluate(s, pos, maxLongValue);
+ }
+
+ public BytesWritable evaluate(BytesWritable bw, LongWritable pos, LongWritable len) {
+
+ if ((bw == null) || (pos == null) || (len == null)) {
+ return null;
+ }
+
+ if ((len.get() <= 0)) {
+ return new BytesWritable();
+ }
+
+ int[] index = makeIndex(pos.get(), len.get(), bw.getLength());
+ if (index == null) {
+ return new BytesWritable();
+ }
+
+ return new BytesWritable(Arrays.copyOfRange(bw.getBytes(), index[0], index[1]));
+ }
Review Comment:
The pattern for the `Text` version of the `evaluate` can be applied here.
--
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.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
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