1 /* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16 #include "sql_priv.h"
17 /*
18 It is necessary to include set_var.h instead of item.h because there
19 are dependencies on include order for set_var.h and item.h. This
20 will be resolved later.
21 */
22 #include "sql_class.h" // THD, set_var.h: THD
23 #include "set_var.h"
24
25 /**
26 Row items used for comparing rows and IN operations on rows:
27
28 @verbatim
29 (a, b, c) > (10, 10, 30)
30 (a, b, c) = (select c, d, e, from t1 where x=12)
31 (a, b, c) IN ((1,2,2), (3,4,5), (6,7,8)
32 (a, b, c) IN (select c, d, e, from t1)
33 @endverbatim
34
35 @todo
36 think placing 2-3 component items in item (as it done for function
37 */
38
Item_row(List<Item> & arg)39 Item_row::Item_row(List<Item> &arg):
40 Item(), used_tables_cache(0), not_null_tables_cache(0),
41 const_item_cache(1), with_null(0)
42 {
43
44 //TODO: think placing 2-3 component items in item (as it done for function)
45 if ((arg_count= arg.elements))
46 items= (Item**) sql_alloc(sizeof(Item*)*arg_count);
47 else
48 items= 0;
49 List_iterator<Item> li(arg);
50 uint i= 0;
51 Item *item;
52 while ((item= li++))
53 {
54 items[i]= item;
55 i++;
56 }
57 }
58
illegal_method_call(const char * method)59 void Item_row::illegal_method_call(const char *method)
60 {
61 DBUG_ENTER("Item_row::illegal_method_call");
62 DBUG_PRINT("error", ("!!! %s method was called for row item", method));
63 DBUG_ASSERT(0);
64 my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
65 DBUG_VOID_RETURN;
66 }
67
fix_fields(THD * thd,Item ** ref)68 bool Item_row::fix_fields(THD *thd, Item **ref)
69 {
70 DBUG_ASSERT(fixed == 0);
71 null_value= 0;
72 maybe_null= 0;
73 Item **arg, **arg_end;
74 for (arg= items, arg_end= items+arg_count; arg != arg_end ; arg++)
75 {
76 if ((*arg)->fix_fields(thd, arg))
77 return TRUE;
78 // we can't assign 'item' before, because fix_fields() can change arg
79 Item *item= *arg;
80 used_tables_cache |= item->used_tables();
81 const_item_cache&= item->const_item() && !with_null;
82 not_null_tables_cache|= item->not_null_tables();
83
84 if (const_item_cache)
85 {
86 if (item->cols() > 1)
87 with_null|= item->null_inside();
88 else
89 {
90 if (item->is_null())
91 with_null|= 1;
92 }
93 }
94 maybe_null|= item->maybe_null;
95 with_sum_func= with_sum_func || item->with_sum_func;
96 }
97 fixed= 1;
98 return FALSE;
99 }
100
101
cleanup()102 void Item_row::cleanup()
103 {
104 DBUG_ENTER("Item_row::cleanup");
105
106 Item::cleanup();
107 /* Reset to the original values */
108 used_tables_cache= 0;
109 const_item_cache= 1;
110 with_null= 0;
111
112 DBUG_VOID_RETURN;
113 }
114
115
split_sum_func(THD * thd,Item ** ref_pointer_array,List<Item> & fields)116 void Item_row::split_sum_func(THD *thd, Item **ref_pointer_array,
117 List<Item> &fields)
118 {
119 Item **arg, **arg_end;
120 for (arg= items, arg_end= items+arg_count; arg != arg_end ; arg++)
121 (*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg, TRUE);
122 }
123
124
update_used_tables()125 void Item_row::update_used_tables()
126 {
127 used_tables_cache= 0;
128 const_item_cache= 1;
129 for (uint i= 0; i < arg_count; i++)
130 {
131 items[i]->update_used_tables();
132 used_tables_cache|= items[i]->used_tables();
133 const_item_cache&= items[i]->const_item();
134 }
135 }
136
check_cols(uint c)137 bool Item_row::check_cols(uint c)
138 {
139 if (c != arg_count)
140 {
141 my_error(ER_OPERAND_COLUMNS, MYF(0), c);
142 return 1;
143 }
144 return 0;
145 }
146
print(String * str,enum_query_type query_type)147 void Item_row::print(String *str, enum_query_type query_type)
148 {
149 str->append('(');
150 for (uint i= 0; i < arg_count; i++)
151 {
152 if (i)
153 str->append(',');
154 items[i]->print(str, query_type);
155 }
156 str->append(')');
157 }
158
159
walk(Item_processor processor,bool walk_subquery,uchar * arg)160 bool Item_row::walk(Item_processor processor, bool walk_subquery, uchar *arg)
161 {
162 for (uint i= 0; i < arg_count; i++)
163 {
164 if (items[i]->walk(processor, walk_subquery, arg))
165 return 1;
166 }
167 return (this->*processor)(arg);
168 }
169
170
transform(Item_transformer transformer,uchar * arg)171 Item *Item_row::transform(Item_transformer transformer, uchar *arg)
172 {
173 DBUG_ASSERT(!current_thd->stmt_arena->is_stmt_prepare());
174
175 for (uint i= 0; i < arg_count; i++)
176 {
177 Item *new_item= items[i]->transform(transformer, arg);
178 if (!new_item)
179 return 0;
180
181 /*
182 THD::change_item_tree() should be called only if the tree was
183 really transformed, i.e. when a new item has been created.
184 Otherwise we'll be allocating a lot of unnecessary memory for
185 change records at each execution.
186 */
187 if (items[i] != new_item)
188 current_thd->change_item_tree(&items[i], new_item);
189 }
190 return (this->*transformer)(arg);
191 }
192
bring_value()193 void Item_row::bring_value()
194 {
195 for (uint i= 0; i < arg_count; i++)
196 items[i]->bring_value();
197 }
198