[ 
https://issues.apache.org/jira/browse/PHOENIX-6741?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17560906#comment-17560906
 ] 

chaijunjie commented on PHOENIX-6741:
-------------------------------------

In the *DivideExpression* class,  int the for loop, when we caculate  val is 
bigger than PDataType.MAX_PRECISION, then return PDataType.MAX_PRECISION as 
maxLength,then the scale return 0.

 

public DivideExpression(List<Expression> children) {
super(children);
Expression firstChild = children.get(0);
maxLength = getPrecision(firstChild);
scale = getScale(firstChild);
for (int i=1; i<children.size(); i++) {
Expression childExpr = children.get(i);
maxLength = getPrecision(maxLength, getPrecision(childExpr), scale, 
getScale(childExpr));
scale = getScale(maxLength, getPrecision(childExpr), scale, 
getScale(childExpr));
}
}

private static Integer getPrecision(Integer lp, Integer rp, Integer ls, Integer 
rs) {
if (ls == null || rs == null) {
return PDataType.MAX_PRECISION;
}
int val = getScale(lp, rp, ls, rs) + lp - ls + rp;
return Math.min(PDataType.MAX_PRECISION, val);
}

private static Integer getScale(Integer lp, Integer rp, Integer ls, Integer rs) 
{
// If we are adding a decimal with scale and precision to a decimal
// with no precision nor scale, the scale system does not apply.
if (ls == null || rs == null) {
return null;
}
int val = Math.max(PDataType.MAX_PRECISION - lp + ls - rs, 0);
return Math.min(PDataType.MAX_PRECISION, val);
}

 

> The result of DivideExpression is wrong when two decimal number divide
> ----------------------------------------------------------------------
>
>                 Key: PHOENIX-6741
>                 URL: https://issues.apache.org/jira/browse/PHOENIX-6741
>             Project: Phoenix
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 5.1.2
>            Reporter: chaijunjie
>            Assignee: chaijunjie
>            Priority: Major
>
> When two number set  precision and scale, then the result will be scaled by 0.
> DROP TABLE IF EXISTS TEST;
> CREATE TABLE TEST (
> ID INTEGER NOT NULL PRIMARY KEY,
> A DECIMAL(18,10),
> B DECIMAL(18,10)
> );
> UPSERT INTO TEST VALUES(1,100,200);
> SELECT ID,A/B AS C FROM TEST;
> 0: jdbc:phoenix:> SELECT ID,A/B AS C FROM TEST;
> {+}--{-}{{-}}{{-}}{-}{-}{-}{+}-+
> |ID  |C  |
> {+}--{-}{{-}}{{-}}{-}{-}{-}{+}-+
> |1  |0  |
> {+}--{-}{{-}}{{-}}{-}{-}{-}{+}-+
>  
> DROP TABLE IF EXISTS TEST;
> CREATE TABLE TEST (
> ID INTEGER NOT NULL PRIMARY KEY,
> A DECIMAL(38,10),
> B DECIMAL(18,10)
> );
> UPSERT INTO TEST VALUES(1,100,200);
> SELECT ID,A/B AS C FROM TEST;
> {+}----{-}{-}{+}---+
> |ID  |C  |
> {+}----{-}{-}{+}---+
> |1  |0  |
> {+}----{-}{-}{+}---+
>  
> But, we need a precise result, so we need add a 1.00*, then the result will 
> be right.
> 0: jdbc:phoenix:> SELECT ID,1.00*A/B AS C FROM TEST;
> {+}--{-}{{-}}{{-}}{-}{-}{-}{+}---+
> |ID  | C  |
> {+}--{-}{{-}}{{-}}{-}{-}{-}{+}---+
> |1  |0.5  |
> {+}--{-}{{-}}{{-}}{-}{-}{-}{+}---+



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to