From 33211ff21fff562e9e544e6edbd0aedfb9a40ac6 Mon Sep 17 00:00:00 2001 From: Vikrant Puppala Date: Fri, 31 Jul 2026 09:59:19 +0000 Subject: [PATCH] [PECOBLR-2065] c-abi: add kernel_metadata_list_table_types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Expose the existing Metadata::table_types() over the C ABI so C / ODBC consumers can enumerate the supported table types. This backs ODBC SQLTables(table_type = SQL_ALL_TABLE_TYPES), the third catalog/schema/ table-type enumeration sentinel — the ODBC bridge already routes the all-catalogs / all-schemas sentinels to list_catalogs / list_schemas but had no C ABI call for table types. The call is static (no wire request): the type set comes from the session's transport (Transport::list_table_types). The stream carries a single TABLE_TYPE column (JDBC getTableTypes shape), mirroring the other list_* entry points. Adds a wiremock C ABI test that opens a session, drives the raw FFI function, and asserts the single-column shape with at least one row. Co-authored-by: Isaac Signed-off-by: Vikrant Puppala --- include/databricks_kernel.h | 6 + src/c_abi/metadata.rs | 28 +++- .../v0_c_abi_metadata_table_types_wiremock.rs | 143 ++++++++++++++++++ 3 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 tests/v0_c_abi_metadata_table_types_wiremock.rs diff --git a/include/databricks_kernel.h b/include/databricks_kernel.h index 3fe7fde4..a2188e6f 100644 --- a/include/databricks_kernel.h +++ b/include/databricks_kernel.h @@ -636,6 +636,12 @@ KernelStatusCode kernel_metadata_foreign_keys(kernel_session_t* session, const c const char* foreign_catalog, const char* foreign_schema, const char* foreign_table, kernel_result_stream_t** out); +/* List the supported table types (`TABLE`, `VIEW`, `SYSTEM TABLE`, …). + * No filters and no wire call — the set is derived from the session's + * transport. Backs ODBC `SQLTables(table_type = SQL_ALL_TABLE_TYPES)`. + * The stream carries a single `TABLE_TYPE` column (JDBC getTableTypes). */ +KernelStatusCode kernel_metadata_list_table_types(kernel_session_t* session, + kernel_result_stream_t** out); #ifdef __cplusplus } /* extern "C" */ diff --git a/src/c_abi/metadata.rs b/src/c_abi/metadata.rs index 0729bb94..7030359e 100644 --- a/src/c_abi/metadata.rs +++ b/src/c_abi/metadata.rs @@ -340,10 +340,30 @@ pub unsafe extern "C" fn kernel_metadata_foreign_keys( }) } -// PR 7d follow-ups: functions, procedures, type_info, table_types — the -// kernel `Metadata` exposes these (some synthesised in-memory). Wire them -// over the C ABI when an ODBC / Go consumer needs SQLGetTypeInfo / -// SQLProcedures. +/// List the table types the workspace supports (`TABLE`, `VIEW`, +/// `SYSTEM TABLE`, …). No filters and no wire call — the set is derived +/// from the session's transport (see `Metadata::table_types`). Backs the +/// ODBC `SQLTables(table_type = SQL_ALL_TABLE_TYPES)` enumeration mode. +/// The stream carries a single `TABLE_TYPE` column (JDBC `getTableTypes` +/// shape). +/// +/// # Safety +/// `session` must be a live handle from `kernel_session_open`; `out` +/// must be a valid, writable pointer. +#[no_mangle] +pub unsafe extern "C" fn kernel_metadata_list_table_types( + session: *mut kernel_session_t, + out: *mut *mut kernel_result_stream_t, +) -> KernelStatusCode { + run_metadata(session, out, |session| { + Box::pin(async move { session.metadata().table_types().await }) + }) +} + +// PR 7d follow-ups: functions, procedures, type_info — the kernel +// `Metadata` exposes these (some synthesised in-memory). Wire them over +// the C ABI when an ODBC / Go consumer needs SQLGetTypeInfo / +// SQLProcedures / SQLPrimaryKeys-adjacent surfaces. #[cfg(test)] mod tests { diff --git a/tests/v0_c_abi_metadata_table_types_wiremock.rs b/tests/v0_c_abi_metadata_table_types_wiremock.rs new file mode 100644 index 00000000..2ca95dea --- /dev/null +++ b/tests/v0_c_abi_metadata_table_types_wiremock.rs @@ -0,0 +1,143 @@ +// Copyright (c) 2026 Databricks, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Wiremock test for the C ABI `kernel_metadata_list_table_types` entry +//! point. `table_types` is a static, no-wire call (the set comes from the +//! session's transport), so only a session-open mock is needed: the test +//! opens a real session over the C ABI, drives the raw FFI function, and +//! asserts the stream carries the single `TABLE_TYPE` column (JDBC +//! getTableTypes shape) with at least one row. This pins the FFI wrapper +//! that backs ODBC `SQLTables(table_type = SQL_ALL_TABLE_TYPES)`. +//! +//! Like the other C ABI wiremock tests, the entry points run on the +//! kernel's own process-wide runtime inside `block_on_ffi`, so they must +//! be called from a plain (non-async) thread — hence `#[test]`, with a +//! dedicated runtime only to host the wiremock server. + +use std::ffi::CString; + +use arrow_array::ffi::{from_ffi, FFI_ArrowArray, FFI_ArrowSchema}; +use arrow_array::{RecordBatch, StructArray}; +use arrow_schema::Schema; +use databricks_sql_kernel::c_abi::metadata::kernel_metadata_list_table_types; +use databricks_sql_kernel::c_abi::result::{ + kernel_result_stream_close, kernel_result_stream_get_schema, kernel_result_stream_next_batch, + kernel_result_stream_t, +}; +use databricks_sql_kernel::c_abi::session::{ + kernel_session_close, kernel_session_config_new, kernel_session_config_set_auth_pat, + kernel_session_config_set_http_path, kernel_session_open, kernel_session_t, + KernelSessionConfig, +}; +use databricks_sql_kernel::c_abi::KernelStatusCode; +use wiremock::matchers::{method, path}; +use wiremock::{Mock, MockServer, ResponseTemplate}; + +#[test] +fn list_table_types_returns_table_type_column() { + let rt = tokio::runtime::Runtime::new().unwrap(); + let server = + rt.block_on(async { + let server = MockServer::start().await; + Mock::given(method("POST")) + .and(path("/api/2.0/sql/sessions")) + .respond_with(ResponseTemplate::new(200).set_body_string( + r#"{"session_id":"sess-table-types","warehouse_id":"test_wh"}"#, + )) + .mount(&server) + .await; + Mock::given(method("DELETE")) + .and(path("/api/2.0/sql/sessions/sess-table-types")) + .respond_with(ResponseTemplate::new(200)) + .mount(&server) + .await; + server + }); + let uri = server.uri(); + + unsafe { + // Build config → open session. + let mut config: *mut KernelSessionConfig = std::ptr::null_mut(); + assert_eq!( + kernel_session_config_new(&mut config), + KernelStatusCode::Success + ); + let host = CString::new(uri.as_str()).unwrap(); + let http_path = CString::new("/sql/1.0/warehouses/test_wh").unwrap(); + assert_eq!( + kernel_session_config_set_http_path(config, host.as_ptr(), http_path.as_ptr()), + KernelStatusCode::Success + ); + let token = CString::new("dapi_test_token").unwrap(); + assert_eq!( + kernel_session_config_set_auth_pat(config, token.as_ptr()), + KernelStatusCode::Success + ); + + let mut session: *mut kernel_session_t = std::ptr::null_mut(); + assert_eq!( + kernel_session_open(config, &mut session), + KernelStatusCode::Success + ); + assert!(!session.is_null()); + + // Drive the FFI entry point. No further wire calls: the type set + // is static, sourced from the session transport. + let mut stream: *mut kernel_result_stream_t = std::ptr::null_mut(); + assert_eq!( + kernel_metadata_list_table_types(session, &mut stream), + KernelStatusCode::Success + ); + assert!(!stream.is_null()); + + // The schema must be the single-column JDBC getTableTypes shape. + let mut ffi_schema = FFI_ArrowSchema::empty(); + assert_eq!( + kernel_result_stream_get_schema(stream, &mut ffi_schema), + KernelStatusCode::Success + ); + let schema = Schema::try_from(&ffi_schema).expect("import schema"); + let names: Vec<&str> = schema.fields().iter().map(|f| f.name().as_str()).collect(); + assert_eq!(names, vec!["TABLE_TYPE"], "JDBC getTableTypes shape"); + + // Drain batches; expect at least one row (e.g. TABLE / VIEW / …). + let mut total_rows = 0usize; + loop { + let mut arr = FFI_ArrowArray::empty(); + let mut sch = FFI_ArrowSchema::empty(); + assert_eq!( + kernel_result_stream_next_batch(stream, &mut arr, &mut sch), + KernelStatusCode::Success + ); + if arr.is_released() { + break; // end-of-stream sentinel + } + // A batch is exported as a StructArray (the Arrow C Data + // batch-as-array form); import it back the same way. + let data = from_ffi(arr, &sch).expect("import array"); + let batch = RecordBatch::from(StructArray::from(data)); + total_rows += batch.num_rows(); + } + assert!( + total_rows >= 1, + "table types must include at least one type, got {total_rows}" + ); + + assert_eq!( + kernel_result_stream_close(stream), + KernelStatusCode::Success + ); + assert_eq!(kernel_session_close(session), KernelStatusCode::Success); + } +}