Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ changes go in their respective binding's NEXT_CHANGELOG.

### Changed

- Table metadata overlaps catalog discovery with a scoped query for lowercase underscore patterns without percent or star characters in the literal scope, reusing the matching catalog's response while other matches run concurrently. A miss may add one statement, which finishes and closes independently when unused.

### Updated

### Fixed
Expand Down
9 changes: 4 additions & 5 deletions src/metadata/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,10 @@ impl SqlCommandBuilder {
/// Unescape an ODBC/JDBC pattern into a literal identifier: `\%` →
/// `%`, `\_` → `_`, `\\` → `\`. A lone trailing backslash is kept.
///
/// Only valid to call on a pattern with no *active* wildcards (see
/// [`pattern_has_wildcards`](Self::pattern_has_wildcards)); such a
/// pattern denotes an exact catalog name, and the escapes must be
/// removed before it goes into a backtick-quoted `IN CATALOG`
/// identifier. Mirrors the JDBC driver's `WildcardUtil.stripJdbcEscapes`.
/// A pattern without active wildcards denotes an exact catalog name.
/// Active wildcards remain literal characters in the returned identifier;
/// speculative callers must confirm the catalog match before reusing it.
/// Mirrors the JDBC driver's `WildcardUtil.stripJdbcEscapes`.
pub fn strip_jdbc_escapes(pattern: &str) -> String {
let mut result = String::with_capacity(pattern.len());
let mut chars = pattern.chars().peekable();
Expand Down
251 changes: 189 additions & 62 deletions src/metadata_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,43 @@ pub struct Metadata {
session: Arc<SessionInner>,
}

struct TablesExecution {
raw: ExecuteResult,
close_guard: Option<TablesCloseGuard>,
}

type TablesExecutionTask = tokio::task::JoinHandle<Result<Option<TablesExecution>>>;

struct TablesCloseGuard {
session: Arc<SessionInner>,
statement_id: String,
}

impl TablesCloseGuard {
fn start_close(&mut self) -> Option<tokio::task::JoinHandle<()>> {
let statement_id = std::mem::take(&mut self.statement_id);
if statement_id.is_empty() {
return None;
}
let transport = Arc::clone(self.session.transport());
let span = self.session.log_span().clone();
Some(
self.session.runtime_handle().spawn(
async move {
transport.close_statement(&statement_id).await.ok();
}
.instrument(span),
),
)
}
}

impl Drop for TablesCloseGuard {
fn drop(&mut self) {
drop(self.start_close());
}
}

impl Metadata {
pub(crate) fn new(session: Arc<SessionInner>) -> Self {
Self { session }
Expand Down Expand Up @@ -868,13 +905,10 @@ impl Metadata {
Self::finish_metadata_call(telemetry, result)
}

/// Enumerate catalogs, keep those matching `pattern`
/// (case-insensitive ODBC/JDBC LIKE), and fan out one scoped
/// `SHOW TABLES IN CATALOG <name>` per match — reproducing Thrift's
/// catalog wildcarding without the whole-workspace scan of
/// `SHOW TABLES IN ALL CATALOGS`. The per-catalog queries run with
/// bounded parallelism; their reshaped rows are concatenated and
/// re-sorted into the JDBC row order.
/// Resolve wildcard catalogs while speculating a narrow literal scope.
/// Reuse its result for the byte-identical matched catalog, if present;
/// use bounded fan-out for the other catalogs. Concatenate and re-sort
/// the reshaped rows into JDBC order without a whole-workspace table scan.
async fn list_tables_matching_catalogs(
&self,
pattern: &str,
Expand All @@ -885,28 +919,68 @@ impl Metadata {
) -> Result<ResultStream> {
use futures::stream::{FuturesUnordered, StreamExt};

// 1. Enumerate + filter catalog names client-side.
let matcher = LikeMatcher::new(pattern);
let noop = StatementTelemetry::noop();
let mut catalogs = self.list_catalogs_uninstrumented(&noop).await?;
let mut names: Vec<String> = Vec::new();
let mut enumerated = 0usize;
while let Some(batch) = catalogs.next_batch().await? {
let ResultBatch::Arrow(rb) = batch;
// GET_CATALOGS_SCHEMA is a single `TABLE_CAT: Utf8` column.
let col = rb
.column(0)
.as_any()
.downcast_ref::<arrow_array::StringArray>()
.ok_or_else(|| Error::data_loss().message("TABLE_CAT is not Utf8"))?;
for i in 0..col.len() {
enumerated += 1;
if !col.is_null(i) && matcher.matches(col.value(i)) {
names.push(col.value(i).to_owned());
let schema_str = schema_pattern.as_ref().map(|p| p.as_str().to_owned());
let table_str = table_pattern.as_ref().map(|p| p.as_str().to_owned());
let types_owned: Option<Vec<String>> =
table_types.map(|s| s.iter().map(|t| (*t).to_owned()).collect());
let literal = SqlCommandBuilder::strip_jdbc_escapes(pattern);
let lookup = async {
let matcher = LikeMatcher::new(pattern);
let noop = StatementTelemetry::noop();
let mut catalogs = self.list_catalogs_uninstrumented(&noop).await?;
let mut names: Vec<String> = Vec::new();
let mut enumerated = 0usize;
while let Some(batch) = catalogs.next_batch().await? {
let ResultBatch::Arrow(rb) = batch;
// GET_CATALOGS_SCHEMA is a single `TABLE_CAT: Utf8` column.
let col = rb
.column(0)
.as_any()
.downcast_ref::<arrow_array::StringArray>()
.ok_or_else(|| Error::data_loss().message("TABLE_CAT is not Utf8"))?;
for i in 0..col.len() {
enumerated += 1;
if !col.is_null(i) && matcher.matches(col.value(i)) {
names.push(col.value(i).to_owned());
}
}
}
}
catalogs.close();
catalogs.close();
Ok::<_, Error>((names, enumerated))
};
let mut speculative = if SqlCommandBuilder::pattern_has_wildcards(pattern)
&& !literal.contains(['%', '*'])
&& !literal.chars().any(char::is_uppercase)
{
let metadata = Self::new(Arc::clone(&self.session));
let catalog = literal.clone();
let schema = schema_str.clone();
let table = table_str.clone();
let types = types_owned.clone();
// Detaching keeps an in-flight execute alive until its known ID
// can be closed, without retaining unused result batches.
Some(
self.session.runtime_handle().spawn(
async move {
let types_ref: Option<Vec<&str>> = types
.as_ref()
.map(|v| v.iter().map(String::as_str).collect());
metadata
.execute_tables_for_catalog(
&catalog,
schema.as_deref(),
table.as_deref(),
types_ref.as_deref(),
)
.await
}
.instrument(tracing::Span::current()),
),
)
} else {
None
};
let (mut names, enumerated) = lookup.await?;
// One correlatable line tying the fan-out back to its cause: how
// many catalogs a wildcard `list_tables` matched (and will query),
// vs how many exist. Lets on-call spot fan-out amplification (a
Expand All @@ -919,8 +993,8 @@ impl Metadata {
"list_tables catalog wildcard fan-out"
);

// No catalog matched → typed empty result, no further wire calls.
if names.is_empty() {
drop(speculative);
Self::observe_inline_result(telemetry);
return ResultStream::from_reader(Box::new(EmptyReader::new(
GET_TABLES_SCHEMA.clone(),
Expand All @@ -933,22 +1007,37 @@ impl Metadata {
// be hit with an unbounded burst when a loose pattern matches many
// catalogs.
const MAX_INFLIGHT: usize = 8;
let schema_str = schema_pattern.as_ref().map(|p| p.as_str().to_owned());
let table_str = table_pattern.as_ref().map(|p| p.as_str().to_owned());
let types_owned: Option<Vec<String>> =
table_types.map(|s| s.iter().map(|t| (*t).to_owned()).collect());

let mut all_batches: Vec<arrow_array::RecordBatch> = Vec::new();
let literal_speculation = names
.iter()
.position(|name| name == &literal)
.and_then(|index| speculative.take().map(|task| (names.remove(index), task)));
// Release completed unused responses now; pending executes finish
// and close on the session runtime without gating matched catalogs.
drop(speculative);
let mut inflight = FuturesUnordered::new();
let mut remaining = names.into_iter();
for name in remaining.by_ref().take(MAX_INFLIGHT) {
let ordinary_slots = MAX_INFLIGHT - usize::from(literal_speculation.is_some());
for name in remaining.by_ref().take(ordinary_slots) {
inflight.push(self.fetch_tables_for_catalog(
name,
schema_str.clone(),
table_str.clone(),
types_owned.clone(),
None,
));
}
// Start other executes before draining an already-ready literal response.
if let Some((name, task)) = literal_speculation {
inflight.push(self.fetch_tables_for_catalog(
name,
schema_str.clone(),
table_str.clone(),
types_owned.clone(),
Some(task),
));
}
let mut all_batches: Vec<arrow_array::RecordBatch> = Vec::new();
while let Some(result) = inflight.next().await {
all_batches.extend(result?);
if let Some(name) = remaining.next() {
Expand All @@ -957,6 +1046,7 @@ impl Metadata {
schema_str.clone(),
table_str.clone(),
types_owned.clone(),
None,
));
}
}
Expand Down Expand Up @@ -985,48 +1075,57 @@ impl Metadata {
}

/// Fetch + reshape `SHOW TABLES IN CATALOG <catalog>` into JDBC-shape
/// batches. An object-not-found error (catalog dropped between
/// enumeration and this call) collapses to zero rows, matching the
/// JDBC empty-result contract. Used by the fan-out path.
/// batches. Only execute-phase not-found collapses to zero rows;
/// read and reshape errors always propagate. A speculative execute
/// that found no catalog is retried after discovery confirms it.
async fn fetch_tables_for_catalog(
&self,
catalog: String,
schema_pattern: Option<String>,
table_pattern: Option<String>,
table_types: Option<Vec<String>>,
speculative: Option<TablesExecutionTask>,
) -> Result<Vec<arrow_array::RecordBatch>> {
let types_ref: Option<Vec<&str>> = table_types
.as_ref()
.map(|v| v.iter().map(String::as_str).collect());
let raw = match self
.session
.transport()
.list_tables(
self.session.session_id(),
Some(catalog.as_str()),
schema_pattern.as_deref(),
table_pattern.as_deref(),
types_ref.as_deref(),
)
.await
{
Ok(r) => r,
// Catalog vanished mid-enumeration → contribute no rows.
Err(e) if thrift_shape::is_object_not_found(&e) => return Ok(Vec::new()),
Err(e) => return Err(e),
let execution = match speculative {
Some(task) => task.await.map_err(|error| {
Error::internal().message(format!("Table metadata execution task failed: {error}"))
})??,
None => None,
};
let execution = match execution {
Some(execution) => Some(execution),
None => {
self.execute_tables_for_catalog(
&catalog,
schema_pattern.as_deref(),
table_pattern.as_deref(),
types_ref.as_deref(),
)
.await?
}
};
let Some(TablesExecution {
raw,
mut close_guard,
}) = execution
else {
return Ok(Vec::new());
};
// Read + reshape into `out`. Capture the result rather than `?`-ing
// out early: the fan-out opens one statement per catalog and
// `ExecuteResult` has no Drop-based close, so a mid-stream read
// error must still close this statement or it leaks server-side
// (unlike the streaming paths, which close via a StatementCloseGuard).
// error must still close this statement or it leaks server-side.
// TablesCloseGuard also reaps it if this future is dropped mid-read;
// an already-started close finishes independently of its awaiter.
//
// `close_id` returns an empty id when the server already reported
// `status.state == Closed` (statement reaped server-side); skip the
// redundant CloseStatement DELETE in that case, mirroring the
// streaming metadata paths and the regular-query path in
// `statement/mutable.rs`.
let stmt_id = close_id(&raw);
let native = is_native_metadata_result(&raw);
let mut reader = raw.reader;
let read: Result<Vec<arrow_array::RecordBatch>> = async {
Expand All @@ -1053,16 +1152,44 @@ impl Metadata {
.await;
// Best-effort close on every exit (success or error), unless the
// statement was already closed server-side.
if !stmt_id.is_empty() {
self.session
.transport()
.close_statement(&stmt_id)
.await
.ok();
let close_task = close_guard.as_mut().and_then(TablesCloseGuard::start_close);
if let Some(close_task) = close_task {
close_task.await.ok();
}
read
}

async fn execute_tables_for_catalog(
&self,
catalog: &str,
schema_pattern: Option<&str>,
table_pattern: Option<&str>,
table_types: Option<&[&str]>,
) -> Result<Option<TablesExecution>> {
let raw = match self
.session
.transport()
.list_tables(
self.session.session_id(),
Some(catalog),
schema_pattern,
table_pattern,
table_types,
)
.await
{
Ok(raw) => raw,
Err(error) if thrift_shape::is_object_not_found(&error) => return Ok(None),
Err(error) => return Err(error),
};
let statement_id = close_id(&raw);
let close_guard = (!statement_id.is_empty()).then(|| TablesCloseGuard {
session: Arc::clone(&self.session),
statement_id,
});
Ok(Some(TablesExecution { raw, close_guard }))
}

/// `SHOW TABLES IN CATALOG <catalog>` (or `IN ALL CATALOGS` when
/// `catalog` is None) as a single lazily-streamed query — the exact
/// literal-catalog fast path.
Expand Down
Loading
Loading