summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorAlexander Barkov <bar@mariadb.org>2017-10-01 00:30:58 +0400
committerAlexander Barkov <bar@mariadb.org>2017-10-01 00:30:58 +0400
commit6857cb57fe7090f131b272f31485b7a478a0b324 (patch)
tree44cd80409ab7d173b7263c55d3f37ddc8b6914ab /sql
parentaa582dedcbd22182e06355b94d1de4fc62f71910 (diff)
downloadmariadb-git-6857cb57fe7090f131b272f31485b7a478a0b324.tar.gz
MDEV-13967 Parameter data type control for Item_long_func
- Implementing stricter data type control for Item_long_func descendants - Cleanup: renaming Type_handler::can_return_str_ascii() to can_return_text() (a better name).
Diffstat (limited to 'sql')
-rw-r--r--sql/item.cc15
-rw-r--r--sql/item.h3
-rw-r--r--sql/item_cmpfunc.h7
-rw-r--r--sql/item_func.cc19
-rw-r--r--sql/item_func.h63
-rw-r--r--sql/item_inetfunc.h2
-rw-r--r--sql/item_jsonfunc.h8
-rw-r--r--sql/item_strfunc.h7
-rw-r--r--sql/item_timefunc.h76
-rw-r--r--sql/sql_type.h8
10 files changed, 169 insertions, 39 deletions
diff --git a/sql/item.cc b/sql/item.cc
index c62ca8029d3..9f8e3df05f5 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -1113,10 +1113,21 @@ bool Item::check_type_can_return_time(const char *opname) const
}
-bool Item::check_type_can_return_str_ascii(const char *opname) const
+bool Item::check_type_can_return_str(const char *opname) const
{
const Type_handler *handler= type_handler();
- if (handler->can_return_str_ascii())
+ if (handler->can_return_str())
+ return false;
+ my_error(ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION, MYF(0),
+ handler->name().ptr(), opname);
+ return true;
+}
+
+
+bool Item::check_type_can_return_text(const char *opname) const
+{
+ const Type_handler *handler= type_handler();
+ if (handler->can_return_text())
return false;
my_error(ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION, MYF(0),
handler->name().ptr(), opname);
diff --git a/sql/item.h b/sql/item.h
index 7662d358b42..bb341959103 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -1712,7 +1712,8 @@ public:
bool check_type_can_return_int(const char *opname) const;
bool check_type_can_return_decimal(const char *opname) const;
bool check_type_can_return_real(const char *opname) const;
- bool check_type_can_return_str_ascii(const char *opname) const;
+ bool check_type_can_return_str(const char *opname) const;
+ bool check_type_can_return_text(const char *opname) const;
bool check_type_can_return_date(const char *opname) const;
bool check_type_can_return_time(const char *opname) const;
// It is not row => null inside is impossible
diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h
index 0756899f47c..0cb66cd1d33 100644
--- a/sql/item_cmpfunc.h
+++ b/sql/item_cmpfunc.h
@@ -930,6 +930,8 @@ public:
class Item_func_strcmp :public Item_long_func
{
+ bool check_arguments() const
+ { return check_argument_types_can_return_str(0, 2); }
String value1, value2;
DTCollation cmp_collation;
public:
@@ -2829,6 +2831,11 @@ public:
*/
class Item_func_regexp_instr :public Item_long_func
{
+ bool check_arguments() const
+ {
+ return args[0]->check_type_can_return_str(func_name()) ||
+ args[1]->check_type_can_return_text(func_name());
+ }
Regexp_processor_pcre re;
DTCollation cmp_collation;
public:
diff --git a/sql/item_func.cc b/sql/item_func.cc
index 7db5e3bccc7..fc7417411e8 100644
--- a/sql/item_func.cc
+++ b/sql/item_func.cc
@@ -218,13 +218,26 @@ bool Item_func::check_argument_types_can_return_real(uint start,
}
-bool Item_func::check_argument_types_can_return_str_ascii(uint start,
- uint end) const
+bool Item_func::check_argument_types_can_return_text(uint start,
+ uint end) const
+{
+ for (uint i= start; i < end ; i++)
+ {
+ DBUG_ASSERT(i < arg_count);
+ if (args[i]->check_type_can_return_text(func_name()))
+ return true;
+ }
+ return false;
+}
+
+
+bool Item_func::check_argument_types_can_return_str(uint start,
+ uint end) const
{
for (uint i= start; i < end ; i++)
{
DBUG_ASSERT(i < arg_count);
- if (args[i]->check_type_can_return_str_ascii(func_name()))
+ if (args[i]->check_type_can_return_str(func_name()))
return true;
}
return false;
diff --git a/sql/item_func.h b/sql/item_func.h
index c061c713de5..605b17b540e 100644
--- a/sql/item_func.h
+++ b/sql/item_func.h
@@ -48,7 +48,8 @@ protected:
uint start, uint end) const;
bool check_argument_types_can_return_int(uint start, uint end) const;
bool check_argument_types_can_return_real(uint start, uint end) const;
- bool check_argument_types_can_return_str_ascii(uint start, uint end) const;
+ bool check_argument_types_can_return_str(uint start, uint end) const;
+ bool check_argument_types_can_return_text(uint start, uint end) const;
bool check_argument_types_can_return_date(uint start, uint end) const;
bool check_argument_types_can_return_time(uint start, uint end) const;
public:
@@ -1433,6 +1434,8 @@ private:
class Item_func_sign :public Item_long_func
{
+ bool check_arguments() const
+ { return args[0]->check_type_can_return_real(func_name()); }
public:
Item_func_sign(THD *thd, Item *a): Item_long_func(thd, a) {}
const char *func_name() const { return "sign"; }
@@ -1599,14 +1602,23 @@ public:
};
-class Item_func_octet_length :public Item_long_func
+class Item_long_func_length: public Item_long_func
+{
+ bool check_arguments() const
+ { return args[0]->check_type_can_return_str(func_name()); }
+public:
+ Item_long_func_length(THD *thd, Item *a): Item_long_func(thd, a) {}
+ void fix_length_and_dec() { max_length=10; }
+};
+
+
+class Item_func_octet_length :public Item_long_func_length
{
String value;
public:
- Item_func_octet_length(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_octet_length(THD *thd, Item *a): Item_long_func_length(thd, a) {}
longlong val_int();
const char *func_name() const { return "octet_length"; }
- void fix_length_and_dec() { max_length=10; }
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_octet_length>(thd, mem_root, this); }
};
@@ -1626,20 +1638,21 @@ public:
{ return get_item_copy<Item_func_bit_length>(thd, mem_root, this); }
};
-class Item_func_char_length :public Item_long_func
+class Item_func_char_length :public Item_long_func_length
{
String value;
public:
- Item_func_char_length(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_char_length(THD *thd, Item *a): Item_long_func_length(thd, a) {}
longlong val_int();
const char *func_name() const { return "char_length"; }
- void fix_length_and_dec() { max_length=10; }
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_char_length>(thd, mem_root, this); }
};
class Item_func_coercibility :public Item_long_func
{
+ bool check_arguments() const
+ { return args[0]->check_type_can_return_str(func_name()); }
public:
Item_func_coercibility(THD *thd, Item *a): Item_long_func(thd, a) {}
longlong val_int();
@@ -1666,6 +1679,11 @@ public:
*/
class Item_func_locate :public Item_long_func
{
+ bool check_arguments() const
+ {
+ return check_argument_types_can_return_str(0, 2) ||
+ (arg_count > 2 && args[2]->check_type_can_return_int(func_name()));
+ }
String value1,value2;
DTCollation cmp_collation;
public:
@@ -1703,6 +1721,8 @@ public:
class Item_func_ascii :public Item_long_func
{
+ bool check_arguments() const
+ { return check_argument_types_can_return_str(0, arg_count); }
String value;
public:
Item_func_ascii(THD *thd, Item *a): Item_long_func(thd, a) {}
@@ -1715,6 +1735,8 @@ public:
class Item_func_ord :public Item_long_func
{
+ bool check_arguments() const
+ { return args[0]->check_type_can_return_str(func_name()); }
String value;
public:
Item_func_ord(THD *thd, Item *a): Item_long_func(thd, a) {}
@@ -1727,6 +1749,8 @@ public:
class Item_func_find_in_set :public Item_long_func
{
+ bool check_arguments() const
+ { return check_argument_types_can_return_str(0, 2); }
String value,value2;
uint enum_value;
ulonglong enum_bit;
@@ -1783,6 +1807,8 @@ public:
class Item_func_bit_count :public Item_long_func
{
+ bool check_arguments() const
+ { return args[0]->check_type_can_return_int(func_name()); }
public:
Item_func_bit_count(THD *thd, Item *a): Item_long_func(thd, a) {}
longlong val_int();
@@ -1858,6 +1884,11 @@ public:
class Item_func_benchmark :public Item_long_func
{
+ bool check_arguments() const
+ {
+ return args[0]->check_type_can_return_int(func_name()) ||
+ args[1]->check_type_scalar(func_name());
+ }
public:
Item_func_benchmark(THD *thd, Item *count_expr, Item *expr):
Item_long_func(thd, count_expr, expr)
@@ -1880,6 +1911,8 @@ void item_func_sleep_free(void);
class Item_func_sleep :public Item_long_func
{
+ bool check_arguments() const
+ { return args[0]->check_type_can_return_real(func_name()); }
public:
Item_func_sleep(THD *thd, Item *a): Item_long_func(thd, a) {}
void fix_length_and_dec() { fix_char_length(1); }
@@ -2160,6 +2193,11 @@ void mysql_ull_set_explicit_lock_duration(THD *thd);
class Item_func_get_lock :public Item_long_func
{
+ bool check_arguments() const
+ {
+ return args[0]->check_type_general_purpose_string(func_name()) ||
+ args[1]->check_type_can_return_real(func_name());
+ }
String value;
public:
Item_func_get_lock(THD *thd, Item *a, Item *b) :Item_long_func(thd, a, b) {}
@@ -2182,6 +2220,8 @@ class Item_func_get_lock :public Item_long_func
class Item_func_release_lock :public Item_long_func
{
+ bool check_arguments() const
+ { return args[0]->check_type_general_purpose_string(func_name()); }
String value;
public:
Item_func_release_lock(THD *thd, Item *a): Item_long_func(thd, a) {}
@@ -2236,6 +2276,11 @@ public:
class Item_master_gtid_wait :public Item_long_func
{
+ bool check_arguments() const
+ {
+ return args[0]->check_type_general_purpose_string(func_name()) ||
+ (arg_count > 1 && args[1]->check_type_can_return_real(func_name()));
+ }
String value;
public:
Item_master_gtid_wait(THD *thd, Item *a)
@@ -2599,6 +2644,8 @@ public:
class Item_func_is_free_lock :public Item_long_func
{
+ bool check_arguments() const
+ { return args[0]->check_type_general_purpose_string(func_name()); }
String value;
public:
Item_func_is_free_lock(THD *thd, Item *a): Item_long_func(thd, a) {}
@@ -2615,6 +2662,8 @@ public:
class Item_func_is_used_lock :public Item_long_func
{
+ bool check_arguments() const
+ { return args[0]->check_type_general_purpose_string(func_name()); }
String value;
public:
Item_func_is_used_lock(THD *thd, Item *a): Item_long_func(thd, a) {}
diff --git a/sql/item_inetfunc.h b/sql/item_inetfunc.h
index 5d24835a625..13ce003a374 100644
--- a/sql/item_inetfunc.h
+++ b/sql/item_inetfunc.h
@@ -27,7 +27,7 @@
class Item_func_inet_aton : public Item_longlong_func
{
bool check_arguments() const
- { return check_argument_types_can_return_str_ascii(0, arg_count); }
+ { return check_argument_types_can_return_text(0, arg_count); }
public:
Item_func_inet_aton(THD *thd, Item *a): Item_longlong_func(thd, a) {}
longlong val_int();
diff --git a/sql/item_jsonfunc.h b/sql/item_jsonfunc.h
index 7da72dc7f89..77e7588be25 100644
--- a/sql/item_jsonfunc.h
+++ b/sql/item_jsonfunc.h
@@ -295,6 +295,12 @@ public:
class Item_func_json_length: public Item_long_func
{
+ bool check_arguments() const
+ {
+ return args[0]->check_type_can_return_text(func_name()) ||
+ (arg_count > 1 &&
+ args[1]->check_type_general_purpose_string(func_name()));
+ }
protected:
json_path_with_flags path;
String tmp_js;
@@ -312,6 +318,8 @@ public:
class Item_func_json_depth: public Item_long_func
{
+ bool check_arguments() const
+ { return args[0]->check_type_can_return_text(func_name()); }
protected:
String tmp_js;
public:
diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h
index d73bd0b76d7..1c35588f884 100644
--- a/sql/item_strfunc.h
+++ b/sql/item_strfunc.h
@@ -1451,6 +1451,8 @@ public:
class Item_func_crc32 :public Item_long_func
{
+ bool check_arguments() const
+ { return args[0]->check_type_can_return_str(func_name()); }
String value;
public:
Item_func_crc32(THD *thd, Item *a): Item_long_func(thd, a)
@@ -1462,11 +1464,12 @@ public:
{ return get_item_copy<Item_func_crc32>(thd, mem_root, this); }
};
-class Item_func_uncompressed_length : public Item_long_func
+class Item_func_uncompressed_length : public Item_long_func_length
{
String value;
public:
- Item_func_uncompressed_length(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_uncompressed_length(THD *thd, Item *a)
+ :Item_long_func_length(thd, a) {}
const char *func_name() const{return "uncompressed_length";}
void fix_length_and_dec() { max_length=10; maybe_null= true; }
longlong val_int();
diff --git a/sql/item_timefunc.h b/sql/item_timefunc.h
index 6bbc37a78e2..65057a71c4e 100644
--- a/sql/item_timefunc.h
+++ b/sql/item_timefunc.h
@@ -33,8 +33,31 @@ enum date_time_format_types
bool get_interval_value(Item *args,interval_type int_type, INTERVAL *interval);
+
+class Item_long_func_date_field: public Item_long_func
+{
+ bool check_arguments() const
+ { return args[0]->check_type_can_return_date(func_name()); }
+public:
+ Item_long_func_date_field(THD *thd, Item *a)
+ :Item_long_func(thd, a) { }
+};
+
+
+class Item_long_func_time_field: public Item_long_func
+{
+ bool check_arguments() const
+ { return args[0]->check_type_can_return_time(func_name()); }
+public:
+ Item_long_func_time_field(THD *thd, Item *a)
+ :Item_long_func(thd, a) { }
+};
+
+
class Item_func_period_add :public Item_long_func
{
+ bool check_arguments() const
+ { return check_argument_types_can_return_int(0, 2); }
public:
Item_func_period_add(THD *thd, Item *a, Item *b): Item_long_func(thd, a, b) {}
longlong val_int();
@@ -50,6 +73,8 @@ public:
class Item_func_period_diff :public Item_long_func
{
+ bool check_arguments() const
+ { return check_argument_types_can_return_int(0, 2); }
public:
Item_func_period_diff(THD *thd, Item *a, Item *b): Item_long_func(thd, a, b) {}
longlong val_int();
@@ -64,10 +89,10 @@ public:
};
-class Item_func_to_days :public Item_long_func
+class Item_func_to_days :public Item_long_func_date_field
{
public:
- Item_func_to_days(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_to_days(THD *thd, Item *a): Item_long_func_date_field(thd, a) {}
longlong val_int();
const char *func_name() const { return "to_days"; }
void fix_length_and_dec()
@@ -117,10 +142,10 @@ public:
};
-class Item_func_dayofmonth :public Item_long_func
+class Item_func_dayofmonth :public Item_long_func_date_field
{
public:
- Item_func_dayofmonth(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_dayofmonth(THD *thd, Item *a): Item_long_func_date_field(thd, a) {}
longlong val_int();
const char *func_name() const { return "dayofmonth"; }
void fix_length_and_dec()
@@ -197,10 +222,10 @@ public:
};
-class Item_func_dayofyear :public Item_long_func
+class Item_func_dayofyear :public Item_long_func_date_field
{
public:
- Item_func_dayofyear(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_dayofyear(THD *thd, Item *a): Item_long_func_date_field(thd, a) {}
longlong val_int();
const char *func_name() const { return "dayofyear"; }
void fix_length_and_dec()
@@ -220,10 +245,10 @@ public:
};
-class Item_func_hour :public Item_long_func
+class Item_func_hour :public Item_long_func_time_field
{
public:
- Item_func_hour(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_hour(THD *thd, Item *a): Item_long_func_time_field(thd, a) {}
longlong val_int();
const char *func_name() const { return "hour"; }
void fix_length_and_dec()
@@ -243,10 +268,10 @@ public:
};
-class Item_func_minute :public Item_long_func
+class Item_func_minute :public Item_long_func_time_field
{
public:
- Item_func_minute(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_minute(THD *thd, Item *a): Item_long_func_time_field(thd, a) {}
longlong val_int();
const char *func_name() const { return "minute"; }
void fix_length_and_dec()
@@ -266,10 +291,10 @@ public:
};
-class Item_func_quarter :public Item_long_func
+class Item_func_quarter :public Item_long_func_date_field
{
public:
- Item_func_quarter(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_quarter(THD *thd, Item *a): Item_long_func_date_field(thd, a) {}
longlong val_int();
const char *func_name() const { return "quarter"; }
void fix_length_and_dec()
@@ -289,10 +314,10 @@ public:
};
-class Item_func_second :public Item_long_func
+class Item_func_second :public Item_long_func_time_field
{
public:
- Item_func_second(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_second(THD *thd, Item *a): Item_long_func_time_field(thd, a) {}
longlong val_int();
const char *func_name() const { return "second"; }
void fix_length_and_dec()
@@ -314,6 +339,11 @@ public:
class Item_func_week :public Item_long_func
{
+ bool check_arguments() const
+ {
+ return args[0]->check_type_can_return_date(func_name()) ||
+ (arg_count > 1 && args[1]->check_type_can_return_int(func_name()));
+ }
public:
Item_func_week(THD *thd, Item *a): Item_long_func(thd, a) {}
Item_func_week(THD *thd, Item *a, Item *b): Item_long_func(thd, a, b) {}
@@ -341,8 +371,14 @@ public:
class Item_func_yearweek :public Item_long_func
{
+ bool check_arguments() const
+ {
+ return args[0]->check_type_can_return_date(func_name()) ||
+ args[1]->check_type_can_return_int(func_name());
+ }
public:
- Item_func_yearweek(THD *thd, Item *a, Item *b): Item_long_func(thd, a, b) {}
+ Item_func_yearweek(THD *thd, Item *a, Item *b)
+ :Item_long_func(thd, a, b) {}
longlong val_int();
const char *func_name() const { return "yearweek"; }
void fix_length_and_dec()
@@ -362,10 +398,10 @@ public:
};
-class Item_func_year :public Item_long_func
+class Item_func_year :public Item_long_func_date_field
{
public:
- Item_func_year(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_year(THD *thd, Item *a): Item_long_func_date_field(thd, a) {}
longlong val_int();
const char *func_name() const { return "year"; }
enum_monotonicity_info get_monotonicity_info() const;
@@ -872,7 +908,7 @@ class Item_func_convert_tz :public Item_datetimefunc
bool check_arguments() const
{
return args[0]->check_type_can_return_date(func_name()) ||
- check_argument_types_can_return_str_ascii(1, arg_count);
+ check_argument_types_can_return_text(1, arg_count);
}
/*
If time zone parameters are constants we are caching objects that
@@ -1211,10 +1247,10 @@ public:
};
-class Item_func_microsecond :public Item_long_func
+class Item_func_microsecond :public Item_long_func_time_field
{
public:
- Item_func_microsecond(THD *thd, Item *a): Item_long_func(thd, a) {}
+ Item_func_microsecond(THD *thd, Item *a): Item_long_func_time_field(thd, a) {}
longlong val_int();
const char *func_name() const { return "microsecond"; }
void fix_length_and_dec()
diff --git a/sql/sql_type.h b/sql/sql_type.h
index f570e91f882..910172134fd 100644
--- a/sql/sql_type.h
+++ b/sql/sql_type.h
@@ -706,7 +706,8 @@ public:
virtual bool can_return_int() const { return true; }
virtual bool can_return_decimal() const { return true; }
virtual bool can_return_real() const { return true; }
- virtual bool can_return_str_ascii() const { return true; }
+ virtual bool can_return_str() const { return true; }
+ virtual bool can_return_text() const { return true; }
virtual bool can_return_date() const { return true; }
virtual bool can_return_time() const { return true; }
virtual bool is_general_purpose_string_type() const { return false; }
@@ -1007,7 +1008,8 @@ public:
bool can_return_int() const { return false; }
bool can_return_decimal() const { return false; }
bool can_return_real() const { return false; }
- bool can_return_str_ascii() const { return false; }
+ bool can_return_str() const { return false; }
+ bool can_return_text() const { return false; }
bool can_return_date() const { return false; }
bool can_return_time() const { return false; }
enum_field_types field_type() const
@@ -2683,7 +2685,7 @@ public:
bool can_return_int() const { return false; }
bool can_return_decimal() const { return false; }
bool can_return_real() const { return false; }
- bool can_return_str_ascii() const { return false; }
+ bool can_return_text() const { return false; }
bool can_return_date() const { return false; }
bool can_return_time() const { return false; }
bool is_traditional_type() const