Skip to content

Commit 23b1f91

Browse files
committed
Add new STRCMP2 Binary Function
1 parent 702007d commit 23b1f91

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

sql/item_cmpfunc.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,6 +2687,27 @@ longlong Item_func_strcmp::val_int() {
26872687
return value == 0 ? 0 : value < 0 ? -1 : 1;
26882688
}
26892689

2690+
longlong Item_func_strcmp2::val_int() {
2691+
assert(fixed);
2692+
const CHARSET_INFO *cs = cmp.cmp_collation.collation;
2693+
String *a = eval_string_arg(cs, args[0], &cmp.value1);
2694+
if (a == nullptr) {
2695+
if (current_thd->is_error()) return error_int();
2696+
null_value = true;
2697+
return 0;
2698+
}
2699+
2700+
String *b = eval_string_arg(cs, args[1], &cmp.value2);
2701+
if (b == nullptr) {
2702+
if (current_thd->is_error()) return error_int();
2703+
null_value = true;
2704+
return 0;
2705+
}
2706+
int value = sortcmp(a, b, cs);
2707+
null_value = false;
2708+
return value == 0 ? 0 : value < 0 ? -1 : 1;
2709+
}
2710+
26902711
bool Item_func_opt_neg::eq(const Item *item, bool binary_cmp) const {
26912712
/* Assume we don't have rtti */
26922713
if (this == item) return true;

sql/item_cmpfunc.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,27 @@ class Item_func_strcmp final : public Item_bool_func2 {
13781378
}
13791379
};
13801380

1381+
class Item_func_strcmp2 final : public Item_bool_func2 {
1382+
public:
1383+
Item_func_strcmp2(const POS &pos, Item *a, Item *b): Item_bool_func2(pos, a, b) {}
1384+
longlong val_int() override;
1385+
optimize_type select_optimize(const THD *) override { return OPTIMIZE_NONE; }
1386+
const char *func_name() const override { return "strcmp2"; }
1387+
enum Functype functype() const override { return STRCMP_FUNC2; }
1388+
1389+
void print(const THD *thd, String *str,enum_query_type query_type) const override {
1390+
Item_func::print(thd, str, query_type);
1391+
}
1392+
// We derive (indirectly) from Item_bool_func, but this is not a true boolean.
1393+
// Override length and unsigned_flag set by set_data_type_bool().
1394+
bool resolve_type(THD *thd) override {
1395+
if (Item_bool_func2::resolve_type(thd)) return true;
1396+
fix_char_length(2); // returns "1" or "0" or "-1"
1397+
unsigned_flag = false;
1398+
return false;
1399+
}
1400+
};
1401+
13811402
struct interval_range {
13821403
Item_result type;
13831404
double dbl;

sql/item_create.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,8 +1553,9 @@ static const std::pair<const char *, Create_func *> func_array[] = {
15531553
{"WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS",
15541554
SQL_FN_V(Item_master_gtid_set_wait, 1, 3)},
15551555
{"SQRT", SQL_FN(Item_func_sqrt, 1)},
1556-
{"SQRT_LOG", SQL_FN(Item_func_sqrt_log, 1)},
1556+
{"SQRT_LOG", SQL_FN(Item_func_sqrt_log, 1)},
15571557
{"STRCMP", SQL_FN(Item_func_strcmp, 2)},
1558+
{"STRCMP2", SQL_FN(Item_func_strcmp2, 2)},
15581559
{"STR_TO_DATE", SQL_FN(Item_func_str_to_date, 2)},
15591560
{"ST_AREA", SQL_FN(Item_func_st_area, 1)},
15601561
{"ST_ASBINARY", SQL_FN_V(Item_func_as_wkb, 1, 2)},

sql/item_func.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ class Item_func : public Item_result_field {
304304
JSON_UNQUOTE_FUNC,
305305
MEMBER_OF_FUNC,
306306
STRCMP_FUNC,
307+
STRCMP_FUNC2,
307308
TRUE_FUNC,
308309
FALSE_FUNC
309310
};

0 commit comments

Comments
 (0)