OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ForeignDataWrapperFactory.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021 OmniSci, 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 
18 
19 #include "CsvDataWrapper.h"
21 #include "ForeignDataWrapper.h"
22 #ifdef ENABLE_IMPORT_PARQUET
23 #include "ParquetDataWrapper.h"
24 #endif
25 
26 namespace foreign_storage {
27 std::unique_ptr<ForeignDataWrapper> ForeignDataWrapperFactory::create(
28  const std::string& data_wrapper_type,
29  const int db_id,
30  const ForeignTable* foreign_table) {
31  std::unique_ptr<ForeignDataWrapper> data_wrapper;
32  if (data_wrapper_type == DataWrapperType::CSV) {
33  if (Csv::validate_and_get_is_s3_select(foreign_table)) {
34  UNREACHABLE();
35  } else {
36  data_wrapper = std::make_unique<CsvDataWrapper>(db_id, foreign_table);
37  }
38 #ifdef ENABLE_IMPORT_PARQUET
39  } else if (data_wrapper_type == DataWrapperType::PARQUET) {
40  data_wrapper = std::make_unique<ParquetDataWrapper>(db_id, foreign_table);
41 #endif
42  } else {
43  throw std::runtime_error("Unsupported data wrapper");
44  }
45  return data_wrapper;
46 }
47 
49  const std::string& data_wrapper_type,
50  const ForeignTable* foreign_table) {
51  bool is_s3_select_wrapper{false};
52  std::string data_wrapper_type_key{data_wrapper_type};
53  constexpr const char* S3_SELECT_WRAPPER_KEY = "CSV_S3_SELECT";
54  if (foreign_table && data_wrapper_type == DataWrapperType::CSV &&
55  Csv::validate_and_get_is_s3_select(foreign_table)) {
56  is_s3_select_wrapper = true;
57  data_wrapper_type_key = S3_SELECT_WRAPPER_KEY;
58  }
59 
60  if (validation_data_wrappers_.find(data_wrapper_type_key) ==
62  if (data_wrapper_type == DataWrapperType::CSV) {
63  if (is_s3_select_wrapper) {
64  UNREACHABLE();
65  } else {
66  validation_data_wrappers_[data_wrapper_type_key] =
67  std::make_unique<CsvDataWrapper>();
68  }
69  } else if (data_wrapper_type == DataWrapperType::PARQUET) {
70  validation_data_wrappers_[data_wrapper_type_key] =
71  std::make_unique<ParquetDataWrapper>();
72  } else {
73  UNREACHABLE();
74  }
75  }
76  CHECK(validation_data_wrappers_.find(data_wrapper_type_key) !=
78  return *validation_data_wrappers_[data_wrapper_type_key];
79 }
80 
82  const std::string& data_wrapper_type) {
83  const auto& supported_wrapper_types = DataWrapperType::supported_data_wrapper_types;
84  if (std::find(supported_wrapper_types.begin(),
85  supported_wrapper_types.end(),
86  data_wrapper_type) == supported_wrapper_types.end()) {
87  throw std::runtime_error{"Invalid data wrapper type \"" + data_wrapper_type +
88  "\". Data wrapper type must be one of the following: " +
89  join(supported_wrapper_types, ", ") + "."};
90  }
91 }
92 
93 std::map<std::string, std::unique_ptr<ForeignDataWrapper>>
95 } // namespace foreign_storage
static const ForeignDataWrapper & createForValidation(const std::string &data_wrapper_type, const ForeignTable *foreign_table=nullptr)
static std::unique_ptr< ForeignDataWrapper > create(const std::string &data_wrapper_type, const int db_id, const ForeignTable *foreign_table)
std::string join(T const &container, std::string const &delim)
#define UNREACHABLE()
Definition: Logger.h:247
static std::map< std::string, std::unique_ptr< ForeignDataWrapper > > validation_data_wrappers_
static constexpr std::array< std::string_view, 2 > supported_data_wrapper_types
static void validateDataWrapperType(const std::string &data_wrapper_type)
#define CHECK(condition)
Definition: Logger.h:203
static constexpr char const * CSV
bool validate_and_get_is_s3_select(const ForeignTable *foreign_table)
Definition: CsvShared.cpp:93
static constexpr char const * PARQUET