From 15c2b77f1173f2ca9d728e0db4bdd6045ebb6187 Mon Sep 17 00:00:00 2001 From: Carlo Piovesan Date: Tue, 19 Sep 2023 12:05:45 +0200 Subject: [PATCH] duckdbvfs fixes --- src/include/sqlite_scanner.hpp | 3 +- src/sqlite/duckdbvfs.cpp | 93 ++++++++++++++++------------------ src/sqlite/sqlite3.c | 4 +- src/sqlite_extension.cpp | 1 + src/sqlite_scanner.cpp | 13 +++-- 5 files changed, 54 insertions(+), 60 deletions(-) diff --git a/src/include/sqlite_scanner.hpp b/src/include/sqlite_scanner.hpp index 10bfd53..52530cf 100644 --- a/src/include/sqlite_scanner.hpp +++ b/src/include/sqlite_scanner.hpp @@ -13,7 +13,8 @@ namespace duckdb { class SQLiteDB; -ClientContext * getclientcontext(); +DatabaseInstance *getdb(void); +void setdb(DatabaseInstance *); struct SqliteBindData : public TableFunctionData { string file_name; diff --git a/src/sqlite/duckdbvfs.cpp b/src/sqlite/duckdbvfs.cpp index a7d1f95..dd1eb61 100644 --- a/src/sqlite/duckdbvfs.cpp +++ b/src/sqlite/duckdbvfs.cpp @@ -119,18 +119,6 @@ #include "sqlite3.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - /* ** Size of the write buffer used by journal files in bytes. */ @@ -345,7 +333,6 @@ static int duckdbSync(sqlite3_file *pFile, int flags){ static int duckdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ DemoFile *p = (DemoFile*)pFile; int rc; /* Return code from fstat() call */ - struct stat sStat; /* Output of fstat() call */ /* Flush the contents of the buffer to disk. As with the flush in the ** duckdbRead() method, it would be possible to avoid this and save a write @@ -423,7 +410,8 @@ static int duckdbOpen( duckdbSectorSize, /* xSectorSize */ duckdbDeviceCharacteristics /* xDeviceCharacteristics */ }; - auto &fs = duckdb::getclientcontext()->db->GetFileSystem(); +D_ASSERT(duckdb::getdb()); + auto &fs = duckdb::getdb()->GetFileSystem(); DemoFile *p = (DemoFile*)pFile; /* Populate this structure */ int oflags = 0; /* flags to pass to open() call */ @@ -466,33 +454,36 @@ static int duckdbOpen( ** file has been synced to disk before returning. */ static int duckdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ - int rc; /* Return code */ + //FIXME: This might need to be properly implemented + return SQLITE_OK; - rc = unlink(zPath); - if( rc!=0 && errno==ENOENT ) return SQLITE_OK; +// int rc; /* Return code */ - if( rc==0 && dirSync ){ - int dfd; /* File descriptor open on directory */ - char *zSlash; - char zDir[MAXPATHNAME+1]; /* Name of directory containing file zPath */ +// rc = unlink(zPath); +// if( rc!=0 && errno==ENOENT ) return SQLITE_OK; + +// if( rc==0 && dirSync ){ +// int dfd; /* File descriptor open on directory */ +// char *zSlash; +// char zDir[MAXPATHNAME+1]; /* Name of directory containing file zPath */ /* Figure out the directory name from the path of the file deleted. */ - sqlite3_snprintf(MAXPATHNAME, zDir, "%s", zPath); - zDir[MAXPATHNAME] = '\0'; - zSlash = strrchr(zDir,'/'); - if( zSlash ){ - /* Open a file-descriptor on the directory. Sync. Close. */ - zSlash[0] = 0; - dfd = open(zDir, O_RDONLY, 0); - if( dfd<0 ){ - rc = -1; - }else{ - rc = fsync(dfd); - close(dfd); - } - } - } - return (rc==0 ? SQLITE_OK : SQLITE_IOERR_DELETE); +// sqlite3_snprintf(MAXPATHNAME, zDir, "%s", zPath); +// zDir[MAXPATHNAME] = '\0'; +// zSlash = strrchr(zDir,'/'); +// if( zSlash ){ +// /* Open a file-descriptor on the directory. Sync. Close. */ +// zSlash[0] = 0; +// dfd = open(zDir, O_RDONLY, 0); +// if( dfd<0 ){ +// rc = -1; +// }else{ +// rc = fsync(dfd); +// close(dfd); +// } +// } +// } +// return (rc==0 ? SQLITE_OK : SQLITE_IOERR_DELETE); } #ifndef F_OK @@ -515,20 +506,23 @@ static int duckdbAccess( int flags, int *pResOut ){ - int rc; /* access() return code */ - int eAccess = F_OK; /* Second argument to access() */ + //FIXME: This might need to be properly implemented + *pResOut = false; + return SQLITE_OK; +// int rc; /* access() return code */ +// int eAccess = F_OK; /* Second argument to access() */ - assert( flags==SQLITE_ACCESS_EXISTS /* access(zPath, F_OK) */ - || flags==SQLITE_ACCESS_READ /* access(zPath, R_OK) */ - || flags==SQLITE_ACCESS_READWRITE /* access(zPath, R_OK|W_OK) */ - ); +// assert( flags==SQLITE_ACCESS_EXISTS /* access(zPath, F_OK) */ +// || flags==SQLITE_ACCESS_READ /* access(zPath, R_OK) */ +// || flags==SQLITE_ACCESS_READWRITE /* access(zPath, R_OK|W_OK) */ +// ); - if( flags==SQLITE_ACCESS_READWRITE ) eAccess = R_OK|W_OK; - if( flags==SQLITE_ACCESS_READ ) eAccess = R_OK; +// if( flags==SQLITE_ACCESS_READWRITE ) eAccess = R_OK|W_OK; +// if( flags==SQLITE_ACCESS_READ ) eAccess = R_OK; - rc = access(zPath, eAccess); - *pResOut = (rc==0); - return SQLITE_OK; +// rc = access(zPath, eAccess); +// *pResOut = (rc==0); +// return SQLITE_OK; } /* @@ -602,8 +596,7 @@ static int duckdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zByte){ ** of microseconds slept for. */ static int duckdbSleep(sqlite3_vfs *pVfs, int nMicro){ - sleep(nMicro / 1000000); - usleep(nMicro % 1000000); + //FIXME: This might need to be properly implemented return nMicro; } diff --git a/src/sqlite/sqlite3.c b/src/sqlite/sqlite3.c index 6bb4456..0c94acc 100644 --- a/src/sqlite/sqlite3.c +++ b/src/sqlite/sqlite3.c @@ -42768,7 +42768,7 @@ static int proxyClose(sqlite3_file *id) { ******************************************************************************/ -sqlite3_vfs *sqlite3_duckdbvfs(); +sqlite3_vfs *sqlite3_duckdbvfs(void); /* ** Initialize the operating system interface. @@ -42828,7 +42828,6 @@ SQLITE_API int sqlite3_os_init(void){ unixGetSystemCall, /* xGetSystemCall */ \ unixNextSystemCall, /* xNextSystemCall */ \ } - sqlite3_vfs_register(sqlite3_duckdbvfs(), 0); /* ** All default VFSes for unix are contained in the following array. ** @@ -42873,6 +42872,7 @@ SQLITE_API int sqlite3_os_init(void){ // We DON'T register any other vfs, so duckdbvfs is the only one used //sqlite3_vfs_register(&aVfs[i], i==0); } + sqlite3_vfs_register(sqlite3_duckdbvfs(), 0); unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); #ifndef SQLITE_OMIT_WAL diff --git a/src/sqlite_extension.cpp b/src/sqlite_extension.cpp index aff292d..ab08df7 100644 --- a/src/sqlite_extension.cpp +++ b/src/sqlite_extension.cpp @@ -16,6 +16,7 @@ using namespace duckdb; extern "C" { static void LoadInternal(DatabaseInstance &db) { + setdb(&db); SqliteScanFunction sqlite_fun; ExtensionUtil::RegisterFunction(db, sqlite_fun); diff --git a/src/sqlite_scanner.cpp b/src/sqlite_scanner.cpp index 6227b60..4b2deec 100644 --- a/src/sqlite_scanner.cpp +++ b/src/sqlite_scanner.cpp @@ -17,9 +17,12 @@ namespace duckdb { -ClientContext * clientcontext = nullptr; -ClientContext * getclientcontext() { - return clientcontext; +DatabaseInstance *_db; +void setdb(DatabaseInstance *db) { + _db=db; +} +DatabaseInstance * getdb() { + return _db; } struct SqliteLocalState : public LocalTableFunctionState { @@ -48,7 +51,6 @@ struct SqliteGlobalState : public GlobalTableFunctionState { static unique_ptr SqliteBind(ClientContext &context, TableFunctionBindInput &input, vector &return_types, vector &names) { - auto result = make_uniq(); result->file_name = input.inputs[0].GetValue(); result->table_name = input.inputs[1].GetValue(); @@ -88,7 +90,6 @@ static unique_ptr SqliteBind(ClientContext &context, TableFunction static void SqliteInitInternal(ClientContext &context, const SqliteBindData &bind_data, SqliteLocalState &local_state, idx_t rowid_min, idx_t rowid_max) { - clientcontext = &context; D_ASSERT(rowid_min <= rowid_max); local_state.done = false; @@ -277,7 +278,6 @@ struct AttachFunctionData : public TableFunctionData { static unique_ptr AttachBind(ClientContext &context, TableFunctionBindInput &input, vector &return_types, vector &names) { - clientcontext = &context; auto result = make_uniq(); result->file_name = input.inputs[0].GetValue(); @@ -294,7 +294,6 @@ static unique_ptr AttachBind(ClientContext &context, TableFunction } static void AttachFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { - clientcontext = &context; auto &data = data_p.bind_data->CastNoConst(); if (data.finished) { return;