pingcap / tidb

TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
https://pingcap.com
Apache License 2.0
37.4k stars 5.85k forks source link

MySQL compatibility - Support @@div_precision_increment #21485

Open kennytm opened 3 years ago

kennytm commented 3 years ago

Feature Request

Is your feature request related to a problem? Please describe:

Describe the feature you'd like:

MySQL (at least since 5.6) has the @@div_precision_increment system variable which controls the precision of division result (default is 4). If we compute p/q where p and q are both exact, and p has type DECIMAL(m, d), then the result p/q has type DECIMAL(?, d + @@div_precision_increment).

mysql> select 1/3, length(1/3);
+--------+-------------+
| 1/3    | length(1/3) |
+--------+-------------+
| 0.3333 |           6 |
+--------+-------------+
1 row in set (0.00 sec)

mysql> set @@div_precision_increment = 10;
Query OK, 0 rows affected (0.00 sec)

mysql> select 1/3, length(1/3);
+--------------+-------------+
| 1/3          | length(1/3) |
+--------------+-------------+
| 0.3333333333 |          12 |
+--------------+-------------+
1 row in set (0.00 sec)

TiDB currently knows the existence of a @@div_precision_increment sys var but changing it is no-op because the "4" is hard-coded in

https://github.com/pingcap/tidb/blob/7e3074afa3f3db4555e44e720d7ed77d9797e501/expression/builtin_arithmetic.go#L58-L60

Describe alternatives you've considered:

Don't support it, it's a niche feature which can be worked around through cast or manually increasing the precision anyway.

mysql> select cast(1 as decimal(7,6))/3 union all select 1.000000/3;
+---------------------------+
| cast(1 as decimal(7,6))/3 |
+---------------------------+
|              0.3333333333 |
|              0.3333333333 |
+---------------------------+
2 rows in set, 1 warning (0.05 sec)

Teachability, Documentation, Adoption, Migration Strategy:

kennytm commented 3 years ago

/label component/expression, sig/execution, type/compatibility