apache / shardingsphere

Distributed SQL transaction & query engine for data sharding, scaling, encryption, and more - on any database.
Apache License 2.0
19.88k stars 6.73k forks source link

Get select sum decimal precision error #16787

Open cucuzi opened 2 years ago

cucuzi commented 2 years ago

Bug Report

I select a list, but the decimal is wrong.

using shardingsphere-jdbc: 5.1.0

Expected behavior

list string: [BookClassStatistic(classChar=A, classNum=2, bookNum=3, sumPrice=89.10), BookClassStatistic(classChar=B, classNum=2, bookNum=3, sumPrice=121.60), BookClassStatistic(classChar=D, classNum=2, bookNum=2, sumPrice=38.80), BookClassStatistic(classChar=Z, classNum=1, bookNum=2, sumPrice=5.52)]

By the way, changing database field ( book.price ) type to decimal will get true result.

Actual behavior

list string: [BookClassStatistic(classChar=A, classNum=2, bookNum=3, sumPrice=89.10000228881836), BookClassStatistic(classChar=B, classNum=2, bookNum=3, sumPrice=121.5999984741211), BookClassStatistic(classChar=D, classNum=2, bookNum=2, sumPrice=38.79999923706055), BookClassStatistic(classChar=Z, classNum=1, bookNum=2, sumPrice=5.519999980926514)]

Example codes

SELECT SQL

SELECT UPPER(LEFT(bg.class_no, 1)) class_char,
        count(DISTINCT (bg.class_no)) class_num,
        count(b.id) book_num,
        sum(b.price) sum_price
        FROM book b
        JOIN bibliography bg ON b.bibliography_id = bg.id
        WHERE b.user_id = ?
        GROUP BY class_char

Object

@Data
public class BookClassStatistic {
    private String classChar;
    private Integer classNum;
    private Integer bookNum;
    private BigDecimal sumPrice;
}

Sharding rule

spring:
  shardingsphere:
    rules:
      sharding:
        tables:
          book:
            actual-data-nodes: db0.book_$->{0..2}
            table-strategy:
              standard:
                sharding-column: user_id
                sharding-algorithm-name: table-inline
            key-generate-strategy:
              column: id
              key-generator-name: snowflake
        sharding-algorithms:
          table-inline:
            type: INLINE
            props:
              algorithm-expression: book_$->{user_id % 3}

SQL FILE demo1.zip

strongduanmu commented 2 years ago

@cucuzi Thank you for your contribution, I will investigate this issue.

github-actions[bot] commented 2 years ago

Hello , this issue has not received a reply for several days. This issue is supposed to be closed.

github-actions[bot] commented 6 months ago

There hasn't been any activity on this issue recently, and in order to prioritize active issues, it will be marked as stale.