Skip to content
Merged
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
9 changes: 8 additions & 1 deletion core/crates/core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::foreign_key_info::ForeignKeyInfo;
use crate::keyspace::{KeyListing, KeyPage, KeyScan, KeyWindow};
use crate::link_watch::LinkState;
use crate::meta::{
IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableRef,
IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef,
};
use crate::query_result::QueryResult;
use crate::read_statement::ReadStatement;
Expand Down Expand Up @@ -148,6 +148,13 @@ pub trait Connection: Send + Sync {

async fn table_estimate(&self, table: &TableRef, options: CallOptions) -> Result<TableEstimate, DriverError>;

/// The table's comment and size, as far as the engine keeps them. An
/// engine that keeps neither says nothing rather than failing, so a
/// caller shows what there is.
async fn table_info(&self, _table: &TableRef, _options: CallOptions) -> Result<TableInfo, DriverError> {
Ok(TableInfo::default())
}

/// Run a generated read and keep at most `limit` rows, marking the
/// result truncated when there were more.
async fn query_bound(
Expand Down
2 changes: 2 additions & 0 deletions core/crates/core/src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod page;
mod row_identity;
mod schema_info;
mod table_estimate;
mod table_info;
mod table_ref;

pub use index_info::{IndexInfo, IndexKey};
Expand All @@ -17,4 +18,5 @@ pub use page::{CtidWindow, HeapAddress, PageCost, PagePosition, PageStability, P
pub use row_identity::{EngineRowId, EngineRowIdPart, RowIdentity};
pub use schema_info::{ObjectScope, SchemaInfo};
pub use table_estimate::{RowCountEstimate, TableEstimate};
pub use table_info::TableInfo;
pub use table_ref::TableRef;
39 changes: 39 additions & 0 deletions core/crates/core/src/meta/table_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// What the engine keeps about a table beyond its columns: its comment
/// and the space it takes. Each figure is `None` where the engine keeps
/// none or the login may not read it.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TableInfo {
pub comment: Option<String>,
/// The rows themselves, with the out-of-line pages an engine keeps
/// apart for long values (PostgreSQL's TOAST, SQL Server's LOB pages).
pub data_bytes: Option<u64>,
pub index_bytes: Option<u64>,
/// Everything the table holds on disk, which can be more than its
/// data and indexes together.
pub total_bytes: Option<u64>,
}

impl TableInfo {
/// A blank comment is no comment.
pub fn with_comment(mut self, comment: Option<String>) -> Self {
self.comment = comment.filter(|text| !text.trim().is_empty());
self
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn a_blank_comment_is_none() {
assert_eq!(TableInfo::default().with_comment(Some(" ".to_owned())).comment, None);
assert_eq!(
TableInfo::default()
.with_comment(Some("Orders".to_owned()))
.comment
.as_deref(),
Some("Orders")
);
}
}
20 changes: 19 additions & 1 deletion core/crates/drivers/cassandra/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tablepro_core::ddl::{
TableDefinition, TableSource,
};
use tablepro_core::meta::{
IndexInfo, IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowIdentity, SchemaInfo, TableRef,
IndexInfo, IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowIdentity, SchemaInfo, TableInfo, TableRef,
};

use scylla::value::{CqlValue, Row};
Expand Down Expand Up @@ -388,6 +388,24 @@ async fn view_row(link: &CqlLink, table: &CatalogTable) -> Result<Option<ViewRow
}))
}

/// A table's or a materialized view's comment, as its schema table keeps it.
pub(crate) async fn table_info(link: &CqlLink, table: &CatalogTable) -> Result<TableInfo, DriverError> {
for sql in [
"SELECT comment FROM system_schema.tables WHERE keyspace_name = ? AND table_name = ?",
"SELECT comment FROM system_schema.views WHERE keyspace_name = ? AND view_name = ?",
] {
let rows = link
.catalogue(sql, (table.keyspace.as_str(), table.name.as_str()))
.await?
.into_rows_result()
.map_err(rows_error)?;
if let Some((comment,)) = rows.maybe_first_row::<(Option<String>,)>().map_err(rows_error)? {
return Ok(TableInfo::default().with_comment(comment));
}
}
Ok(TableInfo::default())
}

/// The options a CREATE statement sets, in the order cqlsh's DESCRIBE
/// writes them. A release keeps only some of them; the rest are left
/// out, and so are the read repair chances Cassandra 4.0 dropped from
Expand Down
12 changes: 11 additions & 1 deletion core/crates/drivers/cassandra/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tablepro_core::column::ColumnInfo;
use tablepro_core::ddl::{DatabaseChoices, InboundForeignKey, OutboundForeignKey, TableDefinition};
use tablepro_core::dialect::SqlDialect;
use tablepro_core::meta::{
IndexInfo, ObjectInfo, ObjectScope, RowCountEstimate, RowIdentity, SchemaInfo, TableEstimate, TableRef,
IndexInfo, ObjectInfo, ObjectScope, RowCountEstimate, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef,
};
use tablepro_core::statement::Statement;
use tablepro_core::{
Expand Down Expand Up @@ -354,6 +354,16 @@ impl Connection for CqlConnection {
})
}

/// CQL keeps a comment. A table's size is each node's own, kept in
/// its metrics rather than in a table a client reads.
async fn table_info(&self, table: &TableRef, options: CallOptions) -> Result<TableInfo, DriverError> {
let table = self.catalog_table(table)?;
self.read(options, move |link, _| async move {
catalog::table_info(&link, &table).await
})
.await
}

async fn query_bound(
&self,
statement: &Statement,
Expand Down
56 changes: 55 additions & 1 deletion core/crates/drivers/cassandra/tests/integration/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! views, a table's key, and the CREATE statements the server's schema
//! tables describe.

use tablepro_core::meta::{ObjectKind, ObjectScope, RowIdentity};
use tablepro_core::meta::{ObjectKind, ObjectScope, RowIdentity, TableInfo};
use tablepro_core::{Connection, TlsConfig, Value, WorkspaceSettings};
use tablepro_test_fixtures::CassandraFlavour;
use tablepro_test_support::conformance::test_liveness;
Expand Down Expand Up @@ -101,6 +101,48 @@ async fn a_table_is_keyed_by_its_primary_key_and_a_view_is_read_only<S: Server>(
assert_eq!(view_identity, RowIdentity::Unordered);
}

async fn a_table_and_a_view_report_their_comment_alone<S: Server>() {
let fixture = start::<S>().await;
let connection = connect(&fixture, false).await;
setup(
connection.as_ref(),
&[
ORDERS,
NOTE_INDEX,
"ALTER TABLE tablepro.orders WITH comment = 'the orders'",
"INSERT INTO tablepro.orders (customer, placed, total) VALUES (1, '2024-01-01', 1.5)",
"CREATE MATERIALIZED VIEW tablepro.orders_by_total AS SELECT * FROM tablepro.orders \
WHERE customer IS NOT NULL AND placed IS NOT NULL AND total IS NOT NULL \
PRIMARY KEY (total, customer, placed) WITH comment = 'by total'",
"CREATE TABLE tablepro.plain (id int PRIMARY KEY)",
],
)
.await;

let orders = connection
.table_info(&table("orders"), within(CALL))
.await
.expect("the table's info");
let view = connection
.table_info(&table("orders_by_total"), within(CALL))
.await
.expect("the view's info");
let plain = connection
.table_info(&table("plain"), within(CALL))
.await
.expect("the plain table's info");

assert_eq!(
orders,
TableInfo {
comment: Some("the orders".to_owned()),
..TableInfo::default()
}
);
assert_eq!(view.comment.as_deref(), Some("by total"));
assert_eq!(plain, TableInfo::default());
}

async fn the_create_script_rebuilds_the_table_it_was_read_from<S: Server>() {
let fixture = start::<S>().await;
let connection = connect(&fixture, false).await;
Expand Down Expand Up @@ -263,3 +305,15 @@ async fn cassandra_create_script_rebuilds_the_table() {
async fn scylla_create_script_rebuilds_the_table() {
the_create_script_rebuilds_the_table_it_was_read_from::<ScyllaServer>().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "requires docker"]
async fn cassandra_table_info_is_the_comment() {
a_table_and_a_view_report_their_comment_alone::<CassandraServer>().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "requires docker"]
async fn scylla_table_info_is_the_comment() {
a_table_and_a_view_report_their_comment_alone::<ScyllaServer>().await;
}
49 changes: 48 additions & 1 deletion core/crates/drivers/clickhouse/src/catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod table_definition;
use clickhouse::query::Query;
use tablepro_core::DriverError;
use tablepro_core::ddl::ddl_sql::parenthesized;
use tablepro_core::meta::{IndexInfo, RowIdentity, TableEstimate};
use tablepro_core::meta::{IndexInfo, RowIdentity, TableEstimate, TableInfo};

pub(crate) use clickhouse_column_row::ClickHouseColumnRow;
pub(crate) use table_definition::fetch_table_definition;
Expand Down Expand Up @@ -138,3 +138,50 @@ pub(crate) async fn table_estimate(calls: &CallClient, table: &CatalogTable) ->
heap_blocks: None,
})
}

/// `system.tables` keeps one figure: a MergeTree table's bytes on disk,
/// NULL for an engine that keeps none, with no split into data and
/// indexes.
pub(crate) async fn table_info(calls: &CallClient, table: &CatalogTable) -> Result<TableInfo, DriverError> {
let sql = "SELECT comment, total_bytes FROM system.tables \
WHERE database = {db:String} AND name = {tbl:String}";
let result = read_table(table.bind(calls.raw(sql)?), None).await?;
match result.rows.first() {
Some(row) => table_info_of(&CatalogRow(row)),
None => Ok(TableInfo::default()),
}
}

fn table_info_of(row: &CatalogRow<'_>) -> Result<TableInfo, DriverError> {
Ok(TableInfo {
total_bytes: row.optional_u64(1)?,
..TableInfo::default()
}
.with_comment(Some(row.text(0)?)))
}

#[cfg(test)]
mod tests {
use tablepro_core::Value;

use super::*;

#[test]
fn a_row_gives_the_total_and_a_blank_comment_is_none() {
let sized = [Value::Text("orders".to_owned()), Value::UInt(4096)];
let view = [Value::Text(String::new()), Value::Null];

assert_eq!(
table_info_of(&CatalogRow(&sized)).expect("the info"),
TableInfo {
comment: Some("orders".to_owned()),
total_bytes: Some(4096),
..TableInfo::default()
}
);
assert_eq!(
table_info_of(&CatalogRow(&view)).expect("the info"),
TableInfo::default()
);
}
}
10 changes: 9 additions & 1 deletion core/crates/drivers/clickhouse/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tablepro_core::ddl::{DatabaseChoices, InboundForeignKey, OutboundForeignKey,
use tablepro_core::dialect::SqlDialect;
use tablepro_core::dump::{DumpCatalog, DumpScope};
use tablepro_core::meta::{
IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableRef,
IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef,
};
use tablepro_core::statement::Statement;
use tablepro_core::{
Expand Down Expand Up @@ -417,6 +417,14 @@ impl Connection for ClickHouseConnection {
.await
}

async fn table_info(&self, table: &TableRef, options: CallOptions) -> Result<TableInfo, DriverError> {
let table = self.catalog_table(table);
self.workspace_read(options, move |calls| async move {
catalog::table_info(&calls, &table).await
})
.await
}

async fn query_bound(
&self,
statement: &Statement,
Expand Down
39 changes: 38 additions & 1 deletion core/crates/drivers/clickhouse/tests/integration/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! What the catalogue says, and SQL the dialect builds against it.

use tablepro_core::column::{ColumnKind, TemporalZone, TextLength};
use tablepro_core::meta::{IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowCountEstimate, RowIdentity};
use tablepro_core::meta::{IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowCountEstimate, RowIdentity, TableInfo};
use tablepro_core::statement::StatementBuilder;
use tablepro_core::{
ConnectTarget, Connection, DatabaseDriver, DriverError, FilterOp, FilterRule, FilterSet, FilterValue, RowLimit,
Expand Down Expand Up @@ -208,6 +208,43 @@ async fn fetch_table_definition_reports_keys_comment_and_source() {
connection.close().await.expect("a close");
}

/// MergeTree keeps its bytes on disk as one figure, a view none.
#[tokio::test]
#[ignore = "requires docker"]
async fn table_info_reports_comment_and_total_bytes() {
let fixture = start().await;
let connection = connect(&fixture, false).await;
let (sized, view) = (unique("sized"), unique("sized_view"));
setup(
connection.as_ref(),
&[
&format!(
"CREATE TABLE {} (id UInt64, label String, INDEX by_label label TYPE bloom_filter GRANULARITY 1) \
ENGINE = MergeTree ORDER BY id COMMENT 'the sized one'",
quoted(&sized)
),
&format!("INSERT INTO {} VALUES (1, 'a'), (2, 'b'), (3, 'c')", quoted(&sized)),
&format!("CREATE VIEW {} AS SELECT id FROM {}", quoted(&view), quoted(&sized)),
],
)
.await;

let table_info = connection
.table_info(&table(&sized), within(CALL))
.await
.expect("the table's info");
let view_info = connection
.table_info(&table(&view), within(CALL))
.await
.expect("the view's info");

assert_eq!(table_info.comment.as_deref(), Some("the sized one"));
assert!(table_info.total_bytes.is_some_and(|total| total > 0), "{table_info:?}");
assert_eq!((table_info.data_bytes, table_info.index_bytes), (None, None));
assert_eq!(view_info, TableInfo::default());
connection.close().await.expect("a close");
}

#[tokio::test]
#[ignore = "requires docker"]
async fn objects_and_schemas_are_listed_by_kind() {
Expand Down
18 changes: 17 additions & 1 deletion core/crates/drivers/duckdb/src/catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tablepro_core::ddl::{
use tablepro_core::edit::parse_literal_text;
use tablepro_core::meta::{
IndexInfo, IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowCountEstimate, RowIdentity, SchemaInfo, TableEstimate,
TableRef,
TableInfo, TableRef,
};
use tablepro_core::sql_syntax::{LiteralToken, MAX_EXPRESSION_LEN, SqlGrammar, parse_expression};
use tablepro_core::value::Value;
Expand Down Expand Up @@ -696,6 +696,22 @@ pub(crate) fn table_estimate(link: &Link, catalog: &str, table: &TableRef) -> Re
})
}

/// DuckDB keeps a comment on a table or a view, and no figure for the
/// bytes either takes.
pub(crate) fn table_info(link: &Link, catalog: &str, table: &TableRef) -> Result<TableInfo, DriverError> {
let schema = schema_of(table);
let rows = text_rows(
link,
"SELECT comment FROM duckdb_tables() WHERE database_name = ? AND schema_name = ? AND table_name = ? \
UNION ALL \
SELECT comment FROM duckdb_views() WHERE database_name = ? AND schema_name = ? AND view_name = ?",
&[catalog, schema, &table.name, catalog, schema, &table.name],
&mut CatalogBudget::default(),
)?;
let comment = rows.into_iter().next().and_then(|row| row.into_iter().next().flatten());
Ok(TableInfo::default().with_comment(comment))
}

/// The table as structure changes plan against it.
pub(crate) fn fetch_table_definition(
link: &Link,
Expand Down
Loading
Loading