OmniSciDB  a667adc9c8
 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 #include "ParquetDataWrapper.h"
23 
24 namespace foreign_storage {
25 std::unique_ptr<ForeignDataWrapper> ForeignDataWrapperFactory::create(
26  const std::string& data_wrapper_type,
27  const int db_id,
28  const ForeignTable* foreign_table) {
29  std::unique_ptr<ForeignDataWrapper> data_wrapper;
30  if (data_wrapper_type == DataWrapperType::CSV) {
31  if (Csv::validate_and_get_is_s3_select(foreign_table)) {
32  UNREACHABLE();
33  } else {
34  data_wrapper = std::make_unique<CsvDataWrapper>(db_id, foreign_table);
35  }
36  } else if (data_wrapper_type == DataWrapperType::PARQUET) {
37  data_wrapper = std::make_unique<ParquetDataWrapper>(db_id, foreign_table);
38  } else {
39  throw std::runtime_error("Unsupported data wrapper");
40  }
41  return data_wrapper;
42 }
43 
45  const std::string& data_wrapper_type,
46  const ForeignTable* foreign_table) {
47  bool is_s3_select_wrapper{false};
48  std::string data_wrapper_type_key{data_wrapper_type};
49  constexpr const char* S3_SELECT_WRAPPER_KEY = "CSV_S3_SELECT";
50  if (foreign_table && data_wrapper_type == DataWrapperType::CSV &&
51  Csv::validate_and_get_is_s3_select(foreign_table)) {
52  is_s3_select_wrapper = true;
53  data_wrapper_type_key = S3_SELECT_WRAPPER_KEY;
54  }
55 
56  if (validation_data_wrappers_.find(data_wrapper_type_key) ==
58  if (data_wrapper_type == DataWrapperType::CSV) {
59  if (is_s3_select_wrapper) {
60  UNREACHABLE();
61  } else {
62  validation_data_wrappers_[data_wrapper_type_key] =
63  std::make_unique<CsvDataWrapper>();
64  }
65  } else if (data_wrapper_type == DataWrapperType::PARQUET) {
66  validation_data_wrappers_[data_wrapper_type_key] =
67  std::make_unique<ParquetDataWrapper>();
68  } else {
69  UNREACHABLE();
70  }
71  }
72  CHECK(validation_data_wrappers_.find(data_wrapper_type_key) !=
74  return *validation_data_wrappers_[data_wrapper_type_key];
75 }
76 
78  const std::string& data_wrapper_type) {
79  const auto& supported_wrapper_types = DataWrapperType::supported_data_wrapper_types;
80  if (std::find(supported_wrapper_types.begin(),
81  supported_wrapper_types.end(),
82  data_wrapper_type) == supported_wrapper_types.end()) {
83  throw std::runtime_error{"Invalid data wrapper type \"" + data_wrapper_type +
84  "\". Data wrapper type must be one of the following: " +
85  join(supported_wrapper_types, ", ") + "."};
86  }
87 }
88 
89 std::map<std::string, std::unique_ptr<ForeignDataWrapper>>
91 } // 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:241
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:197
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