Skip to content

Commit dc9b43a

Browse files
MDEV-34940: Fix Item_func_collect inheritance
Creates new parent Item_sum_str for Item_func_group_concat and Item_func_collect, migrating shared methods from each of those latter classes to the former. Simultaneously, tighten up the method scopes.
1 parent e2bb06b commit dc9b43a

File tree

3 files changed

+132
-103
lines changed

3 files changed

+132
-103
lines changed

sql/item_jsonfunc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ class Item_func_json_format: public Item_json_func
708708

709709
class Item_func_json_arrayagg : public Item_func_group_concat
710710
{
711-
protected:
711+
private:
712712
/*
713713
Overrides Item_func_group_concat::skip_nulls()
714714
NULL-s should be added to the result as JSON null value.

sql/item_sum.cc

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3521,6 +3521,42 @@ String *Item_sum_udf_str::val_str(String *str)
35213521
#endif /* HAVE_DLOPEN */
35223522

35233523

3524+
bool
3525+
Item_sum_str::fix_fields(THD *thd, Item **ref)
3526+
{
3527+
DBUG_ASSERT(fixed() == 0);
3528+
3529+
if (init_sum_func_check(thd))
3530+
return TRUE;
3531+
3532+
set_maybe_null();
3533+
3534+
/*
3535+
Fix fields for select list and ORDER clause
3536+
*/
3537+
3538+
for (uint i=0 ; i < arg_count ; i++)
3539+
{
3540+
if (args[i]->fix_fields_if_needed_for_scalar(thd, &args[i]))
3541+
return TRUE;
3542+
/* We should ignore FIELD's in arguments to sum functions */
3543+
with_flags|= (args[i]->with_flags & ~item_with_t::FIELD);
3544+
if (args[i]->check_type_can_return_str(
3545+
Item_func_group_concat::func_name_cstring()))
3546+
return true;
3547+
}
3548+
3549+
if (fix_fields_impl(thd, ref))
3550+
return TRUE;
3551+
3552+
if (check_sum_func(thd, ref))
3553+
return TRUE;
3554+
3555+
base_flags|= item_base_t::FIXED;
3556+
return FALSE;
3557+
}
3558+
3559+
35243560
/*****************************************************************************
35253561
GROUP_CONCAT function
35263562
@@ -3900,7 +3936,7 @@ Item_func_group_concat(THD *thd, Name_resolution_context *context_arg,
39003936
const SQL_I_List<ORDER> &order_list,
39013937
String *separator_arg, bool limit_clause,
39023938
Item *row_limit_arg, Item *offset_limit_arg)
3903-
:Item_sum(thd), tmp_table_param(0), separator(separator_arg), tree(0),
3939+
:Item_sum_str(thd), tmp_table_param(0), separator(separator_arg), tree(0),
39043940
unique_filter(NULL), table(0),
39053941
order(0), context(context_arg),
39063942
arg_count_order(order_list.elements),
@@ -3963,7 +3999,7 @@ Item_func_group_concat(THD *thd, Name_resolution_context *context_arg,
39633999

39644000
Item_func_group_concat::Item_func_group_concat(THD *thd,
39654001
Item_func_group_concat *item)
3966-
:Item_sum(thd, item),
4002+
:Item_sum_str(thd, item),
39674003
tmp_table_param(item->tmp_table_param),
39684004
separator(item->separator),
39694005
tree(item->tree),
@@ -4232,31 +4268,8 @@ bool Item_func_group_concat::add(bool exclude_nulls)
42324268

42334269

42344270
bool
4235-
Item_func_group_concat::fix_fields(THD *thd, Item **ref)
4271+
Item_func_group_concat::fix_fields_impl(THD *thd, Item **ref)
42364272
{
4237-
uint i; /* for loop variable */
4238-
DBUG_ASSERT(fixed() == 0);
4239-
4240-
if (init_sum_func_check(thd))
4241-
return TRUE;
4242-
4243-
set_maybe_null();
4244-
4245-
/*
4246-
Fix fields for select list and ORDER clause
4247-
*/
4248-
4249-
for (i=0 ; i < arg_count ; i++)
4250-
{
4251-
if (args[i]->fix_fields_if_needed_for_scalar(thd, &args[i]))
4252-
return TRUE;
4253-
/* We should ignore FIELD's in arguments to sum functions */
4254-
with_flags|= (args[i]->with_flags & ~item_with_t::FIELD);
4255-
if (args[i]->check_type_can_return_str(
4256-
Item_func_group_concat::func_name_cstring()))
4257-
return true;
4258-
}
4259-
42604273
/* skip charset aggregation for order columns */
42614274
if (agg_arg_charsets_for_string_result(collation,
42624275
args, arg_count - arg_count_order))
@@ -4296,10 +4309,6 @@ Item_func_group_concat::fix_fields(THD *thd, Item **ref)
42964309
separator= new_separator;
42974310
}
42984311

4299-
if (check_sum_func(thd, ref))
4300-
return TRUE;
4301-
4302-
base_flags|= item_base_t::FIXED;
43034312
return FALSE;
43044313
}
43054314

@@ -4609,7 +4618,7 @@ void Item_func_collect::clear() {
46094618
void Item_func_collect::cleanup() {
46104619
List_iterator<String> geometries_iterator(geometries);
46114620
geometries.delete_elements();
4612-
Item_sum_int::cleanup();
4621+
Item_sum_str::cleanup();
46134622
}
46144623

46154624

@@ -4679,7 +4688,7 @@ bool Item_func_collect::list_contains_element(String *wkb) {
46794688

46804689

46814690
Item_func_collect::Item_func_collect(THD *thd, bool is_distinct, Item *item_par) :
4682-
Item_sum_int(thd, item_par),
4691+
Item_sum_str(thd, item_par),
46834692
mem_root(thd->mem_root),
46844693
is_distinct(is_distinct),
46854694
group_collect_max_len(thd->variables.group_concat_max_len)
@@ -4688,7 +4697,7 @@ Item_func_collect::Item_func_collect(THD *thd, bool is_distinct, Item *item_par)
46884697

46894698

46904699
Item_func_collect::Item_func_collect(THD *thd, bool is_distinct, Item_func_collect *item) :
4691-
Item_sum_int(thd, item),
4700+
Item_sum_str(thd, item),
46924701
mem_root(thd->mem_root),
46934702
is_distinct(is_distinct),
46944703
group_collect_max_len(thd->variables.group_concat_max_len)

sql/item_sum.h

Lines changed: 89 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,9 +1948,65 @@ int dump_leaf_key(void* key_arg,
19481948
void* item_arg);
19491949
C_MODE_END
19501950

1951-
class Item_func_group_concat : public Item_sum
1951+
class Item_sum_str : public Item_sum
19521952
{
1953+
public:
1954+
Item_sum_str(THD *thd)
1955+
: Item_sum(thd)
1956+
{}
1957+
1958+
Item_sum_str(THD *thd, Item_sum_str *item)
1959+
: Item_sum(thd, item)
1960+
{}
1961+
1962+
Item_sum_str(THD *thd, Item *item_par)
1963+
: Item_sum(thd, item_par)
1964+
{}
1965+
1966+
bool fix_fields(THD *, Item **) override;
1967+
1968+
longlong val_int() override
1969+
{
1970+
String *res;
1971+
char *end_ptr;
1972+
int error;
1973+
if (!(res= val_str(&str_value)))
1974+
return (longlong) 0;
1975+
end_ptr= (char*) res->ptr()+ res->length();
1976+
return my_strtoll10(res->ptr(), &end_ptr, &error);
1977+
}
1978+
1979+
double val_real() override
1980+
{
1981+
int error;
1982+
const char *end;
1983+
String *res;
1984+
if (!(res= val_str(&str_value)))
1985+
return 0.0;
1986+
end= res->ptr() + res->length();
1987+
return (my_strtod(res->ptr(), (char**) &end, &error));
1988+
}
1989+
1990+
my_decimal *val_decimal(my_decimal *decimal_value) override
1991+
{
1992+
return val_decimal_from_string(decimal_value);
1993+
}
1994+
1995+
bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
1996+
{
1997+
return get_date_from_string(thd, ltime, fuzzydate);
1998+
}
1999+
2000+
void no_rows_in_result() override {}
2001+
void reset_field() override { DBUG_ASSERT(0); } // not used
2002+
void update_field() override { DBUG_ASSERT(0); } // not used
2003+
19532004
protected:
2005+
virtual bool fix_fields_impl(THD *, Item **) = 0;
2006+
};
2007+
2008+
class Item_func_group_concat : public Item_sum_str
2009+
{
19542010
TMP_TABLE_PARAM *tmp_table_param;
19552011
String result;
19562012
String *separator;
@@ -2030,27 +2086,8 @@ class Item_func_group_concat : public Item_sum
20302086
virtual String *get_str_from_field(Item *i, Field *f, String *tmp,
20312087
const uchar *key, size_t offset)
20322088
{ return f->val_str(tmp, key + offset); }
2033-
virtual void cut_max_length(String *result,
2034-
uint old_length, uint max_length) const;
20352089
bool uses_non_standard_aggregator_for_distinct() const override
20362090
{ return distinct; }
2037-
2038-
public:
2039-
// Methods used by ColumnStore
2040-
bool get_distinct() const { return distinct; }
2041-
uint get_count_field() const { return arg_count_field; }
2042-
uint get_order_field() const { return arg_count_order; }
2043-
const String* get_separator() const { return separator; }
2044-
ORDER** get_order() const { return order; }
2045-
2046-
public:
2047-
Item_func_group_concat(THD *thd, Name_resolution_context *context_arg,
2048-
bool is_distinct, List<Item> *is_select,
2049-
const SQL_I_List<ORDER> &is_order, String *is_separator,
2050-
bool limit_clause, Item *row_limit, Item *offset_limit);
2051-
2052-
Item_func_group_concat(THD *thd, Item_func_group_concat *item);
2053-
~Item_func_group_concat();
20542091
void cleanup() override;
20552092

20562093
enum Sumfunctype sum_func () const override {return GROUP_CONCAT_FUNC;}
@@ -2070,56 +2107,44 @@ class Item_func_group_concat : public Item_sum
20702107
{
20712108
return add(skip_nulls());
20722109
}
2073-
void reset_field() override { DBUG_ASSERT(0); } // not used
2074-
void update_field() override { DBUG_ASSERT(0); } // not used
2075-
bool fix_fields(THD *,Item **) override;
2110+
bool fix_fields_impl(THD *,Item **) override;
20762111
bool setup(THD *thd) override;
20772112
void make_unique() override;
2078-
double val_real() override
2079-
{
2080-
int error;
2081-
const char *end;
2082-
String *res;
2083-
if (!(res= val_str(&str_value)))
2084-
return 0.0;
2085-
end= res->ptr() + res->length();
2086-
return (my_strtod(res->ptr(), (char**) &end, &error));
2087-
}
2088-
longlong val_int() override
2089-
{
2090-
String *res;
2091-
char *end_ptr;
2092-
int error;
2093-
if (!(res= val_str(&str_value)))
2094-
return (longlong) 0;
2095-
end_ptr= (char*) res->ptr()+ res->length();
2096-
return my_strtoll10(res->ptr(), &end_ptr, &error);
2097-
}
2098-
my_decimal *val_decimal(my_decimal *decimal_value) override
2099-
{
2100-
return val_decimal_from_string(decimal_value);
2101-
}
2102-
bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
2103-
{
2104-
return get_date_from_string(thd, ltime, fuzzydate);
2105-
}
2106-
String *val_str(String *str) override;
21072113
Item *copy_or_same(THD* thd) override;
2108-
void no_rows_in_result() override {}
21092114
void print(String *str, enum_query_type query_type) override;
21102115
bool change_context_processor(void *cntx) override
21112116
{ context= (Name_resolution_context *)cntx; return FALSE; }
21122117
Item *do_get_copy(THD *thd) const override
21132118
{ return get_item_copy<Item_func_group_concat>(thd, this); }
2119+
2120+
protected:
2121+
virtual void cut_max_length(String *result,
2122+
uint old_length, uint max_length) const;
2123+
String *val_str(String *str) override;
2124+
2125+
public:
2126+
// Methods used by ColumnStore
2127+
bool get_distinct() const { return distinct; }
2128+
uint get_count_field() const { return arg_count_field; }
2129+
uint get_order_field() const { return arg_count_order; }
2130+
const String* get_separator() const { return separator; }
2131+
ORDER** get_order() const { return order; }
2132+
2133+
Item_func_group_concat(THD *thd, Name_resolution_context *context_arg,
2134+
bool is_distinct, List<Item> *is_select,
2135+
const SQL_I_List<ORDER> &is_order, String *is_separator,
2136+
bool limit_clause, Item *row_limit, Item *offset_limit);
2137+
2138+
Item_func_group_concat(THD *thd, Item_func_group_concat *item);
2139+
~Item_func_group_concat();
21142140
qsort_cmp2 get_comparator_function_for_distinct();
21152141
qsort_cmp2 get_comparator_function_for_order_by();
21162142
uchar* get_record_pointer();
21172143
uint get_null_bytes();
2118-
21192144
};
21202145

21212146

2122-
class Item_func_collect :public Item_sum_int // XXX why *int* ???
2147+
class Item_func_collect : public Item_sum_str
21232148
{
21242149
uint32 srid;
21252150
bool has_cached_result;
@@ -2136,27 +2161,13 @@ class Item_func_collect :public Item_sum_int // XXX why *int* ???
21362161
void remove() override;
21372162
bool list_contains_element(String* wkb);
21382163

2139-
public:
2140-
Item_func_collect(THD *thd, bool is_distinct, Item *item_par);
2141-
Item_func_collect(THD *thd, bool is_distinct, Item_func_collect *item);
2142-
2143-
bool fix_length_and_dec(THD *thd) override
2144-
{
2145-
Item_sum_int::fix_length_and_dec(thd);
2146-
base_flags|= item_base_t::MAYBE_NULL;
2147-
return false;
2148-
}
21492164
enum Sumfunctype sum_func () const override
21502165
{
21512166
return GEOMETRY_COLLECT_FUNC;
21522167
}
2153-
void no_rows_in_result() override {; }
21542168
const Type_handler *type_handler() const override
21552169
{ return &type_handler_string; }
2156-
longlong val_int() override { return 0; }
21572170
String *val_str(String*str) override;
2158-
void reset_field() override {DBUG_ASSERT(0);}
2159-
void update_field() override {DBUG_ASSERT(0);}
21602171
LEX_CSTRING func_name_cstring() const override
21612172
{
21622173
return { STRING_WITH_LEN("st_collect(") };
@@ -2169,5 +2180,14 @@ class Item_func_collect :public Item_sum_int // XXX why *int* ???
21692180
{
21702181
return true;
21712182
}
2183+
2184+
bool fix_fields_impl(THD *,Item **) override
2185+
{
2186+
return FALSE;
2187+
}
2188+
2189+
public:
2190+
Item_func_collect(THD *thd, bool is_distinct, Item *item_par);
2191+
Item_func_collect(THD *thd, bool is_distinct, Item_func_collect *item);
21722192
};
21732193
#endif /* ITEM_SUM_INCLUDED */

0 commit comments

Comments
 (0)