Skip to content

Commit d7a5525

Browse files
committed
add system.branches and show branches
1 parent 4c0e0a2 commit d7a5525

21 files changed

Lines changed: 573 additions & 29 deletions

File tree

src/meta/api/src/api_impl/ref_api.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -716,13 +716,13 @@ where
716716

717717
// 3. Collect all unique branch_ids
718718
let mut branch_id_set: HashSet<u64> = active_map.keys().copied().collect();
719-
let mut dropped_map: HashMap<u64, (String, DateTime<Utc>)> = HashMap::new();
719+
let mut dropped_map: HashMap<u64, (String, DroppedBranchMeta)> = HashMap::new();
720720

721721
for (key, seq_dropped) in &dropped_branches {
722722
branch_id_set.insert(key.branch_id);
723723
dropped_map.insert(
724724
key.branch_id,
725-
(key.branch_name.clone(), seq_dropped.data.drop_on),
725+
(key.branch_name.clone(), seq_dropped.data.clone()),
726726
);
727727
}
728728

@@ -746,8 +746,8 @@ where
746746

747747
// Filter branches whose effective delete time is already beyond the retention window.
748748
if let Some(retention_boundary) = req.retention_boundary {
749-
if let Some((_, drop_on)) = dropped_map.get(&branch_id.table_id) {
750-
if *drop_on < retention_boundary {
749+
if let Some((_, drop_meta)) = dropped_map.get(&branch_id.table_id) {
750+
if drop_meta.drop_on < retention_boundary {
751751
continue;
752752
}
753753
} else if let Some((_, expire_at)) = active_map.get(&branch_id.table_id) {
@@ -760,8 +760,8 @@ where
760760
let (branch_name, expire_at) =
761761
if let Some((name, expire_at)) = active_map.get(&branch_id.table_id) {
762762
(name.clone(), *expire_at)
763-
} else if let Some((name, _)) = dropped_map.get(&branch_id.table_id) {
764-
(name.clone(), None)
763+
} else if let Some((name, drop_meta)) = dropped_map.get(&branch_id.table_id) {
764+
(name.clone(), drop_meta.expire_at)
765765
} else {
766766
// unreachable.
767767
warn!(

src/query/ast/src/ast/statements/statement.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ pub enum Statement {
182182

183183
// Tables
184184
ShowTables(ShowTablesStmt),
185+
ShowBranches(ShowBranchesStmt),
185186
ShowCreateTable(ShowCreateTableStmt),
186187
DescribeTable(DescribeTableStmt),
187188
ShowTablesStatus(ShowTablesStatusStmt),
@@ -495,6 +496,7 @@ impl Statement {
495496
| Statement::ShowCreateDatabase(..)
496497
| Statement::UseDatabase { .. }
497498
| Statement::ShowTables(..)
499+
| Statement::ShowBranches(..)
498500
| Statement::ShowCreateTable(..)
499501
| Statement::DescribeTable(..)
500502
| Statement::ShowStatistics(..)
@@ -859,6 +861,7 @@ impl Display for Statement {
859861
Statement::AlterDatabase(stmt) => write!(f, "{stmt}")?,
860862
Statement::UseDatabase { database } => write!(f, "USE {database}")?,
861863
Statement::ShowTables(stmt) => write!(f, "{stmt}")?,
864+
Statement::ShowBranches(stmt) => write!(f, "{stmt}")?,
862865
Statement::ShowColumns(stmt) => write!(f, "{stmt}")?,
863866
Statement::ShowCreateTable(stmt) => write!(f, "{stmt}")?,
864867
Statement::DescribeTable(stmt) => write!(f, "{stmt}")?,

src/query/ast/src/ast/statements/table.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::ast::UriLocation;
3535
use crate::ast::quote::QuotedString;
3636
use crate::ast::statements::constraint::ConstraintType;
3737
use crate::ast::statements::show::ShowLimit;
38+
use crate::ast::statements::show::ShowOptions;
3839
use crate::ast::write_comma_separated_list;
3940
use crate::ast::write_comma_separated_string_map;
4041
use crate::ast::write_dot_separated_list;
@@ -74,6 +75,31 @@ impl Display for ShowTablesStmt {
7475
}
7576
}
7677

78+
#[derive(Debug, Clone, PartialEq, Drive, DriveMut, Walk, WalkMut)]
79+
pub struct ShowBranchesStmt {
80+
pub catalog: Option<Identifier>,
81+
pub database: Option<Identifier>,
82+
pub table: Identifier,
83+
pub show_options: Option<ShowOptions>,
84+
}
85+
86+
impl Display for ShowBranchesStmt {
87+
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
88+
write!(f, "SHOW BRANCHES FROM ")?;
89+
write_dot_separated_list(
90+
f,
91+
self.catalog
92+
.iter()
93+
.chain(&self.database)
94+
.chain(Some(&self.table)),
95+
)?;
96+
if let Some(show_options) = &self.show_options {
97+
write!(f, " {show_options}")?;
98+
}
99+
Ok(())
100+
}
101+
}
102+
77103
#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut, Walk, WalkMut)]
78104
pub struct ShowCreateTableStmt {
79105
pub catalog: Option<Identifier>,

src/query/ast/src/parser/statement.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,20 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
833833
},
834834
);
835835

836+
let show_branches = map(
837+
rule! {
838+
SHOW ~ BRANCHES ~ ( FROM | IN ) ~ #dot_separated_idents_1_to_3 ~ #show_options?
839+
},
840+
|(_, _, _, (catalog, database, table), opt_options)| {
841+
Statement::ShowBranches(ShowBranchesStmt {
842+
catalog,
843+
database,
844+
table,
845+
show_options: opt_options,
846+
})
847+
},
848+
);
849+
836850
let show_databases = map(
837851
rule! {
838852
SHOW ~ FULL? ~ ( DATABASES | SCHEMAS ) ~ ( ( FROM | IN ) ~ ^#ident )? ~ #show_limit?
@@ -2782,6 +2796,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
27822796
)
27832797
| (
27842798
#show_tables : "`SHOW [FULL] TABLES [FROM <database>] [<show_limit>]`"
2799+
| #show_branches : "`SHOW BRANCHES FROM <table> [<show_options>]`"
27852800
| #show_columns : "`SHOW [FULL] COLUMNS FROM <table> [FROM|IN <catalog>.<database>] [<show_limit>]`"
27862801
| #show_create_table : "`SHOW CREATE TABLE [<database>.]<table>`"
27872802
| #show_fields : "`SHOW FIELDS FROM [<database>.]<table>`"

src/query/ast/src/parser/token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ pub enum TokenKind {
480480
BOTH,
481481
#[token("BRANCH", ignore(ascii_case))]
482482
BRANCH,
483+
#[token("BRANCHES", ignore(ascii_case))]
484+
BRANCHES,
483485
#[token("BY", ignore(ascii_case))]
484486
BY,
485487
#[token("BROTLI", ignore(ascii_case))]

src/query/ast/src/visit/statement.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ impl Walk for Statement {
139139
Statement::AlterDatabase(stmt) => try_walk!(stmt.walk(visitor)),
140140
Statement::UseDatabase { database } => try_walk!(database.walk(visitor)),
141141
Statement::ShowTables(stmt) => try_walk!(stmt.walk(visitor)),
142+
Statement::ShowBranches(stmt) => try_walk!(stmt.walk(visitor)),
142143
Statement::ShowCreateTable(stmt) => try_walk!(stmt.walk(visitor)),
143144
Statement::DescribeTable(stmt) => try_walk!(stmt.walk(visitor)),
144145
Statement::DropTable(stmt) => try_walk!(stmt.walk(visitor)),
@@ -352,6 +353,7 @@ impl WalkMut for Statement {
352353
Statement::AlterDatabase(stmt) => try_walk!(stmt.walk_mut(visitor)),
353354
Statement::UseDatabase { database } => try_walk!(database.walk_mut(visitor)),
354355
Statement::ShowTables(stmt) => try_walk!(stmt.walk_mut(visitor)),
356+
Statement::ShowBranches(stmt) => try_walk!(stmt.walk_mut(visitor)),
355357
Statement::ShowCreateTable(stmt) => try_walk!(stmt.walk_mut(visitor)),
356358
Statement::DescribeTable(stmt) => try_walk!(stmt.walk_mut(visitor)),
357359
Statement::DropTable(stmt) => try_walk!(stmt.walk_mut(visitor)),

src/query/ast/tests/it/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ SELECT * from s;"#,
401401
r#"ALTER TABLE t DROP BRANCH b1;"#,
402402
r#"ALTER TABLE t UNDROP BRANCH b1 RETAIN 7 DAYS;"#,
403403
r#"ALTER TABLE t DROP TAG tag1;"#,
404+
r#"SHOW BRANCHES FROM ctl.db.t LIKE 'dev%' LIMIT 3;"#,
404405
r#"ALTER DATABASE IF EXISTS ctl.c RENAME TO a;"#,
405406
r#"ALTER DATABASE c RENAME TO a;"#,
406407
r#"ALTER DATABASE c set tag tag1='a';"#,

src/query/ast/tests/it/testdata/stmt-error.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ error:
391391
--> SQL:1:6
392392
|
393393
1 | SHOW GRANT FOR ROLE 'role1';
394-
| ^^^^^ unexpected `GRANT`. it's reserved keyword, you may avoid using it, expecting `GRANTS`, `CREATE`, `VIRTUAL`, `NETWORK`, `CATALOGS`, `STREAMS`, `FUNCTIONS`, `DATABASES`, `CONNECTIONS`, `TABLE_FUNCTIONS`, `TAGS`, `DROP`, `TASKS`, `TABLE`, `ROLES`, `ONLINE`, `INDEXES`, `WORKERS`, `COLUMNS`, `WORKLOAD`, `PASSWORD`, `SEQUENCES`, `PROCEDURES`, `PROCESSLIST`, `STAGES`, `TABLES`, `DICTIONARIES`, `METRICS`, `ENGINES`, `SETTINGS`, `VARIABLES`, `WAREHOUSES`, `STATISTICS`, `USER`, `LOCKS`, `SCHEMAS`, `FIELDS`, `VIEWS`, `USERS`, `FILE`, or `FULL`
394+
| ^^^^^ unexpected `GRANT`. it's reserved keyword, you may avoid using it, expecting `GRANTS`, `CREATE`, `BRANCHES`, `VIRTUAL`, `NETWORK`, `CATALOGS`, `STREAMS`, `FUNCTIONS`, `DATABASES`, `CONNECTIONS`, `TABLE_FUNCTIONS`, `TAGS`, `DROP`, `TASKS`, `TABLE`, `ROLES`, `ONLINE`, `INDEXES`, `WORKERS`, `COLUMNS`, `WORKLOAD`, `PASSWORD`, `SEQUENCES`, `PROCEDURES`, `PROCESSLIST`, `STAGES`, `TABLES`, `DICTIONARIES`, `METRICS`, `ENGINES`, `SETTINGS`, `VARIABLES`, `WAREHOUSES`, `STATISTICS`, `USER`, `LOCKS`, `SCHEMAS`, `FIELDS`, `VIEWS`, `USERS`, `FILE`, or `FULL`
395395

396396

397397
---------- Input ----------

src/query/ast/tests/it/testdata/stmt.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18989,6 +18989,57 @@ AlterTable(
1898918989
)
1899018990

1899118991

18992+
---------- Input ----------
18993+
SHOW BRANCHES FROM ctl.db.t LIKE 'dev%' LIMIT 3;
18994+
---------- Output ---------
18995+
SHOW BRANCHES FROM ctl.db.t LIKE 'dev%' LIMIT 3
18996+
---------- AST ------------
18997+
ShowBranches(
18998+
ShowBranchesStmt {
18999+
catalog: Some(
19000+
Identifier {
19001+
span: Some(
19002+
19..22,
19003+
),
19004+
name: "ctl",
19005+
quote: None,
19006+
ident_type: None,
19007+
},
19008+
),
19009+
database: Some(
19010+
Identifier {
19011+
span: Some(
19012+
23..25,
19013+
),
19014+
name: "db",
19015+
quote: None,
19016+
ident_type: None,
19017+
},
19018+
),
19019+
table: Identifier {
19020+
span: Some(
19021+
26..27,
19022+
),
19023+
name: "t",
19024+
quote: None,
19025+
ident_type: None,
19026+
},
19027+
show_options: Some(
19028+
ShowOptions {
19029+
show_limit: Some(
19030+
Like {
19031+
pattern: "dev%",
19032+
},
19033+
),
19034+
limit: Some(
19035+
3,
19036+
),
19037+
},
19038+
),
19039+
},
19040+
)
19041+
19042+
1899219043
---------- Input ----------
1899319044
ALTER DATABASE IF EXISTS ctl.c RENAME TO a;
1899419045
---------- Output ---------

src/query/service/src/databases/system/system_database.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use databend_common_meta_app::schema::DatabaseMeta;
2222
use databend_common_meta_app::schema::database_name_ident::DatabaseNameIdent;
2323
use databend_common_meta_app::tenant::Tenant;
2424
use databend_common_storages_system::BacktraceTable;
25+
use databend_common_storages_system::BranchesTableWithHistory;
26+
use databend_common_storages_system::BranchesTableWithoutHistory;
2527
use databend_common_storages_system::BuildOptionsTable;
2628
use databend_common_storages_system::CachesTable;
2729
use databend_common_storages_system::CatalogsTable;
@@ -128,6 +130,7 @@ impl SystemDatabase {
128130
),
129131
SettingsTable::create(sys_db_meta.next_table_id()),
130132
TablesTableWithoutHistory::create(sys_db_meta.next_table_id(), ctl_name),
133+
BranchesTableWithoutHistory::create(sys_db_meta.next_table_id(), ctl_name),
131134
ClustersTable::create(sys_db_meta.next_table_id()),
132135
DatabasesTableWithoutHistory::create(sys_db_meta.next_table_id(), ctl_name),
133136
FullStreamsTable::create(sys_db_meta.next_table_id(), ctl_name),
@@ -159,6 +162,7 @@ impl SystemDatabase {
159162
if let Some(config) = config {
160163
table_list.extend(vec![
161164
TablesTableWithHistory::create(sys_db_meta.next_table_id(), ctl_name),
165+
BranchesTableWithHistory::create(sys_db_meta.next_table_id(), ctl_name),
162166
DatabasesTableWithHistory::create(sys_db_meta.next_table_id(), ctl_name),
163167
BuildOptionsTable::create(
164168
sys_db_meta.next_table_id(),

0 commit comments

Comments
 (0)