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     with_sum_func= with_sum_func || item->with_sum_func;
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 
cleanup()91 void Item_row::cleanup()
92 {
93   DBUG_ENTER("Item_row::cleanup");
94 
95   Item::cleanup();
96   /* Reset to the original values */
97   used_tables_and_const_cache_init();
98   with_null= 0;
99 
100   DBUG_VOID_RETURN;
101 }
102 
103 
split_sum_func(THD * thd,Ref_ptr_array ref_pointer_array,List<Item> & fields,uint flags)104 void Item_row::split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
105                               List<Item> &fields, uint flags)
106 {
107   Item **arg, **arg_end;
108   for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
109     (*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg,
110                             flags | SPLIT_SUM_SKIP_REGISTERED);
111 }
112 
113 
fix_after_pullout(st_select_lex * new_parent,Item ** ref,bool merge)114 void Item_row::fix_after_pullout(st_select_lex *new_parent, Item **ref,
115                                  bool merge)
116 {
117   used_tables_and_const_cache_init();
118   not_null_tables_cache= 0;
119   for (uint i= 0; i < arg_count; i++)
120   {
121     args[i]->fix_after_pullout(new_parent, &args[i], merge);
122     used_tables_and_const_cache_join(args[i]);
123     not_null_tables_cache|= args[i]->not_null_tables();
124   }
125 }
126 
127 
check_cols(uint c)128 bool Item_row::check_cols(uint c)
129 {
130   if (c != arg_count)
131   {
132     my_error(ER_OPERAND_COLUMNS, MYF(0), c);
133     return 1;
134   }
135   return 0;
136 }
137 
print(String * str,enum_query_type query_type)138 void Item_row::print(String *str, enum_query_type query_type)
139 {
140   str->append('(');
141   for (uint i= 0; i < arg_count; i++)
142   {
143     if (i)
144       str->append(',');
145     args[i]->print(str, query_type);
146   }
147   str->append(')');
148 }
149 
150 
transform(THD * thd,Item_transformer transformer,uchar * arg)151 Item *Item_row::transform(THD *thd, Item_transformer transformer, uchar *arg)
152 {
153   DBUG_ASSERT(!thd->stmt_arena->is_stmt_prepare());
154 
155   if (transform_args(thd, transformer, arg))
156     return 0;
157   return (this->*transformer)(thd, arg);
158 }
159 
bring_value()160 void Item_row::bring_value()
161 {
162   for (uint i= 0; i < arg_count; i++)
163     args[i]->bring_value();
164 }
165 
166 
build_clone(THD * thd)167 Item* Item_row::build_clone(THD *thd)
168 {
169   Item **copy_args= static_cast<Item**>
170     (alloc_root(thd->mem_root, sizeof(Item*) * arg_count));
171   if (unlikely(!copy_args))
172     return 0;
173   for (uint i= 0; i < arg_count; i++)
174   {
175     Item *arg_clone= args[i]->build_clone(thd);
176     if (!arg_clone)
177       return 0;
178     copy_args[i]= arg_clone;
179   }
180   Item_row *copy= (Item_row *) get_copy(thd);
181   if (unlikely(!copy))
182     return 0;
183   copy->args= copy_args;
184   return copy;
185 }
186