1 /*
2    Copyright (c) 2002, 2011, Oracle and/or its affiliates.
3    Copyright (c) 2011, 2020, MariaDB Corporation.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; version 2 of the License.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
17 
18 #include "mariadb.h"
19 #include "sql_priv.h"
20 /*
21   It is necessary to include set_var.h instead of item.h because there
22   are dependencies on include order for set_var.h and item.h. This
23   will be resolved later.
24 */
25 #include "sql_class.h"                          // THD, set_var.h: THD
26 #include "set_var.h"
27 
illegal_method_call(const char * method)28 void Item_row::illegal_method_call(const char *method)
29 {
30   DBUG_ENTER("Item_row::illegal_method_call");
31   DBUG_PRINT("error", ("!!! %s method was called for row item", method));
32   DBUG_ASSERT(0);
33   my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
34   DBUG_VOID_RETURN;
35 }
36 
fix_fields(THD * thd,Item ** ref)37 bool Item_row::fix_fields(THD *thd, Item **ref)
38 {
39   DBUG_ASSERT(fixed == 0);
40   null_value= 0;
41   maybe_null= 0;
42   Item **arg, **arg_end;
43   for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
44   {
45     if ((*arg)->fix_fields_if_needed(thd, arg))
46       return TRUE;
47     // we can't assign 'item' before, because fix_fields() can change arg
48     Item *item= *arg;
49     used_tables_cache |= item->used_tables();
50     const_item_cache&= item->const_item() && !with_null;
51     not_null_tables_cache|= item->not_null_tables();
52 
53     if (const_item_cache)
54     {
55       if (item->cols() > 1)
56 	with_null|= item->null_inside();
57       else
58       {
59 	if (item->is_null())
60           with_null|= 1;
61       }
62     }
63     maybe_null|= item->maybe_null;
64     join_with_sum_func(item);
65     with_window_func = with_window_func || item->with_window_func;
66     with_field= with_field || item->with_field;
67     m_with_subquery|= item->with_subquery();
68     with_param|= item->with_param;
69   }
70   fixed= 1;
71   return FALSE;
72 }
73 
74 
75 bool
eval_not_null_tables(void * opt_arg)76 Item_row::eval_not_null_tables(void *opt_arg)
77 {
78   Item **arg,**arg_end;
79   not_null_tables_cache= 0;
80   if (arg_count)
81   {
82     for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
83     {
84       not_null_tables_cache|= (*arg)->not_null_tables();
85     }
86   }
87   return FALSE;
88 }
89 
90 
91 bool
find_not_null_fields(table_map allowed)92 Item_row::find_not_null_fields(table_map allowed)
93 {
94   if (~allowed & used_tables())
95     return false;
96 
97   Item **arg,**arg_end;
98   if (arg_count)
99   {
100     for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
101     {
102       if (!(*arg)->find_not_null_fields(allowed))
103         continue;
104     }
105   }
106   return false;
107 }
108 
109 
cleanup()110 void Item_row::cleanup()
111 {
112   DBUG_ENTER("Item_row::cleanup");
113 
114   Item_fixed_hybrid::cleanup();
115   /* Reset to the original values */
116   used_tables_and_const_cache_init();
117   with_null= 0;
118 
119   DBUG_VOID_RETURN;
120 }
121 
122 
split_sum_func(THD * thd,Ref_ptr_array ref_pointer_array,List<Item> & fields,uint flags)123 void Item_row::split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
124                               List<Item> &fields, uint flags)
125 {
126   Item **arg, **arg_end;
127   for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
128     (*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg,
129                             flags | SPLIT_SUM_SKIP_REGISTERED);
130 }
131 
132 
fix_after_pullout(st_select_lex * new_parent,Item ** ref,bool merge)133 void Item_row::fix_after_pullout(st_select_lex *new_parent, Item **ref,
134                                  bool merge)
135 {
136   used_tables_and_const_cache_init();
137   not_null_tables_cache= 0;
138   for (uint i= 0; i < arg_count; i++)
139   {
140     args[i]->fix_after_pullout(new_parent, &args[i], merge);
141     used_tables_and_const_cache_join(args[i]);
142     not_null_tables_cache|= args[i]->not_null_tables();
143   }
144 }
145 
146 
check_cols(uint c)147 bool Item_row::check_cols(uint c)
148 {
149   if (c != arg_count)
150   {
151     my_error(ER_OPERAND_COLUMNS, MYF(0), c);
152     return 1;
153   }
154   return 0;
155 }
156 
print(String * str,enum_query_type query_type)157 void Item_row::print(String *str, enum_query_type query_type)
158 {
159   str->append('(');
160   for (uint i= 0; i < arg_count; i++)
161   {
162     if (i)
163       str->append(',');
164     args[i]->print(str, query_type);
165   }
166   str->append(')');
167 }
168 
169 
transform(THD * thd,Item_transformer transformer,uchar * arg)170 Item *Item_row::transform(THD *thd, Item_transformer transformer, uchar *arg)
171 {
172   DBUG_ASSERT(!thd->stmt_arena->is_stmt_prepare());
173 
174   if (transform_args(thd, transformer, arg))
175     return 0;
176   return (this->*transformer)(thd, arg);
177 }
178 
bring_value()179 void Item_row::bring_value()
180 {
181   for (uint i= 0; i < arg_count; i++)
182     args[i]->bring_value();
183 }
184 
185 
build_clone(THD * thd)186 Item* Item_row::build_clone(THD *thd)
187 {
188   Item **copy_args= static_cast<Item**>
189     (alloc_root(thd->mem_root, sizeof(Item*) * arg_count));
190   if (unlikely(!copy_args))
191     return 0;
192   for (uint i= 0; i < arg_count; i++)
193   {
194     Item *arg_clone= args[i]->build_clone(thd);
195     if (!arg_clone)
196       return 0;
197     copy_args[i]= arg_clone;
198   }
199   Item_row *copy= (Item_row *) get_copy(thd);
200   if (unlikely(!copy))
201     return 0;
202   copy->args= copy_args;
203   return copy;
204 }
205