Description:
When using the LAG() window function in an arithmetic expression, the result may be wrong
How to repeat:
This statement:
WITH tab(t, company, quote) AS (
|
SELECT 1 AS t, 'G' AS company, 40 AS quote
|
UNION SELECT 2 , 'G', 60
|
UNION SELECT 3 , 'S', 60
|
UNION SELECT 4, 'S', 20
|
)
|
SELECT DISTINCT company, quote - LAG(quote) OVER(PARTITION BY company ORDER BY t)
|
FROM tab;
|
produces wrong output:
company e
|
--------------
|
G (null)
|
S (null)
|
Remove the DISTINCT keyword....
WITH tab(t, company, quote) AS (
|
SELECT 1 AS t, 'G' AS company, 40 AS quote
|
UNION SELECT 2 , 'G', 60
|
UNION SELECT 3 , 'S', 60
|
UNION SELECT 4, 'S', 20
|
)
|
SELECT company, quote - LAG(quote) OVER(PARTITION BY company ORDER BY t) e
|
FROM tab;
|
to get this:
company e
|
--------------
|
G (null)
|
G 20
|
S (null)
|
S -40
|
As can be seen, the DISTINCT keyword should have no effect on this query.
This is also described in this stack overflow question:
https://stackoverflow.com/q/49700278/521799
The same bug appears also in MySQL:
https://bugs.mysql.com/bug.php?id=92503
- is duplicated by
-
MDEV-14791
Crash with order by expression containing window functions
-
-
Closed
{"report":{"fcp":1353.4000000003725,"ttfb":289.7000000011176,"pageVisibility":"visible","entityId":69866,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"b1dcc5e9-d8da-4ab9-bdf2-ac1226ad6718","navigationType":0,"readyForUser":1430.5,"redirectCount":0,"resourceLoadedEnd":1107.9000000003725,"resourceLoadedStart":297.09999999962747,"resourceTiming":[{"duration":65.40000000037253,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":297.09999999962747,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":297.09999999962747,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":362.5,"responseStart":0,"secureConnectionStart":0},{"duration":65.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":297.40000000037253,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":297.40000000037253,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":362.90000000037253,"responseStart":0,"secureConnectionStart":0},{"duration":630.7000000011176,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":297.59999999962747,"connectEnd":755.5,"connectStart":755.5,"domainLookupEnd":755.5,"domainLookupStart":755.5,"fetchStart":297.59999999962747,"redirectEnd":0,"redirectStart":0,"requestStart":755.9000000003725,"responseEnd":928.3000000007451,"responseStart":772.3000000007451,"secureConnectionStart":755.5},{"duration":789,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":297.80000000074506,"connectEnd":297.80000000074506,"connectStart":297.80000000074506,"domainLookupEnd":297.80000000074506,"domainLookupStart":297.80000000074506,"fetchStart":297.80000000074506,"redirectEnd":0,"redirectStart":0,"requestStart":760.2000000011176,"responseEnd":1086.800000000745,"responseStart":775.7000000011176,"secureConnectionStart":297.80000000074506},{"duration":504.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":297.90000000037253,"connectEnd":297.90000000037253,"connectStart":297.90000000037253,"domainLookupEnd":297.90000000037253,"domainLookupStart":297.90000000037253,"fetchStart":297.90000000037253,"redirectEnd":0,"redirectStart":0,"requestStart":774.3000000007451,"responseEnd":802.4000000003725,"responseStart":799,"secureConnectionStart":297.90000000037253},{"duration":503.90000000037253,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":298.09999999962747,"connectEnd":298.09999999962747,"connectStart":298.09999999962747,"domainLookupEnd":298.09999999962747,"domainLookupStart":298.09999999962747,"fetchStart":298.09999999962747,"redirectEnd":0,"redirectStart":0,"requestStart":774.2000000011176,"responseEnd":802,"responseStart":797.9000000003725,"secureConnectionStart":298.09999999962747},{"duration":504.40000000037253,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":298.30000000074506,"connectEnd":298.30000000074506,"connectStart":298.30000000074506,"domainLookupEnd":298.30000000074506,"domainLookupStart":298.30000000074506,"fetchStart":298.30000000074506,"redirectEnd":0,"redirectStart":0,"requestStart":774.4000000003725,"responseEnd":802.7000000011176,"responseStart":799.9000000003725,"secureConnectionStart":298.30000000074506},{"duration":475.7000000011176,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":298.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":298.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":774.2000000011176,"responseStart":0,"secureConnectionStart":0},{"duration":504.19999999925494,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":298.7000000011176,"connectEnd":298.7000000011176,"connectStart":298.7000000011176,"domainLookupEnd":298.7000000011176,"domainLookupStart":298.7000000011176,"fetchStart":298.7000000011176,"redirectEnd":0,"redirectStart":0,"requestStart":776.7000000011176,"responseEnd":802.9000000003725,"responseStart":800.5999999996275,"secureConnectionStart":298.7000000011176},{"duration":475.80000000074506,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":298.90000000037253,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":298.90000000037253,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":774.7000000011176,"responseStart":0,"secureConnectionStart":0},{"duration":510.30000000074506,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":299,"connectEnd":299,"connectStart":299,"domainLookupEnd":299,"domainLookupStart":299,"fetchStart":299,"redirectEnd":0,"redirectStart":0,"requestStart":776.9000000003725,"responseEnd":809.3000000007451,"responseStart":806.5999999996275,"secureConnectionStart":299},{"duration":800.7999999988824,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":303.7000000011176,"connectEnd":303.7000000011176,"connectStart":303.7000000011176,"domainLookupEnd":303.7000000011176,"domainLookupStart":303.7000000011176,"fetchStart":303.7000000011176,"redirectEnd":0,"redirectStart":0,"requestStart":903.0999999996275,"responseEnd":1104.5,"responseStart":1099.9000000003725,"secureConnectionStart":303.7000000011176},{"duration":793,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":314.90000000037253,"connectEnd":314.90000000037253,"connectStart":314.90000000037253,"domainLookupEnd":314.90000000037253,"domainLookupStart":314.90000000037253,"fetchStart":314.90000000037253,"redirectEnd":0,"redirectStart":0,"requestStart":976.8000000007451,"responseEnd":1107.9000000003725,"responseStart":1102.9000000003725,"secureConnectionStart":314.90000000037253},{"duration":226.30000000074506,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1057,"connectEnd":1057,"connectStart":1057,"domainLookupEnd":1057,"domainLookupStart":1057,"fetchStart":1057,"redirectEnd":0,"redirectStart":0,"requestStart":1245.5999999996275,"responseEnd":1283.300000000745,"responseStart":1282.4000000003725,"secureConnectionStart":1057}],"fetchStart":0,"domainLookupStart":44,"domainLookupEnd":54,"connectStart":54,"connectEnd":74,"secureConnectionStart":63,"requestStart":75,"responseStart":289,"responseEnd":314,"domLoading":293,"domInteractive":1498,"domContentLoadedEventStart":1498,"domContentLoadedEventEnd":1558,"domComplete":1867,"loadEventStart":1867,"loadEventEnd":1867,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1466.2000000011176},{"name":"bigPipe.sidebar-id.end","time":1466.9000000003725},{"name":"bigPipe.activity-panel-pipe-id.start","time":1467.0999999996275},{"name":"bigPipe.activity-panel-pipe-id.end","time":1470.7000000011176},{"name":"activityTabFullyLoaded","time":1573.2000000011176}],"measures":[],"correlationId":"b45707670db062","effectiveType":"4g","downlink":9.3,"rtt":0,"serverDuration":140,"dbReadsTimeInMs":14,"dbConnsTimeInMs":26,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Thanks for the report! Reproducible on MariaDB 10.2,10.3
create table t1 (i1 int, i2 int);
insert into t1 values (1,40), (2,60), (3,60),(4,20);
MariaDB [test]> SELECT 1+LAG(i2) OVER(ORDER BY i1) FROM t1;
+-----------------------------+
| 1+LAG(i2) OVER(ORDER BY i1) |
+-----------------------------+
| NULL |
| 41 |
| 61 |
| 61 |
+-----------------------------+
4 rows in set (0.001 sec)
MariaDB [test]> SELECT distinct 1+LAG(i2) OVER(ORDER BY i1) FROM t1;
+-----------------------------+
| 1+LAG(i2) OVER(ORDER BY i1) |
+-----------------------------+
| NULL |
+-----------------------------+
1 row in set (0.000 sec)
MariaDB [test]> SELECT distinct 1+LAG(i2,0) OVER(ORDER BY i1) FROM t1;
+-------------------------------+
| 1+LAG(i2,0) OVER(ORDER BY i1) |
+-------------------------------+
| 41 |
+-------------------------------+
1 row in set (0.000 sec)
MariaDB [test]> SELECT distinct 1+LEAD(i2) OVER(ORDER BY i1) FROM t1;
+------------------------------+
| 1+LEAD(i2) OVER(ORDER BY i1) |
+------------------------------+
| 61 |
+------------------------------+
1 row in set (0.002 sec)
MariaDB [test]> SELECT distinct 1+rank() OVER(ORDER BY i1) FROM t1;
+----------------------------+
| 1+rank() OVER(ORDER BY i1) |
+----------------------------+
| 2 |
+----------------------------+
1 row in set (0.001 sec)
As a temporary workaround, it is possible to use derived table or CTE table
MariaDB [test]> select distinct * from (SELECT 1+LAG(i2) OVER(ORDER BY i1) FROM t1) tmp;
+-----------------------------+
| 1+LAG(i2) OVER(ORDER BY i1) |
+-----------------------------+
| NULL |
| 41 |
| 61 |
+-----------------------------+
3 rows in set (0.003 sec)