Commit Graph

70 Commits

Author SHA1 Message Date
drh
78b404d976 Add [/info/cec6a06508239c09e363?ln=5525-5531|restriction (5)] to the
omit-noop-join optimization.  Fix for the issue reported by
[forum/forumpost/49f2c7f690|forum post 49f2c7f690].

FossilOrigin-Name: 3e9c9bbdb59b9d500ff218db538c047c83da7ac18ebb95c3ee7629ab15e0b43a
2023-05-11 21:15:55 +00:00
drh
908dec7404 Do not apply the "AND false" optimization if either operand comes from the
ON clause of a join.  Fix for the problem identified by
[forum:/forumpost/96cd4a7e9e|forum post 96cd4a7e9e].

FossilOrigin-Name: d095da0e7a24e3bcab6495d964f76a86d7a5910d2d6edddc6e8092bfa6084fe6
2023-05-01 11:24:35 +00:00
drh
baef8442d3 Further to [46639f682975dac6], the parameters of a table valued function that
is on the left side of a RIGHT JOIN do not need to be in the ON clause.  Add
new test cases and tags to associated test cases with the code.

FossilOrigin-Name: 18ee689de3d1ae43b05ca52e0b62c49442ebf68a88814a7c679e8856250a4b0d
2023-02-27 14:48:54 +00:00
drh
0f51aa3972 Do not allow EP_InnerON terms to be used on a outer join.
FossilOrigin-Name: f47aa745690c018800243bf76930b2499ff4537411c4e27b4b16ba3854cc2bf8
2022-09-20 22:19:13 +00:00
drh
0cc6f78bcf Add new test cases that (current) get an incorrect answer.
FossilOrigin-Name: fe5c37736444e397ace387b761ec2491229785d7a437f3f60232aedf9d1f00ec
2022-09-20 21:07:52 +00:00
drh
4c5c941b6e New test cases for query flattening when there are ON clauses and outer joins.
FossilOrigin-Name: db55387b1972bc7bc6855bd3497af744a111e7d28d09893cd985fbb01be41bcc
2022-09-20 19:45:04 +00:00
drh
521e0b6cb6 New test cases for query flattening with LEFT JOIN.
FossilOrigin-Name: 27f68e47320c751e3663507500c1c44f0b7f885f89c678fce6a35b1bc372dd64
2022-06-23 22:43:11 +00:00
drh
d198183465 Add back the ability to flatten a LEFT JOIN subquery - previously removed
due to ticket [cad1ab4cb7b0fc344].

FossilOrigin-Name: f8fe936ad4f7678f9b26ba6fab41c5df9f4938634cdb3286b0f2dcd1357f919a
2022-06-23 15:15:03 +00:00
drh
a341bae948 New test cases refute check-in [3f45007d544e5f78].
FossilOrigin-Name: 12d3c96c39d12f6b01b77456eb874fff445ebfc60f50aada51b06ed37af364ea
2022-06-08 13:13:12 +00:00
drh
825ecf9c05 New requirement marks.
FossilOrigin-Name: e8479e56c615a6eb38b58e6d360bea8528ec14a9d7b0798b95d3eb513bd08f0f
2022-05-10 18:43:54 +00:00
drh
825a6bffc4 Ensure correct fg.isNestedFrom values even on crazy parses.
FossilOrigin-Name: e611e8e62c948d28cd3f28452d3096bab06998a685ed9434061cc2054f5cac32
2022-04-21 14:08:29 +00:00
drh
0879d5f9e0 For the bad join type error message "unknown or unsupported join type"
remove the "or unsupported" clause, because we now support all valid join
types.

FossilOrigin-Name: ab0a0562dd3594cf50ee56f6b3a5847fa5dcadf69146d560e3e7a95651b8f405
2022-04-12 18:40:14 +00:00
drh
a76ac88af8 Preliminary code to support RIGHT JOIN. Everything seems to work, except that
the code to compute the unmatched rows for the RIGHT JOIN has not yet been
added, so the result of a RIGHT JOIN is currently the same as an INNER JOIN.

FossilOrigin-Name: 415abd6731b8e8a605adabfa6066c8a852a8531c300df41325d5f7e75cae5a70
2022-04-08 19:20:12 +00:00
drh
d44f8b2385 Improved technique for parsing the ON and USING clauses of a join is faster
and uses less memory.

FossilOrigin-Name: 158156a3e3d50042cafc75dea3aaaa68b1f2efb9c3d178518ea6e68e32e0d21c
2022-04-07 01:11:13 +00:00
drh
7d44b22d45 Fix test cases so that they all still work even with -DSQLITE_DQS=0.
FossilOrigin-Name: 4883776669ee2f2310ea82b0d6df4d008eebaa7cb252102539cf21a635402ebb
2022-01-16 19:11:13 +00:00
drh
af3711536b In the query flattener when substituting expression Y in place of expression X,
if X is marked has having come from an ON or USING clause of a LEFT JOIN, then
be sure that all subexpressions of Y, not just the root node of Y, are
similarly marked.  Otherwise, if Y is an AND operator, it will be split up
during WHERE clause analysis and the subexpressions will not get the special
treatment needed by LEFT JOIN ON/USING clauses.
Fix for ticket [66e4b0e271c47145].

FossilOrigin-Name: 69f9eb7343a416c5ab426c8e1b9f0ae576544b4ccc5d87f5481c8ff884f696e1
2020-09-30 15:36:03 +00:00
dan
8c812f98a3 Fix a problem with using views in SQLITE_OMIT_VIRTUAL_TABLE builds. Also some test case fixes required for the same builds.
FossilOrigin-Name: 934ee8bdb481a5cbd3d9c5f53028073129d3bca4fee14fe4a49bbf9c0c9d74f7
2020-01-21 16:23:17 +00:00
dan
51f2b1719c Fix an instance where the planner might choose to use the OR-optimization when it adds no benefit. The same quirk causes an assert() to fail. This is not a bug in released versions - without the assert() the library still gets the right answer, it just does so less efficiently than it should.
FossilOrigin-Name: f4bed1d7af8a94c6facd567dec5afae8865a5ad76b8834493099e5e30bed1132
2019-12-28 15:24:02 +00:00
drh
6e827fa27d When constructing the virtual MATCH term of the WHERE clause for a virtual
table that is in a LEFT JOIN, be sure to set the correct Expr.iRightJoinTable
value.  This value does not appear to ever be used, except inside of a single
assert().  But it is good to set it correctly, nevertheless.  This fixes
ticket [7929c1efb2d67e98], which as far as I can tell is completely harmless.

FossilOrigin-Name: ef604882a275d3d5ebd4d5a08e3fe43e148f169b7d5c3a81464fbe7f54f3582a
2019-12-22 20:03:29 +00:00
drh
396afe6f6a Continue to back away from the LEFT JOIN optimization of check-in [41c27bc0ff1d3135]
by disallowing query flattening if the outer query is DISTINCT.  Without this fix,
if an index scan is run on the table within the view on the right-hand side of the
LEFT JOIN, stale result registers might be accessed yielding incorrect results,
and/or an OP_IfNullRow opcode might be invoked on the un-opened table, resulting
in a NULL-pointer dereference.  This problem was found by the Yongheng and Rui fuzzer.

FossilOrigin-Name: 862974312edf00e9d1068115d1a39b7235b7db68b6d86b81d38a12f025a4748e
2019-12-18 20:51:58 +00:00
drh
ca7a26b5a1 Do not allow a term in the WHERE clause of the query to qualify a partial
index on the right table of a LEFT JOIN.  Ticket [7f39060a24b47353]

FossilOrigin-Name: 4066a34da7bcdcece6c438c27f3a11bc49b8c8373b7e1603f30f6225e2bc800a
2019-11-30 19:29:19 +00:00
drh
db535390db The optimization of check-in [9b2879629c34fc0a] is incorrectly reasoned.
The WHERE clause of the partial index might not be true if the table of
the partial index is the right table of a left join.  So disable the
optimization in that case.  Ticket [623eff57e76d45f6]

FossilOrigin-Name: 3be19e1151af1850b65991edb82420f9412a7798dd756c86eaa9ffdde573263a
2019-11-03 00:07:41 +00:00
drh
66c48907a2 Remove the legacy_file_format PRAGMA. In its place, provide the
SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option to sqlite3_db_config().
Fix for ticket [6484e6ce678fffab]

FossilOrigin-Name: 4d424f3047b48fc441475137f30a719d2f079390c86fe2617710ddfb05c5e240
2019-10-29 16:18:45 +00:00
dan
0287c95165 Prevent SQLite from assuming that if ((? IS NOT NULL) IS NOT NULL) is true, ? may not be NULL. Fix for [d51a8696].
FossilOrigin-Name: 7833feecfe745e237f239ee4c38a9e4bf7ad66a32919150208da87c00a826473
2019-10-10 17:09:44 +00:00
dan
da03c1e65e Avoid assuming that an expression that contains the sub-expression (? IS FALSE) or (? IS TRUE) may only be true if ? is non-null. Fix for [a976c487].
FossilOrigin-Name: eb7ed90b8a65748f0721aaf7bdddd2207f552be5015603fbfb7044d815ae2f36
2019-10-09 21:14:00 +00:00
dan
b6a9121bb3 Fix another case where SQLite assumes that if "~(? AND FALSE)" is true, "?" must be non-null.
FossilOrigin-Name: 616f5663b3dbd0929128c0990fc6d8bba1513c7ae196b87c450ac0b3b3203ecd
2019-08-29 15:50:16 +00:00
drh
9e9a67adb0 Ensure the functions that appear to be constant are not factored out of
expression that originate on the right-hand side of a LEFT JOIN.
Ticket [6710d2f7a13a2997]

FossilOrigin-Name: 500c9152daaf11cf69d778aa8592175f6088337c6667c59af6df3a24cd81eb0e
2019-08-17 17:07:15 +00:00
drh
d57936721b The IS NOT NULL operator does not imply that the operand is never NULL.
Fix for ticket [5948e09b8c415bc45da5cf]

FossilOrigin-Name: d840e9bb023a1e84a7739d764a0f6cf608fc3183ff8c366fcdb486ebd932f0c1
2019-02-05 14:36:33 +00:00
drh
0ad7aa8182 Limit the size of SrcList objects to 200 entries (compile-time configurable
using -DSQLITE_MAX_SRCLIST=n).  The maximum number of tables in a join has
always been 64, so this is not a real constraint on capability.  Limiting the
size of a SrcList prevents DOS attacks (discovered by OSSFuzz) using crazy
nexted CTE joins.

FossilOrigin-Name: 7cac614d5df55eb092b863163483b6782b942b21bd15fd787576fef5619fa849
2019-01-17 14:34:46 +00:00
dan
0493222ff8 Fix a problem causing the LEFT JOIN strength reduction optimization to be
incorrectly applied in some cases where the WHERE clause of the query contains
a filter expression of the form "lhs.x IS NOT ?".

FossilOrigin-Name: 1fdaf2c34431adcac1c7ff29aae0623c4cbaa6a7f38e843c786bd407d8b3e730
2018-04-10 15:31:56 +00:00
dan
a1054dcc3f Fix a problem causing the LEFT JOIN strength reduction optimization to be
incorrectly applied in some cases where the WHERE clause of the query contains
a filter expression of the form NOT(x AND y). Ticket [1e39b966].

FossilOrigin-Name: 38d319c153641ea4138fab2d5a47c31a86f57a071a1e1c299ca54c980cfb2b60
2018-04-10 12:10:01 +00:00
drh
e3eff266bc Yet another fault in the sqlite3ExprImpliesNotNull() routine, causing errors
in the LEFT JOIN strength reduction optimization of check-in
[dd568c27b1d76563].

FossilOrigin-Name: e88cf3d4df64097ebc19aae464b88d0faf9b7d4c30d057042b582d78327e7ad3
2018-03-24 15:47:31 +00:00
drh
2c492061ce Bug fix in the LEFT JOIN strength reduction optimization of
check-in [dd568c27b1d76563].  The sqlite3ExprImpliesNotNull() routine was
mistakenly assuming that a CASE expression must always be NULL if contained
any reference to a variable that was NULL.

FossilOrigin-Name: cf171abe954a5f25262161dd69f2e8cecdbf9446c3f6b298201507dbc743567e
2018-03-24 13:24:02 +00:00
dan
bd11a2acbb Ensure that the query planner knows that any column of a flattened LEFT JOIN
can be NULL even if that column is labeled with "NOT NULL".
Fix for ticket [892fc34f173e99d8].

FossilOrigin-Name: 483462682d3a57fb9dd85b4772596e9738f1694a454b8ebbc480b9452733e88d
2017-06-20 17:43:26 +00:00
drh
f43ce0b445 The TK_IF_NULL_ROW expression node must be treated as a variable that
references the table Expr.iTable.
Proposed fix for ticket [7fde638e94287d2c].

FossilOrigin-Name: 77fc23013cebc7797985864b91d78db5d0e2469511732044ebfaf02b891c979a
2017-05-25 00:08:48 +00:00
drh
1d1fc5e385 Disable the LEFT JOIN flattening optimization for aggregate queries, as it
does not currently work.  Further fix for ticket [cad1ab4cb7b0fc344].

FossilOrigin-Name: 44b21e35c92137cc519d0cc87c627cc531d0bd442a7b9a7356fa2c6bc5f70fb6
2017-05-23 15:21:37 +00:00
drh
eff0a7b25e Ensure that the expression rewriter inside the query flattener decends into
the substructure of the TK_IF_NULL_ROW operator.  This is a continuation
of the fix for ticket [cad1ab4cb7b0fc344].

FossilOrigin-Name: 941d8142b7c9a96ff143d1add3c86cf42d61fd08e532d400dac555f23eadbcfb
2017-05-23 12:36:13 +00:00
drh
3f1e9e00e6 When flattening a query, make sure iTable attribute of TK_IF_NULL_ROW
operators (that result from a prior flattening of a LEFT JOIN) are updated
correctly.  Fix for ticket [cad1ab4cb7b0fc344].

FossilOrigin-Name: 92c178507df553e4f1110342c8f9b11b3ee37989e1d634fcaccabf657befa22f
2017-05-23 01:21:07 +00:00
dan
35175bf7ab If a query contains "FROM t1 LEFT JOIN t2, t3, t4", ensure that tables t3 and t4 are not scanned before t2. The trunk already does this.
FossilOrigin-Name: 0d9edfab9fb61322620f188b48ae2a1798a07581
2015-06-08 18:48:29 +00:00
drh
71607c77c8 SQLite has long accepted some unadvertised and non-standard join syntax.
Add a test to ensure that future versions continue to accept this non-standard
syntax, to avoid breaking legacy applications that use the undefined syntax.

FossilOrigin-Name: 824dde7fc48dbca6d6c956c4eb79bbfa2d139ee5
2014-07-18 17:39:48 +00:00
dan
a6eaa635cc Avoid running a couple of test cases that use realloc() extensively if SQLITE_MEMDEBUG is defined.
FossilOrigin-Name: e406d4bb786a34cdd5dd23ed39f35dfce3e1cabd
2014-03-05 19:13:32 +00:00
dan
13ef14afc1 Add tests to join.test to verify that it is not possible to use more than 64 tables in a join.
FossilOrigin-Name: ef1a998625e1fc051732228844388d6c59665382
2014-03-05 16:15:07 +00:00
dan
f7b0b0ad5f When generating WHERE clause terms internally for NATURAL and USING joins, identify the table by its position in the FROM list, not by its name or alias. Fix for [b73fb0bd64].
FossilOrigin-Name: 6fe6371175482d38ac4aeea994c7b20c18b7de01
2009-10-19 15:52:32 +00:00
danielk1977
bd1a0a4f61 Report an error if a USING or ON clause is specified following a from-list element that is not to the right of a join operator. Fix for #3846. (CVS 6832)
FossilOrigin-Name: 29b48972b65a17dab343b063a620cf8d456a923c
2009-07-01 16:12:07 +00:00
drh
da55c48a9a Expand table.* properly on a USING or a NATURAL join. Ticket #3522. (CVS 5979)
FossilOrigin-Name: 06d206ef7d5e433ccde347d63dfcd2177545e1fd
2008-12-05 00:00:07 +00:00
drh
2b300d5d6f Do not flatten the right term of a LEFT join. Ticket #3300. (CVS 5565)
FossilOrigin-Name: 8947c72f93d0b79c8061a3bfd5ab595edfb155a5
2008-08-14 00:19:48 +00:00
danielk1977
10235605ff Add a test case to join.test. No code changes, no bugs found. (CVS 5384)
FossilOrigin-Name: 417aa1580abec8cc335dcb6ac36245c081a31ef8
2008-07-09 14:47:21 +00:00
drh
a9671a22b3 Remove obsolete code from select.c, including the "affinity" parameter
to the sqlite3Select() module. (CVS 5380)

FossilOrigin-Name: cbd3c1585b7a8f8042aa1448fe1be87de056c41a
2008-07-08 23:40:20 +00:00
danielk1977
4b2688ab31 Modifications so that compilation and testing with the various OMIT macros defined works. (CVS 3276)
FossilOrigin-Name: c6ea353bd9f5fe6c9cc3349d2034ada283f9c80e
2006-06-20 11:01:07 +00:00
drh
41714d6f83 Allow WHERE clause terms on the left table of a LEFT OUTER JOIN to
contain aggregate subqueries.  Ticket #1697. (CVS 3117)

FossilOrigin-Name: a286e54e26f9a364127649eeea160d5fa8928faf
2006-03-02 04:44:23 +00:00