OmniSciDB  085a039ca4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 *, 5 > 
driver_names
 
static constexpr std::array
< const char *, 5 > 
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 >, 5 > 
compression_implemented
 
static std::array
< std::unordered_set
< std::string >, 5 > 
file_type_valid_extensions
 

Function Documentation

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 458 of file QueryExporterGDAL.cpp.

References CHECK, CHECK_EQ, CHECK_GE, shared::convert_temporal_to_iso_format(), SQLTypeInfo::get_elem_type(), SQLTypeInfo::get_subtype(), SQLTypeInfo::get_type_name(), SQLTypeInfo::is_geometry(), is_null(), SQLTypeInfo::is_time(), 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().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 321 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().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 370 of file QueryExporterGDAL.cpp.

References CHECK, CHECK_EQ, CHECK_GE, shared::convert_temporal_to_iso_format(), SQLTypeInfo::get_type(), SQLTypeInfo::is_geometry(), is_null(), SQLTypeInfo::is_time(), 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().

373  {
374  CHECK_GE(field_index, 0);
375  CHECK(!ti.is_geometry());
376 
377  auto field_type = ogr_feature->GetFieldDefnRef(field_index)->GetType();
378 
379  bool is_null{false};
380  if (boost::get<int64_t>(scalar_tv)) {
381  auto int_val = *(boost::get<int64_t>(scalar_tv));
382  bool is_int64 = false;
383  switch (ti.get_type()) {
384  case kBOOLEAN:
385  is_null = (int_val == NULL_BOOLEAN);
386  break;
387  case kTINYINT:
388  is_null = (int_val == NULL_TINYINT);
389  break;
390  case kSMALLINT:
391  is_null = (int_val == NULL_SMALLINT);
392  break;
393  case kINT:
394  is_null = (int_val == NULL_INT);
395  break;
396  case kBIGINT:
397  is_null = (int_val == NULL_BIGINT);
398  is_int64 = true;
399  break;
400  case kTIME:
401  case kTIMESTAMP:
402  case kDATE:
403  is_null = (int_val == NULL_BIGINT);
404  is_int64 = true;
405  break;
406  default:
407  is_null = false;
408  }
409  if (is_null) {
410  ogr_feature->SetFieldNull(field_index);
411  } else if (ti.is_time()) {
412  CHECK_EQ(field_type, OFTString);
413  auto str = shared::convert_temporal_to_iso_format(ti, int_val);
414  ogr_feature->SetField(field_index, str.c_str());
415  } else if (is_int64) {
416  CHECK_EQ(field_type, OFTInteger64);
417  ogr_feature->SetField(field_index, static_cast<GIntBig>(int_val));
418  } else {
419  CHECK_EQ(field_type, OFTInteger);
420  ogr_feature->SetField(field_index, SCI(int_val));
421  }
422  } else if (boost::get<double>(scalar_tv)) {
423  auto real_val = *(boost::get<double>(scalar_tv));
424  if (ti.get_type() == kFLOAT) {
425  is_null = (real_val == NULL_FLOAT);
426  } else {
427  is_null = (real_val == NULL_DOUBLE);
428  }
429  if (is_null) {
430  ogr_feature->SetFieldNull(field_index);
431  } else {
432  CHECK_EQ(field_type, OFTReal);
433  ogr_feature->SetField(field_index, real_val);
434  }
435  } else if (boost::get<float>(scalar_tv)) {
436  CHECK_EQ(kFLOAT, ti.get_type());
437  auto real_val = *(boost::get<float>(scalar_tv));
438  if (real_val == NULL_FLOAT) {
439  ogr_feature->SetFieldNull(field_index);
440  } else {
441  CHECK_EQ(field_type, OFTReal);
442  ogr_feature->SetField(field_index, real_val);
443  }
444  } else {
445  auto s = boost::get<NullableString>(scalar_tv);
446  is_null = !s || boost::get<void*>(s);
447  if (is_null) {
448  ogr_feature->SetFieldNull(field_index);
449  } else {
450  CHECK_EQ(field_type, OFTString);
451  auto s_notnull = boost::get<std::string>(s);
452  CHECK(s_notnull);
453  ogr_feature->SetField(field_index, s_notnull->c_str());
454  }
455  }
456 }
#define CHECK_EQ(x, y)
Definition: Logger.h:231
#define NULL_DOUBLE
Definition: sqltypes.h:49
#define NULL_FLOAT
#define NULL_BIGINT
std::string convert_temporal_to_iso_format(const SQLTypeInfo &type_info, int64_t unix_time)
Definition: misc.cpp:109
#define CHECK_GE(x, y)
Definition: Logger.h:236
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
bool is_time() const
Definition: sqltypes.h:516
#define SCI(x)
#define NULL_INT
CONSTEXPR DEVICE bool is_null(const T &value)
#define NULL_BOOLEAN
Definition: sqltypes.h:53
#define NULL_TINYINT
#define CHECK(condition)
Definition: Logger.h:223
bool is_geometry() const
Definition: sqltypes.h:522
#define NULL_SMALLINT
Definition: sqltypes.h:45

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 94 of file QueryExporterGDAL.cpp.

References SQLTypeInfo::get_subtype(), SQLTypeInfo::get_type(), SQLTypeInfo::get_type_name(), kARRAY, kBIGINT, kBOOLEAN, kCHAR, kDATE, kDECIMAL, kDOUBLE, import_export::QueryExporter::kFlatGeobuf, 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().

96  {
97  // store BOOLEAN as int
98  // store TIME as string (no OFTTimeList and Shapefiles reject OFTTime anyway)
99  // store all other time/date types as int64
100  // Shapefiles cannot store arrays of any type
101  switch (type_info.get_type()) {
102  case kBOOLEAN:
103  case kTINYINT:
104  case kINT:
105  case kSMALLINT:
106  return OFTInteger;
107  case kFLOAT:
108  case kDOUBLE:
109  case kNUMERIC:
110  case kDECIMAL:
111  return OFTReal;
112  case kCHAR:
113  case kVARCHAR:
114  case kTEXT:
115  case kTIME:
116  case kTIMESTAMP:
117  case kDATE:
118  return OFTString;
119  case kBIGINT:
120  case kINTERVAL_DAY_TIME:
122  return OFTInteger64;
123  case kARRAY:
124  if (file_type != QueryExporter::FileType::kShapefile &&
125  file_type != QueryExporter::FileType::kFlatGeobuf) {
126  switch (type_info.get_subtype()) {
127  case kBOOLEAN:
128  case kTINYINT:
129  case kINT:
130  case kSMALLINT:
131  return OFTIntegerList;
132  case kFLOAT:
133  case kDOUBLE:
134  case kNUMERIC:
135  case kDECIMAL:
136  return OFTRealList;
137  case kCHAR:
138  case kVARCHAR:
139  case kTEXT:
140  case kTIME:
141  case kTIMESTAMP:
142  case kDATE:
143  return OFTStringList;
144  case kBIGINT:
145  case kINTERVAL_DAY_TIME:
147  return OFTInteger64List;
148  default:
149  break;
150  }
151  }
152  break;
153  default:
154  break;
155  }
156  throw std::runtime_error("Column '" + name + "' has unsupported type '" +
157  type_info.get_type_name() + "' for file type '" +
158  file_type_names[SCI(file_type)] + "'");
159 }
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:330
static constexpr std::array< const char *, 5 > file_type_names
Definition: sqltypes.h:49
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
#define SCI(x)
Definition: sqltypes.h:52
Definition: sqltypes.h:53
std::string get_type_name() const
Definition: sqltypes.h:443
Definition: sqltypes.h:41
Definition: sqltypes.h:45
string name
Definition: setup.in.py:72

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

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

Definition at line 84 of file QueryExporterGDAL.cpp.

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

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

Definition at line 76 of file QueryExporterGDAL.cpp.

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

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

Definition at line 80 of file QueryExporterGDAL.cpp.

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

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

Definition at line 64 of file QueryExporterGDAL.cpp.

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

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

Definition at line 70 of file QueryExporterGDAL.cpp.

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

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

Definition at line 91 of file QueryExporterGDAL.cpp.

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