Skip to content

Commit

Permalink
clang formatted: Promote LINESTRING to MULTILINESTRING
Browse files Browse the repository at this point in the history
    Author: Simon Eves <simon.eves@heavy.ai>
    Date:   Mon May 9 17:41:49 2022 -0700

        Promote LINESTRING to MULTILINESTRING
        Add helper function for geo promotion validation
  • Loading branch information
shtilman authored and andrewseidl committed Jul 8, 2022
1 parent 4d08f24 commit df3bbda
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 100 deletions.
17 changes: 8 additions & 9 deletions DataMgr/ForeignStorage/GeospatialEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,16 @@ class GeospatialEncoder {
bounds_parse_buffer_,
ring_sizes_parse_buffer_,
poly_rings_parse_buffer_,
PROMOTE_POLYGON_TO_MULTIPOLYGON)) {
PROMOTE_SINGLE_TO_MULTI)) {
throwMalformedGeoElement(geo_column_descriptor_->columnName);
}

// validate types
if (geo_column_descriptor_->columnType.get_type() != import_ti.get_type()) {
if (!PROMOTE_POLYGON_TO_MULTIPOLYGON ||
!(import_ti.get_type() == SQLTypes::kPOLYGON &&
geo_column_descriptor_->columnType.get_type() == SQLTypes::kMULTIPOLYGON)) {
throwMismatchedGeoElement(geo_column_descriptor_->columnName);
}

if (!geo_promoted_type_match(import_ti.get_type(),
geo_column_descriptor_->columnType.get_type(),
PROMOTE_SINGLE_TO_MULTI)) {
throwMismatchedGeoElement(geo_column_descriptor_->columnName);
}

// append coords
Expand Down Expand Up @@ -305,7 +304,7 @@ class GeospatialEncoder {
bounds_parse_buffer_,
ring_sizes_parse_buffer_,
poly_rings_parse_buffer_,
PROMOTE_POLYGON_TO_MULTIPOLYGON);
PROMOTE_SINGLE_TO_MULTI);
// POINT columns are represented using fixed length arrays and need
// special treatment of nulls
if (geo_column_descriptor_->columnType.get_type() == kPOINT) {
Expand Down Expand Up @@ -482,7 +481,7 @@ class GeospatialEncoder {

const ColumnDescriptor* geo_column_descriptor_;

constexpr static bool PROMOTE_POLYGON_TO_MULTIPOLYGON = true;
constexpr static bool PROMOTE_SINGLE_TO_MULTI = true;

StringNoneEncoder* base_column_encoder_;
Encoder* coords_column_encoder_;
Expand Down
25 changes: 9 additions & 16 deletions DataMgr/ForeignStorage/TextFileBufferParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ bool TextFileBufferParser::isCoordinateScalar(const std::string_view datum) {
}

namespace {
constexpr bool PROMOTE_POLYGON_TO_MULTIPOLYGON = true;
constexpr bool PROMOTE_SINGLE_TO_MULTI = true;

bool set_coordinates_from_separate_lon_lat_columns(const std::string_view lon_str,
const std::string_view lat_str,
Expand Down Expand Up @@ -224,7 +224,7 @@ void TextFileBufferParser::processInvalidGeoColumn(

SQLTypeInfo import_ti{col_ti};
Geospatial::GeoTypesFactory::getNullGeoColumns(
import_ti, coords, bounds, ring_sizes, poly_rings, PROMOTE_POLYGON_TO_MULTIPOLYGON);
import_ti, coords, bounds, ring_sizes, poly_rings, PROMOTE_SINGLE_TO_MULTI);

// import extracted geo
import_export::Importer::set_geo_physical_import_buffer(*catalog,
Expand Down Expand Up @@ -287,12 +287,8 @@ void TextFileBufferParser::processGeoColumn(
++import_idx;
} else {
if (is_null || geo_string.empty() || geo_string == "NULL") {
Geospatial::GeoTypesFactory::getNullGeoColumns(import_ti,
coords,
bounds,
ring_sizes,
poly_rings,
PROMOTE_POLYGON_TO_MULTIPOLYGON);
Geospatial::GeoTypesFactory::getNullGeoColumns(
import_ti, coords, bounds, ring_sizes, poly_rings, PROMOTE_SINGLE_TO_MULTI);
is_null = true;
} else {
// extract geometry directly from WKT
Expand All @@ -302,21 +298,18 @@ void TextFileBufferParser::processGeoColumn(
bounds,
ring_sizes,
poly_rings,
PROMOTE_POLYGON_TO_MULTIPOLYGON)) {
PROMOTE_SINGLE_TO_MULTI)) {
std::string msg = "Failed to extract valid geometry from row " +
std::to_string(first_row_index + row_index_plus_one) +
" for column " + cd->columnName;
throw std::runtime_error(msg);
}

// validate types
if (col_type != import_ti.get_type()) {
if (!PROMOTE_POLYGON_TO_MULTIPOLYGON ||
!(import_ti.get_type() == SQLTypes::kPOLYGON &&
col_type == SQLTypes::kMULTIPOLYGON)) {
throw std::runtime_error("Imported geometry doesn't match the type of column " +
cd->columnName);
}
if (!geo_promoted_type_match(
import_ti.get_type(), col_type, PROMOTE_SINGLE_TO_MULTI)) {
throw std::runtime_error("Imported geometry doesn't match the type of column " +
cd->columnName);
}

// get render group
Expand Down
36 changes: 19 additions & 17 deletions Geospatial/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,11 +1021,11 @@ bool GeoTypesFactory::getGeoColumns(const std::string& wkt_or_wkb_hex,
std::vector<double>& bounds,
std::vector<int>& ring_sizes,
std::vector<int>& poly_rings,
const bool promote_poly_to_mpoly) {
const bool promote_single_to_multi) {
try {
if (wkt_or_wkb_hex.empty() || wkt_or_wkb_hex == "NULL") {
getNullGeoColumns(
ti, coords, bounds, ring_sizes, poly_rings, promote_poly_to_mpoly);
ti, coords, bounds, ring_sizes, poly_rings, promote_single_to_multi);
return true;
}

Expand All @@ -1041,7 +1041,7 @@ bool GeoTypesFactory::getGeoColumns(const std::string& wkt_or_wkb_hex,
bounds,
ring_sizes,
poly_rings,
promote_poly_to_mpoly);
promote_single_to_multi);

} catch (const std::exception& e) {
LOG(ERROR) << "Geospatial Import Error: " << e.what();
Expand All @@ -1057,7 +1057,7 @@ bool GeoTypesFactory::getGeoColumns(const std::vector<uint8_t>& wkb,
std::vector<double>& bounds,
std::vector<int>& ring_sizes,
std::vector<int>& poly_rings,
const bool promote_poly_to_mpoly) {
const bool promote_single_to_multi) {
try {
const auto geospatial_base = GeoTypesFactory::createGeoType(wkb);

Expand All @@ -1071,7 +1071,7 @@ bool GeoTypesFactory::getGeoColumns(const std::vector<uint8_t>& wkb,
bounds,
ring_sizes,
poly_rings,
promote_poly_to_mpoly);
promote_single_to_multi);

} catch (const std::exception& e) {
LOG(ERROR) << "Geospatial Import Error: " << e.what();
Expand All @@ -1087,7 +1087,7 @@ bool GeoTypesFactory::getGeoColumns(OGRGeometry* geom,
std::vector<double>& bounds,
std::vector<int>& ring_sizes,
std::vector<int>& poly_rings,
const bool promote_poly_to_mpoly) {
const bool promote_single_to_multi) {
try {
const auto geospatial_base = GeoTypesFactory::createGeoType(geom);

Expand All @@ -1101,7 +1101,7 @@ bool GeoTypesFactory::getGeoColumns(OGRGeometry* geom,
bounds,
ring_sizes,
poly_rings,
promote_poly_to_mpoly);
promote_single_to_multi);

} catch (const std::exception& e) {
LOG(ERROR) << "Geospatial Import Error: " << e.what();
Expand All @@ -1117,7 +1117,7 @@ bool GeoTypesFactory::getGeoColumns(const std::vector<std::string>* wkt_or_wkb_h
std::vector<std::vector<double>>& bounds_column,
std::vector<std::vector<int>>& ring_sizes_column,
std::vector<std::vector<int>>& poly_rings_column,
const bool promote_poly_to_mpoly) {
const bool promote_single_to_multi) {
try {
for (const auto& wkt_or_wkb_hex : *wkt_or_wkb_hex_column) {
std::vector<double> coords;
Expand All @@ -1132,12 +1132,11 @@ bool GeoTypesFactory::getGeoColumns(const std::vector<std::string>* wkt_or_wkb_h
bounds,
ring_sizes,
poly_rings,
promote_poly_to_mpoly);
if (ti.get_type() != row_ti.get_type()) {
if (!promote_poly_to_mpoly || !(row_ti.get_type() == SQLTypes::kPOLYGON &&
ti.get_type() == SQLTypes::kMULTIPOLYGON)) {
throw GeoTypesError("GeoFactory", "Columnar: Geometry type mismatch");
}
promote_single_to_multi);

if (!geo_promoted_type_match(
row_ti.get_type(), ti.get_type(), promote_single_to_multi)) {
throw GeoTypesError("GeoFactory", "Columnar: Geometry type mismatch");
}
coords_column.push_back(coords);
bounds_column.push_back(bounds);
Expand Down Expand Up @@ -1183,7 +1182,7 @@ void GeoTypesFactory::getGeoColumnsImpl(const std::unique_ptr<GeoBase>& geospati
std::vector<double>& bounds,
std::vector<int>& ring_sizes,
std::vector<int>& poly_rings,
const bool promote_poly_to_mpoly) {
const bool promote_single_to_multi) {
switch (geospatial_base->getType()) {
case GeoBase::GeoType::kPOINT: {
const auto geospatial_point = dynamic_cast<GeoPoint*>(geospatial_base.get());
Expand All @@ -1197,6 +1196,9 @@ void GeoTypesFactory::getGeoColumnsImpl(const std::unique_ptr<GeoBase>& geospati
dynamic_cast<GeoLineString*>(geospatial_base.get());
CHECK(geospatial_linestring);
geospatial_linestring->getColumns(coords, bounds);
if (promote_single_to_multi) {
ring_sizes.push_back(1);
}
ti.set_type(kLINESTRING);
break;
}
Expand All @@ -1212,7 +1214,7 @@ void GeoTypesFactory::getGeoColumnsImpl(const std::unique_ptr<GeoBase>& geospati
const auto geospatial_poly = dynamic_cast<GeoPolygon*>(geospatial_base.get());
CHECK(geospatial_poly);
geospatial_poly->getColumns(coords, ring_sizes, bounds);
if (promote_poly_to_mpoly) {
if (promote_single_to_multi) {
if (ring_sizes.size()) {
CHECK_GT(coords.size(), 0u);
poly_rings.push_back(1 + geospatial_poly->getNumInteriorRings());
Expand Down Expand Up @@ -1240,7 +1242,7 @@ void GeoTypesFactory::getNullGeoColumns(SQLTypeInfo& ti,
std::vector<double>& bounds,
std::vector<int>& ring_sizes,
std::vector<int>& poly_rings,
const bool promote_poly_to_mpoly) {
const bool promote_single_to_multi) {
auto t = ti.get_type();
switch (t) {
case kPOINT: {
Expand Down
12 changes: 6 additions & 6 deletions Geospatial/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,38 +277,38 @@ class GeoTypesFactory {
std::vector<double>& bounds,
std::vector<int>& ring_sizes,
std::vector<int>& poly_rings,
const bool promote_poly_to_mpoly = false);
const bool promote_single_to_multi = false);

static bool getGeoColumns(const std::vector<uint8_t>& wkb,
SQLTypeInfo& ti,
std::vector<double>& coords,
std::vector<double>& bounds,
std::vector<int>& ring_sizes,
std::vector<int>& poly_rings,
const bool promote_poly_to_mpoly = false);
const bool promote_single_to_multi = false);

static bool getGeoColumns(OGRGeometry* geom,
SQLTypeInfo& ti,
std::vector<double>& coords,
std::vector<double>& bounds,
std::vector<int>& ring_sizes,
std::vector<int>& poly_rings,
const bool promote_poly_to_mpoly = false);
const bool promote_single_to_multi = false);

static bool getGeoColumns(const std::vector<std::string>* wkt_or_wkb_hex_column,
SQLTypeInfo& ti,
std::vector<std::vector<double>>& coords_column,
std::vector<std::vector<double>>& bounds_column,
std::vector<std::vector<int>>& ring_sizes_column,
std::vector<std::vector<int>>& poly_rings_column,
const bool promote_poly_to_mpoly = false);
const bool promote_single_to_multi = false);

static void getNullGeoColumns(SQLTypeInfo& ti,
std::vector<double>& coords,
std::vector<double>& bounds,
std::vector<int>& ring_sizes,
std::vector<int>& poly_rings,
const bool promote_poly_to_mpoly = false);
const bool promote_single_to_multi = false);

private:
static std::unique_ptr<Geospatial::GeoBase> createGeoTypeImpl(
Expand All @@ -320,7 +320,7 @@ class GeoTypesFactory {
std::vector<double>& bounds,
std::vector<int>& ring_sizes,
std::vector<int>& poly_rings,
const bool promote_poly_to_mpoly = false);
const bool promote_single_to_multi = false);
};

} // namespace Geospatial
Loading

0 comments on commit df3bbda

Please sign in to comment.