diff --git hplsql/src/main/java/org/apache/hive/hplsql/Expression.java hplsql/src/main/java/org/apache/hive/hplsql/Expression.java index c10f702..878d3ed 100644 --- hplsql/src/main/java/org/apache/hive/hplsql/Expression.java +++ hplsql/src/main/java/org/apache/hive/hplsql/Expression.java @@ -338,6 +338,12 @@ public void operatorAdd(HplsqlParser.ExprContext ctx) { else if (v1.type == Type.BIGINT && v2.type == Type.BIGINT) { exec.stackPush(new Var((Long)v1.value + (Long)v2.value)); } + else if (v1.type == Type.DOUBLE || v2.type == Type.DOUBLE) { + exec.stackPush(new Var(v1.doubleValue() + v2.doubleValue())); + } + else if (v1.type == Type.DECIMAL || v2.type == Type.DECIMAL) { + exec.stackPush(new Var(v1.decimalValue().add(v2.decimalValue()))); + } else if (v1.type == Type.BIGINT && v2.type == Type.DATE) { exec.stackPush(changeDateByInt((Date)v2.value, (Long)v1.value, true /*add*/)); } @@ -370,6 +376,12 @@ public void operatorSub(HplsqlParser.ExprContext ctx) { else if (v1.type == Type.BIGINT && v2.type == Type.BIGINT) { exec.stackPush(new Var((Long)v1.value - (Long)v2.value)); } + else if (v1.type == Type.DOUBLE || v2.type == Type.DOUBLE) { + exec.stackPush(new Var(v1.doubleValue() - v2.doubleValue())); + } + else if (v1.type == Type.DECIMAL || v2.type == Type.DECIMAL) { + exec.stackPush(new Var(v1.decimalValue().subtract(v2.decimalValue()))); + } else if (v1.type == Type.DATE && v2.type == Type.BIGINT) { exec.stackPush(changeDateByInt((Date)v1.value, (Long)v2.value, false /*subtract*/)); } diff --git hplsql/src/main/java/org/apache/hive/hplsql/Var.java hplsql/src/main/java/org/apache/hive/hplsql/Var.java index 480d97c..6033c2a 100644 --- hplsql/src/main/java/org/apache/hive/hplsql/Var.java +++ hplsql/src/main/java/org/apache/hive/hplsql/Var.java @@ -522,6 +522,9 @@ public BigDecimal decimalValue() { if (type == Type.DECIMAL) { return (BigDecimal)value; } + else if (type == Type.BIGINT) { + return BigDecimal.valueOf(((Long)value).longValue()); + } throw new NumberFormatException(); } diff --git hplsql/src/test/java/org/apache/hive/hplsql/TestHplsqlLocal.java hplsql/src/test/java/org/apache/hive/hplsql/TestHplsqlLocal.java index 8692661..2216981 100644 --- hplsql/src/test/java/org/apache/hive/hplsql/TestHplsqlLocal.java +++ hplsql/src/test/java/org/apache/hive/hplsql/TestHplsqlLocal.java @@ -37,6 +37,11 @@ public void testAdd() throws Exception { run("add"); } + + @Test + public void testArith() throws Exception { + run("arith"); + } @Test public void testAssign() throws Exception { diff --git hplsql/src/test/queries/local/arith.sql hplsql/src/test/queries/local/arith.sql new file mode 100644 index 0000000..b39ada7 --- /dev/null +++ hplsql/src/test/queries/local/arith.sql @@ -0,0 +1,44 @@ +declare i int default 10; +declare d decimal(4,2) default 1.1; +declare f float default 3.7; +declare db double default 7.1; + +print i + i; +print i + d; +print i + f; +print i + db; + +print i - i; +print i - d; +print i - f; +print i - db; + +print d + i; +print d + d; +print d + f; +print d + db; + +print d - i; +print d - d; +print d - f; +print d - db; + +print f + i; +print f + d; +print f + f; +print f + db; + +print f - i; +print f - d; +print f - f; +print f - db; + +print db + i; +print db + d; +print db + f; +print db + db; + +print db - i; +print db - d; +print db - f; +print db - db; diff --git hplsql/src/test/results/local/arith.out.txt hplsql/src/test/results/local/arith.out.txt new file mode 100644 index 0000000..63b9567 --- /dev/null +++ hplsql/src/test/results/local/arith.out.txt @@ -0,0 +1,68 @@ +Ln:1 DECLARE i int = 10 +Ln:2 DECLARE d decimal = 1.1 +Ln:3 DECLARE f float = 3.7 +Ln:4 DECLARE db double = 7.1 +Ln:6 PRINT +20 +Ln:7 PRINT +11.1 +Ln:8 PRINT +13.7 +Ln:9 PRINT +17.1 +Ln:11 PRINT +0 +Ln:12 PRINT +8.9 +Ln:13 PRINT +6.3 +Ln:14 PRINT +2.9000000000000004 +Ln:16 PRINT +11.1 +Ln:17 PRINT +2.2 +Ln:18 PRINT +4.800000000000001 +Ln:19 PRINT +8.2 +Ln:21 PRINT +-8.9 +Ln:22 PRINT +0.0 +Ln:23 PRINT +-2.6 +Ln:24 PRINT +-6.0 +Ln:26 PRINT +13.7 +Ln:27 PRINT +4.800000000000001 +Ln:28 PRINT +7.4 +Ln:29 PRINT +10.8 +Ln:31 PRINT +-6.3 +Ln:32 PRINT +2.6 +Ln:33 PRINT +0.0 +Ln:34 PRINT +-3.3999999999999995 +Ln:36 PRINT +17.1 +Ln:37 PRINT +8.2 +Ln:38 PRINT +10.8 +Ln:39 PRINT +14.2 +Ln:41 PRINT +-2.9000000000000004 +Ln:42 PRINT +6.0 +Ln:43 PRINT +3.3999999999999995 +Ln:44 PRINT +0.0 \ No newline at end of file diff --git hplsql/src/test/results/local/create_package.out.txt hplsql/src/test/results/local/create_package.out.txt index 4b631e5..f2f2b11 100644 --- hplsql/src/test/results/local/create_package.out.txt +++ hplsql/src/test/results/local/create_package.out.txt @@ -34,7 +34,7 @@ a: 3 b: 1 p1: 3 Ln:23 RETURN -pack1.f1: +pack1.f1: 12 Ln:58 EXEC PACKAGE PROCEDURE PACK1.sp1 Ln:58 SET PARAM p1 = 1 a: 3