From c1402e2d5c606bfb5e35542ff92ba8e53cb797ab Mon Sep 17 00:00:00 2001
From: "monty@donna.mysql.fi" <>
Date: Sat, 10 Mar 2001 17:05:10 +0200
Subject: [PATCH] Fixed bug in MAX() optimizing for BDB tables

---
 .bzrignore                   |  1 +
 Docs/manual.texi             | 18 ++++++++++++++++--
 mysql-test/README            |  5 ++++-
 mysql-test/r/bdb.result      |  2 ++
 mysql-test/r/innobase.result |  2 ++
 mysql-test/t/bdb.test        | 14 ++++++++++++++
 mysql-test/t/innobase.test   | 14 ++++++++++++++
 sql-bench/server-cfg.sh      |  5 +++++
 sql/ha_berkeley.cc           |  7 ++++++-
 sql/ha_innobase.h            | 22 ++++++++++++----------
 sql/handler.h                |  1 +
 sql/opt_sum.cc               |  5 +++++
 12 files changed, 82 insertions(+), 14 deletions(-)

diff --git a/.bzrignore b/.bzrignore
index 9eae1ed254..e9e207dc35 100644
--- a/.bzrignore
+++ b/.bzrignore
@@ -197,3 +197,4 @@ bdb/build_win32/db.h
 bdb/dist/configure
 bdb/dist/tags
 bdb/build_unix/*
+sql/.gdbinit
diff --git a/Docs/manual.texi b/Docs/manual.texi
index 4d9eb645c5..11e7908e23 100644
--- a/Docs/manual.texi
+++ b/Docs/manual.texi
@@ -5679,7 +5679,7 @@ To install the HP-UX tar.gz distribution, you must have a copy of GNU
 
 
 @cindex installing, source distribution
-@cindex source distribtuion, installing
+@cindex source distribution, installing
 @cindex installation overview
 @node Installing source, Installing source tree, Installing binary, Installing
 @section Installing a MySQL Source Distribution
@@ -40246,6 +40246,12 @@ If a test fails totally, you should check the logs file in the
 @item
 If you have compiled @strong{MySQL} with debugging you can try to debug this
 with the @code{--gdb} and @code{--debug} options to @code{mysql-test-run}.
+@xref{Making trace files}.
+
+If you have not compiled @strong{MySQL} for debugging you should probably
+do that.  Just specify the @code{--with-debug} options to @code{configure}!
+@xref{Installing source}.
+
 @end itemize
 
 @page
@@ -42125,6 +42131,8 @@ Fixed bug in bi-directonal replication.
 Fixed bug in @code{BDB} tables when using index on multi-part key where a
 key part may be @code{NULL}.
 @item
+Fixed @code{MAX()} optimization on sub-key for @code{BDB} tables.
+@item
 Fixed problem with 'garbage results' when using @code{BDB} tables and
 @code{BLOB} or @code{TEXT} fields when joining many tables.
 @item
@@ -47038,6 +47046,12 @@ in some cases the @code{PROCEDURE} will not transform the columns.
 @item
 Creation of a table of type @code{MERGE} doesn't check if the underlying
 tables are of compatible types.
+@item
+@strong{MySQL} can't yet handle @code{NaN}, @code{-Inf} and @code{Inf}
+values in double. Using these will cause problems when trying to export
+and import data. We should as a intermediate solution change @code{NaN} to
+@code{NULL} (if possible) and @code{-Inf} and @code{Inf} to the
+Minimum respective maximum possible @code{double} value.
 @end itemize
 
 The following are known bugs in earlier versions of @strong{MySQL}:
@@ -47669,7 +47683,7 @@ send mail to @email{mysql@@lists.mysql.com} and ask for help.  Please use the
 If you can cause the @code{mysqld} server to crash quickly, you can try to
 create a trace file of this:
 
-Start the @code{mysqld} server with a trace log in @file{/tmp/mysql.trace}.
+Start the @code{mysqld} server with a trace log in @file{/tmp/mysqld.trace}.
 The log file will get very @emph{BIG}.
 
 @code{mysqld --debug --log}
diff --git a/mysql-test/README b/mysql-test/README
index 6ad59ea6a8..c5dc3e219d 100644
--- a/mysql-test/README
+++ b/mysql-test/README
@@ -6,7 +6,10 @@ actually have a co-existing MySQL installation - the tests will not
 conflict with it.
 
 All tests must pass. If one or more of them fail on your system, please
-report the details to bugs@lists.mysql.com
+read the following manual section of how to report the problem:
+
+http://www.mysql.com/doc/M/y/MySQL_test_suite.html
+
 
 You can create your own test cases. To create a test case:
 
diff --git a/mysql-test/r/bdb.result b/mysql-test/r/bdb.result
index 42d14e1c34..2e760ae5b7 100644
--- a/mysql-test/r/bdb.result
+++ b/mysql-test/r/bdb.result
@@ -490,3 +490,5 @@ a	2
 a	b
 a	1
 a	2
+MIN(B)	MAX(b)
+1	1
diff --git a/mysql-test/r/innobase.result b/mysql-test/r/innobase.result
index 577bfcbf5b..6c45bfd810 100644
--- a/mysql-test/r/innobase.result
+++ b/mysql-test/r/innobase.result
@@ -443,3 +443,5 @@ i	j
 1	2
 i	j
 1	2
+MIN(B)	MAX(b)
+1	1
diff --git a/mysql-test/t/bdb.test b/mysql-test/t/bdb.test
index 900260217d..564491fc52 100644
--- a/mysql-test/t/bdb.test
+++ b/mysql-test/t/bdb.test
@@ -664,3 +664,17 @@ SELECT * FROM t1 WHERE a='a' AND b=2;
 SELECT * FROM t1 WHERE a='a' AND b in (2);
 SELECT * FROM t1 WHERE a='a' AND b in (1,2);
 drop table t1;
+
+#
+# Test min-max optimization
+#
+
+CREATE TABLE t1 (
+  a int3 unsigned NOT NULL,
+  b int1 unsigned NOT NULL,
+  UNIQUE (a, b)
+) TYPE = BDB;
+ 
+INSERT INTO t1 VALUES (1, 1);
+SELECT MIN(B),MAX(b) FROM t1 WHERE t1.a = 1;
+drop table t1;
diff --git a/mysql-test/t/innobase.test b/mysql-test/t/innobase.test
index bca10751c1..4fa8d07bd5 100644
--- a/mysql-test/t/innobase.test
+++ b/mysql-test/t/innobase.test
@@ -394,3 +394,17 @@ select * from t1 where i=1 and j=2;
 create index ax1 on t1 (i,j);
 select * from t1 where i=1 and j=2;
 drop table t1;
+
+#
+# Test min-max optimization
+#
+
+CREATE TABLE t1 (
+  a int3 unsigned NOT NULL,
+  b int1 unsigned NOT NULL,
+  UNIQUE (a, b)
+) TYPE = innobase;
+ 
+INSERT INTO t1 VALUES (1, 1);
+SELECT MIN(B),MAX(b) FROM t1 WHERE t1.a = 1;
+drop table t1;
diff --git a/sql-bench/server-cfg.sh b/sql-bench/server-cfg.sh
index 8ede6022cf..bfd9d7ce75 100644
--- a/sql-bench/server-cfg.sh
+++ b/sql-bench/server-cfg.sh
@@ -194,6 +194,11 @@ sub new
   {
     $limits{'working_blobs'}	= 0; # HEAP tables can't handle BLOB's
   }
+  if (defined($main::opt_create_options) &&
+      $main::opt_create_options =~ /type=innobase/i)
+  {
+    $limits{'max_text_size'}	= 8000; # Limit in Innobase
+  }
 
   return $self;
 }
diff --git a/sql/ha_berkeley.cc b/sql/ha_berkeley.cc
index 3f6b2629e1..10ff4dcc26 100644
--- a/sql/ha_berkeley.cc
+++ b/sql/ha_berkeley.cc
@@ -1429,7 +1429,12 @@ int ha_berkeley::index_read(byte * buf, const byte * key,
     pack_key(&last_key, active_index, key_buff, key, key_len);
     /* Store for compare */
     memcpy(key_buff2, key_buff, (key_len=last_key.size));
-    key_info->handler.bdb_return_if_eq= -1;
+    /*
+      If HA_READ_AFTER_KEY is set, return next key, else return first
+      matching key.
+    */
+    key_info->handler.bdb_return_if_eq= (find_flag == HA_READ_AFTER_KEY ?
+					 1 : -1);
     error=read_row(cursor->c_get(cursor, &last_key, &row, DB_SET_RANGE),
 		   (char*) buf, active_index, &row, (DBT*) 0, 0);
     key_info->handler.bdb_return_if_eq= 0;
diff --git a/sql/ha_innobase.h b/sql/ha_innobase.h
index 73991be208..7ce22e70dc 100644
--- a/sql/ha_innobase.h
+++ b/sql/ha_innobase.h
@@ -72,16 +72,18 @@ class ha_innobase: public handler
 	/* Init values for the class: */
  public:
   	ha_innobase(TABLE *table): handler(table),
-    		int_option_flag(HA_READ_NEXT | HA_READ_PREV | HA_READ_ORDER |
-		    HA_REC_NOT_IN_SEQ |
-		    HA_KEYPOS_TO_RNDPOS | HA_LASTKEY_ORDER |
-		    HA_HAVE_KEY_READ_ONLY | HA_READ_NOT_EXACT_KEY |
-		    HA_LONGLONG_KEYS | HA_NULL_KEY |
-		    HA_NOT_EXACT_COUNT |
-		    HA_NO_WRITE_DELAYED |
-		    HA_PRIMARY_KEY_IN_READ_INDEX | HA_DROP_BEFORE_CREATE),
-    		last_dup_key((uint) -1),
-    		start_of_scan(0)
+	  int_option_flag(HA_READ_NEXT | HA_READ_PREV | HA_READ_ORDER |
+			  HA_REC_NOT_IN_SEQ |
+			  HA_KEYPOS_TO_RNDPOS | HA_LASTKEY_ORDER |
+			  HA_HAVE_KEY_READ_ONLY | HA_READ_NOT_EXACT_KEY |
+			  HA_LONGLONG_KEYS | HA_NULL_KEY |
+			  HA_NOT_EXACT_COUNT |
+			  HA_NO_WRITE_DELAYED |
+			  HA_PRIMARY_KEY_IN_READ_INDEX |
+			  HA_DROP_BEFORE_CREATE |
+			  HA_NOT_READ_AFTER_KEY),
+	  last_dup_key((uint) -1),
+	  start_of_scan(0)
   	{
   	}
   	~ha_innobase() {}
diff --git a/sql/handler.h b/sql/handler.h
index 8cecd1fe17..25c5d834be 100644
--- a/sql/handler.h
+++ b/sql/handler.h
@@ -70,6 +70,7 @@
 #define HA_NO_WRITE_DELAYED	(HA_NOT_EXACT_COUNT*2)
 #define HA_PRIMARY_KEY_IN_READ_INDEX (HA_NO_WRITE_DELAYED*2)
 #define HA_DROP_BEFORE_CREATE	(HA_PRIMARY_KEY_IN_READ_INDEX*2)
+#define HA_NOT_READ_AFTER_KEY	(HA_DROP_BEFORE_CREATE*2)
 
 	/* Parameters for open() (in register form->filestat) */
 	/* HA_GET_INFO does a implicit HA_ABORT_IF_LOCKED */
diff --git a/sql/opt_sum.cc b/sql/opt_sum.cc
index d56bf68db6..df49d52d54 100644
--- a/sql/opt_sum.cc
+++ b/sql/opt_sum.cc
@@ -141,6 +141,11 @@ int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds)
 	    break;
 	  }
 	  TABLE *table=((Item_field*) expr)->field->table;
+	  if ((table->file->option_flag() & HA_NOT_READ_AFTER_KEY))
+	  {
+	    const_result=0;
+	    break;
+	  }
 	  bool error=table->file->index_init((uint) ref.key);
 
 	  if (!ref.key_length)
-- 
2.30.9