This commit is contained in:
2023-05-20 07:08:10 +08:00
parent dfb3ec2380
commit d0f0b4fc56
24 changed files with 285 additions and 60 deletions
+1
View File
@@ -44,6 +44,7 @@ struct DataSource {
virtual void connect(Context* cxt) = 0;
virtual void exec(const char* q) = 0;
virtual void* getCol(int col_idx, int type) = 0;
virtual void getDSTable(const char* name, void* tbl) = 0;
// virtual long long getFirstElement() = 0;
virtual void close() = 0;
virtual bool haserror() = 0;
+5
View File
@@ -0,0 +1,5 @@
#define __AQUERY_TYPES__ \
AINT32, AFLOAT, ASTR, ADOUBLE, ALDOUBLE, AINT64, AINT128, \
AINT16, ADATE, ATIME, AINT8, AUINT32, AUINT64, AUINT128, \
AUINT16, AUINT8, ABOOL, VECTOR, ATIMESTAMP, ACHAR, ASV, \
NONE, ERROR
+5 -1
View File
@@ -1,6 +1,6 @@
#include "pch_msc.hpp"
#include "duckdb_conn.h"
#include "../deps/duckdb.hpp"
#include "duckdb.hpp"
#include "libaquery.h"
#include "types.h"
#include <cstdio>
@@ -73,6 +73,10 @@ void* DuckdbServer::getCol(int col_idx, int ty) {
}
}
void DuckdbServer::getDSTable(const char* name, void* tbl) {
// not implemented.
puts("NOT IMPLEMENTED ERROR: DuckdbServer::getDSTable");
}
bool DuckdbServer::haserror() {
if (last_error) {
puts(last_error);
+2 -1
View File
@@ -6,7 +6,8 @@ struct DuckdbServer : DataSource {
explicit DuckdbServer(Context* cxt);
void connect(Context* cxt);
void exec(const char* q);
void* getCol(int col_idx, int type);
void* getCol(int col_idx, int type) override;
void getDSTable(const char* name, void* tbl) override;
long long getFirstElement();
void close();
bool haserror();
+1
View File
@@ -104,6 +104,7 @@ public:
}
constexpr static void(*_free) (void*) = free;
};
#else
class GC {
public:
+64
View File
@@ -7,6 +7,7 @@
#include "types.h"
// #include "robin_hood.h"
#include "unordered_dense.h"
template<typename Key, typename Val>
using aq_map = ankerl::unordered_dense::map<Key, Val>;
@@ -137,3 +138,66 @@ namespace ankerl::unordered_dense{
struct hash<std::tuple<Types...>> : public hasher<Types...>{ };
}
template <
typename ValueType = bool,
int PerfectHashingThreshold = 12
>
struct PerfectHashTable {
// static int m_PerfectHashingThreshold = 12;
using key_t = std::conditional_t<PerfectHashingThreshold <= 8, uint8_t,
std::conditional_t<PerfectHashingThreshold <= 16, uint16_t,
std::conditional_t<PerfectHashingThreshold <= 32, uint32_t,
uint64_t
>>>;
int n_cols, n_rows = 0;
// char bits[32];
ValueType table[1 << PerfectHashingThreshold];
// PerfectHashTable(int n_cols, char* bits) {
// this->n_cols = n_cols;
// memcpy(this->bits, bits, 32);
// }
// template<typename ... Types, template <typename> class VT>
// PerfectHashTable(VT<Types> ... args) {
// }
template <typename ... Types, template <typename> class VT>
void construct(VT<Types>&... args) {
((this->n_cols = args.size), ...);
static_assert(
(sizeof...(Types) < PerfectHashingThreshold) &&
//(sizeof(Types) + ...) < PerfectHashingThreshold &&
(std::is_integral_v<Types> && ...),
"Types must be integral and less than 12 wide in total."
);
// this should be an attrib of VT.
key_t* // this better be automatically determined by Threshould
hash_values = static_cast<key_t*>(
calloc(this->n_cols, sizeof(key_t))
);
//new short[this->n_cols] {0}; // use calloc/delete
auto get_hash = [&hash_values](auto& arg, int idx) {
uint32_t i = 0;
if(idx > 0)
for (auto& a : arg) {
hash_values[i] =
(hash_values[i] << arg.stats.bits) +
(a - arg.stats.minima);
++i;
}
else
for (auto& a : arg) {
hash_values[i] = a - arg.stats.minima;
++i;
}
};
int idx = 0;
(get_hash(args, idx++), ...);
for (uint32_t i = 0; i < this->n_cols; ++i) {
this->table[hash_values[i]] = true;
// problem: random memory access
}
// delete[] hash_values;
free(hash_values);
}
};
+7
View File
@@ -235,6 +235,13 @@ inline _This_Type* AQ_DupObject(_This_Type* __val) {
return ret;
}
inline char* AQ_DupString(const char* __val) {
auto __len = strlen(__val) + 1;
auto ret = (char*)malloc(__len);
memcpy(ret, __val, __len);
return ret;
}
#ifdef __USE_STD_SEMAPHORE__
#include <semaphore>
class A_Semaphore {
+14
View File
@@ -267,6 +267,20 @@ long long MonetdbServer::getFirstElement() {
return 0;
}
void MonetdbServer::getDSTable(const char* name, void* tbl) {
TableInfo<void> *table = static_cast<TableInfo<void>*>(tbl);
void*** cols = static_cast<void***>(alloca(table->n_cols * sizeof(void**)));
printf("\tncols: %d\n", table->n_cols);
for (int i = 0; i < table->n_cols; ++i) {
cols[i] = static_cast<void**>(
static_cast<void*>(
&(table->colrefs[i].container)
)
);
}
monetdbe_get_cols(*(void**)(this->server), name, cols, table->n_cols);
}
MonetdbServer::~MonetdbServer(){
close();
}
+6 -1
View File
@@ -7,6 +7,7 @@ struct MonetdbServer : DataSource {
void connect(Context* cxt) override;
void exec(const char* q) override;
void *getCol(int col_idx, int) override;
void getDSTable(const char* name, void* tbl) override;
long long getFirstElement();
void close() override;
bool haserror() override;
@@ -24,8 +25,12 @@ struct monetdbe_table_data{
};
extern "C" size_t
monetdbe_get_size(void* dbhdl, const char *table_name);
monetdbe_get_size(void* dbhdl, const char *table_name, void*);
extern "C" void*
monetdbe_get_col(void* dbhdl, const char *table_name, uint32_t col_id);
extern "C" void
monetdbe_get_cols(void* dbhdl, const char* table_name, void*** cols, int i);
#endif
+34
View File
@@ -80,6 +80,19 @@ monetdbe_get_size(monetdbe_database dbhdl, const char *table_name)
return sz;
}
void *
monetdbe_list_fetch(list *l, int pos)
{
node *n = NULL;
int i;
for (n = l->h, i=0; n && i<pos; n = n->next, i++)
;
if (n)
return n->data;
return NULL;
}
void*
monetdbe_get_col(monetdbe_database dbhdl, const char *table_name, uint32_t col_id) {
monetdbe_database_internal* hdl = (monetdbe_database_internal*)dbhdl;
@@ -95,3 +108,24 @@ monetdbe_get_col(monetdbe_database dbhdl, const char *table_name, uint32_t col_i
//mvc_cancel_session(m);
return iter.base;
}
void monetdbe_get_cols(
monetdbe_database dbhdl,
const char* table_name,
void*** cols,
int i
) {
monetdbe_database_internal* hdl = (monetdbe_database_internal*)dbhdl;
backend* be = ((backend *)(((monetdbe_database_internal*)dbhdl)->c->sqlcontext));
mvc *m = be->mvc;
sql_table *t = find_table_or_view_on_scope(m, NULL, "sys", table_name, "CATALOG", false);
if (!i || !t) return;
node *n = t->columns->l->h;
sqlstore* store = m->store;
while(n && i-- > 0) {
BAT *b = store->storage_api.bind_col(m->session->tr, n->data, QUICK);
BATiter iter = bat_iterator(b);
*(cols++) = iter.base;
n = n->next;
}
}
+31
View File
@@ -549,6 +549,37 @@ start:
}
}
break;
case 'C': //Caching
{
char* cached_table = n_recvd[i] + 1;
char *lazy = (cached_table + 1);
cached_table = AQ_DupString(cached_table);
while(*lazy++);
// get schema
int* n_cols = reinterpret_cast<int *>(lazy + 2);
char* col_schema = reinterpret_cast<char *>(n_cols + 1);
TableInfo<void> *tbl = new TableInfo<void>;
tbl->name = cached_table;//AQ_DupString(cached_table);
tbl->n_cols = *n_cols;
for (int i = 0; i < *n_cols; ++i) {
char* col_name = col_schema;
char* mem_coltype = col_name + 1;
while(*mem_coltype++);
int coltype = *(reinterpret_cast<int*>(mem_coltype));
//
tbl->colrefs[i].name = AQ_DupString(col_name);
tbl->colrefs[i].ty = static_cast<types::Type_t>(coltype);
}
server->getDSTable(cached_table, tbl);
// server->exec( (
// std::string("SELECT * FROM ") + cached_table + std::string(";")
// ).c_str() );
// server->getCol()
// free(cached_table);
break;
}
}
}
+26
View File
@@ -21,6 +21,8 @@ template <>
class vector_type<void>;
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
namespace types {
enum Type_t;
template <typename T>
@@ -54,6 +56,13 @@ std::ostream& operator<<(std::ostream& os, uint8_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 T>
struct TableStats {
T minima = 0;
unsigned char bits = 255;
};
template<typename _Ty>
class ColView;
template<typename _Ty>
@@ -63,6 +72,23 @@ public:
typedef ColRef<_Ty> Decayed_t;
const char* name;
types::Type_t ty = types::Type_t::ERROR;
TableStats<_Ty> stats;
bool populate_stats() {
if constexpr (std::is_integral_v<_Ty>) {
if (stats.bits <= 128) return true;
stats.minima = std::numeric_limits<_Ty>::max();
_Ty maxima = std::numeric_limits<_Ty>::min();
for (uint32_t i = 0; i < this->size; ++i) {
if (this->container[i] < stats.minima)
stats.minima = this->container[i];
else if (this->container[i] > maxima)
maxima = this->container[i];
}
stats.bits = ceil(log2(maxima - stats.minima));
return true;
}
return false;
}
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("") {}
+2 -2
View File
@@ -6,6 +6,7 @@
#include <string_view>
#include <string>
#include <utility>
#include "aquery_types.h"
using std::size_t;
#if defined(__SIZEOF_INT128__) and not defined(_WIN32)
@@ -68,8 +69,7 @@ constexpr bool aqis_same<T1, T2> = aqis_same_impl<T1, T2>::value;
namespace types {
enum Type_t {
AINT32, AFLOAT, ASTR, ADOUBLE, ALDOUBLE, AINT64, AINT128, AINT16, ADATE, ATIME, AINT8,
AUINT32, AUINT64, AUINT128, AUINT16, AUINT8, ABOOL, VECTOR, ATIMESTAMP, ACHAR, ASV, NONE, ERROR
__AQUERY_TYPES__
};
static constexpr const char* printf_str[] = { "%d", "%f", "%s", "%lf", "%Lf", "%ld", "%s", "%hi", "%s", "%s", "%hhd",
"%u", "%lu", "%s", "%hu", "%hhu", "%s", "Vector<%s>", "%s", "%c", "%s", "NULL", "ERROR" };