|
6 | 6 |
|
7 | 7 | #include "CalciteJNI.h"
|
8 | 8 |
|
| 9 | +#include "OSDependent/omnisci_path.h" |
| 10 | + |
| 11 | +#include <boost/algorithm/string.hpp> |
| 12 | + |
| 13 | +#include <fstream> |
| 14 | +#include <regex> |
| 15 | +#include <unordered_map> |
| 16 | + |
| 17 | +namespace { |
| 18 | + |
| 19 | +std::string canonizeType(const std::string& type) { |
| 20 | + static const std::unordered_map<std::string, std::string> type_mapping = { |
| 21 | + {"bool", "i1"}, |
| 22 | + {"_Bool", "i1"}, |
| 23 | + {"int8_t", "i8"}, |
| 24 | + {"int8", "i8"}, |
| 25 | + {"char", "i8"}, |
| 26 | + {"int16_t", "i16"}, |
| 27 | + {"int16", "i16"}, |
| 28 | + {"short", "i16"}, |
| 29 | + {"int32_t", "i32"}, |
| 30 | + {"int32", "i32"}, |
| 31 | + {"int", "i32"}, |
| 32 | + {"int64_t", "i64"}, |
| 33 | + {"size_t", "i64"}, |
| 34 | + {"long", "i64"}, |
| 35 | + {"int64", "i64"}, |
| 36 | + {"float", "float"}, |
| 37 | + {"float32", "float"}, |
| 38 | + {"double", "double"}, |
| 39 | + {"float64", "double"}, |
| 40 | + {"", "void"}, |
| 41 | + {"void", "void"}, |
| 42 | + {"Array<bool>", "{i1*, i64, i8}*"}, |
| 43 | + {"Array<int8_t>", "{i8*, i64, i8}*"}, |
| 44 | + {"Array<char>", "{81*, i64, i8}*"}, |
| 45 | + {"Array<int16_t>", "{i16*, i64, i8}*"}, |
| 46 | + {"Array<short>", "{i16*, i64, i8}*"}, |
| 47 | + {"Array<int16_t>", "{i16*, i64, i8}*"}, |
| 48 | + {"Array<short>", "{i16*, i64, i8}*"}, |
| 49 | + {"Array<int32_t>", "{i32*, i64, i8}*"}, |
| 50 | + {"Array<int>", "{i32*, i64, i8}*"}, |
| 51 | + {"Array<int64_t>", "{i64*, i64, i8}*"}, |
| 52 | + {"Array<size_t>", "{i64*, i64, i8}*"}, |
| 53 | + {"Array<long>", "{i64*, i64, i8}*"}, |
| 54 | + {"Array<float>", "{float*, i64, i8}*"}, |
| 55 | + {"Array<double>", "{double*, i64, i8}*"}}; |
| 56 | + |
| 57 | + static const std::string const_prefix = "const "; |
| 58 | + static const std::string std_prefix = "std::"; |
| 59 | + |
| 60 | + if (type.substr(0, const_prefix.size()) == const_prefix) { |
| 61 | + return canonizeType(type.substr(const_prefix.size())); |
| 62 | + } |
| 63 | + |
| 64 | + if (type.substr(0, std_prefix.size()) == std_prefix) { |
| 65 | + return canonizeType(type.substr(std_prefix.size())); |
| 66 | + } |
| 67 | + |
| 68 | + auto it = type_mapping.find(type); |
| 69 | + if (it == type_mapping.end()) { |
| 70 | + throw std::runtime_error("Unknown type string in extension function: " + type); |
| 71 | + } |
| 72 | + |
| 73 | + return it->second; |
| 74 | +} |
| 75 | + |
| 76 | +std::string parseExtensionFunctionSignatures() { |
| 77 | + auto root_abs_path = omnisci::get_root_abs_path(); |
| 78 | + std::string ext_ast_path = root_abs_path + "/QueryEngine/ExtensionFunctions.ast"; |
| 79 | + std::ifstream fs(ext_ast_path); |
| 80 | + if (!fs.is_open()) { |
| 81 | + throw std::runtime_error("Cannot open extension functions file: " + ext_ast_path); |
| 82 | + } |
| 83 | + |
| 84 | + std::regex sig_parser("\\| (?:[\\` ]|used)+ ([\\w]+) '([\\w<>]+) \\((.*)\\)'"); |
| 85 | + std::smatch match_res; |
| 86 | + std::stringstream ss; |
| 87 | + ss << "[\n"; |
| 88 | + bool first = true; |
| 89 | + for (std::string line; std::getline(fs, line);) { |
| 90 | + if (!std::regex_match(line, match_res, sig_parser)) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + std::vector<std::string> arg_types; |
| 95 | + boost::split(arg_types, match_res[3].str(), boost::is_any_of(",")); |
| 96 | + |
| 97 | + if (first) { |
| 98 | + first = false; |
| 99 | + } else { |
| 100 | + ss << ",\n"; |
| 101 | + } |
| 102 | + |
| 103 | + ss << "{\n \"name\": \"" << match_res[1].str() << "\",\n" |
| 104 | + << " \"ret\": \"" << canonizeType(match_res[2].str()) << "\",\n" |
| 105 | + << " \"args\": [\n"; |
| 106 | + for (size_t i = 0; i < arg_types.size(); ++i) { |
| 107 | + ss << (i ? ",\n \"" : " \"") << canonizeType(boost::trim_copy(arg_types[i])) |
| 108 | + << "\""; |
| 109 | + } |
| 110 | + ss << "\n ]\n}"; |
| 111 | + } |
| 112 | + ss << "\n]\n"; |
| 113 | + |
| 114 | + return ss.str(); |
| 115 | +} |
| 116 | + |
| 117 | +} // namespace |
| 118 | + |
9 | 119 | CalciteMgr::~CalciteMgr() {}
|
10 | 120 |
|
11 | 121 | CalciteMgr* CalciteMgr::get(const std::string& udf_filename,
|
@@ -35,7 +145,11 @@ std::string CalciteMgr::process(const std::string&,
|
35 | 145 | }
|
36 | 146 |
|
37 | 147 | std::string CalciteMgr::getExtensionFunctionWhitelist() {
|
38 |
| - return "[]"; |
| 148 | + static std::string cached_res = ""; |
| 149 | + if (cached_res.empty()) { |
| 150 | + cached_res = parseExtensionFunctionSignatures(); |
| 151 | + } |
| 152 | + return cached_res; |
39 | 153 | }
|
40 | 154 |
|
41 | 155 | std::string CalciteMgr::getUserDefinedFunctionWhitelist() {
|
|
0 commit comments