Elffuss BraidHub

kikocis/lancedb public

bbc8a2ecbe2c · 5.8 KiBRawPermalink
1// Copyright 2024 Lance Developers.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use lancedb::index::scalar::FullTextSearchQuery;
16use lancedb::query::ExecutableQuery;
17use lancedb::query::Query as LanceDbQuery;
18use lancedb::query::QueryBase;
19use lancedb::query::QueryExecutionOptions;
20use lancedb::query::Select;
21use lancedb::query::VectorQuery as LanceDbVectorQuery;
22use napi::bindgen_prelude::*;
23use napi_derive::napi;
24
25use crate::error::NapiErrorExt;
26use crate::iterator::RecordBatchIterator;
27use crate::util::parse_distance_type;
28
29#[napi]
30pub struct Query {
31 inner: LanceDbQuery,
32}
33
34#[napi]
35impl Query {
36 pub fn new(query: LanceDbQuery) -> Self {
37 Self { inner: query }
38 }
39
40 // We cannot call this r#where because NAPI gets confused by the r#
41 #[napi]
42 pub fn only_if(&mut self, predicate: String) {
43 self.inner = self.inner.clone().only_if(predicate);
44 }
45
46 #[napi]
47 pub fn full_text_search(&mut self, query: String, columns: Option<Vec<String>>) {
48 let query = FullTextSearchQuery::new(query).columns(columns);
49 self.inner = self.inner.clone().full_text_search(query);
50 }
51
52 #[napi]
53 pub fn select(&mut self, columns: Vec<(String, String)>) {
54 self.inner = self.inner.clone().select(Select::dynamic(&columns));
55 }
56
57 #[napi]
58 pub fn select_columns(&mut self, columns: Vec<String>) {
59 self.inner = self.inner.clone().select(Select::columns(&columns));
60 }
61
62 #[napi]
63 pub fn limit(&mut self, limit: u32) {
64 self.inner = self.inner.clone().limit(limit as usize);
65 }
66
67 #[napi]
68 pub fn nearest_to(&mut self, vector: Float32Array) -> Result<VectorQuery> {
69 let inner = self
70 .inner
71 .clone()
72 .nearest_to(vector.as_ref())
73 .default_error()?;
74 Ok(VectorQuery { inner })
75 }
76
77 #[napi(catch_unwind)]
78 pub async fn execute(
79 &self,
80 max_batch_length: Option<u32>,
81 ) -> napi::Result<RecordBatchIterator> {
82 let mut execution_opts = QueryExecutionOptions::default();
83 if let Some(max_batch_length) = max_batch_length {
84 execution_opts.max_batch_length = max_batch_length;
85 }
86 let inner_stream = self
87 .inner
88 .execute_with_options(execution_opts)
89 .await
90 .map_err(|e| {
91 napi::Error::from_reason(format!("Failed to execute query stream: {}", e))
92 })?;
93 Ok(RecordBatchIterator::new(inner_stream))
94 }
95
96 #[napi]
97 pub async fn explain_plan(&self, verbose: bool) -> napi::Result<String> {
98 self.inner.explain_plan(verbose).await.map_err(|e| {
99 napi::Error::from_reason(format!("Failed to retrieve the query plan: {}", e))
100 })
101 }
102}
103
104#[napi]
105pub struct VectorQuery {
106 inner: LanceDbVectorQuery,
107}
108
109#[napi]
110impl VectorQuery {
111 #[napi]
112 pub fn column(&mut self, column: String) {
113 self.inner = self.inner.clone().column(&column);
114 }
115
116 #[napi]
117 pub fn distance_type(&mut self, distance_type: String) -> napi::Result<()> {
118 let distance_type = parse_distance_type(distance_type)?;
119 self.inner = self.inner.clone().distance_type(distance_type);
120 Ok(())
121 }
122
123 #[napi]
124 pub fn postfilter(&mut self) {
125 self.inner = self.inner.clone().postfilter();
126 }
127
128 #[napi]
129 pub fn refine_factor(&mut self, refine_factor: u32) {
130 self.inner = self.inner.clone().refine_factor(refine_factor);
131 }
132
133 #[napi]
134 pub fn nprobes(&mut self, nprobe: u32) {
135 self.inner = self.inner.clone().nprobes(nprobe as usize);
136 }
137
138 #[napi]
139 pub fn bypass_vector_index(&mut self) {
140 self.inner = self.inner.clone().bypass_vector_index()
141 }
142
143 #[napi]
144 pub fn only_if(&mut self, predicate: String) {
145 self.inner = self.inner.clone().only_if(predicate);
146 }
147
148 #[napi]
149 pub fn full_text_search(&mut self, query: String, columns: Option<Vec<String>>) {
150 let query = FullTextSearchQuery::new(query).columns(columns);
151 self.inner = self.inner.clone().full_text_search(query);
152 }
153
154 #[napi]
155 pub fn select(&mut self, columns: Vec<(String, String)>) {
156 self.inner = self.inner.clone().select(Select::dynamic(&columns));
157 }
158
159 #[napi]
160 pub fn select_columns(&mut self, columns: Vec<String>) {
161 self.inner = self.inner.clone().select(Select::columns(&columns));
162 }
163
164 #[napi]
165 pub fn limit(&mut self, limit: u32) {
166 self.inner = self.inner.clone().limit(limit as usize);
167 }
168
169 #[napi(catch_unwind)]
170 pub async fn execute(
171 &self,
172 max_batch_length: Option<u32>,
173 ) -> napi::Result<RecordBatchIterator> {
174 let mut execution_opts = QueryExecutionOptions::default();
175 if let Some(max_batch_length) = max_batch_length {
176 execution_opts.max_batch_length = max_batch_length;
177 }
178 let inner_stream = self
179 .inner
180 .execute_with_options(execution_opts)
181 .await
182 .map_err(|e| {
183 napi::Error::from_reason(format!("Failed to execute query stream: {}", e))
184 })?;
185 Ok(RecordBatchIterator::new(inner_stream))
186 }
187
188 #[napi]
189 pub async fn explain_plan(&self, verbose: bool) -> napi::Result<String> {
190 self.inner.explain_plan(verbose).await.map_err(|e| {
191 napi::Error::from_reason(format!("Failed to retrieve the query plan: {}", e))
192 })
193 }
194}