OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataPreview.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "DataPreview.h"
18 
19 #include <regex>
20 
21 namespace foreign_storage {
22 std::optional<SQLTypes> detect_geo_type(const SampleRows& sample_rows,
23  size_t column_index) {
24  std::optional<SQLTypes> tentative_geo_type{};
25  for (const auto& row : sample_rows) {
26  static std::regex geo_regex{
27  "\\s*(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING|POLYGON|MULTIPOLYGON)\\s*\\(.+"
28  "\\)\\s*"};
29  std::smatch match;
30  CHECK_LT(column_index, row.size());
31  if (std::regex_match(row[column_index], match, geo_regex)) {
32  CHECK_EQ(match.size(), static_cast<size_t>(2));
33  SQLTypes geo_type{kNULLT};
34  const auto& geo_type_str = match[1];
35  if (geo_type_str == "POINT") {
36  geo_type = kPOINT;
37  } else if (geo_type_str == "MULTIPOINT") {
38  geo_type = kMULTIPOINT;
39  } else if (geo_type_str == "LINESTRING") {
40  geo_type = kLINESTRING;
41  } else if (geo_type_str == "MULTILINESTRING") {
42  geo_type = kMULTILINESTRING;
43  } else if (geo_type_str == "POLYGON") {
44  geo_type = kPOLYGON;
45  } else if (geo_type_str == "MULTIPOLYGON") {
46  geo_type = kMULTIPOLYGON;
47  } else {
48  UNREACHABLE() << "Unexpected geo type match: " << geo_type_str;
49  }
50  if (tentative_geo_type.has_value()) {
51  if (tentative_geo_type.value() != geo_type) {
52  return {}; // geo type does not match between rows, can not be imported
53  }
54  } else {
55  tentative_geo_type = geo_type;
56  }
57  }
58  }
59  return tentative_geo_type;
60 }
61 } // namespace foreign_storage
#define CHECK_EQ(x, y)
Definition: Logger.h:301
std::optional< SQLTypes > detect_geo_type(const SampleRows &sample_rows, size_t column_index)
Definition: DataPreview.cpp:22
SQLTypes
Definition: sqltypes.h:65
#define UNREACHABLE()
Definition: Logger.h:338
std::vector< std::vector< std::string >> SampleRows
Definition: DataPreview.h:26
#define CHECK_LT(x, y)
Definition: Logger.h:303