From c6a395af0962a8044db6922d1af794e771818c9b Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Wed, 15 Aug 2007 18:10:16 -0600 Subject: [PATCH 01/35] Bug#23062 (GCOV build: helper scripts missing in the BUILD directory) This patch provides compile helper scripts only, no server logic is affected. Before this patch, GCOV and GPROF build scripts were only provided for pentium platforms. With this patch, pentium, pentium64 and amd64 platforms have associated helper build scripts. The GCOV and GPROF specific compilation flags are set once in SETUP.sh, to avoid code duplication. --- BUILD/SETUP.sh | 25 +++++++++++++++++++++++++ BUILD/compile-amd64-gcov | 17 +++++++++++++++++ BUILD/compile-amd64-gprof | 9 +++++++++ BUILD/compile-pentium-gcov | 13 +++---------- BUILD/compile-pentium-gprof | 4 ++-- BUILD/compile-pentium64-gcov | 17 +++++++++++++++++ BUILD/compile-pentium64-gprof | 9 +++++++++ 7 files changed, 82 insertions(+), 12 deletions(-) create mode 100755 BUILD/compile-amd64-gcov create mode 100755 BUILD/compile-amd64-gprof create mode 100755 BUILD/compile-pentium64-gcov create mode 100755 BUILD/compile-pentium64-gprof diff --git a/BUILD/SETUP.sh b/BUILD/SETUP.sh index 10324c22e56..a8efaf75db4 100755 --- a/BUILD/SETUP.sh +++ b/BUILD/SETUP.sh @@ -138,3 +138,28 @@ then echo "$CC" | grep "ccache" > /dev/null || CC="ccache $CC" echo "$CXX" | grep "ccache" > /dev/null || CXX="ccache $CXX" fi + +# gcov + +# The -fprofile-arcs and -ftest-coverage options cause GCC to instrument the +# code with profiling information used by gcov. +# The -DDISABLE_TAO_ASM is needed to avoid build failures in Yassl. +# The -DHAVE_gcov enables code to write out coverage info even when crashing. + +gcov_compile_flags="-fprofile-arcs -ftest-coverage" +gcov_compile_flags="$gcov_compile_flags -DDISABLE_TAO_ASM" +gcov_compile_flags="$gcov_compile_flags -DMYSQL_SERVER_SUFFIX=-gcov -DHAVE_gcov" + +# GCC4 needs -fprofile-arcs -ftest-coverage on the linker command line (as well +# as on the compiler command line), and this requires setting LDFLAGS for BDB. + +gcov_link_flags="-fprofile-arcs -ftest-coverage" + +gcov_configs="--disable-shared $static_link" + +# gprof + +gprof_compile_flags="-O2 -pg -g" + +gprof_link_flags="--disable-shared $static_link" + diff --git a/BUILD/compile-amd64-gcov b/BUILD/compile-amd64-gcov new file mode 100755 index 00000000000..239a4aed0fb --- /dev/null +++ b/BUILD/compile-amd64-gcov @@ -0,0 +1,17 @@ +#! /bin/sh + +path=`dirname $0` +. "$path/SETUP.sh" + +# Need to disable ccache, or we loose the gcov-needed compiler output files. +CCACHE_DISABLE=1 +export CCACHE_DISABLE + +export LDFLAGS="$gcov_link_flags" + +extra_flags="$amd64_cflags $debug_cflags $max_cflags $gcov_compile_flags" +c_warnings="$c_warnings $debug_extra_warnings" +cxx_warnings="$cxx_warnings $debug_extra_warnings" +extra_configs="$amd64_configs $debug_configs $gcov_configs $max_configs" + +. "$path/FINISH.sh" diff --git a/BUILD/compile-amd64-gprof b/BUILD/compile-amd64-gprof new file mode 100755 index 00000000000..6cfb8a4302c --- /dev/null +++ b/BUILD/compile-amd64-gprof @@ -0,0 +1,9 @@ +#! /bin/sh + +path=`dirname $0` +. "$path/SETUP.sh" + +extra_flags="$amd64_cflags $gprof_compile_flags" +extra_configs="$amd64_configs $debug_configs $gprof_link_flags" + +. "$path/FINISH.sh" diff --git a/BUILD/compile-pentium-gcov b/BUILD/compile-pentium-gcov index 0d561d5b147..d4878dc591e 100755 --- a/BUILD/compile-pentium-gcov +++ b/BUILD/compile-pentium-gcov @@ -7,18 +7,11 @@ path=`dirname $0` CCACHE_DISABLE=1 export CCACHE_DISABLE -# GCC4 needs -fprofile-arcs -ftest-coverage on the linker command line (as well -# as on the compiler command line), and this requires setting LDFLAGS for BDB. -export LDFLAGS="-fprofile-arcs -ftest-coverage" +export LDFLAGS="$gcov_link_flags" -# The -fprofile-arcs and -ftest-coverage options cause GCC to instrument the -# code with profiling information used by gcov. -# The -DDISABLE_TAO_ASM is needed to avoid build failures in Yassl. -# The -DHAVE_gcov enables code to write out coverage info even when crashing. -extra_flags="$pentium_cflags -fprofile-arcs -ftest-coverage -DDISABLE_TAO_ASM $debug_cflags $max_cflags -DMYSQL_SERVER_SUFFIX=-gcov -DHAVE_gcov" +extra_flags="$pentium_cflags $debug_cflags $max_cflags $gcov_compile_flags" c_warnings="$c_warnings $debug_extra_warnings" cxx_warnings="$cxx_warnings $debug_extra_warnings" -extra_configs="$pentium_configs $debug_configs --disable-shared $static_link" -extra_configs="$extra_configs $max_configs" +extra_configs="$pentium_configs $debug_configs $gcov_configs $max_configs" . "$path/FINISH.sh" diff --git a/BUILD/compile-pentium-gprof b/BUILD/compile-pentium-gprof index aa74de0b1b2..4aebc1d2e02 100755 --- a/BUILD/compile-pentium-gprof +++ b/BUILD/compile-pentium-gprof @@ -3,7 +3,7 @@ path=`dirname $0` . "$path/SETUP.sh" -extra_flags="$pentium_cflags -O2 -pg -g" -extra_configs="$pentium_configs $debug_configs --disable-shared $static_link" +extra_flags="$pentium_cflags $gprof_compile_flags" +extra_configs="$pentium_configs $debug_configs $gprof_link_flags" . "$path/FINISH.sh" diff --git a/BUILD/compile-pentium64-gcov b/BUILD/compile-pentium64-gcov new file mode 100755 index 00000000000..5a99b7f8796 --- /dev/null +++ b/BUILD/compile-pentium64-gcov @@ -0,0 +1,17 @@ +#! /bin/sh + +path=`dirname $0` +. "$path/SETUP.sh" + +# Need to disable ccache, or we loose the gcov-needed compiler output files. +CCACHE_DISABLE=1 +export CCACHE_DISABLE + +export LDFLAGS="$gcov_link_flags" + +extra_flags="$pentium64_cflags $debug_cflags $max_cflags $gcov_compile_flags" +c_warnings="$c_warnings $debug_extra_warnings" +cxx_warnings="$cxx_warnings $debug_extra_warnings" +extra_configs="$pentium64_configs $debug_configs $gcov_configs $max_configs" + +. "$path/FINISH.sh" diff --git a/BUILD/compile-pentium64-gprof b/BUILD/compile-pentium64-gprof new file mode 100755 index 00000000000..f64dee6d196 --- /dev/null +++ b/BUILD/compile-pentium64-gprof @@ -0,0 +1,9 @@ +#! /bin/sh + +path=`dirname $0` +. "$path/SETUP.sh" + +extra_flags="$pentium64_cflags $gprof_compile_flags" +extra_configs="$pentium64_configs $debug_configs $gprof_link_flags" + +. "$path/FINISH.sh" From 82f99c9359c015f2c373797759653aed75b98834 Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Wed, 22 Aug 2007 11:05:35 -0600 Subject: [PATCH 02/35] Bug#30237 (Performance regression in boolean expressions) This is a performance bug, related to the parsing or 'OR' and 'AND' boolean expressions. Let N be the number of expressions involved in a OR (respectively AND). When N=1 For example, "select 1" involve only 1 term: there is no OR operator. In 4.0 and 4.1, parsing expressions not involving OR had no overhead. In 5.0, parsing adds some overhead, with Select->expr_list. With this patch, the overhead introduced in 5.0 has been removed, so that performances for N=1 should be identical to the 4.0 performances, which are optimal (there is no code executed at all) The overhead in 5.0 was in fact affecting significantly some operations. For example, loading 1 Million rows into a table with INSERTs, for a table that has 100 columns, leads to parsing 100 Millions of expressions, which means that the overhead related to Select->expr_list is executed 100 Million times ... Considering that N=1 is by far the most probable expression, this case should be optimal. When N=2 For example, "select a OR b" involves 2 terms in the OR operator. In 4.0 and 4.1, parsing expressions involving 2 terms created 1 Item_cond_or node, which is the expected result. In 5.0, parsing these expression also produced 1 node, but with some extra overhead related to Select->expr_list : creating 1 list in Select->expr_list and another in Item_cond::list is inefficient. With this patch, the overhead introduced in 5.0 has been removed so that performances for N=2 should be identical to the 4.0 performances. Note that the memory allocation uses the new (thd->mem_root) syntax directly. The cost of "is_cond_or" is estimated to be neglectable: the real problem of the performance degradation comes from unneeded memory allocations. When N>=3 For example, "select a OR b OR c ...", which involves 3 or more terms. In 4.0 and 4.1, the parser had no significant cost overhead, but produced an Item tree which is difficult to evaluate / optimize during runtime. In 5.0, the parser produces a better Item tree, using the Item_cond constructor that accepts a list of children directly, but at an extra cost related to Select->expr_list. With this patch, the code is implemented to take the best of the two implementations: - there is no overhead with Select->expr_list - the Item tree generated is optimized and flattened. This is achieved by adding children nodes into the Item tree directly, with Item_cond::add(), which avoids the need for temporary lists and memory allocation Note that this patch also provide an extra optimization, that the previous code in 5.0 did not provide: expressions are flattened in the Item tree, based on what the expression already parsed is, and not based on the order in which rules are reduced. For example : "(a OR b) OR c", "a OR (b OR c)" would both be represented with 2 Item_cond_or nodes before this patch, and with 1 node only with this patch. The logic used is based on the mathematical properties of the OR operator (it's associative), and produces a simpler tree. --- mysql-test/r/parser_precedence.result | 356 ++++++++++++++++++++++++++ mysql-test/t/parser_precedence.test | 93 +++++++ sql/item_cmpfunc.h | 18 ++ sql/sql_yacc.yy | 144 +++++++---- 4 files changed, 566 insertions(+), 45 deletions(-) create mode 100644 mysql-test/r/parser_precedence.result create mode 100644 mysql-test/t/parser_precedence.test diff --git a/mysql-test/r/parser_precedence.result b/mysql-test/r/parser_precedence.result new file mode 100644 index 00000000000..e2d35521ca9 --- /dev/null +++ b/mysql-test/r/parser_precedence.result @@ -0,0 +1,356 @@ +drop table if exists t1_30237_bool; +create table t1_30237_bool(A boolean, B boolean, C boolean); +insert into t1_30237_bool values +(FALSE, FALSE, FALSE), +(FALSE, FALSE, NULL), +(FALSE, FALSE, TRUE), +(FALSE, NULL, FALSE), +(FALSE, NULL, NULL), +(FALSE, NULL, TRUE), +(FALSE, TRUE, FALSE), +(FALSE, TRUE, NULL), +(FALSE, TRUE, TRUE), +(NULL, FALSE, FALSE), +(NULL, FALSE, NULL), +(NULL, FALSE, TRUE), +(NULL, NULL, FALSE), +(NULL, NULL, NULL), +(NULL, NULL, TRUE), +(NULL, TRUE, FALSE), +(NULL, TRUE, NULL), +(NULL, TRUE, TRUE), +(TRUE, FALSE, FALSE), +(TRUE, FALSE, NULL), +(TRUE, FALSE, TRUE), +(TRUE, NULL, FALSE), +(TRUE, NULL, NULL), +(TRUE, NULL, TRUE), +(TRUE, TRUE, FALSE), +(TRUE, TRUE, NULL), +(TRUE, TRUE, TRUE) ; +Testing OR, XOR, AND +select A, B, A OR B, A XOR B, A AND B +from t1_30237_bool where C is null order by A, B; +A B A OR B A XOR B A AND B +NULL NULL NULL NULL NULL +NULL 0 NULL NULL 0 +NULL 1 1 NULL NULL +0 NULL NULL NULL 0 +0 0 0 0 0 +0 1 1 1 0 +1 NULL 1 NULL NULL +1 0 1 1 0 +1 1 1 0 1 +Testing that OR is associative +select A, B, C, (A OR B) OR C, A OR (B OR C), A OR B OR C +from t1_30237_bool order by A, B, C; +A B C (A OR B) OR C A OR (B OR C) A OR B OR C +NULL NULL NULL NULL NULL NULL +NULL NULL 0 NULL NULL NULL +NULL NULL 1 1 1 1 +NULL 0 NULL NULL NULL NULL +NULL 0 0 NULL NULL NULL +NULL 0 1 1 1 1 +NULL 1 NULL 1 1 1 +NULL 1 0 1 1 1 +NULL 1 1 1 1 1 +0 NULL NULL NULL NULL NULL +0 NULL 0 NULL NULL NULL +0 NULL 1 1 1 1 +0 0 NULL NULL NULL NULL +0 0 0 0 0 0 +0 0 1 1 1 1 +0 1 NULL 1 1 1 +0 1 0 1 1 1 +0 1 1 1 1 1 +1 NULL NULL 1 1 1 +1 NULL 0 1 1 1 +1 NULL 1 1 1 1 +1 0 NULL 1 1 1 +1 0 0 1 1 1 +1 0 1 1 1 1 +1 1 NULL 1 1 1 +1 1 0 1 1 1 +1 1 1 1 1 1 +select count(*) from t1_30237_bool +where ((A OR B) OR C) != (A OR (B OR C)); +count(*) +0 +Testing that XOR is associative +select A, B, C, (A XOR B) XOR C, A XOR (B XOR C), A XOR B XOR C +from t1_30237_bool order by A, B, C; +A B C (A XOR B) XOR C A XOR (B XOR C) A XOR B XOR C +NULL NULL NULL NULL NULL NULL +NULL NULL 0 NULL NULL NULL +NULL NULL 1 NULL NULL NULL +NULL 0 NULL NULL NULL NULL +NULL 0 0 NULL NULL NULL +NULL 0 1 NULL NULL NULL +NULL 1 NULL NULL NULL NULL +NULL 1 0 NULL NULL NULL +NULL 1 1 NULL NULL NULL +0 NULL NULL NULL NULL NULL +0 NULL 0 NULL NULL NULL +0 NULL 1 NULL NULL NULL +0 0 NULL NULL NULL NULL +0 0 0 0 0 0 +0 0 1 1 1 1 +0 1 NULL NULL NULL NULL +0 1 0 1 1 1 +0 1 1 0 0 0 +1 NULL NULL NULL NULL NULL +1 NULL 0 NULL NULL NULL +1 NULL 1 NULL NULL NULL +1 0 NULL NULL NULL NULL +1 0 0 1 1 1 +1 0 1 0 0 0 +1 1 NULL NULL NULL NULL +1 1 0 0 0 0 +1 1 1 1 1 1 +select count(*) from t1_30237_bool +where ((A XOR B) XOR C) != (A XOR (B XOR C)); +count(*) +0 +Testing that AND is associative +select A, B, C, (A AND B) AND C, A AND (B AND C), A AND B AND C +from t1_30237_bool order by A, B, C; +A B C (A AND B) AND C A AND (B AND C) A AND B AND C +NULL NULL NULL NULL NULL NULL +NULL NULL 0 0 0 0 +NULL NULL 1 NULL NULL NULL +NULL 0 NULL 0 0 0 +NULL 0 0 0 0 0 +NULL 0 1 0 0 0 +NULL 1 NULL NULL NULL NULL +NULL 1 0 0 0 0 +NULL 1 1 NULL NULL NULL +0 NULL NULL 0 0 0 +0 NULL 0 0 0 0 +0 NULL 1 0 0 0 +0 0 NULL 0 0 0 +0 0 0 0 0 0 +0 0 1 0 0 0 +0 1 NULL 0 0 0 +0 1 0 0 0 0 +0 1 1 0 0 0 +1 NULL NULL NULL NULL NULL +1 NULL 0 0 0 0 +1 NULL 1 NULL NULL NULL +1 0 NULL 0 0 0 +1 0 0 0 0 0 +1 0 1 0 0 0 +1 1 NULL NULL NULL NULL +1 1 0 0 0 0 +1 1 1 1 1 1 +select count(*) from t1_30237_bool +where ((A AND B) AND C) != (A AND (B AND C)); +count(*) +0 +Testing that AND has precedence over OR +select A, B, C, (A OR B) AND C, A OR (B AND C), A OR B AND C +from t1_30237_bool order by A, B, C; +A B C (A OR B) AND C A OR (B AND C) A OR B AND C +NULL NULL NULL NULL NULL NULL +NULL NULL 0 0 NULL NULL +NULL NULL 1 NULL NULL NULL +NULL 0 NULL NULL NULL NULL +NULL 0 0 0 NULL NULL +NULL 0 1 NULL NULL NULL +NULL 1 NULL NULL NULL NULL +NULL 1 0 0 NULL NULL +NULL 1 1 1 1 1 +0 NULL NULL NULL NULL NULL +0 NULL 0 0 0 0 +0 NULL 1 NULL NULL NULL +0 0 NULL 0 0 0 +0 0 0 0 0 0 +0 0 1 0 0 0 +0 1 NULL NULL NULL NULL +0 1 0 0 0 0 +0 1 1 1 1 1 +1 NULL NULL NULL 1 1 +1 NULL 0 0 1 1 +1 NULL 1 1 1 1 +1 0 NULL NULL 1 1 +1 0 0 0 1 1 +1 0 1 1 1 1 +1 1 NULL NULL 1 1 +1 1 0 0 1 1 +1 1 1 1 1 1 +select count(*) from t1_30237_bool +where (A OR (B AND C)) != (A OR B AND C); +count(*) +0 +select A, B, C, (A AND B) OR C, A AND (B OR C), A AND B OR C +from t1_30237_bool order by A, B, C; +A B C (A AND B) OR C A AND (B OR C) A AND B OR C +NULL NULL NULL NULL NULL NULL +NULL NULL 0 NULL NULL NULL +NULL NULL 1 1 NULL 1 +NULL 0 NULL NULL NULL NULL +NULL 0 0 0 0 0 +NULL 0 1 1 NULL 1 +NULL 1 NULL NULL NULL NULL +NULL 1 0 NULL NULL NULL +NULL 1 1 1 NULL 1 +0 NULL NULL NULL 0 NULL +0 NULL 0 0 0 0 +0 NULL 1 1 0 1 +0 0 NULL NULL 0 NULL +0 0 0 0 0 0 +0 0 1 1 0 1 +0 1 NULL NULL 0 NULL +0 1 0 0 0 0 +0 1 1 1 0 1 +1 NULL NULL NULL NULL NULL +1 NULL 0 NULL NULL NULL +1 NULL 1 1 1 1 +1 0 NULL NULL NULL NULL +1 0 0 0 0 0 +1 0 1 1 1 1 +1 1 NULL 1 1 1 +1 1 0 1 1 1 +1 1 1 1 1 1 +select count(*) from t1_30237_bool +where ((A AND B) OR C) != (A AND B OR C); +count(*) +0 +Testing that AND has precedence over XOR +select A, B, C, (A XOR B) AND C, A XOR (B AND C), A XOR B AND C +from t1_30237_bool order by A, B, C; +A B C (A XOR B) AND C A XOR (B AND C) A XOR B AND C +NULL NULL NULL NULL NULL NULL +NULL NULL 0 0 NULL NULL +NULL NULL 1 NULL NULL NULL +NULL 0 NULL NULL NULL NULL +NULL 0 0 0 NULL NULL +NULL 0 1 NULL NULL NULL +NULL 1 NULL NULL NULL NULL +NULL 1 0 0 NULL NULL +NULL 1 1 NULL NULL NULL +0 NULL NULL NULL NULL NULL +0 NULL 0 0 0 0 +0 NULL 1 NULL NULL NULL +0 0 NULL 0 0 0 +0 0 0 0 0 0 +0 0 1 0 0 0 +0 1 NULL NULL NULL NULL +0 1 0 0 0 0 +0 1 1 1 1 1 +1 NULL NULL NULL NULL NULL +1 NULL 0 0 1 1 +1 NULL 1 NULL NULL NULL +1 0 NULL NULL 1 1 +1 0 0 0 1 1 +1 0 1 1 1 1 +1 1 NULL 0 NULL NULL +1 1 0 0 1 1 +1 1 1 0 0 0 +select count(*) from t1_30237_bool +where (A XOR (B AND C)) != (A XOR B AND C); +count(*) +0 +select A, B, C, (A AND B) XOR C, A AND (B XOR C), A AND B XOR C +from t1_30237_bool order by A, B, C; +A B C (A AND B) XOR C A AND (B XOR C) A AND B XOR C +NULL NULL NULL NULL NULL NULL +NULL NULL 0 NULL NULL NULL +NULL NULL 1 NULL NULL NULL +NULL 0 NULL NULL NULL NULL +NULL 0 0 0 0 0 +NULL 0 1 1 NULL 1 +NULL 1 NULL NULL NULL NULL +NULL 1 0 NULL NULL NULL +NULL 1 1 NULL 0 NULL +0 NULL NULL NULL 0 NULL +0 NULL 0 0 0 0 +0 NULL 1 1 0 1 +0 0 NULL NULL 0 NULL +0 0 0 0 0 0 +0 0 1 1 0 1 +0 1 NULL NULL 0 NULL +0 1 0 0 0 0 +0 1 1 1 0 1 +1 NULL NULL NULL NULL NULL +1 NULL 0 NULL NULL NULL +1 NULL 1 NULL NULL NULL +1 0 NULL NULL NULL NULL +1 0 0 0 0 0 +1 0 1 1 1 1 +1 1 NULL NULL NULL NULL +1 1 0 1 1 1 +1 1 1 0 0 0 +select count(*) from t1_30237_bool +where ((A AND B) XOR C) != (A AND B XOR C); +count(*) +0 +Testing that XOR has precedence over OR +select A, B, C, (A XOR B) OR C, A XOR (B OR C), A XOR B OR C +from t1_30237_bool order by A, B, C; +A B C (A XOR B) OR C A XOR (B OR C) A XOR B OR C +NULL NULL NULL NULL NULL NULL +NULL NULL 0 NULL NULL NULL +NULL NULL 1 1 NULL 1 +NULL 0 NULL NULL NULL NULL +NULL 0 0 NULL NULL NULL +NULL 0 1 1 NULL 1 +NULL 1 NULL NULL NULL NULL +NULL 1 0 NULL NULL NULL +NULL 1 1 1 NULL 1 +0 NULL NULL NULL NULL NULL +0 NULL 0 NULL NULL NULL +0 NULL 1 1 1 1 +0 0 NULL NULL NULL NULL +0 0 0 0 0 0 +0 0 1 1 1 1 +0 1 NULL 1 1 1 +0 1 0 1 1 1 +0 1 1 1 1 1 +1 NULL NULL NULL NULL NULL +1 NULL 0 NULL NULL NULL +1 NULL 1 1 0 1 +1 0 NULL 1 NULL 1 +1 0 0 1 1 1 +1 0 1 1 0 1 +1 1 NULL NULL 0 NULL +1 1 0 0 0 0 +1 1 1 1 0 1 +select count(*) from t1_30237_bool +where ((A XOR B) OR C) != (A XOR B OR C); +count(*) +0 +select A, B, C, (A OR B) XOR C, A OR (B XOR C), A OR B XOR C +from t1_30237_bool order by A, B, C; +A B C (A OR B) XOR C A OR (B XOR C) A OR B XOR C +NULL NULL NULL NULL NULL NULL +NULL NULL 0 NULL NULL NULL +NULL NULL 1 NULL NULL NULL +NULL 0 NULL NULL NULL NULL +NULL 0 0 NULL NULL NULL +NULL 0 1 NULL 1 1 +NULL 1 NULL NULL NULL NULL +NULL 1 0 1 1 1 +NULL 1 1 0 NULL NULL +0 NULL NULL NULL NULL NULL +0 NULL 0 NULL NULL NULL +0 NULL 1 NULL NULL NULL +0 0 NULL NULL NULL NULL +0 0 0 0 0 0 +0 0 1 1 1 1 +0 1 NULL NULL NULL NULL +0 1 0 1 1 1 +0 1 1 0 0 0 +1 NULL NULL NULL 1 1 +1 NULL 0 1 1 1 +1 NULL 1 0 1 1 +1 0 NULL NULL 1 1 +1 0 0 1 1 1 +1 0 1 0 1 1 +1 1 NULL NULL 1 1 +1 1 0 1 1 1 +1 1 1 0 1 1 +select count(*) from t1_30237_bool +where (A OR (B XOR C)) != (A OR B XOR C); +count(*) +0 +drop table t1_30237_bool; diff --git a/mysql-test/t/parser_precedence.test b/mysql-test/t/parser_precedence.test new file mode 100644 index 00000000000..a3a80776fb1 --- /dev/null +++ b/mysql-test/t/parser_precedence.test @@ -0,0 +1,93 @@ + +--disable_warnings +drop table if exists t1_30237_bool; +--enable_warnings + +create table t1_30237_bool(A boolean, B boolean, C boolean); + +insert into t1_30237_bool values +(FALSE, FALSE, FALSE), +(FALSE, FALSE, NULL), +(FALSE, FALSE, TRUE), +(FALSE, NULL, FALSE), +(FALSE, NULL, NULL), +(FALSE, NULL, TRUE), +(FALSE, TRUE, FALSE), +(FALSE, TRUE, NULL), +(FALSE, TRUE, TRUE), +(NULL, FALSE, FALSE), +(NULL, FALSE, NULL), +(NULL, FALSE, TRUE), +(NULL, NULL, FALSE), +(NULL, NULL, NULL), +(NULL, NULL, TRUE), +(NULL, TRUE, FALSE), +(NULL, TRUE, NULL), +(NULL, TRUE, TRUE), +(TRUE, FALSE, FALSE), +(TRUE, FALSE, NULL), +(TRUE, FALSE, TRUE), +(TRUE, NULL, FALSE), +(TRUE, NULL, NULL), +(TRUE, NULL, TRUE), +(TRUE, TRUE, FALSE), +(TRUE, TRUE, NULL), +(TRUE, TRUE, TRUE) ; + +--echo Testing OR, XOR, AND +select A, B, A OR B, A XOR B, A AND B + from t1_30237_bool where C is null order by A, B; + +--echo Testing that OR is associative +select A, B, C, (A OR B) OR C, A OR (B OR C), A OR B OR C + from t1_30237_bool order by A, B, C; + +select count(*) from t1_30237_bool + where ((A OR B) OR C) != (A OR (B OR C)); + +--echo Testing that XOR is associative +select A, B, C, (A XOR B) XOR C, A XOR (B XOR C), A XOR B XOR C + from t1_30237_bool order by A, B, C; + +select count(*) from t1_30237_bool + where ((A XOR B) XOR C) != (A XOR (B XOR C)); + +--echo Testing that AND is associative +select A, B, C, (A AND B) AND C, A AND (B AND C), A AND B AND C + from t1_30237_bool order by A, B, C; + +select count(*) from t1_30237_bool + where ((A AND B) AND C) != (A AND (B AND C)); + +--echo Testing that AND has precedence over OR +select A, B, C, (A OR B) AND C, A OR (B AND C), A OR B AND C + from t1_30237_bool order by A, B, C; +select count(*) from t1_30237_bool + where (A OR (B AND C)) != (A OR B AND C); +select A, B, C, (A AND B) OR C, A AND (B OR C), A AND B OR C + from t1_30237_bool order by A, B, C; +select count(*) from t1_30237_bool + where ((A AND B) OR C) != (A AND B OR C); + +--echo Testing that AND has precedence over XOR +select A, B, C, (A XOR B) AND C, A XOR (B AND C), A XOR B AND C + from t1_30237_bool order by A, B, C; +select count(*) from t1_30237_bool + where (A XOR (B AND C)) != (A XOR B AND C); +select A, B, C, (A AND B) XOR C, A AND (B XOR C), A AND B XOR C + from t1_30237_bool order by A, B, C; +select count(*) from t1_30237_bool + where ((A AND B) XOR C) != (A AND B XOR C); + +--echo Testing that XOR has precedence over OR +select A, B, C, (A XOR B) OR C, A XOR (B OR C), A XOR B OR C + from t1_30237_bool order by A, B, C; +select count(*) from t1_30237_bool + where ((A XOR B) OR C) != (A XOR B OR C); +select A, B, C, (A OR B) XOR C, A OR (B XOR C), A OR B XOR C + from t1_30237_bool order by A, B, C; +select count(*) from t1_30237_bool + where (A OR (B XOR C)) != (A OR B XOR C); + +drop table t1_30237_bool; + diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index 9afc0507817..da71b3ef512 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -1361,6 +1361,7 @@ public: Item_cond(List &nlist) :Item_bool_func(), list(nlist), abort_on_null(0) {} bool add(Item *item) { return list.push_back(item); } + bool add_at_head(Item *item) { return list.push_front(item); } void add_at_head(List *nlist) { list.prepand(nlist); } bool fix_fields(THD *, Item **ref); @@ -1554,6 +1555,15 @@ public: Item *neg_transformer(THD *thd); }; +inline bool is_cond_and(Item *item) +{ + if (item->type() != Item::COND_ITEM) + return FALSE; + + Item_cond *cond_item= (Item_cond*) item; + return (cond_item->functype() == Item_func::COND_AND_FUNC); +} + class Item_cond_or :public Item_cond { public: @@ -1575,6 +1585,14 @@ public: Item *neg_transformer(THD *thd); }; +inline bool is_cond_or(Item *item) +{ + if (item->type() != Item::COND_ITEM) + return FALSE; + + Item_cond *cond_item= (Item_cond*) item; + return (cond_item->functype() == Item_func::COND_OR_FUNC); +} /* XOR is Item_cond, not an Item_int_func because we could like to diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index d71e756e91c..4230e6ee371 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -458,10 +458,10 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %pure_parser /* We have threads */ /* - Currently there is 251 shift/reduce conflict. We should not introduce - new conflicts any more. + Currently there are 245 shift/reduce conflicts. + We should not introduce new conflicts any more. */ -%expect 251 +%expect 245 %token END_OF_INPUT @@ -1011,7 +1011,8 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); /* A dummy token to force the priority of table_ref production in a join. */ %left TABLE_REF_PRIORITY %left SET_VAR -%left OR_OR_SYM OR_SYM OR2_SYM XOR +%left OR_OR_SYM OR_SYM OR2_SYM +%left XOR %left AND_SYM AND_AND_SYM %left BETWEEN_SYM CASE_SYM WHEN_SYM THEN_SYM ELSE %left EQ EQUAL_SYM GE GT_SYM LE LT NE IS LIKE REGEXP IN_SYM @@ -1024,6 +1025,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %left NEG '~' %right NOT_SYM NOT2_SYM %right BINARY COLLATE_SYM +%left INTERVAL_SYM %type IDENT IDENT_QUOTED TEXT_STRING DECIMAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM @@ -1066,7 +1068,8 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %type literal text_literal insert_ident order_ident simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr - variable variable_aux bool_term bool_factor bool_test bool_pri + variable variable_aux bool_factor + bool_test bool_pri predicate bit_expr bit_term bit_factor value_expr term factor table_wild simple_expr udf_expr expr_or_default set_expr_or_default interval_expr @@ -4464,53 +4467,103 @@ optional_braces: | '(' ')' {}; /* all possible expressions */ -expr: - bool_term { Select->expr_list.push_front(new List); } - bool_or_expr +expr: + bool_factor + | expr or expr %prec OR_SYM { - List *list= Select->expr_list.pop(); - if (list->elements) + /* + Design notes: + Do not use a manually maintained stack like thd->lex->xxx_list, + but use the internal bison stack ($$, $1 and $3) instead. + Using the bison stack is: + - more robust to changes in the grammar, + - guaranteed to be in sync with the parser state, + - better for performances (no memory allocation). + */ + Item_cond_or *item1; + Item_cond_or *item3; + if (is_cond_or($1)) { - list->push_front($1); - $$= new Item_cond_or(*list); - /* optimize construction of logical OR to reduce - amount of objects for complex expressions */ + item1= (Item_cond_or*) $1; + if (is_cond_or($3)) + { + item3= (Item_cond_or*) $3; + /* + (X1 OR X2) OR (Y1 OR Y2) ==> OR (X1, X2, Y1, Y2) + */ + item3->add_at_head(item1->argument_list()); + $$ = $3; + } + else + { + /* + (X1 OR X2) OR Y ==> OR (X1, X2, Y) + */ + item1->add($3); + $$ = $1; + } + } + else if (is_cond_or($3)) + { + item3= (Item_cond_or*) $3; + /* + X OR (Y1 OR Y2) ==> OR (X, Y1, Y2) + */ + item3->add_at_head($1); + $$ = $3; } else - $$= $1; - delete list; - } - ; - -bool_or_expr: - /* empty */ - | bool_or_expr or bool_term - { Select->expr_list.head()->push_back($3); } - ; - -bool_term: - bool_term XOR bool_term { $$= new Item_cond_xor($1,$3); } - | bool_factor { Select->expr_list.push_front(new List); } - bool_and_expr - { - List *list= Select->expr_list.pop(); - if (list->elements) { - list->push_front($1); - $$= new Item_cond_and(*list); - /* optimize construction of logical AND to reduce - amount of objects for complex expressions */ + /* X OR Y */ + $$ = new (YYTHD->mem_root) Item_cond_or($1, $3); + } + } + | expr XOR expr %prec XOR + { + /* XOR is a proprietary extension */ + $$ = new (YYTHD->mem_root) Item_cond_xor($1, $3); + } + | expr and expr %prec AND_SYM + { + /* See comments in rule expr: expr or expr */ + Item_cond_and *item1; + Item_cond_and *item3; + if (is_cond_and($1)) + { + item1= (Item_cond_and*) $1; + if (is_cond_and($3)) + { + item3= (Item_cond_and*) $3; + /* + (X1 AND X2) AND (Y1 AND Y2) ==> AND (X1, X2, Y1, Y2) + */ + item3->add_at_head(item1->argument_list()); + $$ = $3; + } + else + { + /* + (X1 AND X2) AND Y ==> AND (X1, X2, Y) + */ + item1->add($3); + $$ = $1; + } + } + else if (is_cond_and($3)) + { + item3= (Item_cond_and*) $3; + /* + X AND (Y1 AND Y2) ==> AND (X, Y1, Y2) + */ + item3->add_at_head($1); + $$ = $3; } else - $$= $1; - delete list; + { + /* X AND Y */ + $$ = new (YYTHD->mem_root) Item_cond_and($1, $3); + } } - ; - -bool_and_expr: - /* empty */ - | bool_and_expr and bool_factor - { Select->expr_list.head()->push_back($3); } ; bool_factor: @@ -4648,7 +4701,8 @@ all_or_any: ALL { $$ = 1; } ; interval_expr: - INTERVAL_SYM expr { $$=$2; } + INTERVAL_SYM expr %prec INTERVAL_SYM + { $$=$2; } ; simple_expr: From 74bbe20546a16590f52524653578a2d3baeeb0ae Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Wed, 22 Aug 2007 12:02:23 -0600 Subject: [PATCH 03/35] Manual merge --- BUILD/compile-pentium-gcov | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/BUILD/compile-pentium-gcov b/BUILD/compile-pentium-gcov index 77de5867d48..42666b2632c 100755 --- a/BUILD/compile-pentium-gcov +++ b/BUILD/compile-pentium-gcov @@ -20,18 +20,11 @@ export CCACHE_GCOV_VERSION_ENABLED path=`dirname $0` . "$path/SETUP.sh" -# GCC4 needs -fprofile-arcs -ftest-coverage on the linker command line (as well -# as on the compiler command line), and this requires setting LDFLAGS for BDB. -export LDFLAGS="-fprofile-arcs -ftest-coverage" +export LDFLAGS="$gcov_link_flags" -# The -fprofile-arcs and -ftest-coverage options cause GCC to instrument the -# code with profiling information used by gcov. -# The -DDISABLE_TAO_ASM is needed to avoid build failures in Yassl. -# The -DHAVE_gcov enables code to write out coverage info even when crashing. -extra_flags="$pentium_cflags -fprofile-arcs -ftest-coverage -DDISABLE_TAO_ASM -DHAVE_MUTEX_THREAD_ONLY $debug_extra_flags $debug_cflags $max_cflags -DMYSQL_SERVER_SUFFIX=-gcov -DHAVE_gcov" -extra_configs="$pentium_configs $debug_configs --disable-shared $static_link" -extra_configs="$extra_configs $max_configs" +extra_flags="$pentium_cflags $debug_cflags $max_cflags $gcov_compile_flags" c_warnings="$c_warnings $debug_extra_warnings" cxx_warnings="$cxx_warnings $debug_extra_warnings" +extra_configs="$pentium_configs $debug_configs $gcov_configs $max_configs" . "$path/FINISH.sh" From b1e0dcc09f86690e3ab8831c3d082a1797c4bebb Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Wed, 22 Aug 2007 14:25:36 -0600 Subject: [PATCH 04/35] Manual merge --- sql/sql_yacc.yy | 143 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 98 insertions(+), 45 deletions(-) diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 8f66e41b338..a0033c27a6b 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -508,10 +508,10 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %pure_parser /* We have threads */ /* - Currently there is 286 shift/reduce conflict. We should not introduce - new conflicts any more. + Currently there are 280 shift/reduce conflicts. + We should not introduce new conflicts any more. */ -%expect 286 +%expect 280 /* Comments for TOKENS. @@ -1089,7 +1089,8 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); /* A dummy token to force the priority of table_ref production in a join. */ %left TABLE_REF_PRIORITY %left SET_VAR -%left OR_OR_SYM OR_SYM OR2_SYM XOR +%left OR_OR_SYM OR_SYM OR2_SYM +%left XOR %left AND_SYM AND_AND_SYM %left BETWEEN_SYM CASE_SYM WHEN_SYM THEN_SYM ELSE %left EQ EQUAL_SYM GE GT_SYM LE LT NE IS LIKE REGEXP IN_SYM @@ -1102,6 +1103,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %left NEG '~' %right NOT_SYM NOT2_SYM %right BINARY COLLATE_SYM +%left INTERVAL_SYM %type IDENT IDENT_QUOTED TEXT_STRING DECIMAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM @@ -1150,7 +1152,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %type literal text_literal insert_ident order_ident simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr - variable variable_aux bool_term bool_factor bool_test bool_pri + variable variable_aux bool_factor bool_test bool_pri predicate bit_expr bit_term bit_factor value_expr term factor table_wild simple_expr udf_expr expr_or_default set_expr_or_default interval_expr @@ -6554,53 +6556,103 @@ optional_braces: ; /* all possible expressions */ -expr: - bool_term { Select->expr_list.push_front(new List); } - bool_or_expr +expr: + bool_factor + | expr or expr %prec OR_SYM { - List *list= Select->expr_list.pop(); - if (list->elements) + /* + Design notes: + Do not use a manually maintained stack like thd->lex->xxx_list, + but use the internal bison stack ($$, $1 and $3) instead. + Using the bison stack is: + - more robust to changes in the grammar, + - guaranteed to be in sync with the parser state, + - better for performances (no memory allocation). + */ + Item_cond_or *item1; + Item_cond_or *item3; + if (is_cond_or($1)) { - list->push_front($1); - $$= new Item_cond_or(*list); - /* optimize construction of logical OR to reduce - amount of objects for complex expressions */ + item1= (Item_cond_or*) $1; + if (is_cond_or($3)) + { + item3= (Item_cond_or*) $3; + /* + (X1 OR X2) OR (Y1 OR Y2) ==> OR (X1, X2, Y1, Y2) + */ + item3->add_at_head(item1->argument_list()); + $$ = $3; + } + else + { + /* + (X1 OR X2) OR Y ==> OR (X1, X2, Y) + */ + item1->add($3); + $$ = $1; + } + } + else if (is_cond_or($3)) + { + item3= (Item_cond_or*) $3; + /* + X OR (Y1 OR Y2) ==> OR (X, Y1, Y2) + */ + item3->add_at_head($1); + $$ = $3; } else - $$= $1; - delete list; - } - ; - -bool_or_expr: - /* empty */ - | bool_or_expr or bool_term - { Select->expr_list.head()->push_back($3); } - ; - -bool_term: - bool_term XOR bool_term { $$= new Item_cond_xor($1,$3); } - | bool_factor { Select->expr_list.push_front(new List); } - bool_and_expr - { - List *list= Select->expr_list.pop(); - if (list->elements) { - list->push_front($1); - $$= new Item_cond_and(*list); - /* optimize construction of logical AND to reduce - amount of objects for complex expressions */ + /* X OR Y */ + $$ = new (YYTHD->mem_root) Item_cond_or($1, $3); + } + } + | expr XOR expr %prec XOR + { + /* XOR is a proprietary extension */ + $$ = new (YYTHD->mem_root) Item_cond_xor($1, $3); + } + | expr and expr %prec AND_SYM + { + /* See comments in rule expr: expr or expr */ + Item_cond_and *item1; + Item_cond_and *item3; + if (is_cond_and($1)) + { + item1= (Item_cond_and*) $1; + if (is_cond_and($3)) + { + item3= (Item_cond_and*) $3; + /* + (X1 AND X2) AND (Y1 AND Y2) ==> AND (X1, X2, Y1, Y2) + */ + item3->add_at_head(item1->argument_list()); + $$ = $3; + } + else + { + /* + (X1 AND X2) AND Y ==> AND (X1, X2, Y) + */ + item1->add($3); + $$ = $1; + } + } + else if (is_cond_and($3)) + { + item3= (Item_cond_and*) $3; + /* + X AND (Y1 AND Y2) ==> AND (X, Y1, Y2) + */ + item3->add_at_head($1); + $$ = $3; } else - $$= $1; - delete list; + { + /* X AND Y */ + $$ = new (YYTHD->mem_root) Item_cond_and($1, $3); + } } - ; - -bool_and_expr: - /* empty */ - | bool_and_expr and bool_factor - { Select->expr_list.head()->push_back($3); } ; bool_factor: @@ -6766,7 +6818,8 @@ all_or_any: ; interval_expr: - INTERVAL_SYM expr { $$=$2; } + INTERVAL_SYM expr %prec INTERVAL_SYM + { $$=$2; } ; simple_expr: From 81114a7208464eb82831a85662d67d368bb10a73 Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Wed, 22 Aug 2007 15:38:32 -0600 Subject: [PATCH 05/35] Bug#30333 (Performance, expressions lists in the parser) Before this patch, the parser would execute: - Select->expr_list.push_front() - Select->expr_list.pop() when parsing expressions lists, in the following rules: - udf_expr_list - expr_list - ident_list This is unnecessary, and introduces overhead due to the memory allocations performed with Select->expr_list With this patch, this code has been removed. The list being parsed is maintained in the parser stack instead. Also, 'udf_expr_list' has been renamed 'opt_udf_expr_list', since this production can be empty. --- sql/sql_lex.cc | 2 -- sql/sql_lex.h | 1 - sql/sql_yacc.yy | 78 ++++++++++++++++++++++++++----------------------- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 78b4f034f98..7fe2ed04d51 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -323,7 +323,6 @@ void lex_start(THD *thd) lex->length=0; lex->part_info= 0; lex->select_lex.in_sum_expr=0; - lex->select_lex.expr_list.empty(); lex->select_lex.ftfunc_list_alloc.empty(); lex->select_lex.ftfunc_list= &lex->select_lex.ftfunc_list_alloc; lex->select_lex.group_list.empty(); @@ -1555,7 +1554,6 @@ void st_select_lex::init_select() options= 0; sql_cache= SQL_CACHE_UNSPECIFIED; braces= 0; - expr_list.empty(); interval_list.empty(); ftfunc_list_alloc.empty(); inner_sum_func_list= 0; diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 2e72a87c35d..f80ddfb8339 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -596,7 +596,6 @@ public: const char *type; /* type of select for EXPLAIN */ SQL_LIST order_list; /* ORDER clause */ - List expr_list; SQL_LIST *gorder_list; Item *select_limit, *offset_limit; /* LIMIT clause parameters */ // Arrays of pointers to top elements of all_fields list diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index a0033c27a6b..7b03f7d4eae 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -1171,7 +1171,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); NUM_literal %type - expr_list udf_expr_list udf_expr_list2 when_list + expr_list opt_udf_expr_list udf_expr_list when_list ident_list ident_list_arg opt_expr_list %type @@ -1245,7 +1245,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); select_item_list select_item values_list no_braces opt_limit_clause delete_limit_clause fields opt_values values procedure_list procedure_list2 procedure_item - expr_list2 udf_expr_list3 handler + handler opt_precision opt_ignore opt_column opt_restrict grant revoke set lock unlock string_list field_options field_option field_opt_list opt_binary table_lock_list table_lock @@ -7253,7 +7253,7 @@ function_call_generic: $$= udf; #endif } - udf_expr_list ')' + opt_udf_expr_list ')' { THD *thd= YYTHD; Create_func *builder; @@ -7350,27 +7350,23 @@ opt_query_expansion: | WITH QUERY_SYM EXPANSION_SYM { $$= FT_EXPAND; } ; +opt_udf_expr_list: + /* empty */ { $$= NULL; } + | udf_expr_list { $$= $1; } + ; + udf_expr_list: - /* empty */ { $$= NULL; } - | udf_expr_list2 { $$= $1;} - ; - -udf_expr_list2: - { Select->expr_list.push_front(new List); } - udf_expr_list3 - { $$= Select->expr_list.pop(); } - ; - -udf_expr_list3: - udf_expr - { - Select->expr_list.head()->push_back($1); - } - | udf_expr_list3 ',' udf_expr - { - Select->expr_list.head()->push_back($3); - } - ; + udf_expr + { + $$= new (YYTHD->mem_root) List; + $$->push_back($1); + } + | udf_expr_list ',' udf_expr + { + $1->push_back($3); + $$= $1; + } + ; udf_expr: remember_name expr remember_end select_alias @@ -7568,13 +7564,17 @@ opt_expr_list: ; expr_list: - { Select->expr_list.push_front(new List); } - expr_list2 - { $$= Select->expr_list.pop(); }; - -expr_list2: - expr { Select->expr_list.head()->push_back($1); } - | expr_list2 ',' expr { Select->expr_list.head()->push_back($3); }; + expr + { + $$= new (YYTHD->mem_root) List; + $$->push_back($1); + } + | expr_list ',' expr + { + $1->push_back($3); + $$= $1; + } + ; ident_list_arg: ident_list { $$= $1; } @@ -7582,13 +7582,17 @@ ident_list_arg: ; ident_list: - { Select->expr_list.push_front(new List); } - ident_list2 - { $$= Select->expr_list.pop(); }; - -ident_list2: - simple_ident { Select->expr_list.head()->push_back($1); } - | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); }; + simple_ident + { + $$= new (YYTHD->mem_root) List; + $$->push_back($1); + } + | ident_list ',' simple_ident + { + $1->push_back($3); + $$= $1; + } + ; opt_expr: /* empty */ { $$= NULL; } From 7a1942d2e1954a3641287cf1514d7dd77aa2351a Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Wed, 22 Aug 2007 18:11:01 -0600 Subject: [PATCH 06/35] Do not use $static_link for GCOV builds, since this flag was explicitly removed in pushbuild for GCOV builds. BUILD_CMD => ['sh', '-c', 'perl -i.bak -pe "s/ \\\\\$static_link//" ' . 'BUILD/compile-pentium-gcov; BUILD/compile-pentium-gcov'], Moving $static_link to SETUP.sh broke this, and is now fixed. Should this flag be needed on some platforms, the proper location is compile--gcov Tested the amd64 and pentium64 build fine without it, and can run NDB tests. --- BUILD/SETUP.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BUILD/SETUP.sh b/BUILD/SETUP.sh index a8efaf75db4..532ea4eb0f1 100755 --- a/BUILD/SETUP.sh +++ b/BUILD/SETUP.sh @@ -155,7 +155,7 @@ gcov_compile_flags="$gcov_compile_flags -DMYSQL_SERVER_SUFFIX=-gcov -DHAVE_gcov" gcov_link_flags="-fprofile-arcs -ftest-coverage" -gcov_configs="--disable-shared $static_link" +gcov_configs="--disable-shared" # gprof From ddb2ce8f2216ef0f05031b13fb9052d51ab01332 Mon Sep 17 00:00:00 2001 From: "thek@adventure.(none)" <> Date: Thu, 23 Aug 2007 15:37:55 +0200 Subject: [PATCH 07/35] Bug#28285 Test "events_bugs" has instable results of "select /*1*/ ... from processlist" Test case contains possible race conditions. This patch fixes these race conditions and also adjust the test to execute according to its documentation. --- mysql-test/r/events.result | 3 +++ mysql-test/t/events.test | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/events.result b/mysql-test/r/events.result index 579f5907882..be67e2acc7b 100644 --- a/mysql-test/r/events.result +++ b/mysql-test/r/events.result @@ -499,6 +499,9 @@ set global event_scheduler=off; select /*4*/ user, host, db, command, state, info from information_schema.processlist where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; user host db command state info root localhost events_test Connect User lock select get_lock("test_lock2_1", 20) +select release_lock("test_lock2_1"); +release_lock("test_lock2_1") +1 drop event закачка21; create table t_16 (s1 int); create trigger t_16_bi before insert on t_16 for each row create event e_16 on schedule every 1 second do set @a=5; diff --git a/mysql-test/t/events.test b/mysql-test/t/events.test index d7232705b81..cbb36cbd2be 100644 --- a/mysql-test/t/events.test +++ b/mysql-test/t/events.test @@ -466,13 +466,17 @@ drop event закачка; set global event_scheduler=1; select get_lock("test_lock2_1", 20); create event закачка21 on schedule every 10 hour do select get_lock("test_lock2_1", 20); ---sleep 1 +let $wait_condition= select count(*) = 3 from information_schema.processlist; +--source include/wait_condition.inc --echo "Should have only 3 processes: the scheduler, our conn and the locked event" select /*3*/ user, host, db, command, state, info from information_schema.processlist where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; set global event_scheduler=off; ---sleep 0.8 +let $wait_condition= select count(*) =1 from information_schema.processlist + where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; +--source include/wait_condition.inc --echo "Should have only our process now:" select /*4*/ user, host, db, command, state, info from information_schema.processlist where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; +select release_lock("test_lock2_1"); drop event закачка21; let $wait_condition= select count(*) = 0 from information_schema.processlist From 162a1be4f93431c0fd24e2a93f9b13e9582b08d4 Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Fri, 24 Aug 2007 09:08:11 -0600 Subject: [PATCH 08/35] Whitespace cleanup --- sql/sql_yacc.yy | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 7b03f7d4eae..24ee080a7aa 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -31,7 +31,7 @@ #define MYSQL_YACC #define YYINITDEPTH 100 -#define YYMAXDEPTH 3200 /* Because of 64K stack */ +#define YYMAXDEPTH 3200 /* Because of 64K stack */ #define Lex (YYTHD->lex) #define Select Lex->current_select #include "mysql_priv.h" @@ -506,7 +506,7 @@ Item* handle_sql2003_note184_exception(THD *thd, Item* left, bool equal, bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %} -%pure_parser /* We have threads */ +%pure_parser /* We have threads */ /* Currently there are 280 shift/reduce conflicts. We should not introduce new conflicts any more. @@ -1315,10 +1315,11 @@ rule: <-- starts at col 1 } ; <-- on a line by itself, starts at col 9 -Also, please do not use any , but spaces. -Having a uniform indentation in this file helps -code reviews, patches, merges, and make maintenance easier. -Thanks. + Also, please do not use any , but spaces. + Having a uniform indentation in this file helps + code reviews, patches, merges, and make maintenance easier. + Tip: grep [[:cntrl:]] sql_yacc.yy + Thanks. */ query: @@ -5149,7 +5150,7 @@ opt_bin_charset: MYSQL_YYABORT; } } - | charset charset_name { Lex->charset=$2; } + | charset charset_name { Lex->charset=$2; } ; opt_primary: @@ -5371,7 +5372,7 @@ alter: lex->create_info.row_type= ROW_TYPE_NOT_USED; lex->alter_info.reset(); lex->no_write_to_binlog= 0; - lex->create_info.storage_media= HA_SM_DEFAULT; + lex->create_info.storage_media= HA_SM_DEFAULT; } alter_commands {} @@ -6675,9 +6676,9 @@ bool_test: ; bool_pri: - bool_pri IS NULL_SYM { $$= new Item_func_isnull($1); } + bool_pri IS NULL_SYM { $$= new Item_func_isnull($1); } | bool_pri IS not NULL_SYM { $$= new Item_func_isnotnull($1); } - | bool_pri EQUAL_SYM predicate { $$= new Item_func_equal($1,$3); } + | bool_pri EQUAL_SYM predicate { $$= new Item_func_equal($1,$3); } | bool_pri comp_op predicate %prec EQ { $$= (*$2)(0)->create($1,$3); } | bool_pri comp_op all_or_any '(' subselect ')' %prec EQ @@ -6842,7 +6843,7 @@ simple_expr: | sum_expr | simple_expr OR_OR_SYM simple_expr { $$= new (YYTHD->mem_root) Item_func_concat($1, $3); } - | '+' simple_expr %prec NEG { $$= $2; } + | '+' simple_expr %prec NEG { $$= $2; } | '-' simple_expr %prec NEG { $$= new (YYTHD->mem_root) Item_func_neg($2); } | '~' simple_expr %prec NEG From 0b8bc8a96b355459829b34552a81c00c2f42ff12 Mon Sep 17 00:00:00 2001 From: "davi@moksha.local" <> Date: Fri, 24 Aug 2007 19:17:29 -0300 Subject: [PATCH 09/35] Add test case for bug 29936 Stored Procedure DML ignores low_priority_updates setting. This test case uses the wait_condition helper (only available in 5.1) in order to wait till the select/update opens and locks the table. --- mysql-test/r/sp.result | 23 +++++++++++++++ mysql-test/t/sp.test | 65 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/mysql-test/r/sp.result b/mysql-test/r/sp.result index 163bbb4aab4..9e00424e879 100644 --- a/mysql-test/r/sp.result +++ b/mysql-test/r/sp.result @@ -6546,4 +6546,27 @@ DROP VIEW v1; DROP FUNCTION f1; DROP FUNCTION f2; DROP TABLE t1; +drop table if exists t1; +drop procedure if exists p1; +create table t1 (value varchar(15)); +create procedure p1() update t1 set value='updated' where value='old'; +call p1(); +insert into t1 (value) values ("old"); +select get_lock('b26162',120); +get_lock('b26162',120) +1 +select 'rl_acquirer', value from t1 where get_lock('b26162',120);; +set session low_priority_updates=on; +call p1();; +select 'rl_contender', value from t1; +rl_contender value +rl_contender old +select release_lock('b26162'); +release_lock('b26162') +1 +rl_acquirer value +rl_acquirer old +drop procedure p1; +drop table t1; +set session low_priority_updates=default; End of 5.0 tests diff --git a/mysql-test/t/sp.test b/mysql-test/t/sp.test index 46ef6bc6ddd..37e3880cb82 100644 --- a/mysql-test/t/sp.test +++ b/mysql-test/t/sp.test @@ -7494,4 +7494,69 @@ DROP FUNCTION f1; DROP FUNCTION f2; DROP TABLE t1; +# +# Bug#29936 Stored Procedure DML ignores low_priority_updates setting +# + +--disable_warnings +drop table if exists t1; +drop procedure if exists p1; +--enable_warnings + +create table t1 (value varchar(15)); +create procedure p1() update t1 set value='updated' where value='old'; + +# load the procedure into sp cache and execute once +call p1(); + +insert into t1 (value) values ("old"); + +connect (rl_holder, localhost, root,,); +connect (rl_acquirer, localhost, root,,); +connect (rl_contender, localhost, root,,); +connect (rl_wait, localhost, root,,); + +connection rl_holder; +select get_lock('b26162',120); + +connection rl_acquirer; +--send select 'rl_acquirer', value from t1 where get_lock('b26162',120); + +# we must wait till this select opens and locks the tables +connection rl_wait; +let $wait_condition= + select count(*) = 1 from information_schema.processlist + where state = "User lock" and + info = "select 'rl_acquirer', value from t1 where get_lock('b26162',120)"; +--source include/wait_condition.inc + +connection default; +set session low_priority_updates=on; +--send call p1(); + +connection rl_wait; +let $wait_condition= + select count(*) = 1 from information_schema.processlist + where state = "Locked" and + info = "update t1 set value='updated' where value='old'"; +--source include/wait_condition.inc + +connection rl_contender; +select 'rl_contender', value from t1; + +connection rl_holder; +select release_lock('b26162'); + +connection rl_acquirer; +--reap +connection default; +--reap + +disconnect rl_holder; +disconnect rl_acquirer; +disconnect rl_wait; +drop procedure p1; +drop table t1; +set session low_priority_updates=default; + --echo End of 5.0 tests From 91e1cc2195988b4fa5bae66914e12c9dc9da8187 Mon Sep 17 00:00:00 2001 From: "davi@moksha.local" <> Date: Mon, 27 Aug 2007 10:13:54 -0300 Subject: [PATCH 10/35] Bug#25164 create table `a` as select * from `A` hangs The problem from a user's perspective: user creates table A, and then tries to CREATE TABLE a SELECT from A - and this causes a deadlock error, a hang, or fails with a debug assert, but only if the storage engine is InnoDB. The origin of the problem: InnoDB uses case-insensitive collation (system_charset_info) when looking up the internal table share, thus returning the same share for 'a' and 'A'. Cause of the user-visible behavior: since the same share is returned to SQL locking subsystem, it assumes that the same table is first locked (within the same session) for WRITE, and then for READ, and returns a deadlock error. However, the code is wrong in not properly cleaning up upon an error, leaving external locks in place, which leads to assertion failures and hangs. Fix that has been implemented: the SQL layer should properly propagate the deadlock error, cleaning up and freeing all resources. Further work towards a more complete solution: InnoDB should not use case insensitive collation for table share hash if table names on disk honor the case. --- mysql-test/r/innodb-deadlock.result | 11 +++++++++++ mysql-test/t/innodb-deadlock.test | 27 ++++++++++++++++++++++++++- sql/lock.cc | 2 ++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/innodb-deadlock.result b/mysql-test/r/innodb-deadlock.result index 2ca82101fb4..b6a3373e8c6 100644 --- a/mysql-test/r/innodb-deadlock.result +++ b/mysql-test/r/innodb-deadlock.result @@ -94,3 +94,14 @@ id x 300 300 commit; drop table t1, t2; +End of 4.1 tests +set storage_engine=innodb; +drop table if exists a; +drop table if exists A; +create table A (c int); +insert into A (c) values (0); +create table a as select * from A; +drop table A; +drop table if exists a; +set storage_engine=default; +End of 5.0 tests. diff --git a/mysql-test/t/innodb-deadlock.test b/mysql-test/t/innodb-deadlock.test index 81acfba5c93..1a184f98771 100644 --- a/mysql-test/t/innodb-deadlock.test +++ b/mysql-test/t/innodb-deadlock.test @@ -112,4 +112,29 @@ commit; drop table t1, t2; -# End of 4.1 tests +--echo End of 4.1 tests + +# +# Bug#25164 create table `a` as select * from `A` hangs +# + +set storage_engine=innodb; + +--disable_warnings +drop table if exists a; +drop table if exists A; +--enable_warnings + +create table A (c int); +insert into A (c) values (0); +--error 0,ER_LOCK_DEADLOCK,ER_UPDATE_TABLE_USED +create table a as select * from A; +drop table A; + +--disable_warnings +drop table if exists a; +--enable_warnings + +set storage_engine=default; + +--echo End of 5.0 tests. diff --git a/sql/lock.cc b/sql/lock.cc index 0036d0aef77..cf06be5f95f 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -172,6 +172,8 @@ MYSQL_LOCK *mysql_lock_tables(THD *thd, TABLE **tables, uint count, thd->lock_id)]; if (rc > 1) /* a timeout or a deadlock */ { + if (sql_lock->table_count) + VOID(unlock_external(thd, sql_lock->table, sql_lock->table_count)); my_error(rc, MYF(0)); my_free((gptr) sql_lock,MYF(0)); sql_lock= 0; From 77d78a883fa61550ab9686f5c7ca0cec9c08ea8a Mon Sep 17 00:00:00 2001 From: "davi@moksha.local" <> Date: Mon, 27 Aug 2007 10:37:12 -0300 Subject: [PATCH 11/35] Bug#30632 HANDLER read failure causes hang If, after the tables are locked, one of the conditions to read from a HANDLER table is not met, the handler code wrongly jumps to a error path that won't unlock the tables. The user-visible effect is that after a error in a handler read command, all subsequent handler operations on the same table will hang. The fix is simply to correct the code to jump to the (same) error path that unlocks the tables. --- mysql-test/r/handler.result | 13 +++++++++++++ mysql-test/t/handler.test | 19 +++++++++++++++++++ sql/sql_handler.cc | 6 +++--- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/handler.result b/mysql-test/r/handler.result index 5e123df9103..dc3750e16ea 100644 --- a/mysql-test/r/handler.result +++ b/mysql-test/r/handler.result @@ -489,3 +489,16 @@ handler t1 open; ERROR HY000: Table storage engine for 't1' doesn't have this option --> client 1 drop table t1; +drop table if exists t1; +create table t1 (a int); +handler t1 open as t1_alias; +handler t1_alias read a next; +ERROR HY000: Key 'a' doesn't exist in table 't1_alias' +handler t1_alias READ a next where inexistent > 0; +ERROR 42S22: Unknown column 'inexistent' in 'field list' +handler t1_alias read a next; +ERROR HY000: Key 'a' doesn't exist in table 't1_alias' +handler t1_alias READ a next where inexistent > 0; +ERROR 42S22: Unknown column 'inexistent' in 'field list' +handler t1_alias close; +drop table t1; diff --git a/mysql-test/t/handler.test b/mysql-test/t/handler.test index 4edd5b5ec32..6ef216f6ed2 100644 --- a/mysql-test/t/handler.test +++ b/mysql-test/t/handler.test @@ -441,3 +441,22 @@ handler t1 open; --echo --> client 1 connection default; drop table t1; + +# +# Bug#30632 HANDLER read failure causes hang +# +--disable_warnings +drop table if exists t1; +--enable_warnings +create table t1 (a int); +handler t1 open as t1_alias; +--error 1176 +handler t1_alias read a next; +--error 1054 +handler t1_alias READ a next where inexistent > 0; +--error 1176 +handler t1_alias read a next; +--error 1054 +handler t1_alias READ a next where inexistent > 0; +handler t1_alias close; +drop table t1; diff --git a/sql/sql_handler.cc b/sql/sql_handler.cc index 83c141f099f..9aefa71647e 100644 --- a/sql/sql_handler.cc +++ b/sql/sql_handler.cc @@ -440,7 +440,7 @@ bool mysql_ha_read(THD *thd, TABLE_LIST *tables, cond->cleanup(); // File was reopened if ((!cond->fixed && cond->fix_fields(thd, &cond)) || cond->check_cols(1)) - goto err0; + goto err; } if (keyname) @@ -448,13 +448,13 @@ bool mysql_ha_read(THD *thd, TABLE_LIST *tables, if ((keyno=find_type(keyname, &table->s->keynames, 1+2)-1)<0) { my_error(ER_KEY_DOES_NOT_EXITS, MYF(0), keyname, tables->alias); - goto err0; + goto err; } } if (insert_fields(thd, &thd->lex->select_lex.context, tables->db, tables->alias, &it, 0)) - goto err0; + goto err; protocol->send_fields(&list, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF); From ec9b9a98aa0aff20f30bee78f14100c6baf5eff5 Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Tue, 28 Aug 2007 11:16:03 -0600 Subject: [PATCH 12/35] Bug#30625 (Performance, reduce depth for expressions) This is a performance bug, affecting in particular the bison generated code for the parser. Prior to this fix, the grammar used a long chain of reduces to parse an expression, like: bit_expr -> bit_term bit_term -> bit_factor bit_factor -> value_expr value_expr -> term term -> factor etc This chain of reduces cause the internal state automaton in the generated parser to execute more state transitions and more reduces, so that the generated MySQLParse() function would spend a lot of time looping to execute all the grammar reductions. With this patch, the grammar has been reorganized so that rules are more "flat", limiting the depth of reduces needed to parse . Tests have been written to enforce that relative priorities and properties of operators have not changed while changing the grammar. See the bug report for performance data. --- mysql-test/r/parser_precedence.result | 391 ++++++++++++++++++++++++++ mysql-test/t/parser_precedence.test | 240 ++++++++++++++++ sql/sql_yacc.yy | 115 ++++---- 3 files changed, 686 insertions(+), 60 deletions(-) diff --git a/mysql-test/r/parser_precedence.result b/mysql-test/r/parser_precedence.result index e2d35521ca9..cf301ec677b 100644 --- a/mysql-test/r/parser_precedence.result +++ b/mysql-test/r/parser_precedence.result @@ -354,3 +354,394 @@ where (A OR (B XOR C)) != (A OR B XOR C); count(*) 0 drop table t1_30237_bool; +Testing that NOT has precedence over OR +select (NOT FALSE) OR TRUE, NOT (FALSE OR TRUE), NOT FALSE OR TRUE; +(NOT FALSE) OR TRUE NOT (FALSE OR TRUE) NOT FALSE OR TRUE +1 0 1 +Testing that NOT has precedence over XOR +select (NOT FALSE) XOR FALSE, NOT (FALSE XOR FALSE), NOT FALSE XOR FALSE; +(NOT FALSE) XOR FALSE NOT (FALSE XOR FALSE) NOT FALSE XOR FALSE +1 1 1 +Testing that NOT has precedence over AND +select (NOT FALSE) AND FALSE, NOT (FALSE AND FALSE), NOT FALSE AND FALSE; +(NOT FALSE) AND FALSE NOT (FALSE AND FALSE) NOT FALSE AND FALSE +0 1 0 +Testing that NOT is associative +select NOT NOT TRUE, NOT NOT NOT FALSE; +NOT NOT TRUE NOT NOT NOT FALSE +1 1 +Testing that IS has precedence over NOT +select (NOT NULL) IS TRUE, NOT (NULL IS TRUE), NOT NULL IS TRUE; +(NOT NULL) IS TRUE NOT (NULL IS TRUE) NOT NULL IS TRUE +0 1 1 +select (NOT NULL) IS NOT TRUE, NOT (NULL IS NOT TRUE), NOT NULL IS NOT TRUE; +(NOT NULL) IS NOT TRUE NOT (NULL IS NOT TRUE) NOT NULL IS NOT TRUE +1 0 0 +select (NOT NULL) IS FALSE, NOT (NULL IS FALSE), NOT NULL IS FALSE; +(NOT NULL) IS FALSE NOT (NULL IS FALSE) NOT NULL IS FALSE +0 1 1 +select (NOT NULL) IS NOT FALSE, NOT (NULL IS NOT FALSE), NOT NULL IS NOT FALSE; +(NOT NULL) IS NOT FALSE NOT (NULL IS NOT FALSE) NOT NULL IS NOT FALSE +1 0 0 +select (NOT TRUE) IS UNKNOWN, NOT (TRUE IS UNKNOWN), NOT TRUE IS UNKNOWN; +(NOT TRUE) IS UNKNOWN NOT (TRUE IS UNKNOWN) NOT TRUE IS UNKNOWN +0 1 1 +select (NOT TRUE) IS NOT UNKNOWN, NOT (TRUE IS NOT UNKNOWN), NOT TRUE IS NOT UNKNOWN; +(NOT TRUE) IS NOT UNKNOWN NOT (TRUE IS NOT UNKNOWN) NOT TRUE IS NOT UNKNOWN +1 0 0 +select (NOT TRUE) IS NULL, NOT (TRUE IS NULL), NOT TRUE IS NULL; +(NOT TRUE) IS NULL NOT (TRUE IS NULL) NOT TRUE IS NULL +0 1 1 +select (NOT TRUE) IS NOT NULL, NOT (TRUE IS NOT NULL), NOT TRUE IS NOT NULL; +(NOT TRUE) IS NOT NULL NOT (TRUE IS NOT NULL) NOT TRUE IS NOT NULL +1 0 0 +Testing that IS [NOT] TRUE/FALSE/UNKNOWN predicates are not associative +select TRUE IS TRUE IS TRUE IS TRUE; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS TRUE IS TRUE' at line 1 +select FALSE IS NOT TRUE IS NOT TRUE IS NOT TRUE; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS NOT TRUE IS NOT TRUE' at line 1 +select NULL IS FALSE IS FALSE IS FALSE; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS FALSE IS FALSE' at line 1 +select TRUE IS NOT FALSE IS NOT FALSE IS NOT FALSE; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS NOT FALSE IS NOT FALSE' at line 1 +select FALSE IS UNKNOWN IS UNKNOWN IS UNKNOWN; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS UNKNOWN IS UNKNOWN' at line 1 +select TRUE IS NOT UNKNOWN IS NOT UNKNOWN IS NOT UNKNOWN; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS NOT UNKNOWN IS NOT UNKNOWN' at line 1 +Testing that IS [NOT] NULL predicates are associative +select FALSE IS NULL IS NULL IS NULL; +FALSE IS NULL IS NULL IS NULL +0 +select TRUE IS NOT NULL IS NOT NULL IS NOT NULL; +TRUE IS NOT NULL IS NOT NULL IS NOT NULL +1 +Testing that comparison operators are left associative +select 1 <=> 2 <=> 2, (1 <=> 2) <=> 2, 1 <=> (2 <=> 2); +1 <=> 2 <=> 2 (1 <=> 2) <=> 2 1 <=> (2 <=> 2) +0 0 1 +select 1 = 2 = 2, (1 = 2) = 2, 1 = (2 = 2); +1 = 2 = 2 (1 = 2) = 2 1 = (2 = 2) +0 0 1 +select 1 != 2 != 3, (1 != 2) != 3, 1 != (2 != 3); +1 != 2 != 3 (1 != 2) != 3 1 != (2 != 3) +1 1 0 +select 1 <> 2 <> 3, (1 <> 2) <> 3, 1 <> (2 <> 3); +1 <> 2 <> 3 (1 <> 2) <> 3 1 <> (2 <> 3) +1 1 0 +select 1 < 2 < 3, (1 < 2) < 3, 1 < (2 < 3); +1 < 2 < 3 (1 < 2) < 3 1 < (2 < 3) +1 1 0 +select 3 <= 2 <= 1, (3 <= 2) <= 1, 3 <= (2 <= 1); +3 <= 2 <= 1 (3 <= 2) <= 1 3 <= (2 <= 1) +1 1 0 +select 1 > 2 > 3, (1 > 2) > 3, 1 > (2 > 3); +1 > 2 > 3 (1 > 2) > 3 1 > (2 > 3) +0 0 1 +select 1 >= 2 >= 3, (1 >= 2) >= 3, 1 >= (2 >= 3); +1 >= 2 >= 3 (1 >= 2) >= 3 1 >= (2 >= 3) +0 0 1 +Testing that | is associative +select 0xF0 | 0x0F | 0x55, (0xF0 | 0x0F) | 0x55, 0xF0 | (0x0F | 0x55); +0xF0 | 0x0F | 0x55 (0xF0 | 0x0F) | 0x55 0xF0 | (0x0F | 0x55) +255 255 255 +Testing that & is associative +select 0xF5 & 0x5F & 0x55, (0xF5 & 0x5F) & 0x55, 0xF5 & (0x5F & 0x55); +0xF5 & 0x5F & 0x55 (0xF5 & 0x5F) & 0x55 0xF5 & (0x5F & 0x55) +85 85 85 +Testing that << is left associative +select 4 << 3 << 2, (4 << 3) << 2, 4 << (3 << 2); +4 << 3 << 2 (4 << 3) << 2 4 << (3 << 2) +128 128 16384 +Testing that >> is left associative +select 256 >> 3 >> 2, (256 >> 3) >> 2, 256 >> (3 >> 2); +256 >> 3 >> 2 (256 >> 3) >> 2 256 >> (3 >> 2) +8 8 256 +Testing that & has precedence over | +select 0xF0 & 0x0F | 0x55, (0xF0 & 0x0F) | 0x55, 0xF0 & (0x0F | 0x55); +0xF0 & 0x0F | 0x55 (0xF0 & 0x0F) | 0x55 0xF0 & (0x0F | 0x55) +85 85 80 +select 0x55 | 0xF0 & 0x0F, (0x55 | 0xF0) & 0x0F, 0x55 | (0xF0 & 0x0F); +0x55 | 0xF0 & 0x0F (0x55 | 0xF0) & 0x0F 0x55 | (0xF0 & 0x0F) +85 5 85 +Testing that << has precedence over | +select 0x0F << 4 | 0x0F, (0x0F << 4) | 0x0F, 0x0F << (4 | 0x0F); +0x0F << 4 | 0x0F (0x0F << 4) | 0x0F 0x0F << (4 | 0x0F) +255 255 491520 +select 0x0F | 0x0F << 4, (0x0F | 0x0F) << 4, 0x0F | (0x0F << 4); +0x0F | 0x0F << 4 (0x0F | 0x0F) << 4 0x0F | (0x0F << 4) +255 240 255 +Testing that >> has precedence over | +select 0xF0 >> 4 | 0xFF, (0xF0 >> 4) | 0xFF, 0xF0 >> (4 | 0xFF); +0xF0 >> 4 | 0xFF (0xF0 >> 4) | 0xFF 0xF0 >> (4 | 0xFF) +255 255 0 +select 0xFF | 0xF0 >> 4, (0xFF | 0xF0) >> 4, 0xFF | (0xF0 >> 4); +0xFF | 0xF0 >> 4 (0xFF | 0xF0) >> 4 0xFF | (0xF0 >> 4) +255 15 255 +Testing that << has precedence over & +select 0x0F << 4 & 0xF0, (0x0F << 4) & 0xF0, 0x0F << (4 & 0xF0); +0x0F << 4 & 0xF0 (0x0F << 4) & 0xF0 0x0F << (4 & 0xF0) +240 240 15 +select 0xF0 & 0x0F << 4, (0xF0 & 0x0F) << 4, 0xF0 & (0x0F << 4); +0xF0 & 0x0F << 4 (0xF0 & 0x0F) << 4 0xF0 & (0x0F << 4) +240 0 240 +Testing that >> has precedence over & +select 0xF0 >> 4 & 0x55, (0xF0 >> 4) & 0x55, 0xF0 >> (4 & 0x55); +0xF0 >> 4 & 0x55 (0xF0 >> 4) & 0x55 0xF0 >> (4 & 0x55) +5 5 15 +select 0x0F & 0xF0 >> 4, (0x0F & 0xF0) >> 4, 0x0F & (0xF0 >> 4); +0x0F & 0xF0 >> 4 (0x0F & 0xF0) >> 4 0x0F & (0xF0 >> 4) +15 0 15 +Testing that >> and << have the same precedence +select 0xFF >> 4 << 2, (0xFF >> 4) << 2, 0xFF >> (4 << 2); +0xFF >> 4 << 2 (0xFF >> 4) << 2 0xFF >> (4 << 2) +60 60 0 +select 0x0F << 4 >> 2, (0x0F << 4) >> 2, 0x0F << (4 >> 2); +0x0F << 4 >> 2 (0x0F << 4) >> 2 0x0F << (4 >> 2) +60 60 30 +Testing that binary + is associative +select 1 + 2 + 3, (1 + 2) + 3, 1 + (2 + 3); +1 + 2 + 3 (1 + 2) + 3 1 + (2 + 3) +6 6 6 +Testing that binary - is left associative +select 1 - 2 - 3, (1 - 2) - 3, 1 - (2 - 3); +1 - 2 - 3 (1 - 2) - 3 1 - (2 - 3) +-4 -4 2 +Testing that binary + and binary - have the same precedence +select 1 + 2 - 3, (1 + 2) - 3, 1 + (2 - 3); +1 + 2 - 3 (1 + 2) - 3 1 + (2 - 3) +0 0 0 +select 1 - 2 + 3, (1 - 2) + 3, 1 - (2 + 3); +1 - 2 + 3 (1 - 2) + 3 1 - (2 + 3) +2 2 -4 +Testing that binary + has precedence over | +select 0xF0 + 0x0F | 0x55, (0xF0 + 0x0F) | 0x55, 0xF0 + (0x0F | 0x55); +0xF0 + 0x0F | 0x55 (0xF0 + 0x0F) | 0x55 0xF0 + (0x0F | 0x55) +255 255 335 +select 0x55 | 0xF0 + 0x0F, (0x55 | 0xF0) + 0x0F, 0x55 | (0xF0 + 0x0F); +0x55 | 0xF0 + 0x0F (0x55 | 0xF0) + 0x0F 0x55 | (0xF0 + 0x0F) +255 260 255 +Testing that binary + has precedence over & +select 0xF0 + 0x0F & 0x55, (0xF0 + 0x0F) & 0x55, 0xF0 + (0x0F & 0x55); +0xF0 + 0x0F & 0x55 (0xF0 + 0x0F) & 0x55 0xF0 + (0x0F & 0x55) +85 85 245 +select 0x55 & 0xF0 + 0x0F, (0x55 & 0xF0) + 0x0F, 0x55 & (0xF0 + 0x0F); +0x55 & 0xF0 + 0x0F (0x55 & 0xF0) + 0x0F 0x55 & (0xF0 + 0x0F) +85 95 85 +Testing that binary + has precedence over << +select 2 + 3 << 4, (2 + 3) << 4, 2 + (3 << 4); +2 + 3 << 4 (2 + 3) << 4 2 + (3 << 4) +80 80 50 +select 3 << 4 + 2, (3 << 4) + 2, 3 << (4 + 2); +3 << 4 + 2 (3 << 4) + 2 3 << (4 + 2) +192 50 192 +Testing that binary + has precedence over >> +select 4 + 3 >> 2, (4 + 3) >> 2, 4 + (3 >> 2); +4 + 3 >> 2 (4 + 3) >> 2 4 + (3 >> 2) +1 1 4 +select 3 >> 2 + 1, (3 >> 2) + 1, 3 >> (2 + 1); +3 >> 2 + 1 (3 >> 2) + 1 3 >> (2 + 1) +0 1 0 +Testing that binary - has precedence over | +select 0xFF - 0x0F | 0x55, (0xFF - 0x0F) | 0x55, 0xFF - (0x0F | 0x55); +0xFF - 0x0F | 0x55 (0xFF - 0x0F) | 0x55 0xFF - (0x0F | 0x55) +245 245 160 +select 0x55 | 0xFF - 0xF0, (0x55 | 0xFF) - 0xF0, 0x55 | (0xFF - 0xF0); +0x55 | 0xFF - 0xF0 (0x55 | 0xFF) - 0xF0 0x55 | (0xFF - 0xF0) +95 15 95 +Testing that binary - has precedence over & +select 0xFF - 0xF0 & 0x55, (0xFF - 0xF0) & 0x55, 0xFF - (0xF0 & 0x55); +0xFF - 0xF0 & 0x55 (0xFF - 0xF0) & 0x55 0xFF - (0xF0 & 0x55) +5 5 175 +select 0x55 & 0xFF - 0xF0, (0x55 & 0xFF) - 0xF0, 0x55 & (0xFF - 0xF0); +0x55 & 0xFF - 0xF0 (0x55 & 0xFF) - 0xF0 0x55 & (0xFF - 0xF0) +5 -155 5 +Testing that binary - has precedence over << +select 16 - 3 << 2, (16 - 3) << 2, 16 - (3 << 2); +16 - 3 << 2 (16 - 3) << 2 16 - (3 << 2) +52 52 4 +select 4 << 3 - 2, (4 << 3) - 2, 4 << (3 - 2); +4 << 3 - 2 (4 << 3) - 2 4 << (3 - 2) +8 30 8 +Testing that binary - has precedence over >> +select 16 - 3 >> 2, (16 - 3) >> 2, 16 - (3 >> 2); +16 - 3 >> 2 (16 - 3) >> 2 16 - (3 >> 2) +3 3 16 +select 16 >> 3 - 2, (16 >> 3) - 2, 16 >> (3 - 2); +16 >> 3 - 2 (16 >> 3) - 2 16 >> (3 - 2) +8 0 8 +Testing that * is associative +select 2 * 3 * 4, (2 * 3) * 4, 2 * (3 * 4); +2 * 3 * 4 (2 * 3) * 4 2 * (3 * 4) +24 24 24 +Testing that * has precedence over | +select 2 * 0x40 | 0x0F, (2 * 0x40) | 0x0F, 2 * (0x40 | 0x0F); +2 * 0x40 | 0x0F (2 * 0x40) | 0x0F 2 * (0x40 | 0x0F) +143 143 158 +select 0x0F | 2 * 0x40, (0x0F | 2) * 0x40, 0x0F | (2 * 0x40); +0x0F | 2 * 0x40 (0x0F | 2) * 0x40 0x0F | (2 * 0x40) +143 960 143 +Testing that * has precedence over & +select 2 * 0x40 & 0x55, (2 * 0x40) & 0x55, 2 * (0x40 & 0x55); +2 * 0x40 & 0x55 (2 * 0x40) & 0x55 2 * (0x40 & 0x55) +0 0 128 +select 0xF0 & 2 * 0x40, (0xF0 & 2) * 0x40, 0xF0 & (2 * 0x40); +0xF0 & 2 * 0x40 (0xF0 & 2) * 0x40 0xF0 & (2 * 0x40) +128 0 128 +Testing that * has precedence over << +select 5 * 3 << 4, (5 * 3) << 4, 5 * (3 << 4); +5 * 3 << 4 (5 * 3) << 4 5 * (3 << 4) +240 240 240 +select 2 << 3 * 4, (2 << 3) * 4, 2 << (3 * 4); +2 << 3 * 4 (2 << 3) * 4 2 << (3 * 4) +8192 64 8192 +Testing that * has precedence over >> +select 3 * 4 >> 2, (3 * 4) >> 2, 3 * (4 >> 2); +3 * 4 >> 2 (3 * 4) >> 2 3 * (4 >> 2) +3 3 3 +select 4 >> 2 * 3, (4 >> 2) * 3, 4 >> (2 * 3); +4 >> 2 * 3 (4 >> 2) * 3 4 >> (2 * 3) +0 3 0 +Testing that * has precedence over binary + +select 2 * 3 + 4, (2 * 3) + 4, 2 * (3 + 4); +2 * 3 + 4 (2 * 3) + 4 2 * (3 + 4) +10 10 14 +select 2 + 3 * 4, (2 + 3) * 4, 2 + (3 * 4); +2 + 3 * 4 (2 + 3) * 4 2 + (3 * 4) +14 20 14 +Testing that * has precedence over binary - +select 4 * 3 - 2, (4 * 3) - 2, 4 * (3 - 2); +4 * 3 - 2 (4 * 3) - 2 4 * (3 - 2) +10 10 4 +select 4 - 3 * 2, (4 - 3) * 2, 4 - (3 * 2); +4 - 3 * 2 (4 - 3) * 2 4 - (3 * 2) +-2 2 -2 +Testing that / is left associative +select 15 / 5 / 3, (15 / 5) / 3, 15 / (5 / 3); +15 / 5 / 3 (15 / 5) / 3 15 / (5 / 3) +1.00000000 1.00000000 9.0000 +Testing that / has precedence over | +select 105 / 5 | 2, (105 / 5) | 2, 105 / (5 | 2); +105 / 5 | 2 (105 / 5) | 2 105 / (5 | 2) +23 23 15.0000 +select 105 | 2 / 5, (105 | 2) / 5, 105 | (2 / 5); +105 | 2 / 5 (105 | 2) / 5 105 | (2 / 5) +105 21.4000 105 +Testing that / has precedence over & +select 105 / 5 & 0x0F, (105 / 5) & 0x0F, 105 / (5 & 0x0F); +105 / 5 & 0x0F (105 / 5) & 0x0F 105 / (5 & 0x0F) +5 5 21.0000 +select 0x0F & 105 / 5, (0x0F & 105) / 5, 0x0F & (105 / 5); +0x0F & 105 / 5 (0x0F & 105) / 5 0x0F & (105 / 5) +5 1.8000 5 +Testing that / has precedence over << +select 0x80 / 4 << 2, (0x80 / 4) << 2, 0x80 / (4 << 2); +0x80 / 4 << 2 (0x80 / 4) << 2 0x80 / (4 << 2) +128 128 8.0000 +select 0x80 << 4 / 2, (0x80 << 4) / 2, 0x80 << (4 / 2); +0x80 << 4 / 2 (0x80 << 4) / 2 0x80 << (4 / 2) +512 1024.0000 512 +Testing that / has precedence over >> +select 0x80 / 4 >> 2, (0x80 / 4) >> 2, 0x80 / (4 >> 2); +0x80 / 4 >> 2 (0x80 / 4) >> 2 0x80 / (4 >> 2) +8 8 128.0000 +select 0x80 >> 4 / 2, (0x80 >> 4) / 2, 0x80 >> (4 / 2); +0x80 >> 4 / 2 (0x80 >> 4) / 2 0x80 >> (4 / 2) +32 4.0000 32 +Testing that / has precedence over binary + +select 0x80 / 2 + 2, (0x80 / 2) + 2, 0x80 / (2 + 2); +0x80 / 2 + 2 (0x80 / 2) + 2 0x80 / (2 + 2) +66.0000 66.0000 32.0000 +select 0x80 + 2 / 2, (0x80 + 2) / 2, 0x80 + (2 / 2); +0x80 + 2 / 2 (0x80 + 2) / 2 0x80 + (2 / 2) +129.0000 65.0000 129.0000 +Testing that / has precedence over binary - +select 0x80 / 4 - 2, (0x80 / 4) - 2, 0x80 / (4 - 2); +0x80 / 4 - 2 (0x80 / 4) - 2 0x80 / (4 - 2) +30.0000 30.0000 64.0000 +select 0x80 - 4 / 2, (0x80 - 4) / 2, 0x80 - (4 / 2); +0x80 - 4 / 2 (0x80 - 4) / 2 0x80 - (4 / 2) +126.0000 62.0000 126.0000 +Testing that ^ is associative +select 0xFF ^ 0xF0 ^ 0x0F, (0xFF ^ 0xF0) ^ 0x0F, 0xFF ^ (0xF0 ^ 0x0F); +0xFF ^ 0xF0 ^ 0x0F (0xFF ^ 0xF0) ^ 0x0F 0xFF ^ (0xF0 ^ 0x0F) +0 0 0 +select 0xFF ^ 0xF0 ^ 0x55, (0xFF ^ 0xF0) ^ 0x55, 0xFF ^ (0xF0 ^ 0x55); +0xFF ^ 0xF0 ^ 0x55 (0xFF ^ 0xF0) ^ 0x55 0xFF ^ (0xF0 ^ 0x55) +90 90 90 +Testing that ^ has precedence over | +select 0xFF ^ 0xF0 | 0x0F, (0xFF ^ 0xF0) | 0x0F, 0xFF ^ (0xF0 | 0x0F); +0xFF ^ 0xF0 | 0x0F (0xFF ^ 0xF0) | 0x0F 0xFF ^ (0xF0 | 0x0F) +15 15 0 +select 0xF0 | 0xFF ^ 0xF0, (0xF0 | 0xFF) ^ 0xF0, 0xF0 | (0xFF ^ 0xF0); +0xF0 | 0xFF ^ 0xF0 (0xF0 | 0xFF) ^ 0xF0 0xF0 | (0xFF ^ 0xF0) +255 15 255 +Testing that ^ has precedence over & +select 0xFF ^ 0xF0 & 0x0F, (0xFF ^ 0xF0) & 0x0F, 0xFF ^ (0xF0 & 0x0F); +0xFF ^ 0xF0 & 0x0F (0xFF ^ 0xF0) & 0x0F 0xFF ^ (0xF0 & 0x0F) +15 15 255 +select 0x0F & 0xFF ^ 0xF0, (0x0F & 0xFF) ^ 0xF0, 0x0F & (0xFF ^ 0xF0); +0x0F & 0xFF ^ 0xF0 (0x0F & 0xFF) ^ 0xF0 0x0F & (0xFF ^ 0xF0) +15 255 15 +Testing that ^ has precedence over << +select 0xFF ^ 0xF0 << 2, (0xFF ^ 0xF0) << 2, 0xFF ^ (0xF0 << 2); +0xFF ^ 0xF0 << 2 (0xFF ^ 0xF0) << 2 0xFF ^ (0xF0 << 2) +60 60 831 +select 0x0F << 2 ^ 0xFF, (0x0F << 2) ^ 0xFF, 0x0F << (2 ^ 0xFF); +0x0F << 2 ^ 0xFF (0x0F << 2) ^ 0xFF 0x0F << (2 ^ 0xFF) +0 195 0 +Testing that ^ has precedence over >> +select 0xFF ^ 0xF0 >> 2, (0xFF ^ 0xF0) >> 2, 0xFF ^ (0xF0 >> 2); +0xFF ^ 0xF0 >> 2 (0xFF ^ 0xF0) >> 2 0xFF ^ (0xF0 >> 2) +3 3 195 +select 0xFF >> 2 ^ 0xF0, (0xFF >> 2) ^ 0xF0, 0xFF >> (2 ^ 0xF0); +0xFF >> 2 ^ 0xF0 (0xFF >> 2) ^ 0xF0 0xFF >> (2 ^ 0xF0) +0 207 0 +Testing that ^ has precedence over binary + +select 0xFF ^ 0xF0 + 0x0F, (0xFF ^ 0xF0) + 0x0F, 0xFF ^ (0xF0 + 0x0F); +0xFF ^ 0xF0 + 0x0F (0xFF ^ 0xF0) + 0x0F 0xFF ^ (0xF0 + 0x0F) +30 30 0 +select 0x0F + 0xFF ^ 0xF0, (0x0F + 0xFF) ^ 0xF0, 0x0F + (0xFF ^ 0xF0); +0x0F + 0xFF ^ 0xF0 (0x0F + 0xFF) ^ 0xF0 0x0F + (0xFF ^ 0xF0) +30 510 30 +Testing that ^ has precedence over binary - +select 0xFF ^ 0xF0 - 1, (0xFF ^ 0xF0) - 1, 0xFF ^ (0xF0 - 1); +0xFF ^ 0xF0 - 1 (0xFF ^ 0xF0) - 1 0xFF ^ (0xF0 - 1) +14 14 16 +select 0x55 - 0x0F ^ 0x55, (0x55 - 0x0F) ^ 0x55, 0x55 - (0x0F ^ 0x55); +0x55 - 0x0F ^ 0x55 (0x55 - 0x0F) ^ 0x55 0x55 - (0x0F ^ 0x55) +-5 19 -5 +Testing that ^ has precedence over * +select 0xFF ^ 0xF0 * 2, (0xFF ^ 0xF0) * 2, 0xFF ^ (0xF0 * 2); +0xFF ^ 0xF0 * 2 (0xFF ^ 0xF0) * 2 0xFF ^ (0xF0 * 2) +30 30 287 +select 2 * 0xFF ^ 0xF0, (2 * 0xFF) ^ 0xF0, 2 * (0xFF ^ 0xF0); +2 * 0xFF ^ 0xF0 (2 * 0xFF) ^ 0xF0 2 * (0xFF ^ 0xF0) +30 270 30 +Testing that ^ has precedence over / +select 0xFF ^ 0xF0 / 2, (0xFF ^ 0xF0) / 2, 0xFF ^ (0xF0 / 2); +0xFF ^ 0xF0 / 2 (0xFF ^ 0xF0) / 2 0xFF ^ (0xF0 / 2) +7.5000 7.5000 135 +select 0xF2 / 2 ^ 0xF0, (0xF2 / 2) ^ 0xF0, 0xF2 / (2 ^ 0xF0); +0xF2 / 2 ^ 0xF0 (0xF2 / 2) ^ 0xF0 0xF2 / (2 ^ 0xF0) +1.0000 137 1.0000 +Testing that ^ has precedence over % +select 0xFF ^ 0xF0 % 0x20, (0xFF ^ 0xF0) % 0x20, 0xFF ^ (0xF0 % 0x20); +0xFF ^ 0xF0 % 0x20 (0xFF ^ 0xF0) % 0x20 0xFF ^ (0xF0 % 0x20) +15 15 239 +select 0xFF % 0x20 ^ 0xF0, (0xFF % 0x20) ^ 0xF0, 0xFF % (0x20 ^ 0xF0); +0xFF % 0x20 ^ 0xF0 (0xFF % 0x20) ^ 0xF0 0xFF % (0x20 ^ 0xF0) +47 239 47 +Testing that ^ has precedence over DIV +select 0xFF ^ 0xF0 DIV 2, (0xFF ^ 0xF0) DIV 2, 0xFF ^ (0xF0 DIV 2); +0xFF ^ 0xF0 DIV 2 (0xFF ^ 0xF0) DIV 2 0xFF ^ (0xF0 DIV 2) +7 7 135 +select 0xF2 DIV 2 ^ 0xF0, (0xF2 DIV 2) ^ 0xF0, 0xF2 DIV (2 ^ 0xF0); +0xF2 DIV 2 ^ 0xF0 (0xF2 DIV 2) ^ 0xF0 0xF2 DIV (2 ^ 0xF0) +1 137 1 +Testing that ^ has precedence over MOD +select 0xFF ^ 0xF0 MOD 0x20, (0xFF ^ 0xF0) MOD 0x20, 0xFF ^ (0xF0 MOD 0x20); +0xFF ^ 0xF0 MOD 0x20 (0xFF ^ 0xF0) MOD 0x20 0xFF ^ (0xF0 MOD 0x20) +15 15 239 +select 0xFF MOD 0x20 ^ 0xF0, (0xFF MOD 0x20) ^ 0xF0, 0xFF MOD (0x20 ^ 0xF0); +0xFF MOD 0x20 ^ 0xF0 (0xFF MOD 0x20) ^ 0xF0 0xFF MOD (0x20 ^ 0xF0) +47 239 47 diff --git a/mysql-test/t/parser_precedence.test b/mysql-test/t/parser_precedence.test index a3a80776fb1..484c8759779 100644 --- a/mysql-test/t/parser_precedence.test +++ b/mysql-test/t/parser_precedence.test @@ -91,3 +91,243 @@ select count(*) from t1_30237_bool drop table t1_30237_bool; +--echo Testing that NOT has precedence over OR +select (NOT FALSE) OR TRUE, NOT (FALSE OR TRUE), NOT FALSE OR TRUE; + +--echo Testing that NOT has precedence over XOR +select (NOT FALSE) XOR FALSE, NOT (FALSE XOR FALSE), NOT FALSE XOR FALSE; + +--echo Testing that NOT has precedence over AND +select (NOT FALSE) AND FALSE, NOT (FALSE AND FALSE), NOT FALSE AND FALSE; + +--echo Testing that NOT is associative +select NOT NOT TRUE, NOT NOT NOT FALSE; + +--echo Testing that IS has precedence over NOT +select (NOT NULL) IS TRUE, NOT (NULL IS TRUE), NOT NULL IS TRUE; +select (NOT NULL) IS NOT TRUE, NOT (NULL IS NOT TRUE), NOT NULL IS NOT TRUE; +select (NOT NULL) IS FALSE, NOT (NULL IS FALSE), NOT NULL IS FALSE; +select (NOT NULL) IS NOT FALSE, NOT (NULL IS NOT FALSE), NOT NULL IS NOT FALSE; +select (NOT TRUE) IS UNKNOWN, NOT (TRUE IS UNKNOWN), NOT TRUE IS UNKNOWN; +select (NOT TRUE) IS NOT UNKNOWN, NOT (TRUE IS NOT UNKNOWN), NOT TRUE IS NOT UNKNOWN; +select (NOT TRUE) IS NULL, NOT (TRUE IS NULL), NOT TRUE IS NULL; +select (NOT TRUE) IS NOT NULL, NOT (TRUE IS NOT NULL), NOT TRUE IS NOT NULL; + +--echo Testing that IS [NOT] TRUE/FALSE/UNKNOWN predicates are not associative +# Documenting existing behavior in 5.0.48 +-- error ER_PARSE_ERROR +select TRUE IS TRUE IS TRUE IS TRUE; +-- error ER_PARSE_ERROR +select FALSE IS NOT TRUE IS NOT TRUE IS NOT TRUE; +-- error ER_PARSE_ERROR +select NULL IS FALSE IS FALSE IS FALSE; +-- error ER_PARSE_ERROR +select TRUE IS NOT FALSE IS NOT FALSE IS NOT FALSE; +-- error ER_PARSE_ERROR +select FALSE IS UNKNOWN IS UNKNOWN IS UNKNOWN; +-- error ER_PARSE_ERROR +select TRUE IS NOT UNKNOWN IS NOT UNKNOWN IS NOT UNKNOWN; + +--echo Testing that IS [NOT] NULL predicates are associative +# Documenting existing behavior in 5.0.48 +select FALSE IS NULL IS NULL IS NULL; +select TRUE IS NOT NULL IS NOT NULL IS NOT NULL; + +--echo Testing that comparison operators are left associative +select 1 <=> 2 <=> 2, (1 <=> 2) <=> 2, 1 <=> (2 <=> 2); +select 1 = 2 = 2, (1 = 2) = 2, 1 = (2 = 2); +select 1 != 2 != 3, (1 != 2) != 3, 1 != (2 != 3); +select 1 <> 2 <> 3, (1 <> 2) <> 3, 1 <> (2 <> 3); +select 1 < 2 < 3, (1 < 2) < 3, 1 < (2 < 3); +select 3 <= 2 <= 1, (3 <= 2) <= 1, 3 <= (2 <= 1); +select 1 > 2 > 3, (1 > 2) > 3, 1 > (2 > 3); +select 1 >= 2 >= 3, (1 >= 2) >= 3, 1 >= (2 >= 3); + +-- echo Testing that | is associative +select 0xF0 | 0x0F | 0x55, (0xF0 | 0x0F) | 0x55, 0xF0 | (0x0F | 0x55); + +-- echo Testing that & is associative +select 0xF5 & 0x5F & 0x55, (0xF5 & 0x5F) & 0x55, 0xF5 & (0x5F & 0x55); + +-- echo Testing that << is left associative +select 4 << 3 << 2, (4 << 3) << 2, 4 << (3 << 2); + +-- echo Testing that >> is left associative +select 256 >> 3 >> 2, (256 >> 3) >> 2, 256 >> (3 >> 2); + +--echo Testing that & has precedence over | +select 0xF0 & 0x0F | 0x55, (0xF0 & 0x0F) | 0x55, 0xF0 & (0x0F | 0x55); +select 0x55 | 0xF0 & 0x0F, (0x55 | 0xF0) & 0x0F, 0x55 | (0xF0 & 0x0F); + +--echo Testing that << has precedence over | +select 0x0F << 4 | 0x0F, (0x0F << 4) | 0x0F, 0x0F << (4 | 0x0F); +select 0x0F | 0x0F << 4, (0x0F | 0x0F) << 4, 0x0F | (0x0F << 4); + +--echo Testing that >> has precedence over | +select 0xF0 >> 4 | 0xFF, (0xF0 >> 4) | 0xFF, 0xF0 >> (4 | 0xFF); +select 0xFF | 0xF0 >> 4, (0xFF | 0xF0) >> 4, 0xFF | (0xF0 >> 4); + +--echo Testing that << has precedence over & +select 0x0F << 4 & 0xF0, (0x0F << 4) & 0xF0, 0x0F << (4 & 0xF0); +select 0xF0 & 0x0F << 4, (0xF0 & 0x0F) << 4, 0xF0 & (0x0F << 4); + +--echo Testing that >> has precedence over & +select 0xF0 >> 4 & 0x55, (0xF0 >> 4) & 0x55, 0xF0 >> (4 & 0x55); +select 0x0F & 0xF0 >> 4, (0x0F & 0xF0) >> 4, 0x0F & (0xF0 >> 4); + +--echo Testing that >> and << have the same precedence +select 0xFF >> 4 << 2, (0xFF >> 4) << 2, 0xFF >> (4 << 2); +select 0x0F << 4 >> 2, (0x0F << 4) >> 2, 0x0F << (4 >> 2); + +--echo Testing that binary + is associative +select 1 + 2 + 3, (1 + 2) + 3, 1 + (2 + 3); + +--echo Testing that binary - is left associative +select 1 - 2 - 3, (1 - 2) - 3, 1 - (2 - 3); + +--echo Testing that binary + and binary - have the same precedence +# evaluated left to right +select 1 + 2 - 3, (1 + 2) - 3, 1 + (2 - 3); +select 1 - 2 + 3, (1 - 2) + 3, 1 - (2 + 3); + +--echo Testing that binary + has precedence over | +select 0xF0 + 0x0F | 0x55, (0xF0 + 0x0F) | 0x55, 0xF0 + (0x0F | 0x55); +select 0x55 | 0xF0 + 0x0F, (0x55 | 0xF0) + 0x0F, 0x55 | (0xF0 + 0x0F); + +--echo Testing that binary + has precedence over & +select 0xF0 + 0x0F & 0x55, (0xF0 + 0x0F) & 0x55, 0xF0 + (0x0F & 0x55); +select 0x55 & 0xF0 + 0x0F, (0x55 & 0xF0) + 0x0F, 0x55 & (0xF0 + 0x0F); + +--echo Testing that binary + has precedence over << +select 2 + 3 << 4, (2 + 3) << 4, 2 + (3 << 4); +select 3 << 4 + 2, (3 << 4) + 2, 3 << (4 + 2); + +--echo Testing that binary + has precedence over >> +select 4 + 3 >> 2, (4 + 3) >> 2, 4 + (3 >> 2); +select 3 >> 2 + 1, (3 >> 2) + 1, 3 >> (2 + 1); + +--echo Testing that binary - has precedence over | +select 0xFF - 0x0F | 0x55, (0xFF - 0x0F) | 0x55, 0xFF - (0x0F | 0x55); +select 0x55 | 0xFF - 0xF0, (0x55 | 0xFF) - 0xF0, 0x55 | (0xFF - 0xF0); + +--echo Testing that binary - has precedence over & +select 0xFF - 0xF0 & 0x55, (0xFF - 0xF0) & 0x55, 0xFF - (0xF0 & 0x55); +select 0x55 & 0xFF - 0xF0, (0x55 & 0xFF) - 0xF0, 0x55 & (0xFF - 0xF0); + +--echo Testing that binary - has precedence over << +select 16 - 3 << 2, (16 - 3) << 2, 16 - (3 << 2); +select 4 << 3 - 2, (4 << 3) - 2, 4 << (3 - 2); + +--echo Testing that binary - has precedence over >> +select 16 - 3 >> 2, (16 - 3) >> 2, 16 - (3 >> 2); +select 16 >> 3 - 2, (16 >> 3) - 2, 16 >> (3 - 2); + +--echo Testing that * is associative +select 2 * 3 * 4, (2 * 3) * 4, 2 * (3 * 4); + +--echo Testing that * has precedence over | +select 2 * 0x40 | 0x0F, (2 * 0x40) | 0x0F, 2 * (0x40 | 0x0F); +select 0x0F | 2 * 0x40, (0x0F | 2) * 0x40, 0x0F | (2 * 0x40); + +--echo Testing that * has precedence over & +select 2 * 0x40 & 0x55, (2 * 0x40) & 0x55, 2 * (0x40 & 0x55); +select 0xF0 & 2 * 0x40, (0xF0 & 2) * 0x40, 0xF0 & (2 * 0x40); + +--echo Testing that * has precedence over << +# Actually, can't prove it for the first case, +# since << is a multiplication by a power of 2, +# and * is associative +select 5 * 3 << 4, (5 * 3) << 4, 5 * (3 << 4); +select 2 << 3 * 4, (2 << 3) * 4, 2 << (3 * 4); + +--echo Testing that * has precedence over >> +# >> is a multiplication by a (negative) power of 2, +# see above. +select 3 * 4 >> 2, (3 * 4) >> 2, 3 * (4 >> 2); +select 4 >> 2 * 3, (4 >> 2) * 3, 4 >> (2 * 3); + +--echo Testing that * has precedence over binary + +select 2 * 3 + 4, (2 * 3) + 4, 2 * (3 + 4); +select 2 + 3 * 4, (2 + 3) * 4, 2 + (3 * 4); + +--echo Testing that * has precedence over binary - +select 4 * 3 - 2, (4 * 3) - 2, 4 * (3 - 2); +select 4 - 3 * 2, (4 - 3) * 2, 4 - (3 * 2); + +--echo Testing that / is left associative +select 15 / 5 / 3, (15 / 5) / 3, 15 / (5 / 3); + +--echo Testing that / has precedence over | +select 105 / 5 | 2, (105 / 5) | 2, 105 / (5 | 2); +select 105 | 2 / 5, (105 | 2) / 5, 105 | (2 / 5); + +--echo Testing that / has precedence over & +select 105 / 5 & 0x0F, (105 / 5) & 0x0F, 105 / (5 & 0x0F); +select 0x0F & 105 / 5, (0x0F & 105) / 5, 0x0F & (105 / 5); + +--echo Testing that / has precedence over << +select 0x80 / 4 << 2, (0x80 / 4) << 2, 0x80 / (4 << 2); +select 0x80 << 4 / 2, (0x80 << 4) / 2, 0x80 << (4 / 2); + +--echo Testing that / has precedence over >> +select 0x80 / 4 >> 2, (0x80 / 4) >> 2, 0x80 / (4 >> 2); +select 0x80 >> 4 / 2, (0x80 >> 4) / 2, 0x80 >> (4 / 2); + +--echo Testing that / has precedence over binary + +select 0x80 / 2 + 2, (0x80 / 2) + 2, 0x80 / (2 + 2); +select 0x80 + 2 / 2, (0x80 + 2) / 2, 0x80 + (2 / 2); + +--echo Testing that / has precedence over binary - +select 0x80 / 4 - 2, (0x80 / 4) - 2, 0x80 / (4 - 2); +select 0x80 - 4 / 2, (0x80 - 4) / 2, 0x80 - (4 / 2); + +# TODO: %, DIV, MOD + +--echo Testing that ^ is associative +select 0xFF ^ 0xF0 ^ 0x0F, (0xFF ^ 0xF0) ^ 0x0F, 0xFF ^ (0xF0 ^ 0x0F); +select 0xFF ^ 0xF0 ^ 0x55, (0xFF ^ 0xF0) ^ 0x55, 0xFF ^ (0xF0 ^ 0x55); + +--echo Testing that ^ has precedence over | +select 0xFF ^ 0xF0 | 0x0F, (0xFF ^ 0xF0) | 0x0F, 0xFF ^ (0xF0 | 0x0F); +select 0xF0 | 0xFF ^ 0xF0, (0xF0 | 0xFF) ^ 0xF0, 0xF0 | (0xFF ^ 0xF0); + +--echo Testing that ^ has precedence over & +select 0xFF ^ 0xF0 & 0x0F, (0xFF ^ 0xF0) & 0x0F, 0xFF ^ (0xF0 & 0x0F); +select 0x0F & 0xFF ^ 0xF0, (0x0F & 0xFF) ^ 0xF0, 0x0F & (0xFF ^ 0xF0); + +--echo Testing that ^ has precedence over << +select 0xFF ^ 0xF0 << 2, (0xFF ^ 0xF0) << 2, 0xFF ^ (0xF0 << 2); +select 0x0F << 2 ^ 0xFF, (0x0F << 2) ^ 0xFF, 0x0F << (2 ^ 0xFF); + +--echo Testing that ^ has precedence over >> +select 0xFF ^ 0xF0 >> 2, (0xFF ^ 0xF0) >> 2, 0xFF ^ (0xF0 >> 2); +select 0xFF >> 2 ^ 0xF0, (0xFF >> 2) ^ 0xF0, 0xFF >> (2 ^ 0xF0); + +--echo Testing that ^ has precedence over binary + +select 0xFF ^ 0xF0 + 0x0F, (0xFF ^ 0xF0) + 0x0F, 0xFF ^ (0xF0 + 0x0F); +select 0x0F + 0xFF ^ 0xF0, (0x0F + 0xFF) ^ 0xF0, 0x0F + (0xFF ^ 0xF0); + +--echo Testing that ^ has precedence over binary - +select 0xFF ^ 0xF0 - 1, (0xFF ^ 0xF0) - 1, 0xFF ^ (0xF0 - 1); +select 0x55 - 0x0F ^ 0x55, (0x55 - 0x0F) ^ 0x55, 0x55 - (0x0F ^ 0x55); + +--echo Testing that ^ has precedence over * +select 0xFF ^ 0xF0 * 2, (0xFF ^ 0xF0) * 2, 0xFF ^ (0xF0 * 2); +select 2 * 0xFF ^ 0xF0, (2 * 0xFF) ^ 0xF0, 2 * (0xFF ^ 0xF0); + +--echo Testing that ^ has precedence over / +select 0xFF ^ 0xF0 / 2, (0xFF ^ 0xF0) / 2, 0xFF ^ (0xF0 / 2); +select 0xF2 / 2 ^ 0xF0, (0xF2 / 2) ^ 0xF0, 0xF2 / (2 ^ 0xF0); + +--echo Testing that ^ has precedence over % +select 0xFF ^ 0xF0 % 0x20, (0xFF ^ 0xF0) % 0x20, 0xFF ^ (0xF0 % 0x20); +select 0xFF % 0x20 ^ 0xF0, (0xFF % 0x20) ^ 0xF0, 0xFF % (0x20 ^ 0xF0); + +--echo Testing that ^ has precedence over DIV +select 0xFF ^ 0xF0 DIV 2, (0xFF ^ 0xF0) DIV 2, 0xFF ^ (0xF0 DIV 2); +select 0xF2 DIV 2 ^ 0xF0, (0xF2 DIV 2) ^ 0xF0, 0xF2 DIV (2 ^ 0xF0); + +--echo Testing that ^ has precedence over MOD +select 0xFF ^ 0xF0 MOD 0x20, (0xFF ^ 0xF0) MOD 0x20, 0xFF ^ (0xF0 MOD 0x20); +select 0xFF MOD 0x20 ^ 0xF0, (0xFF MOD 0x20) ^ 0xF0, 0xFF MOD (0x20 ^ 0xF0); + diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index bfce73716c7..e0b9ab28594 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -1068,9 +1068,9 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %type literal text_literal insert_ident order_ident simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr - variable variable_aux bool_factor - bool_test bool_pri - predicate bit_expr bit_term bit_factor value_expr term factor + variable variable_aux + bool_pri + predicate bit_expr table_wild simple_expr udf_expr expr_or_default set_expr_or_default interval_expr param_marker geometry_function @@ -4468,8 +4468,7 @@ optional_braces: /* all possible expressions */ expr: - bool_factor - | expr or expr %prec OR_SYM + expr or expr %prec OR_SYM { /* Design notes: @@ -4564,30 +4563,30 @@ expr: $$ = new (YYTHD->mem_root) Item_cond_and($1, $3); } } - ; - -bool_factor: - NOT_SYM bool_factor { $$= negate_expression(YYTHD, $2); } - | bool_test ; - -bool_test: - bool_pri IS TRUE_SYM + | NOT_SYM expr %prec NOT_SYM + { $$= negate_expression(YYTHD, $2); } + | bool_pri IS TRUE_SYM %prec IS { $$= new (YYTHD->mem_root) Item_func_istrue($1); } - | bool_pri IS not TRUE_SYM + | bool_pri IS not TRUE_SYM %prec IS { $$= new (YYTHD->mem_root) Item_func_isnottrue($1); } - | bool_pri IS FALSE_SYM + | bool_pri IS FALSE_SYM %prec IS { $$= new (YYTHD->mem_root) Item_func_isfalse($1); } - | bool_pri IS not FALSE_SYM + | bool_pri IS not FALSE_SYM %prec IS { $$= new (YYTHD->mem_root) Item_func_isnotfalse($1); } - | bool_pri IS UNKNOWN_SYM { $$= new Item_func_isnull($1); } - | bool_pri IS not UNKNOWN_SYM { $$= new Item_func_isnotnull($1); } + | bool_pri IS UNKNOWN_SYM %prec IS + { $$= new Item_func_isnull($1); } + | bool_pri IS not UNKNOWN_SYM %prec IS + { $$= new Item_func_isnotnull($1); } | bool_pri ; bool_pri: - bool_pri IS NULL_SYM { $$= new Item_func_isnull($1); } - | bool_pri IS not NULL_SYM { $$= new Item_func_isnotnull($1); } - | bool_pri EQUAL_SYM predicate { $$= new Item_func_equal($1,$3); } + bool_pri IS NULL_SYM %prec IS + { $$= new Item_func_isnull($1); } + | bool_pri IS not NULL_SYM %prec IS + { $$= new Item_func_isnotnull($1); } + | bool_pri EQUAL_SYM predicate %prec EQUAL_SYM + { $$= new Item_func_equal($1,$3); } | bool_pri comp_op predicate %prec EQ { $$= (*$2)(0)->create($1,$3); } | bool_pri comp_op all_or_any '(' subselect ')' %prec EQ @@ -4630,11 +4629,11 @@ predicate: | bit_expr BETWEEN_SYM bit_expr AND_SYM predicate { $$= new Item_func_between($1,$3,$5); } | bit_expr not BETWEEN_SYM bit_expr AND_SYM predicate - { - Item_func_between *item= new Item_func_between($1,$4,$6); - item->negate(); - $$= item; - } + { + Item_func_between *item= new Item_func_between($1,$4,$6); + item->negate(); + $$= item; + } | bit_expr SOUNDS_SYM LIKE bit_expr { $$= new Item_func_eq(new Item_func_soundex($1), new Item_func_soundex($4)); } @@ -4648,40 +4647,36 @@ predicate: | bit_expr ; bit_expr: - bit_expr '|' bit_term { $$= new Item_func_bit_or($1,$3); } - | bit_term ; - -bit_term: - bit_term '&' bit_factor { $$= new Item_func_bit_and($1,$3); } - | bit_factor ; - -bit_factor: - bit_factor SHIFT_LEFT value_expr - { $$= new Item_func_shift_left($1,$3); } - | bit_factor SHIFT_RIGHT value_expr - { $$= new Item_func_shift_right($1,$3); } - | value_expr ; - -value_expr: - value_expr '+' term { $$= new Item_func_plus($1,$3); } - | value_expr '-' term { $$= new Item_func_minus($1,$3); } - | value_expr '+' interval_expr interval - { $$= new Item_date_add_interval($1,$3,$4,0); } - | value_expr '-' interval_expr interval - { $$= new Item_date_add_interval($1,$3,$4,1); } - | term ; - -term: - term '*' factor { $$= new Item_func_mul($1,$3); } - | term '/' factor { $$= new Item_func_div($1,$3); } - | term '%' factor { $$= new Item_func_mod($1,$3); } - | term DIV_SYM factor { $$= new Item_func_int_div($1,$3); } - | term MOD_SYM factor { $$= new Item_func_mod($1,$3); } - | factor ; - -factor: - factor '^' simple_expr { $$= new Item_func_bit_xor($1,$3); } - | simple_expr ; + bit_expr '|' bit_expr %prec '|' + { $$= new Item_func_bit_or($1,$3); } + | bit_expr '&' bit_expr %prec '&' + { $$= new Item_func_bit_and($1,$3); } + | bit_expr SHIFT_LEFT bit_expr %prec SHIFT_LEFT + { $$= new Item_func_shift_left($1,$3); } + | bit_expr SHIFT_RIGHT bit_expr %prec SHIFT_RIGHT + { $$= new Item_func_shift_right($1,$3); } + | bit_expr '+' bit_expr %prec '+' + { $$= new Item_func_plus($1,$3); } + | bit_expr '-' bit_expr %prec '-' + { $$= new Item_func_minus($1,$3); } + | bit_expr '+' interval_expr interval %prec '+' + { $$= new Item_date_add_interval($1,$3,$4,0); } + | bit_expr '-' interval_expr interval %prec '-' + { $$= new Item_date_add_interval($1,$3,$4,1); } + | bit_expr '*' bit_expr %prec '*' + { $$= new Item_func_mul($1,$3); } + | bit_expr '/' bit_expr %prec '/' + { $$= new Item_func_div($1,$3); } + | bit_expr '%' bit_expr %prec '%' + { $$= new Item_func_mod($1,$3); } + | bit_expr DIV_SYM bit_expr %prec DIV_SYM + { $$= new Item_func_int_div($1,$3); } + | bit_expr MOD_SYM bit_expr %prec MOD_SYM + { $$= new Item_func_mod($1,$3); } + | bit_expr '^' bit_expr + { $$= new Item_func_bit_xor($1,$3); } + | simple_expr + ; or: OR_SYM | OR2_SYM; and: AND_SYM | AND_AND_SYM; From d25340c93d4475569c8f13625de60938c7fb8c10 Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Tue, 28 Aug 2007 15:56:12 -0600 Subject: [PATCH 13/35] Manual merge --- sql/sql_yacc.yy | 100 +++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 56 deletions(-) diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 24ee080a7aa..29c00bc54d0 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -1152,8 +1152,8 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %type literal text_literal insert_ident order_ident simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr - variable variable_aux bool_factor bool_test bool_pri - predicate bit_expr bit_term bit_factor value_expr term factor + variable variable_aux bool_pri + predicate bit_expr table_wild simple_expr udf_expr expr_or_default set_expr_or_default interval_expr param_marker geometry_function @@ -6558,8 +6558,7 @@ optional_braces: /* all possible expressions */ expr: - bool_factor - | expr or expr %prec OR_SYM + expr or expr %prec OR_SYM { /* Design notes: @@ -6654,36 +6653,35 @@ expr: $$ = new (YYTHD->mem_root) Item_cond_and($1, $3); } } - ; - -bool_factor: - NOT_SYM bool_factor { $$= negate_expression(YYTHD, $2); } - | bool_test - ; - -bool_test: - bool_pri IS TRUE_SYM + | NOT_SYM expr %prec NOT_SYM + { $$= negate_expression(YYTHD, $2); } + | bool_pri IS TRUE_SYM %prec IS { $$= new (YYTHD->mem_root) Item_func_istrue($1); } - | bool_pri IS not TRUE_SYM + | bool_pri IS not TRUE_SYM %prec IS { $$= new (YYTHD->mem_root) Item_func_isnottrue($1); } - | bool_pri IS FALSE_SYM + | bool_pri IS FALSE_SYM %prec IS { $$= new (YYTHD->mem_root) Item_func_isfalse($1); } - | bool_pri IS not FALSE_SYM + | bool_pri IS not FALSE_SYM %prec IS { $$= new (YYTHD->mem_root) Item_func_isnotfalse($1); } - | bool_pri IS UNKNOWN_SYM { $$= new Item_func_isnull($1); } - | bool_pri IS not UNKNOWN_SYM { $$= new Item_func_isnotnull($1); } + | bool_pri IS UNKNOWN_SYM %prec IS + { $$= new Item_func_isnull($1); } + | bool_pri IS not UNKNOWN_SYM %prec IS + { $$= new Item_func_isnotnull($1); } | bool_pri ; bool_pri: - bool_pri IS NULL_SYM { $$= new Item_func_isnull($1); } - | bool_pri IS not NULL_SYM { $$= new Item_func_isnotnull($1); } - | bool_pri EQUAL_SYM predicate { $$= new Item_func_equal($1,$3); } + bool_pri IS NULL_SYM %prec IS + { $$= new Item_func_isnull($1); } + | bool_pri IS not NULL_SYM %prec IS + { $$= new Item_func_isnotnull($1); } + | bool_pri EQUAL_SYM predicate %prec EQUAL_SYM + { $$= new Item_func_equal($1,$3); } | bool_pri comp_op predicate %prec EQ { $$= (*$2)(0)->create($1,$3); } | bool_pri comp_op all_or_any '(' subselect ')' %prec EQ { $$= all_any_subquery_creator($1, $2, $3, $5); } - | predicate + | predicate ; ; predicate: @@ -6743,44 +6741,34 @@ predicate: ; bit_expr: - bit_expr '|' bit_term { $$= new Item_func_bit_or($1,$3); } - | bit_term - ; - -bit_term: - bit_term '&' bit_factor { $$= new Item_func_bit_and($1,$3); } - | bit_factor - ; - -bit_factor: - bit_factor SHIFT_LEFT value_expr + bit_expr '|' bit_expr %prec '|' + { $$= new Item_func_bit_or($1,$3); } + | bit_expr '&' bit_expr %prec '&' + { $$= new Item_func_bit_and($1,$3); } + | bit_expr SHIFT_LEFT bit_expr %prec SHIFT_LEFT { $$= new Item_func_shift_left($1,$3); } - | bit_factor SHIFT_RIGHT value_expr + | bit_expr SHIFT_RIGHT bit_expr %prec SHIFT_RIGHT { $$= new Item_func_shift_right($1,$3); } - | value_expr - ; - -value_expr: - value_expr '+' term { $$= new Item_func_plus($1,$3); } - | value_expr '-' term { $$= new Item_func_minus($1,$3); } - | value_expr '+' interval_expr interval + | bit_expr '+' bit_expr %prec '+' + { $$= new Item_func_plus($1,$3); } + | bit_expr '-' bit_expr %prec '-' + { $$= new Item_func_minus($1,$3); } + | bit_expr '+' interval_expr interval %prec '+' { $$= new Item_date_add_interval($1,$3,$4,0); } - | value_expr '-' interval_expr interval + | bit_expr '-' interval_expr interval %prec '-' { $$= new Item_date_add_interval($1,$3,$4,1); } - | term - ; - -term: - term '*' factor { $$= new Item_func_mul($1,$3); } - | term '/' factor { $$= new Item_func_div($1,$3); } - | term '%' factor { $$= new Item_func_mod($1,$3); } - | term DIV_SYM factor { $$= new Item_func_int_div($1,$3); } - | term MOD_SYM factor { $$= new Item_func_mod($1,$3); } - | factor - ; - -factor: - factor '^' simple_expr { $$= new Item_func_bit_xor($1,$3); } + | bit_expr '*' bit_expr %prec '*' + { $$= new Item_func_mul($1,$3); } + | bit_expr '/' bit_expr %prec '/' + { $$= new Item_func_div($1,$3); } + | bit_expr '%' bit_expr %prec '%' + { $$= new Item_func_mod($1,$3); } + | bit_expr DIV_SYM bit_expr %prec DIV_SYM + { $$= new Item_func_int_div($1,$3); } + | bit_expr MOD_SYM bit_expr %prec MOD_SYM + { $$= new Item_func_mod($1,$3); } + | bit_expr '^' bit_expr + { $$= new Item_func_bit_xor($1,$3); } | simple_expr ; From 290d724943d8bb1fded3afeba1de37fd44461a9f Mon Sep 17 00:00:00 2001 From: "anozdrin/alik@ibm." <> Date: Wed, 29 Aug 2007 14:57:59 +0400 Subject: [PATCH 14/35] Test case for Bug#13675: DATETIME/DATE type in store proc param seems to be converted as varbinary. The bug has been already fixed. This CS just adds a test case for it. --- mysql-test/r/sp.result | 52 ++++++++++++++++++++++++++++++++++++++++ mysql-test/t/sp.test | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/mysql-test/r/sp.result b/mysql-test/r/sp.result index 4a278cd4aec..7e80d9c3ad9 100644 --- a/mysql-test/r/sp.result +++ b/mysql-test/r/sp.result @@ -6314,4 +6314,56 @@ CALL p1(); NULL SET NAMES default; DROP PROCEDURE p1; + +# Bug#13675. + +DROP PROCEDURE IF EXISTS p1; +DROP PROCEDURE IF EXISTS p2; +DROP TABLE IF EXISTS t1; + +CREATE PROCEDURE p1(v DATETIME) CREATE TABLE t1 SELECT v; +CREATE PROCEDURE p2(v INT) CREATE TABLE t1 SELECT v; + +CALL p1(NOW()); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `v` datetime default NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 + +DROP TABLE t1; + +CALL p1('text'); +Warnings: +Warning 1264 Out of range value adjusted for column 'v' at row 1 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `v` datetime default NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 + +DROP TABLE t1; + +CALL p2(10); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `v` bigint(11) default NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 + +DROP TABLE t1; + +CALL p2('text'); +Warnings: +Warning 1366 Incorrect integer value: 'text' for column 'v' at row 1 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `v` bigint(11) default NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 + +DROP TABLE t1; + +DROP PROCEDURE p1; +DROP PROCEDURE p2; End of 5.0 tests diff --git a/mysql-test/t/sp.test b/mysql-test/t/sp.test index 46a1b1dc740..189273aeb06 100644 --- a/mysql-test/t/sp.test +++ b/mysql-test/t/sp.test @@ -7299,4 +7299,58 @@ CALL p1(); SET NAMES default; DROP PROCEDURE p1; +# +# Bug#13675: DATETIME/DATE type in store proc param seems to be converted as +# varbinary +# + +--echo +--echo # Bug#13675. +--echo + +--disable_warnings +DROP PROCEDURE IF EXISTS p1; +DROP PROCEDURE IF EXISTS p2; + +DROP TABLE IF EXISTS t1; +--enable_warnings + +--echo + +CREATE PROCEDURE p1(v DATETIME) CREATE TABLE t1 SELECT v; + +CREATE PROCEDURE p2(v INT) CREATE TABLE t1 SELECT v; + +--echo +CALL p1(NOW()); +SHOW CREATE TABLE t1; + +--echo +DROP TABLE t1; + +--echo +CALL p1('text'); +SHOW CREATE TABLE t1; + +--echo +DROP TABLE t1; + +--echo +CALL p2(10); +SHOW CREATE TABLE t1; + +--echo +DROP TABLE t1; + +--echo +CALL p2('text'); +SHOW CREATE TABLE t1; + +--echo +DROP TABLE t1; + +--echo +DROP PROCEDURE p1; +DROP PROCEDURE p2; + --echo End of 5.0 tests From cb7486b4bf444f74d98e9f68ec1ae910ae24f061 Mon Sep 17 00:00:00 2001 From: "davi@moksha.local" <> Date: Wed, 29 Aug 2007 16:59:38 -0300 Subject: [PATCH 15/35] Bug#21975 Grant and revoke statements are non-transactional Bug#21422 GRANT/REVOKE possible inside stored function, probably in a trigger Bug#17244 GRANT gives strange error message when used in a stored function GRANT/REVOKE statements are non-transactional (no explicit transaction boundaries) in nature and hence are forbidden inside stored functions and triggers, but they weren't being effectively forbidden. Furthermore, the absence of implict commits makes changes made by GRANT/REVOKE statements to not be rolled back. The implemented fix is to issue a implicit commit with every GRANT/REVOKE statement, effectively prohibiting these statements in stored functions and triggers. The implicit commit also fixes the replication bug, and looks like being in concert with the behavior of DDL and administrative statements. Since this is a incompatible change, the following sentence should be added to the Manual in the very end of the 3rd paragraph, subclause 13.4.3 "Statements That Cause an Implicit Commit": "Beginning with MySQL 5.0.??, the GRANT and REVOKE statements cause an implicit commit." Patch contributed by Vladimir Shebordaev --- mysql-test/r/sp-error.result | 12 +++++ .../suite/rpl/r/rpl_binlog_grant.result | 50 +++++++++++++++++++ mysql-test/suite/rpl/t/rpl_binlog_grant.test | 41 +++++++++++++++ mysql-test/t/sp-error.test | 15 ++++++ sql/sp_head.cc | 3 ++ sql/sql_parse.cc | 5 ++ 6 files changed, 126 insertions(+) create mode 100644 mysql-test/suite/rpl/r/rpl_binlog_grant.result create mode 100644 mysql-test/suite/rpl/t/rpl_binlog_grant.test diff --git a/mysql-test/r/sp-error.result b/mysql-test/r/sp-error.result index 675a59f1fb7..2e0d437aeb6 100644 --- a/mysql-test/r/sp-error.result +++ b/mysql-test/r/sp-error.result @@ -969,6 +969,18 @@ CREATE TRIGGER tr1 BEFORE INSERT ON t1 FOR EACH ROW BEGIN create user 'mysqltest ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger. CREATE FUNCTION bug_13627_f() returns int BEGIN create user 'mysqltest_1'; return 1; END | ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger. +CREATE TRIGGER bug21975 BEFORE INSERT ON t1 FOR EACH ROW BEGIN grant select on t1 to 'mysqltest_1'; END | +ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger. +CREATE FUNCTION bug21975() returns int BEGIN grant select on t1 to 'mysqltest_1'; return 1; END | +ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger. +CREATE TRIGGER bug21975 BEFORE INSERT ON t1 FOR EACH ROW BEGIN revoke select on t1 from 'mysqltest_1'; END | +ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger. +CREATE FUNCTION bug21975() returns int BEGIN revoke select on t1 from 'mysqltest_1'; return 1; END | +ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger. +CREATE TRIGGER bug21975 BEFORE INSERT ON t1 FOR EACH ROW BEGIN revoke all privileges on *.* from 'mysqltest_1'; END | +ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger. +CREATE FUNCTION bug21975() returns int BEGIN revoke all privileges on *.* from 'mysqltest_1'; return 1; END | +ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger. CREATE TRIGGER tr1 BEFORE INSERT ON t1 FOR EACH ROW BEGIN drop user 'mysqltest_1'; END | ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger. CREATE FUNCTION bug_13627_f() returns int BEGIN drop user 'mysqltest_1'; return 1; END | diff --git a/mysql-test/suite/rpl/r/rpl_binlog_grant.result b/mysql-test/suite/rpl/r/rpl_binlog_grant.result new file mode 100644 index 00000000000..1692bcee21f --- /dev/null +++ b/mysql-test/suite/rpl/r/rpl_binlog_grant.result @@ -0,0 +1,50 @@ +drop database if exists d1; +create database d1; +use d1; +create table t (s1 int) engine=innodb; +set @@autocommit=0; +start transaction; +insert into t values (1); +grant select on t to x@y; +rollback; +show grants for x@y; +Grants for x@y +GRANT USAGE ON *.* TO 'x'@'y' +GRANT SELECT ON `d1`.`t` TO 'x'@'y' +show binlog events; +Log_name Pos Event_type Server_id End_log_pos Info +master-bin.000001 4 Format_desc 1 106 Server ver: VERSION, Binlog ver: 4 +master-bin.000001 106 Query 1 193 drop database if exists d1 +master-bin.000001 193 Query 1 272 create database d1 +master-bin.000001 272 Query 1 370 use `d1`; create table t (s1 int) engine=innodb +master-bin.000001 370 Query 1 436 use `d1`; BEGIN +master-bin.000001 436 Query 1 521 use `d1`; insert into t values (1) +master-bin.000001 521 Xid 1 548 COMMIT /* xid=12 */ +master-bin.000001 548 Query 1 633 use `d1`; grant select on t to x@y +start transaction; +insert into t values (2); +revoke select on t from x@y; +commit; +select * from t; +s1 +1 +2 +show grants for x@y; +Grants for x@y +GRANT USAGE ON *.* TO 'x'@'y' +show binlog events; +Log_name Pos Event_type Server_id End_log_pos Info +master-bin.000001 4 Format_desc 1 106 Server ver: VERSION, Binlog ver: 4 +master-bin.000001 106 Query 1 193 drop database if exists d1 +master-bin.000001 193 Query 1 272 create database d1 +master-bin.000001 272 Query 1 370 use `d1`; create table t (s1 int) engine=innodb +master-bin.000001 370 Query 1 436 use `d1`; BEGIN +master-bin.000001 436 Query 1 521 use `d1`; insert into t values (1) +master-bin.000001 521 Xid 1 548 COMMIT /* xid=12 */ +master-bin.000001 548 Query 1 633 use `d1`; grant select on t to x@y +master-bin.000001 633 Query 1 699 use `d1`; BEGIN +master-bin.000001 699 Query 1 784 use `d1`; insert into t values (2) +master-bin.000001 784 Xid 1 811 COMMIT /* xid=18 */ +master-bin.000001 811 Query 1 899 use `d1`; revoke select on t from x@y +drop user x@y; +drop database d1; diff --git a/mysql-test/suite/rpl/t/rpl_binlog_grant.test b/mysql-test/suite/rpl/t/rpl_binlog_grant.test new file mode 100644 index 00000000000..b6868f7ebf8 --- /dev/null +++ b/mysql-test/suite/rpl/t/rpl_binlog_grant.test @@ -0,0 +1,41 @@ +-- source include/have_innodb.inc +-- source include/not_embedded.inc +-- source include/have_binlog_format_mixed_or_statement.inc + +let $VERSION=`select version()`; + +# Bug #21975: grant/revoke statements in transaction +# used to disappear from binlog upon rallback. +# Now GRANT/REVOKE do implicitly commit +# transaction + +--disable_warnings +drop database if exists d1; +--enable_warnings +create database d1; +use d1; +create table t (s1 int) engine=innodb; +set @@autocommit=0; +start transaction; +insert into t values (1); +grant select on t to x@y; +# +# There is no active transaction here +# +rollback; +show grants for x@y; +--replace_result $VERSION VERSION +show binlog events; +start transaction; +insert into t values (2); +revoke select on t from x@y; +# +# There is no active transaction here +# +commit; +select * from t; +show grants for x@y; +--replace_result $VERSION VERSION +show binlog events; +drop user x@y; +drop database d1; diff --git a/mysql-test/t/sp-error.test b/mysql-test/t/sp-error.test index ef9bed8b789..012f2b33225 100644 --- a/mysql-test/t/sp-error.test +++ b/mysql-test/t/sp-error.test @@ -1386,6 +1386,21 @@ CREATE TRIGGER tr1 BEFORE INSERT ON t1 FOR EACH ROW BEGIN create user 'mysqltest -- error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG CREATE FUNCTION bug_13627_f() returns int BEGIN create user 'mysqltest_1'; return 1; END | +-- error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG +CREATE TRIGGER bug21975 BEFORE INSERT ON t1 FOR EACH ROW BEGIN grant select on t1 to 'mysqltest_1'; END | +-- error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG +CREATE FUNCTION bug21975() returns int BEGIN grant select on t1 to 'mysqltest_1'; return 1; END | + +-- error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG +CREATE TRIGGER bug21975 BEFORE INSERT ON t1 FOR EACH ROW BEGIN revoke select on t1 from 'mysqltest_1'; END | +-- error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG +CREATE FUNCTION bug21975() returns int BEGIN revoke select on t1 from 'mysqltest_1'; return 1; END | + +-- error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG +CREATE TRIGGER bug21975 BEFORE INSERT ON t1 FOR EACH ROW BEGIN revoke all privileges on *.* from 'mysqltest_1'; END | +-- error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG +CREATE FUNCTION bug21975() returns int BEGIN revoke all privileges on *.* from 'mysqltest_1'; return 1; END | + -- error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG CREATE TRIGGER tr1 BEFORE INSERT ON t1 FOR EACH ROW BEGIN drop user 'mysqltest_1'; END | -- error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 7b2073b8de3..de8c4d89466 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -247,11 +247,14 @@ sp_get_flags_for_command(LEX *lex) case SQLCOM_CREATE_TRIGGER: case SQLCOM_CREATE_USER: case SQLCOM_ALTER_TABLE: + case SQLCOM_GRANT: + case SQLCOM_REVOKE: case SQLCOM_BEGIN: case SQLCOM_RENAME_TABLE: case SQLCOM_RENAME_USER: case SQLCOM_DROP_INDEX: case SQLCOM_DROP_DB: + case SQLCOM_REVOKE_ALL: case SQLCOM_DROP_USER: case SQLCOM_DROP_VIEW: case SQLCOM_DROP_TRIGGER: diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 2e4ce65f1c4..bbd6cb16d11 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -3381,6 +3381,8 @@ end_with_restore_list: } case SQLCOM_REVOKE_ALL: { + if (end_active_trans(thd)) + goto error; if (check_access(thd, UPDATE_ACL, "mysql", 0, 1, 1, 0) && check_global_access(thd,CREATE_USER_ACL)) break; @@ -3392,6 +3394,9 @@ end_with_restore_list: case SQLCOM_REVOKE: case SQLCOM_GRANT: { + if (end_active_trans(thd)) + goto error; + if (check_access(thd, lex->grant | lex->grant_tot_col | GRANT_ACL, first_table ? first_table->db : select_lex->db, first_table ? &first_table->grant.privilege : 0, From 6f72d9909925f634cd4f93413f696c963d8edd77 Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Wed, 29 Aug 2007 14:50:32 -0600 Subject: [PATCH 16/35] Bug#28779 (mysql_query() allows execution of statements with unbalanced comments) Before this fix, the server would accept queries that contained comments, even when the comments were not properly closed with a '*' '/' marker. For example, select 1 /* + 2 would be accepted as select 1 /* + 2 */ and executed as select 1 With this fix, the server now rejects queries with unclosed comments as syntax errors. Both regular comments ('/' '*') and special comments ('/' '*' '!') must be closed with '*' '/' to be parsed correctly. --- mysql-test/r/comments.result | 15 +++++++++++++++ mysql-test/t/comments.test | 33 ++++++++++++++++++++++++++++++++ sql/sql_lex.cc | 37 +++++++++++++++++++++++++++++------- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/mysql-test/r/comments.result b/mysql-test/r/comments.result index a9106ce0538..1040c3fc8e9 100644 --- a/mysql-test/r/comments.result +++ b/mysql-test/r/comments.result @@ -26,3 +26,18 @@ select 1 # The rest of the row will be ignored 1 1 /* line with only comment */; +drop table if exists table_28779; +create table table_28779 (a int); +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*' AND b = 'bar';"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/*' AND b = 'bar'' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*' AND b = 'bar';*"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/*' AND b = 'bar';*' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*! AND 2=2;"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*! AND 2=2;*"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';*' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!98765' AND b = 'bar';"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/*!98765' AND b = 'bar'' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!98765' AND b = 'bar';*"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/*!98765' AND b = 'bar';*' at line 1 +drop table table_28779; diff --git a/mysql-test/t/comments.test b/mysql-test/t/comments.test index 52273ec9523..0c6853cf298 100644 --- a/mysql-test/t/comments.test +++ b/mysql-test/t/comments.test @@ -19,3 +19,36 @@ select 1 # The rest of the row will be ignored /* line with only comment */; # End of 4.1 tests + + +# +# Bug#28779 (mysql_query() allows execution of statements with unbalanced +# comments) +# + +--disable_warnings +drop table if exists table_28779; +--enable_warnings + +create table table_28779 (a int); + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*' AND b = 'bar';"; + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*' AND b = 'bar';*"; + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*! AND 2=2;"; + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*! AND 2=2;*"; + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!98765' AND b = 'bar';"; + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!98765' AND b = 'bar';*"; + +drop table table_28779; + diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 7911da69862..1770ff358a2 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -526,6 +526,7 @@ static inline uint int_token(const char *str,uint length) int MYSQLlex(void *arg, void *yythd) { reg1 uchar c; + bool comment_closed; int tokval, result_state; uint length; enum my_lex_states state; @@ -961,15 +962,34 @@ int MYSQLlex(void *arg, void *yythd) break; } } - while (lip->ptr != lip->end_of_query && - ((c=yyGet()) != '*' || yyPeek() != '/')) + /* + Discard: + - regular '/' '*' comments, + - special comments '/' '*' '!' for a future version, + by scanning until we find a closing '*' '/' marker. + Note: There is no such thing as nesting comments, + the first '*' '/' sequence seen will mark the end. + */ + comment_closed= FALSE; + while (lip->ptr != lip->end_of_query) { - if (c == '\n') - lip->yylineno++; + c= yyGet(); + if (c == '*') + { + if (yyPeek() == '/') + { + yySkip(); + comment_closed= TRUE; + state = MY_LEX_START; + break; + } + } + else if (c == '\n') + lip->yylineno++; } - if (lip->ptr != lip->end_of_query) - yySkip(); // remove last '/' - state = MY_LEX_START; // Try again + /* Unbalanced comments with a missing '*' '/' are a syntax error */ + if (! comment_closed) + return (ABORT_SYM); break; case MY_LEX_END_LONG_COMMENT: if (lex->in_comment && yyPeek() == '/') @@ -1009,6 +1029,9 @@ int MYSQLlex(void *arg, void *yythd) if (lip->ptr >= lip->end_of_query) { lip->next_state=MY_LEX_END; // Mark for next loop + /* Unbalanced comments with a missing '*' '/' are a syntax error */ + if (lex->in_comment) + return (ABORT_SYM); return(END_OF_INPUT); } state=MY_LEX_CHAR; From a2a5104de74baf22d1faa5a669d9db98b0ff24f1 Mon Sep 17 00:00:00 2001 From: "davi@moksha.local" <> Date: Wed, 29 Aug 2007 19:00:49 -0300 Subject: [PATCH 17/35] Post-merge fixes for Bug#30632 --- mysql-test/r/handler_innodb.result | 13 +++++++++++++ mysql-test/r/handler_myisam.result | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/handler_innodb.result b/mysql-test/r/handler_innodb.result index 81d1e26f8d0..98b8922bc5f 100644 --- a/mysql-test/r/handler_innodb.result +++ b/mysql-test/r/handler_innodb.result @@ -522,3 +522,16 @@ handler t1 open; ERROR HY000: Table storage engine for 't1' doesn't have this option --> client 1 drop table t1; +drop table if exists t1; +create table t1 (a int); +handler t1 open as t1_alias; +handler t1_alias read a next; +ERROR 42000: Key 'a' doesn't exist in table 't1_alias' +handler t1_alias READ a next where inexistent > 0; +ERROR 42S22: Unknown column 'inexistent' in 'field list' +handler t1_alias read a next; +ERROR 42000: Key 'a' doesn't exist in table 't1_alias' +handler t1_alias READ a next where inexistent > 0; +ERROR 42S22: Unknown column 'inexistent' in 'field list' +handler t1_alias close; +drop table t1; diff --git a/mysql-test/r/handler_myisam.result b/mysql-test/r/handler_myisam.result index 5a9b2824232..464b775b795 100644 --- a/mysql-test/r/handler_myisam.result +++ b/mysql-test/r/handler_myisam.result @@ -526,11 +526,11 @@ drop table if exists t1; create table t1 (a int); handler t1 open as t1_alias; handler t1_alias read a next; -ERROR HY000: Key 'a' doesn't exist in table 't1_alias' +ERROR 42000: Key 'a' doesn't exist in table 't1_alias' handler t1_alias READ a next where inexistent > 0; ERROR 42S22: Unknown column 'inexistent' in 'field list' handler t1_alias read a next; -ERROR HY000: Key 'a' doesn't exist in table 't1_alias' +ERROR 42000: Key 'a' doesn't exist in table 't1_alias' handler t1_alias READ a next where inexistent > 0; ERROR 42S22: Unknown column 'inexistent' in 'field list' handler t1_alias close; From 496eb569d947d45d59279110af4f02c9246478e7 Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Wed, 29 Aug 2007 17:27:38 -0600 Subject: [PATCH 18/35] Manual merge --- mysql-test/r/sp.result | 52 ++++++++++++++++++++++++++++++++++++++++ mysql-test/t/sp.test | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/mysql-test/r/sp.result b/mysql-test/r/sp.result index 9e00424e879..2abff35b232 100644 --- a/mysql-test/r/sp.result +++ b/mysql-test/r/sp.result @@ -6569,4 +6569,56 @@ rl_acquirer old drop procedure p1; drop table t1; set session low_priority_updates=default; + +# Bug#13675. + +DROP PROCEDURE IF EXISTS p1; +DROP PROCEDURE IF EXISTS p2; +DROP TABLE IF EXISTS t1; + +CREATE PROCEDURE p1(v DATETIME) CREATE TABLE t1 SELECT v; +CREATE PROCEDURE p2(v INT) CREATE TABLE t1 SELECT v; + +CALL p1(NOW()); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `v` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 + +DROP TABLE t1; + +CALL p1('text'); +Warnings: +Warning 1264 Out of range value for column 'v' at row 1 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `v` datetime DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 + +DROP TABLE t1; + +CALL p2(10); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `v` bigint(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 + +DROP TABLE t1; + +CALL p2('text'); +Warnings: +Warning 1366 Incorrect integer value: 'text' for column 'v' at row 1 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `v` bigint(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 + +DROP TABLE t1; + +DROP PROCEDURE p1; +DROP PROCEDURE p2; End of 5.0 tests diff --git a/mysql-test/t/sp.test b/mysql-test/t/sp.test index 37e3880cb82..1c8f1ca51c2 100644 --- a/mysql-test/t/sp.test +++ b/mysql-test/t/sp.test @@ -7559,4 +7559,58 @@ drop procedure p1; drop table t1; set session low_priority_updates=default; +# +# Bug#13675: DATETIME/DATE type in store proc param seems to be converted as +# varbinary +# + +--echo +--echo # Bug#13675. +--echo + +--disable_warnings +DROP PROCEDURE IF EXISTS p1; +DROP PROCEDURE IF EXISTS p2; + +DROP TABLE IF EXISTS t1; +--enable_warnings + +--echo + +CREATE PROCEDURE p1(v DATETIME) CREATE TABLE t1 SELECT v; + +CREATE PROCEDURE p2(v INT) CREATE TABLE t1 SELECT v; + +--echo +CALL p1(NOW()); +SHOW CREATE TABLE t1; + +--echo +DROP TABLE t1; + +--echo +CALL p1('text'); +SHOW CREATE TABLE t1; + +--echo +DROP TABLE t1; + +--echo +CALL p2(10); +SHOW CREATE TABLE t1; + +--echo +DROP TABLE t1; + +--echo +CALL p2('text'); +SHOW CREATE TABLE t1; + +--echo +DROP TABLE t1; + +--echo +DROP PROCEDURE p1; +DROP PROCEDURE p2; + --echo End of 5.0 tests From 88091e82506aac0fdb241ed155979d9ebcea549c Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Wed, 29 Aug 2007 18:56:00 -0600 Subject: [PATCH 19/35] Bug#30712 (open_performance_schema_table() cause an open table leak on failures) Fixed open_performance_schema_table() and close_performance_schema_table() implementation and callers, to always execute balanced calls to: thd->reset_n_backup_open_tables_state(backup); thd->restore_backup_open_tables_state(backup); --- sql/log.cc | 10 ++++++++-- sql/sql_base.cc | 38 +++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/sql/log.cc b/sql/log.cc index 3686459fedf..a88f925c2e7 100644 --- a/sql/log.cc +++ b/sql/log.cc @@ -656,8 +656,14 @@ int Log_to_csv_event_handler:: table= open_performance_schema_table(thd, & table_list, & open_tables_backup); - result= (table ? 0 : 1); - close_performance_schema_table(thd, & open_tables_backup); + if (table) + { + result= 0; + close_performance_schema_table(thd, & open_tables_backup); + } + else + result= 1; + DBUG_RETURN(result); } diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 83bab51f870..7ea25447ba0 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -7746,6 +7746,9 @@ open_performance_schema_table(THD *thd, TABLE_LIST *one_table, */ table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET; } + else + thd->restore_backup_open_tables_state(backup); + thd->utime_after_lock= save_utime_after_lock; DBUG_RETURN(table); } @@ -7761,24 +7764,25 @@ void close_performance_schema_table(THD *thd, Open_tables_state *backup) { bool found_old_table; - if (thd->lock) - { - /* - Note: - We do not create explicitly a separate transaction for the - performance table I/O, but borrow the current transaction. - lock + unlock will autocommit the change done in the - performance schema table: this is the expected result. - The current transaction should not be affected by this code. - TODO: Note that if a transactional engine is used for log tables, - this code will need to be revised, as a separate transaction - might be needed. - */ - mysql_unlock_tables(thd, thd->lock); - thd->lock= 0; - } + /* + If open_performance_schema_table() fails, + this function should not be called. + */ + DBUG_ASSERT(thd->lock != NULL); - safe_mutex_assert_not_owner(&LOCK_open); + /* + Note: + We do not create explicitly a separate transaction for the + performance table I/O, but borrow the current transaction. + lock + unlock will autocommit the change done in the + performance schema table: this is the expected result. + The current transaction should not be affected by this code. + TODO: Note that if a transactional engine is used for log tables, + this code will need to be revised, as a separate transaction + might be needed. + */ + mysql_unlock_tables(thd, thd->lock); + thd->lock= 0; pthread_mutex_lock(&LOCK_open); From 2bb849644b8fe74584096924064fcde8ec0a512f Mon Sep 17 00:00:00 2001 From: "kaa@polly.(none)" <> Date: Thu, 30 Aug 2007 12:53:24 +0400 Subject: [PATCH 20/35] Bug #30164: Using client side macro inside server side comments generates broken queries Problem: In cases when a client-side macro appears inside a server-side comment, the add_line() function in mysql.cc discarded all characters until the next delimiter to remove macro arguments from the query string. This resulted in broken queries being sent to the server when the next delimiter character appeared past the comment's boundaries, because the comment closing sequence ('*/') was discarded. Fix: If a client-side macro appears inside a server-side comment, discard all characters in the comment after the macro (that is, until the end of the comment rather than the next delimiter). This is a minimal fix to allow only simple cases used by the mysqlbinlog utility. Limitations that are worth documenting: - Nested server-side and/or client-side comments are not supported by mysql.cc - Using client-side macros in multi-line server-side comments is not supported - All characters after a client-side macro in a server-side comment will be omitted from the query string (and thus, will not be sent to server). --- client/mysql.cc | 54 +++++++++++++++++++++++++++------------ mysql-test/r/mysql.result | 2 ++ mysql-test/t/mysql.test | 5 ++++ 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/client/mysql.cc b/client/mysql.cc index 277b56328a6..8e1b6c2a9b4 100644 --- a/client/mysql.cc +++ b/client/mysql.cc @@ -1245,6 +1245,7 @@ static bool add_line(String &buffer,char *line,char *in_string, char buff[80], *pos, *out; COMMANDS *com; bool need_space= 0; + bool ss_comment= 0; DBUG_ENTER("add_line"); if (!line[0] && buffer.is_empty()) @@ -1293,22 +1294,36 @@ static bool add_line(String &buffer,char *line,char *in_string, } if ((com=find_command(NullS,(char) inchar))) { - const String tmp(line,(uint) (out-line), charset_info); - buffer.append(tmp); - if ((*com->func)(&buffer,pos-1) > 0) - DBUG_RETURN(1); // Quit - if (com->takes_params) - { - for (pos++ ; - *pos && (*pos != *delimiter || - !is_prefix(pos + 1, delimiter + 1)) ; pos++) - ; // Remove parameters - if (!*pos) - pos--; - else - pos+= delimiter_length - 1; // Point at last delim char - } - out=line; + const String tmp(line,(uint) (out-line), charset_info); + buffer.append(tmp); + if ((*com->func)(&buffer,pos-1) > 0) + DBUG_RETURN(1); // Quit + if (com->takes_params) + { + if (ss_comment) + { + /* + If a client-side macro appears inside a server-side comment, + discard all characters in the comment after the macro (that is, + until the end of the comment rather than the next delimiter) + */ + for (pos++; *pos && (*pos != '*' || *(pos + 1) != '/'); pos++) + ; + pos--; + } + else + { + for (pos++ ; + *pos && (*pos != *delimiter || + !is_prefix(pos + 1, delimiter + 1)) ; pos++) + ; // Remove parameters + if (!*pos) + pos--; + else + pos+= delimiter_length - 1; // Point at last delim char + } + } + out=line; } else { @@ -1368,7 +1383,7 @@ static bool add_line(String &buffer,char *line,char *in_string, out=line; } } - else if (*ml_comment && inchar == '*' && *(pos + 1) == '/') + else if (*ml_comment && !ss_comment && inchar == '*' && *(pos + 1) == '/') { pos++; *ml_comment= 0; @@ -1376,6 +1391,11 @@ static bool add_line(String &buffer,char *line,char *in_string, } else { // Add found char to buffer + if (!*in_string && inchar == '/' && *(pos + 1) == '*' && + *(pos + 2) == '!') + ss_comment= 1; + else if (!*in_string && ss_comment && inchar == '*' && *(pos + 1) == '/') + ss_comment= 0; if (inchar == *in_string) *in_string= 0; else if (!*ml_comment && !*in_string && diff --git a/mysql-test/r/mysql.result b/mysql-test/r/mysql.result index 843f2c7285a..74b5c42e59b 100644 --- a/mysql-test/r/mysql.result +++ b/mysql-test/r/mysql.result @@ -176,4 +176,6 @@ ERROR at line 1: DELIMITER cannot contain a backslash character ERROR at line 1: DELIMITER cannot contain a backslash character 1 1 +1 +1 End of 5.0 tests diff --git a/mysql-test/t/mysql.test b/mysql-test/t/mysql.test index 37bbca77d9f..fad1d1fe746 100644 --- a/mysql-test/t/mysql.test +++ b/mysql-test/t/mysql.test @@ -273,4 +273,9 @@ EOF --exec $MYSQL --pager="540bytelengthstringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -e "select 1" > /dev/null 2>&1 --exec $MYSQL --character-sets-dir="540bytelengthstringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -e "select 1" 2>&1 +# +# bug #30164: Using client side macro inside server side comments generates broken queries +# +--exec $MYSQL test -e '/*! \C latin1 */ select 1;' + --echo End of 5.0 tests From 33d10a0667853bc0fd546549ef3ba2646ab93741 Mon Sep 17 00:00:00 2001 From: "kaa@polly.(none)" <> Date: Thu, 30 Aug 2007 17:21:43 +0400 Subject: [PATCH 21/35] Use double quotes instead of single ones which make the test fail on Windows. This is for bug #30164. --- mysql-test/t/mysql.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysql-test/t/mysql.test b/mysql-test/t/mysql.test index fad1d1fe746..9f8841ec1f4 100644 --- a/mysql-test/t/mysql.test +++ b/mysql-test/t/mysql.test @@ -276,6 +276,6 @@ EOF # # bug #30164: Using client side macro inside server side comments generates broken queries # ---exec $MYSQL test -e '/*! \C latin1 */ select 1;' +--exec $MYSQL test -e "/*! \C latin1 */ select 1;" --echo End of 5.0 tests From f141ba5e2688df860fe18b298d5366b7ff11f91b Mon Sep 17 00:00:00 2001 From: "anozdrin/alik@ibm.opbmk" <> Date: Thu, 30 Aug 2007 20:06:19 +0400 Subject: [PATCH 22/35] Cleanup-patch for BUG#25843: changing default database between PREPARE and EXECUTE of statement breaks binlog. --- sql/sp.cc | 55 ++++++++++++++++++-------------- sql/sql_class.h | 39 ++++++++++++++++++++--- sql/sql_db.cc | 85 +++++++++++++++++++++++++++++++++++++------------ 3 files changed, 129 insertions(+), 50 deletions(-) diff --git a/sql/sp.cc b/sql/sp.cc index 7b69842669f..8655e520041 100644 --- a/sql/sp.cc +++ b/sql/sp.cc @@ -574,7 +574,7 @@ db_load_routine(THD *thd, int type, sp_name *name, sp_head **sphp, we'll update it later in switch_query_ctx(). */ - if ((ret= sp_use_new_db(thd, name->m_db, &old_db, 1, &dbchanged))) + if ((ret= sp_use_new_db(thd, name->m_db, &old_db, TRUE, &dbchanged))) goto end; thd->spcont= NULL; @@ -2027,34 +2027,41 @@ create_string(THD *thd, String *buf, -/* +/** Change the current database if needed. - SYNOPSIS - sp_use_new_db() - thd thread handle - new_db new database name (a string and its length) - old_db [IN] str points to a buffer where to store the old - database, length contains the size of the buffer - [OUT] if old db was not NULL, its name is copied - to the buffer pointed at by str and length is updated - accordingly. Otherwise str[0] is set to '\0' and length - is set to 0. The out parameter should be used only if - the database name has been changed (see dbchangedp). - dbchangedp [OUT] is set to TRUE if the current database is changed, - FALSE otherwise. A database is not changed if the old - name is the same as the new one, both names are empty, - or an error has occurred. + @param[in] thd thread handle + @param[in] new_db new database name + @param[in, out] old_db IN: str points to a buffer where to store + the old database, length contains the + size of the buffer + OUT: if old db was not NULL, its name is + copied to the buffer pointed at by str + and length is updated accordingly. + Otherwise str[0] is set to '\0' and + length is set to 0. The out parameter + should be used only if the database name + has been changed (see dbchangedp). + @param[in] force_switch Flag to mysql_change_db(). For more information, + see mysql_change_db() comment. + @param[out] dbchangedp is set to TRUE if the current database is + changed, FALSE otherwise. The current + database is not changed if the old name + is equal to the new one, both names are + empty, or an error has occurred. - RETURN VALUE - 0 success - 1 access denied or out of memory (the error message is - set in THD) + @return Operation status. + @retval 0 on success + @retval 1 access denied or out of memory + (the error message is set in THD) */ int -sp_use_new_db(THD *thd, LEX_STRING new_db, LEX_STRING *old_db, - bool no_access_check, bool *dbchangedp) +sp_use_new_db(THD *thd, + LEX_STRING new_db, + LEX_STRING *old_db, + bool force_switch, + bool *dbchangedp) { int ret; DBUG_ENTER("sp_use_new_db"); @@ -2085,7 +2092,7 @@ sp_use_new_db(THD *thd, LEX_STRING new_db, LEX_STRING *old_db, DBUG_RETURN(0); } - ret= mysql_change_db(thd, &new_db, no_access_check); + ret= mysql_change_db(thd, &new_db, force_switch); *dbchangedp= ret == 0; DBUG_RETURN(ret); diff --git a/sql/sql_class.h b/sql/sql_class.h index 55987ae8ad9..abed643f822 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -1802,11 +1802,27 @@ public: } } - /* - Initialize the current database from a NULL-terminated string with length - If we run out of memory, we free the current database and return TRUE. - This way the user will notice the error as there will be no current - database selected (in addition to the error message set by malloc). + /** + Set the current database; use deep copy of C-string. + + @param new_db a pointer to the new database name. + @param new_db_len length of the new database name. + + Initialize the current database from a NULL-terminated string with + length. If we run out of memory, we free the current database and + return TRUE. This way the user will notice the error as there will be + no current database selected (in addition to the error message set by + malloc). + + @note This operation just sets {thd->db, thd->db_length}. Switching the + current database usually involves other actions, like switching other + database attributes including security context. In the future, this + operation will be made private and more convenient interface will be + provided. + + @return Operation status + @retval FALSE Success + @retval TRUE Out-of-memory error */ bool set_db(const char *new_db, size_t new_db_len) { @@ -1821,6 +1837,19 @@ public: db_length= db ? new_db_len : 0; return new_db && !db; } + + /** + Set the current database; use shallow copy of C-string. + + @param new_db a pointer to the new database name. + @param new_db_len length of the new database name. + + @note This operation just sets {thd->db, thd->db_length}. Switching the + current database usually involves other actions, like switching other + database attributes including security context. In the future, this + operation will be made private and more convenient interface will be + provided. + */ void reset_db(char *new_db, size_t new_db_len) { db= new_db; diff --git a/sql/sql_db.cc b/sql/sql_db.cc index 23e18f85a3c..207c48b45f0 100644 --- a/sql/sql_db.cc +++ b/sql/sql_db.cc @@ -39,6 +39,10 @@ static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, static long mysql_rm_arc_files(THD *thd, MY_DIR *dirp, const char *org_path); static my_bool rm_dir_w_symlink(const char *org_path, my_bool send_error); +static void mysql_change_db_impl(THD *thd, + LEX_STRING *new_db_name, + ulong new_db_access, + CHARSET_INFO *new_db_charset); /* Database lock hash */ @@ -988,7 +992,7 @@ exit: it to 0. */ if (thd->db && !strcmp(thd->db, db)) - thd->set_db(NULL, 0); + mysql_change_db_impl(thd, NULL, 0, thd->variables.collation_server); VOID(pthread_mutex_unlock(&LOCK_mysql_create_db)); start_waiting_global_read_lock(thd); exit2: @@ -1347,21 +1351,48 @@ static void mysql_change_db_impl(THD *thd, /** - @brief Change the current database. + @brief Change the current database and its attributes. @param thd thread handle @param new_db_name database name - @param force_switch if this flag is set (TRUE), mysql_change_db() will - switch to NULL db if the specified database is not - available anymore. Corresponding warning will be - thrown in this case. This flag is used to change - database in stored-routine-execution code. + @param force_switch if force_switch is FALSE, then the operation will fail if - @details Check that the database name corresponds to a valid and existent - database, check access rights (unless called with no_access_check), and - set the current database. This function is called to change the current - database upon user request (COM_CHANGE_DB command) or temporarily, to - execute a stored routine. + - new_db_name is NULL or empty; + + - OR new database name is invalid + (check_db_name() failed); + + - OR user has no privilege on the new database; + + - OR new database does not exist; + + if force_switch is TRUE, then + + - if new_db_name is NULL or empty, the current + database will be NULL, @@collation_database will + be set to @@collation_server, the operation will + succeed. + + - if new database name is invalid + (check_db_name() failed), the current database + will be NULL, @@collation_database will be set to + @@collation_server, but the operation will fail; + + - user privileges will not be checked + (THD::db_access however is updated); + + TODO: is this really the intention? + (see sp-security.test). + + - if new database does not exist,the current database + will be NULL, @@collation_database will be set to + @@collation_server, a warning will be thrown, the + operation will succeed. + + @details The function checks that the database name corresponds to a + valid and existent database, checks access rights and changes the current + database with database attributes (@@collation_database session variable, + THD::db_access). This function is not the only way to switch the database that is currently employed. When the replication slave thread switches the @@ -1398,8 +1429,13 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) if (force_switch) { /* - This can only happen when we restore the old db in THD after - execution of a routine is complete. Change db to NULL. + This can happen only if we're switching the current database back + after loading stored program. The thing is that loading of stored + program can happen when there is no current database. + + TODO: actually, new_db_name and new_db_name->str seem to be always + non-NULL. In case of stored program, new_db_name->str == "" and + new_db_name->length == 0. */ mysql_change_db_impl(thd, NULL, 0, thd->variables.collation_server); @@ -1417,7 +1453,7 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) if (my_strcasecmp(system_charset_info, new_db_name->str, INFORMATION_SCHEMA_NAME.str) == 0) { - /* Switch database to INFORMATION_SCHEMA. */ + /* Switch the current database to INFORMATION_SCHEMA. */ mysql_change_db_impl(thd, &INFORMATION_SCHEMA_NAME, SELECT_ACL, system_charset_info); @@ -1444,8 +1480,8 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) even if we are called from sp_head::execute(). It's next to impossible however to get this error when we are called - from sp_head::execute(). But let's switch database to NULL in this case - to be sure. + from sp_head::execute(). But let's switch the current database to NULL + in this case to be sure. */ if (check_db_name(&new_db_file_name)) @@ -1454,10 +1490,8 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) my_free(new_db_file_name.str, MYF(0)); if (force_switch) - { - /* Change db to NULL. */ mysql_change_db_impl(thd, NULL, 0, thd->variables.collation_server); - } + DBUG_RETURN(TRUE); } @@ -1492,6 +1526,8 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) { if (force_switch) { + /* Throw a warning and free new_db_file_name. */ + push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE, ER_BAD_DB_ERROR, ER(ER_BAD_DB_ERROR), new_db_file_name.str); @@ -1502,12 +1538,19 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) mysql_change_db_impl(thd, NULL, 0, thd->variables.collation_server); + /* The operation succeed. */ + DBUG_RETURN(FALSE); } else { + /* Report an error and free new_db_file_name. */ + my_error(ER_BAD_DB_ERROR, MYF(0), new_db_file_name.str); my_free(new_db_file_name.str, MYF(0)); + + /* The operation failed. */ + DBUG_RETURN(TRUE); } } @@ -1820,7 +1863,7 @@ bool mysql_rename_db(THD *thd, LEX_STRING *old_db, LEX_STRING *new_db) /* Step9: Let's do "use newdb" if we renamed the current database */ if (change_to_newdb) - error|= mysql_change_db(thd, new_db, 0); + error|= mysql_change_db(thd, new_db, FALSE); exit: pthread_mutex_lock(&LOCK_lock_db); From 5b10ba2ce22a5392a49776490db64d641a14a38c Mon Sep 17 00:00:00 2001 From: "kostja@bodhi.(none)" <> Date: Thu, 30 Aug 2007 21:19:05 +0400 Subject: [PATCH 23/35] Add -Wno-unused to --warning-mode=pedantic, otherwise sql/ compilation generated so many warnings that the mode was unusable. --- BUILD/SETUP.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BUILD/SETUP.sh b/BUILD/SETUP.sh index dc439e68c45..e940f7a3371 100755 --- a/BUILD/SETUP.sh +++ b/BUILD/SETUP.sh @@ -105,7 +105,7 @@ if [ "x$warning_mode" != "xpedantic" ]; then # Added unless --with-debug=full debug_extra_cflags="-O1 -Wuninitialized" else - warnings="-W -Wall -ansi -pedantic -Wno-long-long -D_POSIX_SOURCE" + warnings="-W -Wall -ansi -pedantic -Wno-long-long -Wno-unused -D_POSIX_SOURCE" c_warnings="$warnings" cxx_warnings="$warnings -std=c++98" # NOTE: warning mode should not influence optimize/debug mode. From 4792ed42204e3ea67b7bb3d3c9613dfafc608629 Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Thu, 30 Aug 2007 12:57:05 -0600 Subject: [PATCH 24/35] Bug#28779 (mysql_query() allows execution of statements with unbalanced comments) This change set is for 5.1 (manually merged) Before this fix, the server would accept queries that contained comments, even when the comments were not properly closed with a '*' '/' marker. For example, select 1 /* + 2 would be accepted as select 1 /* + 2 */ and executed as select 1 With this fix, the server now rejects queries with unclosed comments as syntax errors. Both regular comments ('/' '*') and special comments ('/' '*' '!') must be closed with '*' '/' to be parsed correctly. --- mysql-test/r/comments.result | 15 +++++++++++++ mysql-test/t/comments.test | 31 ++++++++++++++++++++++++++ sql/sql_lex.cc | 43 +++++++++++++++++++++++++++++------- 3 files changed, 81 insertions(+), 8 deletions(-) diff --git a/mysql-test/r/comments.result b/mysql-test/r/comments.result index 98921c561d1..d8ac68e2468 100644 --- a/mysql-test/r/comments.result +++ b/mysql-test/r/comments.result @@ -36,3 +36,18 @@ select 1/*!999992*/; select 1 + /*!00000 2 */ + 3 /*!99999 noise*/ + 4; 1 + 2 + 3 + 4 10 +drop table if exists table_28779; +create table table_28779 (a int); +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*' AND b = 'bar';"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/*' AND b = 'bar'' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*' AND b = 'bar';*"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/*' AND b = 'bar';*' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*! AND 2=2;"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*! AND 2=2;*"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';*' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!98765' AND b = 'bar';"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/*!98765' AND b = 'bar'' at line 1 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!98765' AND b = 'bar';*"; +ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/*!98765' AND b = 'bar';*' at line 1 +drop table table_28779; diff --git a/mysql-test/t/comments.test b/mysql-test/t/comments.test index 8ae6ba5779e..3a18a8bd483 100644 --- a/mysql-test/t/comments.test +++ b/mysql-test/t/comments.test @@ -34,3 +34,34 @@ select 1/*!999992*/; select 1 + /*!00000 2 */ + 3 /*!99999 noise*/ + 4; +# +# Bug#28779 (mysql_query() allows execution of statements with unbalanced +# comments) +# + +--disable_warnings +drop table if exists table_28779; +--enable_warnings + +create table table_28779 (a int); + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*' AND b = 'bar';"; + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*' AND b = 'bar';*"; + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*! AND 2=2;"; + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*! AND 2=2;*"; + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!98765' AND b = 'bar';"; + +--error 1064 +prepare bar from "DELETE FROM table_28779 WHERE a = 7 OR 1=1/*!98765' AND b = 'bar';*"; + +drop table table_28779; + diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 7fe2ed04d51..02837b5f0df 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -719,6 +719,7 @@ static inline uint int_token(const char *str,uint length) int MYSQLlex(void *arg, void *yythd) { reg1 uchar c; + bool comment_closed; int tokval, result_state; uint length; enum my_lex_states state; @@ -1211,7 +1212,10 @@ int MYSQLlex(void *arg, void *yythd) /* The special comment format is very strict: '/' '*' '!', followed by exactly - 2 digits (major), then 3 digits (minor). + 1 digit (major), 2 digits (minor), then 2 digits (dot). + 32302 -> 3.23.02 + 50032 -> 5.0.32 + 50114 -> 5.1.14 */ char version_str[6]; version_str[0]= lip->yyPeekn(0); @@ -1230,7 +1234,7 @@ int MYSQLlex(void *arg, void *yythd) ulong version; version=strtol(version_str, NULL, 10); - /* Accept 'M' 'M' 'm' 'm' 'm' */ + /* Accept 'M' 'm' 'm' 'd' 'd' */ lip->yySkipn(5); if (version <= MYSQL_VERSION_ID) @@ -1254,16 +1258,36 @@ int MYSQLlex(void *arg, void *yythd) lip->yySkip(); // Accept / lip->yySkip(); // Accept * } - - while (! lip->eof() && - ((c=lip->yyGet()) != '*' || lip->yyPeek() != '/')) + /* + Discard: + - regular '/' '*' comments, + - special comments '/' '*' '!' for a future version, + by scanning until we find a closing '*' '/' marker. + Note: There is no such thing as nesting comments, + the first '*' '/' sequence seen will mark the end. + */ + comment_closed= FALSE; + while (! lip->eof()) { - if (c == '\n') + c= lip->yyGet(); + if (c == '*') + { + if (lip->yyPeek() == '/') + { + lip->yySkip(); + comment_closed= TRUE; + state = MY_LEX_START; + break; + } + } + else if (c == '\n') lip->yylineno++; } - if (! lip->eof()) - lip->yySkip(); // remove last '/' + /* Unbalanced comments with a missing '*' '/' are a syntax error */ + if (! comment_closed) + return (ABORT_SYM); state = MY_LEX_START; // Try again + lip->in_comment= NO_COMMENT; lip->set_echo(TRUE); break; case MY_LEX_END_LONG_COMMENT: @@ -1315,6 +1339,9 @@ int MYSQLlex(void *arg, void *yythd) lip->set_echo(FALSE); lip->yySkip(); lip->set_echo(TRUE); + /* Unbalanced comments with a missing '*' '/' are a syntax error */ + if (lip->in_comment != NO_COMMENT) + return (ABORT_SYM); lip->next_state=MY_LEX_END; // Mark for next loop return(END_OF_INPUT); } From 1180c22aef303d85926acba398500c6abbd5d0cb Mon Sep 17 00:00:00 2001 From: "davi@moksha.local" <> Date: Thu, 30 Aug 2007 16:11:53 -0300 Subject: [PATCH 25/35] Bug#28587 SELECT is blocked by INSERT waiting on read lock, even with low_priority_updates The problem is that a SELECT on one thread is blocked by INSERT ... ON DUPLICATE KEY UPDATE on another thread even when low_priority_updates is activated. The solution is to possibly downgrade the lock type to the setting of low_priority_updates if the INSERT cannot be concurrent. --- sql/sql_insert.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index bd21d929291..f07af393070 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -417,7 +417,7 @@ void upgrade_lock_type(THD *thd, thr_lock_type *lock_type, if (duplic == DUP_UPDATE || duplic == DUP_REPLACE && *lock_type == TL_WRITE_CONCURRENT_INSERT) { - *lock_type= TL_WRITE; + *lock_type= TL_WRITE_DEFAULT; return; } From 37d48bfe30a893720435a4888ec06606b8c7f463 Mon Sep 17 00:00:00 2001 From: "davi@moksha.local" <> Date: Thu, 30 Aug 2007 16:19:23 -0300 Subject: [PATCH 26/35] Post-merge test case with wait_condition for Bug#28587 --- mysql-test/r/insert_update.result | 19 +++++++++++++++++ mysql-test/t/insert_update.test | 35 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/mysql-test/r/insert_update.result b/mysql-test/r/insert_update.result index 704cf444681..3aec9594d36 100644 --- a/mysql-test/r/insert_update.result +++ b/mysql-test/r/insert_update.result @@ -407,3 +407,22 @@ select if( @stamp1 = @stamp2, "correct", "wrong"); if( @stamp1 = @stamp2, "correct", "wrong") correct drop table t1; +connection: default +set low_priority_updates=1; +drop table if exists t1; +create table t1 (a int, b int, unique key t1$a (a)); +lock table t1 read; +connection: update +set low_priority_updates=1; +show variables like 'low_priority_updates'; +Variable_name Value +low_priority_updates ON +insert into t1 values (1, 2) ON DUPLICATE KEY UPDATE b = 2;; +connection: select +select * from t1; +a b +connection: default +select * from t1; +a b +drop table t1; +set low_priority_updates=default; diff --git a/mysql-test/t/insert_update.test b/mysql-test/t/insert_update.test index 67f21731afe..aad241e031f 100644 --- a/mysql-test/t/insert_update.test +++ b/mysql-test/t/insert_update.test @@ -306,3 +306,38 @@ insert into t1(f1) values(1) on duplicate key update f1=1; select @stamp2:=f2 from t1; select if( @stamp1 = @stamp2, "correct", "wrong"); drop table t1; + +# +# Bug#28587 SELECT is blocked by INSERT waiting on read lock, even with low_priority_updates +# +--echo connection: default +set low_priority_updates=1; +--disable_warnings +drop table if exists t1; +--enable_warnings +create table t1 (a int, b int, unique key t1$a (a)); +lock table t1 read; +connect (update,localhost,root,,); +connection update; +--echo connection: update +set low_priority_updates=1; +show variables like 'low_priority_updates'; +let $ID= `select connection_id()`; +--send insert into t1 values (1, 2) ON DUPLICATE KEY UPDATE b = 2; +connection default; +# we must wait till the insert opens and locks the table +let $wait_condition= + select count(*) = 1 from information_schema.processlist + where state = "Locked" and id = $ID; +--source include/wait_condition.inc +connect (select,localhost,root,,); +--echo connection: select +select * from t1; +connection default; +--echo connection: default +select * from t1; +connection default; +disconnect update; +disconnect select; +drop table t1; +set low_priority_updates=default; From 8d1af60da0e29618f822ea19cfed6c64cc9ae82a Mon Sep 17 00:00:00 2001 From: "kostja@bodhi.(none)" <> Date: Fri, 31 Aug 2007 10:19:52 +0400 Subject: [PATCH 27/35] Never access thd->ha_data directly, use getters/setters from the plugin API instead. This is a pre-requisite of the fix for Bug 12713, which changes the data type of thd->ha_data from void * to struct Ha_data. --- include/mysql/plugin.h | 23 ++++++++++++++++ sql/ha_ndbcluster_binlog.h | 6 ++-- sql/handler.cc | 4 +-- sql/log.cc | 46 +++++++++++++++---------------- sql/rpl_utility.h | 2 +- storage/federated/ha_federated.cc | 12 ++++---- 6 files changed, 59 insertions(+), 34 deletions(-) diff --git a/include/mysql/plugin.h b/include/mysql/plugin.h index 61ed7be738f..50ec051d111 100644 --- a/include/mysql/plugin.h +++ b/include/mysql/plugin.h @@ -785,5 +785,28 @@ void mysql_query_cache_invalidate4(MYSQL_THD thd, } #endif +#ifdef __cplusplus +/** + Provide a handler data getter to simplify coding +*/ +inline +void * +thd_get_ha_data(const MYSQL_THD thd, const struct handlerton *hton) +{ + return *thd_ha_data(thd, hton); +} + +/** + Provide a handler data setter to simplify coding +*/ +inline +void +thd_set_ha_data(const MYSQL_THD thd, const struct handlerton *hton, + const void *ha_data) +{ + *thd_ha_data(thd, hton)= (void*) ha_data; +} +#endif + #endif diff --git a/sql/ha_ndbcluster_binlog.h b/sql/ha_ndbcluster_binlog.h index b5b8d0d9745..53852028f08 100644 --- a/sql/ha_ndbcluster_binlog.h +++ b/sql/ha_ndbcluster_binlog.h @@ -216,10 +216,12 @@ inline void free_share(NDB_SHARE **share, bool have_lock= FALSE) inline Thd_ndb * -get_thd_ndb(THD *thd) { return (Thd_ndb *) thd->ha_data[ndbcluster_hton->slot]; } +get_thd_ndb(THD *thd) +{ return (Thd_ndb *) thd_get_ha_data(thd, ndbcluster_hton); } inline void -set_thd_ndb(THD *thd, Thd_ndb *thd_ndb) { thd->ha_data[ndbcluster_hton->slot]= thd_ndb; } +set_thd_ndb(THD *thd, Thd_ndb *thd_ndb) +{ thd_set_ha_data(thd, ndbcluster_hton, thd_ndb); } Ndb* check_ndb_in_thd(THD* thd); diff --git a/sql/handler.cc b/sql/handler.cc index 3e9524e9821..0479b3b1443 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -563,7 +563,7 @@ static my_bool closecon_handlerton(THD *thd, plugin_ref plugin, be rolled back already */ if (hton->state == SHOW_OPTION_YES && hton->close_connection && - thd->ha_data[hton->slot]) + thd_get_ha_data(thd, hton)) hton->close_connection(hton, thd); return FALSE; } @@ -1511,7 +1511,7 @@ void handler::ha_statistic_increment(ulong SSV::*offset) const void **handler::ha_data(THD *thd) const { - return (void **) thd->ha_data + ht->slot; + return thd_ha_data(thd, ht); } THD *handler::ha_thd(void) const diff --git a/sql/log.cc b/sql/log.cc index 3686459fedf..6296c7ffaf3 100644 --- a/sql/log.cc +++ b/sql/log.cc @@ -1215,10 +1215,10 @@ binlog_trans_log_savepos(THD *thd, my_off_t *pos) { DBUG_ENTER("binlog_trans_log_savepos"); DBUG_ASSERT(pos != NULL); - if (thd->ha_data[binlog_hton->slot] == NULL) + if (thd_get_ha_data(thd, binlog_hton) == NULL) thd->binlog_setup_trx_data(); binlog_trx_data *const trx_data= - (binlog_trx_data*) thd->ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton); DBUG_ASSERT(mysql_bin_log.is_open()); *pos= trx_data->position(); DBUG_PRINT("return", ("*pos: %lu", (ulong) *pos)); @@ -1247,12 +1247,12 @@ binlog_trans_log_truncate(THD *thd, my_off_t pos) DBUG_ENTER("binlog_trans_log_truncate"); DBUG_PRINT("enter", ("pos: %lu", (ulong) pos)); - DBUG_ASSERT(thd->ha_data[binlog_hton->slot] != NULL); + DBUG_ASSERT(thd_get_ha_data(thd, binlog_hton) != NULL); /* Only true if binlog_trans_log_savepos() wasn't called before */ DBUG_ASSERT(pos != ~(my_off_t) 0); binlog_trx_data *const trx_data= - (binlog_trx_data*) thd->ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton); trx_data->truncate(pos); DBUG_VOID_RETURN; } @@ -1283,9 +1283,9 @@ int binlog_init(void *p) static int binlog_close_connection(handlerton *hton, THD *thd) { binlog_trx_data *const trx_data= - (binlog_trx_data*) thd->ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton); DBUG_ASSERT(trx_data->empty()); - thd->ha_data[binlog_hton->slot]= 0; + thd_set_ha_data(thd, binlog_hton, NULL); trx_data->~binlog_trx_data(); my_free((uchar*)trx_data, MYF(0)); return 0; @@ -1408,7 +1408,7 @@ static int binlog_commit(handlerton *hton, THD *thd, bool all) { DBUG_ENTER("binlog_commit"); binlog_trx_data *const trx_data= - (binlog_trx_data*) thd->ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton); if (trx_data->empty()) { @@ -1435,7 +1435,7 @@ static int binlog_rollback(handlerton *hton, THD *thd, bool all) DBUG_ENTER("binlog_rollback"); int error=0; binlog_trx_data *const trx_data= - (binlog_trx_data*) thd->ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton); if (trx_data->empty()) { trx_data->reset(); @@ -3252,23 +3252,22 @@ int THD::binlog_setup_trx_data() { DBUG_ENTER("THD::binlog_setup_trx_data"); binlog_trx_data *trx_data= - (binlog_trx_data*) ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(this, binlog_hton); if (trx_data) DBUG_RETURN(0); // Already set up - ha_data[binlog_hton->slot]= trx_data= - (binlog_trx_data*) my_malloc(sizeof(binlog_trx_data), MYF(MY_ZEROFILL)); + trx_data= (binlog_trx_data*) my_malloc(sizeof(binlog_trx_data), MYF(MY_ZEROFILL)); if (!trx_data || open_cached_file(&trx_data->trans_log, mysql_tmpdir, LOG_PREFIX, binlog_cache_size, MYF(MY_WME))) { my_free((uchar*)trx_data, MYF(MY_ALLOW_ZERO_PTR)); - ha_data[binlog_hton->slot]= 0; DBUG_RETURN(1); // Didn't manage to set it up } + thd_set_ha_data(this, binlog_hton, trx_data); - trx_data= new (ha_data[binlog_hton->slot]) binlog_trx_data; + trx_data= new (thd_get_ha_data(this, binlog_hton)) binlog_trx_data; DBUG_RETURN(0); } @@ -3304,7 +3303,7 @@ int THD::binlog_setup_trx_data() void THD::binlog_start_trans_and_stmt() { - binlog_trx_data *trx_data= (binlog_trx_data*) ha_data[binlog_hton->slot]; + binlog_trx_data *trx_data= (binlog_trx_data*) thd_get_ha_data(this, binlog_hton); DBUG_ENTER("binlog_start_trans_and_stmt"); DBUG_PRINT("enter", ("trx_data: 0x%lx trx_data->before_stmt_pos: %lu", (long) trx_data, @@ -3324,7 +3323,7 @@ THD::binlog_start_trans_and_stmt() void THD::binlog_set_stmt_begin() { binlog_trx_data *trx_data= - (binlog_trx_data*) ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(this, binlog_hton); /* The call to binlog_trans_log_savepos() might create the trx_data @@ -3334,14 +3333,15 @@ void THD::binlog_set_stmt_begin() { */ my_off_t pos= 0; binlog_trans_log_savepos(this, &pos); - trx_data= (binlog_trx_data*) ha_data[binlog_hton->slot]; + trx_data= (binlog_trx_data*) thd_get_ha_data(this, binlog_hton); trx_data->before_stmt_pos= pos; } int THD::binlog_flush_transaction_cache() { DBUG_ENTER("binlog_flush_transaction_cache"); - binlog_trx_data *trx_data= (binlog_trx_data*) ha_data[binlog_hton->slot]; + binlog_trx_data *trx_data= (binlog_trx_data*) + thd_get_ha_data(this, binlog_hton); DBUG_PRINT("enter", ("trx_data=0x%lu", (ulong) trx_data)); if (trx_data) DBUG_PRINT("enter", ("trx_data->before_stmt_pos=%lu", @@ -3404,7 +3404,7 @@ Rows_log_event* THD::binlog_get_pending_rows_event() const { binlog_trx_data *const trx_data= - (binlog_trx_data*) ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(this, binlog_hton); /* This is less than ideal, but here's the story: If there is no trx_data, prepare_pending_rows_event() has never been called @@ -3417,11 +3417,11 @@ THD::binlog_get_pending_rows_event() const void THD::binlog_set_pending_rows_event(Rows_log_event* ev) { - if (ha_data[binlog_hton->slot] == NULL) + if (thd_get_ha_data(this, binlog_hton) == NULL) binlog_setup_trx_data(); binlog_trx_data *const trx_data= - (binlog_trx_data*) ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(this, binlog_hton); DBUG_ASSERT(trx_data); trx_data->set_pending(ev); @@ -3444,7 +3444,7 @@ MYSQL_BIN_LOG::flush_and_set_pending_rows_event(THD *thd, int error= 0; binlog_trx_data *const trx_data= - (binlog_trx_data*) thd->ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton); DBUG_ASSERT(trx_data); @@ -3595,7 +3595,7 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info) goto err; binlog_trx_data *const trx_data= - (binlog_trx_data*) thd->ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton); IO_CACHE *trans_log= &trx_data->trans_log; my_off_t trans_log_pos= my_b_tell(trans_log); if (event_info->get_cache_stmt() || trans_log_pos != 0) @@ -5032,7 +5032,7 @@ int TC_LOG_BINLOG::log_xid(THD *thd, my_xid xid) DBUG_ENTER("TC_LOG_BINLOG::log"); Xid_log_event xle(thd, xid); binlog_trx_data *trx_data= - (binlog_trx_data*) thd->ha_data[binlog_hton->slot]; + (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton); /* We always commit the entire transaction when writing an XID. Also note that the return value is inverted. diff --git a/sql/rpl_utility.h b/sql/rpl_utility.h index eac3d14dfc6..62b2ca23918 100644 --- a/sql/rpl_utility.h +++ b/sql/rpl_utility.h @@ -241,8 +241,8 @@ public: private: ulong m_size; // Number of elements in the types array field_type *m_type; // Array of type descriptors - uint16 *m_field_metadata; uint m_field_metadata_size; + uint16 *m_field_metadata; uchar *m_null_bits; uchar *m_memory; }; diff --git a/storage/federated/ha_federated.cc b/storage/federated/ha_federated.cc index ded0ce88484..edcd1127dbd 100644 --- a/storage/federated/ha_federated.cc +++ b/storage/federated/ha_federated.cc @@ -3169,7 +3169,7 @@ int ha_federated::external_lock(THD *thd, int lock_type) #ifdef XXX_SUPERCEDED_BY_WL2952 if (lock_type != F_UNLCK) { - ha_federated *trx= (ha_federated *)thd->ha_data[ht->slot]; + ha_federated *trx= (ha_federated *)thd_get_ha_data(thd, ht); DBUG_PRINT("info",("federated not lock F_UNLCK")); if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))) @@ -3200,7 +3200,7 @@ int ha_federated::external_lock(THD *thd, int lock_type) DBUG_PRINT("info", ("error setting autocommit FALSE: %d", error)); DBUG_RETURN(error); } - thd->ha_data[ht->slot]= this; + thd_set_ha_data(thd, ht, this); trans_register_ha(thd, TRUE, ht); /* Send a lock table to the remote end. @@ -3230,7 +3230,7 @@ int ha_federated::external_lock(THD *thd, int lock_type) static int federated_commit(handlerton *hton, THD *thd, bool all) { int return_val= 0; - ha_federated *trx= (ha_federated *)thd->ha_data[hton->slot]; + ha_federated *trx= (ha_federated *) thd_get_ha_data(thd, hton); DBUG_ENTER("federated_commit"); if (all) @@ -3245,7 +3245,7 @@ static int federated_commit(handlerton *hton, THD *thd, bool all) if (error && !return_val) return_val= error; } - thd->ha_data[hton->slot]= NULL; + thd_set_ha_data(thd, hton, NULL); } DBUG_PRINT("info", ("error val: %d", return_val)); @@ -3256,7 +3256,7 @@ static int federated_commit(handlerton *hton, THD *thd, bool all) static int federated_rollback(handlerton *hton, THD *thd, bool all) { int return_val= 0; - ha_federated *trx= (ha_federated *)thd->ha_data[hton->slot]; + ha_federated *trx= (ha_federated *)thd_get_ha_data(thd, hton); DBUG_ENTER("federated_rollback"); if (all) @@ -3271,7 +3271,7 @@ static int federated_rollback(handlerton *hton, THD *thd, bool all) if (error && !return_val) return_val= error; } - thd->ha_data[hton->slot]= NULL; + thd_set_ha_data(thd, hton, NULL); } DBUG_PRINT("info", ("error val: %d", return_val)); From e5ac5a0881a1525034453c1566df827717d20cf0 Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Fri, 31 Aug 2007 08:49:49 -0600 Subject: [PATCH 28/35] Test cleanup for events.test --- mysql-test/r/events.result | 26 ++++++++++++++++-------- mysql-test/t/events.test | 41 ++++++++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/mysql-test/r/events.result b/mysql-test/r/events.result index be67e2acc7b..d1ee6d1c5a1 100644 --- a/mysql-test/r/events.result +++ b/mysql-test/r/events.result @@ -454,8 +454,11 @@ create event закачка on schedule every 10 hour do select get_lock("test_l select definer, name, db from mysql.event; definer name db root@localhost закачка events_test -"Should be only 1 process" -select /*1*/ user, host, db, command, state, info from information_schema.processlist where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; +"Should be only 0 process" +select /*1*/ user, host, db, command, state, info +from information_schema.processlist +where (user='event_scheduler') +order by info; user host db command state info select release_lock("test_lock1"); release_lock("test_lock1") @@ -472,14 +475,15 @@ get_lock("test_lock2", 20) 1 "Create an event which tries to acquire a mutex. The event locks on the mutex" create event закачка on schedule every 10 hour do select get_lock("test_lock2", 20); -"Let some time pass to the event starts" "Should have only 2 processes: the scheduler and the locked event" -select /*2*/ user, host, db, command, state, info from information_schema.processlist where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; +select /*2*/ user, host, db, command, state, info +from information_schema.processlist +where (info like "select get_lock%" OR user='event_scheduler') +order by info; user host db command state info event_scheduler localhost NULL Daemon Waiting for next activation NULL root localhost events_test Connect User lock select get_lock("test_lock2", 20) "Release the mutex, the event worker should finish." -"Release the mutex, the event worker should finish." select release_lock("test_lock2"); release_lock("test_lock2") 1 @@ -489,14 +493,20 @@ select get_lock("test_lock2_1", 20); get_lock("test_lock2_1", 20) 1 create event закачка21 on schedule every 10 hour do select get_lock("test_lock2_1", 20); -"Should have only 3 processes: the scheduler, our conn and the locked event" -select /*3*/ user, host, db, command, state, info from information_schema.processlist where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; +"Should have only 2 processes: the scheduler and the locked event" +select /*3*/ user, host, db, command, state, info +from information_schema.processlist +where (info like "select get_lock%" OR user='event_scheduler') +order by info; user host db command state info event_scheduler localhost NULL Daemon Waiting for next activation NULL root localhost events_test Connect User lock select get_lock("test_lock2_1", 20) set global event_scheduler=off; "Should have only our process now:" -select /*4*/ user, host, db, command, state, info from information_schema.processlist where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; +select /*4*/ user, host, db, command, state, info +from information_schema.processlist +where (info like "select get_lock%" OR user='event_scheduler') +order by info; user host db command state info root localhost events_test Connect User lock select get_lock("test_lock2_1", 20) select release_lock("test_lock2_1"); diff --git a/mysql-test/t/events.test b/mysql-test/t/events.test index cbb36cbd2be..365318e1db9 100644 --- a/mysql-test/t/events.test +++ b/mysql-test/t/events.test @@ -432,8 +432,11 @@ create event закачка on schedule every 10 hour do select get_lock("test_l --echo "Should return 1 row" select definer, name, db from mysql.event; ---echo "Should be only 1 process" -select /*1*/ user, host, db, command, state, info from information_schema.processlist where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; +--echo "Should be only 0 process" +select /*1*/ user, host, db, command, state, info + from information_schema.processlist + where (user='event_scheduler') + order by info; select release_lock("test_lock1"); drop event закачка; --echo "Should have 0 events" @@ -447,10 +450,16 @@ set global event_scheduler=on; select get_lock("test_lock2", 20); --echo "Create an event which tries to acquire a mutex. The event locks on the mutex" create event закачка on schedule every 10 hour do select get_lock("test_lock2", 20); ---echo "Let some time pass to the event starts" ---sleep 1 + --echo "Should have only 2 processes: the scheduler and the locked event" -select /*2*/ user, host, db, command, state, info from information_schema.processlist where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info;--echo "Release the mutex, the event worker should finish." +let $wait_condition= select count(*) = 2 from information_schema.processlist + where (info like "select get_lock%" OR user='event_scheduler'); +--source include/wait_condition.inc + +select /*2*/ user, host, db, command, state, info + from information_schema.processlist + where (info like "select get_lock%" OR user='event_scheduler') + order by info; --echo "Release the mutex, the event worker should finish." select release_lock("test_lock2"); drop event закачка; @@ -466,16 +475,28 @@ drop event закачка; set global event_scheduler=1; select get_lock("test_lock2_1", 20); create event закачка21 on schedule every 10 hour do select get_lock("test_lock2_1", 20); -let $wait_condition= select count(*) = 3 from information_schema.processlist; + +--echo "Should have only 2 processes: the scheduler and the locked event" +let $wait_condition= select count(*) = 2 from information_schema.processlist + where (info like "select get_lock%" OR user='event_scheduler'); --source include/wait_condition.inc ---echo "Should have only 3 processes: the scheduler, our conn and the locked event" -select /*3*/ user, host, db, command, state, info from information_schema.processlist where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; + +select /*3*/ user, host, db, command, state, info + from information_schema.processlist + where (info like "select get_lock%" OR user='event_scheduler') + order by info; + set global event_scheduler=off; + let $wait_condition= select count(*) =1 from information_schema.processlist - where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; + where (info like "select get_lock%" OR user='event_scheduler'); --source include/wait_condition.inc + --echo "Should have only our process now:" -select /*4*/ user, host, db, command, state, info from information_schema.processlist where (command!='Daemon' || user='event_scheduler') and (info is null or info not like '%processlist%') order by info; +select /*4*/ user, host, db, command, state, info + from information_schema.processlist + where (info like "select get_lock%" OR user='event_scheduler') + order by info; select release_lock("test_lock2_1"); drop event закачка21; let $wait_condition= From d6f94b062c370fc0765a8720bdbba50e6d872802 Mon Sep 17 00:00:00 2001 From: "anozdrin/alik@ibm.opbmk" <> Date: Fri, 31 Aug 2007 20:42:14 +0400 Subject: [PATCH 29/35] Fix for BUG#25843: changing default database between PREPARE and EXECUTE of statement breaks binlog. There were two problems discovered by this bug: 1. Default (current) database is not fixed at the creation time. That leads to wrong output of DATABASE() function. 2. Database attributes (@@collation_database) are not fixed at the creation time. That leads to wrong resultset. Binlog breakage and Query Cache wrong output happened because of the first problem. The fix is to remember the current database at the PREPARE-time and set it each time at EXECUTE. --- .../include/query_cache_sql_prepare.inc | 218 ++++++++++++++++++ mysql-test/r/query_cache_ps_no_prot.result | 158 +++++++++++++ mysql-test/r/query_cache_ps_ps_prot.result | 158 +++++++++++++ mysql-test/suite/rpl/r/rpl_ps.result | 39 ++++ mysql-test/suite/rpl/t/rpl_ps.test | 68 ++++++ sql/mysql_priv.h | 7 + sql/sp.cc | 150 ++++-------- sql/sp.h | 11 - sql/sp_head.cc | 22 +- sql/sql_class.cc | 7 +- sql/sql_class.h | 60 +++-- sql/sql_db.cc | 98 +++++++- sql/sql_prepare.cc | 48 ++++ 13 files changed, 891 insertions(+), 153 deletions(-) diff --git a/mysql-test/include/query_cache_sql_prepare.inc b/mysql-test/include/query_cache_sql_prepare.inc index cf6d4c26959..1842d5412bb 100644 --- a/mysql-test/include/query_cache_sql_prepare.inc +++ b/mysql-test/include/query_cache_sql_prepare.inc @@ -275,5 +275,223 @@ drop table t1; --echo ---- disconnect connection con1 ---- disconnect con1; +# +# Bug #25843 Changing default database between PREPARE and EXECUTE of statement +# breaks binlog. +# +# There were actually two problems discovered by this bug: +# +# 1. Default (current) database is not fixed at the creation time. +# That leads to wrong output of DATABASE() function. +# +# 2. Database attributes (@@collation_database) are not fixed at the creation +# time. That leads to wrong resultset. +# +# Binlog breakage and Query Cache wrong output happened because of the first +# problem. +# + +--echo ######################################################################## +--echo # +--echo # BUG#25843: Changing default database between PREPARE and EXECUTE of +--echo # statement breaks binlog. +--echo # +--echo ######################################################################## + +############################################################################### + +--echo +--echo # +--echo # Check that default database and its attributes are fixed at the +--echo # creation time. +--echo # + +# Prepare data structures. + +--echo +--disable_warnings +DROP DATABASE IF EXISTS mysqltest1; +DROP DATABASE IF EXISTS mysqltest2; +--enable_warnings + +--echo +CREATE DATABASE mysqltest1 COLLATE utf8_unicode_ci; +CREATE DATABASE mysqltest2 COLLATE utf8_general_ci; + +--echo +CREATE TABLE mysqltest1.t1(msg VARCHAR(255)); +CREATE TABLE mysqltest2.t1(msg VARCHAR(255)); + +# - Create a prepared statement with mysqltest1 as default database; + +--echo + +use mysqltest1; + +PREPARE stmt_a_1 FROM 'INSERT INTO t1 VALUES(DATABASE())'; +PREPARE stmt_a_2 FROM 'INSERT INTO t1 VALUES(@@collation_database)'; + +# - Execute on mysqltest1. + +--echo + +EXECUTE stmt_a_1; +EXECUTE stmt_a_2; + +# - Execute on mysqltest2. + +--echo + +use mysqltest2; + +EXECUTE stmt_a_1; +EXECUTE stmt_a_2; + +# - Check the results; + +--echo +SELECT * FROM mysqltest1.t1; + +--echo +SELECT * FROM mysqltest2.t1; + +# - Drop prepared statements. + +--echo +DROP PREPARE stmt_a_1; +DROP PREPARE stmt_a_2; + +############################################################################### + +--echo +--echo # +--echo # The Query Cache test case. +--echo # + +--echo +DELETE FROM mysqltest1.t1; +DELETE FROM mysqltest2.t1; + +--echo +INSERT INTO mysqltest1.t1 VALUES('mysqltest1.t1'); +INSERT INTO mysqltest2.t1 VALUES('mysqltest2.t1'); + +--echo +use mysqltest1; +PREPARE stmt_b_1 FROM 'SELECT * FROM t1'; + +--echo +use mysqltest2; +PREPARE stmt_b_2 FROM 'SELECT * FROM t1'; + +--echo +EXECUTE stmt_b_1; + +--echo +EXECUTE stmt_b_2; + +--echo +use mysqltest1; + +--echo +EXECUTE stmt_b_1; + +--echo +EXECUTE stmt_b_2; + +--echo +DROP PREPARE stmt_b_1; +DROP PREPARE stmt_b_2; + +# Cleanup. + +--echo +use test; + +--echo +DROP DATABASE mysqltest1; +DROP DATABASE mysqltest2; + +############################################################################### + +--echo +--echo # +--echo # Check that prepared statements work properly when there is no current +--echo # database. +--echo # + +--echo +CREATE DATABASE mysqltest1 COLLATE utf8_unicode_ci; +CREATE DATABASE mysqltest2 COLLATE utf8_general_ci; + +--echo +use mysqltest1; + +--echo +PREPARE stmt_c_1 FROM 'SELECT DATABASE(), @@collation_database'; + +--echo +use mysqltest2; + +--echo +PREPARE stmt_c_2 FROM 'SELECT DATABASE(), @@collation_database'; + +--echo +DROP DATABASE mysqltest2; + +--echo +SELECT DATABASE(), @@collation_database; + +# -- Here we have: current db: NULL; stmt db: mysqltest1; +--echo +EXECUTE stmt_c_1; + +--echo +SELECT DATABASE(), @@collation_database; + +# -- Here we have: current db: NULL; stmt db: mysqltest2 (non-existent); +--echo +EXECUTE stmt_c_2; + +--echo +SELECT DATABASE(), @@collation_database; + +# -- Create prepared statement, which has no current database. + +--echo +PREPARE stmt_c_3 FROM 'SELECT DATABASE(), @@collation_database'; + +# -- Here we have: current db: NULL; stmt db: NULL; +--echo +EXECUTE stmt_c_3; + +--echo +use mysqltest1; + +# -- Here we have: current db: mysqltest1; stmt db: mysqltest2 (non-existent); +--echo +EXECUTE stmt_c_2; + +--echo +SELECT DATABASE(), @@collation_database; + +# -- Here we have: current db: mysqltest1; stmt db: NULL; +--echo +EXECUTE stmt_c_3; + +--echo +SELECT DATABASE(), @@collation_database; + +--echo +DROP DATABASE mysqltest1; + +--echo +use test; + +--echo +--echo ######################################################################## + +############################################################################### + set @@global.query_cache_size=@initial_query_cache_size; flush status; # reset Qcache status variables for next tests diff --git a/mysql-test/r/query_cache_ps_no_prot.result b/mysql-test/r/query_cache_ps_no_prot.result index 29d16d8a619..0efb2540c64 100644 --- a/mysql-test/r/query_cache_ps_no_prot.result +++ b/mysql-test/r/query_cache_ps_no_prot.result @@ -371,5 +371,163 @@ Variable_name Value Qcache_hits 21 drop table t1; ---- disconnect connection con1 ---- +######################################################################## +# +# BUG#25843: Changing default database between PREPARE and EXECUTE of +# statement breaks binlog. +# +######################################################################## + +# +# Check that default database and its attributes are fixed at the +# creation time. +# + +DROP DATABASE IF EXISTS mysqltest1; +DROP DATABASE IF EXISTS mysqltest2; + +CREATE DATABASE mysqltest1 COLLATE utf8_unicode_ci; +CREATE DATABASE mysqltest2 COLLATE utf8_general_ci; + +CREATE TABLE mysqltest1.t1(msg VARCHAR(255)); +CREATE TABLE mysqltest2.t1(msg VARCHAR(255)); + +use mysqltest1; +PREPARE stmt_a_1 FROM 'INSERT INTO t1 VALUES(DATABASE())'; +PREPARE stmt_a_2 FROM 'INSERT INTO t1 VALUES(@@collation_database)'; + +EXECUTE stmt_a_1; +EXECUTE stmt_a_2; + +use mysqltest2; +EXECUTE stmt_a_1; +EXECUTE stmt_a_2; + +SELECT * FROM mysqltest1.t1; +msg +mysqltest1 +utf8_unicode_ci +mysqltest1 +utf8_unicode_ci + +SELECT * FROM mysqltest2.t1; +msg + +DROP PREPARE stmt_a_1; +DROP PREPARE stmt_a_2; + +# +# The Query Cache test case. +# + +DELETE FROM mysqltest1.t1; +DELETE FROM mysqltest2.t1; + +INSERT INTO mysqltest1.t1 VALUES('mysqltest1.t1'); +INSERT INTO mysqltest2.t1 VALUES('mysqltest2.t1'); + +use mysqltest1; +PREPARE stmt_b_1 FROM 'SELECT * FROM t1'; + +use mysqltest2; +PREPARE stmt_b_2 FROM 'SELECT * FROM t1'; + +EXECUTE stmt_b_1; +msg +mysqltest1.t1 + +EXECUTE stmt_b_2; +msg +mysqltest2.t1 + +use mysqltest1; + +EXECUTE stmt_b_1; +msg +mysqltest1.t1 + +EXECUTE stmt_b_2; +msg +mysqltest2.t1 + +DROP PREPARE stmt_b_1; +DROP PREPARE stmt_b_2; + +use test; + +DROP DATABASE mysqltest1; +DROP DATABASE mysqltest2; + +# +# Check that prepared statements work properly when there is no current +# database. +# + +CREATE DATABASE mysqltest1 COLLATE utf8_unicode_ci; +CREATE DATABASE mysqltest2 COLLATE utf8_general_ci; + +use mysqltest1; + +PREPARE stmt_c_1 FROM 'SELECT DATABASE(), @@collation_database'; + +use mysqltest2; + +PREPARE stmt_c_2 FROM 'SELECT DATABASE(), @@collation_database'; + +DROP DATABASE mysqltest2; + +SELECT DATABASE(), @@collation_database; +DATABASE() @@collation_database +NULL latin1_swedish_ci + +EXECUTE stmt_c_1; +DATABASE() @@collation_database +mysqltest1 utf8_unicode_ci + +SELECT DATABASE(), @@collation_database; +DATABASE() @@collation_database +NULL latin1_swedish_ci + +EXECUTE stmt_c_2; +DATABASE() @@collation_database +NULL latin1_swedish_ci +Warnings: +Note 1049 Unknown database 'mysqltest2' + +SELECT DATABASE(), @@collation_database; +DATABASE() @@collation_database +NULL latin1_swedish_ci + +PREPARE stmt_c_3 FROM 'SELECT DATABASE(), @@collation_database'; + +EXECUTE stmt_c_3; +DATABASE() @@collation_database +NULL latin1_swedish_ci + +use mysqltest1; + +EXECUTE stmt_c_2; +DATABASE() @@collation_database +NULL latin1_swedish_ci +Warnings: +Note 1049 Unknown database 'mysqltest2' + +SELECT DATABASE(), @@collation_database; +DATABASE() @@collation_database +mysqltest1 utf8_unicode_ci + +EXECUTE stmt_c_3; +DATABASE() @@collation_database +NULL latin1_swedish_ci + +SELECT DATABASE(), @@collation_database; +DATABASE() @@collation_database +mysqltest1 utf8_unicode_ci + +DROP DATABASE mysqltest1; + +use test; + +######################################################################## set @@global.query_cache_size=@initial_query_cache_size; flush status; diff --git a/mysql-test/r/query_cache_ps_ps_prot.result b/mysql-test/r/query_cache_ps_ps_prot.result index ba675d57f50..dc0c4a0193a 100644 --- a/mysql-test/r/query_cache_ps_ps_prot.result +++ b/mysql-test/r/query_cache_ps_ps_prot.result @@ -371,5 +371,163 @@ Variable_name Value Qcache_hits 19 drop table t1; ---- disconnect connection con1 ---- +######################################################################## +# +# BUG#25843: Changing default database between PREPARE and EXECUTE of +# statement breaks binlog. +# +######################################################################## + +# +# Check that default database and its attributes are fixed at the +# creation time. +# + +DROP DATABASE IF EXISTS mysqltest1; +DROP DATABASE IF EXISTS mysqltest2; + +CREATE DATABASE mysqltest1 COLLATE utf8_unicode_ci; +CREATE DATABASE mysqltest2 COLLATE utf8_general_ci; + +CREATE TABLE mysqltest1.t1(msg VARCHAR(255)); +CREATE TABLE mysqltest2.t1(msg VARCHAR(255)); + +use mysqltest1; +PREPARE stmt_a_1 FROM 'INSERT INTO t1 VALUES(DATABASE())'; +PREPARE stmt_a_2 FROM 'INSERT INTO t1 VALUES(@@collation_database)'; + +EXECUTE stmt_a_1; +EXECUTE stmt_a_2; + +use mysqltest2; +EXECUTE stmt_a_1; +EXECUTE stmt_a_2; + +SELECT * FROM mysqltest1.t1; +msg +mysqltest1 +utf8_unicode_ci +mysqltest1 +utf8_unicode_ci + +SELECT * FROM mysqltest2.t1; +msg + +DROP PREPARE stmt_a_1; +DROP PREPARE stmt_a_2; + +# +# The Query Cache test case. +# + +DELETE FROM mysqltest1.t1; +DELETE FROM mysqltest2.t1; + +INSERT INTO mysqltest1.t1 VALUES('mysqltest1.t1'); +INSERT INTO mysqltest2.t1 VALUES('mysqltest2.t1'); + +use mysqltest1; +PREPARE stmt_b_1 FROM 'SELECT * FROM t1'; + +use mysqltest2; +PREPARE stmt_b_2 FROM 'SELECT * FROM t1'; + +EXECUTE stmt_b_1; +msg +mysqltest1.t1 + +EXECUTE stmt_b_2; +msg +mysqltest2.t1 + +use mysqltest1; + +EXECUTE stmt_b_1; +msg +mysqltest1.t1 + +EXECUTE stmt_b_2; +msg +mysqltest2.t1 + +DROP PREPARE stmt_b_1; +DROP PREPARE stmt_b_2; + +use test; + +DROP DATABASE mysqltest1; +DROP DATABASE mysqltest2; + +# +# Check that prepared statements work properly when there is no current +# database. +# + +CREATE DATABASE mysqltest1 COLLATE utf8_unicode_ci; +CREATE DATABASE mysqltest2 COLLATE utf8_general_ci; + +use mysqltest1; + +PREPARE stmt_c_1 FROM 'SELECT DATABASE(), @@collation_database'; + +use mysqltest2; + +PREPARE stmt_c_2 FROM 'SELECT DATABASE(), @@collation_database'; + +DROP DATABASE mysqltest2; + +SELECT DATABASE(), @@collation_database; +DATABASE() @@collation_database +NULL latin1_swedish_ci + +EXECUTE stmt_c_1; +DATABASE() @@collation_database +mysqltest1 utf8_unicode_ci + +SELECT DATABASE(), @@collation_database; +DATABASE() @@collation_database +NULL latin1_swedish_ci + +EXECUTE stmt_c_2; +DATABASE() @@collation_database +NULL latin1_swedish_ci +Warnings: +Note 1049 Unknown database 'mysqltest2' + +SELECT DATABASE(), @@collation_database; +DATABASE() @@collation_database +NULL latin1_swedish_ci + +PREPARE stmt_c_3 FROM 'SELECT DATABASE(), @@collation_database'; + +EXECUTE stmt_c_3; +DATABASE() @@collation_database +NULL latin1_swedish_ci + +use mysqltest1; + +EXECUTE stmt_c_2; +DATABASE() @@collation_database +NULL latin1_swedish_ci +Warnings: +Note 1049 Unknown database 'mysqltest2' + +SELECT DATABASE(), @@collation_database; +DATABASE() @@collation_database +mysqltest1 utf8_unicode_ci + +EXECUTE stmt_c_3; +DATABASE() @@collation_database +NULL latin1_swedish_ci + +SELECT DATABASE(), @@collation_database; +DATABASE() @@collation_database +mysqltest1 utf8_unicode_ci + +DROP DATABASE mysqltest1; + +use test; + +######################################################################## set @@global.query_cache_size=@initial_query_cache_size; flush status; diff --git a/mysql-test/suite/rpl/r/rpl_ps.result b/mysql-test/suite/rpl/r/rpl_ps.result index 73c36af4862..dd3ed165226 100644 --- a/mysql-test/suite/rpl/r/rpl_ps.result +++ b/mysql-test/suite/rpl/r/rpl_ps.result @@ -26,5 +26,44 @@ from-master-2-'', from-var-from-master-3 drop table t1; stop slave; + +######################################################################## +# +# BUG#25843: Changing default database between PREPARE and EXECUTE of +# statement breaks binlog. +# +######################################################################## + +# +# Check that binlog is filled properly. +# + +CREATE DATABASE mysqltest1; +CREATE TABLE t1(c INT); + +RESET MASTER; +RESET SLAVE; + +PREPARE stmt_d_1 FROM 'INSERT INTO t1 VALUES(1)'; + +EXECUTE stmt_d_1; + +use mysqltest1; + +EXECUTE stmt_d_1; + +FLUSH LOGS; + +SHOW BINLOG EVENTS FROM 106; +Log_name Pos Event_type Server_id End_log_pos Info +slave-bin.000001 106 Query 2 193 use `test`; INSERT INTO t1 VALUES(1) +slave-bin.000001 193 Query 2 280 use `test`; INSERT INTO t1 VALUES(1) +slave-bin.000001 280 Rotate 2 323 slave-bin.000002;pos=4 + +DROP DATABASE mysqltest1; + +use test; + +######################################################################## reset master; reset slave; diff --git a/mysql-test/suite/rpl/t/rpl_ps.test b/mysql-test/suite/rpl/t/rpl_ps.test index b8792722192..0c8a499c9fe 100644 --- a/mysql-test/suite/rpl/t/rpl_ps.test +++ b/mysql-test/suite/rpl/t/rpl_ps.test @@ -46,6 +46,74 @@ stop slave; # End of 4.1 tests +# +# Bug #25843 Changing default database between PREPARE and EXECUTE of statement +# breaks binlog. +# +# There were actually two problems discovered by this bug: +# +# 1. Default (current) database is not fixed at the creation time. +# That leads to wrong output of DATABASE() function. +# +# 2. Database attributes (@@collation_database) are not fixed at the creation +# time. That leads to wrong resultset. +# +# Binlog breakage and Query Cache wrong output happened because of the first +# problem. +# + +--echo +--echo ######################################################################## +--echo # +--echo # BUG#25843: Changing default database between PREPARE and EXECUTE of +--echo # statement breaks binlog. +--echo # +--echo ######################################################################## + +############################################################################### + +--echo +--echo # +--echo # Check that binlog is filled properly. +--echo # + +--echo +CREATE DATABASE mysqltest1; +CREATE TABLE t1(c INT); + +--echo +RESET MASTER; +RESET SLAVE; + +--echo +PREPARE stmt_d_1 FROM 'INSERT INTO t1 VALUES(1)'; + +--echo +EXECUTE stmt_d_1; + +--echo +use mysqltest1; + +--echo +EXECUTE stmt_d_1; + +--echo +FLUSH LOGS; + +--echo +SHOW BINLOG EVENTS FROM 106; + +--echo +DROP DATABASE mysqltest1; + +--echo +use test; + +--echo +--echo ######################################################################## + +############################################################################### + reset master; reset slave; disconnect master; diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index a607e77eecf..7a451b7d287 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -937,9 +937,16 @@ bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list, bool silent); bool do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name, char *new_table_alias, bool skip_error); + bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch); +bool mysql_opt_change_db(THD *thd, + const LEX_STRING *new_db_name, + LEX_STRING *saved_db_name, + bool force_switch, + bool *cur_db_changed); + void mysql_parse(THD *thd, const char *inBuf, uint length, const char ** semicolon); diff --git a/sql/sp.cc b/sql/sp.cc index 8655e520041..e910bd6443d 100644 --- a/sql/sp.cc +++ b/sql/sp.cc @@ -519,9 +519,10 @@ db_load_routine(THD *thd, int type, sp_name *name, sp_head **sphp, { LEX *old_lex= thd->lex, newlex; String defstr; - char old_db_buf[NAME_LEN+1]; - LEX_STRING old_db= { old_db_buf, sizeof(old_db_buf) }; - bool dbchanged; + char saved_cur_db_name_buf[NAME_LEN+1]; + LEX_STRING saved_cur_db_name= + { saved_cur_db_name_buf, sizeof(saved_cur_db_name_buf) }; + bool cur_db_changed; ulong old_sql_mode= thd->variables.sql_mode; ha_rows old_select_limit= thd->variables.select_limit; sp_rcontext *old_spcont= thd->spcont; @@ -566,16 +567,16 @@ db_load_routine(THD *thd, int type, sp_name *name, sp_head **sphp, } /* - Change current database if needed. + Change the current database (if needed). - collation_database will be updated here. However, it can be wrong, - because it will contain the current value of the database collation. - We need collation_database to be fixed at the creation time -- so - we'll update it later in switch_query_ctx(). + TODO: why do we force switch here? */ - if ((ret= sp_use_new_db(thd, name->m_db, &old_db, TRUE, &dbchanged))) + if (mysql_opt_change_db(thd, &name->m_db, &saved_cur_db_name, TRUE, + &cur_db_changed)) + { goto end; + } thd->spcont= NULL; @@ -584,34 +585,42 @@ db_load_routine(THD *thd, int type, sp_name *name, sp_head **sphp, lex_start(thd); - if (parse_sql(thd, &lip, creation_ctx) || newlex.sphead == NULL) - { - sp_head *sp= newlex.sphead; + ret= parse_sql(thd, &lip, creation_ctx) || newlex.sphead == NULL; - if (dbchanged && (ret= mysql_change_db(thd, &old_db, TRUE))) - goto end; - delete sp; - ret= SP_PARSE_ERROR; - } - else + /* + Force switching back to the saved current database (if changed), + because it may be NULL. In this case, mysql_change_db() would + generate an error. + */ + + if (cur_db_changed && mysql_change_db(thd, &saved_cur_db_name, TRUE)) { - if (dbchanged && (ret= mysql_change_db(thd, &old_db, TRUE))) - goto end; - *sphp= newlex.sphead; - (*sphp)->set_definer(&definer_user_name, &definer_host_name); - (*sphp)->set_info(created, modified, &chistics, sql_mode); - (*sphp)->set_creation_ctx(creation_ctx); - (*sphp)->optimize(); - /* - Not strictly necessary to invoke this method here, since we know - that we've parsed CREATE PROCEDURE/FUNCTION and not an - UPDATE/DELETE/INSERT/REPLACE/LOAD/CREATE TABLE, but we try to - maintain the invariant that this method is called for each - distinct statement, in case its logic is extended with other - types of analyses in future. - */ - newlex.set_trg_event_type_for_tables(); + delete newlex.sphead; + ret= -1; + goto end; } + + if (ret) + { + delete newlex.sphead; + ret= SP_PARSE_ERROR; + goto end; + } + + *sphp= newlex.sphead; + (*sphp)->set_definer(&definer_user_name, &definer_host_name); + (*sphp)->set_info(created, modified, &chistics, sql_mode); + (*sphp)->set_creation_ctx(creation_ctx); + (*sphp)->optimize(); + /* + Not strictly necessary to invoke this method here, since we know + that we've parsed CREATE PROCEDURE/FUNCTION and not an + UPDATE/DELETE/INSERT/REPLACE/LOAD/CREATE TABLE, but we try to + maintain the invariant that this method is called for each + distinct statement, in case its logic is extended with other + types of analyses in future. + */ + newlex.set_trg_event_type_for_tables(); } end: @@ -2024,76 +2033,3 @@ create_string(THD *thd, String *buf, buf->append(body, bodylen); return TRUE; } - - - -/** - Change the current database if needed. - - @param[in] thd thread handle - @param[in] new_db new database name - @param[in, out] old_db IN: str points to a buffer where to store - the old database, length contains the - size of the buffer - OUT: if old db was not NULL, its name is - copied to the buffer pointed at by str - and length is updated accordingly. - Otherwise str[0] is set to '\0' and - length is set to 0. The out parameter - should be used only if the database name - has been changed (see dbchangedp). - @param[in] force_switch Flag to mysql_change_db(). For more information, - see mysql_change_db() comment. - @param[out] dbchangedp is set to TRUE if the current database is - changed, FALSE otherwise. The current - database is not changed if the old name - is equal to the new one, both names are - empty, or an error has occurred. - - @return Operation status. - @retval 0 on success - @retval 1 access denied or out of memory - (the error message is set in THD) -*/ - -int -sp_use_new_db(THD *thd, - LEX_STRING new_db, - LEX_STRING *old_db, - bool force_switch, - bool *dbchangedp) -{ - int ret; - DBUG_ENTER("sp_use_new_db"); - DBUG_PRINT("enter", ("newdb: %s", new_db.str)); - - /* - A stored routine always belongs to some database. The - old database (old_db) might be NULL, but to restore the - old database we will use mysql_change_db. - */ - DBUG_ASSERT(new_db.str && new_db.length); - - if (thd->db) - { - old_db->length= (strmake(old_db->str, thd->db, old_db->length) - - old_db->str); - } - else - { - old_db->str[0]= '\0'; - old_db->length= 0; - } - - /* Don't change the database if the new name is the same as the old one. */ - if (my_strcasecmp(system_charset_info, old_db->str, new_db.str) == 0) - { - *dbchangedp= FALSE; - DBUG_RETURN(0); - } - - ret= mysql_change_db(thd, &new_db, force_switch); - - *dbchangedp= ret == 0; - DBUG_RETURN(ret); -} diff --git a/sql/sp.h b/sql/sp.h index 52b0344a2e2..3797eb289a4 100644 --- a/sql/sp.h +++ b/sql/sp.h @@ -85,15 +85,4 @@ extern "C" uchar* sp_sroutine_key(const uchar *ptr, size_t *plen, */ TABLE *open_proc_table_for_read(THD *thd, Open_tables_state *backup); - -/* - Do a "use new_db". The current db is stored at old_db. If new_db is the - same as the current one, nothing is changed. dbchangedp is set to true if - the db was actually changed. -*/ - -int -sp_use_new_db(THD *thd, LEX_STRING new_db, LEX_STRING *old_db, - bool no_access_check, bool *dbchangedp); - #endif /* _SP_H_ */ diff --git a/sql/sp_head.cc b/sql/sp_head.cc index de8c4d89466..1c2dcef476b 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -1015,9 +1015,10 @@ bool sp_head::execute(THD *thd) { DBUG_ENTER("sp_head::execute"); - char old_db_buf[NAME_LEN+1]; - LEX_STRING old_db= { old_db_buf, sizeof(old_db_buf) }; - bool dbchanged; + char saved_cur_db_name_buf[NAME_LEN+1]; + LEX_STRING saved_cur_db_name= + { saved_cur_db_name_buf, sizeof(saved_cur_db_name_buf) }; + bool cur_db_changed= FALSE; sp_rcontext *ctx; bool err_status= FALSE; uint ip= 0; @@ -1072,8 +1073,11 @@ sp_head::execute(THD *thd) */ if (m_db.length && - (err_status= sp_use_new_db(thd, m_db, &old_db, 0, &dbchanged))) + (err_status= mysql_opt_change_db(thd, &m_db, &saved_cur_db_name, FALSE, + &cur_db_changed))) + { goto done; + } if ((ctx= thd->spcont)) ctx->clear_handler(); @@ -1252,14 +1256,14 @@ sp_head::execute(THD *thd) If the DB has changed, the pointer has changed too, but the original thd->db will then have been freed */ - if (dbchanged) + if (cur_db_changed && !thd->killed) { /* - No access check when changing back to where we came from. - (It would generate an error from mysql_change_db() when old_db=="") + Force switching back to the saved current database, because it may be + NULL. In this case, mysql_change_db() would generate an error. */ - if (! thd->killed) - err_status|= mysql_change_db(thd, &old_db, TRUE); + + err_status|= mysql_change_db(thd, &saved_cur_db_name, TRUE); } m_flags&= ~IS_INVOKED; DBUG_PRINT("info", diff --git a/sql/sql_class.cc b/sql/sql_class.cc index d5159929ecd..9cbb125fa35 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -387,7 +387,6 @@ THD::THD() init_sql_alloc(&main_mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0); stmt_arena= this; thread_stack= 0; - db= 0; catalog= (char*)"std"; // the only catalog we have for now main_security_ctx.init(); security_ctx= &main_security_ctx; @@ -395,7 +394,7 @@ THD::THD() query_start_used= 0; count_cuted_fields= CHECK_FIELD_IGNORE; killed= NOT_KILLED; - db_length= col_access=0; + col_access=0; query_error= thread_specific_used= FALSE; hash_clear(&handler_tables_hash); tmp_table=0; @@ -2040,7 +2039,9 @@ Statement::Statement(LEX *lex_arg, MEM_ROOT *mem_root_arg, lex(lex_arg), query(0), query_length(0), - cursor(0) + cursor(0), + db(NULL), + db_length(0) { name.str= NULL; } diff --git a/sql/sql_class.h b/sql/sql_class.h index abed643f822..80f73945465 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -593,6 +593,22 @@ public: uint32 query_length; // current query length Server_side_cursor *cursor; + /** + Name of the current (default) database. + + If there is the current (default) database, "db" contains its name. If + there is no current (default) database, "db" is NULL and "db_length" is + 0. In other words, "db", "db_length" must either be NULL, or contain a + valid database name. + + @note this attribute is set and alloced by the slave SQL thread (for + the THD of that thread); that thread is (and must remain, for now) the + only responsible for freeing this member. + */ + + char *db; + uint db_length; + public: /* This constructor is called for backup statements */ @@ -1024,18 +1040,21 @@ public: */ char *thread_stack; + /** + Currently selected catalog. + */ + char *catalog; + /* - db - currently selected database - catalog - currently selected catalog - WARNING: some members of THD (currently 'db', 'catalog' and 'query') are - set and alloced by the slave SQL thread (for the THD of that thread); that - thread is (and must remain, for now) the only responsible for freeing these - 3 members. If you add members here, and you add code to set them in - replication, don't forget to free_them_and_set_them_to_0 in replication - properly. For details see the 'err:' label of the handle_slave_sql() - in sql/slave.cc. - */ - char *db, *catalog; + WARNING: some members of THD (currently 'Statement::db', + 'catalog' and 'query') are set and alloced by the slave SQL thread + (for the THD of that thread); that thread is (and must remain, for now) + the only responsible for freeing these 3 members. If you add members + here, and you add code to set them in replication, don't forget to + free_them_and_set_them_to_0 in replication properly. For details see + the 'err:' label of the handle_slave_sql() in sql/slave.cc. + */ + Security_context main_security_ctx; Security_context *security_ctx; @@ -1390,7 +1409,6 @@ public: uint tmp_table, global_read_lock; uint server_status,open_options; enum enum_thread_type system_thread; - uint db_length; uint select_number; //number of select (used for EXPLAIN) /* variables.transaction_isolation is reset to this after each commit */ enum_tx_isolation session_tx_isolation; @@ -1814,11 +1832,10 @@ public: no current database selected (in addition to the error message set by malloc). - @note This operation just sets {thd->db, thd->db_length}. Switching the - current database usually involves other actions, like switching other - database attributes including security context. In the future, this - operation will be made private and more convenient interface will be - provided. + @note This operation just sets {db, db_length}. Switching the current + database usually involves other actions, like switching other database + attributes including security context. In the future, this operation + will be made private and more convenient interface will be provided. @return Operation status @retval FALSE Success @@ -1844,11 +1861,10 @@ public: @param new_db a pointer to the new database name. @param new_db_len length of the new database name. - @note This operation just sets {thd->db, thd->db_length}. Switching the - current database usually involves other actions, like switching other - database attributes including security context. In the future, this - operation will be made private and more convenient interface will be - provided. + @note This operation just sets {db, db_length}. Switching the current + database usually involves other actions, like switching other database + attributes including security context. In the future, this operation + will be made private and more convenient interface will be provided. */ void reset_db(char *new_db, size_t new_db_len) { diff --git a/sql/sql_db.cc b/sql/sql_db.cc index 207c48b45f0..228c56afb13 100644 --- a/sql/sql_db.cc +++ b/sql/sql_db.cc @@ -1350,8 +1350,67 @@ static void mysql_change_db_impl(THD *thd, } + /** - @brief Change the current database and its attributes. + Backup the current database name before switch. + + @param[in] thd thread handle + @param[in, out] saved_db_name IN: "str" points to a buffer where to store + the old database name, "length" contains the + buffer size + OUT: if the current (default) database is + not NULL, its name is copied to the + buffer pointed at by "str" + and "length" is updated accordingly. + Otherwise "str" is set to NULL and + "length" is set to 0. +*/ + +static void backup_current_db_name(THD *thd, + LEX_STRING *saved_db_name) +{ + if (!thd->db) + { + /* No current (default) database selected. */ + + saved_db_name->str= NULL; + saved_db_name->length= 0; + } + else + { + strmake(saved_db_name->str, thd->db, saved_db_name->length); + saved_db_name->length= thd->db_length; + } +} + + +/** + Return TRUE if db1_name is equal to db2_name, FALSE otherwise. + + The function allows to compare database names according to the MySQL + rules. The database names db1 and db2 are equal if: + - db1 is NULL and db2 is NULL; + or + - db1 is not-NULL, db2 is not-NULL, db1 is equal (ignoring case) to + db2 in system character set (UTF8). +*/ + +static inline bool +cmp_db_names(const char *db1_name, + const char *db2_name) +{ + return + /* db1 is NULL and db2 is NULL */ + !db1_name && !db2_name || + + /* db1 is not-NULL, db2 is not-NULL, db1 == db2. */ + db1_name && db2_name && + my_strcasecmp(system_charset_info, db1_name, db2_name) == 0; +} + + +/** + @brief Change the current database and its attributes unconditionally. @param thd thread handle @param new_db_name database name @@ -1568,6 +1627,43 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) } +/** + Change the current database and its attributes if needed. + + @param thd thread handle + @param new_db_name database name + @param[in, out] saved_db_name IN: "str" points to a buffer where to store + the old database name, "length" contains the + buffer size + OUT: if the current (default) database is + not NULL, its name is copied to the + buffer pointed at by "str" + and "length" is updated accordingly. + Otherwise "str" is set to NULL and + "length" is set to 0. + @param force_switch @see mysql_change_db() + @param[out] cur_db_changed out-flag to indicate whether the current + database has been changed (valid only if + the function suceeded) +*/ + +bool mysql_opt_change_db(THD *thd, + const LEX_STRING *new_db_name, + LEX_STRING *saved_db_name, + bool force_switch, + bool *cur_db_changed) +{ + *cur_db_changed= !cmp_db_names(thd->db, new_db_name->str); + + if (!*cur_db_changed) + return FALSE; + + backup_current_db_name(thd, saved_db_name); + + return mysql_change_db(thd, new_db_name, force_switch); +} + + static int lock_databases(THD *thd, const char *db1, uint length1, const char *db2, uint length2) diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index 6b60f89b8e3..9337a2aa329 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -2868,6 +2868,19 @@ bool Prepared_statement::prepare(const char *packet, uint packet_len) init_param_array(this); lex->set_trg_event_type_for_tables(); + /* Remember the current database. */ + + if (thd->db && thd->db_length) + { + db= this->strmake(thd->db, thd->db_length); + db_length= thd->db_length; + } + else + { + db= NULL; + db_length= 0; + } + /* While doing context analysis of the query (in check_prepared_statement) we allocate a lot of additional memory: for open tables, JOINs, derived @@ -2974,6 +2987,13 @@ bool Prepared_statement::execute(String *expanded_query, bool open_cursor) Query_arena *old_stmt_arena; bool error= TRUE; + char saved_cur_db_name_buf[NAME_LEN+1]; + LEX_STRING saved_cur_db_name= + { saved_cur_db_name_buf, sizeof(saved_cur_db_name_buf) }; + bool cur_db_changed; + + LEX_STRING stmt_db_name= { db, db_length }; + status_var_increment(thd->status_var.com_stmt_execute); /* Check if we got an error when sending long data */ @@ -3022,6 +3042,21 @@ bool Prepared_statement::execute(String *expanded_query, bool open_cursor) */ thd->set_n_backup_statement(this, &stmt_backup); + + /* + Change the current database (if needed). + + Force switching, because the database of the prepared statement may be + NULL (prepared statements can be created while no current database + selected). + */ + + if (mysql_opt_change_db(thd, &stmt_db_name, &saved_cur_db_name, TRUE, + &cur_db_changed)) + goto error; + + /* Allocate query. */ + if (expanded_query->length() && alloc_query(thd, (char*) expanded_query->ptr(), expanded_query->length()+1)) @@ -3050,6 +3085,8 @@ bool Prepared_statement::execute(String *expanded_query, bool open_cursor) thd->protocol= protocol; /* activate stmt protocol */ + /* Go! */ + if (open_cursor) error= mysql_open_cursor(thd, (uint) ALWAYS_MATERIALIZED_CURSOR, &result, &cursor); @@ -3068,6 +3105,17 @@ bool Prepared_statement::execute(String *expanded_query, bool open_cursor) } } + /* + Restore the current database (if changed). + + Force switching back to the saved current database (if changed), + because it may be NULL. In this case, mysql_change_db() would generate + an error. + */ + + if (cur_db_changed) + mysql_change_db(thd, &saved_cur_db_name, TRUE); + thd->protocol= &thd->protocol_text; /* use normal protocol */ /* Assert that if an error, no cursor is open */ From 136a5412e927b6e60e5fac51fa0f4facc364e826 Mon Sep 17 00:00:00 2001 From: "anozdrin/alik@ibm.opbmk" <> Date: Sat, 1 Sep 2007 00:12:00 +0400 Subject: [PATCH 30/35] Eliminate compile warnings. --- sql/sp.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sql/sp.cc b/sql/sp.cc index abd0f618d54..6032688f7f1 100644 --- a/sql/sp.cc +++ b/sql/sp.cc @@ -576,6 +576,7 @@ db_load_routine(THD *thd, int type, sp_name *name, sp_head **sphp, if (mysql_opt_change_db(thd, &name->m_db, &saved_cur_db_name, TRUE, &cur_db_changed)) { + ret= SP_INTERNAL_ERROR; goto end; } @@ -597,7 +598,7 @@ db_load_routine(THD *thd, int type, sp_name *name, sp_head **sphp, if (cur_db_changed && mysql_change_db(thd, &saved_cur_db_name, TRUE)) { delete newlex.sphead; - ret= -1; + ret= SP_INTERNAL_ERROR; goto end; } From 143d8e460e1b307f7cd7a87fe585ae519045bb9d Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Fri, 31 Aug 2007 15:51:57 -0600 Subject: [PATCH 31/35] Fixed timing issues in events.test --- mysql-test/t/events.test | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mysql-test/t/events.test b/mysql-test/t/events.test index 365318e1db9..20134765609 100644 --- a/mysql-test/t/events.test +++ b/mysql-test/t/events.test @@ -453,7 +453,8 @@ create event закачка on schedule every 10 hour do select get_lock("test_l --echo "Should have only 2 processes: the scheduler and the locked event" let $wait_condition= select count(*) = 2 from information_schema.processlist - where (info like "select get_lock%" OR user='event_scheduler'); + where ( (state like 'User lock%' AND info like 'select get_lock%') + OR (command='Daemon' AND user='event_scheduler')); --source include/wait_condition.inc select /*2*/ user, host, db, command, state, info @@ -464,6 +465,13 @@ select /*2*/ user, host, db, command, state, info select release_lock("test_lock2"); drop event закачка; +# Wait for release_lock("test_lock2") to complete, +# to avoid polluting the next test information_schema.processlist +let $wait_condition= select count(*) = 0 from information_schema.processlist + where (state like 'User lock%' AND info like 'select get_lock%'); +--source include/wait_condition.inc + + ## ## 1. get a lock ## 2. create an event @@ -478,7 +486,8 @@ create event закачка21 on schedule every 10 hour do select get_lock("test --echo "Should have only 2 processes: the scheduler and the locked event" let $wait_condition= select count(*) = 2 from information_schema.processlist - where (info like "select get_lock%" OR user='event_scheduler'); + where ( (state like 'User lock%' AND info like 'select get_lock%') + OR (command='Daemon' AND user='event_scheduler')); --source include/wait_condition.inc select /*3*/ user, host, db, command, state, info From f5ecb35e6ce310ffedf5f06fbf1df77e81c2d027 Mon Sep 17 00:00:00 2001 From: "thek@adventure.(none)" <> Date: Mon, 3 Sep 2007 10:47:24 +0200 Subject: [PATCH 32/35] Bug #21074 Large query_cache freezes mysql server sporadically under heavy load Invaldating a subset of a sufficiently large query cache can take a long time. During this time the server is efficiently frozen and no other operation can be executed. This patch addresses this problem by setting a time limit on how long time a dictionary access request can take before giving up on the attempt. This patch does not work for query cache invalidations issued by DROP, ALTER or RENAME TABLE operations. --- sql/sql_cache.cc | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc index 33d658ce6a1..cb00b98ccf8 100644 --- a/sql/sql_cache.cc +++ b/sql/sql_cache.cc @@ -1023,6 +1023,13 @@ Query_cache::send_result_to_client(THD *thd, char *sql, uint query_length) Query_cache_block_table *block_table, *block_table_end; ulong tot_length; Query_cache_query_flags flags; + const uint spin_treshold= 50000; + const double lock_time_treshold= 0.1; /* Time in seconds */ + uint spin_count= 0; + int lock_status= 0; + ulong new_time= 0; + ulong stop_time= 0; + DBUG_ENTER("Query_cache::send_result_to_client"); /* @@ -1069,7 +1076,29 @@ Query_cache::send_result_to_client(THD *thd, char *sql, uint query_length) } } - STRUCT_LOCK(&structure_guard_mutex); + stop_time= my_clock()+(ulong)lock_time_treshold*CLOCKS_PER_SEC; + while ((lock_status= pthread_mutex_trylock(&structure_guard_mutex)) == EBUSY + && spin_count < spin_treshold + && new_time < stop_time) + { + spin_count++; + if (spin_count%5) + new_time= my_clock(); + pthread_yield(); + } + + if (lock_status != 0) + { + /* + Query cache is too busy doing something else. + Fall back on ordinary statement execution. We also mark this + query as unsafe to cache because otherwise this thread will + still be halted when the result set is stored to the cache. + */ + thd->lex->safe_to_cache_query= FALSE; + goto err; + } + if (query_cache_size == 0 || flush_in_progress) { DBUG_PRINT("qcache", ("query cache disabled")); From c7de9652158d12fa26dc295a772f962d30b9ff8f Mon Sep 17 00:00:00 2001 From: "anozdrin/alik@station." <> Date: Mon, 3 Sep 2007 15:12:28 +0400 Subject: [PATCH 33/35] Make mysql compilable on gcc-4.2.1. c++config.h now has the following code: // For example, is known to #define min and max as macros... #undef min #undef max So, our defines in my_global.h are undefined when header is included. Move definitions of min()/max() to the end of my_global.h. --- include/my_global.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/my_global.h b/include/my_global.h index 8b6cdef8daa..12129523939 100644 --- a/include/my_global.h +++ b/include/my_global.h @@ -562,12 +562,6 @@ int __void__; #define PURIFY_OR_LINT_INIT(var) #endif -/* Define some useful general macros */ -#if !defined(max) -#define max(a, b) ((a) > (b) ? (a) : (b)) -#define min(a, b) ((a) < (b) ? (a) : (b)) -#endif - #if !defined(HAVE_UINT) #undef HAVE_UINT #define HAVE_UINT @@ -1508,4 +1502,10 @@ inline void operator delete[](void*, void*) { /* Do nothing */ } /* Length of decimal number represented by INT64. */ #define MY_INT64_NUM_DECIMAL_DIGITS 21 +/* Define some useful general macros (should be done after all headers). */ +#if !defined(max) +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + #endif /* my_global_h */ From c39d032da2ba8144683385697980af7a68d0d3ca Mon Sep 17 00:00:00 2001 From: "anozdrin/alik@station." <> Date: Mon, 3 Sep 2007 15:13:34 +0400 Subject: [PATCH 34/35] Rewrite test case for BUG 25843 to avoid SHOW BINLOG EVENTS statement. --- mysql-test/suite/rpl/r/rpl_ps.result | 39 ++++++++++++++-------- mysql-test/suite/rpl/t/rpl_ps.test | 49 +++++++++++++++++++++------- 2 files changed, 63 insertions(+), 25 deletions(-) diff --git a/mysql-test/suite/rpl/r/rpl_ps.result b/mysql-test/suite/rpl/r/rpl_ps.result index dd3ed165226..faba43ae934 100644 --- a/mysql-test/suite/rpl/r/rpl_ps.result +++ b/mysql-test/suite/rpl/r/rpl_ps.result @@ -34,17 +34,18 @@ stop slave; # ######################################################################## -# -# Check that binlog is filled properly. -# +# Connection: slave + + +START SLAVE; + +# Connection: master + CREATE DATABASE mysqltest1; -CREATE TABLE t1(c INT); +CREATE TABLE t1(db_name CHAR(32), db_col_name CHAR(32)); -RESET MASTER; -RESET SLAVE; - -PREPARE stmt_d_1 FROM 'INSERT INTO t1 VALUES(1)'; +PREPARE stmt_d_1 FROM 'INSERT INTO t1 VALUES(DATABASE(), @@collation_database)'; EXECUTE stmt_d_1; @@ -52,18 +53,28 @@ use mysqltest1; EXECUTE stmt_d_1; -FLUSH LOGS; -SHOW BINLOG EVENTS FROM 106; -Log_name Pos Event_type Server_id End_log_pos Info -slave-bin.000001 106 Query 2 193 use `test`; INSERT INTO t1 VALUES(1) -slave-bin.000001 193 Query 2 280 use `test`; INSERT INTO t1 VALUES(1) -slave-bin.000001 280 Rotate 2 323 slave-bin.000002;pos=4 +# Connection: slave + + +SELECT * FROM t1; +db_name db_col_name +test latin1_swedish_ci +test latin1_swedish_ci + +# Connection: master + DROP DATABASE mysqltest1; use test; + +# Connection: slave + + +STOP SLAVE; + ######################################################################## reset master; reset slave; diff --git a/mysql-test/suite/rpl/t/rpl_ps.test b/mysql-test/suite/rpl/t/rpl_ps.test index 0c8a499c9fe..27f1ac3348d 100644 --- a/mysql-test/suite/rpl/t/rpl_ps.test +++ b/mysql-test/suite/rpl/t/rpl_ps.test @@ -73,20 +73,24 @@ stop slave; ############################################################################### --echo ---echo # ---echo # Check that binlog is filled properly. ---echo # +--echo # Connection: slave +--echo +--connection slave + +--echo +START SLAVE; + +--echo +--echo # Connection: master +--echo +--connection master --echo CREATE DATABASE mysqltest1; -CREATE TABLE t1(c INT); +CREATE TABLE t1(db_name CHAR(32), db_col_name CHAR(32)); --echo -RESET MASTER; -RESET SLAVE; - ---echo -PREPARE stmt_d_1 FROM 'INSERT INTO t1 VALUES(1)'; +PREPARE stmt_d_1 FROM 'INSERT INTO t1 VALUES(DATABASE(), @@collation_database)'; --echo EXECUTE stmt_d_1; @@ -98,10 +102,21 @@ use mysqltest1; EXECUTE stmt_d_1; --echo -FLUSH LOGS; +--save_master_pos --echo -SHOW BINLOG EVENTS FROM 106; +--echo # Connection: slave +--echo +--connection slave +--sync_with_master + +--echo +SELECT * FROM t1; + +--echo +--echo # Connection: master +--echo +--connection master --echo DROP DATABASE mysqltest1; @@ -109,6 +124,18 @@ DROP DATABASE mysqltest1; --echo use test; +--echo +--save_master_pos + +--echo +--echo # Connection: slave +--echo +--connection slave +--sync_with_master + +--echo +STOP SLAVE; + --echo --echo ######################################################################## From a8b9bd9f678d66daf2454b426d9d4bc9d9b8c845 Mon Sep 17 00:00:00 2001 From: "thek@adventure.(none)" <> Date: Mon, 3 Sep 2007 13:42:32 +0200 Subject: [PATCH 35/35] - Fix cross compatibility issues by exchanging pthread_yield with my_sleep(0) --- sql/sql_cache.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc index cb00b98ccf8..97e37c870e2 100644 --- a/sql/sql_cache.cc +++ b/sql/sql_cache.cc @@ -1084,7 +1084,7 @@ Query_cache::send_result_to_client(THD *thd, char *sql, uint query_length) spin_count++; if (spin_count%5) new_time= my_clock(); - pthread_yield(); + my_sleep(0); } if (lock_status != 0)