-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
item_cmpfunc.cc
executable file
·1625 lines (1390 loc) · 37.9 KB
/
item_cmpfunc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file
@brief
This file defines all compare functions
*/
#include "sql_priv.h"
#include <m_ctype.h>
#include "sql_parse.h" // check_stack_overrun
#include "sql_time.h" // make_truncated_value_warning
#include <algorithm>
using std::min;
using std::max;
/*
Compare row signature of two expressions
SYNOPSIS:
cmp_row_type()
item1 the first expression
item2 the second expression
DESCRIPTION
The function checks that two expressions have compatible row signatures
i.e. that the number of columns they return are the same and that if they
are both row expressions then each component from the first expression has
a row signature compatible with the signature of the corresponding component
of the second expression.
RETURN VALUES
1 type incompatibility has been detected
0 otherwise
*/
static int cmp_row_type(Item* item1, Item* item2)
{
uint n= item1->cols();
if (item2->check_cols(n))
return 1;
for (uint i=0; i<n; i++)
{
if (item2->element_index(i)->check_cols(item1->element_index(i)->cols()) ||
(item1->element_index(i)->result_type() == ROW_RESULT &&
cmp_row_type(item1->element_index(i), item2->element_index(i))))
return 1;
}
return 0;
}
Item_bool_func2* Eq_creator::create(Item *a, Item *b) const
{
return new Item_func_eq(a, b);
}
Item_bool_func2* Ne_creator::create(Item *a, Item *b) const
{
return new Item_func_ne(a, b);
}
Item_bool_func2* Gt_creator::create(Item *a, Item *b) const
{
return new Item_func_gt(a, b);
}
Item_bool_func2* Lt_creator::create(Item *a, Item *b) const
{
return new Item_func_lt(a, b);
}
Item_bool_func2* Ge_creator::create(Item *a, Item *b) const
{
return new Item_func_ge(a, b);
}
Item_bool_func2* Le_creator::create(Item *a, Item *b) const
{
return new Item_func_le(a, b);
}
/*
We put any NOT expression into parenthesis to avoid
possible problems with internal view representations where
any '!' is converted to NOT. It may cause a problem if
'!' is used in an expression together with other operators
whose precedence is lower than the precedence of '!' yet
higher than the precedence of NOT.
*/
void Item_func_not::print(String *str, enum_query_type query_type)
{
str->append('(');
Item_func::print(str, query_type);
str->append(')');
}
void Item_func_not_all::print(String *str, enum_query_type query_type)
{
if (show)
Item_func::print(str, query_type);
else
args[0]->print(str, query_type);
}
/**
Parse date provided in a string to a MYSQL_TIME.
@param[in] thd Thread handle
@param[in] str A string to convert
@param[in] warn_type Type of the timestamp for issuing the warning
@param[in] warn_name Field name for issuing the warning
@param[out] l_time The MYSQL_TIME objects is initialized.
Parses a date provided in the string str into a MYSQL_TIME object. If the
string contains an incorrect date or doesn't correspond to a date at all
then a warning is issued. The warn_type and the warn_name arguments are used
as the name and the type of the field when issuing the warning. If any input
was discarded (trailing or non-timestamp-y characters), return value will be
TRUE.
@return Status flag
@retval FALSE Success.
@retval True Indicates failure.
*/
bool get_mysql_time_from_str(THD *thd, String *str, timestamp_type warn_type,
const char *warn_name, MYSQL_TIME *l_time)
{
bool value;
MYSQL_TIME_STATUS status;
if (!str_to_datetime(str, l_time,
(TIME_FUZZY_DATE | MODE_INVALID_DATES |
(thd->variables.sql_mode &
(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE))), &status) &&
(l_time->time_type == MYSQL_TIMESTAMP_DATETIME ||
l_time->time_type == MYSQL_TIMESTAMP_DATE))
/*
Do not return yet, we may still want to throw a "trailing garbage"
warning.
*/
value= FALSE;
else
{
value= TRUE;
status.warnings= MYSQL_TIME_WARN_TRUNCATED; /* force warning */
}
if (status.warnings > 0)
make_truncated_value_warning(thd, Sql_condition::WARN_LEVEL_WARN,
ErrConvString(str), warn_type, warn_name);
return value;
}
/**
@brief Convert date provided in a string
to its packed temporal int representation.
@param[in] thd thread handle
@param[in] str a string to convert
@param[in] warn_type type of the timestamp for issuing the warning
@param[in] warn_name field name for issuing the warning
@param[out] error_arg could not extract a DATE or DATETIME
@details Convert date provided in the string str to the int
representation. If the string contains wrong date or doesn't
contain it at all then a warning is issued. The warn_type and
the warn_name arguments are used as the name and the type of the
field when issuing the warning.
@return
converted value. 0 on error and on zero-dates -- check 'failure'
*/
static ulonglong get_date_from_str(THD *thd, String *str,
timestamp_type warn_type,
const char *warn_name, bool *error_arg)
{
MYSQL_TIME l_time;
*error_arg= get_mysql_time_from_str(thd, str, warn_type, warn_name, &l_time);
if (*error_arg)
return 0;
return TIME_to_longlong_datetime_packed(&l_time);
}
/*
Retrieves correct DATETIME value from given item.
SYNOPSIS
get_datetime_value()
thd thread handle
item_arg [in/out] item to retrieve DATETIME value from
cache_arg [in/out] pointer to place to store the caching item to
warn_item [in] item for issuing the conversion warning
is_null [out] TRUE <=> the item_arg is null
DESCRIPTION
Retrieves the correct DATETIME value from given item for comparison by the
compare_datetime() function.
If item's result can be compared as longlong then its int value is used
and its string value is used otherwise. Strings are always parsed and
converted to int values by the get_date_from_str() function.
This allows us to compare correctly string dates with missed insignificant
zeros. If an item is a constant one then its value is cached and it isn't
get parsed again. An Item_cache_int object is used for caching values. It
seamlessly substitutes the original item. The cache item is marked as
non-constant to prevent re-caching it again. In order to compare
correctly DATE and DATETIME items the result of the former are treated as
a DATETIME with zero time (00:00:00).
RETURN
obtained value
*/
longlong
get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg,
Item *warn_item, bool *is_null)
{
longlong value= 0;
String buf, *str= 0;
Item *item= **item_arg;
if (item->is_temporal())
{
// value= item->val_date_temporal();
*is_null= item->null_value;
}
else
{
str= item->val_str(&buf);
*is_null= item->null_value;
}
if (*is_null)
return ~(ulonglong) 0;
/*
Convert strings to the integer DATE/DATETIME representation.
Even if both dates provided in strings we can't compare them directly as
strings as there is no warranty that they are correct and do not miss
some insignificant zeros.
*/
if (str)
{
bool error;
enum_field_types f_type= warn_item->field_type();
timestamp_type t_type= f_type ==
MYSQL_TYPE_DATE ? MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME;
value= (longlong) get_date_from_str(thd, str, t_type, warn_item->item_name.ptr(), &error);
/*
If str did not contain a valid date according to the current
SQL_MODE, get_date_from_str() has already thrown a warning,
and we don't want to throw NULL on invalid date (see 5.2.6
"SQL modes" in the manual), so we're done here.
*/
}
return value;
}
/*
Compare items values as dates.
SYNOPSIS
Arg_comparator::compare_datetime()
DESCRIPTION
Compare items values as DATE/DATETIME for both EQUAL_FUNC and from other
comparison functions. The correct DATETIME values are obtained
with help of the get_datetime_value() function.
RETURN
If is_nulls_eq is TRUE:
1 if items are equal or both are null
0 otherwise
If is_nulls_eq is FALSE:
-1 a < b or at least one item is null
0 a == b
1 a > b
See the table:
is_nulls_eq | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
a_is_null | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |
b_is_null | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 |
result | 1 | 0 | 0 |0/1|-1 |-1 |-1 |-1/0/1|
*/
int Arg_comparator::compare_datetime()
{
bool a_is_null, b_is_null;
longlong a_value, b_value;
/* Get DATE/DATETIME/TIME value of the 'a' item. */
a_value= (*get_value_a_func)(thd, &a, &a_cache, *b, &a_is_null);
if (!is_nulls_eq && a_is_null)
{
if (set_null)
owner->null_value= 1;
return -1;
}
/* Get DATE/DATETIME/TIME value of the 'b' item. */
b_value= (*get_value_b_func)(thd, &b, &b_cache, *a, &b_is_null);
if (a_is_null || b_is_null)
{
if (set_null)
owner->null_value= is_nulls_eq ? 0 : 1;
return is_nulls_eq ? (a_is_null == b_is_null) : -1;
}
/* Here we have two not-NULL values. */
if (set_null)
owner->null_value= 0;
/* Compare values. */
if (is_nulls_eq)
return (a_value == b_value);
return a_value < b_value ? -1 : (a_value > b_value ? 1 : 0);
}
int Arg_comparator::compare_string()
{
String *res1,*res2;
if ((res1= (*a)->val_str(&value1)))
{
if ((res2= (*b)->val_str(&value2)))
{
if (set_null)
owner->null_value= 0;
return sortcmp(res1,res2,cmp_collation.collation);
}
}
if (set_null)
owner->null_value= 1;
return -1;
}
/**
Compare strings byte by byte. End spaces are also compared.
@retval
<0 *a < *b
@retval
0 *b == *b
@retval
>0 *a > *b
*/
int Arg_comparator::compare_binary_string()
{
String *res1,*res2;
if ((res1= (*a)->val_str(&value1)))
{
if ((res2= (*b)->val_str(&value2)))
{
if (set_null)
owner->null_value= 0;
uint res1_length= res1->length();
uint res2_length= res2->length();
int cmp= memcmp(res1->ptr(), res2->ptr(), min(res1_length,res2_length));
return cmp ? cmp : (int) (res1_length - res2_length);
}
}
if (set_null)
owner->null_value= 1;
return -1;
}
/**
Compare strings, but take into account that NULL == NULL.
*/
int Arg_comparator::compare_e_string()
{
String *res1,*res2;
res1= (*a)->val_str(&value1);
res2= (*b)->val_str(&value2);
if (!res1 || !res2)
return MY_TEST(res1 == res2);
return MY_TEST(sortcmp(res1, res2, cmp_collation.collation) == 0);
}
int Arg_comparator::compare_e_binary_string()
{
String *res1,*res2;
res1= (*a)->val_str(&value1);
res2= (*b)->val_str(&value2);
if (!res1 || !res2)
return MY_TEST(res1 == res2);
return MY_TEST(stringcmp(res1, res2) == 0);
}
int Arg_comparator::compare_real()
{
/*
Fix yet another manifestation of Bug#2338. 'Volatile' will instruct
gcc to flush double values out of 80-bit Intel FPU registers before
performing the comparison.
*/
volatile double val1, val2;
val1= (*a)->val_real();
if (!(*a)->null_value)
{
val2= (*b)->val_real();
if (!(*b)->null_value)
{
if (set_null)
owner->null_value= 0;
if (val1 < val2) return -1;
if (val1 == val2) return 0;
return 1;
}
}
if (set_null)
owner->null_value= 1;
return -1;
}
int Arg_comparator::compare_decimal()
{
my_decimal decimal1;
my_decimal *val1= (*a)->val_decimal(&decimal1);
if (!(*a)->null_value)
{
my_decimal decimal2;
my_decimal *val2= (*b)->val_decimal(&decimal2);
if (!(*b)->null_value)
{
if (set_null)
owner->null_value= 0;
return my_decimal_cmp(val1, val2);
}
}
if (set_null)
owner->null_value= 1;
return -1;
}
int Arg_comparator::compare_e_real()
{
double val1= (*a)->val_real();
double val2= (*b)->val_real();
if ((*a)->null_value || (*b)->null_value)
return MY_TEST((*a)->null_value && (*b)->null_value);
return MY_TEST(val1 == val2);
}
int Arg_comparator::compare_e_decimal()
{
my_decimal decimal1, decimal2;
my_decimal *val1= (*a)->val_decimal(&decimal1);
my_decimal *val2= (*b)->val_decimal(&decimal2);
if ((*a)->null_value || (*b)->null_value)
return MY_TEST((*a)->null_value && (*b)->null_value);
return MY_TEST(my_decimal_cmp(val1, val2) == 0);
}
int Arg_comparator::compare_real_fixed()
{
/*
Fix yet another manifestation of Bug#2338. 'Volatile' will instruct
gcc to flush double values out of 80-bit Intel FPU registers before
performing the comparison.
*/
volatile double val1, val2;
val1= (*a)->val_real();
if (!(*a)->null_value)
{
val2= (*b)->val_real();
if (!(*b)->null_value)
{
if (set_null)
owner->null_value= 0;
if (val1 == val2 || fabs(val1 - val2) < precision)
return 0;
if (val1 < val2)
return -1;
return 1;
}
}
if (set_null)
owner->null_value= 1;
return -1;
}
int Arg_comparator::compare_e_real_fixed()
{
double val1= (*a)->val_real();
double val2= (*b)->val_real();
if ((*a)->null_value || (*b)->null_value)
return MY_TEST((*a)->null_value && (*b)->null_value);
return MY_TEST(val1 == val2 || fabs(val1 - val2) < precision);
}
int Arg_comparator::compare_int_signed()
{
longlong val1= (*a)->val_int();
if (!(*a)->null_value)
{
longlong val2= (*b)->val_int();
if (!(*b)->null_value)
{
if (set_null)
owner->null_value= 0;
if (val1 < val2) return -1;
if (val1 == val2) return 0;
return 1;
}
}
if (set_null)
owner->null_value= 1;
return -1;
}
/**
Compare values as BIGINT UNSIGNED.
*/
int Arg_comparator::compare_int_unsigned()
{
ulonglong val1= (*a)->val_int();
if (!(*a)->null_value)
{
ulonglong val2= (*b)->val_int();
if (!(*b)->null_value)
{
if (set_null)
owner->null_value= 0;
if (val1 < val2) return -1;
if (val1 == val2) return 0;
return 1;
}
}
if (set_null)
owner->null_value= 1;
return -1;
}
/**
Compare signed (*a) with unsigned (*B)
*/
int Arg_comparator::compare_int_signed_unsigned()
{
longlong sval1= (*a)->val_int();
if (!(*a)->null_value)
{
ulonglong uval2= (ulonglong)(*b)->val_int();
if (!(*b)->null_value)
{
if (set_null)
owner->null_value= 0;
if (sval1 < 0 || (ulonglong)sval1 < uval2)
return -1;
if ((ulonglong)sval1 == uval2)
return 0;
return 1;
}
}
if (set_null)
owner->null_value= 1;
return -1;
}
/**
Compare unsigned (*a) with signed (*B)
*/
int Arg_comparator::compare_int_unsigned_signed()
{
ulonglong uval1= (ulonglong)(*a)->val_int();
if (!(*a)->null_value)
{
longlong sval2= (*b)->val_int();
if (!(*b)->null_value)
{
if (set_null)
owner->null_value= 0;
if (sval2 < 0)
return 1;
if (uval1 < (ulonglong)sval2)
return -1;
if (uval1 == (ulonglong)sval2)
return 0;
return 1;
}
}
if (set_null)
owner->null_value= 1;
return -1;
}
int Arg_comparator::compare_e_int()
{
longlong val1= (*a)->val_int();
longlong val2= (*b)->val_int();
if ((*a)->null_value || (*b)->null_value)
return MY_TEST((*a)->null_value && (*b)->null_value);
return MY_TEST(val1 == val2);
}
/**
Compare unsigned *a with signed *b or signed *a with unsigned *b.
*/
int Arg_comparator::compare_e_int_diff_signedness()
{
longlong val1= (*a)->val_int();
longlong val2= (*b)->val_int();
if ((*a)->null_value || (*b)->null_value)
return MY_TEST((*a)->null_value && (*b)->null_value);
return (val1 >= 0) && MY_TEST(val1 == val2);
}
int Arg_comparator::compare_row()
{
int res= 0;
bool was_null= 0;
(*a)->bring_value();
(*b)->bring_value();
if ((*a)->null_value || (*b)->null_value)
{
owner->null_value= 1;
return -1;
}
uint n= (*a)->cols();
for (uint i= 0; i<n; i++)
{
res= comparators[i].compare();
/* Aggregate functions don't need special null handling. */
if (owner->null_value && owner->type() == Item::FUNC_ITEM)
{
// NULL was compared
switch (((Item_func*)owner)->functype()) {
case Item_func::NE_FUNC:
break; // NE never aborts on NULL even if abort_on_null is set
case Item_func::LT_FUNC:
case Item_func::LE_FUNC:
case Item_func::GT_FUNC:
case Item_func::GE_FUNC:
return -1; // <, <=, > and >= always fail on NULL
default: // EQ_FUNC
if (((Item_bool_func2*)owner)->abort_on_null)
return -1; // We do not need correct NULL returning
}
was_null= 1;
owner->null_value= 0;
res= 0; // continue comparison (maybe we will meet explicit difference)
}
else if (res)
return res;
}
if (was_null)
{
/*
There was NULL(s) in comparison in some parts, but there was no
explicit difference in other parts, so we have to return NULL.
*/
owner->null_value= 1;
return -1;
}
return 0;
}
int Arg_comparator::compare_e_row()
{
(*a)->bring_value();
(*b)->bring_value();
uint n= (*a)->cols();
for (uint i= 0; i<n; i++)
{
if (!comparators[i].compare())
return 0;
}
return 1;
}
void Item_func_truth::print(String *str, enum_query_type query_type)
{
str->append('(');
args[0]->print(str, query_type);
str->append(STRING_WITH_LEN(" is "));
if (! affirmative)
str->append(STRING_WITH_LEN("not "));
if (value)
str->append(STRING_WITH_LEN("true"));
else
str->append(STRING_WITH_LEN("false"));
str->append(')');
}
void Item_func_between::print(String *str, enum_query_type query_type)
{
str->append('(');
args[0]->print(str, query_type);
if (negated)
str->append(STRING_WITH_LEN(" not"));
str->append(STRING_WITH_LEN(" between "));
args[1]->print(str, query_type);
str->append(STRING_WITH_LEN(" and "));
args[2]->print(str, query_type);
str->append(')');
}
uint Item_func_if::decimal_precision() const
{
int arg1_prec= args[1]->decimal_int_part();
int arg2_prec= args[2]->decimal_int_part();
int precision=max(arg1_prec,arg2_prec) + decimals;
return min<uint>(precision, DECIMAL_MAX_PRECISION);
}
bool
Item_func_nullif::is_null()
{
return (null_value= (!cmp.compare() ? 1 : args[0]->null_value));
}
uint Item_func_case::decimal_precision() const
{
int max_int_part=0;
for (uint i=0 ; i < ncases ; i+=2)
set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
if (else_expr_num != -1)
set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
return min<uint>(max_int_part + decimals, DECIMAL_MAX_PRECISION);
}
/**
@todo
Fix this so that it prints the whole CASE expression
*/
void Item_func_case::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("(case "));
if (first_expr_num != -1)
{
args[first_expr_num]->print(str, query_type);
str->append(' ');
}
for (uint i=0 ; i < ncases ; i+=2)
{
str->append(STRING_WITH_LEN("when "));
args[i]->print(str, query_type);
str->append(STRING_WITH_LEN(" then "));
args[i+1]->print(str, query_type);
str->append(' ');
}
if (else_expr_num != -1)
{
str->append(STRING_WITH_LEN("else "));
args[else_expr_num]->print(str, query_type);
str->append(' ');
}
str->append(STRING_WITH_LEN("end)"));
}
/****************************************************************************
Classes and function for the IN operator
****************************************************************************/
/*
Determine which of the signed longlong arguments is bigger
SYNOPSIS
cmp_longs()
a_val left argument
b_val right argument
DESCRIPTION
This function will compare two signed longlong arguments
and will return -1, 0, or 1 if left argument is smaller than,
equal to or greater than the right argument.
RETURN VALUE
-1 left argument is smaller than the right argument.
0 left argument is equal to the right argument.
1 left argument is greater than the right argument.
*/
static inline int cmp_longs (longlong a_val, longlong b_val)
{
return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
}
/*
Determine which of the unsigned longlong arguments is bigger
SYNOPSIS
cmp_ulongs()
a_val left argument
b_val right argument
DESCRIPTION
This function will compare two unsigned longlong arguments
and will return -1, 0, or 1 if left argument is smaller than,
equal to or greater than the right argument.
RETURN VALUE
-1 left argument is smaller than the right argument.
0 left argument is equal to the right argument.
1 left argument is greater than the right argument.
*/
static inline int cmp_ulongs (ulonglong a_val, ulonglong b_val)
{
return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
}
/*
Compare two integers in IN value list format (packed_longlong)
SYNOPSIS
cmp_longlong()
cmp_arg an argument passed to the calling function (my_qsort2)
a left argument
b right argument
DESCRIPTION
This function will compare two integer arguments in the IN value list
format and will return -1, 0, or 1 if left argument is smaller than,
equal to or greater than the right argument.
It's used in sorting the IN values list and finding an element in it.
Depending on the signedness of the arguments cmp_longlong() will
compare them as either signed (using cmp_longs()) or unsigned (using
cmp_ulongs()).
RETURN VALUE
-1 left argument is smaller than the right argument.
0 left argument is equal to the right argument.
1 left argument is greater than the right argument.
*/
int cmp_longlong(void *cmp_arg,
in_longlong::packed_longlong *a,
in_longlong::packed_longlong *b)
{
if (a->unsigned_flag != b->unsigned_flag)
{
/*
One of the args is unsigned and is too big to fit into the
positive signed range. Report no match.
*/
if ((a->unsigned_flag && ((ulonglong) a->val) > (ulonglong) LONGLONG_MAX) ||
(b->unsigned_flag && ((ulonglong) b->val) > (ulonglong) LONGLONG_MAX))
return a->unsigned_flag ? 1 : -1;
/*
Although the signedness differs both args can fit into the signed
positive range. Make them signed and compare as usual.
*/
return cmp_longs (a->val, b->val);
}
if (a->unsigned_flag)
return cmp_ulongs ((ulonglong) a->val, (ulonglong) b->val);
else
return cmp_longs (a->val, b->val);
}
int in_vector::find(Item *item)
{
uchar *result=get_value(item);
if (!result || !used_count)
return 0; // Null value
uint start,end;
start=0; end=used_count-1;
while (start != end)
{
uint mid=(start+end+1)/2;
int res;
if ((res=(*compare)(collation, base+mid*size, result)) == 0)
return 1;
if (res < 0)
start=mid;
else
end=mid-1;
}
return (int) ((*compare)(collation, base+start*size, result) == 0);
}
in_string::in_string(uint elements,qsort2_cmp cmp_func,
const CHARSET_INFO *cs)
:in_vector(elements, sizeof(String), cmp_func, cs),
tmp(buff, sizeof(buff), &my_charset_bin)
{}
in_string::~in_string()
{
if (base)
{
// base was allocated with help of sql_alloc => following is OK
for (uint i=0 ; i < count ; i++)
((String*) base)[i].free();
}
}
cmp_item_row::~cmp_item_row()
{
DBUG_ENTER("~cmp_item_row");
DBUG_PRINT("enter",("this: 0x%lx", (long) this));
if (comparators)
{
for (uint i= 0; i < n; i++)
{
if (comparators[i])
delete comparators[i];
}
}
DBUG_VOID_RETURN;
}
void cmp_item_row::alloc_comparators()
{
if (!comparators)
comparators= (cmp_item **) current_thd->calloc(sizeof(cmp_item *)*n);
}
int cmp_item_row::cmp(Item *arg)
{
arg->null_value= 0;
if (arg->cols() != n)
{
my_error(ER_OPERAND_COLUMNS, MYF(0), n);
return 1;
}
bool was_null= 0;
arg->bring_value();
for (uint i=0; i < n; i++)
{
if (comparators[i]->cmp(arg->element_index(i)))
{
if (!arg->element_index(i)->null_value)
return 1;
was_null= 1;
}
}
return (arg->null_value= was_null);
}
int cmp_item_row::compare(cmp_item *c)
{
cmp_item_row *l_cmp= (cmp_item_row *) c;
for (uint i=0; i < n; i++)
{
int res;
if ((res= comparators[i]->compare(l_cmp->comparators[i])))
return res;