Elffuss BraidHub

kikocis/lancedb public

main · 6.1 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 offset(&mut self, offset: u32) {
69 self.inner = self.inner.clone().offset(offset as usize);
70 }
71
72 #[napi]
73 pub fn nearest_to(&mut self, vector: Float32Array) -> Result<VectorQuery> {
74 let inner = self
75 .inner
76 .clone()
77 .nearest_to(vector.as_ref())
78 .default_error()?;
79 Ok(VectorQuery { inner })
80 }
81
82 #[napi(catch_unwind)]
83 pub async fn execute(
84 &self,
85 max_batch_length: Option<u32>,
86 ) -> napi::Result<RecordBatchIterator> {
87 let mut execution_opts = QueryExecutionOptions::default();
88 if let Some(max_batch_length) = max_batch_length {
89 execution_opts.max_batch_length = max_batch_length;
90 }
91 let inner_stream = self
92 .inner
93 .execute_with_options(execution_opts)
94 .await
95 .map_err(|e| {
96 napi::Error::from_reason(format!("Failed to execute query stream: {}", e))
97 })?;
98 Ok(RecordBatchIterator::new(inner_stream))
99 }
100
101 #[napi]
102 pub async fn explain_plan(&self, verbose: bool) -> napi::Result<String> {
103 self.inner.explain_plan(verbose).await.map_err(|e| {
104 napi::Error::from_reason(format!("Failed to retrieve the query plan: {}", e))
105 })
106 }
107}
108
109#[napi]
110pub struct VectorQuery {
111 inner: LanceDbVectorQuery,
112}
113
114#[napi]
115impl VectorQuery {
116 #[napi]
117 pub fn column(&mut self, column: String) {
118 self.inner = self.inner.clone().column(&column);
119 }
120
121 #[napi]
122 pub fn distance_type(&mut self, distance_type: String) -> napi::Result<()> {
123 let distance_type = parse_distance_type(distance_type)?;
124 self.inner = self.inner.clone().distance_type(distance_type);
125 Ok(())
126 }
127
128 #[napi]
129 pub fn postfilter(&mut self) {
130 self.inner = self.inner.clone().postfilter();
131 }
132
133 #[napi]
134 pub fn refine_factor(&mut self, refine_factor: u32) {
135 self.inner = self.inner.clone().refine_factor(refine_factor);
136 }
137
138 #[napi]
139 pub fn nprobes(&mut self, nprobe: u32) {
140 self.inner = self.inner.clone().nprobes(nprobe as usize);
141 }
142
143 #[napi]
144 pub fn bypass_vector_index(&mut self) {
145 self.inner = self.inner.clone().bypass_vector_index()
146 }
147
148 #[napi]
149 pub fn only_if(&mut self, predicate: String) {
150 self.inner = self.inner.clone().only_if(predicate);
151 }
152
153 #[napi]
154 pub fn full_text_search(&mut self, query: String, columns: Option<Vec<String>>) {
155 let query = FullTextSearchQuery::new(query).columns(columns);
156 self.inner = self.inner.clone().full_text_search(query);
157 }
158
159 #[napi]
160 pub fn select(&mut self, columns: Vec<(String, String)>) {
161 self.inner = self.inner.clone().select(Select::dynamic(&columns));
162 }
163
164 #[napi]
165 pub fn select_columns(&mut self, columns: Vec<String>) {
166 self.inner = self.inner.clone().select(Select::columns(&columns));
167 }
168
169 #[napi]
170 pub fn limit(&mut self, limit: u32) {
171 self.inner = self.inner.clone().limit(limit as usize);
172 }
173
174 #[napi]
175 pub fn offset(&mut self, offset: u32) {
176 self.inner = self.inner.clone().offset(offset as usize);
177 }
178
179 #[napi(catch_unwind)]
180 pub async fn execute(
181 &self,
182 max_batch_length: Option<u32>,
183 ) -> napi::Result<RecordBatchIterator> {
184 let mut execution_opts = QueryExecutionOptions::default();
185 if let Some(max_batch_length) = max_batch_length {
186 execution_opts.max_batch_length = max_batch_length;
187 }
188 let inner_stream = self
189 .inner
190 .execute_with_options(execution_opts)
191 .await
192 .map_err(|e| {
193 napi::Error::from_reason(format!("Failed to execute query stream: {}", e))
194 })?;
195 Ok(RecordBatchIterator::new(inner_stream))
196 }
197
198 #[napi]
199 pub async fn explain_plan(&self, verbose: bool) -> napi::Result<String> {
200 self.inner.explain_plan(verbose).await.map_err(|e| {
201 napi::Error::from_reason(format!("Failed to retrieve the query plan: {}", e))
202 })
203 }
204}