OmniSciDB  2e3a973ef4
import_export::anonymous_namespace{QueryExporterGDAL.cpp} Namespace Reference

Functions

OGRFieldType sql_type_info_to_ogr_field_type (const std::string &name, const SQLTypeInfo &type_info, const QueryExporter::FileType file_type)
 
void insert_geo_column (const GeoTargetValue *geo_tv, const SQLTypeInfo &ti, const int field_index, OGRFeature *ogr_feature)
 
void insert_scalar_column (const ScalarTargetValue *scalar_tv, const SQLTypeInfo &ti, const int field_index, OGRFeature *ogr_feature)
 
void insert_array_column (const ArrayTargetValue *array_tv, const SQLTypeInfo &ti, const int field_index, OGRFeature *ogr_feature, const std::string &column_name, QueryExporter::ArrayNullHandling array_null_handling)
 

Variables

static constexpr std::array< const char *, 4 > driver_names
 
static constexpr std::array< const char *, 4 > file_type_names
 
static constexpr std::array< const char *, 3 > compression_prefix
 
static constexpr std::array< const char *, 3 > compression_suffix = {"", ".gz", ".zip"}
 
static constexpr std::array< std::array< bool, 3 >, 4 > compression_implemented
 
static std::array< std::unordered_set< std::string >, 4 > file_type_valid_extensions
 

Function Documentation

◆ insert_array_column()

void import_export::anonymous_namespace{QueryExporterGDAL.cpp}::insert_array_column ( const ArrayTargetValue array_tv,
const SQLTypeInfo ti,
const int  field_index,
OGRFeature *  ogr_feature,
const std::string &  column_name,
QueryExporter::ArrayNullHandling  array_null_handling 
)

Definition at line 457 of file QueryExporterGDAL.cpp.

References CHECK, CHECK_EQ, CHECK_GE, shared::formatHMS(), SQLTypeInfo::get_subtype(), SQLTypeInfo::get_type_name(), SQLTypeInfo::is_geometry(), anonymous_namespace{TypedDataAccessors.h}::is_null(), import_export::QueryExporter::kAbortWithWarning, kBIGINT, kBOOLEAN, kDATE, import_export::QueryExporter::kExportZeros, kFLOAT, kINT, import_export::QueryExporter::kNullEntireField, kSMALLINT, kTIME, kTIMESTAMP, kTINYINT, NULL_BIGINT, NULL_BOOLEAN, NULL_DOUBLE, NULL_FLOAT, NULL_INT, NULL_SMALLINT, and NULL_TINYINT.

Referenced by import_export::QueryExporterGDAL::exportResults().

462  {
463  CHECK_GE(field_index, 0);
464  CHECK(!ti.is_geometry());
465 
466  if (!array_tv->is_initialized()) {
467  // entire array is null
468  ogr_feature->SetFieldNull(field_index);
469  return;
470  }
471 
472  auto const& scalar_tvs = array_tv->get();
473 
474  auto field_type = ogr_feature->GetFieldDefnRef(field_index)->GetType();
475 
476  // only one of these will get used
477  // could use a std::vector<ScalarTargetValue> but need raw data at the end
478  // so we would have to extract it there anyway, so not sure it's worthwhile
479  // we can, at least, pre-reserve whichever array we're going to use
480  std::vector<int> int_values;
481  std::vector<GIntBig> int64_values;
482  std::vector<std::string> string_values;
483  std::vector<double> real_values;
484  switch (field_type) {
485  case OFTIntegerList:
486  int_values.reserve(scalar_tvs.size());
487  break;
488  case OFTInteger64List:
489  int64_values.reserve(scalar_tvs.size());
490  break;
491  case OFTRealList:
492  real_values.reserve(scalar_tvs.size());
493  break;
494  case OFTStringList:
495  string_values.reserve(scalar_tvs.size());
496  break;
497  default:
498  CHECK(false);
499  }
500 
501  bool force_null_to_zero =
502  (array_null_handling == QueryExporter::ArrayNullHandling::kExportZeros);
503 
504  // now extract the data
505  bool any_null = false;
506  for (uint32_t i = 0; i < scalar_tvs.size(); i++) {
507  bool is_null = false;
508  auto const scalar_tv = &scalar_tvs[i];
509  if (boost::get<int64_t>(scalar_tv)) {
510  auto int_val = *(boost::get<int64_t>(scalar_tv));
511  bool is_int64 = false;
512  switch (ti.get_subtype()) {
513  case kBOOLEAN:
514  is_null = (int_val == NULL_BOOLEAN);
515  break;
516  case kTINYINT:
517  is_null = (int_val == NULL_TINYINT);
518  break;
519  case kSMALLINT:
520  is_null = (int_val == NULL_SMALLINT);
521  break;
522  case kINT:
523  is_null = (int_val == NULL_INT);
524  break;
525  case kBIGINT:
526  is_null = (int_val == NULL_BIGINT);
527  is_int64 = true;
528  break;
529  case kTIME:
530  case kTIMESTAMP:
531  case kDATE:
532  is_null = (int_val == NULL_BIGINT);
533  is_int64 = true;
534  break;
535  default:
536  is_null = false;
537  }
538  if (ti.get_subtype() == kTIME) {
539  if (is_null) {
540  string_values.emplace_back("");
541  } else {
542  constexpr size_t buf_size = 9;
543  char buf[buf_size];
544  size_t const len = shared::formatHMS(buf, buf_size, int_val);
545  CHECK_EQ(8u, len); // 8 == strlen("HH:MM:SS")
546  string_values.emplace_back(buf);
547  }
548  } else if (is_int64) {
549  if (is_null && force_null_to_zero) {
550  int64_values.push_back(0);
551  } else {
552  int64_values.push_back(int_val);
553  }
554  } else {
555  if (is_null && force_null_to_zero) {
556  int_values.push_back(0);
557  } else {
558  int_values.push_back(int_val);
559  }
560  }
561  } else if (boost::get<double>(scalar_tv)) {
562  auto real_val = *(boost::get<double>(scalar_tv));
563  if (ti.get_subtype() == kFLOAT) {
564  is_null = (real_val == NULL_FLOAT);
565  } else {
566  is_null = (real_val == NULL_DOUBLE);
567  }
568  if (is_null && force_null_to_zero) {
569  real_values.push_back(0.0);
570  } else {
571  real_values.push_back(real_val);
572  }
573  } else if (boost::get<float>(scalar_tv)) {
574  CHECK_EQ(kFLOAT, ti.get_subtype());
575  auto real_val = *(boost::get<float>(scalar_tv));
576  is_null = (real_val == NULL_FLOAT);
577  if (is_null && force_null_to_zero) {
578  real_values.push_back(0.0);
579  } else {
580  real_values.push_back(static_cast<double>(real_val));
581  }
582  } else {
583  auto s = boost::get<NullableString>(scalar_tv);
584  is_null = !s || boost::get<void*>(s);
585  if (is_null) {
586  string_values.emplace_back("");
587  } else {
588  auto s_notnull = boost::get<std::string>(s);
589  CHECK(s_notnull);
590  string_values.emplace_back(s_notnull->c_str());
591  }
592  }
593  any_null |= is_null;
594  }
595 
596  // special behaviour if we found any individual nulls?
597  if (any_null) {
598  switch (array_null_handling) {
599  case QueryExporter::ArrayNullHandling::kAbortWithWarning:
600  throw std::runtime_error(
601  "Found individual nulls in Array Column '" + column_name + "' of type '" +
602  ti.get_type_name() +
603  "'. Use 'array_null_handling' Export Option to specify behaviour.");
604  case QueryExporter::ArrayNullHandling::kNullEntireField:
605  ogr_feature->SetFieldNull(field_index);
606  return;
607  default:
608  break;
609  }
610  }
611 
612  // store the captured array in the feature
613  switch (field_type) {
614  case OFTIntegerList:
615  ogr_feature->SetField(field_index, int_values.size(), int_values.data());
616  break;
617  case OFTInteger64List:
618  ogr_feature->SetField(field_index, int64_values.size(), int64_values.data());
619  break;
620  case OFTRealList:
621  ogr_feature->SetField(field_index, real_values.size(), real_values.data());
622  break;
623  case OFTStringList: {
624  std::vector<const char*> raw_strings;
625  raw_strings.reserve(string_values.size() + 1);
626  for (auto const& string_value : string_values) {
627  raw_strings.push_back(string_value.c_str());
628  }
629  raw_strings.push_back(nullptr);
630  ogr_feature->SetField(field_index, raw_strings.data());
631  } break;
632  default:
633  CHECK(false);
634  }
635 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
#define NULL_DOUBLE
Definition: sqltypes.h:186
Definition: sqltypes.h:51
#define NULL_BIGINT
Definition: sqltypes.h:184
#define CHECK_GE(x, y)
Definition: Logger.h:210
size_t formatHMS(char *buf, size_t const max, int64_t const unixtime)
Definition: misc.cpp:80
#define NULL_TINYINT
Definition: sqltypes.h:181
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:260
#define NULL_FLOAT
Definition: sqltypes.h:185
#define NULL_INT
Definition: sqltypes.h:183
Definition: sqltypes.h:55
bool is_geometry() const
Definition: sqltypes.h:429
std::string get_type_name() const
Definition: sqltypes.h:362
bool is_null(const T &v, const SQLTypeInfo &t)
#define NULL_SMALLINT
Definition: sqltypes.h:182
#define CHECK(condition)
Definition: Logger.h:197
Definition: sqltypes.h:47
#define NULL_BOOLEAN
Definition: sqltypes.h:180
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insert_geo_column()

void import_export::anonymous_namespace{QueryExporterGDAL.cpp}::insert_geo_column ( const GeoTargetValue geo_tv,
const SQLTypeInfo ti,
const int  field_index,
OGRFeature *  ogr_feature 
)

Definition at line 317 of file QueryExporterGDAL.cpp.

References CHECK, CHECK_EQ, SQLTypeInfo::get_type(), Geospatial::GeoBase::getOGRGeometry(), SQLTypeInfo::is_geometry(), kLINESTRING, kMULTIPOLYGON, kPOINT, and kPOLYGON.

Referenced by import_export::QueryExporterGDAL::exportResults().

320  {
321  CHECK_EQ(field_index, -1);
322  CHECK(ti.is_geometry());
323 
324  // use Geo classes to convert to OGRGeometry
325  // and destroy when Geo object goes out of scope
326  switch (ti.get_type()) {
327  case kPOINT: {
328  auto const point_tv = boost::get<GeoPointTargetValue>(geo_tv->get());
329  auto* coords = point_tv.coords.get();
330  CHECK(coords);
331  Geospatial::GeoPoint point(*coords);
332  ogr_feature->SetGeometry(point.getOGRGeometry());
333  } break;
334  case kLINESTRING: {
335  auto const linestring_tv = boost::get<GeoLineStringTargetValue>(geo_tv->get());
336  auto* coords = linestring_tv.coords.get();
337  CHECK(coords);
338  Geospatial::GeoLineString linestring(*coords);
339  ogr_feature->SetGeometry(linestring.getOGRGeometry());
340  } break;
341  case kPOLYGON: {
342  auto const polygon_tv = boost::get<GeoPolyTargetValue>(geo_tv->get());
343  auto* coords = polygon_tv.coords.get();
344  CHECK(coords);
345  auto* ring_sizes = polygon_tv.ring_sizes.get();
346  CHECK(ring_sizes);
347  Geospatial::GeoPolygon polygon(*coords, *ring_sizes);
348  ogr_feature->SetGeometry(polygon.getOGRGeometry());
349  } break;
350  case kMULTIPOLYGON: {
351  auto const multipolygon_tv = boost::get<GeoMultiPolyTargetValue>(geo_tv->get());
352  auto* coords = multipolygon_tv.coords.get();
353  CHECK(coords);
354  auto* ring_sizes = multipolygon_tv.ring_sizes.get();
355  CHECK(ring_sizes);
356  auto* poly_rings = multipolygon_tv.poly_rings.get();
357  CHECK(poly_rings);
358  Geospatial::GeoMultiPolygon multipolygon(*coords, *ring_sizes, *poly_rings);
359  ogr_feature->SetGeometry(multipolygon.getOGRGeometry());
360  } break;
361  default:
362  CHECK(false);
363  }
364 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
bool is_geometry() const
Definition: sqltypes.h:429
#define CHECK(condition)
Definition: Logger.h:197
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:259
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insert_scalar_column()

void import_export::anonymous_namespace{QueryExporterGDAL.cpp}::insert_scalar_column ( const ScalarTargetValue scalar_tv,
const SQLTypeInfo ti,
const int  field_index,
OGRFeature *  ogr_feature 
)

Definition at line 366 of file QueryExporterGDAL.cpp.

References CHECK, CHECK_EQ, CHECK_GE, shared::formatHMS(), SQLTypeInfo::get_type(), SQLTypeInfo::is_geometry(), anonymous_namespace{TypedDataAccessors.h}::is_null(), kBIGINT, kBOOLEAN, kDATE, kFLOAT, kINT, kSMALLINT, kTIME, kTIMESTAMP, kTINYINT, NULL_BIGINT, NULL_BOOLEAN, NULL_DOUBLE, NULL_FLOAT, NULL_INT, NULL_SMALLINT, NULL_TINYINT, and SCI.

Referenced by import_export::QueryExporterGDAL::exportResults().

369  {
370  CHECK_GE(field_index, 0);
371  CHECK(!ti.is_geometry());
372 
373  auto field_type = ogr_feature->GetFieldDefnRef(field_index)->GetType();
374 
375  bool is_null{false};
376  if (boost::get<int64_t>(scalar_tv)) {
377  auto int_val = *(boost::get<int64_t>(scalar_tv));
378  bool is_int64 = false;
379  switch (ti.get_type()) {
380  case kBOOLEAN:
381  is_null = (int_val == NULL_BOOLEAN);
382  break;
383  case kTINYINT:
384  is_null = (int_val == NULL_TINYINT);
385  break;
386  case kSMALLINT:
387  is_null = (int_val == NULL_SMALLINT);
388  break;
389  case kINT:
390  is_null = (int_val == NULL_INT);
391  break;
392  case kBIGINT:
393  is_null = (int_val == NULL_BIGINT);
394  is_int64 = true;
395  break;
396  case kTIME:
397  case kTIMESTAMP:
398  case kDATE:
399  is_null = (int_val == NULL_BIGINT);
400  is_int64 = true;
401  break;
402  default:
403  is_null = false;
404  }
405  if (is_null) {
406  ogr_feature->SetFieldNull(field_index);
407  } else if (ti.get_type() == kTIME) {
408  CHECK_EQ(field_type, OFTString);
409  constexpr size_t buf_size = 9;
410  char buf[buf_size];
411  size_t const len = shared::formatHMS(buf, buf_size, int_val);
412  CHECK_EQ(8u, len); // 8 == strlen("HH:MM:SS")
413  ogr_feature->SetField(field_index, buf);
414  } else if (is_int64) {
415  CHECK_EQ(field_type, OFTInteger64);
416  ogr_feature->SetField(field_index, static_cast<GIntBig>(int_val));
417  } else {
418  CHECK_EQ(field_type, OFTInteger);
419  ogr_feature->SetField(field_index, SCI(int_val));
420  }
421  } else if (boost::get<double>(scalar_tv)) {
422  auto real_val = *(boost::get<double>(scalar_tv));
423  if (ti.get_type() == kFLOAT) {
424  is_null = (real_val == NULL_FLOAT);
425  } else {
426  is_null = (real_val == NULL_DOUBLE);
427  }
428  if (is_null) {
429  ogr_feature->SetFieldNull(field_index);
430  } else {
431  CHECK_EQ(field_type, OFTReal);
432  ogr_feature->SetField(field_index, real_val);
433  }
434  } else if (boost::get<float>(scalar_tv)) {
435  CHECK_EQ(kFLOAT, ti.get_type());
436  auto real_val = *(boost::get<float>(scalar_tv));
437  if (real_val == NULL_FLOAT) {
438  ogr_feature->SetFieldNull(field_index);
439  } else {
440  CHECK_EQ(field_type, OFTReal);
441  ogr_feature->SetField(field_index, real_val);
442  }
443  } else {
444  auto s = boost::get<NullableString>(scalar_tv);
445  is_null = !s || boost::get<void*>(s);
446  if (is_null) {
447  ogr_feature->SetFieldNull(field_index);
448  } else {
449  CHECK_EQ(field_type, OFTString);
450  auto s_notnull = boost::get<std::string>(s);
451  CHECK(s_notnull);
452  ogr_feature->SetField(field_index, s_notnull->c_str());
453  }
454  }
455 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
#define NULL_DOUBLE
Definition: sqltypes.h:186
Definition: sqltypes.h:51
#define NULL_BIGINT
Definition: sqltypes.h:184
#define CHECK_GE(x, y)
Definition: Logger.h:210
#define SCI(x)
size_t formatHMS(char *buf, size_t const max, int64_t const unixtime)
Definition: misc.cpp:80
#define NULL_TINYINT
Definition: sqltypes.h:181
#define NULL_FLOAT
Definition: sqltypes.h:185
#define NULL_INT
Definition: sqltypes.h:183
Definition: sqltypes.h:55
bool is_geometry() const
Definition: sqltypes.h:429
bool is_null(const T &v, const SQLTypeInfo &t)
#define NULL_SMALLINT
Definition: sqltypes.h:182
#define CHECK(condition)
Definition: Logger.h:197
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:259
Definition: sqltypes.h:47
#define NULL_BOOLEAN
Definition: sqltypes.h:180
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sql_type_info_to_ogr_field_type()

OGRFieldType import_export::anonymous_namespace{QueryExporterGDAL.cpp}::sql_type_info_to_ogr_field_type ( const std::string &  name,
const SQLTypeInfo type_info,
const QueryExporter::FileType  file_type 
)

Definition at line 91 of file QueryExporterGDAL.cpp.

References SQLTypeInfo::get_subtype(), SQLTypeInfo::get_type(), SQLTypeInfo::get_type_name(), kARRAY, kBIGINT, kBOOLEAN, kCHAR, kDATE, kDECIMAL, kDOUBLE, kFLOAT, kINT, kINTERVAL_DAY_TIME, kINTERVAL_YEAR_MONTH, kNUMERIC, import_export::QueryExporter::kShapefile, kSMALLINT, kTEXT, kTIME, kTIMESTAMP, kTINYINT, kVARCHAR, and SCI.

Referenced by import_export::QueryExporterGDAL::beginExport().

93  {
94  // store BOOLEAN as int
95  // store TIME as string (no OFTTimeList and Shapefiles reject OFTTime anyway)
96  // store all other time/date types as int64
97  // Shapefiles cannot store arrays of any type
98  switch (type_info.get_type()) {
99  case kBOOLEAN:
100  case kTINYINT:
101  case kINT:
102  case kSMALLINT:
103  return OFTInteger;
104  case kFLOAT:
105  case kDOUBLE:
106  case kNUMERIC:
107  case kDECIMAL:
108  return OFTReal;
109  case kCHAR:
110  case kVARCHAR:
111  case kTEXT:
112  case kTIME:
113  return OFTString;
114  case kBIGINT:
115  case kTIMESTAMP:
116  case kDATE:
117  case kINTERVAL_DAY_TIME:
119  return OFTInteger64;
120  case kARRAY:
121  if (file_type != QueryExporter::FileType::kShapefile) {
122  switch (type_info.get_subtype()) {
123  case kBOOLEAN:
124  case kTINYINT:
125  case kINT:
126  case kSMALLINT:
127  return OFTIntegerList;
128  case kFLOAT:
129  case kDOUBLE:
130  case kNUMERIC:
131  case kDECIMAL:
132  return OFTRealList;
133  case kCHAR:
134  case kVARCHAR:
135  case kTEXT:
136  case kTIME:
137  return OFTStringList;
138  case kBIGINT:
139  case kTIMESTAMP:
140  case kDATE:
141  case kINTERVAL_DAY_TIME:
143  return OFTInteger64List;
144  default:
145  break;
146  }
147  }
148  break;
149  default:
150  break;
151  }
152  throw std::runtime_error("Column '" + name + "' has unsupported type '" +
153  type_info.get_type_name() + "' for file type '" +
154  file_type_names[SCI(file_type)] + "'");
155 }
Definition: sqltypes.h:51
#define SCI(x)
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:260
Definition: sqltypes.h:54
Definition: sqltypes.h:55
std::string get_type_name() const
Definition: sqltypes.h:362
Definition: sqltypes.h:43
static constexpr std::array< const char *, 4 > file_type_names
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:259
Definition: sqltypes.h:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ compression_implemented

constexpr std::array<std::array<bool, 3>, 4> import_export::anonymous_namespace{QueryExporterGDAL.cpp}::compression_implemented
static
Initial value:
= {
{{true, false, false},
{true, true, false},
{true, true, false},
{true, false, false}}}

Definition at line 82 of file QueryExporterGDAL.cpp.

Referenced by import_export::QueryExporterGDAL::beginExport().

◆ compression_prefix

constexpr std::array<const char*, 3> import_export::anonymous_namespace{QueryExporterGDAL.cpp}::compression_prefix
static
Initial value:
= {"",
"/vsigzip/",
"/vsizip/"}

Definition at line 74 of file QueryExporterGDAL.cpp.

Referenced by import_export::QueryExporterGDAL::beginExport().

◆ compression_suffix

constexpr std::array<const char*, 3> import_export::anonymous_namespace{QueryExporterGDAL.cpp}::compression_suffix = {"", ".gz", ".zip"}
static

Definition at line 78 of file QueryExporterGDAL.cpp.

Referenced by import_export::QueryExporterGDAL::beginExport().

◆ driver_names

constexpr std::array<const char*, 4> import_export::anonymous_namespace{QueryExporterGDAL.cpp}::driver_names
static
Initial value:
= {"INVALID",
"GeoJSON",
"GeoJSONSeq",
"ESRI Shapefile"}

Definition at line 64 of file QueryExporterGDAL.cpp.

Referenced by import_export::QueryExporterGDAL::beginExport().

◆ file_type_names

constexpr std::array<const char*, 4> import_export::anonymous_namespace{QueryExporterGDAL.cpp}::file_type_names
static
Initial value:
= {"CSV",
"GeoJSON",
"GeoJSONL",
"Shapefile"}

Definition at line 69 of file QueryExporterGDAL.cpp.

Referenced by import_export::QueryExporterGDAL::beginExport().

◆ file_type_valid_extensions

std::array<std::unordered_set<std::string>, 4> import_export::anonymous_namespace{QueryExporterGDAL.cpp}::file_type_valid_extensions
static
Initial value:
= {
{{".csv", ".tsv"}, {".geojson", ".json"}, {".geojson", ".json"}, {".shp"}}}

Definition at line 88 of file QueryExporterGDAL.cpp.

Referenced by import_export::QueryExporterGDAL::beginExport().