diff --git a/mysql-test/r/join_nested.result b/mysql-test/r/join_nested.result
index dfcfa35d31d1da7a2c15b566cb82a98409cec4b6..0747418111b49ff993eeba6c0e9678e50ffa41bc 100644
--- a/mysql-test/r/join_nested.result
+++ b/mysql-test/r/join_nested.result
@@ -1504,3 +1504,61 @@ id	type	cid	id	pid	pid	type
 1	A	NULL	NULL	NULL	NULL	NULL
 DROP VIEW v1;
 DROP TABLE t1,t2,t3;
+CREATE TABLE t1 (id1 int PRIMARY KEY, id2 int);
+CREATE TABLE t2 (id1 int PRIMARY KEY, id2 int);
+CREATE TABLE t3 (id1 int PRIMARY KEY, id2 int);
+CREATE TABLE t4 (id1 int PRIMARY KEY, id2 int);
+CREATE TABLE t5 (id1 int PRIMARY KEY, id2 int);
+SELECT t1.id1 AS id, t5.id1 AS ngroupbynsa
+FROM t1 INNER JOIN t2 ON t2.id2 = t1.id1
+LEFT OUTER JOIN
+(t3 INNER JOIN t4 ON t4.id1 = t3.id2 INNER JOIN t5 ON t4.id2 = t5.id1)
+ON t3.id2 IS NOT NULL
+WHERE t1.id1=2;
+id	ngroupbynsa
+PREPARE stmt FROM
+"SELECT t1.id1 AS id, t5.id1 AS ngroupbynsa
+  FROM t1 INNER JOIN t2 ON t2.id2 = t1.id1
+       LEFT OUTER JOIN
+       (t3 INNER JOIN t4 ON t4.id1 = t3.id2 INNER JOIN t5 ON t4.id2 = t5.id1)
+       ON t3.id2 IS NOT NULL
+    WHERE t1.id1=2";
+EXECUTE stmt;
+id	ngroupbynsa
+EXECUTE stmt;
+id	ngroupbynsa
+EXECUTE stmt;
+id	ngroupbynsa
+EXECUTE stmt;
+id	ngroupbynsa
+INSERT INTO t1 VALUES (1,1), (2,1), (3,2);
+INSERT INTO t2 VALUES (2,1), (3,2), (4,3);
+INSERT INTO t3 VALUES (1,1), (3,2), (2,NULL);
+INSERT INTO t4 VALUES (1,1), (2,1), (3,3);
+INSERT INTO t5 VALUES (1,1), (2,2), (3,3), (4,3);
+EXECUTE stmt;
+id	ngroupbynsa
+2	1
+2	1
+EXECUTE stmt;
+id	ngroupbynsa
+2	1
+2	1
+EXECUTE stmt;
+id	ngroupbynsa
+2	1
+2	1
+EXECUTE stmt;
+id	ngroupbynsa
+2	1
+2	1
+SELECT t1.id1 AS id, t5.id1 AS ngroupbynsa
+FROM t1 INNER JOIN t2 ON t2.id2 = t1.id1
+LEFT OUTER JOIN
+(t3 INNER JOIN t4 ON t4.id1 = t3.id2 INNER JOIN t5 ON t4.id2 = t5.id1)
+ON t3.id2 IS NOT NULL
+WHERE t1.id1=2;
+id	ngroupbynsa
+2	1
+2	1
+DROP TABLE t1,t2,t3,t4,t5;
diff --git a/mysql-test/r/ndb_read_multi_range.result b/mysql-test/r/ndb_read_multi_range.result
index bb9398054ff9d172fff9ca52f4869f31c80e0b51..9941d2b28a35b6972c0e7d1fe0c764af0f4467a5 100644
--- a/mysql-test/r/ndb_read_multi_range.result
+++ b/mysql-test/r/ndb_read_multi_range.result
@@ -263,3 +263,107 @@ a	b	c	d
 10	10	10	10
 drop table t2;
 drop table t1;
+CREATE TABLE t1 (
+a int(11) NOT NULL,
+b int(11) NOT NULL,
+c datetime default NULL,
+PRIMARY KEY  (a),
+KEY idx_bc (b,c)
+) ENGINE=ndbcluster;
+INSERT INTO t1 VALUES 
+(406989,67,'2006-02-23 17:08:46'), (150078,67,'2005-10-26 11:17:45'),
+(406993,67,'2006-02-27 11:20:57'), (245655,67,'2005-12-08 15:59:08'),
+(406994,67,'2006-02-27 11:26:46'), (256,67,NULL),
+(398341,67,'2006-02-20 04:48:44'), (254,67,NULL),(1120,67,NULL),
+(406988,67,'2006-02-23 17:07:22'), (255,67,NULL),
+(398340,67,'2006-02-20 04:38:53'),(406631,67,'2006-02-23 10:49:42'),
+(245653,67,'2005-12-08 15:59:07'),(406992,67,'2006-02-24 16:47:18'),
+(245654,67,'2005-12-08 15:59:08'),(406995,67,'2006-02-28 11:55:00'),
+(127261,67,'2005-10-13 12:17:58'),(406991,67,'2006-02-24 16:42:32'),
+(245652,67,'2005-12-08 15:58:27'),(398545,67,'2006-02-20 04:53:13'),
+(154504,67,'2005-10-28 11:53:01'),(9199,67,NULL),(1,67,'2006-02-23 15:01:35'),
+(223456,67,NULL),(4101,67,NULL),(1133,67,NULL),
+(406990,67,'2006-02-23 18:01:45'),(148815,67,'2005-10-25 15:34:17'),
+(148812,67,'2005-10-25 15:30:01'),(245651,67,'2005-12-08 15:58:27'),
+(154503,67,'2005-10-28 11:52:38');
+create table t11 select * from t1 where b = 67 AND (c IS NULL OR c > NOW()) order by 3 asc;
+create table t12 select * from t1 where b = 67 AND (c IS NULL OR c > NOW()) order by 3 desc;
+create table t21 select * from t1 where b = 67 AND (c IS NULL OR c > '2005-12-08') order by 3 asc;
+create table t22 select * from t1 where b = 67 AND (c IS NULL OR c > '2005-12-08') order by 3 desc;
+select * from t11 order by 1,2,3;
+a	b	c
+254	67	NULL
+255	67	NULL
+256	67	NULL
+1120	67	NULL
+1133	67	NULL
+4101	67	NULL
+9199	67	NULL
+223456	67	NULL
+select * from t12 order by 1,2,3;
+a	b	c
+254	67	NULL
+255	67	NULL
+256	67	NULL
+1120	67	NULL
+1133	67	NULL
+4101	67	NULL
+9199	67	NULL
+223456	67	NULL
+select * from t21 order by 1,2,3;
+a	b	c
+1	67	2006-02-23 15:01:35
+254	67	NULL
+255	67	NULL
+256	67	NULL
+1120	67	NULL
+1133	67	NULL
+4101	67	NULL
+9199	67	NULL
+223456	67	NULL
+245651	67	2005-12-08 15:58:27
+245652	67	2005-12-08 15:58:27
+245653	67	2005-12-08 15:59:07
+245654	67	2005-12-08 15:59:08
+245655	67	2005-12-08 15:59:08
+398340	67	2006-02-20 04:38:53
+398341	67	2006-02-20 04:48:44
+398545	67	2006-02-20 04:53:13
+406631	67	2006-02-23 10:49:42
+406988	67	2006-02-23 17:07:22
+406989	67	2006-02-23 17:08:46
+406990	67	2006-02-23 18:01:45
+406991	67	2006-02-24 16:42:32
+406992	67	2006-02-24 16:47:18
+406993	67	2006-02-27 11:20:57
+406994	67	2006-02-27 11:26:46
+406995	67	2006-02-28 11:55:00
+select * from t22 order by 1,2,3;
+a	b	c
+1	67	2006-02-23 15:01:35
+254	67	NULL
+255	67	NULL
+256	67	NULL
+1120	67	NULL
+1133	67	NULL
+4101	67	NULL
+9199	67	NULL
+223456	67	NULL
+245651	67	2005-12-08 15:58:27
+245652	67	2005-12-08 15:58:27
+245653	67	2005-12-08 15:59:07
+245654	67	2005-12-08 15:59:08
+245655	67	2005-12-08 15:59:08
+398340	67	2006-02-20 04:38:53
+398341	67	2006-02-20 04:48:44
+398545	67	2006-02-20 04:53:13
+406631	67	2006-02-23 10:49:42
+406988	67	2006-02-23 17:07:22
+406989	67	2006-02-23 17:08:46
+406990	67	2006-02-23 18:01:45
+406991	67	2006-02-24 16:42:32
+406992	67	2006-02-24 16:47:18
+406993	67	2006-02-27 11:20:57
+406994	67	2006-02-27 11:26:46
+406995	67	2006-02-28 11:55:00
+DROP TABLE t1, t11, t12, t21, t22;
diff --git a/mysql-test/t/join_nested.test b/mysql-test/t/join_nested.test
index 8adcf05be93355e8854eb0c82f4619c6133ff4de..69886d035bf2b0655369730f247f7179c85013c2 100644
--- a/mysql-test/t/join_nested.test
+++ b/mysql-test/t/join_nested.test
@@ -942,3 +942,55 @@ SELECT * FROM t1 p LEFT JOIN v1 ON p.id=v1.id
 
 DROP VIEW v1;
 DROP TABLE t1,t2,t3;
+
+
+#
+# Test for bug #18279: crash when on conditions are moved out of a nested join
+#                      to the on conditions for the nest
+
+CREATE TABLE t1 (id1 int PRIMARY KEY, id2 int);
+CREATE TABLE t2 (id1 int PRIMARY KEY, id2 int);
+CREATE TABLE t3 (id1 int PRIMARY KEY, id2 int);
+CREATE TABLE t4 (id1 int PRIMARY KEY, id2 int);
+CREATE TABLE t5 (id1 int PRIMARY KEY, id2 int);
+
+SELECT t1.id1 AS id, t5.id1 AS ngroupbynsa
+  FROM t1 INNER JOIN t2 ON t2.id2 = t1.id1
+       LEFT OUTER JOIN
+       (t3 INNER JOIN t4 ON t4.id1 = t3.id2 INNER JOIN t5 ON t4.id2 = t5.id1)
+       ON t3.id2 IS NOT NULL
+    WHERE t1.id1=2;
+
+PREPARE stmt FROM
+"SELECT t1.id1 AS id, t5.id1 AS ngroupbynsa
+  FROM t1 INNER JOIN t2 ON t2.id2 = t1.id1
+       LEFT OUTER JOIN
+       (t3 INNER JOIN t4 ON t4.id1 = t3.id2 INNER JOIN t5 ON t4.id2 = t5.id1)
+       ON t3.id2 IS NOT NULL
+    WHERE t1.id1=2";
+
+EXECUTE stmt; 
+EXECUTE stmt; 
+EXECUTE stmt; 
+EXECUTE stmt;
+
+INSERT INTO t1 VALUES (1,1), (2,1), (3,2);
+INSERT INTO t2 VALUES (2,1), (3,2), (4,3);
+INSERT INTO t3 VALUES (1,1), (3,2), (2,NULL);
+INSERT INTO t4 VALUES (1,1), (2,1), (3,3);
+INSERT INTO t5 VALUES (1,1), (2,2), (3,3), (4,3);
+
+EXECUTE stmt; 
+EXECUTE stmt; 
+EXECUTE stmt; 
+EXECUTE stmt;
+
+SELECT t1.id1 AS id, t5.id1 AS ngroupbynsa
+  FROM t1 INNER JOIN t2 ON t2.id2 = t1.id1
+       LEFT OUTER JOIN
+       (t3 INNER JOIN t4 ON t4.id1 = t3.id2 INNER JOIN t5 ON t4.id2 = t5.id1)
+       ON t3.id2 IS NOT NULL
+    WHERE t1.id1=2; 
+
+DROP TABLE t1,t2,t3,t4,t5;
+
diff --git a/mysql-test/t/ndb_read_multi_range.test b/mysql-test/t/ndb_read_multi_range.test
index b1bf7fe4258c5e895b3143cc7d7806785048488a..855f778903214496a54227a84daccf22de489c89 100644
--- a/mysql-test/t/ndb_read_multi_range.test
+++ b/mysql-test/t/ndb_read_multi_range.test
@@ -200,3 +200,41 @@ select * from t2 order by a;
 drop table t2;
 
 drop table t1;
+
+# bug17729
+
+CREATE TABLE t1 (
+  a int(11) NOT NULL,
+  b int(11) NOT NULL,
+  c datetime default NULL,
+  PRIMARY KEY  (a),
+  KEY idx_bc (b,c)
+) ENGINE=ndbcluster;
+
+INSERT INTO t1 VALUES 
+(406989,67,'2006-02-23 17:08:46'), (150078,67,'2005-10-26 11:17:45'),
+(406993,67,'2006-02-27 11:20:57'), (245655,67,'2005-12-08 15:59:08'),
+(406994,67,'2006-02-27 11:26:46'), (256,67,NULL),
+(398341,67,'2006-02-20 04:48:44'), (254,67,NULL),(1120,67,NULL),
+(406988,67,'2006-02-23 17:07:22'), (255,67,NULL),
+(398340,67,'2006-02-20 04:38:53'),(406631,67,'2006-02-23 10:49:42'),
+(245653,67,'2005-12-08 15:59:07'),(406992,67,'2006-02-24 16:47:18'),
+(245654,67,'2005-12-08 15:59:08'),(406995,67,'2006-02-28 11:55:00'),
+(127261,67,'2005-10-13 12:17:58'),(406991,67,'2006-02-24 16:42:32'),
+(245652,67,'2005-12-08 15:58:27'),(398545,67,'2006-02-20 04:53:13'),
+(154504,67,'2005-10-28 11:53:01'),(9199,67,NULL),(1,67,'2006-02-23 15:01:35'),
+(223456,67,NULL),(4101,67,NULL),(1133,67,NULL),
+(406990,67,'2006-02-23 18:01:45'),(148815,67,'2005-10-25 15:34:17'),
+(148812,67,'2005-10-25 15:30:01'),(245651,67,'2005-12-08 15:58:27'),
+(154503,67,'2005-10-28 11:52:38');
+
+create table t11 select * from t1 where b = 67 AND (c IS NULL OR c > NOW()) order by 3 asc;
+create table t12 select * from t1 where b = 67 AND (c IS NULL OR c > NOW()) order by 3 desc;
+create table t21 select * from t1 where b = 67 AND (c IS NULL OR c > '2005-12-08') order by 3 asc;
+create table t22 select * from t1 where b = 67 AND (c IS NULL OR c > '2005-12-08') order by 3 desc;
+
+select * from t11 order by 1,2,3;
+select * from t12 order by 1,2,3;
+select * from t21 order by 1,2,3;
+select * from t22 order by 1,2,3;
+DROP TABLE t1, t11, t12, t21, t22;
diff --git a/scripts/mysql_config.sh b/scripts/mysql_config.sh
index 4d732485b1dc68f30a95d91535d38ae3842dfdec..84ff518c3811e7a29e27998ae9f99416e90e98db 100644
--- a/scripts/mysql_config.sh
+++ b/scripts/mysql_config.sh
@@ -82,10 +82,15 @@ basedir=`echo $me | sed -e 's;/bin/mysql_config;;'`
 ldata='@localstatedir@'
 execdir='@libexecdir@'
 bindir='@bindir@'
+
+# If installed, search for the compiled in directory first (might be "lib64")
 pkglibdir='@pkglibdir@'
-fix_path pkglibdir lib/mysql lib
+pkglibdir_rel=`echo $pkglibdir | sed -e "s;^$basedir/;;"`
+fix_path pkglibdir $pkglibdir_rel lib/mysql lib
+
 pkgincludedir='@pkgincludedir@'
 fix_path pkgincludedir include/mysql include
+
 version='@VERSION@'
 socket='@MYSQL_UNIX_ADDR@'
 port='@MYSQL_TCP_PORT@'
diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc
index 1a1748671891aa38b2c12117d22a5e6e77806bdf..4ad69f18ffce21b5c9ce848b82dbc78c9271a239 100644
--- a/sql/ha_ndbcluster.cc
+++ b/sql/ha_ndbcluster.cc
@@ -7612,7 +7612,9 @@ ha_ndbcluster::setup_recattr(const NdbRecAttr* curr)
     if ((* value).ptr)
     {
       DBUG_ASSERT(curr != 0);
-      (* value).rec= curr;
+      NdbValue* val= m_value + curr->getColumn()->getColumnNo();
+      DBUG_ASSERT(val->ptr);
+      val->rec= curr;
       curr= curr->next();
     }
   }
diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
index a0cfc5a7a9e140faf474e3ef48d119186b9d4ab9..ced15b3f7285970cc512f08b7eac323428408627 100644
--- a/sql/sql_prepare.cc
+++ b/sql/sql_prepare.cc
@@ -2128,11 +2128,17 @@ void reinit_stmt_before_use(THD *thd, LEX *lex)
     /* Reset is_schema_table_processed value(needed for I_S tables */
     tables->is_schema_table_processed= FALSE;
 
-    if (tables->prep_on_expr)
+    TABLE_LIST *embedded; /* The table at the current level of nesting. */
+    TABLE_LIST *embedding= tables; /* The parent nested table reference. */
+    do
     {
-      tables->on_expr= tables->prep_on_expr->copy_andor_structure(thd);
-      tables->on_expr->cleanup();
+      embedded= embedding;
+      if (embedded->prep_on_expr)
+        embedded->on_expr= embedded->prep_on_expr->copy_andor_structure(thd);
+      embedding= embedded->embedding;
     }
+    while (embedding &&
+           embedding->nested_join->join_list.head() == embedded);
   }
   lex->current_select= &lex->select_lex;
 
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 59c7436d85c37a299f4838f4299140bf6b06ae56..e799dd6ba1f2af872933538db995857dc030e409 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -7517,7 +7517,7 @@ simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top)
       */
       if (table->on_expr)
       {
-        Item *expr= table->prep_on_expr ? table->prep_on_expr : table->on_expr;
+        Item *expr= table->on_expr;
         /* 
            If an on expression E is attached to the table, 
            check all null rejected predicates in this expression.
@@ -7528,7 +7528,9 @@ simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top)
 	*/ 
         expr= simplify_joins(join, &nested_join->join_list,
                              expr, FALSE);
-        table->prep_on_expr= table->on_expr= expr;
+        table->on_expr= expr;
+        if (!table->prep_on_expr)
+          table->prep_on_expr= expr->copy_andor_structure(join->thd);
       }
       nested_join->used_tables= (table_map) 0;
       nested_join->not_null_tables=(table_map) 0;
diff --git a/storage/ndb/tools/Makefile.am b/storage/ndb/tools/Makefile.am
index 77263a92b322d9f4df99a49e4117fcbec59430f4..32da72b00940adfe21df3190fa49dcbe71ede68c 100644
--- a/storage/ndb/tools/Makefile.am
+++ b/storage/ndb/tools/Makefile.am
@@ -1,4 +1,7 @@
 
+bin_SCRIPTS =		ndb_size.pl ndb_error_reporter
+dist_pkgdata_DATA =	ndb_size.tmpl
+
 ndbtools_PROGRAMS = \
   ndb_test_platform \
   ndb_waiter \