11/*
2- Copyright (c) 2000, 2015 , Oracle and/or its affiliates. All rights reserved.
2+ Copyright (c) 2000, 2016 , Oracle and/or its affiliates. All rights reserved.
33
44 This program is free software; you can redistribute it and/or modify
55 it under the terms of the GNU General Public License as published by
@@ -2876,26 +2876,46 @@ static void print_join(THD *thd,
28762876 /* List is reversed => we should reverse it before using */
28772877 List_iterator_fast<TABLE_LIST> ti (*tables);
28782878 TABLE_LIST **table;
2879- uint non_const_tables= 0 ;
2879+
2880+ /*
2881+ If the QT_NO_DATA_EXPANSION flag is specified, we print the
2882+ original table list, including constant tables that have been
2883+ optimized away, as the constant tables may be referenced in the
2884+ expression printed by Item_field::print() when this flag is given.
2885+ Otherwise, only non-const tables are printed.
2886+
2887+ Example:
2888+
2889+ Original SQL:
2890+ select * from (select 1) t
2891+
2892+ Printed without QT_NO_DATA_EXPANSION:
2893+ select '1' AS `1` from dual
2894+
2895+ Printed with QT_NO_DATA_EXPANSION:
2896+ select `t`.`1` from (select 1 AS `1`) `t`
2897+ */
2898+ const bool print_const_tables= (query_type & QT_NO_DATA_EXPANSION);
2899+ size_t tables_to_print= 0 ;
28802900
28812901 for (TABLE_LIST *t= ti++; t ; t= ti++)
2882- if (!t->optimized_away )
2883- non_const_tables ++;
2884- if (!non_const_tables )
2902+ if (print_const_tables || !t->optimized_away )
2903+ tables_to_print ++;
2904+ if (tables_to_print == 0 )
28852905 {
28862906 str->append (STRING_WITH_LEN (" dual" ));
28872907 return ; // all tables were optimized away
28882908 }
28892909 ti.rewind ();
28902910
2891- if (!(table= ( TABLE_LIST **) thd->alloc (sizeof (TABLE_LIST*) *
2892- non_const_tables )))
2911+ if (!(table= static_cast < TABLE_LIST **>( thd->alloc (sizeof (TABLE_LIST*) *
2912+ tables_to_print) )))
28932913 return ; // out of memory
28942914
2895- TABLE_LIST *tmp, **t= table + (non_const_tables - 1 );
2915+ TABLE_LIST *tmp, **t= table + (tables_to_print - 1 );
28962916 while ((tmp= ti++))
28972917 {
2898- if (tmp->optimized_away )
2918+ if (tmp->optimized_away && !print_const_tables )
28992919 continue ;
29002920 *t--= tmp;
29012921 }
@@ -2907,7 +2927,7 @@ static void print_join(THD *thd,
29072927 */
29082928 if ((*table)->sj_cond ())
29092929 {
2910- TABLE_LIST **end= table + non_const_tables ;
2930+ TABLE_LIST **end= table + tables_to_print ;
29112931 for (TABLE_LIST **t2= table; t2!=end; t2++)
29122932 {
29132933 if (!(*t2)->sj_cond ())
@@ -2919,8 +2939,8 @@ static void print_join(THD *thd,
29192939 }
29202940 }
29212941 }
2922- DBUG_ASSERT (non_const_tables >= 1 );
2923- print_table_array (thd, str, table, table + non_const_tables , query_type);
2942+ DBUG_ASSERT (tables_to_print >= 1 );
2943+ print_table_array (thd, str, table, table + tables_to_print , query_type);
29242944}
29252945
29262946
0 commit comments