~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/eval/eval0eval.c

  • Committer: Monty Taylor
  • Date: 2009-03-08 23:45:12 UTC
  • mto: (923.2.1 mordred)
  • mto: This revision was merged to the branch mainline in revision 921.
  • Revision ID: mordred@inaugust.com-20090308234512-tqkygxtu1iaig23s
Removed C99 isnan() usage, which allows us to remove the util/math.{cc,h} workarounds. Yay for standards!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (C) 1997, 2009, Innobase Oy. All Rights Reserved.
4
 
 
5
 
This program is free software; you can redistribute it and/or modify it under
6
 
the terms of the GNU General Public License as published by the Free Software
7
 
Foundation; version 2 of the License.
8
 
 
9
 
This program is distributed in the hope that it will be useful, but WITHOUT
10
 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
 
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
 
 
13
 
You should have received a copy of the GNU General Public License along with
14
 
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
 
St, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
*****************************************************************************/
18
 
 
19
 
/**************************************************//**
20
 
@file eval/eval0eval.c
 
1
/******************************************************
21
2
SQL evaluator: evaluates simple data structures, like expressions, in
22
3
a query graph
23
4
 
 
5
(c) 1997 Innobase Oy
 
6
 
24
7
Created 12/29/1997 Heikki Tuuri
25
8
*******************************************************/
26
9
 
33
16
#include "data0data.h"
34
17
#include "row0sel.h"
35
18
 
36
 
/** The RND function seed */
 
19
/* The RND function seed */
37
20
static ulint    eval_rnd        = 128367121;
38
21
 
39
 
/** Dummy adress used when we should allocate a buffer of size 0 in
40
 
eval_node_alloc_val_buf */
 
22
/* Dummy adress used when we should allocate a buffer of size 0 in
 
23
the function below */
41
24
 
42
25
static byte     eval_dummy;
43
26
 
44
 
/*****************************************************************//**
 
27
/*********************************************************************
45
28
Allocate a buffer from global dynamic memory for a value of a que_node.
46
29
NOTE that this memory must be explicitly freed when the query graph is
47
30
freed. If the node already has an allocated buffer, that buffer is freed
48
31
here. NOTE that this is the only function where dynamic memory should be
49
 
allocated for a query node val field.
50
 
@return pointer to allocated buffer */
 
32
allocated for a query node val field. */
51
33
UNIV_INTERN
52
34
byte*
53
35
eval_node_alloc_val_buf(
54
36
/*====================*/
55
 
        que_node_t*     node,   /*!< in: query graph node; sets the val field
 
37
                                /* out: pointer to allocated buffer */
 
38
        que_node_t*     node,   /* in: query graph node; sets the val field
56
39
                                data field to point to the new buffer, and
57
40
                                len field equal to size */
58
 
        ulint           size)   /*!< in: buffer size */
 
41
        ulint           size)   /* in: buffer size */
59
42
{
60
43
        dfield_t*       dfield;
61
44
        byte*           data;
65
48
 
66
49
        dfield = que_node_get_val(node);
67
50
 
68
 
        data = static_cast<unsigned char *>(dfield_get_data(dfield));
 
51
        data = dfield_get_data(dfield);
69
52
 
70
53
        if (data && data != &eval_dummy) {
71
54
                mem_free(data);
74
57
        if (size == 0) {
75
58
                data = &eval_dummy;
76
59
        } else {
77
 
                data = static_cast<unsigned char *>(mem_alloc(size));
 
60
                data = mem_alloc(size);
78
61
        }
79
62
 
80
63
        que_node_set_val_buf_size(node, size);
84
67
        return(data);
85
68
}
86
69
 
87
 
/*****************************************************************//**
 
70
/*********************************************************************
88
71
Free the buffer from global dynamic memory for a value of a que_node,
89
72
if it has been allocated in the above function. The freeing for pushed
90
73
column values is done in sel_col_prefetch_buf_free. */
92
75
void
93
76
eval_node_free_val_buf(
94
77
/*===================*/
95
 
        que_node_t*     node)   /*!< in: query graph node */
 
78
        que_node_t*     node)   /* in: query graph node */
96
79
{
97
80
        dfield_t*       dfield;
98
81
        byte*           data;
102
85
 
103
86
        dfield = que_node_get_val(node);
104
87
 
105
 
        data = static_cast<unsigned char *>(dfield_get_data(dfield));
 
88
        data = dfield_get_data(dfield);
106
89
 
107
90
        if (que_node_get_val_buf_size(node) > 0) {
108
91
                ut_a(data);
111
94
        }
112
95
}
113
96
 
114
 
/*****************************************************************//**
115
 
Evaluates a comparison node.
116
 
@return the result of the comparison */
 
97
/*********************************************************************
 
98
Evaluates a comparison node. */
117
99
UNIV_INTERN
118
100
ibool
119
101
eval_cmp(
120
102
/*=====*/
121
 
        func_node_t*    cmp_node)       /*!< in: comparison node */
 
103
                                        /* out: the result of the comparison */
 
104
        func_node_t*    cmp_node)       /* in: comparison node */
122
105
{
123
106
        que_node_t*     arg1;
124
107
        que_node_t*     arg2;
170
153
        return(val);
171
154
}
172
155
 
173
 
/*****************************************************************//**
 
156
/*********************************************************************
174
157
Evaluates a logical operation node. */
175
158
UNIV_INLINE
176
159
void
177
160
eval_logical(
178
161
/*=========*/
179
 
        func_node_t*    logical_node)   /*!< in: logical operation node */
 
162
        func_node_t*    logical_node)   /* in: logical operation node */
180
163
{
181
164
        que_node_t*     arg1;
182
165
        que_node_t*     arg2;
211
194
        eval_node_set_ibool_val(logical_node, val);
212
195
}
213
196
 
214
 
/*****************************************************************//**
 
197
/*********************************************************************
215
198
Evaluates an arithmetic operation node. */
216
199
UNIV_INLINE
217
200
void
218
201
eval_arith(
219
202
/*=======*/
220
 
        func_node_t*    arith_node)     /*!< in: arithmetic operation node */
 
203
        func_node_t*    arith_node)     /* in: arithmetic operation node */
221
204
{
222
205
        que_node_t*     arg1;
223
206
        que_node_t*     arg2;
255
238
        eval_node_set_int_val(arith_node, val);
256
239
}
257
240
 
258
 
/*****************************************************************//**
 
241
/*********************************************************************
259
242
Evaluates an aggregate operation node. */
260
243
UNIV_INLINE
261
244
void
262
245
eval_aggregate(
263
246
/*===========*/
264
 
        func_node_t*    node)   /*!< in: aggregate operation node */
 
247
        func_node_t*    node)   /* in: aggregate operation node */
265
248
{
266
249
        que_node_t*     arg;
267
250
        lint            val;
289
272
        eval_node_set_int_val(node, val);
290
273
}
291
274
 
292
 
/*****************************************************************//**
 
275
/*********************************************************************
293
276
Evaluates a predefined function node where the function is not relevant
294
277
in benchmarks. */
295
278
static
296
279
void
297
280
eval_predefined_2(
298
281
/*==============*/
299
 
        func_node_t*    func_node)      /*!< in: predefined function node */
 
282
        func_node_t*    func_node)      /* in: predefined function node */
300
283
{
301
284
        que_node_t*     arg;
302
285
        que_node_t*     arg1;
376
359
        }
377
360
}
378
361
 
379
 
/*****************************************************************//**
 
362
/*********************************************************************
380
363
Evaluates a notfound-function node. */
381
364
UNIV_INLINE
382
365
void
383
366
eval_notfound(
384
367
/*==========*/
385
 
        func_node_t*    func_node)      /*!< in: function node */
 
368
        func_node_t*    func_node)      /* in: function node */
386
369
{
 
370
        que_node_t*     arg1;
 
371
        que_node_t*     arg2;
387
372
        sym_node_t*     cursor;
388
373
        sel_node_t*     sel_node;
389
374
        ibool           ibool_val;
390
375
 
 
376
        arg1 = func_node->args;
 
377
        arg2 = que_node_get_next(arg1);
 
378
 
391
379
        ut_ad(func_node->func == PARS_NOTFOUND_TOKEN);
392
380
 
393
 
        cursor = static_cast<sym_node_t *>(func_node->args);
 
381
        cursor = arg1;
394
382
 
395
383
        ut_ad(que_node_get_type(cursor) == QUE_NODE_SYMBOL);
396
384
 
413
401
        eval_node_set_ibool_val(func_node, ibool_val);
414
402
}
415
403
 
416
 
/*****************************************************************//**
 
404
/*********************************************************************
417
405
Evaluates a substr-function node. */
418
406
UNIV_INLINE
419
407
void
420
408
eval_substr(
421
409
/*========*/
422
 
        func_node_t*    func_node)      /*!< in: function node */
 
410
        func_node_t*    func_node)      /* in: function node */
423
411
{
424
412
        que_node_t*     arg1;
425
413
        que_node_t*     arg2;
436
424
 
437
425
        arg3 = que_node_get_next(arg2);
438
426
 
439
 
        str1 = static_cast<unsigned char *>(dfield_get_data(que_node_get_val(arg1)));
 
427
        str1 = dfield_get_data(que_node_get_val(arg1));
440
428
 
441
429
        len1 = (ulint)eval_node_get_int_val(arg2);
442
430
        len2 = (ulint)eval_node_get_int_val(arg3);
446
434
        dfield_set_data(dfield, str1 + len1, len2);
447
435
}
448
436
 
449
 
/*****************************************************************//**
 
437
/*********************************************************************
450
438
Evaluates a replstr-procedure node. */
451
439
static
452
440
void
453
441
eval_replstr(
454
442
/*=========*/
455
 
        func_node_t*    func_node)      /*!< in: function node */
 
443
        func_node_t*    func_node)      /* in: function node */
456
444
{
457
445
        que_node_t*     arg1;
458
446
        que_node_t*     arg2;
471
459
        arg3 = que_node_get_next(arg2);
472
460
        arg4 = que_node_get_next(arg3);
473
461
 
474
 
        str1 = static_cast<unsigned char *>(dfield_get_data(que_node_get_val(arg1)));
475
 
        str2 = static_cast<unsigned char *>(dfield_get_data(que_node_get_val(arg2)));
 
462
        str1 = dfield_get_data(que_node_get_val(arg1));
 
463
        str2 = dfield_get_data(que_node_get_val(arg2));
476
464
 
477
465
        len1 = (ulint)eval_node_get_int_val(arg3);
478
466
        len2 = (ulint)eval_node_get_int_val(arg4);
486
474
        ut_memcpy(str1 + len1, str2, len2);
487
475
}
488
476
 
489
 
/*****************************************************************//**
 
477
/*********************************************************************
490
478
Evaluates an instr-function node. */
491
479
static
492
480
void
493
481
eval_instr(
494
482
/*=======*/
495
 
        func_node_t*    func_node)      /*!< in: function node */
 
483
        func_node_t*    func_node)      /* in: function node */
496
484
{
497
485
        que_node_t*     arg1;
498
486
        que_node_t*     arg2;
513
501
        dfield1 = que_node_get_val(arg1);
514
502
        dfield2 = que_node_get_val(arg2);
515
503
 
516
 
        str1 = static_cast<unsigned char *>(dfield_get_data(dfield1));
517
 
        str2 = static_cast<unsigned char *>(dfield_get_data(dfield2));
 
504
        str1 = dfield_get_data(dfield1);
 
505
        str2 = dfield_get_data(dfield2);
518
506
 
519
507
        len1 = dfield_get_len(dfield1);
520
508
        len2 = dfield_get_len(dfield2);
558
546
        eval_node_set_int_val(func_node, int_val);
559
547
}
560
548
 
561
 
/*****************************************************************//**
 
549
/*********************************************************************
562
550
Evaluates a predefined function node. */
563
551
UNIV_INLINE
564
552
void
565
553
eval_binary_to_number(
566
554
/*==================*/
567
 
        func_node_t*    func_node)      /*!< in: function node */
 
555
        func_node_t*    func_node)      /* in: function node */
568
556
{
569
557
        que_node_t*     arg1;
570
558
        dfield_t*       dfield;
577
565
 
578
566
        dfield = que_node_get_val(arg1);
579
567
 
580
 
        str1 = static_cast<unsigned char *>(dfield_get_data(dfield));
 
568
        str1 = dfield_get_data(dfield);
581
569
        len1 = dfield_get_len(dfield);
582
570
 
583
571
        if (len1 > 4) {
596
584
        eval_node_copy_and_alloc_val(func_node, str2, 4);
597
585
}
598
586
 
599
 
/*****************************************************************//**
 
587
/*********************************************************************
600
588
Evaluates a predefined function node. */
601
589
static
602
590
void
603
591
eval_concat(
604
592
/*========*/
605
 
        func_node_t*    func_node)      /*!< in: function node */
 
593
        func_node_t*    func_node)      /* in: function node */
606
594
{
607
595
        que_node_t*     arg;
608
596
        dfield_t*       dfield;
638
626
        }
639
627
}
640
628
 
641
 
/*****************************************************************//**
 
629
/*********************************************************************
642
630
Evaluates a predefined function node. If the first argument is an integer,
643
631
this function looks at the second argument which is the integer length in
644
632
bytes, and converts the integer to a VARCHAR.
648
636
void
649
637
eval_to_binary(
650
638
/*===========*/
651
 
        func_node_t*    func_node)      /*!< in: function node */
 
639
        func_node_t*    func_node)      /* in: function node */
652
640
{
653
641
        que_node_t*     arg1;
654
642
        que_node_t*     arg2;
659
647
 
660
648
        arg1 = func_node->args;
661
649
 
662
 
        str1 = static_cast<unsigned char *>(dfield_get_data(que_node_get_val(arg1)));
 
650
        str1 = dfield_get_data(que_node_get_val(arg1));
663
651
 
664
652
        if (dtype_get_mtype(que_node_get_data_type(arg1)) != DATA_INT) {
665
653
 
686
674
        dfield_set_data(dfield, str1 + (4 - len1), len1);
687
675
}
688
676
 
689
 
/*****************************************************************//**
 
677
/*********************************************************************
690
678
Evaluates a predefined function node. */
691
679
UNIV_INLINE
692
680
void
693
681
eval_predefined(
694
682
/*============*/
695
 
        func_node_t*    func_node)      /*!< in: function node */
 
683
        func_node_t*    func_node)      /* in: function node */
696
684
{
697
685
        que_node_t*     arg1;
698
686
        lint            int_val;
778
766
        eval_node_set_int_val(func_node, int_val);
779
767
}
780
768
 
781
 
/*****************************************************************//**
 
769
/*********************************************************************
782
770
Evaluates a function node. */
783
771
UNIV_INTERN
784
772
void
785
773
eval_func(
786
774
/*======*/
787
 
        func_node_t*    func_node)      /*!< in: function node */
 
775
        func_node_t*    func_node)      /* in: function node */
788
776
{
789
777
        que_node_t*     arg;
790
 
        ulint           func_class;
 
778
        ulint           class;
791
779
        ulint           func;
792
780
 
793
781
        ut_ad(que_node_get_type(func_node) == QUE_NODE_FUNC);
794
782
 
795
 
        func_class = func_node->func_class;
 
783
        class = func_node->class;
796
784
        func = func_node->func;
797
785
 
798
786
        arg = func_node->args;
805
793
                values, except for eval_cmp and notfound */
806
794
 
807
795
                if (dfield_is_null(que_node_get_val(arg))
808
 
                    && (func_class != PARS_FUNC_CMP)
 
796
                    && (class != PARS_FUNC_CMP)
809
797
                    && (func != PARS_NOTFOUND_TOKEN)
810
798
                    && (func != PARS_PRINTF_TOKEN)) {
811
799
                        ut_error;
814
802
                arg = que_node_get_next(arg);
815
803
        }
816
804
 
817
 
        if (func_class == PARS_FUNC_CMP) {
 
805
        if (class == PARS_FUNC_CMP) {
818
806
                eval_cmp(func_node);
819
 
        } else if (func_class == PARS_FUNC_ARITH) {
 
807
        } else if (class == PARS_FUNC_ARITH) {
820
808
                eval_arith(func_node);
821
 
        } else if (func_class == PARS_FUNC_AGGREGATE) {
 
809
        } else if (class == PARS_FUNC_AGGREGATE) {
822
810
                eval_aggregate(func_node);
823
 
        } else if (func_class == PARS_FUNC_PREDEFINED) {
 
811
        } else if (class == PARS_FUNC_PREDEFINED) {
824
812
 
825
813
                if (func == PARS_NOTFOUND_TOKEN) {
826
814
                        eval_notfound(func_node);
840
828
                        eval_predefined(func_node);
841
829
                }
842
830
        } else {
843
 
                ut_ad(func_class == PARS_FUNC_LOGICAL);
 
831
                ut_ad(class == PARS_FUNC_LOGICAL);
844
832
 
845
833
                eval_logical(func_node);
846
834
        }