Skip to content

Commit 54e556c

Browse files
authored
feat(query): add create_query to /v1/catalog/list_database_tables (#19099)
1 parent 35b00b5 commit 54e556c

File tree

2 files changed

+59
-46
lines changed

2 files changed

+59
-46
lines changed

src/query/service/src/servers/http/v1/catalog/get_database_table.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ use crate::servers::http::v1::HttpQueryContext;
3434

3535
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Default)]
3636
pub struct GetDatabaseTableResponse {
37-
pub table: Option<TableDetails>,
37+
pub table: Option<TableDetail>,
3838
pub warnings: Vec<String>,
3939
}
4040

4141
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Default)]
42-
pub struct TableDetails {
42+
pub struct TableDetail {
4343
pub name: String,
4444
pub database: String,
4545
pub catalog: String,
@@ -122,7 +122,7 @@ async fn handle(
122122
});
123123

124124
Ok(GetDatabaseTableResponse {
125-
table: Some(TableDetails {
125+
table: Some(TableDetail {
126126
name: tbl.name().to_string(),
127127
database: db.name().to_string(),
128128
catalog: catalog.name().to_string(),

src/query/service/src/servers/http/v1/catalog/list_database_tables.rs

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use chrono::DateTime;
16-
use chrono::Utc;
15+
use databend_common_ast::parser::Dialect;
1716
use databend_common_catalog::catalog::CatalogManager;
1817
use databend_common_exception::ErrorCode;
1918
use databend_common_exception::Result;
@@ -27,27 +26,17 @@ use poem::IntoResponse;
2726
use serde::Deserialize;
2827
use serde::Serialize;
2928

29+
use crate::interpreters::ShowCreateQuerySettings;
30+
use crate::interpreters::ShowCreateTableInterpreter;
31+
use crate::servers::http::v1::catalog::get_database_table::TableDetail;
3032
use crate::servers::http::v1::HttpQueryContext;
3133

3234
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Default)]
3335
pub struct ListDatabaseTablesResponse {
34-
pub tables: Vec<TableInfo>,
36+
pub tables: Vec<TableDetail>,
3537
pub warnings: Vec<String>,
3638
}
3739

38-
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Default)]
39-
pub struct TableInfo {
40-
pub name: String,
41-
pub database: String,
42-
pub catalog: String,
43-
pub engine: String,
44-
pub create_time: DateTime<Utc>,
45-
pub num_rows: u64,
46-
pub data_size: u64,
47-
pub data_compressed_size: u64,
48-
pub index_size: u64,
49-
}
50-
5140
#[async_backtrace::framed]
5241
async fn handle(ctx: &HttpQueryContext, database: String) -> Result<ListDatabaseTablesResponse> {
5342
let tenant = ctx.session.get_current_tenant();
@@ -70,35 +59,59 @@ async fn handle(ctx: &HttpQueryContext, database: String) -> Result<ListDatabase
7059
)));
7160
}
7261

73-
let warnings = vec![];
74-
let tables = db
75-
.list_tables()
76-
.await?
77-
.into_iter()
78-
.filter(|tbl| {
79-
visibility_checker.check_table_visibility(
80-
catalog.name().as_str(),
62+
let settings = ShowCreateQuerySettings {
63+
sql_dialect: Dialect::PostgreSQL,
64+
force_quoted_ident: false,
65+
quoted_ident_case_sensitive: true,
66+
hide_options_in_show_create_table: false,
67+
};
68+
69+
let mut warnings = vec![];
70+
let mut tables = vec![];
71+
for tbl in db.list_tables().await? {
72+
if !visibility_checker.check_table_visibility(
73+
catalog.name().as_str(),
74+
db.name(),
75+
tbl.name(),
76+
db.get_db_info().database_id.db_id,
77+
tbl.get_table_info().ident.table_id,
78+
) {
79+
continue;
80+
}
81+
let info = tbl.get_table_info();
82+
let create_query = ShowCreateTableInterpreter::show_create_query(
83+
catalog.as_ref(),
84+
db.name(),
85+
tbl.as_ref(),
86+
&settings,
87+
)
88+
.await
89+
.unwrap_or_else(|e| {
90+
let msg = format!(
91+
"Failed to generate CREATE query for table {}.{}.{}: {}",
92+
catalog.name(),
8193
db.name(),
8294
tbl.name(),
83-
db.get_db_info().database_id.db_id,
84-
tbl.get_table_info().ident.table_id,
85-
)
86-
})
87-
.map(|tbl| {
88-
let info = tbl.get_table_info();
89-
TableInfo {
90-
name: tbl.name().to_string(),
91-
database: db.name().to_string(),
92-
catalog: catalog.name().clone(),
93-
engine: info.meta.engine.clone(),
94-
create_time: info.meta.created_on,
95-
num_rows: info.meta.statistics.number_of_rows,
96-
data_size: info.meta.statistics.data_bytes,
97-
data_compressed_size: info.meta.statistics.compressed_data_bytes,
98-
index_size: info.meta.statistics.index_data_bytes,
99-
}
100-
})
101-
.collect::<Vec<_>>();
95+
e
96+
);
97+
log::warn!("{}", msg);
98+
warnings.push(msg);
99+
"".to_owned()
100+
});
101+
102+
tables.push(TableDetail {
103+
name: tbl.name().to_string(),
104+
database: db.name().to_string(),
105+
catalog: catalog.name().clone(),
106+
engine: info.meta.engine.clone(),
107+
create_time: info.meta.created_on,
108+
num_rows: info.meta.statistics.number_of_rows,
109+
data_size: info.meta.statistics.data_bytes,
110+
data_compressed_size: info.meta.statistics.compressed_data_bytes,
111+
index_size: info.meta.statistics.index_data_bytes,
112+
create_query,
113+
});
114+
}
102115

103116
Ok(ListDatabaseTablesResponse { tables, warnings })
104117
}

0 commit comments

Comments
 (0)