lance_graph/sql_catalog.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4//! Bridge between catalog connectors and the SQL query engine.
5//!
6//! Provides utilities for building a DataFusion `SessionContext` from a
7//! [`Connector`], enabling users to auto-register tables from an external
8//! catalog and query them via [`SqlQuery`] or [`SqlEngine`].
9
10use crate::error::{GraphError, Result};
11use datafusion::execution::context::SessionContext;
12use lance_graph_catalog::connector::Connector;
13
14/// Build a DataFusion `SessionContext` with all tables from a catalog schema
15/// auto-registered.
16///
17/// This discovers tables in the given catalog/schema via the connector's
18/// [`CatalogProvider`] and registers them using the appropriate [`TableReader`]
19/// based on each table's data format.
20///
21/// # Example
22///
23/// ```no_run
24/// # use lance_graph::sql_catalog::build_context_from_connector;
25/// # use lance_graph_catalog::Connector;
26/// # async fn example(connector: &Connector) {
27/// let ctx = build_context_from_connector(connector, "unity", "default")
28/// .await
29/// .unwrap();
30/// // ctx now has all tables from unity.default registered
31/// # }
32/// ```
33pub async fn build_context_from_connector(
34 connector: &Connector,
35 catalog_name: &str,
36 schema_name: &str,
37) -> Result<SessionContext> {
38 let ctx = SessionContext::new();
39 connector
40 .register_schema(&ctx, catalog_name, schema_name)
41 .await
42 .map_err(|e| GraphError::PlanError {
43 message: format!("Failed to register catalog tables: {}", e),
44 location: snafu::Location::new(file!(), line!(), column!()),
45 })?;
46 Ok(ctx)
47}