Commit graph

192 commits

Author SHA1 Message Date
cmiller@zippy.cornsilk.net
15dcc14c8f Merge zippy.cornsilk.net:/home/cmiller/work/mysql/bug36570/my50-bug36570
into  zippy.cornsilk.net:/home/cmiller/work/mysql/bug36570/my51-bug36570
2008-05-15 19:45:42 -04:00
cmiller@zippy.cornsilk.net
573828aa6a Bug#36570: Parse error of CREATE PROCEDURE stmt with comments on \
slave

The stored-routine code took the contents of the (lowest) parser
and copied it directly to the binlog, which causes problems if there
is a special case of interpretation at the parser level -- which 
there is, in the "/*!VER */" comments.  The trailing "*/" caused
errors on the slave, naturally.

Now, since by that point we have /properly/ created parse-tree (as 
the rest of the server should do!) for the stored-routine CREATE, we
can construct a perfect statement from that information, instead of
writing uncertain information from an unknown parser state.  
Fortunately, there's already a function nearby that does exactly 
that.
---
Update for Bug#36570.  Qualify routine names with db name when
writing to the binlog ONLY if the source text is qualified.
2008-05-15 19:13:24 -04:00
tsmith@ramayana.hindu.god
3906cc27d7 Merge ramayana.hindu.god:/home/tsmith/m/bk/51
into  ramayana.hindu.god:/home/tsmith/m/bk/maint/51
2008-01-25 10:37:29 -07:00
malff@lambda.hsd1.co.comcast.net.
ec6c4fad89 Merge lambda.hsd1.co.comcast.net.:/home/malff/TREE/mysql-5.0-33618
into  lambda.hsd1.co.comcast.net.:/home/malff/TREE/mysql-5.1-33618
2008-01-23 15:36:57 -07:00
malff@lambda.hsd1.co.comcast.net.
c3ad0cac75 Bug#33618 (Crash in sp_rcontext)
Bug 33983 (Stored Procedures: wrong end <label> syntax is accepted)

The server used to crash when REPEAT or another control instruction
was used in conjunction with labels and a LEAVE instruction.

The crash was caused by a missing "pop" of handlers or cursors in the
code representing the stored program. When executing the code in a loop,
this missing "pop" would result in a stack overflow, corrupting memory.

Code generation has been fixed to produce the missing h_pop/c_pop
instructions.

Also, the logic checking that labels at the beginning and the end of a
statement are matched was incorrect, causing Bug 33983.
End labels, when used, must match the label used at the beginning of a block.
2008-01-23 13:26:41 -07:00
cmiller@zippy.cornsilk.net
c940d64a69 Merge bk-internal.mysql.com:/home/bk/mysql-5.1-maint
into  zippy.cornsilk.net:/home/cmiller/work/mysql/mysql-5.1-maint
2007-12-14 10:52:10 -05:00
thek@adventure.(none)
13be72f9c3 Merge adventure.(none):/home/thek/Development/cpp/bug31153/my51-bug31153
into  adventure.(none):/home/thek/Development/cpp/mysql-5.1-runtime
2007-11-21 20:57:28 +01:00
thek@adventure.(none)
7580f9043a Merge adventure.(none):/home/thek/Development/cpp/bug31153/my50-bug31153
into  adventure.(none):/home/thek/Development/cpp/bug31153/my51-bug31153
2007-11-21 11:25:17 +01:00
thek@adventure.(none)
0b38e67453 Merge adventure.(none):/home/thek/Development/cpp/bug31153/my50-bug31153
into  adventure.(none):/home/thek/Development/cpp/mysql-5.0-runtime
2007-11-21 10:02:44 +01:00
thek@adventure.(none)
1794242b24 Bug #31153 calling stored procedure crashes server if available memory is low
When the server was out of memory it crashed because of invalid memory access.

This patch adds detection for failed memory allocations and make the server
output a proper error message.
2007-11-19 17:59:44 +01:00
malff@lambda.hsd1.co.comcast.net.
a4c66084a8 Merge lambda.hsd1.co.comcast.net.:/home/malff/TREE/mysql-5.0-runtime
into  lambda.hsd1.co.comcast.net.:/home/malff/TREE/mysql-5.1-rt-merge
2007-10-16 15:43:16 -06:00
cmiller@zippy.cornsilk.net
5a19f077b3 Doxygenize comments. 2007-10-16 16:11:50 -04:00
davi@virtua-cwbas201-21-158-74.ctb.virtua.com.br
fd3c6b1855 Bug#28318 CREATE FUNCTION (UDF) requires a schema
Bug#29816 Syntactically wrong query fails with misleading error message

The core problem is that an SQL-invoked function name can be a <schema
qualified routine name> that contains no <schema name>, but the mysql
parser insists that all stored procedures (function, procedures and
triggers) must have a <schema name>, which is not true for functions.
This problem is especially visible when trying to create a function
or when a query contains a syntax error after a function call (in the
same query), both will fail with a "No database selected" message if
the session is not attached to a particular schema, but the first
one should succeed and the second fail with a "syntax error" message.

Part of the fix is to revamp the sp name handling so that a schema
name may be omitted for functions -- this means that the internal
function name representation may not have a dot, which represents
that the function doesn't have a schema name. The other part is
to place schema checks after the type (function, trigger or procedure)
of the routine is known.
2007-10-09 20:46:33 -03:00
kostja@bodhi.(none)
2537a87fb5 Provide initial module structure to Doxygen. 2007-08-15 19:08:44 +04:00
kostja@bodhi.(none)
91fe15bb9d Fix doxygen warnings. 2007-08-15 17:43:08 +04:00
kostja@bodhi.(none)
88fc7cab26 Merge bodhi.(none):/opt/local/work/mysql-5.0-runtime
into  bodhi.(none):/opt/local/work/mysql-5.1-runtime
2007-07-16 23:31:36 +04:00
kostja@bodhi.(none)
5ab4b6f1ac A fix and a test case for Bug#26141 mixing table types in trigger
causes full table lock on innodb table.
Also fixes Bug#28502 Triggers that update another innodb table 
will block on X lock unnecessarily (duplciate).
Code review fixes.

Both bugs' synopses are misleading: InnoDB table is
not X locked. The statements, however, cannot proceed concurrently, 
but this happens due to lock conflicts for tables used in triggers,
not for the InnoDB table. 

If a user had an InnoDB table, and two triggers, AFTER UPDATE and 
AFTER INSERT, competing for different resources (e.g. two distinct
MyISAM tables), then these two triggers would not be able to execute
concurrently. Moreover, INSERTS/UPDATES of the InnoDB table would
not be able to run concurrently. 
The problem had other side-effects (see respective bug reports).

This behavior was a consequence of a shortcoming of the pre-locking
algorithm, which would not distinguish between different DML operations
(e.g. INSERT and DELETE) and pre-lock all the tables
that are used by any trigger defined on the subject table.

The idea of the fix is to extend the pre-locking algorithm to keep track,
for each table, what DML operation it is used for and not
load triggers that are known to never be fired.
2007-07-12 22:26:41 +04:00
anozdrin/alik@ibm.
9fae9ef66f Patch for the following bugs:
- BUG#11986: Stored routines and triggers can fail if the code
    has a non-ascii symbol
  - BUG#16291: mysqldump corrupts string-constants with non-ascii-chars
  - BUG#19443: INFORMATION_SCHEMA does not support charsets properly
  - BUG#21249: Character set of SP-var can be ignored
  - BUG#25212: Character set of string constant is ignored (stored routines)
  - BUG#25221: Character set of string constant is ignored (triggers)

There were a few general problems that caused these bugs:
1. Character set information of the original (definition) query for views,
   triggers, stored routines and events was lost.
2. mysqldump output query in client character set, which can be
   inappropriate to encode definition-query.
3. INFORMATION_SCHEMA used strings with mixed encodings to display object
   definition;

1. No query-definition-character set.

In order to compile query into execution code, some extra data (such as
environment variables or the database character set) is used. The problem
here was that this context was not preserved. So, on the next load it can
differ from the original one, thus the result will be different.

The context contains the following data:
  - client character set;
  - connection collation (character set and collation);
  - collation of the owner database;

The fix is to store this context and use it each time we parse (compile)
and execute the object (stored routine, trigger, ...).

2. Wrong mysqldump-output.

The original query can contain several encodings (by means of character set
introducers). The problem here was that we tried to convert original query
to the mysqldump-client character set.

Moreover, we stored queries in different character sets for different
objects (views, for one, used UTF8, triggers used original character set).

The solution is
  - to store definition queries in the original character set;
  - to change SHOW CREATE statement to output definition query in the
    binary character set (i.e. without any conversion);
  - introduce SHOW CREATE TRIGGER statement;
  - to dump special statements to switch the context to the original one
    before dumping and restore it afterwards.

Note, in order to preserve the database collation at the creation time,
additional ALTER DATABASE might be used (to temporary switch the database
collation back to the original value). In this case, ALTER DATABASE
privilege will be required. This is a backward-incompatible change.

3. INFORMATION_SCHEMA showed non-UTF8 strings

The fix is to generate UTF8-query during the parsing, store it in the object
and show it in the INFORMATION_SCHEMA.

Basically, the idea is to create a copy of the original query convert it to
UTF8. Character set introducers are removed and all text literals are
converted to UTF8.

This UTF8 query is intended to provide user-readable output. It must not be
used to recreate the object.  Specialized SHOW CREATE statements should be
used for this.

The reason for this limitation is the following: the original query can
contain symbols from several character sets (by means of character set
introducers).

Example:

  - original query:
    CREATE VIEW v1 AS SELECT _cp1251 'Hello' AS c1;

  - UTF8 query (for INFORMATION_SCHEMA):
    CREATE VIEW v1 AS SELECT 'Hello' AS c1;
2007-06-28 21:34:54 +04:00
lars/lthalmann@dl145k.mysql.com
5c667b6fa5 Merge mysql.com:/nfsdisk1/lars/bk/mysql-5.1
into  mysql.com:/nfsdisk1/lars/bk/mysql-5.1-new-rpl
2007-06-21 17:13:02 +02:00
anozdrin/alik@ibm.
754e9788b8 Part of patch for BUG#11986: make sp_head::m_body_begin pointer
private and provide a setter for it. The setter will be used to
construct UTF-query in the following patches.
2007-06-14 22:14:52 +04:00
anozdrin/alik@ibm.
62e3e46275 This the 4-th patch in scope of CS patch (BUG#11986).
The patch contains the following changes:
  - Introduce auxilary functions to convenient work with character sets:
    - resolve_charset();
    - resolve_collation();
    - get_default_db_collation();
  - Introduce lex_string_set();
  - Refactor Table_trigger_list::process_triggers() &
    sp_head::execute_trigger() to be consistent with other code;
  - Move reusable code from add_table_for_trigger() into
    build_trn_path(), check_trn_exists() and load_table_name_for_trigger()
    to be used in the following patch.
  - Rename triggers_file_ext and trigname_file_ext into TRN_EXT and
    TRG_EXT respectively.
2007-06-14 19:23:55 +04:00
kostja@bodhi.(none)
6c352d16d9 Follow up after work on Bug 4968
Coding style: classes start with a capital letter.
Rename some classes related to parsing:
create_field -> Create_field
foreign_key -> Foreign_key
key_part_spec -> Key_part_spec
2007-06-10 14:43:57 +04:00
lars/lthalmann@dl145h.mysql.com
98501127fb Merge mysql.com:/nfsdisk1/lars/bkroot/mysql-5.1-new-rpl
into  mysql.com:/nfsdisk1/lars/MERGE/mysql-5.1-merge
2007-06-05 01:15:07 +02:00
kostja@bodhi.(none)
16633169e4 Merge bk-internal.mysql.com:/home/bk/mysql-5.1
into  bodhi.(none):/opt/local/work/mysql-5.1-runtime
2007-06-01 12:12:06 +04:00
mats@kindahl-laptop.dnsalias.net
5c662d6d7c Merge mkindahl@bk-internal.mysql.com:/home/bk/mysql-5.1-new-rpl
into  kindahl-laptop.dnsalias.net:/home/bk/b28618-mysql-5.1-rpl
2007-05-31 20:17:31 +02:00
anozdrin/alik@ibm.
7d02017cb6 Refactoring patch:
1. Refactor sp_show_create_function() and sp_show_create_procedure()
  into show_create_routine().
2. Code cleanup: eliminate proxy functions.
2007-05-29 21:22:33 +04:00
jani@a88-113-38-195.elisa-laajakaista.fi
fc3b3a0a86 Merge jamppa@bk-internal.mysql.com:/home/bk/mysql-5.1
into  a88-113-38-195.elisa-laajakaista.fi:/home/my/bk/mysql-5.1-marvel
2007-05-24 13:24:36 +03:00
mats@romeo.kindahl.net
6a7925a262 WL#3339 (Issue warnings when statement-based replication may fail):
Replacing binlog_row_based_if_mixed with variable binlog_stmt_flags
holding several flags and adding member functions to manipulate the
flags.

Added code to generate a warning when an attempt to log an unsafe
statement to the binary log was made. The warning is both pushed to the
SHOW WARNINGS table and written to the error log. The prevent flooding
the error log, the warning is just written to the error log once per
open session.
2007-05-14 14:45:38 +02:00
monty@mysql.com/narttu.mysql.fi
088e2395f1 WL#3817: Simplify string / memory area types and make things more consistent (first part)
The following type conversions was done:

- Changed byte to uchar
- Changed gptr to uchar*
- Change my_string to char *
- Change my_size_t to size_t
- Change size_s to size_t

Removed declaration of byte, gptr, my_string, my_size_t and size_s. 

Following function parameter changes was done:
- All string functions in mysys/strings was changed to use size_t
  instead of uint for string lengths.
- All read()/write() functions changed to use size_t (including vio).
- All protocoll functions changed to use size_t instead of uint
- Functions that used a pointer to a string length was changed to use size_t*
- Changed malloc(), free() and related functions from using gptr to use void *
  as this requires fewer casts in the code and is more in line with how the
  standard functions work.
- Added extra length argument to dirname_part() to return the length of the
  created string.
- Changed (at least) following functions to take uchar* as argument:
  - db_dump()
  - my_net_write()
  - net_write_command()
  - net_store_data()
  - DBUG_DUMP()
  - decimal2bin() & bin2decimal()
- Changed my_compress() and my_uncompress() to use size_t. Changed one
  argument to my_uncompress() from a pointer to a value as we only return
  one value (makes function easier to use).
- Changed type of 'pack_data' argument to packfrm() to avoid casts.
- Changed in readfrm() and writefrom(), ha_discover and handler::discover()
  the type for argument 'frmdata' to uchar** to avoid casts.
- Changed most Field functions to use uchar* instead of char* (reduced a lot of
  casts).
- Changed field->val_xxx(xxx, new_ptr) to take const pointers.

Other changes:
- Removed a lot of not needed casts
- Added a few new cast required by other changes
- Added some cast to my_multi_malloc() arguments for safety (as string lengths
  needs to be uint, not size_t).
- Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done
  explicitely as this conflict was often hided by casting the function to
  hash_get_key).
- Changed some buffers to memory regions to uchar* to avoid casts.
- Changed some string lengths from uint to size_t.
- Changed field->ptr to be uchar* instead of char*. This allowed us to
  get rid of a lot of casts.
- Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar
- Include zlib.h in some files as we needed declaration of crc32()
- Changed MY_FILE_ERROR to be (size_t) -1.
- Changed many variables to hold the result of my_read() / my_write() to be
  size_t. This was needed to properly detect errors (which are
  returned as (size_t) -1).
- Removed some very old VMS code
- Changed packfrm()/unpackfrm() to not be depending on uint size
  (portability fix)
- Removed windows specific code to restore cursor position as this
  causes slowdown on windows and we should not mix read() and pread()
  calls anyway as this is not thread safe. Updated function comment to
  reflect this. Changed function that depended on original behavior of
  my_pwrite() to itself restore the cursor position (one such case).
- Added some missing checking of return value of malloc().
- Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow.
- Changed type of table_def::m_size from my_size_t to ulong to reflect that
  m_size is the number of elements in the array, not a string/memory
  length.
- Moved THD::max_row_length() to table.cc (as it's not depending on THD).
  Inlined max_row_length_blob() into this function.
- More function comments
- Fixed some compiler warnings when compiled without partitions.
- Removed setting of LEX_STRING() arguments in declaration (portability fix).
- Some trivial indentation/variable name changes.
- Some trivial code simplifications:
  - Replaced some calls to alloc_root + memcpy to use
    strmake_root()/strdup_root().
  - Changed some calls from memdup() to strmake() (Safety fix)
  - Simpler loops in client-simple.c
2007-05-10 12:59:39 +03:00
thek@adventure.(none)
62a34703c4 Merge adventure.(none):/home/thek/Development/cpp/bug26977/my50-bug26977
into  adventure.(none):/home/thek/Development/cpp/bug26977/my51-bug26977
2007-05-07 10:26:20 +02:00
thek@adventure.(none)
ae10d3d9e2 Bug#26977 exception handlers never hreturn
- In some cases, flow control optimization implemented in sp::optimize
    removes hreturn instructions, causing SQL exception handlers to:
      * never return
      * execute wrong logic
  - This patch overrides default short cut optimization on hreturn instructions
    to avoid this problem.
2007-05-07 10:23:10 +02:00
malff/marcsql@weblab.(none)
62fd6aa696 manual merge 5.0-runtime -> 5.1->runtime, with 25411 part I 2007-04-25 21:38:12 -06:00
malff/marcsql@weblab.(none)
fc809c70cc Bug#25411 (trigger code truncated), PART I
The issue found with bug 25411 is due to the function skip_rear_comments()
which damages the source code while implementing a work around.
The root cause of the problem is in the lexical analyser, which does not
process special comments properly.
For special comments like :
[1] aaa /*!50000 bbb */ ccc
since 5.0 is a version older that the current code, the parser is in lining
the content of the special comment, so that the query to process is
[2] aaa bbb ccc
However, the text of the query captured when processing a stored procedure,
stored function or trigger (or event in 5.1), can be after rebuilding it:
[3] aaa bbb */ ccc
which is wrong.

To fix bug 25411 properly, the lexical analyser needs to return [2] when
in lining special comments.
In order to implement this, some preliminary cleanup is required in the code,
which is implemented by this patch.

Before this change, the structure named LEX (or st_lex) contains attributes
that belong to lexical analysis, as well as attributes that represents the
abstract syntax tree (AST) of a statement.
Creating a new LEX structure for each statements (which makes sense for the
AST part) also re-initialized the lexical analysis phase each time, which
is conceptually wrong.

With this patch, the previous st_lex structure has been split in two:
- st_lex represents the Abstract Syntax Tree for a statement. The name "lex"
has not been changed to avoid a bigger impact in the code base.
- class lex_input_stream represents the internal state of the lexical
  analyser, which by definition should *not* be reinitialized when parsing
  multiple statements from the same input stream.

This change is a pre-requisite for bug 25411, since the implementation of
lex_input_stream will later improve to deal properly with special comments,
and this processing can not be done with the current implementation of
sp_head::reset_lex and sp_head::restore_lex, which interfere with the lexer.

This change set alone does not fix bug 25411.
2007-04-24 09:24:21 -06:00
holyfoot/hf@hfmain.(none)
ce426dd622 Merge bk@192.168.21.1:mysql-5.1-opt
into  mysql.com:/d2/hf/mrg/mysql-5.1-opt
2007-04-07 14:59:53 +05:00
tsmith@quadxeon.mysql.com
2117193783 Merge tsmith@bk-internal.mysql.com:/home/bk/mysql-5.1
into  quadxeon.mysql.com:/benchmarks/ext3/TOSAVE/tsmith/bk/maint/mrg04/51
2007-04-04 11:05:22 +02:00
gluh@mysql.com/eagle.(none)
2d47f0cb1b Bug#21432 Database/Table name limited to 64 bytes, not chars, problems with multi-byte 2007-04-03 16:13:27 +05:00
iggy@recycle.(none)
6a9811b594 Merge bk-internal.mysql.com:/home/bk/mysql-5.0-maint
into  recycle.(none):/src/bug23491/my50-bug23491
2007-03-29 12:20:13 -04:00
iggy@recycle.(none)
c90400c538 Merge bk-internal.mysql.com:/home/bk/mysql-5.1-new-maint
into  recycle.(none):/src/bug23491/my51-bug23491
2007-03-29 12:13:42 -04:00
iggy@recycle.(none)
4f498dec3a Merge recycle.(none):/src/bug23491/my50-bug23491
into  recycle.(none):/src/bug23491/my51-bug23491
2007-03-29 10:42:03 -04:00
kostja@bodhi.local
b42b416400 Remove unnecessary casts to uchar. The casts are stemming from
the lexer API which internally uses  unsigned char variables to
address its state map. The implementation of the lexer should be
internal to the lexer, and not influence the rest of the code.
2007-03-27 21:09:56 +04:00
iggy@recycle.(none)
bbc38df090 Bug#23491 MySQLDump prefix function call in a view by database name
- mysqldump executes a SHOW CREATE VIEW statement to generate the text
that it outputs.  When the function name is retrieved it's database 
name is unconditionally prepended.  This change causes the function's 
database name to be prepended only when it was used to define the 
function.
2007-03-27 12:31:44 -04:00
malff/marcsql@weblab.(none)
62247621b0 Merge weblab.(none):/home/marcsql/TREE/mysql-5.0-runtime
into  weblab.(none):/home/marcsql/TREE/mysql-5.1-26503-merge
2007-03-14 15:36:11 -06:00
malff/marcsql@weblab.(none)
bef323b1d6 Bug#26503 (Illegal SQL exception handler code causes the server to crash)
Before this fix, the parser would accept illegal code in SQL exceptions
handlers, that later causes the runtime to crash when executing the code,
due to memory violations in the exception handler stack.

The root cause of the problem is instructions within an exception handler
that jumps to code located outside of the handler. This is illegal according
to the SQL 2003 standard, since labels located outside the handler are not
supposed to be visible (they are "out of scope"), so any instruction that
jumps to these labels, like ITERATE or LEAVE, should not parse.

The section of the standard that is relevant for this is :
  SQL:2003 SQL/PSM (ISO/IEC 9075-4:2003)
  section 13.1 <compound statement>,
  syntax rule 4
<quote>
  The scope of the <beginning label> is CS excluding every <SQL schema
  statement> contained in CS and excluding every
  <local handler declaration list> contained in CS. <beginning label> shall
  not be equivalent to any other <beginning label>s within that scope.
</quote>

With this fix, the C++ class sp_pcontext, which represent the "parsing
context" tree (a.k.a symbol table) of a stored procedure, has been changed
as follows:
- constructors have been cleaned up, so that only building a root node for
the tree is public; building nodes inside a tree is not public.
- a new member, m_label_scope, indicates if a given syntactic context
belongs to a DECLARE HANDLER block,
- label resolution, in the method find_label(), has been changed to
implement the restriction of scope regarding labels used in a compound
statement.

The actions in the parser, when parsing the body of a SQL exception handler,
have been changed as follows:
- the implementation of an exception handler (DECLARE HANDLER) now creates
explicitly a new sp_pcontext, to isolate the code inside the handler from
the containing compound statement context.
- registering exception handlers as a result occurs in the parent context,
see the rule sp_hcond_element
- the code in sp_hcond_list has been cleaned up, to avoid code duplication

In addition, the flags IN_SIMPLE_CASE and IN_HANDLER, declared in sp_head.h
have been removed, since they are unused and broken by design (as seen with
Bug 19194 (Right recursion in parser for CASE causes excessive stack usage,
limitation), representing a stack in a single flag is not possible.

Tests in sp-error have been added to show that illegal constructs are now
rejected.

Tests in sp have been added for code coverage, to show that ITERATE or LEAVE
statements are legal when jumping to a label in scope, inside the body of
an exception handler.
2007-03-14 12:02:32 -06:00
malff/marcsql@weblab.(none)
77f73c83b8 Merge weblab.(none):/home/marcsql/TREE/mysql-5.0-runtime
into  weblab.(none):/home/marcsql/TREE/mysql-5.1-8407-cleanup
2007-03-07 11:25:20 -07:00
malff/marcsql@weblab.(none)
1f91f47d5e Bug 8407, post review cleanup: use instr::get_cont_dest() to get the instruction
continuation instruction, for CONTINUE exception handlers.
2007-03-07 09:53:46 -07:00
malff/marcsql@weblab.(none)
9f0b0df961 Merge malff@bk-internal.mysql.com:/home/bk/mysql-5.0-runtime
into  weblab.(none):/home/marcsql/TREE/mysql-5.0-8407_b
2007-03-06 11:30:08 -07:00
malff/marcsql@weblab.(none)
8643745d3e Merge weblab.(none):/home/marcsql/TREE/mysql-5.0-8407_b
into  weblab.(none):/home/marcsql/TREE/mysql-5.1-8407-merge
2007-03-06 10:33:10 -07:00
malff/marcsql@weblab.(none)
b216d959bb Bug#8407 (Stored functions/triggers ignore exception handler)
Bug 18914 (Calling certain SPs from triggers fail)
Bug 20713 (Functions will not not continue for SQLSTATE VALUE '42S02')
Bug 21825 (Incorrect message error deleting records in a table with a
  trigger for inserting)
Bug 22580 (DROP TABLE in nested stored procedure causes strange dependency
  error)
Bug 25345 (Cursors from Functions)


This fix resolves a long standing issue originally reported with bug 8407,
which affect the behavior of Stored Procedures, Stored Functions and Trigger
in many different ways, causing symptoms reported by all the bugs listed.
In all cases, the root cause of the problem traces back to 8407 and how the
server locks tables involved with sub statements.

Prior to this fix, the implementation of stored routines would:
- compute the transitive closure of all the tables referenced by a top level
statement
- open and lock all the tables involved
- execute the top level statement
"transitive closure of tables" means collecting:
- all the tables,
- all the stored functions,
- all the views,
- all the table triggers
- all the stored procedures
involved, and recursively inspect these objects definition to find more
references to more objects, until the list of every object referenced does
not grow any more.
This mechanism is known as "pre-locking" tables before execution.
The motivation for locking all the tables (possibly) used at once is to
prevent dead locks.

One problem with this approach is that, if the execution path the code
really takes during runtime does not use a given table, and if the table is
missing, the server would not execute the statement.
This in particular has a major impact on triggers, since a missing table
referenced by an update/delete trigger would prevent an insert trigger to run.

Another problem is that stored routines might define SQL exception handlers
to deal with missing tables, but the server implementation would never give
user code a chance to execute this logic, since the routine is never
executed when a missing table cause the pre-locking code to fail.

With this fix, the internal implementation of the pre-locking code has been
relaxed of some constraints, so that failure to open a table does not
necessarily prevent execution of a stored routine.

In particular, the pre-locking mechanism is now behaving as follows:

1) the first step, to compute the transitive closure of all the tables
possibly referenced by a statement, is unchanged.

2) the next step, which is to open all the tables involved, only attempts
to open the tables added by the pre-locking code, but silently fails without
reporting any error or invoking any exception handler is the table is not
present. This is achieved by trapping internal errors with
Prelock_error_handler

3) the locking step only locks tables that were successfully opened.

4) when executing sub statements, the list of tables used by each statements
is evaluated as before. The tables needed by the sub statement are expected
to be already opened and locked. Statement referencing tables that were not
opened in step 2) will fail to find the table in the open list, and only at
this point will execution of the user code fail.

5) when a runtime exception is raised at 4), the instruction continuation
destination (the next instruction to execute in case of SQL continue
handlers) is evaluated.
This is achieved with sp_instr::exec_open_and_lock_tables()

6) if a user exception handler is present in the stored routine, that
handler is invoked as usual, so that ER_NO_SUCH_TABLE exceptions can be
trapped by stored routines. If no handler exists, then the runtime execution
will fail as expected.

With all these changes, a side effect is that view security is impacted, in
two different ways.

First, a view defined as "select stored_function()", where the stored
function references a table that may not exist, is considered valid.
The rationale is that, because the stored function might trap exceptions
during execution and still return a valid result, there is no way to decide
when the view is created if a missing table really cause the view to be invalid.

Secondly, testing for existence of tables is now done later during
execution. View security, which consist of trapping errors and return a
generic ER_VIEW_INVALID (to prevent disclosing information) was only
implemented at very specific phases covering *opening* tables, but not
covering the runtime execution. Because of this existing limitation,
errors that were previously trapped and converted into ER_VIEW_INVALID are
not trapped, causing table names to be reported to the user.
This change is exposing an existing problem, which is independent and will
be resolved separately.
2007-03-05 19:42:07 -07:00
monty@narttu.mysql.fi
8a80e36ac3 Merge mysql.com:/home/my/mysql-5.0
into  mysql.com:/home/my/mysql-5.1
Merge of 'remove compiler warnings when using -Wshadow'
2007-01-27 03:46:45 +02:00
monty@mysql.com/narttu.mysql.fi
a04157fbb3 Merge bk-internal.mysql.com:/home/bk/mysql-5.0
into  mysql.com:/home/my/mysql-5.0
2007-01-22 14:04:40 +02:00