[MDEV-17263] Different Result of calculation in view Created: 2018-09-21  Updated: 2023-04-27

Status: Confirmed
Project: MariaDB Server
Component/s: Views
Affects Version/s: 10.2.17, 10.3.9, 10.2, 10.3
Fix Version/s: 10.4

Type: Bug Priority: Major
Reporter: Björn Gießler Assignee: Oleksandr Byelkin
Resolution: Unresolved Votes: 0
Labels: None
Environment:

Docker Image mariadb:latest
10.3.9-MariaDB-1:10.3.9+maria~bionic - mariadb.org binary distribution



 Description   

After upgrading MariaDB an old view we are using suddenly returned different results. I have built an test case:

Two tables containing forms and position. Each form has n positions. Every position has an amount and every form has a discount, valid for all its positions:

CREATE TABLE `forms` (
  `form_id` int(11) NOT NULL UNIQUE,
  `discount` decimal(5,2) NOT NULL,
  PRIMARY KEY (`form_id`)
);
 
CREATE TABLE `positions` (
  `pos_id` int(11) NOT NULL AUTO_INCREMENT,
  `form_id` int(11) NOT NULL,
  `amount` decimal(9,2) NOT NULL,
  PRIMARY KEY (`pos_id`)
);
 
INSERT INTO `forms` (`form_id`, `discount`) VALUES ('1', '10');
INSERT INTO `positions` (`pos_id`, `form_id`, `amount`) VALUES (NULL, '1', '100'), (NULL, '1', '200');

When I query this, I get the right result:
100 + 200 = 300
-10% = 270

SELECT
	F.form_id,
	COUNT(P.pos_id) AS pos,
	SUM(P.amount) * (100 - F.discount)/100 AS total
FROM `forms` AS F
	INNER JOIN `positions` AS P USING(form_id)
GROUP BY form_id;

Output

+---------+-----+--------------+
| form_id | pos | total        |
+---------+-----+--------------+
|       1 |   2 | 270.00000000 |
+---------+-----+--------------+
1 row in set (0.001 sec)

Now when I wrap exactly this SQL statement inside an view, a different result occurs:

CREATE VIEW view_1 AS
SELECT
	F.form_id,
	COUNT(P.pos_id) AS pos,
	SUM(P.amount) * (100 - F.discount)/100 AS total
FROM `forms` AS F
	INNER JOIN `positions` AS P USING(form_id)
GROUP BY form_id;
 
SELECT * FROM view_1;

Output

+---------+-----+--------------+
| form_id | pos | total        |
+---------+-----+--------------+
|       1 |   2 | 29999.900000 |
+---------+-----+--------------+
1 row in set (0.001 sec)

Problem is obviously, that the parentheses in SUM(P.amount) * (100 - F.discount)/100 are ignored in the VIEW and therefor the multiplication is executed before the subtraction.

w/ parentheses

SUM(P.amount) * (100 - F.discount)/100
= (100+200)   * (100 -     10    )/100
=    300      *        90/100
=    300      *         0.9
= 270

w/o parentheses

SUM(P.amount) * 100 - F.discount/100
= (100+200)   * 100 -         10/100
=    300      * 100 -            0.9
= 29999.9

The problem occurred when upgrading a docker container to mariadb:latest. Unfortunately I cannot say which version we were running before.

So I have tested all the latest minor releases of 5.5 and 10.x from the official docker repo:

Version Image Works as expected
5.5.54 5.5.54-MariaDB-1~wheezy
10.0.36 10.0.36-MariaDB-1~xenial
10.1.36 10.1.36-MariaDB-1~bionic
10.2.17 10.2.17-MariaDB-1:10.2.17+maria~bionic
10.3.9 10.3.9-MariaDB-1:10.3.9+maria~bionic

Hope that helps. It's my first bug here



 Comments   
Comment by Alice Sherepa [ 2018-09-24 ]

Thanks for the report and the test case!
Reproduced as described, on MariaDB 10.2, 10.3, correct results with 5.5-10.1

MariaDB [test]> show create view  view_1\G
*************************** 1. row ***************************
                View: view_1
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_1` AS select `F`.`form_id` AS `form_id`,count(`P`.`pos_id`) AS `pos`,sum(`P`.`amount`) * 100 - `F`.`discount` / 100 AS `total` from (`forms` `F` join `positions` `P` on(`F`.`form_id` = `P`.`form_id`)) group by `F`.`form_id`
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.001 sec)

Generated at Thu Feb 08 08:35:08 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.