summaryrefslogtreecommitdiff
path: root/sql/sql_type_geom.cc
blob: 5732ae472175bcb0eba29b205d7bab819e120fb9 (plain)
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
/*
   Copyright (c) 2015, 2020, MariaDB

   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 Street, Fifth Floor, Boston, MA 02111-1301 USA */

#include "mariadb.h"

#ifdef HAVE_SPATIAL

#include "sql_class.h"
#include "sql_type_geom.h"
#include "item_geofunc.h"

Named_type_handler<Type_handler_geometry> type_handler_geometry("geometry");
Named_type_handler<Type_handler_point> type_handler_point("point");
Named_type_handler<Type_handler_linestring> type_handler_linestring("linestring");
Named_type_handler<Type_handler_polygon> type_handler_polygon("polygon");
Named_type_handler<Type_handler_multipoint> type_handler_multipoint("multipoint");
Named_type_handler<Type_handler_multilinestring> type_handler_multilinestring("multilinestring");
Named_type_handler<Type_handler_multipolygon> type_handler_multipolygon("multipolygon");
Named_type_handler<Type_handler_geometrycollection> type_handler_geometrycollection("geometrycollection");

Type_collection_geometry        type_collection_geometry;


LEX_CSTRING Type_handler_geometry::extended_metadata_data_type_name() const
{
  return geometry_type() == GEOM_GEOMETRY ? null_clex_str :
                                            name().lex_cstring();
}


const Type_handler_geometry *
Type_handler_geometry::type_handler_geom_by_type(uint type)
{
  switch (type) {
  case Type_handler_geometry::GEOM_POINT:
    return &type_handler_point;
  case Type_handler_geometry::GEOM_LINESTRING:
    return &type_handler_linestring;
  case Type_handler_geometry::GEOM_POLYGON:
    return &type_handler_polygon;
  case Type_handler_geometry::GEOM_MULTIPOINT:
    return &type_handler_multipoint;
  case Type_handler_geometry::GEOM_MULTILINESTRING:
    return &type_handler_multilinestring;
  case Type_handler_geometry::GEOM_MULTIPOLYGON:
    return &type_handler_multipolygon;
  case Type_handler_geometry::GEOM_GEOMETRYCOLLECTION:
    return &type_handler_geometrycollection;
  case Type_handler_geometry::GEOM_GEOMETRY:
    break;
  }
  return &type_handler_geometry;
}


const Type_handler *
Type_collection_geometry::handler_by_name(const LEX_CSTRING &name) const
{
  if (type_handler_point.name().eq(name))
    return &type_handler_point;
  if (type_handler_linestring.name().eq(name))
    return &type_handler_linestring;
  if (type_handler_polygon.name().eq(name))
    return &type_handler_polygon;
  if (type_handler_multipoint.name().eq(name))
    return &type_handler_multipoint;
  if (type_handler_multilinestring.name().eq(name))
    return &type_handler_multilinestring;
  if (type_handler_multipolygon.name().eq(name))
    return &type_handler_multipolygon;
  if (type_handler_geometry.name().eq(name))
    return &type_handler_geometry;
  if (type_handler_geometrycollection.name().eq(name))
    return &type_handler_geometrycollection;
  return NULL;
}


const Type_collection *Type_handler_geometry::type_collection() const
{
  return &type_collection_geometry;
}


const Type_handler *
Type_handler_geometry::type_handler_frm_unpack(const uchar *buffer) const
{
  // charset and geometry_type share the same byte in frm
  return type_handler_geom_by_type((uint) buffer[14]);
}


const Type_handler *
Type_collection_geometry::aggregate_for_comparison(const Type_handler *a,
                                                   const Type_handler *b)
                                                   const
{
  const Type_handler *h;
  if ((h= aggregate_common(a, b)) ||
      (h= aggregate_if_null(a, b)) ||
      (h= aggregate_if_long_blob(a, b)))
    return h;
  return NULL;
}


const Type_handler *
Type_collection_geometry::aggregate_for_result(const Type_handler *a,
                                               const Type_handler *b)
                                               const
{
  const Type_handler *h;
  if ((h= aggregate_common(a, b)) ||
      (h= aggregate_if_null(a, b)) ||
      (h= aggregate_if_long_blob(a, b)) ||
      (h= aggregate_if_string(a, b)))
    return h;
  return NULL;
}


const Type_handler *
Type_collection_geometry::aggregate_for_min_max(const Type_handler *a,
                                                const Type_handler *b)
                                                const
{
  const Type_handler *h;
  if ((h= aggregate_common(a, b)) ||
      (h= aggregate_if_null(a, b)) ||
      (h= aggregate_if_long_blob(a, b)) ||
      (h= aggregate_if_string(a, b)))
    return h;
  return NULL;
}


const Type_handler *
Type_collection_geometry::aggregate_if_string(const Type_handler *a,
                                              const Type_handler *b) const
{
  if (a->type_collection() == this)
  {
    DBUG_ASSERT(b->type_collection() != this);
    swap_variables(const Type_handler *, a, b);
  }
  if (a == &type_handler_hex_hybrid ||
      a == &type_handler_tiny_blob ||
      a == &type_handler_blob ||
      a == &type_handler_medium_blob ||
      a == &type_handler_varchar ||
      a == &type_handler_string)
    return &type_handler_long_blob;
  return NULL;
}


#ifndef DBUG_OFF
bool Type_collection_geometry::init_aggregators(Type_handler_data *data,
                                                const Type_handler *geom) const
{
  Type_aggregator *r= &data->m_type_aggregator_for_result;
  return
    r->add(geom, &type_handler_hex_hybrid,  &type_handler_long_blob) ||
    r->add(geom, &type_handler_tiny_blob,   &type_handler_long_blob) ||
    r->add(geom, &type_handler_blob,        &type_handler_long_blob) ||
    r->add(geom, &type_handler_medium_blob, &type_handler_long_blob) ||
    r->add(geom, &type_handler_varchar,     &type_handler_long_blob) ||
    r->add(geom, &type_handler_string,      &type_handler_long_blob);
}
#endif


bool Type_collection_geometry::init(Type_handler_data *data)
{
#ifndef DBUG_OFF
  Type_aggregator *nct= &data->m_type_aggregator_non_commutative_test;
  if (nct->add(&type_handler_point,
               &type_handler_varchar,
               &type_handler_long_blob))
    return true;
  return
    init_aggregators(data, &type_handler_geometry) ||
    init_aggregators(data, &type_handler_geometrycollection) ||
    init_aggregators(data, &type_handler_point) ||
    init_aggregators(data, &type_handler_linestring) ||
    init_aggregators(data, &type_handler_polygon) ||
    init_aggregators(data, &type_handler_multipoint) ||
    init_aggregators(data, &type_handler_multilinestring) ||
    init_aggregators(data, &type_handler_multipolygon);
#endif // DBUG_OFF
  return false;
}


bool Type_handler_geometry::
check_type_geom_or_binary(const LEX_CSTRING &opname,
                          const Item *item)
{
  const Type_handler *handler= item->type_handler();
  if (handler->type_handler_for_comparison() == &type_handler_geometry ||
      (handler->is_general_purpose_string_type() &&
       item->collation.collation == &my_charset_bin))
    return false;
  my_error(ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION, MYF(0),
           handler->name().ptr(), opname.str);
  return true;
}


bool Type_handler_geometry::
check_types_geom_or_binary(const LEX_CSTRING &opname,
                           Item* const *args,
                           uint start, uint end)
{
  for (uint i= start; i < end ; i++)
  {
    if (check_type_geom_or_binary(opname, args[i]))
      return true;
  }
  return false;
}


const Type_handler *Type_handler_geometry::type_handler_for_comparison() const
{
  return &type_handler_geometry;
}


Field *Type_handler_geometry::make_conversion_table_field(MEM_ROOT *root,
                                                          TABLE *table,
                                                          uint metadata,
                                                          const Field *target)
                                                          const
{
  DBUG_ASSERT(target->type() == MYSQL_TYPE_GEOMETRY);
  /*
    We do not do not update feature_gis statistics here:
    status_var_increment(target->table->in_use->status_var.feature_gis);
    as this is only a temporary field.
    The statistics was already incremented when "target" was created.
  */
  const Field_geom *fg= static_cast<const Field_geom*>(target);
  return new (root)
         Field_geom(NULL, (uchar *) "", 1, Field::NONE, &empty_clex_str,
                    table->s, 4, fg->type_handler_geom(), fg->srid);
}


bool Type_handler_geometry::
       Column_definition_fix_attributes(Column_definition *def) const
{
  def->flags|= BLOB_FLAG;
  return false;
}

void Type_handler_geometry::
       Column_definition_reuse_fix_attributes(THD *thd,
                                              Column_definition *def,
                                              const Field *field) const
{
  def->srid= ((Field_geom*) field)->srid;
}


bool Type_handler_geometry::
       Column_definition_prepare_stage1(THD *thd,
                                        MEM_ROOT *mem_root,
                                        Column_definition *def,
                                        handler *file,
                                        ulonglong table_flags,
                                        const Column_derived_attributes
                                              *derived_attr) const
{
  def->charset= &my_charset_bin;
  def->create_length_to_internal_length_string();
  return def->prepare_blob_field(thd);
}


bool Type_handler_geometry::
       Column_definition_prepare_stage2(Column_definition *def,
                                        handler *file,
                                        ulonglong table_flags) const
{
  if (!(table_flags & HA_CAN_GEOMETRY))
  {
    my_error(ER_CHECK_NOT_IMPLEMENTED, MYF(0), "GEOMETRY");
    return true;
  }
  return def->prepare_stage2_blob(file, table_flags, FIELDFLAG_GEOM);
}

bool Type_handler_geometry::Key_part_spec_init_primary(Key_part_spec *part,
                                              const Column_definition &def,
                                              const handler *file) const
{
  return part->check_primary_key_for_blob(file);
}


bool Type_handler_geometry::Key_part_spec_init_unique(Key_part_spec *part,
                                              const Column_definition &def,
                                              const handler *file,
                                              bool *hash_field_needed) const
{
  if (!part->length)
    *hash_field_needed= true;
  return part->check_key_for_blob(file);
}


bool Type_handler_geometry::Key_part_spec_init_multiple(Key_part_spec *part,
                                               const Column_definition &def,
                                               const handler *file) const
{
  return part->init_multiple_key_for_blob(file);
}


bool Type_handler_geometry::Key_part_spec_init_foreign(Key_part_spec *part,
                                               const Column_definition &def,
                                               const handler *file) const
{
  return part->check_foreign_key_for_blob(file);
}


bool Type_handler_geometry::Key_part_spec_init_spatial(Key_part_spec *part,
                                                  const Column_definition &def)
                                                  const
{
  if (part->length)
  {
    my_error(ER_WRONG_SUB_KEY, MYF(0));
    return true;
  }
  /*
    4 is: (Xmin,Xmax,Ymin,Ymax), this is for 2D case
    Lately we'll extend this code to support more dimensions
  */
  part->length= 4 * sizeof(double);
  return false;
}


Item *
Type_handler_geometry::create_typecast_item(THD *thd, Item *item,
                                           const Type_cast_attributes &attr)
                                           const
{
  DBUG_EXECUTE_IF("emulate_geometry_create_typecast_item",
    return new (thd->mem_root) Item_func_geometry_from_text(thd, item);
  );

  return NULL;
}

bool Type_handler_point::Key_part_spec_init_primary(Key_part_spec *part,
                                              const Column_definition &def,
                                              const handler *file) const
{
  /*
    QQ:
    The below assignment (here and in all other Key_part_spec_init_xxx methods)
    overrides the explicitly given key part length, so in this query:
      CREATE OR REPLACE TABLE t1 (a POINT, KEY(a(10)));
    the key becomes KEY(a(25)).
    This might be a bug.
  */
  part->length= octet_length();
  return part->check_key_for_blob(file);
}


bool Type_handler_point::Key_part_spec_init_unique(Key_part_spec *part,
                                              const Column_definition &def,
                                              const handler *file,
                                              bool *hash_field_needed) const
{
  part->length= octet_length();
  return part->check_key_for_blob(file);
}


bool Type_handler_point::Key_part_spec_init_multiple(Key_part_spec *part,
                                              const Column_definition &def,
                                              const handler *file) const
{
  part->length= octet_length();
  return part->check_key_for_blob(file);
}


bool Type_handler_point::Key_part_spec_init_foreign(Key_part_spec *part,
                                              const Column_definition &def,
                                              const handler *file) const
{
  part->length= octet_length();
  return part->check_key_for_blob(file);
}


Item *
Type_handler_point::make_constructor_item(THD *thd, List<Item> *args) const
{
  if (!args || args->elements != 2)
    return NULL;
  Item_args tmp(thd, *args);
  return new (thd->mem_root) Item_func_point(thd,
                                             tmp.arguments()[0],
                                             tmp.arguments()[1]);
}


Item *
Type_handler_linestring::make_constructor_item(THD *thd, List<Item> *args) const
{
  return args ? new (thd->mem_root) Item_func_linestring(thd, *args) : NULL;
}


Item *
Type_handler_polygon::make_constructor_item(THD *thd, List<Item> *args) const
{
  return args ? new (thd->mem_root) Item_func_polygon(thd, *args) : NULL;
}


Item *
Type_handler_multipoint::make_constructor_item(THD *thd, List<Item> *args) const
{
  return args ? new (thd->mem_root) Item_func_multipoint(thd, *args) : NULL;
}


Item *
Type_handler_multilinestring::make_constructor_item(THD *thd,
                                                    List<Item> *args) const
{
  return args ? new (thd->mem_root) Item_func_multilinestring(thd, *args) :
                NULL;
}


Item *
Type_handler_multipolygon::make_constructor_item(THD *thd,
                                                 List<Item> *args) const
{
  return args ? new (thd->mem_root) Item_func_multipolygon(thd, *args) : NULL;
}


Item *
Type_handler_geometrycollection::make_constructor_item(THD *thd,
                                                       List<Item> *args) const
{
  return args ? new (thd->mem_root) Item_func_geometrycollection(thd, *args) :
                NULL;
}


uint32 Type_handler_geometry::calc_pack_length(uint32 length) const
{
  return 4 + portable_sizeof_char_ptr;
}


Field *Type_handler_geometry::make_table_field(MEM_ROOT *root,
                                               const LEX_CSTRING *name,
                                               const Record_addr &addr,
                                               const Type_all_attributes &attr,
                                               TABLE_SHARE *share) const
{
  return new (root)
         Field_geom(addr.ptr(), addr.null_ptr(), addr.null_bit(),
                    Field::NONE, name, share, 4, this, 0);
}


bool Type_handler_geometry::
       Item_hybrid_func_fix_attributes(THD *thd,
                                       const LEX_CSTRING &func_name,
                                       Type_handler_hybrid_field_type *handler,
                                       Type_all_attributes *func,
                                       Item **items, uint nitems) const
{
  DBUG_ASSERT(nitems > 0);
  func->collation.set(&my_charset_bin);
  func->unsigned_flag= false;
  func->decimals= 0;
  func->max_length= (uint32) UINT_MAX32;
  func->set_type_maybe_null(true);
  return false;
}


bool Type_handler_geometry::
       Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
{
  LEX_CSTRING name= {STRING_WITH_LEN("sum") };
  return Item_func_or_sum_illegal_param(name);
}


bool Type_handler_geometry::
       Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
{
  LEX_CSTRING name= {STRING_WITH_LEN("avg") };
  return Item_func_or_sum_illegal_param(name);
}


bool Type_handler_geometry::
       Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
{
  return Item_func_or_sum_illegal_param(item);
}


bool Type_handler_geometry::
       Item_func_round_fix_length_and_dec(Item_func_round *item) const
{
  return Item_func_or_sum_illegal_param(item);
}


bool Type_handler_geometry::
       Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
{
  return Item_func_or_sum_illegal_param(item);
}


bool Type_handler_geometry::
       Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
{
  return Item_func_or_sum_illegal_param(item);
}


bool Type_handler_geometry::
       Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
{
  return Item_func_or_sum_illegal_param(item);
}



bool Type_handler_geometry::
       Item_func_signed_fix_length_and_dec(Item_func_signed *item) const
{
  return Item_func_or_sum_illegal_param(item);
}


bool Type_handler_geometry::
       Item_func_unsigned_fix_length_and_dec(Item_func_unsigned *item) const
{
  return Item_func_or_sum_illegal_param(item);
}


bool Type_handler_geometry::
       Item_double_typecast_fix_length_and_dec(Item_double_typecast *item) const
{
  return Item_func_or_sum_illegal_param(item);
}


bool Type_handler_geometry::
       Item_float_typecast_fix_length_and_dec(Item_float_typecast *item) const
{
  return Item_func_or_sum_illegal_param(item);
}


bool Type_handler_geometry::
       Item_decimal_typecast_fix_length_and_dec(Item_decimal_typecast *item) const
{
  return Item_func_or_sum_illegal_param(item);
}


bool Type_handler_geometry::
       Item_char_typecast_fix_length_and_dec(Item_char_typecast *item) const
{
  if (item->cast_charset() != &my_charset_bin)
    return Item_func_or_sum_illegal_param(item); // CAST(geom AS CHAR)
  item->fix_length_and_dec_str();
  return false; // CAST(geom AS BINARY)
}


bool Type_handler_geometry::
       Item_time_typecast_fix_length_and_dec(Item_time_typecast *item) const
{
  return Item_func_or_sum_illegal_param(item);
}



bool Type_handler_geometry::
       Item_date_typecast_fix_length_and_dec(Item_date_typecast *item) const
{
  return Item_func_or_sum_illegal_param(item);
}


bool Type_handler_geometry::
       Item_datetime_typecast_fix_length_and_dec(Item_datetime_typecast *item)
                                                 const
{
  return Item_func_or_sum_illegal_param(item);

}


bool Type_handler_geometry::
  Item_param_set_from_value(THD *thd,
                            Item_param *param,
                            const Type_all_attributes *attr,
                            const st_value *val) const
{
  param->unsigned_flag= false;
  param->setup_conversion_blob(thd);
  return param->set_str(val->m_string.ptr(), val->m_string.length(),
                        &my_charset_bin, &my_charset_bin);
}


void Type_handler_geometry::Item_param_set_param_func(Item_param *param,
                                                      uchar **pos,
                                                      ulong len) const
{
  param->set_null(); // Not possible type code in the client-server protocol
}


Field *Type_handler_geometry::
  make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *root,
                            const LEX_CSTRING *name,
                            const Record_addr &rec, const Bit_addr &bit,
                            const Column_definition_attributes *attr,
                            uint32 flags) const
{
  status_var_increment(current_thd->status_var.feature_gis);
  return new (root)
    Field_geom(rec.ptr(), rec.null_ptr(), rec.null_bit(),
               attr->unireg_check, name, share,
               attr->pack_flag_to_pack_length(), this, attr->srid);
}


void Type_handler_geometry::
  Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
                                        uchar *buff) const
{
  DBUG_ASSERT(f_decimals(def->pack_flag & ~FIELDFLAG_GEOM) == 0);
  def->frm_pack_basic(buff);
  buff[11]= 0;
  buff[14]= (uchar) geometry_type();
}



/* Values 1-40 reserved for 1-byte options,
   41-80 for 2-byte options,
   81-120 for 4-byte options,
   121-160 for 8-byte options,
   other - varied length in next 1-3 bytes.
*/
enum extra2_gis_field_options {
  FIELDGEOM_END=0,
  FIELDGEOM_STORAGE_MODEL=1,
  FIELDGEOM_PRECISION=2,
  FIELDGEOM_SCALE=3,
  FIELDGEOM_SRID=81,
};


uint
Type_handler_geometry::
  Column_definition_gis_options_image(uchar *cbuf,
                                      const Column_definition &def) const
{
  if (cbuf)
  {
    cbuf[0]= FIELDGEOM_STORAGE_MODEL;
    cbuf[1]= (uchar) Field_geom::GEOM_STORAGE_WKB;

    cbuf[2]= FIELDGEOM_PRECISION;
    cbuf[3]= (uchar) def.length;

    cbuf[4]= FIELDGEOM_SCALE;
    cbuf[5]= (uchar) def.decimals;

    cbuf[6]= FIELDGEOM_SRID;
    int4store(cbuf + 7, ((uint32) def.srid));

    cbuf[11]= FIELDGEOM_END;
  }
  return 12;
}


static uint gis_field_options_read(const uchar *buf, size_t buf_len,
                                   Field_geom::storage_type *st_type,
                                   uint *precision, uint *scale, uint *srid)
{
  const uchar *buf_end= buf + buf_len;
  const uchar *cbuf= buf;
  int option_id;

  *precision= *scale= *srid= 0;
  *st_type= Field_geom::GEOM_STORAGE_WKB;

  if (!buf)  /* can only happen with the old FRM file */
    goto end_of_record;

  while (cbuf < buf_end)
  {
    switch ((option_id= *(cbuf++)))
    {
    case FIELDGEOM_STORAGE_MODEL:
      *st_type= (Field_geom::storage_type) cbuf[0];
      break;
    case FIELDGEOM_PRECISION:
      *precision= cbuf[0];
      break;
    case FIELDGEOM_SCALE:
      *scale= cbuf[0];
      break;
    case FIELDGEOM_SRID:
      *srid= uint4korr(cbuf);
      break;
    case FIELDGEOM_END:
      goto end_of_record;
    }
    if (option_id > 0 && option_id <= 40)
      cbuf+= 1;
    else if (option_id > 40 && option_id <= 80)
      cbuf+= 2;
    else if (option_id > 80 && option_id <= 120)
      cbuf+= 4;
    else if (option_id > 120 && option_id <= 160)
      cbuf+= 8;
    else /* > 160 and <=255 */
      cbuf+= cbuf[0] ? 1 + cbuf[0] : 3 + uint2korr(cbuf+1);
  }

end_of_record:
  return (uint)(cbuf - buf);
}


bool Type_handler_geometry::
  Column_definition_attributes_frm_unpack(Column_definition_attributes *attr,
                                          TABLE_SHARE *share,
                                          const uchar *buffer,
                                          LEX_CUSTRING *gis_options)
                                          const
{
  uint gis_opt_read, gis_length, gis_decimals;
  Field_geom::storage_type st_type;
  attr->frm_unpack_basic(buffer);
  gis_opt_read= gis_field_options_read(gis_options->str,
                                       gis_options->length,
                                       &st_type, &gis_length,
                                       &gis_decimals, &attr->srid);
  gis_options->str+= gis_opt_read;
  gis_options->length-= gis_opt_read;
  return false;
}


uint32
Type_handler_geometry::max_display_length_for_field(const Conv_source &src)
                                                    const
{
  return (uint32) my_set_bits(4 * 8);
}


enum_conv_type
Field_geom::rpl_conv_type_from(const Conv_source &source,
                               const Relay_log_info *rli,
                               const Conv_param &param) const
{
  return binlog_type() == source.real_field_type() ?
         rpl_conv_type_from_same_data_type(source.metadata(), rli, param) :
         CONV_TYPE_IMPOSSIBLE;
}


/*****************************************************************/
void Field_geom::sql_type(String &res) const
{
  CHARSET_INFO *cs= &my_charset_latin1;
  const Name tmp= m_type_handler->name();
  res.set(tmp.ptr(), tmp.length(), cs);
}


int Field_geom::store(double nr)
{
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER_THD(get_thd(), ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
  return -1;
}


int Field_geom::store(longlong nr, bool unsigned_val)
{
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER_THD(get_thd(), ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
  return -1;
}


int Field_geom::store_decimal(const my_decimal *)
{
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER_THD(get_thd(), ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
  return -1;
}


int Field_geom::store(const char *from, size_t length, CHARSET_INFO *cs)
{
  if (!length)
    bzero(ptr, Field_blob::pack_length());
  else
  {
    // Check given WKB
    uint32 wkb_type;
    if (length < SRID_SIZE + WKB_HEADER_SIZE + 4)
      goto err;
    wkb_type= uint4korr(from + SRID_SIZE + 1);
    if (wkb_type < (uint32) Geometry::wkb_point ||
	wkb_type > (uint32) Geometry::wkb_last)
      goto err;

    if (m_type_handler->geometry_type() != Type_handler_geometry::GEOM_GEOMETRY &&
        m_type_handler->geometry_type() != Type_handler_geometry::GEOM_GEOMETRYCOLLECTION &&
        (uint32) m_type_handler->geometry_type() != wkb_type)
    {
      const char *db= table->s->db.str;
      const char *tab_name= table->s->table_name.str;

      if (!db)
        db= "";
      if (!tab_name)
        tab_name= "";

      Geometry_buffer buffer;
      Geometry *geom= NULL;
      String wkt;
      const char *dummy;
      wkt.set_charset(&my_charset_latin1);
      if (!(geom= Geometry::construct(&buffer, from, uint32(length))) ||
          geom->as_wkt(&wkt, &dummy))
        goto err;

      my_error(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, MYF(0),
               Geometry::ci_collection[m_type_handler->geometry_type()]->m_name.str,
	       wkt.c_ptr_safe(),
               db, tab_name, field_name.str,
               (ulong) table->in_use->get_stmt_da()->
               current_row_for_warning());
      goto err_exit;
    }

    Field_blob::store_length(length);
    if ((table->copy_blobs || length <= MAX_FIELD_WIDTH) &&
        from != value.ptr())
    {						// Must make a copy
      value.copy(from, length, cs);
      from= value.ptr();
    }
    bmove(ptr + packlength, &from, sizeof(char*));
  }
  return 0;

err:
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER_THD(get_thd(), ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
err_exit:
  bzero(ptr, Field_blob::pack_length());
  return -1;
}


bool Field_geom::is_equal(const Column_definition &new_field) const
{
  /*
    - Allow ALTER..INPLACE to supertype (GEOMETRY),
      e.g. POINT to GEOMETRY or POLYGON to GEOMETRY.
    - Allow ALTER..INPLACE to the same geometry type: POINT -> POINT
  */
  if (new_field.type_handler() == m_type_handler)
    return true;
  const Type_handler_geometry *gth=
    dynamic_cast<const Type_handler_geometry*>(new_field.type_handler());
  return gth && gth->is_binary_compatible_geom_super_type_for(m_type_handler);
}


bool Field_geom::can_optimize_range(const Item_bool_func *cond,
                                    const Item *item,
                                    bool is_eq_func) const
{
  return item->cmp_type() == STRING_RESULT;
}


bool Field_geom::load_data_set_no_data(THD *thd, bool fixed_format)
{
  return Field_geom::load_data_set_null(thd);
}


bool Field_geom::load_data_set_null(THD *thd)
{
  Field_blob::reset();
  if (!maybe_null())
  {
    my_error(ER_WARN_NULL_TO_NOTNULL, MYF(0), field_name.str,
             thd->get_stmt_da()->current_row_for_warning());
    return true;
  }
  set_null();
  set_has_explicit_value(); // Do not auto-update this field
  return false;
}


uint Field_geom::get_key_image(uchar *buff,uint length, const uchar *ptr_arg,
                               imagetype type_arg) const
{
  if (type_arg == itMBR)
  {
    LEX_CSTRING tmp;
    tmp.str= (const char *) get_ptr(ptr_arg);
    tmp.length= get_length(ptr_arg);
    return Geometry::get_key_image_itMBR(tmp, buff, length);
  }
  return Field_blob::get_key_image_itRAW(ptr_arg, buff, length);
}

Binlog_type_info Field_geom::binlog_type_info() const
{
  DBUG_ASSERT(Field_geom::type() == binlog_type());
  return Binlog_type_info(Field_geom::type(), pack_length_no_ptr(), 1,
                          field_charset(), type_handler_geom()->geometry_type());
}

#endif // HAVE_SPATIAL