This is an automated email from the ASF dual-hosted git repository. daijy pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/hive.git
The following commit(s) were added to refs/heads/master by this push: new 49fe5fc HIVE-21308: Negative forms of variables are not supported in HPL/SQL (Baoning He, reviewed by Daniel Dai) 49fe5fc is described below commit 49fe5fc50a97c9fbd08c74aa4ee6e7e30fe22ec8 Author: Daniel Dai <dai...@gmail.com> AuthorDate: Fri Feb 22 09:41:43 2019 -0800 HIVE-21308: Negative forms of variables are not supported in HPL/SQL (Baoning He, reviewed by Daniel Dai) --- .../main/antlr4/org/apache/hive/hplsql/Hplsql.g4 | 2 +- .../src/main/java/org/apache/hive/hplsql/Exec.java | 20 +++++++++++++++++--- .../src/main/java/org/apache/hive/hplsql/Var.java | 22 ++++++++++++++++++++-- hplsql/src/test/queries/local/declare2.sql | 9 +++++++++ hplsql/src/test/results/local/declare2.out.txt | 10 +++++++++- 5 files changed, 56 insertions(+), 7 deletions(-) diff --git a/hplsql/src/main/antlr4/org/apache/hive/hplsql/Hplsql.g4 b/hplsql/src/main/antlr4/org/apache/hive/hplsql/Hplsql.g4 index 77c2e2c..d25e8c5 100644 --- a/hplsql/src/main/antlr4/org/apache/hive/hplsql/Hplsql.g4 +++ b/hplsql/src/main/antlr4/org/apache/hive/hplsql/Hplsql.g4 @@ -1183,7 +1183,7 @@ timestamp_literal : // TIMESTAMP 'YYYY-MM-DD HH:MI:SS.FFF' ; ident : - (L_ID | non_reserved_words) ('.' (L_ID | non_reserved_words))* + '-'? (L_ID | non_reserved_words) ('.' (L_ID | non_reserved_words))* ; string : // String literal (single or double quoted) diff --git a/hplsql/src/main/java/org/apache/hive/hplsql/Exec.java b/hplsql/src/main/java/org/apache/hive/hplsql/Exec.java index 9e27ba1..47f5cef 100644 --- a/hplsql/src/main/java/org/apache/hive/hplsql/Exec.java +++ b/hplsql/src/main/java/org/apache/hive/hplsql/Exec.java @@ -2129,12 +2129,26 @@ public class Exec extends HplsqlBaseVisitor<Integer> { * Identifier */ @Override - public Integer visitIdent(HplsqlParser.IdentContext ctx) { + public Integer visitIdent(HplsqlParser.IdentContext ctx) { + boolean hasSub = false; String ident = ctx.getText(); - Var var = findVariable(ident); + String actualIdent = ident; + if (ident.startsWith("-")) { + hasSub = true; + actualIdent = ident.substring(1, ident.length()); + } + + Var var = findVariable(actualIdent); if (var != null) { if (!exec.buildSql) { - exec.stackPush(var); + if (hasSub) { + Var var1 = new Var(var); + var1.negate(); + exec.stackPush(var1); + } + else { + exec.stackPush(var); + } } else { exec.stackPush(new Var(ident, Var.Type.STRING, var.toSqlString())); diff --git a/hplsql/src/main/java/org/apache/hive/hplsql/Var.java b/hplsql/src/main/java/org/apache/hive/hplsql/Var.java index a117cb6..06b0e9b 100644 --- a/hplsql/src/main/java/org/apache/hive/hplsql/Var.java +++ b/hplsql/src/main/java/org/apache/hive/hplsql/Var.java @@ -552,13 +552,31 @@ public class Var { } /** - * Negate the boolean value + * Negate the value */ public void negate() { - if(type == Type.BOOL && value != null) { + if (value == null){ + return; + } + if (type == Type.BOOL) { boolean v = ((Boolean)value).booleanValue(); value = Boolean.valueOf(!v); } + else if (type == Type.DECIMAL) { + BigDecimal v = (BigDecimal)value; + value = v.negate(); + } + else if (type == Type.DOUBLE) { + Double v = (Double)value; + value = -v; + } + else if (type == Type.BIGINT) { + Long v = (Long)value; + value = -v; + } + else { + throw new NumberFormatException("invalid type " + type); + } } /** diff --git a/hplsql/src/test/queries/local/declare2.sql b/hplsql/src/test/queries/local/declare2.sql index 992d09e..9622033 100644 --- a/hplsql/src/test/queries/local/declare2.sql +++ b/hplsql/src/test/queries/local/declare2.sql @@ -11,3 +11,12 @@ declare begin null; end; + +declare + num1 int := 1; + num2 int := 2; +begin + print num1; + print -num1; + print -num1*2+num2; +end; \ No newline at end of file diff --git a/hplsql/src/test/results/local/declare2.out.txt b/hplsql/src/test/results/local/declare2.out.txt index e22ca78..6a0ac18 100644 --- a/hplsql/src/test/results/local/declare2.out.txt +++ b/hplsql/src/test/results/local/declare2.out.txt @@ -4,4 +4,12 @@ Ln:5 PRINT a Ln:6 PRINT 1 -Ln:10 DECLARE code char = 'a' \ No newline at end of file +Ln:10 DECLARE code char = 'a' +Ln:16 DECLARE num1 int = 1 +Ln:17 DECLARE num2 int = 2 +Ln:19 PRINT +1 +Ln:20 PRINT +-1 +Ln:21 PRINT +0 \ No newline at end of file