[MDEV-9801] Variables in SQL query Created: 2016-03-27 Updated: 2016-03-31 Resolved: 2016-03-31 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Affects Version/s: | 10.0.23 |
| Fix Version/s: | N/A |
| Type: | Bug | Priority: | Major |
| Reporter: | Lukáš Kvídera | Assignee: | Sergei Golubchik |
| Resolution: | Won't Fix | Votes: | 0 |
| Labels: | None | ||
| Environment: |
Raspbian 4.1.17-v7+ #838 SMP Tue Feb 9 13:15:09 GMT 2016 armv7l GNU/Linux |
||
| Description |
|
The problem is with variables in SQL. Sometimes they are properly calculated, sometimes they are NULL. How to reproduce:
2) run query
3) output:
4) run query again
DDL:
|
| Comments |
| Comment by Elena Stepanova [ 2016-03-30 ] | |||||
|
Did you try to initialize the variables to 0 before using them? | |||||
| Comment by Lukáš Kvídera [ 2016-03-30 ] | |||||
|
If I added: SET @prev_received := NULL; The output is correct at first call. | |||||
| Comment by Sergei Golubchik [ 2016-03-31 ] | |||||
|
This is explicitly documented in the manual:
MySQL manual says the same:
In your example (simplified)
you implicitly assume that the server will first calculate the first column value @prev_sent, then the second c.bytes_sent - @prev_sent, and then the third, that assigns new value to @prev_sent. But there is no reason to assume that, the server evaluates expressions in the SELECT list in some internal unspecified order, so your SELECT statement is inherently unreliable, it might work and might not work —- which is exactly what you see. |