From 10a7b7eafaf0a34499ac33c0e59424ed7e637424 Mon Sep 17 00:00:00 2001 From: bill Date: Wed, 28 Sep 2022 00:09:47 +0800 Subject: [PATCH] Merge branch 'master' of https://github.com/sunyinqi0508/AQuery2 --- build_instructions.txt | 14 --- msc-plugin/libaquery.vcxproj | 4 + msc-plugin/msc-plugin.vcxproj | 2 + msc-plugin/sdk_example.vcxproj | 4 + sdk/aquery.h | 6 ++ server/aggregations.h | 83 +++++++++----- server/server.cpp | 59 +++++----- server/table.h | 192 +++++++++++++++++---------------- server/types.h | 110 ++++++++++++------- 9 files changed, 274 insertions(+), 200 deletions(-) delete mode 100644 build_instructions.txt diff --git a/build_instructions.txt b/build_instructions.txt deleted file mode 100644 index d5d8297..0000000 --- a/build_instructions.txt +++ /dev/null @@ -1,14 +0,0 @@ -## Windows -- clang-msvc: - - "%CXX%" -D_CRT_SECURE_NO_WARNINGS -shared server/server.cpp server/winhelper.cpp server/monetdb_conn.cpp -Imonetdb/msvc -Lmonetdb/msvc -lmonetdbe.lib --std=c++2a -o server.so - - os.add_dll_directory(os.path.abspath('./monetdb/msvc')) -- gcc-mingw (link w/ msvc monetdb): - - "%CXX%" -shared -fPIC server/server.cpp server/winhelper.cpp server/monetdb_conn.cpp -Imonetdb/msvc msc-plugin/monetdbe.dll --std=c++2a -o server.so - - os.add_dll_directory('c:/msys64/usr/bin') - - os.add_dll_directory(os.path.abspath('./monetdb/msvc')) -- gcc-mingw (link w/ mingw monetdb, can only load under mingw python): - - $(CXX) server/server.cpp server/monetdb_conn.cpp -fPIC -shared $(OS_SUPPORT) --std=c++1z -O3 -march=native -o server.so -I./monetdb/msys64 -L./lib -lmonetdbe - - add_dll_dir(os.path.abspath('./lib')) -- msvc: - - D:\gg\vs22\MSBuild\Current\Bin\msbuild "d:\gg\AQuery++\server\server.vcxproj" /p:configuration=Release /p:platform=x64 - - os.add_dll_directory(os.path.abspath('./monetdb/msvc')) diff --git a/msc-plugin/libaquery.vcxproj b/msc-plugin/libaquery.vcxproj index 7da895f..96b11f8 100644 --- a/msc-plugin/libaquery.vcxproj +++ b/msc-plugin/libaquery.vcxproj @@ -100,6 +100,8 @@ pch.hpp $(ProjectDir)\..\monetdb\msvc ../libaquery.pch + true + true Console @@ -161,6 +163,8 @@ pch.hpp $(ProjectDir)\..\monetdb\msvc ../libaquery.pch + true + true Console diff --git a/msc-plugin/msc-plugin.vcxproj b/msc-plugin/msc-plugin.vcxproj index c187361..e0765b4 100644 --- a/msc-plugin/msc-plugin.vcxproj +++ b/msc-plugin/msc-plugin.vcxproj @@ -108,6 +108,7 @@ ./server/pch.hpp ../libaquery.pch true + true Console @@ -178,6 +179,7 @@ ./server/pch.hpp ../libaquery.pch true + true Console diff --git a/msc-plugin/sdk_example.vcxproj b/msc-plugin/sdk_example.vcxproj index 09ddc80..f55e7a9 100644 --- a/msc-plugin/sdk_example.vcxproj +++ b/msc-plugin/sdk_example.vcxproj @@ -98,6 +98,8 @@ true stdcpplatest stdc17 + true + true Console @@ -130,6 +132,8 @@ true stdcpplatest stdc17 + true + true Console diff --git a/sdk/aquery.h b/sdk/aquery.h index 3ef5bb6..fbe6517 100644 --- a/sdk/aquery.h +++ b/sdk/aquery.h @@ -75,7 +75,13 @@ extern void register_memory(void* ptr, deallocator_t deallocator); __AQEXPORT__(void) init_session(Context* cxt); #define __AQ_NO_SESSION__ __AQEXPORT__(void) init_session(Context*) {} + +#ifdef _MSC_VER +void* _cdecl memcpy(void*, void*, size_t); +#else void* memcpy(void*, const void*, unsigned long long); +#endif + struct ColRef_storage { void* container; unsigned int capacity, size; diff --git a/server/aggregations.h b/server/aggregations.h index 514e4a6..583f246 100644 --- a/server/aggregations.h +++ b/server/aggregations.h @@ -16,7 +16,7 @@ constexpr static inline size_t count(const T&) { return 1; } // TODO: Specializations for dt/str/none template class VT> -types::GetLongType +types::GetLongType sum(const VT& v) { types::GetLongType ret = 0; for (const auto& _v : v) @@ -31,8 +31,8 @@ double avg(const VT& v) { template class VT> VT sqrt(const VT& v) { - VT ret {v.size}; - for (uint32_t i = 0; i < v.size; ++i){ + VT ret{ v.size }; + for (uint32_t i = 0; i < v.size; ++i) { ret[i] = sqrt(v[i]); } return ret; @@ -53,10 +53,10 @@ T min(const VT& v) { return min_v; } template class VT> -decayed_t mins(const VT& arr) { +decayed_t mins(const VT& arr) { const uint32_t& len = arr.size; std::deque> cache; - decayed_t ret(len); + decayed_t ret(len); T min = std::numeric_limits::max(); for (int i = 0; i < len; ++i) { if (arr[i] < min) @@ -66,9 +66,9 @@ decayed_t mins(const VT& arr) { return ret; } template class VT> -decayed_t maxs(const VT& arr) { +decayed_t maxs(const VT& arr) { const uint32_t& len = arr.size; - decayed_t ret(len); + decayed_t ret(len); T max = std::numeric_limits::min(); for (int i = 0; i < len; ++i) { if (arr[i] > max) @@ -79,9 +79,9 @@ decayed_t maxs(const VT& arr) { } template class VT> -decayed_t minw(uint32_t w, const VT& arr) { +decayed_t minw(uint32_t w, const VT& arr) { const uint32_t& len = arr.size; - decayed_t ret{len}; + decayed_t ret{ len }; std::deque> cache; for (int i = 0; i < len; ++i) { if (!cache.empty() && cache.front().second == i - w) cache.pop_front(); @@ -93,7 +93,7 @@ decayed_t minw(uint32_t w, const VT& arr) { } template class VT> -decayed_t maxw(uint32_t w, const VT& arr) { +decayed_t maxw(uint32_t w, const VT& arr) { const uint32_t& len = arr.size; decayed_t ret(len); std::deque> cache; @@ -106,14 +106,27 @@ decayed_t maxw(uint32_t w, const VT& arr) { return ret; } +template class VT> +decayed_t> ratios(const VT& arr) { + uint32_t len = arr.size - 1; + if (!arr.size) + len = 1; + decayed_t> ret(len); + ret[0] = 0; + + for (uint32_t i = 1; i < arr.size; ++i) + ret[i - 1] = arr[i] / arr[i - 1]; + return ret; +} + template class VT> decayed_t> sums(const VT& arr) { const uint32_t& len = arr.size; decayed_t> ret(len); uint32_t i = 0; - if(len) ret[i++] = arr[0]; - for (; i < len; ++i) - ret[i] = ret[i-1] + arr[i]; + if (len) ret[i++] = arr[0]; + for (; i < len; ++i) + ret[i] = ret[i - 1] + arr[i]; return ret; } template class VT> @@ -123,9 +136,9 @@ decayed_t>> avgs(const VT& arr) { decayed_t ret(len); uint32_t i = 0; types::GetLongType s; - if(len) s = ret[i++] = arr[0]; - for (; i < len; ++i) - ret[i] = (s+=arr[i])/(FPType)(i+1); + if (len) s = ret[i++] = arr[0]; + for (; i < len; ++i) + ret[i] = (s += arr[i]) / (FPType)(i + 1); return ret; } template class VT> @@ -134,11 +147,11 @@ decayed_t> sumw(uint32_t w, const VT& arr) { decayed_t> ret(len); uint32_t i = 0; w = w > len ? len : w; - if(len) ret[i++] = arr[0]; - for (; i < w; ++i) - ret[i] = ret[i-1] + arr[i]; - for (; i < len; ++i) - ret[i] = ret[i-1] + arr[i] - arr[i-w]; + if (len) ret[i++] = arr[0]; + for (; i < w; ++i) + ret[i] = ret[i - 1] + arr[i]; + for (; i < len; ++i) + ret[i] = ret[i - 1] + arr[i] - arr[i - w]; return ret; } template class VT> @@ -149,11 +162,11 @@ decayed_t>> avgw(uint32_t w, const VT uint32_t i = 0; types::GetLongType s{}; w = w > len ? len : w; - if(len) s = ret[i++] = arr[0]; - for (; i < w; ++i) - ret[i] = (s += arr[i])/(FPType)(i+1); - for (; i < len; ++i) - ret[i] = ret[i-1] + (arr[i] - arr[i-w])/(FPType)w; + if (len) s = ret[i++] = arr[0]; + for (; i < w; ++i) + ret[i] = (s += arr[i]) / (FPType)(i + 1); + for (; i < len; ++i) + ret[i] = ret[i - 1] + (arr[i] - arr[i - w]) / (FPType)w; return ret; } @@ -163,18 +176,29 @@ decayed_t deltas(const VT& arr) { const uint32_t& len = arr.size; decayed_t ret(len); uint32_t i = 0; - if(len) ret[i++] = 0; - for (; i < len; ++i) - ret[i] = arr[i] - arr[i-1]; + if (len) ret[i++] = 0; + for (; i < len; ++i) + ret[i] = arr[i] - arr[i - 1]; return ret; } template class VT> T last(const VT& arr) { + if (!arr.size) return 0; const uint32_t& len = arr.size; return arr[arr.size - 1]; } +template class VT> +T first(const VT& arr) { + if (!arr.size) return 0; + const uint32_t& len = arr.size; + return arr[0]; +} + +#define __DEFAULT_AGGREGATE_FUNCTION__(NAME, RET) \ +template constexpr inline T NAME(const T& v) { return RET; } + // wrong behavior with count(0) template constexpr inline T count(const T& v) { return 1; } template constexpr inline T max(const T& v) { return v; } @@ -191,3 +215,4 @@ template constexpr inline T avgs(const T& v) { return v; } template constexpr inline T sums(const T& v) { return v; } template constexpr inline T last(const T& v) { return v; } template constexpr inline T daltas(const T& v) { return 0; } +template constexpr inline T ratios(const T& v) { return 1; } diff --git a/server/server.cpp b/server/server.cpp index c92d25a..b10ca80 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -320,35 +320,44 @@ int test_main() Server* server = reinterpret_cast(cxt->alt_server); const char* qs[]= { - "CREATE TABLE test1(a INT, b INT, c INT, d INT);", - "COPY OFFSET 2 INTO test1 FROM 'w:/gg/AQuery++/data/test.csv' ON SERVER USING DELIMITERS ',';", - "SELECT sum(a), b, d, c FROM test1 GROUP BY c, b, d ORDER BY b ;", + "QCREATE TABLE trade(stocksymbol INT, time INT, quantity INT, price INT);", + "QCOPY OFFSET 2 INTO trade FROM 'w:/gg/AQuery++/data/trade_numerical.csv' ON SERVER USING DELIMITERS ',';", + "QSELECT stocksymbol, (SUM((quantity * price)) / SUM(quantity)) AS weighted_average FROM trade GROUP BY stocksymbol ;", + "Pdll_5lYrMY", + "QSELECT stocksymbol, price FROM trade ORDER BY time ;", + "Pdll_4Sg6Ri", + "QSELECT stocksymbol, quantity, price FROM trade ORDER BY time ;", + "Pdll_5h4kL2", + "QSELECT stocksymbol, price FROM trade ORDER BY time ;", + "Pdll_7tEWCO", + "QSELECT query_c.weighted_moving_averages, query_c.stocksymbol FROM query_c;", + "Pdll_7FCPnF" }; n_recv = sizeof(qs)/(sizeof (char*)); n_recvd = const_cast(qs); - if (n_recv > 0) { - for (int i = 0; i < n_recv; ++i) - { - server->exec(n_recvd[i]); - printf("Exec Q%d: %s\n", i, n_recvd[i]); - } - n_recv = 0; - } + void* handle = 0; + handle = dlopen("./dll.so", RTLD_LAZY); + cxt->init_session(); + for (int i = 0; i < n_recv; ++i) + { + //printf("%s, %d\n", n_recvd[i], n_recvd[i][0] == 'Q'); + switch (n_recvd[i][0]) { + case 'Q': // SQL query for monetdbe + { + server->exec(n_recvd[i] + 1); + printf("Exec Q%d: %s\n", i, n_recvd[i]); + } + break; + case 'P': // Postprocessing procedure + if (handle && !server->haserror()) { + code_snippet c = reinterpret_cast(dlsym(handle, n_recvd[i] + 1)); + c(cxt); + } + break; + } + } + n_recv = 0; - cxt->log_level = LOG_INFO; - puts(cpp_17 ?"true":"false"); - void* handle = dlopen("./dll.so", RTLD_LAZY); - cxt->log("handle: %p\n", handle); - if (handle) { - cxt->log("inner\n"); - code_snippet c = reinterpret_cast(dlsym(handle, "dll_C4nJZu")); - cxt->log("routine: %p\n", c); - if (c) { - cxt->log("inner\n"); - cxt->log("return: %d\n", c(cxt)); - } - dlclose(handle); - } //static_assert(std::is_same_v()), std::integer_sequence>, ""); return 0; diff --git a/server/table.h b/server/table.h index 1e07968..76f5fb0 100644 --- a/server/table.h +++ b/server/table.h @@ -34,13 +34,13 @@ std::ostream& operator<<(std::ostream& os, const VT& v) } #ifdef __AQ__HAS__INT128__ -std::ostream& operator<<(std::ostream& os, __int128 & v); -std::ostream& operator<<(std::ostream& os, __uint128_t & v); +std::ostream& operator<<(std::ostream& os, __int128& v); +std::ostream& operator<<(std::ostream& os, __uint128_t& v); #endif -std::ostream& operator<<(std::ostream& os, types::date_t & v); -std::ostream& operator<<(std::ostream& os, types::time_t & v); -std::ostream& operator<<(std::ostream& os, types::timestamp_t & v); +std::ostream& operator<<(std::ostream& os, types::date_t& v); +std::ostream& operator<<(std::ostream& os, types::time_t& v); +std::ostream& operator<<(std::ostream& os, types::timestamp_t& v); template class ColView; template @@ -50,27 +50,27 @@ public: typedef ColRef<_Ty> Decayed_t; const char* name; types::Type_t ty = types::Type_t::ERROR; - ColRef(const ColRef<_Ty>& vt) : vector_type<_Ty>(vt) {} + ColRef(const ColRef<_Ty>& vt) : vector_type<_Ty>(vt) {} ColRef(ColRef<_Ty>&& vt) : vector_type<_Ty>(std::move(vt)) {} ColRef() : vector_type<_Ty>(0), name("") {} ColRef(const uint32_t& size, const char* name = "") : vector_type<_Ty>(size), name(name) {} ColRef(const char* name) : name(name) {} ColRef(const uint32_t size, void* data, const char* name = "") : vector_type<_Ty>(size, data), name(name) {} void init(const char* name = "") { ty = types::Types<_Ty>::getType(); this->size = this->capacity = 0; this->container = 0; this->name = name; } - void initfrom(uint32_t sz, void*container, const char* name = "") { - ty = types::Types<_Ty>::getType(); + void initfrom(uint32_t sz, void* container, const char* name = "") { + ty = types::Types<_Ty>::getType(); this->size = sz; this->capacity = 0; - this->container = (_Ty*)container; - this->name = name; + this->container = (_Ty*)container; + this->name = name; } template