OmniSciDB  4201147b46
 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|LINESTRING|POLYGON|MULTIPOLYGON)\\s*\\(.+\\)\\s*"};
28  std::smatch match;
29  CHECK_LT(column_index, row.size());
30  if (std::regex_match(row[column_index], match, geo_regex)) {
31  CHECK_EQ(match.size(), static_cast<size_t>(2));
32  SQLTypes geo_type{kNULLT};
33  const auto& geo_type_str = match[1];
34  if (geo_type_str == "POINT") {
35  geo_type = kPOINT;
36  } else if (geo_type_str == "LINESTRING") {
37  geo_type = kLINESTRING;
38  } else if (geo_type_str == "POLYGON") {
39  geo_type = kPOLYGON;
40  } else if (geo_type_str == "MULTIPOLYGON") {
41  geo_type = kMULTIPOLYGON;
42  } else {
43  UNREACHABLE() << "Unexpected geo type match: " << geo_type_str;
44  }
45  if (tentative_geo_type.has_value()) {
46  if (tentative_geo_type.value() != geo_type) {
47  return {}; // geo type does not match between rows, can not be imported
48  }
49  } else {
50  tentative_geo_type = geo_type;
51  }
52  }
53  }
54  return tentative_geo_type;
55 }
56 } // namespace foreign_storage
#define CHECK_EQ(x, y)
Definition: Logger.h:230
std::optional< SQLTypes > detect_geo_type(const SampleRows &sample_rows, size_t column_index)
Definition: DataPreview.cpp:22
SQLTypes
Definition: sqltypes.h:38
#define UNREACHABLE()
Definition: Logger.h:266
std::vector< std::vector< std::string >> SampleRows
Definition: DataPreview.h:26
#define CHECK_LT(x, y)
Definition: Logger.h:232