Commit Graph

3 Commits

Author SHA1 Message Date
Zach Amsden
c87ab35af1 IMPALA-4813: Round on divide and multiply
Address rounding on divide and multiply when results are truncated.

Testing: Manually ran some divides that should overflow, then added
the results to the test.  Made the decimal-test use rounding behavior
by default, and now the error margin of the test has decreased.

Initial perf results:

Multiply is totall uninteresting so far, all implementations
return the same values in the same time:

+-------------------------+-----------------------------------+
| sum(l_quantity * l_tax) | sum(l_extendedprice * l_discount) |
+-------------------------+-----------------------------------+
| 61202493.3700           | 114698450836.4234                 |
+-------------------------+-----------------------------------+
Fetched 1 row(s) in 1.13s

Divide shows no regression from prior with DECIMAL_V2 off:

+-----------------------------+-----------------------------------+
| sum(l_quantity / l_tax)     | sum(l_extendedprice / l_discount) |
+-----------------------------+-----------------------------------+
| 46178777464.523809516381723 | 61076151920731.010714279183910    |
+-----------------------------+-----------------------------------+
before:  Fetched 1 row(s) in 13.08s
after:   Fetched 1 row(s) in 13.06s

And with DECIMAL_V2 on:

+-----------------------------+-----------------------------------+
| sum(l_quantity / l_tax)     | sum(l_extendedprice / l_discount) |
+-----------------------------+-----------------------------------+
| 46178777464.523809523847285 | 61076151920731.010714285714202    |
+-----------------------------+-----------------------------------+
Fetched 1 row(s) in 16.06s

So the performance regression is not as bad as expected.  Still,
divide performance could use some work.

Change-Id: Ie6bfcbe37555b74598d409c6f84f06b0ae5c4312
Reviewed-on: http://gerrit.cloudera.org:8080/6132
Reviewed-by: Dan Hecht <dhecht@cloudera.com>
Tested-by: Impala Public Jenkins
2017-03-02 20:12:05 +00:00
Michael Ho
637cc3e447 IMPALA-4821: Update AVG() for DECIMAL_V2
This change implements the DECIMAL_V2's behavior for AVG().
The differences with DECIMAL_V1 are:

1. The output type has a minimum scale of 6. This is similar
to MS SQL's behavior which takes the max of 6 and the input
type's scale. We deviate from MS SQL in the output's precision
which is always set to 38. We use the smallest precision which
can store the output. A key insight is that the output of AVG()
is no wider than the inputs. Precision only needs to be adjusted
when the scale is augmented. Using a smaller precision avoids
potential loss of precision in subsequent decimal operations
(e.g. division) if AVG() is a subexpression. Please note that
the output type is different from SUM()/COUNT() as the latter
can have a much larger scale.

2. Due to a minimum of 6 decimal places for the output,
AVG() for decimal values whose whole number part exceeds 32
decimal places (e.g. DECIMAL(38,4), DECIMAL(33,0)) will
always overflow as the scale is augmented to 6. Certain
decimal types which work with AVG() in DECIMAL_V1 no longer
work in DECIMAL_V2.

Change-Id: I28f5ef0370938440eb5b1c6d29b2f24e6f88499f
Reviewed-on: http://gerrit.cloudera.org:8080/6038
Reviewed-by: Dan Hecht <dhecht@cloudera.com>
Reviewed-by: Alex Behm <alex.behm@cloudera.com>
Tested-by: Impala Public Jenkins
2017-02-22 06:31:14 +00:00
Dan Hecht
a53eeb2068 IMPALA-4370: Divide and modulo result types for DECIMAL version V2
Implement the new DECIMAL return type rules for divide and modulo
expressions, active when query option DECIMAL_V2=1. See the comment
in the code for more details. A couple of examples that show why new
return type rules for divide are desirable.

For modulo, the return types are actually equivalent, though the
rules are expressed differently to have consistency with how
precision fixups are handled for each version.

DECIMAL Version 1:

+-------------------------------------------------------+
| cast(1 as decimal(20,0)) / cast(3 as decimal(20,0)) |
+-----------------------------------------------------+
| 0                                                   |
+-------------------------------------------------------+

DECIMAL Version 2:

+-------------------------------------------------------+
| cast(1 as decimal(20,0)) / cast(3 as decimal(20,0)) |
+-----------------------------------------------------+
| 0.333333333333333333                                |
+-------------------------------------------------------+

DECIMAL Version 1:

+-------------------------------------------------------+
| cast(1 as decimal(6,0)) / cast(0.1 as decimal(38,38)) |
+-------------------------------------------------------+
| NULL                                                  |
+-------------------------------------------------------+
WARNINGS: UDF WARNING: Expression overflowed, returning NULL

DECIMAL Version 2:

+-------------------------------------------------------+
| cast(1 as decimal(6,0)) / cast(0.1 as decimal(38,38)) |
+-------------------------------------------------------+
| 10.000000                                             |
+-------------------------------------------------------+

Change-Id: I83e7f7787edfa4b4bddc25945090542a0e90881b
Reviewed-on: http://gerrit.cloudera.org:8080/5952
Reviewed-by: Dan Hecht <dhecht@cloudera.com>
Tested-by: Impala Public Jenkins
2017-02-14 18:40:54 +00:00