@@ -806,8 +806,10 @@ void *vector_context_create (void) {
806806
807807table_context * vector_context_lookup (vector_context * ctx , const char * table_name , const char * column_name ) {
808808 for (int i = 0 ; i < ctx -> table_count ; ++ i ) {
809- if ((strcasecmp (ctx -> tables [i ].t_name , table_name ) == 0 ) &&
810- (strcasecmp (ctx -> tables [i ].c_name , column_name ) == 0 )) return & ctx -> tables [i ];
809+ // tname and cname can be NULL after adding vector_cleanup function
810+ const char * tname = ctx -> tables [i ].t_name ;
811+ const char * cname = ctx -> tables [i ].c_name ;
812+ if (tname && cname && (strcasecmp (tname , table_name ) == 0 ) && (strcasecmp (cname , column_name ) == 0 )) return & ctx -> tables [i ];
811813 }
812814 return NULL ;
813815}
@@ -1150,6 +1152,33 @@ static void vector_quantize_preload (sqlite3_context *context, int argc, sqlite3
11501152 return ;
11511153}
11521154
1155+ static void vector_cleanup (sqlite3_context * context , int argc , sqlite3_value * * argv ) {
1156+ int types [] = {SQLITE_TEXT , SQLITE_TEXT };
1157+ if (sanity_check_args (context , "vector_cleanup" , argc , argv , 2 , types ) == false) return ;
1158+
1159+ const char * table_name = (const char * )sqlite3_value_text (argv [0 ]);
1160+ const char * column_name = (const char * )sqlite3_value_text (argv [1 ]);
1161+
1162+ vector_context * v_ctx = (vector_context * )sqlite3_user_data (context );
1163+ table_context * t_ctx = vector_context_lookup (v_ctx , table_name , column_name );
1164+ if (!t_ctx ) return ; // if no table context exists then do nothing
1165+
1166+ // release memory
1167+ if (t_ctx -> t_name ) sqlite3_free (t_ctx -> t_name );
1168+ if (t_ctx -> c_name ) sqlite3_free (t_ctx -> c_name );
1169+ if (t_ctx -> pk_name ) sqlite3_free (t_ctx -> pk_name );
1170+ if (t_ctx -> preloaded ) sqlite3_free (t_ctx -> preloaded );
1171+ memset (t_ctx , 0 , sizeof (table_context ));
1172+
1173+ // drop quant table (if any)
1174+ char sql [STATIC_SQL_SIZE ];
1175+ sqlite3 * db = sqlite3_context_db_handle (context );
1176+ generate_drop_quant_table (table_name , column_name , sql );
1177+ sqlite3_exec (db , sql , NULL , NULL , NULL );
1178+
1179+ // do not decrease v_ctx->table_count
1180+ }
1181+
11531182// MARK: -
11541183
11551184static char * vector_convert_from_json (sqlite3_context * context , vector_type type , const char * json , int * size ) {
@@ -1818,6 +1847,10 @@ SQLITE_VECTOR_API int sqlite3_vector_init (sqlite3 *db, char **pzErrMsg, const s
18181847 rc = sqlite3_create_function (db , "vector_quantize_preload" , 2 , SQLITE_UTF8 , ctx , vector_quantize_preload , NULL , NULL );
18191848 if (rc != SQLITE_OK ) goto cleanup ;
18201849
1850+ // table_name, column_name
1851+ rc = sqlite3_create_function (db , "vector_cleanup" , 2 , SQLITE_UTF8 , ctx , vector_cleanup , NULL , NULL );
1852+ if (rc != SQLITE_OK ) goto cleanup ;
1853+
18211854 rc = sqlite3_create_module (db , "vector_full_scan" , & vFullScanModule , ctx );
18221855 if (rc != SQLITE_OK ) goto cleanup ;
18231856
0 commit comments