~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

  • Committer: Prafulla Tekawade
  • Date: 2010-07-13 16:07:35 UTC
  • mto: (1662.1.4 rollup)
  • mto: This revision was merged to the branch mainline in revision 1664.
  • Revision ID: prafulla_t@users.sourceforge.net-20100713160735-2fsdtrm3azayuyu1
This bug is simillar to mysql bug 36133
http://bugs.mysql.com/bug.php?id=36133

Taking changes from that fix.

  - The problem was that the range optimizer evaluated constant expressions, 
    and among them it would try to evaluate IN-subquery predicates slated for
    handling with materialization strategy. However, these predicates require
    that parent_join->setup_subquery_materialization() is invoked before one
    attempts to evaluate them.
  
  - Fixed by making the range optimizer not to evaluate expressions that have
    item->is_expensive() == TRUE (these are materialization subqueries and 
    stored function calls). This should also resolve the problem that EXPLAIN 
    may be too long. 
    This change cuts off some opportunities for range optimizer, but this is 
    the price we're willing to pay for separation of query optimization and
    execution. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2010 Brian Aker
5
4
 *  Copyright (C) 2008 MySQL
6
5
 *
7
6
 *  This program is free software; you can redistribute it and/or modify
23
22
 * @file This file implements the Field class and API
24
23
 */
25
24
 
26
 
#include <config.h>
 
25
#include "config.h"
27
26
#include <cstdio>
28
27
#include <errno.h>
29
28
#include <float.h>
30
 
#include <drizzled/sql_select.h>
31
 
#include <drizzled/error.h>
32
 
#include <drizzled/field/str.h>
33
 
#include <drizzled/field/num.h>
34
 
#include <drizzled/field/blob.h>
35
 
#include <drizzled/field/boolean.h>
36
 
#include <drizzled/field/enum.h>
37
 
#include <drizzled/field/null.h>
38
 
#include <drizzled/field/date.h>
39
 
#include <drizzled/field/decimal.h>
40
 
#include <drizzled/field/real.h>
41
 
#include <drizzled/field/double.h>
42
 
#include <drizzled/field/int32.h>
43
 
#include <drizzled/field/int64.h>
44
 
#include <drizzled/field/num.h>
45
 
#include <drizzled/field/time.h>
46
 
#include <drizzled/field/epoch.h>
47
 
#include <drizzled/field/datetime.h>
48
 
#include <drizzled/field/microtime.h>
49
 
#include <drizzled/field/varstring.h>
50
 
#include <drizzled/field/uuid.h>
51
 
#include <drizzled/time_functions.h>
52
 
#include <drizzled/internal/m_string.h>
53
 
#include <drizzled/table.h>
54
 
#include <drizzled/util/test.h>
55
 
#include <drizzled/session.h>
56
 
#include <drizzled/current_session.h>
57
 
#include <drizzled/display.h>
58
 
#include <drizzled/typelib.h>
 
29
#include "drizzled/sql_select.h"
 
30
#include "drizzled/error.h"
 
31
#include "drizzled/field/str.h"
 
32
#include "drizzled/field/num.h"
 
33
#include "drizzled/field/blob.h"
 
34
#include "drizzled/field/enum.h"
 
35
#include "drizzled/field/null.h"
 
36
#include "drizzled/field/date.h"
 
37
#include "drizzled/field/decimal.h"
 
38
#include "drizzled/field/real.h"
 
39
#include "drizzled/field/double.h"
 
40
#include "drizzled/field/long.h"
 
41
#include "drizzled/field/int64_t.h"
 
42
#include "drizzled/field/num.h"
 
43
#include "drizzled/field/timestamp.h"
 
44
#include "drizzled/field/datetime.h"
 
45
#include "drizzled/field/varstring.h"
 
46
#include "drizzled/time_functions.h"
 
47
#include "drizzled/internal/m_string.h"
59
48
 
60
49
namespace drizzled
61
50
{
65
54
*****************************************************************************/
66
55
 
67
56
static enum_field_types
68
 
field_types_merge_rules [enum_field_types_size][enum_field_types_size]=
 
57
field_types_merge_rules [DRIZZLE_TYPE_MAX+1][DRIZZLE_TYPE_MAX+1]=
69
58
{
70
59
  /* DRIZZLE_TYPE_LONG -> */
71
60
  {
91
80
    DRIZZLE_TYPE_VARCHAR,
92
81
    //DRIZZLE_TYPE_BLOB
93
82
    DRIZZLE_TYPE_BLOB,
94
 
    //DRIZZLE_TYPE_TIME
95
 
    DRIZZLE_TYPE_TIME,
96
 
    //DRIZZLE_TYPE_BOOLEAN
97
 
    DRIZZLE_TYPE_VARCHAR,
98
 
    //DRIZZLE_TYPE_UUID
99
 
    DRIZZLE_TYPE_VARCHAR,
100
 
    //DRIZZLE_TYPE_MICROTIME
101
 
    DRIZZLE_TYPE_VARCHAR,
102
83
  },
103
84
  /* DRIZZLE_TYPE_DOUBLE -> */
104
85
  {
124
105
    DRIZZLE_TYPE_VARCHAR,
125
106
    //DRIZZLE_TYPE_BLOB
126
107
    DRIZZLE_TYPE_BLOB,
127
 
    //DRIZZLE_TYPE_TIME
128
 
    DRIZZLE_TYPE_TIME,
129
 
    //DRIZZLE_TYPE_BOOLEAN
130
 
    DRIZZLE_TYPE_VARCHAR,
131
 
    //DRIZZLE_TYPE_UUID
132
 
    DRIZZLE_TYPE_VARCHAR,
133
 
    //DRIZZLE_TYPE_MICROTIME
134
 
    DRIZZLE_TYPE_VARCHAR,
135
108
  },
136
109
  /* DRIZZLE_TYPE_NULL -> */
137
110
  {
157
130
    DRIZZLE_TYPE_ENUM,
158
131
    //DRIZZLE_TYPE_BLOB
159
132
    DRIZZLE_TYPE_BLOB,
160
 
    //DRIZZLE_TYPE_TIME
161
 
    DRIZZLE_TYPE_TIME,
162
 
    //DRIZZLE_TYPE_BOOLEAN
163
 
    DRIZZLE_TYPE_BOOLEAN,
164
 
    //DRIZZLE_TYPE_UUID
165
 
    DRIZZLE_TYPE_UUID,
166
 
    //DRIZZLE_TYPE_MICROTIME
167
 
    DRIZZLE_TYPE_MICROTIME,
168
133
  },
169
134
  /* DRIZZLE_TYPE_TIMESTAMP -> */
170
135
  {
190
155
    DRIZZLE_TYPE_VARCHAR,
191
156
    //DRIZZLE_TYPE_BLOB
192
157
    DRIZZLE_TYPE_BLOB,
193
 
    //DRIZZLE_TYPE_TIME
194
 
    DRIZZLE_TYPE_TIME,
195
 
    //DRIZZLE_TYPE_BOOLEAN
196
 
    DRIZZLE_TYPE_VARCHAR,
197
 
    //DRIZZLE_TYPE_UUID
198
 
    DRIZZLE_TYPE_VARCHAR,
199
 
    //DRIZZLE_TYPE_MICROTIME
200
 
    DRIZZLE_TYPE_VARCHAR,
201
158
  },
202
159
  /* DRIZZLE_TYPE_LONGLONG -> */
203
160
  {
222
179
    DRIZZLE_TYPE_VARCHAR,
223
180
    //DRIZZLE_TYPE_BLOB
224
181
    DRIZZLE_TYPE_BLOB,
225
 
    //DRIZZLE_TYPE_TIME
226
 
    DRIZZLE_TYPE_TIME,
227
 
    //DRIZZLE_TYPE_BOOLEAN
228
 
    DRIZZLE_TYPE_VARCHAR,
229
 
    //DRIZZLE_TYPE_UUID
230
 
    DRIZZLE_TYPE_VARCHAR,
231
 
    //DRIZZLE_TYPE_MICROTIME
232
 
    DRIZZLE_TYPE_VARCHAR,
233
182
  },
234
183
  /* DRIZZLE_TYPE_DATETIME -> */
235
184
  {
255
204
    DRIZZLE_TYPE_VARCHAR,
256
205
    //DRIZZLE_TYPE_BLOB
257
206
    DRIZZLE_TYPE_BLOB,
258
 
    //DRIZZLE_TYPE_TIME
259
 
    DRIZZLE_TYPE_TIME,
260
 
    //DRIZZLE_TYPE_BOOLEAN
261
 
    DRIZZLE_TYPE_VARCHAR,
262
 
    //DRIZZLE_TYPE_UUID
263
 
    DRIZZLE_TYPE_VARCHAR,
264
 
    //DRIZZLE_TYPE_MICROTIME
265
 
    DRIZZLE_TYPE_VARCHAR,
266
207
  },
267
208
  /* DRIZZLE_TYPE_DATE -> */
268
209
  {
288
229
    DRIZZLE_TYPE_VARCHAR,
289
230
    //DRIZZLE_TYPE_BLOB
290
231
    DRIZZLE_TYPE_BLOB,
291
 
    //DRIZZLE_TYPE_TIME
292
 
    DRIZZLE_TYPE_TIME,
293
 
    //DRIZZLE_TYPE_BOOLEAN
294
 
    DRIZZLE_TYPE_VARCHAR,
295
 
    //DRIZZLE_TYPE_UUID
296
 
    DRIZZLE_TYPE_VARCHAR,
297
 
    //DRIZZLE_TYPE_MICROTIME
298
 
    DRIZZLE_TYPE_VARCHAR,
299
232
  },
300
233
  /* DRIZZLE_TYPE_VARCHAR -> */
301
234
  {
321
254
    DRIZZLE_TYPE_VARCHAR,
322
255
    //DRIZZLE_TYPE_BLOB
323
256
    DRIZZLE_TYPE_BLOB,
324
 
    //DRIZZLE_TYPE_TIME
325
 
    DRIZZLE_TYPE_TIME,
326
 
    //DRIZZLE_TYPE_BOOLEAN
327
 
    DRIZZLE_TYPE_VARCHAR,
328
 
    //DRIZZLE_TYPE_UUID
329
 
    DRIZZLE_TYPE_VARCHAR,
330
 
    //DRIZZLE_TYPE_MICROTIME
331
 
    DRIZZLE_TYPE_VARCHAR,
332
257
  },
333
258
  /* DRIZZLE_TYPE_DECIMAL -> */
334
259
  {
354
279
    DRIZZLE_TYPE_VARCHAR,
355
280
    //DRIZZLE_TYPE_BLOB
356
281
    DRIZZLE_TYPE_BLOB,
357
 
    //DRIZZLE_TYPE_TIME
358
 
    DRIZZLE_TYPE_TIME,
359
 
    //DRIZZLE_TYPE_BOOLEAN
360
 
    DRIZZLE_TYPE_VARCHAR,
361
 
    //DRIZZLE_TYPE_UUID
362
 
    DRIZZLE_TYPE_VARCHAR,
363
 
    //DRIZZLE_TYPE_MICROTIME
364
 
    DRIZZLE_TYPE_VARCHAR,
365
282
  },
366
283
  /* DRIZZLE_TYPE_ENUM -> */
367
284
  {
387
304
    DRIZZLE_TYPE_VARCHAR,
388
305
    //DRIZZLE_TYPE_BLOB
389
306
    DRIZZLE_TYPE_BLOB,
390
 
    //DRIZZLE_TYPE_TIME
391
 
    DRIZZLE_TYPE_TIME,
392
 
    //DRIZZLE_TYPE_BOOLEAN
393
 
    DRIZZLE_TYPE_VARCHAR,
394
 
    //DRIZZLE_TYPE_UUID
395
 
    DRIZZLE_TYPE_VARCHAR,
396
 
    //DRIZZLE_TYPE_MICROTIME
397
 
    DRIZZLE_TYPE_VARCHAR,
398
 
   },
 
307
  },
399
308
  /* DRIZZLE_TYPE_BLOB -> */
400
309
  {
401
310
    //DRIZZLE_TYPE_LONG
420
329
    DRIZZLE_TYPE_BLOB,
421
330
    //DRIZZLE_TYPE_BLOB
422
331
    DRIZZLE_TYPE_BLOB,
423
 
    //DRIZZLE_TYPE_TIME
424
 
    DRIZZLE_TYPE_TIME,
425
 
    //DRIZZLE_TYPE_BOOLEAN
426
 
    DRIZZLE_TYPE_VARCHAR,
427
 
    //DRIZZLE_TYPE_UUID
428
 
    DRIZZLE_TYPE_VARCHAR,
429
 
    //DRIZZLE_TYPE_MICROTIME
430
 
    DRIZZLE_TYPE_VARCHAR,
431
 
  },
432
 
  /* DRIZZLE_TYPE_TIME -> */
433
 
  {
434
 
    //DRIZZLE_TYPE_LONG
435
 
    DRIZZLE_TYPE_VARCHAR,
436
 
    //DRIZZLE_TYPE_DOUBLE
437
 
    DRIZZLE_TYPE_VARCHAR,
438
 
    //DRIZZLE_TYPE_NULL
439
 
    DRIZZLE_TYPE_TIME,
440
 
    //DRIZZLE_TYPE_TIMESTAMP
441
 
    DRIZZLE_TYPE_VARCHAR,
442
 
    //DRIZZLE_TYPE_LONGLONG
443
 
    DRIZZLE_TYPE_VARCHAR,
444
 
    //DRIZZLE_TYPE_DATETIME
445
 
    DRIZZLE_TYPE_VARCHAR,
446
 
    //DRIZZLE_TYPE_DATE
447
 
    DRIZZLE_TYPE_VARCHAR,
448
 
    //DRIZZLE_TYPE_VARCHAR
449
 
    DRIZZLE_TYPE_VARCHAR,
450
 
    //DRIZZLE_TYPE_DECIMAL
451
 
    DRIZZLE_TYPE_VARCHAR,
452
 
    //DRIZZLE_TYPE_VARCHAR,
453
 
    DRIZZLE_TYPE_VARCHAR,
454
 
    //DRIZZLE_TYPE_BLOB
455
 
    DRIZZLE_TYPE_BLOB,
456
 
    //DRIZZLE_TYPE_TIME
457
 
    DRIZZLE_TYPE_TIME,
458
 
    //DRIZZLE_TYPE_BOOLEAN
459
 
    DRIZZLE_TYPE_VARCHAR,
460
 
    //DRIZZLE_TYPE_UUID
461
 
    DRIZZLE_TYPE_UUID,
462
 
    //DRIZZLE_TYPE_MICROTIME
463
 
    DRIZZLE_TYPE_VARCHAR,
464
 
  },
465
 
  /* DRIZZLE_TYPE_BOOLEAN -> */
466
 
  {
467
 
    //DRIZZLE_TYPE_LONG
468
 
    DRIZZLE_TYPE_VARCHAR,
469
 
    //DRIZZLE_TYPE_DOUBLE
470
 
    DRIZZLE_TYPE_VARCHAR,
471
 
    //DRIZZLE_TYPE_NULL
472
 
    DRIZZLE_TYPE_BOOLEAN,
473
 
    //DRIZZLE_TYPE_TIMESTAMP
474
 
    DRIZZLE_TYPE_VARCHAR,
475
 
    //DRIZZLE_TYPE_LONGLONG
476
 
    DRIZZLE_TYPE_VARCHAR,
477
 
    //DRIZZLE_TYPE_DATETIME
478
 
    DRIZZLE_TYPE_VARCHAR,
479
 
    //DRIZZLE_TYPE_DATE
480
 
    DRIZZLE_TYPE_VARCHAR,
481
 
    //DRIZZLE_TYPE_VARCHAR
482
 
    DRIZZLE_TYPE_VARCHAR,
483
 
    //DRIZZLE_TYPE_DECIMAL
484
 
    DRIZZLE_TYPE_VARCHAR,
485
 
    //DRIZZLE_TYPE_VARCHAR,
486
 
    DRIZZLE_TYPE_VARCHAR,
487
 
    //DRIZZLE_TYPE_BLOB
488
 
    DRIZZLE_TYPE_BLOB,
489
 
    //DRIZZLE_TYPE_TIME
490
 
    DRIZZLE_TYPE_TIME,
491
 
    //DRIZZLE_TYPE_BOOLEAN
492
 
    DRIZZLE_TYPE_BOOLEAN,
493
 
    //DRIZZLE_TYPE_UUID
494
 
    DRIZZLE_TYPE_VARCHAR,
495
 
    //DRIZZLE_TYPE_MICROTIME
496
 
    DRIZZLE_TYPE_VARCHAR,
497
 
  },
498
 
  /* DRIZZLE_TYPE_UUID -> */
499
 
  {
500
 
    //DRIZZLE_TYPE_LONG
501
 
    DRIZZLE_TYPE_VARCHAR,
502
 
    //DRIZZLE_TYPE_DOUBLE
503
 
    DRIZZLE_TYPE_VARCHAR,
504
 
    //DRIZZLE_TYPE_NULL
505
 
    DRIZZLE_TYPE_UUID,
506
 
    //DRIZZLE_TYPE_TIMESTAMP
507
 
    DRIZZLE_TYPE_VARCHAR,
508
 
    //DRIZZLE_TYPE_LONGLONG
509
 
    DRIZZLE_TYPE_VARCHAR,
510
 
    //DRIZZLE_TYPE_DATETIME
511
 
    DRIZZLE_TYPE_VARCHAR,
512
 
    //DRIZZLE_TYPE_DATE
513
 
    DRIZZLE_TYPE_VARCHAR,
514
 
    //DRIZZLE_TYPE_VARCHAR
515
 
    DRIZZLE_TYPE_VARCHAR,
516
 
    //DRIZZLE_TYPE_DECIMAL
517
 
    DRIZZLE_TYPE_VARCHAR,
518
 
    //DRIZZLE_TYPE_VARCHAR,
519
 
    DRIZZLE_TYPE_VARCHAR,
520
 
    //DRIZZLE_TYPE_BLOB
521
 
    DRIZZLE_TYPE_BLOB,
522
 
    //DRIZZLE_TYPE_TIME
523
 
    DRIZZLE_TYPE_TIME,
524
 
    //DRIZZLE_TYPE_BOOLEAN
525
 
    DRIZZLE_TYPE_VARCHAR,
526
 
    //DRIZZLE_TYPE_UUID
527
 
    DRIZZLE_TYPE_UUID,
528
 
    //DRIZZLE_TYPE_MICROTIME
529
 
    DRIZZLE_TYPE_VARCHAR,
530
 
  },
531
 
  /* DRIZZLE_TYPE_MICROTIME -> */
532
 
  {
533
 
    //DRIZZLE_TYPE_LONG
534
 
    DRIZZLE_TYPE_VARCHAR,
535
 
    //DRIZZLE_TYPE_DOUBLE
536
 
    DRIZZLE_TYPE_VARCHAR,
537
 
    //DRIZZLE_TYPE_NULL
538
 
    DRIZZLE_TYPE_MICROTIME,
539
 
    //DRIZZLE_TYPE_TIMESTAMP
540
 
    DRIZZLE_TYPE_VARCHAR,
541
 
    //DRIZZLE_TYPE_LONGLONG
542
 
    DRIZZLE_TYPE_VARCHAR,
543
 
    //DRIZZLE_TYPE_DATETIME
544
 
    DRIZZLE_TYPE_VARCHAR,
545
 
    //DRIZZLE_TYPE_DATE
546
 
    DRIZZLE_TYPE_VARCHAR,
547
 
    //DRIZZLE_TYPE_VARCHAR
548
 
    DRIZZLE_TYPE_VARCHAR,
549
 
    //DRIZZLE_TYPE_DECIMAL
550
 
    DRIZZLE_TYPE_VARCHAR,
551
 
    //DRIZZLE_TYPE_VARCHAR,
552
 
    DRIZZLE_TYPE_VARCHAR,
553
 
    //DRIZZLE_TYPE_BLOB
554
 
    DRIZZLE_TYPE_BLOB,
555
 
    //DRIZZLE_TYPE_TIME
556
 
    DRIZZLE_TYPE_TIME,
557
 
    //DRIZZLE_TYPE_BOOLEAN
558
 
    DRIZZLE_TYPE_VARCHAR,
559
 
    //DRIZZLE_TYPE_UUID
560
 
    DRIZZLE_TYPE_UUID,
561
 
    //DRIZZLE_TYPE_MICROTIME
562
 
    DRIZZLE_TYPE_MICROTIME,
563
332
  },
564
333
};
565
334
 
566
 
static Item_result field_types_result_type [enum_field_types_size]=
 
335
static Item_result field_types_result_type [DRIZZLE_TYPE_MAX+1]=
567
336
{
568
337
  //DRIZZLE_TYPE_LONG
569
338
  INT_RESULT,
587
356
  STRING_RESULT,
588
357
  //DRIZZLE_TYPE_BLOB
589
358
  STRING_RESULT,
590
 
  //DRIZZLE_TYPE_TIME
591
 
  STRING_RESULT,
592
 
  //DRIZZLE_TYPE_BOOLEAN
593
 
  STRING_RESULT,
594
 
  //DRIZZLE_TYPE_UUID
595
 
  STRING_RESULT,
596
 
  //DRIZZLE_TYPE_MICROTIME
597
 
  STRING_RESULT,
598
359
};
599
360
 
600
361
bool test_if_important_data(const CHARSET_INFO * const cs, 
619
380
enum_field_types Field::field_type_merge(enum_field_types a,
620
381
                                         enum_field_types b)
621
382
{
622
 
  assert(a < enum_field_types_size);
623
 
  assert(b < enum_field_types_size);
 
383
  assert(a <= DRIZZLE_TYPE_MAX);
 
384
  assert(b <= DRIZZLE_TYPE_MAX);
624
385
  return field_types_merge_rules[a][b];
625
386
}
626
387
 
627
388
Item_result Field::result_merge_type(enum_field_types field_type)
628
389
{
629
 
  assert(field_type < enum_field_types_size);
 
390
  assert(field_type <= DRIZZLE_TYPE_MAX);
630
391
  return field_types_result_type[field_type];
631
392
}
632
393
 
677
438
 
678
439
void Field::set_default()
679
440
{
680
 
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->getInsertRecord());
 
441
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->record[0]);
681
442
  memcpy(ptr, ptr + l_offset, pack_length());
682
443
  if (null_ptr)
683
444
    *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
746
507
  return 0;
747
508
}
748
509
 
749
 
bool Field::is_null(ptrdiff_t row_offset) const
 
510
bool Field::is_null(ptrdiff_t row_offset)
750
511
{
751
512
  return null_ptr ?
752
513
    (null_ptr[row_offset] & null_bit ? true : false) :
753
514
    table->null_row;
754
515
}
755
516
 
756
 
bool Field::is_real_null(ptrdiff_t row_offset) const
 
517
bool Field::is_real_null(ptrdiff_t row_offset)
757
518
{
758
519
  return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
759
520
}
760
521
 
761
 
bool Field::is_null_in_record(const unsigned char *record) const
 
522
bool Field::is_null_in_record(const unsigned char *record)
762
523
{
763
524
  if (! null_ptr)
764
525
    return false;
765
 
  return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
 
526
  return test(record[(uint32_t) (null_ptr -table->record[0])] & null_bit);
766
527
}
767
528
 
768
 
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset) const
 
529
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset)
769
530
{
770
531
  if (! null_ptr)
771
532
    return false;
784
545
    null_ptr[row_offset]&= (unsigned char) ~null_bit;
785
546
}
786
547
 
787
 
bool Field::maybe_null(void) const
 
548
bool Field::maybe_null(void)
788
549
{
789
550
  return null_ptr != 0 || table->maybe_null;
790
551
}
791
552
 
792
 
bool Field::real_maybe_null(void) const
 
553
bool Field::real_maybe_null(void)
793
554
{
794
555
  return null_ptr != 0;
795
556
}
823
584
void Field::init(Table *table_arg)
824
585
{
825
586
  orig_table= table= table_arg;
 
587
  table_name= &table_arg->alias;
826
588
}
827
589
 
828
590
/// This is used as a table name when the table structure is not set up
831
593
             unsigned char *null_ptr_arg,
832
594
             unsigned char null_bit_arg,
833
595
             utype unireg_check_arg, 
834
 
             const char *field_name_arg) :
 
596
             const char *field_name_arg)
 
597
  :
835
598
    ptr(ptr_arg),
836
599
    null_ptr(null_ptr_arg),
837
600
    table(NULL),
838
601
    orig_table(NULL),
 
602
    table_name(NULL),
839
603
    field_name(field_name_arg),
840
 
    comment(NULL_LEX_STRING),
841
604
    key_start(0),
842
605
    part_of_key(0),
843
606
    part_of_key_not_clustered(0),
844
607
    part_of_sortkey(0),
845
608
    unireg_check(unireg_check_arg),
846
609
    field_length(length_arg),
847
 
    flags(null_ptr ? 0: NOT_NULL_FLAG),
848
 
    field_index(0),
849
610
    null_bit(null_bit_arg),
850
611
    is_created_from_null_item(false)
851
612
{
 
613
  flags= null_ptr ? 0: NOT_NULL_FLAG;
 
614
  comment.str= (char*) "";
 
615
  comment.length= 0;
 
616
  field_index= 0;
852
617
}
853
618
 
854
 
void Field::hash(uint32_t *nr, uint32_t *nr2) const
 
619
void Field::hash(uint32_t *nr, uint32_t *nr2)
855
620
{
856
621
  if (is_null())
857
622
  {
877
642
  }
878
643
}
879
644
 
880
 
int Field::store_and_check(enum_check_fields check_level,
881
 
                           const char *to, 
882
 
                           uint32_t length,
883
 
                           const CHARSET_INFO * const cs)
884
 
 
 
645
int Field::store(const char *to, 
 
646
                 uint32_t length,
 
647
                 const CHARSET_INFO * const cs,
 
648
                 enum_check_fields check_level)
885
649
{
886
650
  int res;
887
651
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
943
707
  return(result);
944
708
}
945
709
 
946
 
type::Decimal *Field::val_decimal(type::Decimal *) const
 
710
my_decimal *Field::val_decimal(my_decimal *)
947
711
{
948
712
  /* This never have to be called */
949
713
  assert(0);
955
719
{
956
720
  if (orig_table && orig_table->getShare()->getSchemaName() && *orig_table->getShare()->getSchemaName())
957
721
  {
958
 
    field->db_name= orig_table->getShare()->getSchemaName();
959
 
    field->org_table_name= orig_table->getShare()->getTableName();
 
722
    field->db_name= orig_table->getMutableShare()->getSchemaName();
 
723
    field->org_table_name= orig_table->getMutableShare()->getTableName();
960
724
  }
961
725
  else
962
726
    field->org_table_name= field->db_name= "";
963
727
  if (orig_table)
964
728
  {
965
 
    field->table_name= orig_table->getAlias();
 
729
    field->table_name= orig_table->alias;
966
730
    field->org_col_name= field_name;
967
731
  }
968
732
  else
978
742
  field->decimals= 0;
979
743
}
980
744
 
981
 
int64_t Field::convert_decimal2int64_t(const type::Decimal *val, bool, int *err)
 
745
int64_t Field::convert_decimal2int64_t(const my_decimal *val, bool, int *err)
982
746
{
983
747
  int64_t i;
984
 
  if (warn_if_overflow(val->val_int32(E_DEC_ERROR &
 
748
  if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
985
749
                                      ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
986
 
                                      false, &i)))
 
750
                                      val, false, &i)))
987
751
  {
988
752
    i= (val->sign() ? INT64_MIN : INT64_MAX);
989
753
    *err= 1;
1001
765
  {
1002
766
    copy->blob_field=(Field_blob*) this;
1003
767
    copy->strip=0;
1004
 
    copy->length-= table->getShare()->sizeBlobPtr();
 
768
    copy->length-= table->getShare()->blob_ptr_size;
1005
769
    return copy->length;
1006
770
  }
1007
771
  else
1012
776
  return copy->length+ store_length;
1013
777
}
1014
778
 
1015
 
bool Field::get_date(type::Time &ltime, uint32_t fuzzydate) const
1016
 
{
1017
 
  char buff[type::Time::MAX_STRING_LENGTH];
1018
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1019
 
 
1020
 
  assert(getTable() and getTable()->getSession());
1021
 
 
1022
 
  if (not (res= val_str_internal(&tmp)) or
1023
 
      str_to_datetime_with_warn(getTable()->getSession(),
1024
 
                                res->ptr(), res->length(),
1025
 
                                &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
1026
 
  {
1027
 
    return true;
1028
 
  }
1029
 
 
1030
 
  return false;
1031
 
}
1032
 
 
1033
 
bool Field::get_time(type::Time &ltime) const
1034
 
{
1035
 
  char buff[type::Time::MAX_STRING_LENGTH];
1036
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1037
 
 
1038
 
  if (not (res= val_str_internal(&tmp)) or
1039
 
      str_to_time_with_warn(getTable()->getSession(), res->ptr(), res->length(), &ltime))
1040
 
  {
1041
 
    return true;
1042
 
  }
1043
 
 
1044
 
  return false;
1045
 
}
1046
 
 
1047
 
int Field::store_time(type::Time &ltime, type::timestamp_t)
1048
 
{
1049
 
  String tmp;
1050
 
 
1051
 
  ltime.convert(tmp);
1052
 
 
1053
 
  return store(tmp.ptr(), tmp.length(), &my_charset_bin);
 
779
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
780
{
 
781
  char buff[40];
 
782
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
783
  if (!(res=val_str(&tmp)) ||
 
784
      str_to_datetime_with_warn(res->ptr(), res->length(),
 
785
                                ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
786
    return 1;
 
787
  return 0;
 
788
}
 
789
 
 
790
bool Field::get_time(DRIZZLE_TIME *ltime)
 
791
{
 
792
  char buff[40];
 
793
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
794
  if (!(res=val_str(&tmp)) ||
 
795
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
 
796
    return 1;
 
797
  return 0;
 
798
}
 
799
 
 
800
int Field::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
 
801
{
 
802
  char buff[MAX_DATE_STRING_REP_LENGTH];
 
803
  uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
 
804
  return store(buff, length, &my_charset_bin);
1054
805
}
1055
806
 
1056
807
bool Field::optimize_range(uint32_t idx, uint32_t)
1097
848
  if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
1098
849
  {
1099
850
    tmp->init(new_table);
1100
 
    tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
 
851
    tmp->move_field_offset((ptrdiff_t) (new_table->record[0] -
1101
852
                                           new_table->getDefaultValues()));
1102
853
  }
1103
854
  return tmp;
1121
872
{
1122
873
  if (!Field::eq_def(field))
1123
874
    return 0;
1124
 
 
1125
875
  TYPELIB *from_lib=((Field_enum*) field)->typelib;
1126
876
 
1127
877
  if (typelib->count < from_lib->count)
1128
878
    return 0;
1129
 
 
1130
879
  for (uint32_t i=0 ; i < from_lib->count ; i++)
1131
 
  {
1132
880
    if (my_strnncoll(field_charset,
1133
881
                     (const unsigned char*)typelib->type_names[i],
1134
882
                     strlen(typelib->type_names[i]),
1135
883
                     (const unsigned char*)from_lib->type_names[i],
1136
884
                     strlen(from_lib->type_names[i])))
1137
885
      return 0;
1138
 
  }
1139
 
 
1140
886
  return 1;
1141
887
}
1142
888
 
1144
890
{
1145
891
  switch (type) {
1146
892
  case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1147
 
  case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
1148
 
  case DRIZZLE_TYPE_MICROTIME: return field::Microtime::max_string_length();
1149
 
  case DRIZZLE_TYPE_TIMESTAMP: return field::Epoch::max_string_length();
1150
 
  case DRIZZLE_TYPE_BOOLEAN: return field::Boolean::max_string_length();
1151
 
  case DRIZZLE_TYPE_DATE:
1152
 
  case DRIZZLE_TYPE_ENUM:
 
893
  case DRIZZLE_TYPE_DATE: return 3;
 
894
  case DRIZZLE_TYPE_TIMESTAMP:
1153
895
  case DRIZZLE_TYPE_LONG: return 4;
1154
896
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1155
 
  case DRIZZLE_TYPE_TIME:
1156
897
  case DRIZZLE_TYPE_DATETIME:
1157
898
  case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1158
899
  case DRIZZLE_TYPE_NULL: return 0;
1159
900
  case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
 
901
  case DRIZZLE_TYPE_ENUM:
1160
902
  case DRIZZLE_TYPE_DECIMAL:
1161
 
                          break;
 
903
    abort();
 
904
  default:
 
905
    return 0;
1162
906
  }
1163
 
 
1164
 
  assert(0);
1165
 
  abort();
1166
907
}
1167
908
 
1168
909
uint32_t pack_length_to_packflag(uint32_t type)
1171
912
    case 1: return 1 << FIELDFLAG_PACK_SHIFT;
1172
913
    case 2: assert(1);
1173
914
    case 3: assert(1);
1174
 
    case 4: return f_settype(DRIZZLE_TYPE_LONG);
1175
 
    case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
 
915
    case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
 
916
    case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
1176
917
  }
1177
918
  return 0;                                     // This shouldn't happen
1178
919
}
1182
923
*****************************************************************************/
1183
924
 
1184
925
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1185
 
                        drizzled::error_t code,
 
926
                        uint32_t code,
1186
927
                        int cuted_increment)
1187
928
{
1188
929
  /*
1202
943
 
1203
944
 
1204
945
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1205
 
                                 drizzled::error_t code,
 
946
                                 unsigned int code,
1206
947
                                 const char *str, 
1207
948
                                 uint32_t str_length,
1208
 
                                 type::timestamp_t ts_type, 
 
949
                                 enum enum_drizzle_timestamp_type ts_type, 
1209
950
                                 int cuted_increment)
1210
951
{
1211
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1212
 
 
1213
 
  if ((session->abortOnWarning() and
 
952
  Session *session= table ? table->in_use : current_session;
 
953
  if ((session->really_abort_on_warning() &&
1214
954
       level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
1215
955
      set_warning(level, code, cuted_increment))
1216
956
    make_truncated_value_warning(session, level, str, str_length, ts_type,
1218
958
}
1219
959
 
1220
960
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, 
1221
 
                                 drizzled::error_t code,
 
961
                                 uint32_t code,
1222
962
                                 int64_t nr, 
1223
 
                                 type::timestamp_t ts_type,
 
963
                                 enum enum_drizzle_timestamp_type ts_type,
1224
964
                                 int cuted_increment)
1225
965
{
1226
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1227
 
 
1228
 
  if (session->abortOnWarning() or
 
966
  Session *session= table ? table->in_use : current_session;
 
967
  if (session->really_abort_on_warning() ||
1229
968
      set_warning(level, code, cuted_increment))
1230
969
  {
1231
 
    char str_nr[DECIMAL_LONGLONG_DIGITS];
 
970
    char str_nr[22];
1232
971
    char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
1233
972
    make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1234
973
                                 ts_type, field_name);
1236
975
}
1237
976
 
1238
977
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1239
 
                                 const drizzled::error_t code,
 
978
                                 const uint32_t code,
1240
979
                                 double nr, 
1241
 
                                 type::timestamp_t ts_type)
 
980
                                 enum enum_drizzle_timestamp_type ts_type)
1242
981
{
1243
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1244
 
 
1245
 
  if (session->abortOnWarning() or
 
982
  Session *session= table ? table->in_use : current_session;
 
983
  if (session->really_abort_on_warning() ||
1246
984
      set_warning(level, code, 1))
1247
985
  {
1248
986
    /* DBL_DIG is enough to print '-[digits].E+###' */
1253
991
  }
1254
992
}
1255
993
 
1256
 
bool Field::isReadSet() const 
 
994
bool Field::isReadSet() 
1257
995
1258
996
  return table->isReadSet(field_index); 
1259
997
}
1279
1017
    table->clearWriteSet(field_index);
1280
1018
}
1281
1019
 
1282
 
void Field::pack_num(uint64_t arg, unsigned char *destination)
1283
 
{
1284
 
  if (not destination)
1285
 
    destination= ptr;
1286
 
 
1287
 
  int64_tstore(destination, arg);
1288
 
}
1289
 
 
1290
 
void Field::pack_num(uint32_t arg, unsigned char *destination)
1291
 
{
1292
 
  if (not destination)
1293
 
    destination= ptr;
1294
 
 
1295
 
  longstore(destination, arg);
1296
 
}
1297
 
 
1298
 
uint64_t Field::unpack_num(uint64_t &destination, const unsigned char *arg) const
1299
 
{
1300
 
  if (not arg)
1301
 
    arg= ptr;
1302
 
 
1303
 
  int64_tget(destination, arg);
1304
 
 
1305
 
  return destination;
1306
 
}
1307
 
 
1308
 
uint32_t Field::unpack_num(uint32_t &destination, const unsigned char *arg) const
1309
 
{
1310
 
  if (not arg)
1311
 
    arg= ptr;
1312
 
 
1313
 
  longget(destination, arg);
1314
 
 
1315
 
  return destination;
1316
 
}
1317
 
 
1318
 
std::ostream& operator<<(std::ostream& output, const Field &field)
1319
 
{
1320
 
  output << "Field:(";
1321
 
  output <<  field.field_name;
1322
 
  output << ", ";
1323
 
  output << drizzled::display::type(field.real_type());
1324
 
  output << ", { ";
1325
 
 
1326
 
  if (field.flags & NOT_NULL_FLAG)
1327
 
    output << " NOT_NULL";
1328
 
 
1329
 
  if (field.flags & PRI_KEY_FLAG)
1330
 
    output << ", PRIMARY KEY";
1331
 
 
1332
 
  if (field.flags & UNIQUE_KEY_FLAG)
1333
 
    output << ", UNIQUE KEY";
1334
 
 
1335
 
  if (field.flags & MULTIPLE_KEY_FLAG)
1336
 
    output << ", MULTIPLE KEY";
1337
 
 
1338
 
  if (field.flags & BLOB_FLAG)
1339
 
    output << ", BLOB";
1340
 
 
1341
 
  if (field.flags & UNSIGNED_FLAG)
1342
 
    output << ", UNSIGNED";
1343
 
 
1344
 
  if (field.flags & BINARY_FLAG)
1345
 
    output << ", BINARY";
1346
 
  output << "}, ";
1347
 
  output << ")";
1348
 
 
1349
 
  return output;  // for multiple << operators.
1350
 
}
1351
 
 
1352
1020
} /* namespace drizzled */