Skip to content
This repository was archived by the owner on May 9, 2024. It is now read-only.

Commit ed49825

Browse files
committed
Add extension functions support to CalciteStub.
Signed-off-by: ienkovich <[email protected]>
1 parent 0405414 commit ed49825

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

omniscidb/Calcite/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ if(ENABLE_SQL)
7272
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/java/calcite/target/calcite-1.0-SNAPSHOT-jar-with-dependencies.jar DESTINATION bin COMPONENT "jar")
7373
else()
7474
add_library(Calcite CalciteStub.cpp SchemaJson.cpp)
75-
target_link_libraries(Calcite PRIVATE IR OSDependent Shared)
75+
target_link_libraries(Calcite PRIVATE IR OSDependent Shared ${Boost_LIBRARIES})
7676
endif()

omniscidb/Calcite/CalciteStub.cpp

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,116 @@
66

77
#include "CalciteJNI.h"
88

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+
9119
CalciteMgr::~CalciteMgr() {}
10120

11121
CalciteMgr* CalciteMgr::get(const std::string& udf_filename,
@@ -35,7 +145,11 @@ std::string CalciteMgr::process(const std::string&,
35145
}
36146

37147
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;
39153
}
40154

41155
std::string CalciteMgr::getUserDefinedFunctionWhitelist() {

0 commit comments

Comments
 (0)