generated from duckdb/extension-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Maxxen/main
- Loading branch information
Showing
9 changed files
with
161 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule duckdb
updated
2266 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#include "duckdb/function/table_macro_function.hpp" | ||
#include "duckdb/main/extension_util.hpp" | ||
#include "duckdb/optimizer/matcher/expression_matcher.hpp" | ||
#include "hnsw/hnsw.hpp" | ||
#include "hnsw/hnsw_index.hpp" | ||
#include "duckdb/parser/parser.hpp" | ||
|
||
namespace duckdb { | ||
|
||
static constexpr auto VSS_JOIN_MACRO = R"( | ||
SELECT | ||
score, | ||
left_tbl, | ||
right_tbl, | ||
FROM | ||
(SELECT * FROM query_table(left_table::VARCHAR)) as left_tbl, | ||
( | ||
SELECT | ||
struct_pack(*columns([x for x in (matches.*) if x != 'score'])) as right_tbl, | ||
matches.score as score | ||
FROM ( | ||
SELECT ( | ||
unnest( | ||
CASE WHEN metric = 'l2sq' OR metric = 'l2' | ||
THEN min_by(tbl, tbl.score, k) | ||
ELSE max_by(tbl, tbl.score, k) | ||
END, | ||
max_depth := 2 | ||
) | ||
) as result | ||
FROM ( | ||
SELECT | ||
*, | ||
CASE | ||
WHEN metric = 'l2sq' OR metric = 'l2' | ||
THEN array_distance(left_col, right_col) | ||
WHEN metric = 'cosine' OR metric = 'cos' | ||
THEN array_cosine_similarity(left_col, right_col) | ||
WHEN metric = 'ip' | ||
THEN array_inner_product(left_col, right_col) | ||
ELSE error('Unknown metric') | ||
END as score, | ||
FROM query_table(right_table::VARCHAR) | ||
) as tbl | ||
) as matches | ||
) | ||
)"; | ||
|
||
static constexpr auto VSS_MATCH_MACRO = R"( | ||
SELECT | ||
right_tbl as matches, | ||
FROM | ||
( | ||
SELECT ( | ||
CASE WHEN metric = 'l2sq' OR metric = 'l2' | ||
THEN min_by({'score': score, 'row': t}, score, k) | ||
ELSE max_by({'score': score, 'row': t}, score, k) | ||
END | ||
) as right_tbl | ||
FROM ( | ||
SELECT | ||
CASE | ||
WHEN metric = 'l2sq' OR metric = 'l2' | ||
THEN array_distance(left_col, right_col) | ||
WHEN metric = 'cosine' OR metric = 'cos' | ||
THEN array_cosine_similarity(left_col, right_col) | ||
WHEN metric = 'ip' | ||
THEN array_inner_product(left_col, right_col) | ||
ELSE error('Unknown metric') | ||
END as score, | ||
tbl as t, | ||
FROM (SELECT * FROM query_table(right_table::VARCHAR)) as tbl | ||
) | ||
) | ||
)"; | ||
|
||
//------------------------------------------------------------------------- | ||
// Register | ||
//------------------------------------------------------------------------- | ||
static void RegisterTableMacro(DatabaseInstance &db, const string &name, const string &query, | ||
const vector<string> ¶ms, const child_list_t<Value> &named_params) { | ||
|
||
Parser parser; | ||
parser.ParseQuery(query); | ||
const auto &stmt = parser.statements.back(); | ||
auto &node = stmt->Cast<SelectStatement>().node; | ||
|
||
auto func = make_uniq<TableMacroFunction>(std::move(node)); | ||
for (auto ¶m : params) { | ||
func->parameters.push_back(make_uniq<ColumnRefExpression>(param)); | ||
} | ||
|
||
for (auto ¶m : named_params) { | ||
func->default_parameters[param.first] = make_uniq<ConstantExpression>(param.second); | ||
} | ||
|
||
CreateMacroInfo info(CatalogType::TABLE_MACRO_ENTRY); | ||
info.schema = DEFAULT_SCHEMA; | ||
info.name = name; | ||
info.temporary = true; | ||
info.internal = true; | ||
info.macros.push_back(std::move(func)); | ||
|
||
ExtensionUtil::RegisterFunction(db, info); | ||
} | ||
|
||
void HNSWModule::RegisterMacros(DatabaseInstance &db) { | ||
|
||
RegisterTableMacro(db, "vss_join", VSS_JOIN_MACRO, {"left_table", "right_table", "left_col", "right_col", "k"}, | ||
{{"metric", Value("l2sq")}}); | ||
|
||
RegisterTableMacro(db, "vss_match", VSS_MATCH_MACRO, {"right_table", "left_col", "right_col", "k"}, | ||
{{"metric", Value("l2sq")}}); | ||
} | ||
|
||
} // namespace duckdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require vss | ||
|
||
statement ok | ||
CREATE TABLE t1 (id int, vec FLOAT[3]); | ||
|
||
statement ok | ||
INSERT INTO t1 SELECT row_number() over (), array_value(a,b,c) FROM range(1,10) ra(a), range(1,10) rb(b), range(1,10) rc(c); | ||
|
||
statement ok | ||
CREATE TABLE s(s_vec FLOAT[3]); | ||
|
||
statement ok | ||
INSERT INTO s VALUES ([5,5,5]), ([1,1,1]); | ||
|
||
query I | ||
SELECT bool_and(score <= 1.0) FROM vss_join(s, t1, s_vec, vec, 3) as res; | ||
---- | ||
true | ||
|
||
query I | ||
SELECT len(matches) = 3 FROM s, vss_match(t1, s_vec, vec, 3) as res; | ||
---- | ||
true | ||
true |