Commit Graph

98 Commits

Author SHA1 Message Date
drh
e6d3c57204 Fix the window-function group_concat() so that it returns an empty string
if it has one or more empty string inputs.  This fixes
a bug introduced by [c6da39115d3e2b0f] on 2019-03-26 (version 3.28.0) and
reported by [forum/forumpost/bf8f43aa522c2299|forum post bf8f43aa522c2299].

FossilOrigin-Name: cec6bb3fc9932ea78ec8e63d9c2d4e56a4d94b8973b9ea46033cc4baa87c0476
2024-05-23 23:26:04 +00:00
dan
5e4233a9e4 Fix a spurious "misuse of aggregate function" error that could occur when an aggregate function was used within the FROM clause of a sub-select of the select that owns the aggregate. e.g. "SELECT (SELECT x FROM (SELECT sum(t1.a) AS x)) FROM t1". [forum:/forumpost/c9970a37ed | Forum post c9970a37ed].
FossilOrigin-Name: 4470f657d2069972d02a00983252dec1f814d90c0d8d0906e320e955111e8c11
2023-11-02 21:02:53 +00:00
drh
d5e040b6f0 Add string_agg(X,Y) as an alias for group_concat(X,Y), for compatibility
with SQLServer and PG.

FossilOrigin-Name: b91c19bf2680f60d7826ab5d9e7902e2dc2a55d847bbea565a6489d47f2cc8f1
2023-10-20 20:19:30 +00:00
drh
ddf6b27579 When converting an aggregate expression to AGG_COLUMN, be sure to remove
the EP_Unlikely flag if it is present.
[forum:/info/fbfe330a2009d036|Forum post fbfe330a2009d036].

FossilOrigin-Name: 48e65f18b2ebbe786e84e208f2c6c68de6067ba86912fea5a120fd41d0af2184
2023-05-23 15:16:20 +00:00
dan
8efa288f99 Add extra test cases to window1.test.
FossilOrigin-Name: ebc844fbfb046c5789efe21fd607c9301cb7b3d78edef72b2926b8c889b048a9
2023-04-13 14:53:19 +00:00
drh
017bdf7838 Fix a code-generator issue associated with very unusual use of window
functions.  Both the expr.c or the window.c changes will each
independently fix the problem.  They are both included in this patch for
defense in depth.  [forum:/forumpost/0d48347967|Forum post 0d48347967].

FossilOrigin-Name: 1ba22631a7831e3562eda0eb6a5edf7f009c85c7ab4451d9eacd13ef0fb6036a
2023-04-13 14:50:50 +00:00
drh
82aacda7eb Remove an ALWAYS() that can sometimes be false. Add a test case that makes
the test false.

FossilOrigin-Name: c8fb143d64d8e823684cd26799080da4b42bef121ca3c6315b1803a593490926
2023-04-11 15:06:20 +00:00
drh
e1be2b7ad9 Fix a weird corner case in aggregate function processing that results from the
recent addition of support for index expressions on aggregate queries.
[forum:/forumpost/bad532820c|Forum post bad532820c].

FossilOrigin-Name: c34fd9fe1b76e0a5943f014f46141cbe55d41bb1e6980adf9bcb6785a03e7883
2023-03-28 16:02:28 +00:00
drh
3cbf38c783 Fix multiple problems with RETURNING on a DML statement against a view,
all inspired by [forum:/forumpost/dc3b92cfa0|forum post dc3b92cfa0].
(1) Do not allow a RETURNING clause to trick the code generator into thinking
that the view being updated has an INSTEAD OF trigger.
(2) Generate all result columns for a view in a DML statement.
(3) The automatic covering index for a view should cover all result columns
of the view.

FossilOrigin-Name: c8bedef0d61731c29ae34de1594222d15b578f9e2cddbbd5b74fb3059644fe0f
2023-03-28 11:18:04 +00:00
dan
0433789863 When it is known when preparing a statement that X cannot be NULL, transform the expression (X IS NULL) to integer value 1 instead of 'true'. This is because under some circumstances, "Y IS TRUE" may not be equivalent to "Y IS 1".
FossilOrigin-Name: cc4bb05b3653e9502b95ea6fe0bfb77feebc11285b66e1dde4c7b945928efbf1
2023-03-03 16:25:18 +00:00
drh
a3fc683c80 In the query planner, add a heuristic that will reduce the cost of a full
table scan for a materialized view or subquery if the full scan is the
outer-most loop.  This is shown to speed up some queries.

FossilOrigin-Name: 609fbb94b8f01d6792e5941ab23ce041313d359f6788c4dde6b1ca749ab49137
2022-09-01 10:29:02 +00:00
drh
b9294de1e6 Allow a HAVING clause on any aggregate query, even if there is no GROUP BY
clause.  This brings SQLite into closer agreement with PostgreSQL and fixes
the concern raised by 
[forum:/forumpost/1a7fea4651|forum post 1a7fea4651].

FossilOrigin-Name: 9322a7c21f1c22ba00e9b889223e89bc1591db6e561ce05091e905e98c1bf2b3
2022-06-21 13:41:24 +00:00
drh
6d64b4a027 Ensure that the window function rewrite does not leave the parse tree
in an invalid state that might cause problems downstream before the
error is recognized and unwinds the stack.  Also take steps such that
an invalid parse tree does not cause problems even if it goes 
unrecognized.  
[forum:/forumpost/398e9d5aa9|Forum post 398e9d5aa9].

FossilOrigin-Name: 0f9fc6b6073365d5159cd71e7fe08f8dadbc7b42abd324361e809502f4359155
2021-11-07 23:33:01 +00:00
dan
37f3ac8faa Change things so that SQLITE_OMIT_VIRTUALTABLE implies SQLITE_OMIT_ALTER_TABLE.
FossilOrigin-Name: 2f7c946c5f25a858167f5193ca06e53310394c8cff15426ab8a1327a1ec835fd
2021-10-01 20:39:50 +00:00
dan
a261c02d44 Fix a problem handling ORDER BY terms of the form "ORDER BY likely(<integer>)" within window frames.
FossilOrigin-Name: 710f75b98bb4ac5bfdfa745c1de832dd1e4d9fb387da52b2f0d1353613f5cdc3
2021-06-23 11:12:48 +00:00
drh
0542812726 Additional defenses (above and beyond [b986600520696b0c]) to prevent an
invalid subquery from causing problems downstream.  If an error is found
while analyzing a subquery expression, change the expression to TK_ERROR
so inhibit further processing on that expression.
dbsqlfuzz cf624b8c0484c66e0f552bf6475e3e3f2c22b24e.

FossilOrigin-Name: 0be6b6c9f7c562e764792a4a5eb53ed11b230174b19361f7cd7778c743314bbd
2021-05-24 00:17:04 +00:00
drh
5e1a7ded4d Do not invoke sqlite3ExprAffinity() after a syntax error that might have
left the tree in an inconsistent state.  See also [e8a1515b44380cc5] and
[forum:/forumpost/7e484e225c|forum post 7e484e225c].

FossilOrigin-Name: b986600520696b0c91c4ccc6aff1b698391b4bcaf8a3ea436be1967883faa2fe
2021-05-22 11:23:20 +00:00
dan
3d691fd9ff Allow aggregate sub-selects within ORDER BY and PARTITION BY clauses of window frame definitions.
FossilOrigin-Name: 3daab94977af5e8a95690acc555540311403d890a3261a9757c633fcaaf428a9
2021-05-19 14:49:51 +00:00
dan
be12083bc7 Fix problems with refering to CTEs from within sub-selects in PARTITION BY or ORDER BY clauses of window frame definitions. Also a problem with renaming a column when the schema contains a trigger containing a correlated sub-select within a window frames PARTITION BY or ORDER BY clause.
FossilOrigin-Name: 4c6cd54a8db78e5535912e76856bed4f797261aaca4248c69d2e2452194de297
2021-05-17 16:20:41 +00:00
drh
4752bbd872 Detect misuse of aggregate functions in the ORDER BY clause of a query
even if the query also contains window functions.

FossilOrigin-Name: 0d11d777c8d368f0b6e9faf3afccf7da1b041d303a68782e43aebd713aaecf51
2021-05-07 15:46:36 +00:00
dan
3c6fbd6dd9 Remove a couple of NEVER() macros from the code for walking window lists.
FossilOrigin-Name: 4ec9ef4bcd12bd9ba5e1a2424f75479070d12c25f1272548c6cf3f89be826b8c
2021-04-17 20:13:53 +00:00
drh
7b0d34fe32 Ensure that variables are not used in the WINDOW clause of a query inside
of a trigger.  dbsqlfuzz d9cf66100064952b66951845dfab41de1c124611

FossilOrigin-Name: 1a341378ab24a5091e6cf03b9e957d11b2bfe5c711835e8c583785f8fa0125d3
2021-04-17 13:46:23 +00:00
dan
d487e37367 Fix a segfault that could occur if the ORDER BY clause of a compound SELECT contains a sub-select that uses one or more window functions.
FossilOrigin-Name: 5ba15ebb34c3af85ef6c54bbb3acb57176d629cda83774881b2a6467e138e904
2021-04-12 16:59:28 +00:00
dan
6b6ec407ab Do not allow floating point rounding errors to cause a window function xInverse() function to be invoked before the corresponding xStep() call.
FossilOrigin-Name: 7a19fed4f222bc6c20e13a1367c8235916d21ba5e6f5a31cd26842efe748e744
2021-04-05 19:05:18 +00:00
dan
27da907f8e Fix a problem causing queries containing window functions to ignore collation sequences under some circumstances.
FossilOrigin-Name: e6c2192ef88e9990c8b91755c8e779f09e23f936a17123d8e42059257b756ed7
2020-07-13 15:20:27 +00:00
dan
46daa99b8d Avoid rewriting compound SELECT statements that use a different collation sequence for ORDER BY and record processing a second time if they contain window functions. Fix for [b706351c].
FossilOrigin-Name: 32a88bdd4be5acdc1b80856bf6e32724dc3a467d5050bec0fe1a3dfedcc06f34
2020-06-11 15:53:54 +00:00
dan
f65e379919 Ensure that the "push-down" optimization does not push constraints down into compound queries if any of the component queries uses window functions.
FossilOrigin-Name: 094dcfe779613301521e8bb990432df187b3686add75a3420b4a193f02f3467f
2020-06-10 10:58:15 +00:00
drh
bf7909734a AggInfo objects might be referenced even after the sqlite3Select() function
that created them has exited.  So AggInfo cannot be a stack variable.  And it
must not be freed until the Parse object is destroyed.

FossilOrigin-Name: 3c840b4df306e2db1da08673e9ede973b4cb6d2b3f9eeeab5835e39452ee3056
2020-06-07 20:18:07 +00:00
drh
896366282d Alternative fix to ticket [c8d3b9f0a750a529]: Prior to deleting or modifying
an Expr not that is referenced by an AggInfo, modify the AggInfo to get its
own copy of the original Expr.

FossilOrigin-Name: 7682d8a768fbccfe0cc956e9f6481637146e1ab9763b248ff11052761ce32e32
2020-06-07 17:33:18 +00:00
drh
c37577bb2d When rewriting a query for window functions, if the rewrite changes the
depth of TK_AGG_FUNCTION nodes, be sure to adjust the Expr.op2 field
appropriately.  Fix for ticket [7a5279a25c57adf1]

FossilOrigin-Name: ad7bb70af9bb68d192137188bb2528f1e9e43ad164c925174ca1dafc9e1f5339
2020-05-24 03:38:37 +00:00
dan
efa78884a8 Fix a problem handling constant integer expressions with collation sequences in PARTITION BY clauses.
FossilOrigin-Name: 155e6649efe8614718be7ac6c3cccf5b073ae57496dc220db5e4313621f5188e
2020-05-11 10:55:24 +00:00
drh
c415d91007 In the event of a semantic error in an aggregate query, early-out the
resetAccumulator() function to prevent problems due to incomplete or
incorrect initialization of the AggInfo object.
Fix for ticket [af4556bb5c285c08].

FossilOrigin-Name: 4a302b42c7bf5e11ddb5522ca999f74aba397d3a7eb91b1844bb02852f772441
2020-04-03 13:19:03 +00:00
dan
0a21ea997d Fix a problem with window functions occuring within sub-selects that are part of an OR term in a WHERE clause of the outer SELECT.
FossilOrigin-Name: 1e174ed0d29366eb56ad1a0cc8defcb440b426bfd9525aed2f93468248606efc
2020-02-29 17:19:42 +00:00
dan
fbb6e9ff48 Fix an assert() in window.c that could fail with some obscure SELECT statements that use window functions.
FossilOrigin-Name: 83dc55679a91bf5d1d13706088ce58eed02b9aad1ad0ae237966e78e0d769663
2020-01-09 20:11:29 +00:00
dan
aa328b6ab1 Ensure that when a Select object is reset a new, empty, SrcList is allocated.
FossilOrigin-Name: 4889cbf898d7ec54f061b21b6d3621b22fc482cbeaa7115d40995a4cc30e41db
2020-01-03 13:55:14 +00:00
dan
4ea562ee70 Ensure that when code for a scalar SELECT featuring window functions is generated more than once by the planner, separate ephemeral tables are opened for each instance.
FossilOrigin-Name: ce1417325273aba866767349b55d9bbfb61a08e716bebda2122918a9657ee38c
2020-01-01 20:17:15 +00:00
dan
997d7434b7 Do not attempt to flatten compound sub-queries in a FROM clause into the parent if any component of the sub-query uses a window function.
FossilOrigin-Name: eeb76f621de2f930a548db0fbb9fe25b4479b73581826b8dfa2e63cd1f1ab783
2019-12-28 18:25:51 +00:00
dan
b4b3630657 Remove a NEVER() that is no longer true. Fix for [36ffedcb9].
FossilOrigin-Name: 597896ed0ae9e2960a8f39576bd7f77a11dccc1da84b6a44ebb5c38d90ebc330
2019-12-27 20:06:32 +00:00
dan
e3735bf466 Fix a problem involving window function aliases being referenced from sub-selects.
FossilOrigin-Name: e3b5fc05c00fc58be7a7c94ce1d97a5b05113f39aba03df64aab08364f85616b
2019-12-27 19:46:07 +00:00
drh
6473ba95a0 Add a missing "ifcapable rtree {...}" line to the window1.test test module.
FossilOrigin-Name: 4e6fbb1a5100fe29209bcf668fbecf3a733034b84925265d8a15b0a84baf6cf0
2019-12-27 18:15:04 +00:00
dan
431704375e Fix a problem with window functions in aggregate queries that do not have GROUP BY clauses.
FossilOrigin-Name: 99609786f485653464d1caef1d207ae299d28c925bff3e8e94891759b4765dcc
2019-12-27 16:25:56 +00:00
dan
d8d2fb9222 Do not mistake constant integers in a PARTITION BY expression for references to ORDER BY style references to values returned by the SELECT statement.
FossilOrigin-Name: 45c64d39d5afed7fdc6b1de19408559740d1fba1f5e0269ab68b132366a289e4
2019-12-27 15:31:47 +00:00
dan
5e484cb3eb When determining if an aggregate within a sub-query should be processed as part of the sub-query or an outer query, consider any FILTER clause in the same way as the arguments to the aggregate.
FossilOrigin-Name: 1ffc045d2bbeeda9c13a3a3585538ff6434fc852b0ae6679d3c8b9f606bf92e6
2019-12-27 08:57:08 +00:00
drh
a9ebfe2030 When the sqlite3WindowRewrite() routine detects and error, have it convert
the SELECT statement into just "SELECT null" so that it does not leave the
parse tree in a goofy state that can cause problems with subsequent code
before the stack has a chance to unwind and report the error.
Ticket [d87336c81c7d0873]

FossilOrigin-Name: fa58aad48a788802b13a819e49f9b8787f713bbe395c46c7295e821c52c81738
2019-12-25 23:54:21 +00:00
dan
1d24a53125 Fix a case in which SQLite could fail to identify "x BETWEEN ? AND ?" being true as implying that x is not null. Ticket [dfd66334].
FossilOrigin-Name: 2f17974912ec5e99089dc0da803e7ff1bf033377a49762d2689a812c005f2641
2019-12-23 15:17:11 +00:00
dan
2811ea6be7 For expressions like (x, y) IN (SELECT ...) where the SELECT uses window-functions, require that all columns on the LHS be indexed before an index can be used. Fix for [d9ed4ebe].
FossilOrigin-Name: 0b1dbd60f5db3abe2097dbc0b6de9671685ca5eaf7d3fc8e3f87ff5065a9d114
2019-12-23 14:20:46 +00:00
drh
8cc8feae7e When computing dependencies on WHERE-clause terms, be sure to take into
account the FILTER clause of aggregate functions.  Problem reported by
Manuel Rigger.

FossilOrigin-Name: 3cc2b5709e66ef605c3b85ed13ae6cc9c3090e864ebc18859db36284b8f715b6
2019-12-20 15:35:56 +00:00
drh
8c72afaf33 Add a test case for fix in check-in [1ca0bd982ab1183b]
FossilOrigin-Name: 519864da8bb671941a64bf333c2086ad9a068b5f85759e7abd368f0706d3aefd
2019-12-18 09:17:55 +00:00
drh
29cdbadf8a Correctly deal with multi-row VALUES clauses that contain window functions.
FossilOrigin-Name: 26d991f214db143976e2593d3564b5003eb3539a2728d1a0ccae2a2accece76f
2019-12-07 13:42:47 +00:00
dan
fcc057db25 Fix a double-free that could occur when a component of a compound SELECT with an ORDER BY clause uses named window definitions.
FossilOrigin-Name: 92893b7980cbb0c6e26bc0b21390a717193205c9897fea5f26476462928897f9
2019-12-04 01:42:07 +00:00