Commit Graph

55 Commits

Author SHA1 Message Date
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
drh
873fac0cae Avoid ambiguous column name errors when the column name is in the USING clause
of a join.  Ticket #743. (CVS 2495)

FossilOrigin-Name: 6a51bdeeff8312fa54fa2b1200f823428f35d605
2005-06-06 17:11:46 +00:00
drh
355ef36114 Naming the join columns in the result set of a natural join does not result
in an "ambiguous column name" error.  Ticket #1217. (CVS 2494)

FossilOrigin-Name: 265fb6079c0a4b7a93f063939436db494ff1d56c
2005-06-06 16:59:24 +00:00
drh
7bf5661068 Final preparations for the 3.1.0 alpha release. (CVS 2255)
FossilOrigin-Name: 17122c7e8e9cae36f1ffa7b42bd69d702980ac6a
2005-01-21 15:52:32 +00:00
danielk1977
e61b9f4f3c Modify test scripts to work when SQLITE_OMIT_SUBQUERY (but no other OMIT macros) is defined. (CVS 2252)
FossilOrigin-Name: d4e19efcef68d834c55c7d496cdb3696d63a31a2
2005-01-21 04:25:47 +00:00
danielk1977
3e8c37e7f8 Modify test scripts to work when SQLITE_OMIT_SUBQUERY (along with other OMIT macros) is defined. (CVS 2251)
FossilOrigin-Name: bb0254ab14417f0ab40f10f37cb63a60507f070a
2005-01-21 03:12:14 +00:00
drh
030530de9b Make sure USING and NATURAL work on joins even if the columns are renamed
using an AS phrase.  Ticket #523. (CVS 2233)

FossilOrigin-Name: c06add57bf0ce17503181b584ea4197b39b9e538
2005-01-18 17:40:04 +00:00
drh
47a6db2bfd Column names coming back from a SELECT are now just the name of the
source column without the "table." prefix. In other words,
"PRAGMA short_column_names=ON" is now the default.
This makes the names of columns behave more like other SQL engines.
The old behavior can be restored by setting "PRAGMA short_column_names=OFF". (CVS 2231)

FossilOrigin-Name: 9295050af1bf2d9d4dc63adc225a2848d67cbe17
2005-01-18 16:02:40 +00:00
danielk1977
27c774388a Ensure tests pass when SQLITE_OMIT_COMPOUND_SELECT is defined. (CVS 2138)
FossilOrigin-Name: 007be591b7829f9ff38e4b14ba5a5043796c2ff3
2004-11-22 13:35:41 +00:00
danielk1977
0fa8ddbdec Modify test suite to work when SQLITE_OMIT_VIEW is defined. (CVS 2132)
FossilOrigin-Name: 711e8d7695dfc74b3f1ee00591dcdda2cd7fc7d5
2004-11-22 08:43:32 +00:00
drh
dab3518880 Remove support for the Oracle8 outer join syntax. (CVS 1106)
FossilOrigin-Name: 824430b3ce435386b83ceb882f1510ac9f27d8fa
2003-09-27 13:39:38 +00:00
drh
3fc673e622 Do not flatten a subquery which is the right term of a LEFT OUTER JOIN
if the subquery contains a WHERE clause.  Ticket #350. (CVS 1027)

FossilOrigin-Name: dd8943e8583cf7ed3b662570a5607856fd246dac
2003-06-16 00:40:34 +00:00
drh
8af4d3ace6 Restrain the flattener in the presence of outer joins. Ticket #306. (CVS 958)
FossilOrigin-Name: 6d4b6597e560578253960d9876dc8c8657f41fef
2003-05-06 20:35:16 +00:00
drh
50cceb36b6 When the right table in a LEFT OUTER JOIN contains an INTEGER PRIMARY KEY
make sure that key is NULL if there is no row in the right table that
matches the current row in the left table.  Tickets #246 and #247. (CVS 873)

FossilOrigin-Name: 6a45fe3bd7e19cf9c20fc6cb65b0269cdd704490
2003-02-20 01:48:12 +00:00
drh
1f16230b5c Minimal support for oracle8 outer join syntax. (CVS 771)
FossilOrigin-Name: 31df3690d0fe4bd4a293cbe8ca9a26c98c3ed3ce
2002-10-27 19:35:33 +00:00
drh
c8f8b632c3 Fix a bug in the left outer join logic. (CVS 758)
FossilOrigin-Name: 6c0f44bd6374010f7a4a091e585eb36e0665f96f
2002-09-30 12:36:26 +00:00