-
Notifications
You must be signed in to change notification settings - Fork 89
fix(datafusion): apply dynamic options to vector search #623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shyjsarah
wants to merge
2
commits into
apache:main
Choose a base branch
from
shyjsarah:fix/vector-search-dynamic-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -45,6 +45,8 @@ use futures::{stream, TryStreamExt}; | |
| use paimon::catalog::Catalog; | ||
| use paimon::spec::{ | ||
| BigIntType, CoreOptions, DataField, DataType, ROW_ID_FIELD_ID, ROW_ID_FIELD_NAME, | ||
| SCAN_SNAPSHOT_ID_OPTION, SCAN_TAG_NAME_OPTION, SCAN_TIMESTAMP_MILLIS_OPTION, | ||
| SCAN_VERSION_OPTION, | ||
| }; | ||
| use paimon::table::Table; | ||
|
|
||
|
|
@@ -55,23 +57,38 @@ use crate::table_function_args::{ | |
| extract_int_literal, extract_string_literal, parse_table_identifier, | ||
| }; | ||
| use crate::table_loader::load_data_table_for_read; | ||
| use crate::DynamicOptions; | ||
|
|
||
| const FUNCTION_NAME: &str = "vector_search"; | ||
|
|
||
| pub fn register_vector_search( | ||
| ctx: &SessionContext, | ||
| catalog: Arc<dyn Catalog>, | ||
| default_database: &str, | ||
| ) { | ||
| register_vector_search_with_dynamic_options(ctx, catalog, default_database, Default::default()); | ||
| } | ||
|
|
||
| pub(crate) fn register_vector_search_with_dynamic_options( | ||
| ctx: &SessionContext, | ||
| catalog: Arc<dyn Catalog>, | ||
| default_database: &str, | ||
| dynamic_options: DynamicOptions, | ||
| ) { | ||
| ctx.register_udtf( | ||
| "vector_search", | ||
| Arc::new(VectorSearchFunction::new(catalog, default_database)), | ||
| Arc::new(VectorSearchFunction::new_with_dynamic_options( | ||
| catalog, | ||
| default_database, | ||
| dynamic_options, | ||
| )), | ||
| ); | ||
| } | ||
|
|
||
| pub struct VectorSearchFunction { | ||
| catalog: Arc<dyn Catalog>, | ||
| default_database: String, | ||
| dynamic_options: DynamicOptions, | ||
| } | ||
|
|
||
| impl Debug for VectorSearchFunction { | ||
|
|
@@ -84,9 +101,18 @@ impl Debug for VectorSearchFunction { | |
|
|
||
| impl VectorSearchFunction { | ||
| pub fn new(catalog: Arc<dyn Catalog>, default_database: &str) -> Self { | ||
| Self::new_with_dynamic_options(catalog, default_database, Default::default()) | ||
| } | ||
|
|
||
| pub(crate) fn new_with_dynamic_options( | ||
| catalog: Arc<dyn Catalog>, | ||
| default_database: &str, | ||
| dynamic_options: DynamicOptions, | ||
| ) -> Self { | ||
| Self { | ||
| catalog, | ||
| default_database: default_database.to_string(), | ||
| dynamic_options, | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -113,8 +139,17 @@ impl TableFunctionImpl for VectorSearchFunction { | |
| parse_table_identifier(FUNCTION_NAME, &table_name, &self.default_database)?; | ||
|
|
||
| let catalog = Arc::clone(&self.catalog); | ||
| let dynamic_options = vector_search_dynamic_options(&self.dynamic_options); | ||
| let table = block_on_with_runtime( | ||
| async move { load_data_table_for_read(&catalog, &identifier, FUNCTION_NAME).await }, | ||
| async move { | ||
| let table = load_data_table_for_read(&catalog, &identifier, FUNCTION_NAME).await?; | ||
| let table = if dynamic_options.is_empty() { | ||
| table | ||
| } else { | ||
| table.copy_with_options(dynamic_options) | ||
| }; | ||
| Ok::<_, DataFusionError>(table) | ||
| }, | ||
| "vector_search: catalog access thread panicked", | ||
| )?; | ||
|
|
||
|
|
@@ -426,6 +461,21 @@ impl ExecutionPlan for VectorSearchExec { | |
| } | ||
| } | ||
|
|
||
| /// Vector search currently resolves candidates from the latest snapshot, so forwarding a | ||
| /// time-travel selector would search one snapshot and materialize rows from another. | ||
| fn vector_search_dynamic_options(dynamic_options: &DynamicOptions) -> HashMap<String, String> { | ||
| let mut options = dynamic_options.read().unwrap().clone(); | ||
| for key in [ | ||
| SCAN_VERSION_OPTION, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why need to remote these options? Can we just keep it as is. |
||
| SCAN_TIMESTAMP_MILLIS_OPTION, | ||
| SCAN_SNAPSHOT_ID_OPTION, | ||
| SCAN_TAG_NAME_OPTION, | ||
| ] { | ||
| options.remove(key); | ||
| } | ||
| options | ||
| } | ||
|
|
||
| /// Projected user columns (+ internal `_ROW_ID`, needed to realign rows to rank). | ||
| /// Errors if the table has no row tracking, since results then can't be ordered. | ||
| fn projected_read_fields( | ||
|
|
@@ -535,3 +585,83 @@ fn gather_rows_by_rank( | |
| RecordBatch::try_new_with_options(Arc::clone(output_schema), columns, &options) | ||
| .map_err(DataFusionError::from) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use datafusion::catalog::TableFunctionArgs; | ||
| use datafusion::logical_expr::lit; | ||
| use paimon::{CatalogOptions, FileSystemCatalog, Options}; | ||
|
|
||
| use super::*; | ||
| use crate::SQLContext; | ||
|
|
||
| #[tokio::test] | ||
| async fn test_vector_search_applies_supported_session_dynamic_options() { | ||
| let temp_dir = tempfile::tempdir().unwrap(); | ||
| let mut catalog_options = Options::new(); | ||
| catalog_options.set( | ||
| CatalogOptions::WAREHOUSE, | ||
| format!("file://{}", temp_dir.path().display()), | ||
| ); | ||
| let catalog = Arc::new(FileSystemCatalog::new(catalog_options).unwrap()); | ||
|
|
||
| let mut sql_context = SQLContext::new(); | ||
| sql_context | ||
| .register_catalog("paimon", catalog) | ||
| .await | ||
| .unwrap(); | ||
| sql_context | ||
| .sql( | ||
| "CREATE TABLE paimon.default.vector_blob (\ | ||
| id INT, \ | ||
| embedding ARRAY<FLOAT>, \ | ||
| picture BLOB\ | ||
| ) WITH (\ | ||
| 'data-evolution.enabled' = 'true', \ | ||
| 'row-tracking.enabled' = 'true'\ | ||
| )", | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
| sql_context | ||
| .sql("SET 'paimon.blob-as-descriptor' = 'true'") | ||
| .await | ||
| .unwrap(); | ||
| sql_context | ||
| .sql("SET 'paimon.scan.version' = '1'") | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| let state = sql_context.ctx().state(); | ||
| let table_function = state | ||
| .table_functions() | ||
| .get(FUNCTION_NAME) | ||
| .expect("vector_search should be registered"); | ||
| let args = [ | ||
| lit("paimon.default.vector_blob"), | ||
| lit("embedding"), | ||
| lit("[1.0]"), | ||
| lit(1_i64), | ||
| ]; | ||
| let provider = table_function | ||
| .create_table_provider_with_args(TableFunctionArgs::new(&args, &state)) | ||
| .unwrap(); | ||
| let provider = provider | ||
| .downcast_ref::<VectorSearchTableProvider>() | ||
| .expect("vector_search should return its table provider"); | ||
|
|
||
| assert!( | ||
| CoreOptions::new(provider.inner.table().schema().options()).blob_as_descriptor(), | ||
| "vector_search should apply session dynamic options to the loaded table" | ||
| ); | ||
| assert!( | ||
| !provider | ||
| .inner | ||
| .table() | ||
| .schema() | ||
| .options() | ||
| .contains_key(SCAN_VERSION_OPTION), | ||
| "vector_search should not forward unsupported time-travel options" | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.