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/11/26 18:38:23 UTC
[GitHub] [hive] mdayakar opened a new pull request, #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
mdayakar opened a new pull request, #3808:
URL: https://github.com/apache/hive/pull/3808
HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Checked and supported all primitive types like boolean, smallint, int, bigint, float, double, string, date, timestamp, decimal, varchar and char types.
<!--
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?
The changes supports users to create HPLSQL functions with all primitive types.
<!--
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?
To fix HIVE-26768 and also it helps users to create HPLSQL functions with all primitive types.
<!--
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?
No
<!--
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?
Tests are added.
<!--
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.
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] mdayakar commented on a diff in pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
mdayakar commented on code in PR #3808:
URL: https://github.com/apache/hive/pull/3808#discussion_r1033335531
##########
itests/hive-unit/src/test/java/org/apache/hive/beeline/TestHplSqlViaBeeLine.java:
##########
@@ -171,17 +171,197 @@ public void testPackage() throws Throwable {
"SELECT * FROM result;\n";
testScriptFile(SCRIPT_TEXT, args(), "12345");
}
-
+
@Test
- public void testUdf() throws Throwable {
+ public void testUdfBoolean() throws Throwable {
String SCRIPT_TEXT =
- "DROP TABLE IF EXISTS result;\n" +
- "CREATE TABLE result (s string);\n" +
- "INSERT INTO result VALUES('alice');\n" +
- "INSERT INTO result VALUES('bob');\n" +
- "CREATE FUNCTION hello(p STRING) RETURNS STRING BEGIN RETURN 'hello ' || p; END;\n" +
- "SELECT hello(s) FROM result;\n";
- testScriptFile(SCRIPT_TEXT, args(), "hello alice.*hello bob");
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_b boolean);\n" +
+ "INSERT INTO result VALUES(true);\n" +
+ "INSERT INTO result VALUES(false);\n" +
+ "CREATE FUNCTION check(b boolean)\n" +
+ " RETURNS STRING\n" +
+ "BEGIN\n" +
+ " RETURN 'This is ' || b;\n" +
+ "END;\n" +
+ "SELECT check(col_b) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "This is true.*This is false");
+ }
+
+ @Test
+ public void testUdfSmallInt() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_s smallint);\n" +
+ "INSERT INTO result VALUES(123);\n" +
+ "INSERT INTO result VALUES(321);\n" +
+ "CREATE FUNCTION dbl(s smallint)\n" +
+ " RETURNS smallint\n" +
+ "BEGIN\n" +
+ " RETURN s + s;\n" +
+ "END;\n" +
+ "SELECT dbl(col_s) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "246.*642");
+ }
+
+ @Test
+ public void testUdfInt() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_i int);\n" +
+ "INSERT INTO result VALUES(12345);\n" +
+ "INSERT INTO result VALUES(54321);\n" +
+ "CREATE FUNCTION dbl(i int)\n" +
+ " RETURNS int\n" +
+ "BEGIN\n" +
+ " RETURN i * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_i) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "24690.*108642");
+ }
+
+ @Test
+ public void testUdfBigInt() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_b bigint);\n" +
+ "INSERT INTO result VALUES(123456789);\n" +
+ "INSERT INTO result VALUES(987654321);\n" +
+ "CREATE FUNCTION dbl(b bigint)\n" +
+ " RETURNS int8\n" +
+ "BEGIN\n" +
+ " RETURN b * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_b) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "246913578.*1975308642");
+ }
+
+ @Test
+ public void testUdfFloat() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_f float);\n" +
+ "INSERT INTO result VALUES(12345.6789);\n" +
+ "INSERT INTO result VALUES(98765.4321);\n" +
+ "CREATE FUNCTION dbl(f float)\n" +
+ " RETURNS float\n" +
+ "BEGIN\n" +
+ " RETURN f * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_f) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "24691.357421875.*197530.859375");
+ }
+
+ @Test
+ public void testUdfDouble() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_d double);\n" +
+ "INSERT INTO result VALUES(123456789.12);\n" +
+ "INSERT INTO result VALUES(987654321.98);\n" +
+ "CREATE FUNCTION dbl(d float)\n" +
+ " RETURNS double\n" +
+ "BEGIN\n" +
+ " RETURN d * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_d) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "2.4691357824E8.*1.97530864396E9");
+ }
+
+ @Test
+ public void testUdfString() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_s string);\n" +
+ "INSERT INTO result VALUES('Alice');\n" +
+ "INSERT INTO result VALUES('Smith');\n" +
+ "CREATE FUNCTION hello(s string)\n" +
+ " RETURNS string\n" +
+ "BEGIN\n" +
+ " RETURN 'Hello ' || s || '!';\n" +
+ "END;\n" +
+ "SELECT hello(col_s) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "Hello Alice!.*Hello Smith!");
+ }
+
+ @Test
+ public void testUdfDate() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_d date);\n" +
+ "INSERT INTO result VALUES('2022-11-24');\n" +
+ "INSERT INTO result VALUES('2022-12-25');\n" +
+ "CREATE FUNCTION date_today(d date)\n" +
+ " RETURNS date\n" +
+ "BEGIN\n" +
+ " RETURN d;\n" +
+ "END;\n" +
+ "SELECT date_today(col_d) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "2022-11-24.*2022-12-25");
+ }
+
+ @Test
+ public void testUdfTimestamp() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_t timestamp);\n" +
+ "INSERT INTO result VALUES('2022-11-24 10:20:30');\n" +
+ "INSERT INTO result VALUES('2022-12-25 06:30:30');\n" +
+ "CREATE FUNCTION time_today(t timestamp)\n" +
+ " RETURNS timestamp\n" +
+ "BEGIN\n" +
+ " RETURN t;\n" +
+ "END;\n" +
+ "SELECT time_today(col_t) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "2022-11-24 10:20:30.*2022-12-25 06:30:30");
+ }
+
+ @Test
+ public void testUdfDecimal() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_d decimal(15,2));\n" +
+ "INSERT INTO result VALUES(123456789.98);\n" +
+ "INSERT INTO result VALUES(987654321.12);\n" +
+ "CREATE FUNCTION triple(d decimal(15,2))\n" +
+ " RETURNS decimal(15,2)\n" +
+ "BEGIN\n" +
+ " RETURN d * 3;\n" +
+ "END;\n" +
+ "SELECT triple(col_d) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "370370369.94.*2962962963.36");
+ }
+
+ @Test
+ public void testUdfVarchar() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_v varchar(20));\n" +
+ "INSERT INTO result VALUES('Smith');\n" +
+ "INSERT INTO result VALUES('Sachin');\n" +
+ "CREATE FUNCTION hello(v varchar(20))\n" +
+ " RETURNS varchar(20)\n" +
+ "BEGIN\n" +
+ " RETURN 'Hello ' || v || '!';\n" +
+ "END;\n" +
+ "SELECT hello(col_v) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "Hello Smith!.*Hello Sachin!");
+ }
+
+ @Test
+ public void testUdfChar() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_c char(10));\n" +
+ "INSERT INTO result VALUES('Daya');\n" +
+ "INSERT INTO result VALUES('Alice');\n" +
+ "CREATE FUNCTION hello(c char(10))\n" +
+ " RETURNS char(10)\n" +
+ "BEGIN\n" +
+ " RETURN 'Hello ' || c || '!';\n" +
+ "END;\n" +
+ "SELECT hello(col_c) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "Hello Daya!.*Hello Alice!");
}
Review Comment:
The function will be called if the actual parameter type can be converted to udf parameter type. For example, if UDF is taking String and actual parameter is decimal then in this case UDF will be called as Decimal can be easily converted to String. Anyway I will add a testcase for the same.
--
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] sonarcloud[bot] commented on pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on PR #3808:
URL: https://github.com/apache/hive/pull/3808#issuecomment-1328762463
Kudos, SonarCloud Quality Gate passed! [](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=3808)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL) [1 Code Smell](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=3808&metric=coverage&view=list) No Coverage information
[](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=3808&metric=duplicated_lines_density&view=list) No Duplication information
--
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] mdayakar commented on a diff in pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
mdayakar commented on code in PR #3808:
URL: https://github.com/apache/hive/pull/3808#discussion_r1033437012
##########
hplsql/src/main/java/org/apache/hive/hplsql/udf/Udf.java:
##########
@@ -128,27 +127,96 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
void setParameters(DeferredObject[] arguments) throws HiveException {
for (int i = 1; i < arguments.length; i++) {
String name = ":" + i;
- if (argumentsOI[i] instanceof StringObjectInspector) {
- String value = ((StringObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, value);
- }
+ Object inputObject = arguments[i].get();
+ ObjectInspector objectInspector = argumentsOI[i];
+ if (objectInspector.getCategory() == ObjectInspector.Category.PRIMITIVE) {
+ setParameterForPrimitiveTypeArgument(name, inputObject, objectInspector);
+ } else {
+ exec.setVariableToNull(name);
}
- else if (argumentsOI[i] instanceof IntObjectInspector) {
- Integer value = (Integer)((IntObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(new Long(value)));
- }
+ }
+ }
+
+ private void setParameterForPrimitiveTypeArgument(String name, Object inputObject, ObjectInspector objectInspector) {
+ PrimitiveObjectInspector.PrimitiveCategory primitiveCategory =
+ ((PrimitiveObjectInspector) objectInspector).getPrimitiveCategory();
+ switch (primitiveCategory) {
+ case BOOLEAN:
+ Boolean booleanValue = (Boolean) ((BooleanObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (booleanValue != null) {
+ exec.setVariable(name, new Var(booleanValue));
}
- else if (argumentsOI[i] instanceof LongObjectInspector) {
- Long value = (Long)((LongObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(value));
- }
+ break;
+ case SHORT:
+ Short shortValue = (Short) ((ShortObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (shortValue != null) {
+ exec.setVariable(name, new Var(shortValue.longValue()));
}
- else {
- exec.setVariableToNull(name);
+ break;
+ case INT:
+ Integer intValue = (Integer) ((IntObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (intValue != null) {
+ exec.setVariable(name, new Var(intValue.longValue()));
+ }
+ break;
+ case LONG:
+ Long longValue = (Long) ((LongObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (longValue != null) {
+ exec.setVariable(name, new Var(longValue));
+ }
+ break;
+ case FLOAT:
+ Float floatValue = (Float) ((FloatObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (floatValue != null) {
+ exec.setVariable(name, new Var(floatValue.doubleValue()));
+ }
+ break;
+ case DOUBLE:
+ Double doubleValue = (Double) ((DoubleObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (doubleValue != null) {
+ exec.setVariable(name, new Var(doubleValue));
+ }
+ break;
+ case STRING:
+ String strValue = ((StringObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (strValue != null) {
+ exec.setVariable(name, strValue);
Review Comment:
Fixed
--
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] sonarcloud[bot] commented on pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on PR #3808:
URL: https://github.com/apache/hive/pull/3808#issuecomment-1328317156
Kudos, SonarCloud Quality Gate passed! [](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=3808)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL) [1 Code Smell](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=3808&metric=coverage&view=list) No Coverage information
[](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=3808&metric=duplicated_lines_density&view=list) No Duplication information
--
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] zeroflag commented on pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
zeroflag commented on PR #3808:
URL: https://github.com/apache/hive/pull/3808#issuecomment-1328737540
LGTM
--
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] mdayakar commented on a diff in pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
mdayakar commented on code in PR #3808:
URL: https://github.com/apache/hive/pull/3808#discussion_r1033332946
##########
hplsql/src/main/java/org/apache/hive/hplsql/udf/Udf.java:
##########
@@ -128,27 +127,96 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
void setParameters(DeferredObject[] arguments) throws HiveException {
for (int i = 1; i < arguments.length; i++) {
String name = ":" + i;
- if (argumentsOI[i] instanceof StringObjectInspector) {
- String value = ((StringObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, value);
- }
+ Object inputObject = arguments[i].get();
+ ObjectInspector objectInspector = argumentsOI[i];
+ if (objectInspector.getCategory() == ObjectInspector.Category.PRIMITIVE) {
+ setParameterForPrimitiveTypeArgument(name, inputObject, objectInspector);
+ } else {
+ exec.setVariableToNull(name);
}
- else if (argumentsOI[i] instanceof IntObjectInspector) {
- Integer value = (Integer)((IntObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(new Long(value)));
- }
+ }
+ }
+
+ private void setParameterForPrimitiveTypeArgument(String name, Object inputObject, ObjectInspector objectInspector) {
+ PrimitiveObjectInspector.PrimitiveCategory primitiveCategory =
+ ((PrimitiveObjectInspector) objectInspector).getPrimitiveCategory();
+ switch (primitiveCategory) {
+ case BOOLEAN:
+ Boolean booleanValue = (Boolean) ((BooleanObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (booleanValue != null) {
+ exec.setVariable(name, new Var(booleanValue));
}
- else if (argumentsOI[i] instanceof LongObjectInspector) {
- Long value = (Long)((LongObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(value));
- }
+ break;
+ case SHORT:
+ Short shortValue = (Short) ((ShortObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (shortValue != null) {
+ exec.setVariable(name, new Var(shortValue.longValue()));
}
- else {
- exec.setVariableToNull(name);
+ break;
+ case INT:
+ Integer intValue = (Integer) ((IntObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (intValue != null) {
+ exec.setVariable(name, new Var(intValue.longValue()));
+ }
+ break;
+ case LONG:
+ Long longValue = (Long) ((LongObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (longValue != null) {
+ exec.setVariable(name, new Var(longValue));
+ }
+ break;
+ case FLOAT:
+ Float floatValue = (Float) ((FloatObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (floatValue != null) {
+ exec.setVariable(name, new Var(floatValue.doubleValue()));
+ }
+ break;
+ case DOUBLE:
+ Double doubleValue = (Double) ((DoubleObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (doubleValue != null) {
+ exec.setVariable(name, new Var(doubleValue));
+ }
+ break;
+ case STRING:
+ String strValue = ((StringObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (strValue != null) {
+ exec.setVariable(name, strValue);
+ }
+ break;
+ case DATE:
+ Date dateValue = ((DateObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (dateValue != null) {
+ exec.setVariable(name, new Var(java.sql.Date.valueOf(dateValue.toString())));
+ }
+ break;
+ case TIMESTAMP:
+ Timestamp timestampValue = ((TimestampObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (timestampValue != null) {
+ java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(timestampValue.toString());
+ timestamp.setNanos(timestampValue.getNanos());
+ exec.setVariable(name, new Var(timestamp, 0));
+ }
+ break;
+ case DECIMAL:
+ HiveDecimal decimalValue = ((HiveDecimalObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (decimalValue != null) {
+ exec.setVariable(name, new Var(decimalValue.bigDecimalValue()));
+ }
+ break;
+ case VARCHAR:
+ HiveVarchar varcharValue = ((HiveVarcharObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (varcharValue != null) {
+ exec.setVariable(name, varcharValue.getValue());
+ }
+ break;
+ case CHAR:
+ HiveChar charValue = ((HiveCharObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (charValue != null) {
+ exec.setVariable(name, charValue.getStrippedValue());
Review Comment:
Internally Exec code, Var object is getting created for string type input.
Refer https://github.com/apache/hive/blob/master/hplsql/src/main/java/org/apache/hive/hplsql/Exec.java#L210
--
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 #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
kasakrisz merged PR #3808:
URL: https://github.com/apache/hive/pull/3808
--
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] mdayakar commented on a diff in pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
mdayakar commented on code in PR #3808:
URL: https://github.com/apache/hive/pull/3808#discussion_r1033332407
##########
hplsql/src/main/java/org/apache/hive/hplsql/udf/Udf.java:
##########
@@ -128,27 +127,96 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
void setParameters(DeferredObject[] arguments) throws HiveException {
for (int i = 1; i < arguments.length; i++) {
String name = ":" + i;
- if (argumentsOI[i] instanceof StringObjectInspector) {
- String value = ((StringObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, value);
- }
+ Object inputObject = arguments[i].get();
+ ObjectInspector objectInspector = argumentsOI[i];
+ if (objectInspector.getCategory() == ObjectInspector.Category.PRIMITIVE) {
+ setParameterForPrimitiveTypeArgument(name, inputObject, objectInspector);
+ } else {
+ exec.setVariableToNull(name);
}
- else if (argumentsOI[i] instanceof IntObjectInspector) {
- Integer value = (Integer)((IntObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(new Long(value)));
- }
+ }
+ }
+
+ private void setParameterForPrimitiveTypeArgument(String name, Object inputObject, ObjectInspector objectInspector) {
+ PrimitiveObjectInspector.PrimitiveCategory primitiveCategory =
+ ((PrimitiveObjectInspector) objectInspector).getPrimitiveCategory();
+ switch (primitiveCategory) {
+ case BOOLEAN:
+ Boolean booleanValue = (Boolean) ((BooleanObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (booleanValue != null) {
+ exec.setVariable(name, new Var(booleanValue));
}
- else if (argumentsOI[i] instanceof LongObjectInspector) {
- Long value = (Long)((LongObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(value));
- }
+ break;
+ case SHORT:
+ Short shortValue = (Short) ((ShortObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (shortValue != null) {
+ exec.setVariable(name, new Var(shortValue.longValue()));
}
- else {
- exec.setVariableToNull(name);
+ break;
+ case INT:
+ Integer intValue = (Integer) ((IntObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (intValue != null) {
+ exec.setVariable(name, new Var(intValue.longValue()));
+ }
+ break;
+ case LONG:
+ Long longValue = (Long) ((LongObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (longValue != null) {
+ exec.setVariable(name, new Var(longValue));
+ }
+ break;
+ case FLOAT:
+ Float floatValue = (Float) ((FloatObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (floatValue != null) {
+ exec.setVariable(name, new Var(floatValue.doubleValue()));
+ }
+ break;
+ case DOUBLE:
+ Double doubleValue = (Double) ((DoubleObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (doubleValue != null) {
+ exec.setVariable(name, new Var(doubleValue));
+ }
+ break;
+ case STRING:
+ String strValue = ((StringObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (strValue != null) {
+ exec.setVariable(name, strValue);
+ }
+ break;
+ case DATE:
+ Date dateValue = ((DateObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (dateValue != null) {
+ exec.setVariable(name, new Var(java.sql.Date.valueOf(dateValue.toString())));
+ }
+ break;
+ case TIMESTAMP:
+ Timestamp timestampValue = ((TimestampObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (timestampValue != null) {
+ java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(timestampValue.toString());
+ timestamp.setNanos(timestampValue.getNanos());
+ exec.setVariable(name, new Var(timestamp, 0));
+ }
+ break;
+ case DECIMAL:
+ HiveDecimal decimalValue = ((HiveDecimalObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (decimalValue != null) {
+ exec.setVariable(name, new Var(decimalValue.bigDecimalValue()));
+ }
+ break;
+ case VARCHAR:
+ HiveVarchar varcharValue = ((HiveVarcharObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (varcharValue != null) {
+ exec.setVariable(name, varcharValue.getValue());
Review Comment:
Internally Exec code Var object is getting created for string type input.
Refer https://github.com/apache/hive/blob/master/hplsql/src/main/java/org/apache/hive/hplsql/Exec.java#L210
--
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] mdayakar commented on a diff in pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
mdayakar commented on code in PR #3808:
URL: https://github.com/apache/hive/pull/3808#discussion_r1033436616
##########
hplsql/src/main/java/org/apache/hive/hplsql/udf/Udf.java:
##########
@@ -128,27 +127,96 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
void setParameters(DeferredObject[] arguments) throws HiveException {
for (int i = 1; i < arguments.length; i++) {
String name = ":" + i;
- if (argumentsOI[i] instanceof StringObjectInspector) {
- String value = ((StringObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, value);
- }
+ Object inputObject = arguments[i].get();
+ ObjectInspector objectInspector = argumentsOI[i];
+ if (objectInspector.getCategory() == ObjectInspector.Category.PRIMITIVE) {
+ setParameterForPrimitiveTypeArgument(name, inputObject, objectInspector);
+ } else {
+ exec.setVariableToNull(name);
}
- else if (argumentsOI[i] instanceof IntObjectInspector) {
- Integer value = (Integer)((IntObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(new Long(value)));
- }
+ }
+ }
+
+ private void setParameterForPrimitiveTypeArgument(String name, Object inputObject, ObjectInspector objectInspector) {
+ PrimitiveObjectInspector.PrimitiveCategory primitiveCategory =
+ ((PrimitiveObjectInspector) objectInspector).getPrimitiveCategory();
+ switch (primitiveCategory) {
+ case BOOLEAN:
+ Boolean booleanValue = (Boolean) ((BooleanObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (booleanValue != null) {
+ exec.setVariable(name, new Var(booleanValue));
}
- else if (argumentsOI[i] instanceof LongObjectInspector) {
- Long value = (Long)((LongObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(value));
- }
+ break;
+ case SHORT:
+ Short shortValue = (Short) ((ShortObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (shortValue != null) {
+ exec.setVariable(name, new Var(shortValue.longValue()));
}
- else {
- exec.setVariableToNull(name);
+ break;
+ case INT:
+ Integer intValue = (Integer) ((IntObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (intValue != null) {
+ exec.setVariable(name, new Var(intValue.longValue()));
+ }
+ break;
+ case LONG:
+ Long longValue = (Long) ((LongObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (longValue != null) {
+ exec.setVariable(name, new Var(longValue));
+ }
+ break;
+ case FLOAT:
+ Float floatValue = (Float) ((FloatObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (floatValue != null) {
+ exec.setVariable(name, new Var(floatValue.doubleValue()));
+ }
+ break;
+ case DOUBLE:
+ Double doubleValue = (Double) ((DoubleObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (doubleValue != null) {
+ exec.setVariable(name, new Var(doubleValue));
+ }
+ break;
+ case STRING:
+ String strValue = ((StringObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (strValue != null) {
+ exec.setVariable(name, strValue);
+ }
+ break;
+ case DATE:
+ Date dateValue = ((DateObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (dateValue != null) {
+ exec.setVariable(name, new Var(java.sql.Date.valueOf(dateValue.toString())));
+ }
+ break;
+ case TIMESTAMP:
+ Timestamp timestampValue = ((TimestampObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (timestampValue != null) {
+ java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(timestampValue.toString());
+ timestamp.setNanos(timestampValue.getNanos());
+ exec.setVariable(name, new Var(timestamp, 0));
+ }
+ break;
+ case DECIMAL:
+ HiveDecimal decimalValue = ((HiveDecimalObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (decimalValue != null) {
+ exec.setVariable(name, new Var(decimalValue.bigDecimalValue()));
+ }
+ break;
+ case VARCHAR:
+ HiveVarchar varcharValue = ((HiveVarcharObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (varcharValue != null) {
+ exec.setVariable(name, varcharValue.getValue());
+ }
+ break;
+ case CHAR:
+ HiveChar charValue = ((HiveCharObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (charValue != null) {
+ exec.setVariable(name, charValue.getStrippedValue());
Review Comment:
Fixed
##########
itests/hive-unit/src/test/java/org/apache/hive/beeline/TestHplSqlViaBeeLine.java:
##########
@@ -171,17 +171,197 @@ public void testPackage() throws Throwable {
"SELECT * FROM result;\n";
testScriptFile(SCRIPT_TEXT, args(), "12345");
}
-
+
@Test
- public void testUdf() throws Throwable {
+ public void testUdfBoolean() throws Throwable {
String SCRIPT_TEXT =
- "DROP TABLE IF EXISTS result;\n" +
- "CREATE TABLE result (s string);\n" +
- "INSERT INTO result VALUES('alice');\n" +
- "INSERT INTO result VALUES('bob');\n" +
- "CREATE FUNCTION hello(p STRING) RETURNS STRING BEGIN RETURN 'hello ' || p; END;\n" +
- "SELECT hello(s) FROM result;\n";
- testScriptFile(SCRIPT_TEXT, args(), "hello alice.*hello bob");
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_b boolean);\n" +
+ "INSERT INTO result VALUES(true);\n" +
+ "INSERT INTO result VALUES(false);\n" +
+ "CREATE FUNCTION check(b boolean)\n" +
+ " RETURNS STRING\n" +
+ "BEGIN\n" +
+ " RETURN 'This is ' || b;\n" +
+ "END;\n" +
+ "SELECT check(col_b) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "This is true.*This is false");
+ }
+
+ @Test
+ public void testUdfSmallInt() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_s smallint);\n" +
+ "INSERT INTO result VALUES(123);\n" +
+ "INSERT INTO result VALUES(321);\n" +
+ "CREATE FUNCTION dbl(s smallint)\n" +
+ " RETURNS smallint\n" +
+ "BEGIN\n" +
+ " RETURN s + s;\n" +
+ "END;\n" +
+ "SELECT dbl(col_s) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "246.*642");
+ }
+
+ @Test
+ public void testUdfInt() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_i int);\n" +
+ "INSERT INTO result VALUES(12345);\n" +
+ "INSERT INTO result VALUES(54321);\n" +
+ "CREATE FUNCTION dbl(i int)\n" +
+ " RETURNS int\n" +
+ "BEGIN\n" +
+ " RETURN i * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_i) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "24690.*108642");
+ }
+
+ @Test
+ public void testUdfBigInt() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_b bigint);\n" +
+ "INSERT INTO result VALUES(123456789);\n" +
+ "INSERT INTO result VALUES(987654321);\n" +
+ "CREATE FUNCTION dbl(b bigint)\n" +
+ " RETURNS int8\n" +
+ "BEGIN\n" +
+ " RETURN b * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_b) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "246913578.*1975308642");
+ }
+
+ @Test
+ public void testUdfFloat() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_f float);\n" +
+ "INSERT INTO result VALUES(12345.6789);\n" +
+ "INSERT INTO result VALUES(98765.4321);\n" +
+ "CREATE FUNCTION dbl(f float)\n" +
+ " RETURNS float\n" +
+ "BEGIN\n" +
+ " RETURN f * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_f) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "24691.357421875.*197530.859375");
+ }
+
+ @Test
+ public void testUdfDouble() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_d double);\n" +
+ "INSERT INTO result VALUES(123456789.12);\n" +
+ "INSERT INTO result VALUES(987654321.98);\n" +
+ "CREATE FUNCTION dbl(d float)\n" +
+ " RETURNS double\n" +
+ "BEGIN\n" +
+ " RETURN d * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_d) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "2.4691357824E8.*1.97530864396E9");
+ }
+
+ @Test
+ public void testUdfString() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_s string);\n" +
+ "INSERT INTO result VALUES('Alice');\n" +
+ "INSERT INTO result VALUES('Smith');\n" +
+ "CREATE FUNCTION hello(s string)\n" +
+ " RETURNS string\n" +
+ "BEGIN\n" +
+ " RETURN 'Hello ' || s || '!';\n" +
+ "END;\n" +
+ "SELECT hello(col_s) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "Hello Alice!.*Hello Smith!");
+ }
+
+ @Test
+ public void testUdfDate() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_d date);\n" +
+ "INSERT INTO result VALUES('2022-11-24');\n" +
+ "INSERT INTO result VALUES('2022-12-25');\n" +
+ "CREATE FUNCTION date_today(d date)\n" +
+ " RETURNS date\n" +
+ "BEGIN\n" +
+ " RETURN d;\n" +
+ "END;\n" +
+ "SELECT date_today(col_d) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "2022-11-24.*2022-12-25");
+ }
+
+ @Test
+ public void testUdfTimestamp() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_t timestamp);\n" +
+ "INSERT INTO result VALUES('2022-11-24 10:20:30');\n" +
+ "INSERT INTO result VALUES('2022-12-25 06:30:30');\n" +
+ "CREATE FUNCTION time_today(t timestamp)\n" +
+ " RETURNS timestamp\n" +
+ "BEGIN\n" +
+ " RETURN t;\n" +
+ "END;\n" +
+ "SELECT time_today(col_t) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "2022-11-24 10:20:30.*2022-12-25 06:30:30");
+ }
+
+ @Test
+ public void testUdfDecimal() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_d decimal(15,2));\n" +
+ "INSERT INTO result VALUES(123456789.98);\n" +
+ "INSERT INTO result VALUES(987654321.12);\n" +
+ "CREATE FUNCTION triple(d decimal(15,2))\n" +
+ " RETURNS decimal(15,2)\n" +
+ "BEGIN\n" +
+ " RETURN d * 3;\n" +
+ "END;\n" +
+ "SELECT triple(col_d) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "370370369.94.*2962962963.36");
+ }
+
+ @Test
+ public void testUdfVarchar() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_v varchar(20));\n" +
+ "INSERT INTO result VALUES('Smith');\n" +
+ "INSERT INTO result VALUES('Sachin');\n" +
+ "CREATE FUNCTION hello(v varchar(20))\n" +
+ " RETURNS varchar(20)\n" +
+ "BEGIN\n" +
+ " RETURN 'Hello ' || v || '!';\n" +
+ "END;\n" +
+ "SELECT hello(col_v) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "Hello Smith!.*Hello Sachin!");
+ }
+
+ @Test
+ public void testUdfChar() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_c char(10));\n" +
+ "INSERT INTO result VALUES('Daya');\n" +
+ "INSERT INTO result VALUES('Alice');\n" +
+ "CREATE FUNCTION hello(c char(10))\n" +
+ " RETURNS char(10)\n" +
+ "BEGIN\n" +
+ " RETURN 'Hello ' || c || '!';\n" +
+ "END;\n" +
+ "SELECT hello(col_c) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "Hello Daya!.*Hello Alice!");
}
Review Comment:
Fixed
--
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] mdayakar commented on a diff in pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
mdayakar commented on code in PR #3808:
URL: https://github.com/apache/hive/pull/3808#discussion_r1033336424
##########
hplsql/src/main/java/org/apache/hive/hplsql/udf/Udf.java:
##########
@@ -128,27 +127,96 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
void setParameters(DeferredObject[] arguments) throws HiveException {
for (int i = 1; i < arguments.length; i++) {
String name = ":" + i;
- if (argumentsOI[i] instanceof StringObjectInspector) {
- String value = ((StringObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, value);
- }
+ Object inputObject = arguments[i].get();
+ ObjectInspector objectInspector = argumentsOI[i];
+ if (objectInspector.getCategory() == ObjectInspector.Category.PRIMITIVE) {
+ setParameterForPrimitiveTypeArgument(name, inputObject, objectInspector);
+ } else {
+ exec.setVariableToNull(name);
}
- else if (argumentsOI[i] instanceof IntObjectInspector) {
- Integer value = (Integer)((IntObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(new Long(value)));
- }
+ }
+ }
+
+ private void setParameterForPrimitiveTypeArgument(String name, Object inputObject, ObjectInspector objectInspector) {
+ PrimitiveObjectInspector.PrimitiveCategory primitiveCategory =
+ ((PrimitiveObjectInspector) objectInspector).getPrimitiveCategory();
+ switch (primitiveCategory) {
+ case BOOLEAN:
+ Boolean booleanValue = (Boolean) ((BooleanObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (booleanValue != null) {
+ exec.setVariable(name, new Var(booleanValue));
}
- else if (argumentsOI[i] instanceof LongObjectInspector) {
- Long value = (Long)((LongObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(value));
- }
+ break;
+ case SHORT:
+ Short shortValue = (Short) ((ShortObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (shortValue != null) {
+ exec.setVariable(name, new Var(shortValue.longValue()));
}
- else {
- exec.setVariableToNull(name);
+ break;
+ case INT:
+ Integer intValue = (Integer) ((IntObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (intValue != null) {
+ exec.setVariable(name, new Var(intValue.longValue()));
+ }
+ break;
+ case LONG:
+ Long longValue = (Long) ((LongObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (longValue != null) {
+ exec.setVariable(name, new Var(longValue));
+ }
+ break;
+ case FLOAT:
+ Float floatValue = (Float) ((FloatObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (floatValue != null) {
+ exec.setVariable(name, new Var(floatValue.doubleValue()));
+ }
+ break;
+ case DOUBLE:
+ Double doubleValue = (Double) ((DoubleObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (doubleValue != null) {
+ exec.setVariable(name, new Var(doubleValue));
+ }
+ break;
+ case STRING:
+ String strValue = ((StringObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (strValue != null) {
+ exec.setVariable(name, strValue);
Review Comment:
Internally Exec code, Var object is getting created for string type input.
Refer https://github.com/apache/hive/blob/master/hplsql/src/main/java/org/apache/hive/hplsql/Exec.java#L210
--
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] sonarcloud[bot] commented on pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on PR #3808:
URL: https://github.com/apache/hive/pull/3808#issuecomment-1329386650
Kudos, SonarCloud Quality Gate passed! [](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=3808)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL) [1 Code Smell](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=3808&metric=coverage&view=list) No Coverage information
[](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=3808&metric=duplicated_lines_density&view=list) No Duplication information
--
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] sonarcloud[bot] commented on pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on PR #3808:
URL: https://github.com/apache/hive/pull/3808#issuecomment-1329032944
Kudos, SonarCloud Quality Gate passed! [](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=3808)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL) [1 Code Smell](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=3808&metric=coverage&view=list) No Coverage information
[](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=3808&metric=duplicated_lines_density&view=list) No Duplication information
--
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] sonarcloud[bot] commented on pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on PR #3808:
URL: https://github.com/apache/hive/pull/3808#issuecomment-1328099903
Kudos, SonarCloud Quality Gate passed! [](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=3808)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=BUG)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=VULNERABILITY)
[](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT) [](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=3808&resolved=false&types=SECURITY_HOTSPOT)
[](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL) [](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL) [9 Code Smells](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=3808&resolved=false&types=CODE_SMELL)
[](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=3808&metric=coverage&view=list) No Coverage information
[](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=3808&metric=duplicated_lines_density&view=list) No Duplication information
--
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 #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
kasakrisz commented on code in PR #3808:
URL: https://github.com/apache/hive/pull/3808#discussion_r1033316738
##########
hplsql/src/main/java/org/apache/hive/hplsql/udf/Udf.java:
##########
@@ -128,27 +127,96 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
void setParameters(DeferredObject[] arguments) throws HiveException {
for (int i = 1; i < arguments.length; i++) {
String name = ":" + i;
- if (argumentsOI[i] instanceof StringObjectInspector) {
- String value = ((StringObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, value);
- }
+ Object inputObject = arguments[i].get();
+ ObjectInspector objectInspector = argumentsOI[i];
+ if (objectInspector.getCategory() == ObjectInspector.Category.PRIMITIVE) {
+ setParameterForPrimitiveTypeArgument(name, inputObject, objectInspector);
+ } else {
+ exec.setVariableToNull(name);
}
- else if (argumentsOI[i] instanceof IntObjectInspector) {
- Integer value = (Integer)((IntObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(new Long(value)));
- }
+ }
+ }
+
+ private void setParameterForPrimitiveTypeArgument(String name, Object inputObject, ObjectInspector objectInspector) {
+ PrimitiveObjectInspector.PrimitiveCategory primitiveCategory =
+ ((PrimitiveObjectInspector) objectInspector).getPrimitiveCategory();
+ switch (primitiveCategory) {
+ case BOOLEAN:
+ Boolean booleanValue = (Boolean) ((BooleanObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (booleanValue != null) {
+ exec.setVariable(name, new Var(booleanValue));
}
- else if (argumentsOI[i] instanceof LongObjectInspector) {
- Long value = (Long)((LongObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(value));
- }
+ break;
+ case SHORT:
+ Short shortValue = (Short) ((ShortObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (shortValue != null) {
+ exec.setVariable(name, new Var(shortValue.longValue()));
}
- else {
- exec.setVariableToNull(name);
+ break;
+ case INT:
+ Integer intValue = (Integer) ((IntObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (intValue != null) {
+ exec.setVariable(name, new Var(intValue.longValue()));
+ }
+ break;
+ case LONG:
+ Long longValue = (Long) ((LongObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (longValue != null) {
+ exec.setVariable(name, new Var(longValue));
+ }
+ break;
+ case FLOAT:
+ Float floatValue = (Float) ((FloatObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (floatValue != null) {
+ exec.setVariable(name, new Var(floatValue.doubleValue()));
+ }
+ break;
+ case DOUBLE:
+ Double doubleValue = (Double) ((DoubleObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (doubleValue != null) {
+ exec.setVariable(name, new Var(doubleValue));
+ }
+ break;
+ case STRING:
+ String strValue = ((StringObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (strValue != null) {
+ exec.setVariable(name, strValue);
+ }
+ break;
+ case DATE:
+ Date dateValue = ((DateObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (dateValue != null) {
+ exec.setVariable(name, new Var(java.sql.Date.valueOf(dateValue.toString())));
+ }
+ break;
+ case TIMESTAMP:
+ Timestamp timestampValue = ((TimestampObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (timestampValue != null) {
+ java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(timestampValue.toString());
+ timestamp.setNanos(timestampValue.getNanos());
+ exec.setVariable(name, new Var(timestamp, 0));
+ }
+ break;
+ case DECIMAL:
+ HiveDecimal decimalValue = ((HiveDecimalObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (decimalValue != null) {
+ exec.setVariable(name, new Var(decimalValue.bigDecimalValue()));
+ }
+ break;
+ case VARCHAR:
+ HiveVarchar varcharValue = ((HiveVarcharObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (varcharValue != null) {
+ exec.setVariable(name, varcharValue.getValue());
Review Comment:
Should the valued be wrapped in a Var instance?
##########
hplsql/src/main/java/org/apache/hive/hplsql/udf/Udf.java:
##########
@@ -128,27 +127,96 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
void setParameters(DeferredObject[] arguments) throws HiveException {
for (int i = 1; i < arguments.length; i++) {
String name = ":" + i;
- if (argumentsOI[i] instanceof StringObjectInspector) {
- String value = ((StringObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, value);
- }
+ Object inputObject = arguments[i].get();
+ ObjectInspector objectInspector = argumentsOI[i];
+ if (objectInspector.getCategory() == ObjectInspector.Category.PRIMITIVE) {
+ setParameterForPrimitiveTypeArgument(name, inputObject, objectInspector);
+ } else {
+ exec.setVariableToNull(name);
}
- else if (argumentsOI[i] instanceof IntObjectInspector) {
- Integer value = (Integer)((IntObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(new Long(value)));
- }
+ }
+ }
+
+ private void setParameterForPrimitiveTypeArgument(String name, Object inputObject, ObjectInspector objectInspector) {
+ PrimitiveObjectInspector.PrimitiveCategory primitiveCategory =
+ ((PrimitiveObjectInspector) objectInspector).getPrimitiveCategory();
+ switch (primitiveCategory) {
+ case BOOLEAN:
+ Boolean booleanValue = (Boolean) ((BooleanObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (booleanValue != null) {
+ exec.setVariable(name, new Var(booleanValue));
}
- else if (argumentsOI[i] instanceof LongObjectInspector) {
- Long value = (Long)((LongObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(value));
- }
+ break;
+ case SHORT:
+ Short shortValue = (Short) ((ShortObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (shortValue != null) {
+ exec.setVariable(name, new Var(shortValue.longValue()));
}
- else {
- exec.setVariableToNull(name);
+ break;
+ case INT:
+ Integer intValue = (Integer) ((IntObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (intValue != null) {
+ exec.setVariable(name, new Var(intValue.longValue()));
+ }
+ break;
+ case LONG:
+ Long longValue = (Long) ((LongObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (longValue != null) {
+ exec.setVariable(name, new Var(longValue));
+ }
+ break;
+ case FLOAT:
+ Float floatValue = (Float) ((FloatObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (floatValue != null) {
+ exec.setVariable(name, new Var(floatValue.doubleValue()));
+ }
+ break;
+ case DOUBLE:
+ Double doubleValue = (Double) ((DoubleObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (doubleValue != null) {
+ exec.setVariable(name, new Var(doubleValue));
+ }
+ break;
+ case STRING:
+ String strValue = ((StringObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (strValue != null) {
+ exec.setVariable(name, strValue);
+ }
+ break;
+ case DATE:
+ Date dateValue = ((DateObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (dateValue != null) {
+ exec.setVariable(name, new Var(java.sql.Date.valueOf(dateValue.toString())));
+ }
+ break;
+ case TIMESTAMP:
+ Timestamp timestampValue = ((TimestampObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (timestampValue != null) {
+ java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(timestampValue.toString());
+ timestamp.setNanos(timestampValue.getNanos());
+ exec.setVariable(name, new Var(timestamp, 0));
+ }
+ break;
+ case DECIMAL:
+ HiveDecimal decimalValue = ((HiveDecimalObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (decimalValue != null) {
+ exec.setVariable(name, new Var(decimalValue.bigDecimalValue()));
+ }
+ break;
+ case VARCHAR:
+ HiveVarchar varcharValue = ((HiveVarcharObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (varcharValue != null) {
+ exec.setVariable(name, varcharValue.getValue());
+ }
+ break;
+ case CHAR:
+ HiveChar charValue = ((HiveCharObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (charValue != null) {
+ exec.setVariable(name, charValue.getStrippedValue());
Review Comment:
Should the valued be wrapped in a Var instance?
##########
itests/hive-unit/src/test/java/org/apache/hive/beeline/TestHplSqlViaBeeLine.java:
##########
@@ -171,17 +171,197 @@ public void testPackage() throws Throwable {
"SELECT * FROM result;\n";
testScriptFile(SCRIPT_TEXT, args(), "12345");
}
-
+
@Test
- public void testUdf() throws Throwable {
+ public void testUdfBoolean() throws Throwable {
String SCRIPT_TEXT =
- "DROP TABLE IF EXISTS result;\n" +
- "CREATE TABLE result (s string);\n" +
- "INSERT INTO result VALUES('alice');\n" +
- "INSERT INTO result VALUES('bob');\n" +
- "CREATE FUNCTION hello(p STRING) RETURNS STRING BEGIN RETURN 'hello ' || p; END;\n" +
- "SELECT hello(s) FROM result;\n";
- testScriptFile(SCRIPT_TEXT, args(), "hello alice.*hello bob");
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_b boolean);\n" +
+ "INSERT INTO result VALUES(true);\n" +
+ "INSERT INTO result VALUES(false);\n" +
+ "CREATE FUNCTION check(b boolean)\n" +
+ " RETURNS STRING\n" +
+ "BEGIN\n" +
+ " RETURN 'This is ' || b;\n" +
+ "END;\n" +
+ "SELECT check(col_b) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "This is true.*This is false");
+ }
+
+ @Test
+ public void testUdfSmallInt() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_s smallint);\n" +
+ "INSERT INTO result VALUES(123);\n" +
+ "INSERT INTO result VALUES(321);\n" +
+ "CREATE FUNCTION dbl(s smallint)\n" +
+ " RETURNS smallint\n" +
+ "BEGIN\n" +
+ " RETURN s + s;\n" +
+ "END;\n" +
+ "SELECT dbl(col_s) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "246.*642");
+ }
+
+ @Test
+ public void testUdfInt() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_i int);\n" +
+ "INSERT INTO result VALUES(12345);\n" +
+ "INSERT INTO result VALUES(54321);\n" +
+ "CREATE FUNCTION dbl(i int)\n" +
+ " RETURNS int\n" +
+ "BEGIN\n" +
+ " RETURN i * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_i) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "24690.*108642");
+ }
+
+ @Test
+ public void testUdfBigInt() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_b bigint);\n" +
+ "INSERT INTO result VALUES(123456789);\n" +
+ "INSERT INTO result VALUES(987654321);\n" +
+ "CREATE FUNCTION dbl(b bigint)\n" +
+ " RETURNS int8\n" +
+ "BEGIN\n" +
+ " RETURN b * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_b) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "246913578.*1975308642");
+ }
+
+ @Test
+ public void testUdfFloat() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_f float);\n" +
+ "INSERT INTO result VALUES(12345.6789);\n" +
+ "INSERT INTO result VALUES(98765.4321);\n" +
+ "CREATE FUNCTION dbl(f float)\n" +
+ " RETURNS float\n" +
+ "BEGIN\n" +
+ " RETURN f * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_f) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "24691.357421875.*197530.859375");
+ }
+
+ @Test
+ public void testUdfDouble() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_d double);\n" +
+ "INSERT INTO result VALUES(123456789.12);\n" +
+ "INSERT INTO result VALUES(987654321.98);\n" +
+ "CREATE FUNCTION dbl(d float)\n" +
+ " RETURNS double\n" +
+ "BEGIN\n" +
+ " RETURN d * 2;\n" +
+ "END;\n" +
+ "SELECT dbl(col_d) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "2.4691357824E8.*1.97530864396E9");
+ }
+
+ @Test
+ public void testUdfString() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_s string);\n" +
+ "INSERT INTO result VALUES('Alice');\n" +
+ "INSERT INTO result VALUES('Smith');\n" +
+ "CREATE FUNCTION hello(s string)\n" +
+ " RETURNS string\n" +
+ "BEGIN\n" +
+ " RETURN 'Hello ' || s || '!';\n" +
+ "END;\n" +
+ "SELECT hello(col_s) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "Hello Alice!.*Hello Smith!");
+ }
+
+ @Test
+ public void testUdfDate() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_d date);\n" +
+ "INSERT INTO result VALUES('2022-11-24');\n" +
+ "INSERT INTO result VALUES('2022-12-25');\n" +
+ "CREATE FUNCTION date_today(d date)\n" +
+ " RETURNS date\n" +
+ "BEGIN\n" +
+ " RETURN d;\n" +
+ "END;\n" +
+ "SELECT date_today(col_d) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "2022-11-24.*2022-12-25");
+ }
+
+ @Test
+ public void testUdfTimestamp() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_t timestamp);\n" +
+ "INSERT INTO result VALUES('2022-11-24 10:20:30');\n" +
+ "INSERT INTO result VALUES('2022-12-25 06:30:30');\n" +
+ "CREATE FUNCTION time_today(t timestamp)\n" +
+ " RETURNS timestamp\n" +
+ "BEGIN\n" +
+ " RETURN t;\n" +
+ "END;\n" +
+ "SELECT time_today(col_t) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "2022-11-24 10:20:30.*2022-12-25 06:30:30");
+ }
+
+ @Test
+ public void testUdfDecimal() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_d decimal(15,2));\n" +
+ "INSERT INTO result VALUES(123456789.98);\n" +
+ "INSERT INTO result VALUES(987654321.12);\n" +
+ "CREATE FUNCTION triple(d decimal(15,2))\n" +
+ " RETURNS decimal(15,2)\n" +
+ "BEGIN\n" +
+ " RETURN d * 3;\n" +
+ "END;\n" +
+ "SELECT triple(col_d) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "370370369.94.*2962962963.36");
+ }
+
+ @Test
+ public void testUdfVarchar() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_v varchar(20));\n" +
+ "INSERT INTO result VALUES('Smith');\n" +
+ "INSERT INTO result VALUES('Sachin');\n" +
+ "CREATE FUNCTION hello(v varchar(20))\n" +
+ " RETURNS varchar(20)\n" +
+ "BEGIN\n" +
+ " RETURN 'Hello ' || v || '!';\n" +
+ "END;\n" +
+ "SELECT hello(col_v) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "Hello Smith!.*Hello Sachin!");
+ }
+
+ @Test
+ public void testUdfChar() throws Throwable {
+ String SCRIPT_TEXT =
+ "DROP TABLE IF EXISTS result;\n" +
+ "CREATE TABLE result (col_c char(10));\n" +
+ "INSERT INTO result VALUES('Daya');\n" +
+ "INSERT INTO result VALUES('Alice');\n" +
+ "CREATE FUNCTION hello(c char(10))\n" +
+ " RETURNS char(10)\n" +
+ "BEGIN\n" +
+ " RETURN 'Hello ' || c || '!';\n" +
+ "END;\n" +
+ "SELECT hello(col_c) FROM result;\n";
+ testScriptFile(SCRIPT_TEXT, args(), "Hello Daya!.*Hello Alice!");
}
Review Comment:
What (should) happen when the udf formal parameter type and the actual parameter parameter type is different?
Could you please add tests to cover that?
Ex.:
```
CREATE TABLE result (col_c int);
...
CREATE FUNCTION hello(i smallint)
...
```
```
CREATE TABLE result (col_c date);
...
CREATE FUNCTION hello(i double)
...
```
##########
hplsql/src/main/java/org/apache/hive/hplsql/udf/Udf.java:
##########
@@ -128,27 +127,96 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
void setParameters(DeferredObject[] arguments) throws HiveException {
for (int i = 1; i < arguments.length; i++) {
String name = ":" + i;
- if (argumentsOI[i] instanceof StringObjectInspector) {
- String value = ((StringObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, value);
- }
+ Object inputObject = arguments[i].get();
+ ObjectInspector objectInspector = argumentsOI[i];
+ if (objectInspector.getCategory() == ObjectInspector.Category.PRIMITIVE) {
+ setParameterForPrimitiveTypeArgument(name, inputObject, objectInspector);
+ } else {
+ exec.setVariableToNull(name);
}
- else if (argumentsOI[i] instanceof IntObjectInspector) {
- Integer value = (Integer)((IntObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(new Long(value)));
- }
+ }
+ }
+
+ private void setParameterForPrimitiveTypeArgument(String name, Object inputObject, ObjectInspector objectInspector) {
+ PrimitiveObjectInspector.PrimitiveCategory primitiveCategory =
+ ((PrimitiveObjectInspector) objectInspector).getPrimitiveCategory();
+ switch (primitiveCategory) {
+ case BOOLEAN:
+ Boolean booleanValue = (Boolean) ((BooleanObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (booleanValue != null) {
+ exec.setVariable(name, new Var(booleanValue));
}
- else if (argumentsOI[i] instanceof LongObjectInspector) {
- Long value = (Long)((LongObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(value));
- }
+ break;
+ case SHORT:
+ Short shortValue = (Short) ((ShortObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (shortValue != null) {
+ exec.setVariable(name, new Var(shortValue.longValue()));
}
- else {
- exec.setVariableToNull(name);
+ break;
+ case INT:
+ Integer intValue = (Integer) ((IntObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (intValue != null) {
+ exec.setVariable(name, new Var(intValue.longValue()));
+ }
+ break;
+ case LONG:
+ Long longValue = (Long) ((LongObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (longValue != null) {
+ exec.setVariable(name, new Var(longValue));
+ }
+ break;
+ case FLOAT:
+ Float floatValue = (Float) ((FloatObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (floatValue != null) {
+ exec.setVariable(name, new Var(floatValue.doubleValue()));
+ }
+ break;
+ case DOUBLE:
+ Double doubleValue = (Double) ((DoubleObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (doubleValue != null) {
+ exec.setVariable(name, new Var(doubleValue));
+ }
+ break;
+ case STRING:
+ String strValue = ((StringObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (strValue != null) {
+ exec.setVariable(name, strValue);
Review Comment:
Should the valued be wrapped in a `Var` instance?
--
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] mdayakar commented on a diff in pull request #3808: HIVE-26768: HPLSQL UDF is not working if it is applied on a column of type varchar/char/decimal in a table.
Posted by GitBox <gi...@apache.org>.
mdayakar commented on code in PR #3808:
URL: https://github.com/apache/hive/pull/3808#discussion_r1033436345
##########
hplsql/src/main/java/org/apache/hive/hplsql/udf/Udf.java:
##########
@@ -128,27 +127,96 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
void setParameters(DeferredObject[] arguments) throws HiveException {
for (int i = 1; i < arguments.length; i++) {
String name = ":" + i;
- if (argumentsOI[i] instanceof StringObjectInspector) {
- String value = ((StringObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, value);
- }
+ Object inputObject = arguments[i].get();
+ ObjectInspector objectInspector = argumentsOI[i];
+ if (objectInspector.getCategory() == ObjectInspector.Category.PRIMITIVE) {
+ setParameterForPrimitiveTypeArgument(name, inputObject, objectInspector);
+ } else {
+ exec.setVariableToNull(name);
}
- else if (argumentsOI[i] instanceof IntObjectInspector) {
- Integer value = (Integer)((IntObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(new Long(value)));
- }
+ }
+ }
+
+ private void setParameterForPrimitiveTypeArgument(String name, Object inputObject, ObjectInspector objectInspector) {
+ PrimitiveObjectInspector.PrimitiveCategory primitiveCategory =
+ ((PrimitiveObjectInspector) objectInspector).getPrimitiveCategory();
+ switch (primitiveCategory) {
+ case BOOLEAN:
+ Boolean booleanValue = (Boolean) ((BooleanObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (booleanValue != null) {
+ exec.setVariable(name, new Var(booleanValue));
}
- else if (argumentsOI[i] instanceof LongObjectInspector) {
- Long value = (Long)((LongObjectInspector)argumentsOI[i]).getPrimitiveJavaObject(arguments[i].get());
- if (value != null) {
- exec.setVariable(name, new Var(value));
- }
+ break;
+ case SHORT:
+ Short shortValue = (Short) ((ShortObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (shortValue != null) {
+ exec.setVariable(name, new Var(shortValue.longValue()));
}
- else {
- exec.setVariableToNull(name);
+ break;
+ case INT:
+ Integer intValue = (Integer) ((IntObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (intValue != null) {
+ exec.setVariable(name, new Var(intValue.longValue()));
+ }
+ break;
+ case LONG:
+ Long longValue = (Long) ((LongObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (longValue != null) {
+ exec.setVariable(name, new Var(longValue));
+ }
+ break;
+ case FLOAT:
+ Float floatValue = (Float) ((FloatObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (floatValue != null) {
+ exec.setVariable(name, new Var(floatValue.doubleValue()));
+ }
+ break;
+ case DOUBLE:
+ Double doubleValue = (Double) ((DoubleObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (doubleValue != null) {
+ exec.setVariable(name, new Var(doubleValue));
+ }
+ break;
+ case STRING:
+ String strValue = ((StringObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (strValue != null) {
+ exec.setVariable(name, strValue);
+ }
+ break;
+ case DATE:
+ Date dateValue = ((DateObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (dateValue != null) {
+ exec.setVariable(name, new Var(java.sql.Date.valueOf(dateValue.toString())));
+ }
+ break;
+ case TIMESTAMP:
+ Timestamp timestampValue = ((TimestampObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (timestampValue != null) {
+ java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(timestampValue.toString());
+ timestamp.setNanos(timestampValue.getNanos());
+ exec.setVariable(name, new Var(timestamp, 0));
+ }
+ break;
+ case DECIMAL:
+ HiveDecimal decimalValue = ((HiveDecimalObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (decimalValue != null) {
+ exec.setVariable(name, new Var(decimalValue.bigDecimalValue()));
+ }
+ break;
+ case VARCHAR:
+ HiveVarchar varcharValue = ((HiveVarcharObjectInspector) objectInspector).getPrimitiveJavaObject(inputObject);
+ if (varcharValue != null) {
+ exec.setVariable(name, varcharValue.getValue());
Review Comment:
Fixed
--
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