21
21
This file defines all compare functions
26
#include <drizzled/cached_item.h>
27
#include <drizzled/check_stack_overrun.h>
28
#include <drizzled/current_session.h>
29
#include <drizzled/error.h>
30
#include <drizzled/internal/my_sys.h>
31
#include <drizzled/item/cache_int.h>
32
#include <drizzled/item/cmpfunc.h>
33
#include <drizzled/item/int_with_ref.h>
34
#include <drizzled/item/subselect.h>
35
#include <drizzled/session.h>
36
#include <drizzled/sql_select.h>
37
#include <drizzled/temporal.h>
38
#include <drizzled/time_functions.h>
25
#include "drizzled/sql_select.h"
26
#include "drizzled/error.h"
27
#include "drizzled/temporal.h"
28
#include "drizzled/item/cmpfunc.h"
29
#include "drizzled/cached_item.h"
30
#include "drizzled/item/cache_int.h"
31
#include "drizzled/item/int_with_ref.h"
32
#include "drizzled/check_stack_overrun.h"
33
#include "drizzled/time_functions.h"
34
#include "drizzled/internal/my_sys.h"
41
36
#include <algorithm>
575
Arg_comparator::Arg_comparator():
576
session(current_session),
581
Arg_comparator::Arg_comparator(Item **a1, Item **a2):
584
session(current_session),
589
569
int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type)
592
572
func= comparator_matrix[type]
593
[test(owner->functype() == Item_func::EQUAL_FUNC)];
573
[test(owner->functype() == Item_func::EQUAL_FUNC)];
598
uint32_t n= (*a)->cols();
599
if (n != (*b)->cols())
601
my_error(ER_OPERAND_COLUMNS, MYF(0), n);
605
if (!(comparators= new Arg_comparator[n]))
607
for (uint32_t i=0; i < n; i++)
609
if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
611
my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
614
comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
577
uint32_t n= (*a)->cols();
578
if (n != (*b)->cols())
580
my_error(ER_OPERAND_COLUMNS, MYF(0), n);
584
if (!(comparators= new Arg_comparator[n]))
586
for (uint32_t i=0; i < n; i++)
588
if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
590
my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
593
comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
619
597
case STRING_RESULT:
600
We must set cmp_charset here as we may be called from for an automatic
601
generated item, like in natural join
603
if (cmp_collation.set((*a)->collation, (*b)->collation) ||
604
cmp_collation.derivation == DERIVATION_NONE)
606
my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
609
if (cmp_collation.collation == &my_charset_bin)
622
We must set cmp_charset here as we may be called from for an automatic
623
generated item, like in natural join
612
We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
613
without removing end space
625
if (cmp_collation.set((*a)->collation, (*b)->collation) ||
626
cmp_collation.derivation == DERIVATION_NONE)
628
my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
631
if (cmp_collation.collation == &my_charset_bin)
634
We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
635
without removing end space
637
if (func == &Arg_comparator::compare_string)
638
func= &Arg_comparator::compare_binary_string;
639
else if (func == &Arg_comparator::compare_e_string)
640
func= &Arg_comparator::compare_e_binary_string;
615
if (func == &Arg_comparator::compare_string)
616
func= &Arg_comparator::compare_binary_string;
617
else if (func == &Arg_comparator::compare_e_string)
618
func= &Arg_comparator::compare_e_binary_string;
643
As this is binary compassion, mark all fields that they can't be
644
transformed. Otherwise we would get into trouble with comparisons
646
WHERE col= 'j' AND col LIKE BINARY 'j'
647
which would be transformed to:
621
As this is binary compassion, mark all fields that they can't be
622
transformed. Otherwise we would get into trouble with comparisons
624
WHERE col= 'j' AND col LIKE BINARY 'j'
625
which would be transformed to:
650
(*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
651
(*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
628
(*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
629
(*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
657
if (func == &Arg_comparator::compare_int_signed)
659
if ((*a)->unsigned_flag)
660
func= (((*b)->unsigned_flag)?
661
&Arg_comparator::compare_int_unsigned :
662
&Arg_comparator::compare_int_unsigned_signed);
663
else if ((*b)->unsigned_flag)
664
func= &Arg_comparator::compare_int_signed_unsigned;
666
else if (func== &Arg_comparator::compare_e_int)
668
if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
669
func= &Arg_comparator::compare_e_int_diff_signedness;
635
if (func == &Arg_comparator::compare_int_signed)
637
if ((*a)->unsigned_flag)
638
func= (((*b)->unsigned_flag)?
639
&Arg_comparator::compare_int_unsigned :
640
&Arg_comparator::compare_int_unsigned_signed);
641
else if ((*b)->unsigned_flag)
642
func= &Arg_comparator::compare_int_signed_unsigned;
644
else if (func== &Arg_comparator::compare_e_int)
646
if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
647
func= &Arg_comparator::compare_e_int_diff_signedness;
673
651
case DECIMAL_RESULT:
675
653
case REAL_RESULT:
655
if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
677
if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
679
precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
680
if (func == &Arg_comparator::compare_real)
681
func= &Arg_comparator::compare_real_fixed;
682
else if (func == &Arg_comparator::compare_e_real)
683
func= &Arg_comparator::compare_e_real_fixed;
657
precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
658
if (func == &Arg_comparator::compare_real)
659
func= &Arg_comparator::compare_real_fixed;
660
else if (func == &Arg_comparator::compare_e_real)
661
func= &Arg_comparator::compare_e_real_fixed;
712
691
converted value. 0 on error and on zero-dates -- check 'failure'
716
get_date_from_str(Session *session, String *str, type::timestamp_t warn_type,
695
get_date_from_str(Session *session, String *str, enum enum_drizzle_timestamp_type warn_type,
717
696
char *warn_name, bool *error_arg)
720
type::cut_t error= type::VALID;
722
type::timestamp_t ret;
724
ret= l_time.store(str->ptr(), str->length(),
725
(TIME_FUZZY_DATE | MODE_INVALID_DATES | (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
728
if (ret == type::DRIZZLE_TIMESTAMP_DATETIME || ret == type::DRIZZLE_TIMESTAMP_DATE)
701
enum enum_drizzle_timestamp_type ret;
703
ret= str_to_datetime(str->ptr(), str->length(), &l_time,
704
(TIME_FUZZY_DATE | MODE_INVALID_DATES |
705
(session->variables.sql_mode & MODE_NO_ZERO_DATE)),
708
if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
731
711
Do not return yet, we may still want to throw a "trailing garbage"
734
714
*error_arg= false;
735
l_time.convert(value);
715
value= TIME_to_uint64_t_datetime(&l_time);
739
719
*error_arg= true;
740
error= type::CUT; /* force warning */
720
error= 1; /* force warning */
743
if (error != type::VALID)
745
725
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
746
726
str->ptr(), str->length(),
2144
2124
else if (cmp_type == DECIMAL_RESULT)
2146
type::Decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2126
my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2147
2127
a_buf, *a_dec, b_buf, *b_dec;
2148
2128
if ((null_value=args[0]->null_value))
2150
2130
a_dec= args[1]->val_decimal(&a_buf);
2151
2131
b_dec= args[2]->val_decimal(&b_buf);
2152
2132
if (!args[1]->null_value && !args[2]->null_value)
2153
return (int64_t) ((class_decimal_cmp(dec, a_dec) >= 0 &&
2154
class_decimal_cmp(dec, b_dec) <= 0) != negated);
2133
return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2134
my_decimal_cmp(dec, b_dec) <= 0) != negated);
2155
2135
if (args[1]->null_value && args[2]->null_value)
2157
2137
else if (args[1]->null_value)
2158
null_value= (class_decimal_cmp(dec, b_dec) <= 0);
2138
null_value= (my_decimal_cmp(dec, b_dec) <= 0);
2160
null_value= (class_decimal_cmp(dec, a_dec) >= 0);
2140
null_value= (my_decimal_cmp(dec, a_dec) >= 0);
3266
3230
in_decimal::in_decimal(uint32_t elements)
3267
:in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
3231
:in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3271
3235
void in_decimal::set(uint32_t pos, Item *item)
3273
/* as far as 'item' is constant, we can store reference on type::Decimal */
3274
type::Decimal *dec= ((type::Decimal *)base) + pos;
3237
/* as far as 'item' is constant, we can store reference on my_decimal */
3238
my_decimal *dec= ((my_decimal *)base) + pos;
3275
3239
dec->len= DECIMAL_BUFF_LENGTH;
3276
3240
dec->fix_buffer_pointer();
3277
type::Decimal *res= item->val_decimal(dec);
3241
my_decimal *res= item->val_decimal(dec);
3278
3242
/* if item->val_decimal() is evaluated to NULL then res == 0 */
3279
3243
if (!item->null_value && res != dec)
3280
class_decimal2decimal(res, dec);
3244
my_decimal2decimal(res, dec);
3284
3248
unsigned char *in_decimal::get_value(Item *item)
3286
type::Decimal *result= item->val_decimal(&val);
3250
my_decimal *result= item->val_decimal(&val);
3287
3251
if (item->null_value)
3289
3253
return (unsigned char *)result;
3443
3405
void cmp_item_decimal::store_value(Item *item)
3445
type::Decimal *val= item->val_decimal(&value);
3407
my_decimal *val= item->val_decimal(&value);
3446
3408
/* val may be zero if item is nnull */
3447
3409
if (val && val != &value)
3448
class_decimal2decimal(val, &value);
3410
my_decimal2decimal(val, &value);
3452
3414
int cmp_item_decimal::cmp(Item *arg)
3454
type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3416
my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3455
3417
if (arg->null_value)
3457
return class_decimal_cmp(&value, tmp);
3419
return my_decimal_cmp(&value, tmp);
3461
3423
int cmp_item_decimal::compare(cmp_item *arg)
3463
3425
cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3464
return class_decimal_cmp(&value, &l_cmp->value);
3426
return my_decimal_cmp(&value, &l_cmp->value);
4911
4867
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4912
: item::function::Boolean(), const_item(0), eval_item(0), cond_false(0)
4868
: Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
4914
const_item_cache= false;
4870
const_item_cache= 0;
4915
4871
fields.push_back(f1);
4916
4872
fields.push_back(f2);
4919
4875
Item_equal::Item_equal(Item *c, Item_field *f)
4920
: item::function::Boolean(), eval_item(0), cond_false(0)
4876
: Item_bool_func(), eval_item(0), cond_false(0)
4922
const_item_cache= false;
4878
const_item_cache= 0;
4923
4879
fields.push_back(f);
4928
4884
Item_equal::Item_equal(Item_equal *item_equal)
4929
: item::function::Boolean(), eval_item(0), cond_false(0)
4885
: Item_bool_func(), eval_item(0), cond_false(0)
4931
const_item_cache= false;
4932
List<Item_field>::iterator li(item_equal->fields.begin());
4887
const_item_cache= 0;
4888
List_iterator_fast<Item_field> li(item_equal->fields);
4933
4889
Item_field *item;
4934
4890
while ((item= li++))