Skip to main content

common_meta/rpc/
ddl.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[cfg(feature = "enterprise")]
16pub mod trigger;
17
18use std::collections::{HashMap, HashSet};
19use std::fmt;
20use std::result;
21use std::time::Duration;
22
23use api::helper::{from_pb_time_ranges, to_pb_time_ranges};
24use api::v1::alter_database_expr::Kind as PbAlterDatabaseKind;
25use api::v1::meta::ddl_task_request::Task;
26use api::v1::meta::{
27    AlterDatabaseTask as PbAlterDatabaseTask, AlterTableTask as PbAlterTableTask,
28    AlterTableTasks as PbAlterTableTasks, CommentOnTask as PbCommentOnTask,
29    CreateDatabaseTask as PbCreateDatabaseTask, CreateFlowTask as PbCreateFlowTask,
30    CreateTableTask as PbCreateTableTask, CreateTableTasks as PbCreateTableTasks,
31    CreateViewTask as PbCreateViewTask, DdlTaskRequest as PbDdlTaskRequest,
32    DdlTaskResponse as PbDdlTaskResponse, DropDatabaseTask as PbDropDatabaseTask,
33    DropFlowTask as PbDropFlowTask, DropTableTask as PbDropTableTask,
34    DropTableTasks as PbDropTableTasks, DropViewTask as PbDropViewTask, Partition, ProcedureId,
35    PurgeDroppedTableTask as PbPurgeDroppedTableTask, TruncateTableTask as PbTruncateTableTask,
36    UndropTableTask as PbUndropTableTask,
37};
38use api::v1::{
39    AlterDatabaseExpr, AlterTableExpr, CommentObjectType as PbCommentObjectType, CommentOnExpr,
40    CreateDatabaseExpr, CreateFlowExpr, CreateTableExpr, CreateViewExpr, DropDatabaseExpr,
41    DropFlowExpr, DropTableExpr, DropViewExpr, EvalInterval, ExpireAfter, Option as PbOption,
42    QueryContext as PbQueryContext, TruncateTableExpr,
43};
44use base64::Engine as _;
45use base64::engine::general_purpose;
46use common_base::protocol::Channel;
47use common_catalog::{format_full_flow_name, format_full_table_name};
48use common_error::ext::BoxedError;
49pub use common_event_recorder::TriggerReason;
50use common_time::{DatabaseTimeToLive, Timestamp};
51use prost::Message;
52use serde::{Deserialize, Serialize};
53use serde_with::{DefaultOnNull, serde_as};
54use snafu::{OptionExt, ResultExt};
55use table::metadata::{TableId, TableInfo};
56use table::requests::{validate_database_option, validate_database_option_value};
57use table::table_name::TableName;
58use table::table_reference::TableReference;
59
60use crate::error::{
61    self, ConvertTimeRangesSnafu, ExternalSnafu, InvalidSetDatabaseOptionSnafu,
62    InvalidUnsetDatabaseOptionSnafu, Result,
63};
64use crate::flow_name::FlowName;
65use crate::instruction::CacheIdent;
66use crate::key::FlowId;
67use crate::key::flow::flow_name::FlowNameManager;
68use crate::key::table_name::{TableNameKey, TableNameManager};
69
70/// Reserved query-context extension key for the frontend peer address that submitted a DDL request.
71pub const ORIGIN_FRONTEND_ADDR_EXTENSION_KEY: &str = "__greptime_origin_frontend.addr";
72/// Ordered source table IDs pinned by the caller of an automatic logical-table ALTER.
73pub const ALTER_LOGICAL_TABLE_IDS_EXTENSION_KEY: &str = "__greptime_alter_logical_tables.ids";
74/// Reserved query-context extension key for the authenticated database creator.
75pub const CREATE_DATABASE_CREATOR_EXTENSION_KEY: &str = "__greptime_create_database.creator";
76/// Internal gRPC metadata key for the authenticated database creator.
77pub const CREATE_DATABASE_CREATOR_METADATA_KEY: &str =
78    "x-greptime-internal-create-database-creator-bin";
79
80#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
81pub struct CreatorGrantIntent {
82    pub username: String,
83    pub created_at_ns: i64,
84}
85
86/// DDL tasks
87#[derive(Debug, Clone)]
88pub enum DdlTask {
89    CreateTable(CreateTableTask),
90    DropTable(DropTableTask),
91    UndropTable(UndropTableTask),
92    PurgeDroppedTable(PurgeDroppedTableTask),
93    AlterTable(AlterTableTask),
94    TruncateTable(TruncateTableTask),
95    CreateLogicalTables(Vec<CreateTableTask>),
96    DropLogicalTables(Vec<DropTableTask>),
97    AlterLogicalTables(Vec<AlterTableTask>),
98    CreateDatabase(CreateDatabaseTask),
99    DropDatabase(DropDatabaseTask),
100    AlterDatabase(AlterDatabaseTask),
101    CreateFlow(CreateFlowTask),
102    DropFlow(DropFlowTask),
103    #[cfg(feature = "enterprise")]
104    DropTrigger(trigger::DropTriggerTask),
105    CreateView(CreateViewTask),
106    DropView(DropViewTask),
107    #[cfg(feature = "enterprise")]
108    CreateTrigger(trigger::CreateTriggerTask),
109    CommentOn(CommentOnTask),
110}
111
112impl DdlTask {
113    /// Creates a [`DdlTask`] to create a flow.
114    pub fn new_create_flow(expr: CreateFlowTask) -> Self {
115        DdlTask::CreateFlow(expr)
116    }
117
118    /// Creates a [`DdlTask`] to drop a flow.
119    pub fn new_drop_flow(expr: DropFlowTask) -> Self {
120        DdlTask::DropFlow(expr)
121    }
122
123    /// Creates a [`DdlTask`] to drop a view.
124    pub fn new_drop_view(expr: DropViewTask) -> Self {
125        DdlTask::DropView(expr)
126    }
127
128    /// Creates a [`DdlTask`] to create a table.
129    pub fn new_create_table(
130        expr: CreateTableExpr,
131        partitions: Vec<Partition>,
132        table_info: TableInfo,
133    ) -> Self {
134        DdlTask::CreateTable(CreateTableTask::new(expr, partitions, table_info))
135    }
136
137    /// Creates a [`DdlTask`] to create several logical tables.
138    pub fn new_create_logical_tables(table_data: Vec<(CreateTableExpr, TableInfo)>) -> Self {
139        DdlTask::CreateLogicalTables(
140            table_data
141                .into_iter()
142                .map(|(expr, table_info)| CreateTableTask::new(expr, Vec::new(), table_info))
143                .collect(),
144        )
145    }
146
147    /// Creates a [`DdlTask`] to alter several logical tables.
148    pub fn new_alter_logical_tables(table_data: Vec<AlterTableExpr>) -> Self {
149        DdlTask::AlterLogicalTables(
150            table_data
151                .into_iter()
152                .map(|alter_table| AlterTableTask { alter_table })
153                .collect(),
154        )
155    }
156
157    /// Creates a [`DdlTask`] to drop a table.
158    pub fn new_drop_table(
159        catalog: String,
160        schema: String,
161        table: String,
162        table_id: TableId,
163        drop_if_exists: bool,
164    ) -> Self {
165        DdlTask::DropTable(DropTableTask {
166            catalog,
167            schema,
168            table,
169            table_id,
170            drop_if_exists,
171        })
172    }
173
174    /// Creates a [`DdlTask`] to undrop a table.
175    pub fn new_undrop_table(table_id: TableId) -> Self {
176        DdlTask::UndropTable(UndropTableTask { table_id })
177    }
178
179    /// Creates a [`DdlTask`] to purge a dropped table.
180    pub fn new_purge_dropped_table(table_id: TableId) -> Self {
181        DdlTask::PurgeDroppedTable(PurgeDroppedTableTask { table_id })
182    }
183
184    /// Creates a [`DdlTask`] to create a database.
185    pub fn new_create_database(
186        catalog: String,
187        schema: String,
188        create_if_not_exists: bool,
189        options: HashMap<String, String>,
190        creator: Option<CreatorGrantIntent>,
191    ) -> Self {
192        DdlTask::CreateDatabase(CreateDatabaseTask {
193            catalog,
194            schema,
195            create_if_not_exists,
196            options,
197            creator,
198        })
199    }
200
201    /// Creates a [`DdlTask`] to drop a database.
202    pub fn new_drop_database(catalog: String, schema: String, drop_if_exists: bool) -> Self {
203        DdlTask::DropDatabase(DropDatabaseTask {
204            catalog,
205            schema,
206            drop_if_exists,
207        })
208    }
209
210    /// Creates a [`DdlTask`] to alter a database.
211    pub fn new_alter_database(alter_expr: AlterDatabaseExpr) -> Self {
212        DdlTask::AlterDatabase(AlterDatabaseTask { alter_expr })
213    }
214
215    /// Creates a [`DdlTask`] to alter a table.
216    pub fn new_alter_table(alter_table: AlterTableExpr) -> Self {
217        DdlTask::AlterTable(AlterTableTask { alter_table })
218    }
219
220    /// Creates a [`DdlTask`] to truncate a table.
221    pub fn new_truncate_table(
222        catalog: String,
223        schema: String,
224        table: String,
225        table_id: TableId,
226        time_ranges: Vec<(Timestamp, Timestamp)>,
227    ) -> Self {
228        DdlTask::TruncateTable(TruncateTableTask {
229            catalog,
230            schema,
231            table,
232            table_id,
233            time_ranges,
234        })
235    }
236
237    /// Creates a [`DdlTask`] to create a view.
238    pub fn new_create_view(create_view: CreateViewExpr, view_info: TableInfo) -> Self {
239        DdlTask::CreateView(CreateViewTask {
240            create_view,
241            view_info,
242        })
243    }
244
245    /// Creates a [`DdlTask`] to comment on a table, column, or flow.
246    pub fn new_comment_on(task: CommentOnTask) -> Self {
247        DdlTask::CommentOn(task)
248    }
249}
250
251impl TryFrom<Task> for DdlTask {
252    type Error = error::Error;
253    fn try_from(task: Task) -> Result<Self> {
254        match task {
255            Task::CreateTableTask(create_table) => {
256                Ok(DdlTask::CreateTable(create_table.try_into()?))
257            }
258            Task::DropTableTask(drop_table) => Ok(DdlTask::DropTable(drop_table.try_into()?)),
259            Task::UndropTableTask(undrop_table) => {
260                Ok(DdlTask::UndropTable(undrop_table.try_into()?))
261            }
262            Task::PurgeDroppedTableTask(purge_dropped_table) => {
263                Ok(DdlTask::PurgeDroppedTable(purge_dropped_table.try_into()?))
264            }
265            Task::AlterTableTask(alter_table) => Ok(DdlTask::AlterTable(alter_table.try_into()?)),
266            Task::TruncateTableTask(truncate_table) => {
267                Ok(DdlTask::TruncateTable(truncate_table.try_into()?))
268            }
269            Task::CreateTableTasks(create_tables) => {
270                let tasks = create_tables
271                    .tasks
272                    .into_iter()
273                    .map(|task| task.try_into())
274                    .collect::<Result<Vec<_>>>()?;
275
276                Ok(DdlTask::CreateLogicalTables(tasks))
277            }
278            Task::DropTableTasks(drop_tables) => {
279                let tasks = drop_tables
280                    .tasks
281                    .into_iter()
282                    .map(|task| task.try_into())
283                    .collect::<Result<Vec<_>>>()?;
284
285                Ok(DdlTask::DropLogicalTables(tasks))
286            }
287            Task::AlterTableTasks(alter_tables) => {
288                let tasks = alter_tables
289                    .tasks
290                    .into_iter()
291                    .map(|task| task.try_into())
292                    .collect::<Result<Vec<_>>>()?;
293
294                Ok(DdlTask::AlterLogicalTables(tasks))
295            }
296            Task::CreateDatabaseTask(create_database) => {
297                Ok(DdlTask::CreateDatabase(create_database.try_into()?))
298            }
299            Task::DropDatabaseTask(drop_database) => {
300                Ok(DdlTask::DropDatabase(drop_database.try_into()?))
301            }
302            Task::AlterDatabaseTask(alter_database) => {
303                Ok(DdlTask::AlterDatabase(alter_database.try_into()?))
304            }
305            Task::CreateFlowTask(create_flow) => Ok(DdlTask::CreateFlow(create_flow.try_into()?)),
306            Task::DropFlowTask(drop_flow) => Ok(DdlTask::DropFlow(drop_flow.try_into()?)),
307            Task::CreateViewTask(create_view) => Ok(DdlTask::CreateView(create_view.try_into()?)),
308            Task::DropViewTask(drop_view) => Ok(DdlTask::DropView(drop_view.try_into()?)),
309            Task::CreateTriggerTask(create_trigger) => {
310                #[cfg(feature = "enterprise")]
311                return Ok(DdlTask::CreateTrigger(create_trigger.try_into()?));
312                #[cfg(not(feature = "enterprise"))]
313                {
314                    let _ = create_trigger;
315                    crate::error::UnsupportedSnafu {
316                        operation: "create trigger",
317                    }
318                    .fail()
319                }
320            }
321            Task::DropTriggerTask(drop_trigger) => {
322                #[cfg(feature = "enterprise")]
323                return Ok(DdlTask::DropTrigger(drop_trigger.try_into()?));
324                #[cfg(not(feature = "enterprise"))]
325                {
326                    let _ = drop_trigger;
327                    crate::error::UnsupportedSnafu {
328                        operation: "drop trigger",
329                    }
330                    .fail()
331                }
332            }
333            Task::CommentOnTask(comment_on) => Ok(DdlTask::CommentOn(comment_on.try_into()?)),
334        }
335    }
336}
337
338#[derive(Clone)]
339pub struct SubmitDdlTaskRequest {
340    pub wait: bool,
341    pub timeout: Duration,
342    pub task: DdlTask,
343}
344
345impl SubmitDdlTaskRequest {
346    /// The default constructor for [`SubmitDdlTaskRequest`].
347    pub fn new(task: DdlTask) -> Self {
348        Self {
349            wait: Self::default_wait(),
350            timeout: Self::default_timeout(),
351            task,
352        }
353    }
354
355    /// The default timeout for a DDL task.
356    pub fn default_timeout() -> Duration {
357        Duration::from_secs(60)
358    }
359
360    /// The default wait for a DDL task.
361    pub fn default_wait() -> bool {
362        true
363    }
364}
365
366fn ddl_timeout_secs(timeout: Duration) -> u32 {
367    timeout
368        .as_nanos()
369        .div_ceil(Duration::from_secs(1).as_nanos())
370        .try_into()
371        .unwrap_or(u32::MAX)
372}
373
374impl TryFrom<SubmitDdlTaskRequest> for PbDdlTaskRequest {
375    type Error = error::Error;
376
377    fn try_from(request: SubmitDdlTaskRequest) -> Result<Self> {
378        let SubmitDdlTaskRequest {
379            wait,
380            timeout,
381            task,
382        } = request;
383
384        let task = match task {
385            DdlTask::CreateTable(task) => Task::CreateTableTask(task.try_into()?),
386            DdlTask::DropTable(task) => Task::DropTableTask(task.into()),
387            DdlTask::UndropTable(task) => Task::UndropTableTask(task.into()),
388            DdlTask::PurgeDroppedTable(task) => Task::PurgeDroppedTableTask(task.into()),
389            DdlTask::AlterTable(task) => Task::AlterTableTask(task.try_into()?),
390            DdlTask::TruncateTable(task) => Task::TruncateTableTask(task.try_into()?),
391            DdlTask::CreateLogicalTables(tasks) => {
392                let tasks = tasks
393                    .into_iter()
394                    .map(|task| task.try_into())
395                    .collect::<Result<Vec<_>>>()?;
396
397                Task::CreateTableTasks(PbCreateTableTasks { tasks })
398            }
399            DdlTask::DropLogicalTables(tasks) => {
400                let tasks = tasks
401                    .into_iter()
402                    .map(|task| task.into())
403                    .collect::<Vec<_>>();
404
405                Task::DropTableTasks(PbDropTableTasks { tasks })
406            }
407            DdlTask::AlterLogicalTables(tasks) => {
408                let tasks = tasks
409                    .into_iter()
410                    .map(|task| task.try_into())
411                    .collect::<Result<Vec<_>>>()?;
412
413                Task::AlterTableTasks(PbAlterTableTasks { tasks })
414            }
415            DdlTask::CreateDatabase(task) => Task::CreateDatabaseTask(task.try_into()?),
416            DdlTask::DropDatabase(task) => Task::DropDatabaseTask(task.try_into()?),
417            DdlTask::AlterDatabase(task) => Task::AlterDatabaseTask(task.try_into()?),
418            DdlTask::CreateFlow(task) => Task::CreateFlowTask(task.into()),
419            DdlTask::DropFlow(task) => Task::DropFlowTask(task.into()),
420            DdlTask::CreateView(task) => Task::CreateViewTask(task.try_into()?),
421            DdlTask::DropView(task) => Task::DropViewTask(task.into()),
422            #[cfg(feature = "enterprise")]
423            DdlTask::CreateTrigger(task) => Task::CreateTriggerTask(task.try_into()?),
424            #[cfg(feature = "enterprise")]
425            DdlTask::DropTrigger(task) => Task::DropTriggerTask(task.into()),
426            DdlTask::CommentOn(task) => Task::CommentOnTask(task.into()),
427        };
428
429        Ok(Self {
430            header: None,
431            query_context: None,
432            timeout_secs: ddl_timeout_secs(timeout),
433            wait,
434            task: Some(task),
435            event_context: None,
436            actor: None,
437        })
438    }
439}
440
441#[derive(Debug, Default)]
442pub struct SubmitDdlTaskResponse {
443    pub key: Vec<u8>,
444    // `table_id`s for `CREATE TABLE` or `CREATE LOGICAL TABLES` task.
445    pub table_ids: Vec<TableId>,
446}
447
448impl TryFrom<PbDdlTaskResponse> for SubmitDdlTaskResponse {
449    type Error = error::Error;
450
451    fn try_from(resp: PbDdlTaskResponse) -> Result<Self> {
452        let table_ids = resp.table_ids.into_iter().map(|t| t.id).collect();
453        Ok(Self {
454            key: resp.pid.map(|pid| pid.key).unwrap_or_default(),
455            table_ids,
456        })
457    }
458}
459
460impl From<SubmitDdlTaskResponse> for PbDdlTaskResponse {
461    fn from(val: SubmitDdlTaskResponse) -> Self {
462        Self {
463            pid: Some(ProcedureId { key: val.key }),
464            table_ids: val
465                .table_ids
466                .into_iter()
467                .map(|id| api::v1::TableId { id })
468                .collect(),
469            ..Default::default()
470        }
471    }
472}
473
474/// A `CREATE VIEW` task.
475#[derive(Debug, PartialEq, Clone)]
476pub struct CreateViewTask {
477    pub create_view: CreateViewExpr,
478    pub view_info: TableInfo,
479}
480
481impl CreateViewTask {
482    /// Returns the [`TableReference`] of view.
483    pub fn table_ref(&self) -> TableReference<'_> {
484        TableReference {
485            catalog: &self.create_view.catalog_name,
486            schema: &self.create_view.schema_name,
487            table: &self.create_view.view_name,
488        }
489    }
490
491    /// Returns the encoded logical plan
492    pub fn raw_logical_plan(&self) -> &Vec<u8> {
493        &self.create_view.logical_plan
494    }
495
496    /// Returns the view definition in SQL
497    pub fn view_definition(&self) -> &str {
498        &self.create_view.definition
499    }
500
501    /// Returns the resolved table names in view's logical plan
502    pub fn table_names(&self) -> HashSet<TableName> {
503        self.create_view
504            .table_names
505            .iter()
506            .map(|t| t.clone().into())
507            .collect()
508    }
509
510    /// Returns the view's columns
511    pub fn columns(&self) -> &Vec<String> {
512        &self.create_view.columns
513    }
514
515    /// Returns the original logical plan's columns
516    pub fn plan_columns(&self) -> &Vec<String> {
517        &self.create_view.plan_columns
518    }
519}
520
521impl TryFrom<PbCreateViewTask> for CreateViewTask {
522    type Error = error::Error;
523
524    fn try_from(pb: PbCreateViewTask) -> Result<Self> {
525        let view_info = serde_json::from_slice(&pb.view_info).context(error::SerdeJsonSnafu)?;
526
527        Ok(CreateViewTask {
528            create_view: pb.create_view.context(error::InvalidProtoMsgSnafu {
529                err_msg: "expected create view",
530            })?,
531            view_info,
532        })
533    }
534}
535
536impl TryFrom<CreateViewTask> for PbCreateViewTask {
537    type Error = error::Error;
538
539    fn try_from(task: CreateViewTask) -> Result<PbCreateViewTask> {
540        Ok(PbCreateViewTask {
541            create_view: Some(task.create_view),
542            view_info: serde_json::to_vec(&task.view_info).context(error::SerdeJsonSnafu)?,
543        })
544    }
545}
546
547impl Serialize for CreateViewTask {
548    fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
549    where
550        S: serde::Serializer,
551    {
552        let view_info = serde_json::to_vec(&self.view_info)
553            .map_err(|err| serde::ser::Error::custom(err.to_string()))?;
554
555        let pb = PbCreateViewTask {
556            create_view: Some(self.create_view.clone()),
557            view_info,
558        };
559        let buf = pb.encode_to_vec();
560        let encoded = general_purpose::STANDARD_NO_PAD.encode(buf);
561        serializer.serialize_str(&encoded)
562    }
563}
564
565impl<'de> Deserialize<'de> for CreateViewTask {
566    fn deserialize<D>(deserializer: D) -> result::Result<Self, D::Error>
567    where
568        D: serde::Deserializer<'de>,
569    {
570        let encoded = String::deserialize(deserializer)?;
571        let buf = general_purpose::STANDARD_NO_PAD
572            .decode(encoded)
573            .map_err(|err| serde::de::Error::custom(err.to_string()))?;
574        let expr: PbCreateViewTask = PbCreateViewTask::decode(&*buf)
575            .map_err(|err| serde::de::Error::custom(err.to_string()))?;
576
577        let expr = CreateViewTask::try_from(expr)
578            .map_err(|err| serde::de::Error::custom(err.to_string()))?;
579
580        Ok(expr)
581    }
582}
583
584/// A `DROP VIEW` task.
585#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
586pub struct DropViewTask {
587    pub catalog: String,
588    pub schema: String,
589    pub view: String,
590    pub view_id: TableId,
591    pub drop_if_exists: bool,
592}
593
594impl DropViewTask {
595    /// Returns the [`TableReference`] of view.
596    pub fn table_ref(&self) -> TableReference<'_> {
597        TableReference {
598            catalog: &self.catalog,
599            schema: &self.schema,
600            table: &self.view,
601        }
602    }
603}
604
605impl TryFrom<PbDropViewTask> for DropViewTask {
606    type Error = error::Error;
607
608    fn try_from(pb: PbDropViewTask) -> Result<Self> {
609        let expr = pb.drop_view.context(error::InvalidProtoMsgSnafu {
610            err_msg: "expected drop view",
611        })?;
612
613        Ok(DropViewTask {
614            catalog: expr.catalog_name,
615            schema: expr.schema_name,
616            view: expr.view_name,
617            view_id: expr
618                .view_id
619                .context(error::InvalidProtoMsgSnafu {
620                    err_msg: "expected view_id",
621                })?
622                .id,
623            drop_if_exists: expr.drop_if_exists,
624        })
625    }
626}
627
628impl From<DropViewTask> for PbDropViewTask {
629    fn from(task: DropViewTask) -> Self {
630        PbDropViewTask {
631            drop_view: Some(DropViewExpr {
632                catalog_name: task.catalog,
633                schema_name: task.schema,
634                view_name: task.view,
635                view_id: Some(api::v1::TableId { id: task.view_id }),
636                drop_if_exists: task.drop_if_exists,
637            }),
638        }
639    }
640}
641
642#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
643pub struct DropTableTask {
644    pub catalog: String,
645    pub schema: String,
646    pub table: String,
647    pub table_id: TableId,
648    #[serde(default)]
649    pub drop_if_exists: bool,
650}
651
652#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
653pub struct UndropTableTask {
654    pub table_id: TableId,
655}
656
657#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
658pub struct PurgeDroppedTableTask {
659    pub table_id: TableId,
660}
661
662impl TryFrom<PbUndropTableTask> for UndropTableTask {
663    type Error = error::Error;
664
665    fn try_from(pb: PbUndropTableTask) -> Result<Self> {
666        Ok(Self {
667            table_id: pb
668                .table_id
669                .context(error::InvalidProtoMsgSnafu {
670                    err_msg: "expected table_id",
671                })?
672                .id,
673        })
674    }
675}
676
677impl From<UndropTableTask> for PbUndropTableTask {
678    fn from(task: UndropTableTask) -> Self {
679        Self {
680            table_id: Some(api::v1::TableId { id: task.table_id }),
681        }
682    }
683}
684
685impl TryFrom<PbPurgeDroppedTableTask> for PurgeDroppedTableTask {
686    type Error = error::Error;
687
688    fn try_from(pb: PbPurgeDroppedTableTask) -> Result<Self> {
689        Ok(Self {
690            table_id: pb
691                .table_id
692                .context(error::InvalidProtoMsgSnafu {
693                    err_msg: "expected table_id",
694                })?
695                .id,
696        })
697    }
698}
699
700impl From<PurgeDroppedTableTask> for PbPurgeDroppedTableTask {
701    fn from(task: PurgeDroppedTableTask) -> Self {
702        Self {
703            table_id: Some(api::v1::TableId { id: task.table_id }),
704        }
705    }
706}
707
708impl DropTableTask {
709    pub fn table_ref(&self) -> TableReference<'_> {
710        TableReference {
711            catalog: &self.catalog,
712            schema: &self.schema,
713            table: &self.table,
714        }
715    }
716
717    pub fn table_name(&self) -> TableName {
718        TableName {
719            catalog_name: self.catalog.clone(),
720            schema_name: self.schema.clone(),
721            table_name: self.table.clone(),
722        }
723    }
724}
725
726impl TryFrom<PbDropTableTask> for DropTableTask {
727    type Error = error::Error;
728
729    fn try_from(pb: PbDropTableTask) -> Result<Self> {
730        let drop_table = pb.drop_table.context(error::InvalidProtoMsgSnafu {
731            err_msg: "expected drop table",
732        })?;
733
734        Ok(Self {
735            catalog: drop_table.catalog_name,
736            schema: drop_table.schema_name,
737            table: drop_table.table_name,
738            table_id: drop_table
739                .table_id
740                .context(error::InvalidProtoMsgSnafu {
741                    err_msg: "expected table_id",
742                })?
743                .id,
744            drop_if_exists: drop_table.drop_if_exists,
745        })
746    }
747}
748
749impl From<DropTableTask> for PbDropTableTask {
750    fn from(task: DropTableTask) -> Self {
751        PbDropTableTask {
752            drop_table: Some(DropTableExpr {
753                catalog_name: task.catalog,
754                schema_name: task.schema,
755                table_name: task.table,
756                table_id: Some(api::v1::TableId { id: task.table_id }),
757                drop_if_exists: task.drop_if_exists,
758            }),
759        }
760    }
761}
762
763#[derive(PartialEq, Clone)]
764pub struct CreateTableTask {
765    pub create_table: CreateTableExpr,
766    pub partitions: Vec<Partition>,
767    pub table_info: TableInfo,
768}
769
770impl fmt::Debug for CreateTableTask {
771    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
772        let mut create_table = self.create_table.clone();
773        for (key, value) in create_table.table_options.iter_mut() {
774            if let Some(redacted) = table::requests::redact_option_value(key, value.as_str()) {
775                *value = redacted;
776            }
777        }
778        f.debug_struct("CreateTableTask")
779            .field("create_table", &create_table)
780            .field("partitions", &self.partitions)
781            .field("table_info", &self.table_info)
782            .finish()
783    }
784}
785
786impl TryFrom<PbCreateTableTask> for CreateTableTask {
787    type Error = error::Error;
788
789    fn try_from(pb: PbCreateTableTask) -> Result<Self> {
790        let table_info = serde_json::from_slice(&pb.table_info).context(error::SerdeJsonSnafu)?;
791
792        Ok(CreateTableTask::new(
793            pb.create_table.context(error::InvalidProtoMsgSnafu {
794                err_msg: "expected create table",
795            })?,
796            pb.partitions,
797            table_info,
798        ))
799    }
800}
801
802impl TryFrom<CreateTableTask> for PbCreateTableTask {
803    type Error = error::Error;
804
805    fn try_from(task: CreateTableTask) -> Result<Self> {
806        Ok(PbCreateTableTask {
807            table_info: serde_json::to_vec(&task.table_info).context(error::SerdeJsonSnafu)?,
808            create_table: Some(task.create_table),
809            partitions: task.partitions,
810        })
811    }
812}
813
814impl CreateTableTask {
815    pub fn new(
816        expr: CreateTableExpr,
817        partitions: Vec<Partition>,
818        table_info: TableInfo,
819    ) -> CreateTableTask {
820        CreateTableTask {
821            create_table: expr,
822            partitions,
823            table_info,
824        }
825    }
826
827    pub fn table_name(&self) -> TableName {
828        let table = &self.create_table;
829
830        TableName {
831            catalog_name: table.catalog_name.clone(),
832            schema_name: table.schema_name.clone(),
833            table_name: table.table_name.clone(),
834        }
835    }
836
837    pub fn table_ref(&self) -> TableReference<'_> {
838        let table = &self.create_table;
839
840        TableReference {
841            catalog: &table.catalog_name,
842            schema: &table.schema_name,
843            table: &table.table_name,
844        }
845    }
846
847    /// Sets the `table_info`'s table_id.
848    pub fn set_table_id(&mut self, table_id: TableId) {
849        self.table_info.ident.table_id = table_id;
850    }
851
852    /// Sort the columns in [CreateTableExpr] and [TableInfo].
853    ///
854    /// This function won't do any check or verification. Caller should
855    /// ensure this task is valid.
856    pub fn sort_columns(&mut self) {
857        // sort create table expr
858        // sort column_defs by name
859        self.create_table
860            .column_defs
861            .sort_unstable_by(|a, b| a.name.cmp(&b.name));
862
863        self.table_info.sort_columns();
864    }
865}
866
867impl Serialize for CreateTableTask {
868    fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
869    where
870        S: serde::Serializer,
871    {
872        let table_info = serde_json::to_vec(&self.table_info)
873            .map_err(|err| serde::ser::Error::custom(err.to_string()))?;
874
875        let pb = PbCreateTableTask {
876            create_table: Some(self.create_table.clone()),
877            partitions: self.partitions.clone(),
878            table_info,
879        };
880        let buf = pb.encode_to_vec();
881        let encoded = general_purpose::STANDARD_NO_PAD.encode(buf);
882        serializer.serialize_str(&encoded)
883    }
884}
885
886impl<'de> Deserialize<'de> for CreateTableTask {
887    fn deserialize<D>(deserializer: D) -> result::Result<Self, D::Error>
888    where
889        D: serde::Deserializer<'de>,
890    {
891        let encoded = String::deserialize(deserializer)?;
892        let buf = general_purpose::STANDARD_NO_PAD
893            .decode(encoded)
894            .map_err(|err| serde::de::Error::custom(err.to_string()))?;
895        let expr: PbCreateTableTask = PbCreateTableTask::decode(&*buf)
896            .map_err(|err| serde::de::Error::custom(err.to_string()))?;
897
898        let expr = CreateTableTask::try_from(expr)
899            .map_err(|err| serde::de::Error::custom(err.to_string()))?;
900
901        Ok(expr)
902    }
903}
904
905#[derive(Debug, PartialEq, Clone)]
906pub struct AlterTableTask {
907    // TODO(CookiePieWw): Replace proto struct with user-defined struct
908    pub alter_table: AlterTableExpr,
909}
910
911impl AlterTableTask {
912    pub fn validate(&self) -> Result<()> {
913        self.alter_table
914            .kind
915            .as_ref()
916            .context(error::UnexpectedSnafu {
917                err_msg: "'kind' is absent",
918            })?;
919        Ok(())
920    }
921
922    pub fn table_ref(&self) -> TableReference<'_> {
923        TableReference {
924            catalog: &self.alter_table.catalog_name,
925            schema: &self.alter_table.schema_name,
926            table: &self.alter_table.table_name,
927        }
928    }
929
930    pub fn table_name(&self) -> TableName {
931        let table = &self.alter_table;
932
933        TableName {
934            catalog_name: table.catalog_name.clone(),
935            schema_name: table.schema_name.clone(),
936            table_name: table.table_name.clone(),
937        }
938    }
939}
940
941impl TryFrom<PbAlterTableTask> for AlterTableTask {
942    type Error = error::Error;
943
944    fn try_from(pb: PbAlterTableTask) -> Result<Self> {
945        let alter_table = pb.alter_table.context(error::InvalidProtoMsgSnafu {
946            err_msg: "expected alter_table",
947        })?;
948
949        Ok(AlterTableTask { alter_table })
950    }
951}
952
953impl TryFrom<AlterTableTask> for PbAlterTableTask {
954    type Error = error::Error;
955
956    fn try_from(task: AlterTableTask) -> Result<Self> {
957        Ok(PbAlterTableTask {
958            alter_table: Some(task.alter_table),
959        })
960    }
961}
962
963impl Serialize for AlterTableTask {
964    fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
965    where
966        S: serde::Serializer,
967    {
968        let pb = PbAlterTableTask {
969            alter_table: Some(self.alter_table.clone()),
970        };
971        let buf = pb.encode_to_vec();
972        let encoded = general_purpose::STANDARD_NO_PAD.encode(buf);
973        serializer.serialize_str(&encoded)
974    }
975}
976
977impl<'de> Deserialize<'de> for AlterTableTask {
978    fn deserialize<D>(deserializer: D) -> result::Result<Self, D::Error>
979    where
980        D: serde::Deserializer<'de>,
981    {
982        let encoded = String::deserialize(deserializer)?;
983        let buf = general_purpose::STANDARD_NO_PAD
984            .decode(encoded)
985            .map_err(|err| serde::de::Error::custom(err.to_string()))?;
986        let expr: PbAlterTableTask = PbAlterTableTask::decode(&*buf)
987            .map_err(|err| serde::de::Error::custom(err.to_string()))?;
988
989        let expr = AlterTableTask::try_from(expr)
990            .map_err(|err| serde::de::Error::custom(err.to_string()))?;
991
992        Ok(expr)
993    }
994}
995
996#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
997pub struct TruncateTableTask {
998    pub catalog: String,
999    pub schema: String,
1000    pub table: String,
1001    pub table_id: TableId,
1002    pub time_ranges: Vec<(Timestamp, Timestamp)>,
1003}
1004
1005impl TruncateTableTask {
1006    pub fn table_ref(&self) -> TableReference<'_> {
1007        TableReference {
1008            catalog: &self.catalog,
1009            schema: &self.schema,
1010            table: &self.table,
1011        }
1012    }
1013
1014    pub fn table_name(&self) -> TableName {
1015        TableName {
1016            catalog_name: self.catalog.clone(),
1017            schema_name: self.schema.clone(),
1018            table_name: self.table.clone(),
1019        }
1020    }
1021}
1022
1023impl TryFrom<PbTruncateTableTask> for TruncateTableTask {
1024    type Error = error::Error;
1025
1026    fn try_from(pb: PbTruncateTableTask) -> Result<Self> {
1027        let truncate_table = pb.truncate_table.context(error::InvalidProtoMsgSnafu {
1028            err_msg: "expected truncate table",
1029        })?;
1030
1031        Ok(Self {
1032            catalog: truncate_table.catalog_name,
1033            schema: truncate_table.schema_name,
1034            table: truncate_table.table_name,
1035            table_id: truncate_table
1036                .table_id
1037                .context(error::InvalidProtoMsgSnafu {
1038                    err_msg: "expected table_id",
1039                })?
1040                .id,
1041            time_ranges: truncate_table
1042                .time_ranges
1043                .map(from_pb_time_ranges)
1044                .transpose()
1045                .map_err(BoxedError::new)
1046                .context(ExternalSnafu)?
1047                .unwrap_or_default(),
1048        })
1049    }
1050}
1051
1052impl TryFrom<TruncateTableTask> for PbTruncateTableTask {
1053    type Error = error::Error;
1054
1055    fn try_from(task: TruncateTableTask) -> Result<Self> {
1056        Ok(PbTruncateTableTask {
1057            truncate_table: Some(TruncateTableExpr {
1058                catalog_name: task.catalog,
1059                schema_name: task.schema,
1060                table_name: task.table,
1061                table_id: Some(api::v1::TableId { id: task.table_id }),
1062                time_ranges: Some(
1063                    to_pb_time_ranges(&task.time_ranges).context(ConvertTimeRangesSnafu)?,
1064                ),
1065            }),
1066        })
1067    }
1068}
1069
1070#[serde_as]
1071#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
1072pub struct CreateDatabaseTask {
1073    pub catalog: String,
1074    pub schema: String,
1075    pub create_if_not_exists: bool,
1076    #[serde_as(deserialize_as = "DefaultOnNull")]
1077    pub options: HashMap<String, String>,
1078    #[serde(default)]
1079    pub creator: Option<CreatorGrantIntent>,
1080}
1081
1082impl TryFrom<PbCreateDatabaseTask> for CreateDatabaseTask {
1083    type Error = error::Error;
1084
1085    fn try_from(pb: PbCreateDatabaseTask) -> Result<Self> {
1086        let CreateDatabaseExpr {
1087            catalog_name,
1088            schema_name,
1089            create_if_not_exists,
1090            options,
1091        } = pb.create_database.context(error::InvalidProtoMsgSnafu {
1092            err_msg: "expected create database",
1093        })?;
1094
1095        Ok(CreateDatabaseTask {
1096            catalog: catalog_name,
1097            schema: schema_name,
1098            create_if_not_exists,
1099            options,
1100            creator: None,
1101        })
1102    }
1103}
1104
1105impl TryFrom<CreateDatabaseTask> for PbCreateDatabaseTask {
1106    type Error = error::Error;
1107
1108    fn try_from(
1109        CreateDatabaseTask {
1110            catalog,
1111            schema,
1112            create_if_not_exists,
1113            options,
1114            creator: _,
1115        }: CreateDatabaseTask,
1116    ) -> Result<Self> {
1117        Ok(PbCreateDatabaseTask {
1118            create_database: Some(CreateDatabaseExpr {
1119                catalog_name: catalog,
1120                schema_name: schema,
1121                create_if_not_exists,
1122                options,
1123            }),
1124        })
1125    }
1126}
1127
1128#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
1129pub struct DropDatabaseTask {
1130    pub catalog: String,
1131    pub schema: String,
1132    pub drop_if_exists: bool,
1133}
1134
1135impl TryFrom<PbDropDatabaseTask> for DropDatabaseTask {
1136    type Error = error::Error;
1137
1138    fn try_from(pb: PbDropDatabaseTask) -> Result<Self> {
1139        let DropDatabaseExpr {
1140            catalog_name,
1141            schema_name,
1142            drop_if_exists,
1143        } = pb.drop_database.context(error::InvalidProtoMsgSnafu {
1144            err_msg: "expected drop database",
1145        })?;
1146
1147        Ok(DropDatabaseTask {
1148            catalog: catalog_name,
1149            schema: schema_name,
1150            drop_if_exists,
1151        })
1152    }
1153}
1154
1155impl TryFrom<DropDatabaseTask> for PbDropDatabaseTask {
1156    type Error = error::Error;
1157
1158    fn try_from(
1159        DropDatabaseTask {
1160            catalog,
1161            schema,
1162            drop_if_exists,
1163        }: DropDatabaseTask,
1164    ) -> Result<Self> {
1165        Ok(PbDropDatabaseTask {
1166            drop_database: Some(DropDatabaseExpr {
1167                catalog_name: catalog,
1168                schema_name: schema,
1169                drop_if_exists,
1170            }),
1171        })
1172    }
1173}
1174
1175#[derive(Debug, PartialEq, Clone)]
1176pub struct AlterDatabaseTask {
1177    pub alter_expr: AlterDatabaseExpr,
1178}
1179
1180impl TryFrom<AlterDatabaseTask> for PbAlterDatabaseTask {
1181    type Error = error::Error;
1182
1183    fn try_from(task: AlterDatabaseTask) -> Result<Self> {
1184        Ok(PbAlterDatabaseTask {
1185            task: Some(task.alter_expr),
1186        })
1187    }
1188}
1189
1190impl TryFrom<PbAlterDatabaseTask> for AlterDatabaseTask {
1191    type Error = error::Error;
1192
1193    fn try_from(pb: PbAlterDatabaseTask) -> Result<Self> {
1194        let alter_expr = pb.task.context(error::InvalidProtoMsgSnafu {
1195            err_msg: "expected alter database",
1196        })?;
1197
1198        Ok(AlterDatabaseTask { alter_expr })
1199    }
1200}
1201
1202impl TryFrom<PbAlterDatabaseKind> for AlterDatabaseKind {
1203    type Error = error::Error;
1204
1205    fn try_from(pb: PbAlterDatabaseKind) -> Result<Self> {
1206        match pb {
1207            PbAlterDatabaseKind::SetDatabaseOptions(options) => {
1208                Ok(AlterDatabaseKind::SetDatabaseOptions(SetDatabaseOptions(
1209                    options
1210                        .set_database_options
1211                        .into_iter()
1212                        .map(SetDatabaseOption::try_from)
1213                        .collect::<Result<Vec<_>>>()?,
1214                )))
1215            }
1216            PbAlterDatabaseKind::UnsetDatabaseOptions(options) => Ok(
1217                AlterDatabaseKind::UnsetDatabaseOptions(UnsetDatabaseOptions(
1218                    options
1219                        .keys
1220                        .iter()
1221                        .map(|key| UnsetDatabaseOption::try_from(key.as_str()))
1222                        .collect::<Result<Vec<_>>>()?,
1223                )),
1224            ),
1225        }
1226    }
1227}
1228
1229const TTL_KEY: &str = "ttl";
1230
1231impl TryFrom<PbOption> for SetDatabaseOption {
1232    type Error = error::Error;
1233
1234    fn try_from(PbOption { key, value }: PbOption) -> Result<Self> {
1235        let key_lower = key.to_ascii_lowercase();
1236        match key_lower.as_str() {
1237            TTL_KEY => {
1238                let ttl = DatabaseTimeToLive::from_humantime_or_str(&value)
1239                    .map_err(|_| InvalidSetDatabaseOptionSnafu { key, value }.build())?;
1240
1241                Ok(SetDatabaseOption::Ttl(ttl))
1242            }
1243            _ => {
1244                if validate_database_option(&key_lower)
1245                    && validate_database_option_value(&key_lower, Some(&value)).is_ok()
1246                {
1247                    Ok(SetDatabaseOption::Other(key_lower, value))
1248                } else {
1249                    InvalidSetDatabaseOptionSnafu { key, value }.fail()
1250                }
1251            }
1252        }
1253    }
1254}
1255
1256#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
1257pub enum SetDatabaseOption {
1258    Ttl(DatabaseTimeToLive),
1259    Other(String, String),
1260}
1261
1262#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
1263pub enum UnsetDatabaseOption {
1264    Ttl,
1265    Other(String),
1266}
1267
1268impl TryFrom<&str> for UnsetDatabaseOption {
1269    type Error = error::Error;
1270
1271    fn try_from(key: &str) -> Result<Self> {
1272        let key_lower = key.to_ascii_lowercase();
1273        match key_lower.as_str() {
1274            TTL_KEY => Ok(UnsetDatabaseOption::Ttl),
1275            _ => {
1276                if validate_database_option(&key_lower) {
1277                    Ok(UnsetDatabaseOption::Other(key_lower))
1278                } else {
1279                    InvalidUnsetDatabaseOptionSnafu { key }.fail()
1280                }
1281            }
1282        }
1283    }
1284}
1285
1286#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
1287pub struct SetDatabaseOptions(pub Vec<SetDatabaseOption>);
1288
1289#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
1290pub struct UnsetDatabaseOptions(pub Vec<UnsetDatabaseOption>);
1291
1292#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
1293pub enum AlterDatabaseKind {
1294    SetDatabaseOptions(SetDatabaseOptions),
1295    UnsetDatabaseOptions(UnsetDatabaseOptions),
1296}
1297
1298impl AlterDatabaseTask {
1299    pub fn catalog(&self) -> &str {
1300        &self.alter_expr.catalog_name
1301    }
1302
1303    pub fn schema(&self) -> &str {
1304        &self.alter_expr.catalog_name
1305    }
1306}
1307
1308/// Create flow
1309#[derive(Debug, Clone, Serialize, Deserialize)]
1310pub struct CreateFlowTask {
1311    pub catalog_name: String,
1312    pub flow_name: String,
1313    pub source_table_names: Vec<TableName>,
1314    pub sink_table_name: TableName,
1315    pub or_replace: bool,
1316    pub create_if_not_exists: bool,
1317    /// Duration in seconds. Data older than this duration will not be used.
1318    pub expire_after: Option<i64>,
1319    pub eval_interval_secs: Option<i64>,
1320    /// Phase offset of the evaluation schedule within `eval_interval_secs`,
1321    /// in seconds. Must be in `[0, eval_interval_secs)`. `None` means a zero
1322    /// offset (epoch-anchored schedule).
1323    /// Transported through the transient option map (no proto field), see
1324    /// `INTERNAL_EVAL_OFFSET_KEY`.
1325    #[serde(default)]
1326    pub eval_offset_secs: Option<i64>,
1327    pub comment: String,
1328    pub sql: String,
1329    pub flow_options: HashMap<String, String>,
1330    /// Typed schedule configuration resolved during `on_prepare`.
1331    /// Not populated from proto; set by the procedure layer after
1332    /// defaults are resolved.
1333    #[serde(default)]
1334    pub eval_schedule: Option<crate::key::flow::flow_info::FlowScheduleConfig>,
1335}
1336
1337impl TryFrom<PbCreateFlowTask> for CreateFlowTask {
1338    type Error = error::Error;
1339
1340    fn try_from(pb: PbCreateFlowTask) -> Result<Self> {
1341        let CreateFlowExpr {
1342            catalog_name,
1343            flow_name,
1344            source_table_names,
1345            sink_table_name,
1346            or_replace,
1347            create_if_not_exists,
1348            expire_after,
1349            eval_interval,
1350            comment,
1351            sql,
1352            mut flow_options,
1353        } = pb.create_flow.context(error::InvalidProtoMsgSnafu {
1354            err_msg: "expected create_flow",
1355        })?;
1356
1357        // Parse and strip the trusted transient offset key inserted by the
1358        // operator after user option validation. It must never persist in
1359        // user-visible options.
1360        let eval_offset_secs =
1361            match flow_options.remove(crate::ddl::create_flow::INTERNAL_EVAL_OFFSET_KEY) {
1362                Some(value) => Some(value.parse::<i64>().map_err(|_| {
1363                    error::UnexpectedSnafu {
1364                        err_msg: format!(
1365                            "Invalid internal eval offset payload '{value}': expected whole seconds"
1366                        ),
1367                    }
1368                    .build()
1369                })?),
1370                None => None,
1371            };
1372
1373        Ok(CreateFlowTask {
1374            catalog_name,
1375            flow_name,
1376            source_table_names: source_table_names.into_iter().map(Into::into).collect(),
1377            sink_table_name: sink_table_name
1378                .context(error::InvalidProtoMsgSnafu {
1379                    err_msg: "expected sink_table_name",
1380                })?
1381                .into(),
1382            or_replace,
1383            create_if_not_exists,
1384            expire_after: expire_after.map(|e| e.value),
1385            eval_interval_secs: eval_interval.map(|e| e.seconds),
1386            eval_offset_secs,
1387            comment,
1388            sql,
1389            flow_options,
1390            eval_schedule: None,
1391        })
1392    }
1393}
1394
1395impl From<CreateFlowTask> for PbCreateFlowTask {
1396    fn from(
1397        CreateFlowTask {
1398            catalog_name,
1399            flow_name,
1400            source_table_names,
1401            sink_table_name,
1402            or_replace,
1403            create_if_not_exists,
1404            expire_after,
1405            eval_interval_secs: eval_interval,
1406            eval_offset_secs,
1407            comment,
1408            sql,
1409            mut flow_options,
1410            ..
1411        }: CreateFlowTask,
1412    ) -> Self {
1413        // Re-insert the transient offset key so the proto round-trip (e.g. DDL
1414        // task submission between frontend and metasrv) preserves the offset.
1415        if let Some(offset_secs) = eval_offset_secs {
1416            flow_options.insert(
1417                crate::ddl::create_flow::INTERNAL_EVAL_OFFSET_KEY.to_string(),
1418                offset_secs.to_string(),
1419            );
1420        }
1421        PbCreateFlowTask {
1422            create_flow: Some(CreateFlowExpr {
1423                catalog_name,
1424                flow_name,
1425                source_table_names: source_table_names.into_iter().map(Into::into).collect(),
1426                sink_table_name: Some(sink_table_name.into()),
1427                or_replace,
1428                create_if_not_exists,
1429                expire_after: expire_after.map(|value| ExpireAfter { value }),
1430                eval_interval: eval_interval.map(|seconds| EvalInterval { seconds }),
1431                comment,
1432                sql,
1433                flow_options,
1434            }),
1435        }
1436    }
1437}
1438
1439/// Drop flow
1440#[derive(Debug, Clone, Serialize, Deserialize)]
1441pub struct DropFlowTask {
1442    pub catalog_name: String,
1443    pub flow_name: String,
1444    pub flow_id: FlowId,
1445    pub drop_if_exists: bool,
1446}
1447
1448impl TryFrom<PbDropFlowTask> for DropFlowTask {
1449    type Error = error::Error;
1450
1451    fn try_from(pb: PbDropFlowTask) -> Result<Self> {
1452        let DropFlowExpr {
1453            catalog_name,
1454            flow_name,
1455            flow_id,
1456            drop_if_exists,
1457        } = pb.drop_flow.context(error::InvalidProtoMsgSnafu {
1458            err_msg: "expected drop_flow",
1459        })?;
1460        let flow_id = flow_id
1461            .context(error::InvalidProtoMsgSnafu {
1462                err_msg: "expected flow_id",
1463            })?
1464            .id;
1465        Ok(DropFlowTask {
1466            catalog_name,
1467            flow_name,
1468            flow_id,
1469            drop_if_exists,
1470        })
1471    }
1472}
1473
1474impl From<DropFlowTask> for PbDropFlowTask {
1475    fn from(
1476        DropFlowTask {
1477            catalog_name,
1478            flow_name,
1479            flow_id,
1480            drop_if_exists,
1481        }: DropFlowTask,
1482    ) -> Self {
1483        PbDropFlowTask {
1484            drop_flow: Some(DropFlowExpr {
1485                catalog_name,
1486                flow_name,
1487                flow_id: Some(api::v1::FlowId { id: flow_id }),
1488                drop_if_exists,
1489            }),
1490        }
1491    }
1492}
1493
1494/// Represents the ID of the object being commented on (Table or Flow).
1495#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
1496pub enum CommentObjectId {
1497    Table(TableId),
1498    Flow(FlowId),
1499}
1500
1501/// Comment on table, column, or flow
1502#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
1503pub struct CommentOnTask {
1504    pub catalog_name: String,
1505    pub schema_name: String,
1506    pub object_type: CommentObjectType,
1507    pub object_name: String,
1508    /// Column name (only for Column comments)
1509    pub column_name: Option<String>,
1510    /// Object ID (Table or Flow) for validation and cache invalidation
1511    pub object_id: Option<CommentObjectId>,
1512    pub comment: Option<String>,
1513}
1514
1515#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
1516pub enum CommentObjectType {
1517    Table,
1518    Column,
1519    Flow,
1520}
1521
1522impl CommentOnTask {
1523    pub fn table_id(&self) -> Option<TableId> {
1524        match self.object_id.as_ref() {
1525            Some(CommentObjectId::Table(table_id)) => Some(*table_id),
1526            _ => None,
1527        }
1528    }
1529
1530    pub fn flow_id(&self) -> Option<FlowId> {
1531        match self.object_id.as_ref() {
1532            Some(CommentObjectId::Flow(flow_id)) => Some(*flow_id),
1533            _ => None,
1534        }
1535    }
1536
1537    fn set_table_id(&mut self, table_id: TableId) {
1538        self.object_id = Some(CommentObjectId::Table(table_id));
1539    }
1540
1541    fn set_flow_id(&mut self, flow_id: FlowId) {
1542        self.object_id = Some(CommentObjectId::Flow(flow_id));
1543    }
1544
1545    /// Returns the cache identifiers for the object being commented on.
1546    pub fn cache_idents(&self) -> Vec<CacheIdent> {
1547        match self.object_type {
1548            CommentObjectType::Table | CommentObjectType::Column => {
1549                let mut cache_idents = Vec::with_capacity(2);
1550                if let Some(CommentObjectId::Table(table_id)) = self.object_id.as_ref() {
1551                    cache_idents.push(CacheIdent::TableId(*table_id));
1552                }
1553                cache_idents.push(CacheIdent::TableName(TableName {
1554                    catalog_name: self.catalog_name.clone(),
1555                    schema_name: self.schema_name.clone(),
1556                    table_name: self.object_name.clone(),
1557                }));
1558                cache_idents
1559            }
1560            CommentObjectType::Flow => {
1561                let mut cache_idents = Vec::with_capacity(2);
1562                if let Some(CommentObjectId::Flow(flow_id)) = self.object_id.as_ref() {
1563                    cache_idents.push(CacheIdent::FlowId(*flow_id));
1564                }
1565                cache_idents.push(CacheIdent::FlowName(FlowName {
1566                    catalog_name: self.catalog_name.clone(),
1567                    flow_name: self.object_name.clone(),
1568                }));
1569                cache_idents
1570            }
1571        }
1572    }
1573
1574    /// Enriches the `object_id` field of the `CommentOnTask`
1575    /// by looking up the corresponding table or flow ID using the provided managers.
1576    pub async fn enrich_object_id(
1577        &mut self,
1578        table_name_manager: &TableNameManager,
1579        flow_name_manager: &FlowNameManager,
1580    ) -> Result<()> {
1581        match self.object_type {
1582            CommentObjectType::Table | CommentObjectType::Column => {
1583                let table_id = table_name_manager
1584                    .get(TableNameKey::new(
1585                        &self.catalog_name,
1586                        &self.schema_name,
1587                        &self.object_name,
1588                    ))
1589                    .await?
1590                    .with_context(|| error::TableNotFoundSnafu {
1591                        table_name: format_full_table_name(
1592                            &self.catalog_name,
1593                            &self.schema_name,
1594                            &self.object_name,
1595                        ),
1596                    })?
1597                    .table_id();
1598
1599                self.set_table_id(table_id);
1600            }
1601            CommentObjectType::Flow => {
1602                let flow_id = flow_name_manager
1603                    .get(&self.catalog_name, &self.object_name)
1604                    .await?
1605                    .with_context(|| error::FlowNotFoundSnafu {
1606                        flow_name: format_full_flow_name(&self.catalog_name, &self.object_name),
1607                    })?
1608                    .flow_id();
1609
1610                self.set_flow_id(flow_id);
1611            }
1612        }
1613
1614        Ok(())
1615    }
1616}
1617
1618// Proto conversions for CommentObjectType
1619impl From<CommentObjectType> for PbCommentObjectType {
1620    fn from(object_type: CommentObjectType) -> Self {
1621        match object_type {
1622            CommentObjectType::Table => PbCommentObjectType::Table,
1623            CommentObjectType::Column => PbCommentObjectType::Column,
1624            CommentObjectType::Flow => PbCommentObjectType::Flow,
1625        }
1626    }
1627}
1628
1629impl TryFrom<i32> for CommentObjectType {
1630    type Error = error::Error;
1631
1632    fn try_from(value: i32) -> Result<Self> {
1633        match value {
1634            0 => Ok(CommentObjectType::Table),
1635            1 => Ok(CommentObjectType::Column),
1636            2 => Ok(CommentObjectType::Flow),
1637            _ => error::InvalidProtoMsgSnafu {
1638                err_msg: format!(
1639                    "Invalid CommentObjectType value: {}. Valid values are: 0 (Table), 1 (Column), 2 (Flow)",
1640                    value
1641                ),
1642            }
1643            .fail(),
1644        }
1645    }
1646}
1647
1648// Proto conversions for CommentOnTask
1649impl TryFrom<PbCommentOnTask> for CommentOnTask {
1650    type Error = error::Error;
1651
1652    fn try_from(pb: PbCommentOnTask) -> Result<Self> {
1653        let comment_on = pb.comment_on.context(error::InvalidProtoMsgSnafu {
1654            err_msg: "expected comment_on",
1655        })?;
1656
1657        Ok(CommentOnTask {
1658            catalog_name: comment_on.catalog_name,
1659            schema_name: comment_on.schema_name,
1660            object_type: comment_on.object_type.try_into()?,
1661            object_name: comment_on.object_name,
1662            column_name: if comment_on.column_name.is_empty() {
1663                None
1664            } else {
1665                Some(comment_on.column_name)
1666            },
1667            comment: if comment_on.comment.is_empty() {
1668                None
1669            } else {
1670                Some(comment_on.comment)
1671            },
1672            object_id: None,
1673        })
1674    }
1675}
1676
1677impl From<CommentOnTask> for PbCommentOnTask {
1678    fn from(task: CommentOnTask) -> Self {
1679        let pb_object_type: PbCommentObjectType = task.object_type.into();
1680        PbCommentOnTask {
1681            comment_on: Some(CommentOnExpr {
1682                catalog_name: task.catalog_name,
1683                schema_name: task.schema_name,
1684                object_type: pb_object_type as i32,
1685                object_name: task.object_name,
1686                column_name: task.column_name.unwrap_or_default(),
1687                comment: task.comment.unwrap_or_default(),
1688            }),
1689        }
1690    }
1691}
1692
1693#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
1694pub struct QueryContext {
1695    pub current_catalog: String,
1696    pub current_schema: String,
1697    pub timezone: String,
1698    pub extensions: HashMap<String, String>,
1699    pub channel: u8,
1700    /// Maps region id -> snapshot upper bound sequence for that region.
1701    #[serde(default)]
1702    pub snapshot_seqs: HashMap<u64, u64>,
1703    /// Maps region id -> minimal SST sequence allowed for that region.
1704    #[serde(default)]
1705    pub sst_min_sequences: HashMap<u64, u64>,
1706}
1707
1708impl QueryContext {
1709    /// Get the current catalog
1710    pub fn current_catalog(&self) -> &str {
1711        &self.current_catalog
1712    }
1713
1714    /// Get the current schema
1715    pub fn current_schema(&self) -> &str {
1716        &self.current_schema
1717    }
1718
1719    /// Get the timezone
1720    pub fn timezone(&self) -> &str {
1721        &self.timezone
1722    }
1723
1724    /// Get the extensions
1725    pub fn extensions(&self) -> &HashMap<String, String> {
1726        &self.extensions
1727    }
1728
1729    pub(crate) fn pinned_logical_table_ids(&self) -> Result<Option<Vec<TableId>>> {
1730        self.extensions
1731            .get(ALTER_LOGICAL_TABLE_IDS_EXTENSION_KEY)
1732            .map(|ids| {
1733                ids.split(',')
1734                    .map(|id| {
1735                        id.parse::<TableId>().map_err(|error| {
1736                            error::AlterLogicalTablesInvalidArgumentsSnafu {
1737                                err_msg: format!("Invalid pinned logical table ID: {error}"),
1738                            }
1739                            .build()
1740                        })
1741                    })
1742                    .collect()
1743            })
1744            .transpose()
1745    }
1746
1747    /// Get the channel
1748    pub fn channel(&self) -> u8 {
1749        self.channel
1750    }
1751
1752    /// Returns the protocol derived from the typed query channel.
1753    pub fn protocol(&self) -> Option<String> {
1754        let channel = Channel::from(u32::from(self.channel));
1755        (channel != Channel::Unknown).then(|| channel.as_ref().to_string())
1756    }
1757
1758    pub fn snapshot_seqs(&self) -> &HashMap<u64, u64> {
1759        &self.snapshot_seqs
1760    }
1761
1762    pub fn sst_min_sequences(&self) -> &HashMap<u64, u64> {
1763        &self.sst_min_sequences
1764    }
1765}
1766
1767/// Lightweight query context for flow operations containing only essential fields.
1768/// This is a subset of QueryContext that includes only the fields actually needed
1769/// for flow creation and execution.
1770#[derive(Debug, Clone, Serialize, PartialEq)]
1771pub struct FlowQueryContext {
1772    /// Current catalog name used for flow metadata and execution.
1773    pub catalog: String,
1774    /// Current schema name used for table resolution during flow execution.
1775    pub schema: String,
1776    /// Timezone used for timestamp evaluation in the flow.
1777    pub timezone: String,
1778    /// Query extensions carried into flow execution.
1779    #[serde(default)]
1780    pub extensions: HashMap<String, String>,
1781    /// Request channel propagated from the original query context.
1782    #[serde(default)]
1783    pub channel: u8,
1784    /// Per-region snapshot upper bounds bound during query planning/execution.
1785    #[serde(default)]
1786    pub snapshot_seqs: HashMap<u64, u64>,
1787    /// Per-region lower SST scan bounds carried with the flow context.
1788    #[serde(default)]
1789    pub sst_min_sequences: HashMap<u64, u64>,
1790}
1791
1792impl<'de> Deserialize<'de> for FlowQueryContext {
1793    fn deserialize<D>(deserializer: D) -> result::Result<Self, D::Error>
1794    where
1795        D: serde::Deserializer<'de>,
1796    {
1797        // Support both QueryContext format and FlowQueryContext format
1798        #[derive(Deserialize)]
1799        #[serde(untagged)]
1800        enum ContextCompat {
1801            Flow(FlowQueryContextHelper),
1802            Full(QueryContext),
1803        }
1804
1805        #[derive(Deserialize)]
1806        struct FlowQueryContextHelper {
1807            catalog: String,
1808            schema: String,
1809            timezone: String,
1810            #[serde(default)]
1811            extensions: HashMap<String, String>,
1812            #[serde(default)]
1813            channel: u8,
1814            #[serde(default)]
1815            snapshot_seqs: HashMap<u64, u64>,
1816            #[serde(default)]
1817            sst_min_sequences: HashMap<u64, u64>,
1818        }
1819
1820        match ContextCompat::deserialize(deserializer)? {
1821            ContextCompat::Flow(helper) => Ok(FlowQueryContext {
1822                catalog: helper.catalog,
1823                schema: helper.schema,
1824                timezone: helper.timezone,
1825                extensions: helper.extensions,
1826                channel: helper.channel,
1827                snapshot_seqs: helper.snapshot_seqs,
1828                sst_min_sequences: helper.sst_min_sequences,
1829            }),
1830            ContextCompat::Full(full_ctx) => Ok(full_ctx.into()),
1831        }
1832    }
1833}
1834
1835impl From<PbQueryContext> for QueryContext {
1836    fn from(pb_ctx: PbQueryContext) -> Self {
1837        let (snapshot_seqs, sst_min_sequences) = pb_ctx
1838            .snapshot_seqs
1839            .map(|seqs| (seqs.snapshot_seqs, seqs.sst_min_sequences))
1840            .unwrap_or_default();
1841
1842        Self {
1843            current_catalog: pb_ctx.current_catalog,
1844            current_schema: pb_ctx.current_schema,
1845            timezone: pb_ctx.timezone,
1846            extensions: pb_ctx.extensions,
1847            channel: pb_ctx.channel as u8,
1848            snapshot_seqs,
1849            sst_min_sequences,
1850        }
1851    }
1852}
1853
1854impl From<QueryContext> for PbQueryContext {
1855    fn from(
1856        QueryContext {
1857            current_catalog,
1858            current_schema,
1859            timezone,
1860            extensions,
1861            channel,
1862            snapshot_seqs,
1863            sst_min_sequences,
1864        }: QueryContext,
1865    ) -> Self {
1866        PbQueryContext {
1867            current_catalog,
1868            current_schema,
1869            timezone,
1870            extensions,
1871            channel: channel as u32,
1872            snapshot_seqs: (!snapshot_seqs.is_empty() || !sst_min_sequences.is_empty()).then_some(
1873                api::v1::SnapshotSequences {
1874                    snapshot_seqs,
1875                    sst_min_sequences,
1876                },
1877            ),
1878            explain: None,
1879        }
1880    }
1881}
1882
1883impl From<QueryContext> for FlowQueryContext {
1884    fn from(ctx: QueryContext) -> Self {
1885        Self {
1886            catalog: ctx.current_catalog,
1887            schema: ctx.current_schema,
1888            timezone: ctx.timezone,
1889            extensions: ctx.extensions,
1890            channel: ctx.channel,
1891            snapshot_seqs: ctx.snapshot_seqs,
1892            sst_min_sequences: ctx.sst_min_sequences,
1893        }
1894    }
1895}
1896
1897impl From<FlowQueryContext> for QueryContext {
1898    fn from(flow_ctx: FlowQueryContext) -> Self {
1899        Self {
1900            current_catalog: flow_ctx.catalog,
1901            current_schema: flow_ctx.schema,
1902            timezone: flow_ctx.timezone,
1903            extensions: flow_ctx.extensions,
1904            channel: flow_ctx.channel,
1905            snapshot_seqs: flow_ctx.snapshot_seqs,
1906            sst_min_sequences: flow_ctx.sst_min_sequences,
1907        }
1908    }
1909}
1910
1911impl From<FlowQueryContext> for PbQueryContext {
1912    fn from(flow_ctx: FlowQueryContext) -> Self {
1913        let query_ctx: QueryContext = flow_ctx.into();
1914        query_ctx.into()
1915    }
1916}
1917
1918#[cfg(test)]
1919mod tests {
1920    use std::sync::Arc;
1921
1922    use api::v1::{AlterTableExpr, ColumnDef, CreateTableExpr, SemanticType};
1923    use datatypes::schema::{ColumnSchema, Schema, SchemaBuilder};
1924    use store_api::metric_engine_consts::METRIC_ENGINE_NAME;
1925    use store_api::storage::ConcreteDataType;
1926    use table::metadata::{TableInfo, TableMeta, TableType};
1927    use table::test_util::table_info::test_table_info;
1928
1929    use super::{AlterTableTask, CreateTableTask, *};
1930
1931    #[test]
1932    fn test_pinned_logical_table_ids_wire_compatibility() {
1933        let legacy = include_bytes!(
1934            "../../../../../tests/compatibility/wire/read_preference/legacy_query_context.pb"
1935        );
1936        let mut context = QueryContext::from(PbQueryContext::decode(legacy.as_slice()).unwrap());
1937        assert_eq!(context.pinned_logical_table_ids().unwrap(), None);
1938        context.extensions.insert(
1939            "__greptime_alter_logical_tables.ids".into(),
1940            "1025,1024".into(),
1941        );
1942        let wire = PbQueryContext::from(context.clone()).encode_to_vec();
1943        let decoded = QueryContext::from(PbQueryContext::decode(wire.as_slice()).unwrap());
1944        assert_eq!(decoded, context);
1945        assert_eq!(
1946            decoded.pinned_logical_table_ids().unwrap(),
1947            Some(vec![1025, 1024])
1948        );
1949        for invalid in ["", "1024,", "1024,no", "-1", "4294967296"] {
1950            context
1951                .extensions
1952                .insert(ALTER_LOGICAL_TABLE_IDS_EXTENSION_KEY.into(), invalid.into());
1953            assert!(context.pinned_logical_table_ids().is_err(), "{invalid}");
1954        }
1955    }
1956
1957    #[test]
1958    fn test_ddl_timeout_secs() {
1959        assert_eq!(ddl_timeout_secs(Duration::ZERO), 0);
1960        assert_eq!(ddl_timeout_secs(Duration::from_nanos(1)), 1);
1961        assert_eq!(ddl_timeout_secs(Duration::from_secs(1)), 1);
1962        assert_eq!(ddl_timeout_secs(Duration::from_millis(1500)), 2);
1963        assert_eq!(
1964            ddl_timeout_secs(Duration::from_secs(u32::MAX as u64 + 1)),
1965            u32::MAX
1966        );
1967    }
1968
1969    #[test]
1970    fn test_alter_database_rejects_invalid_trigger_values() {
1971        let overflow = format!("{}0", usize::MAX);
1972        for key in [
1973            "compaction.twcs.trigger_file_num",
1974            "compaction.twcs.active_window.trigger_file_num",
1975            "compaction.twcs.inactive_window.trigger_file_num",
1976            "compaction.twcs.active_window.l1_merge_trigger",
1977            "compaction.twcs.inactive_window.l1_merge_trigger",
1978        ] {
1979            for invalid in ["invalid", "-1", overflow.as_str()] {
1980                let kind = PbAlterDatabaseKind::SetDatabaseOptions(api::v1::SetDatabaseOptions {
1981                    set_database_options: vec![PbOption {
1982                        key: key.to_string(),
1983                        value: invalid.to_string(),
1984                    }],
1985                });
1986                let err = AlterDatabaseKind::try_from(kind).unwrap_err();
1987                assert!(
1988                    matches!(err, error::Error::InvalidSetDatabaseOption { .. }),
1989                    "{key}: {invalid}"
1990                );
1991            }
1992            for boundary in ["0", "1", "2"] {
1993                let option = PbOption {
1994                    key: key.to_string(),
1995                    value: boundary.to_string(),
1996                };
1997                assert_eq!(
1998                    SetDatabaseOption::try_from(option).is_ok(),
1999                    !key.ends_with("l1_merge_trigger") || boundary == "2",
2000                    "{key}: {boundary}"
2001                );
2002            }
2003        }
2004    }
2005
2006    #[test]
2007    fn test_basic_ser_de_create_table_task() {
2008        let schema = SchemaBuilder::default().build().unwrap();
2009        let table_info = test_table_info(1025, "foo", "bar", "baz", Arc::new(schema));
2010        let task = CreateTableTask::new(CreateTableExpr::default(), Vec::new(), table_info);
2011
2012        let output = serde_json::to_vec(&task).unwrap();
2013
2014        let de = serde_json::from_slice(&output).unwrap();
2015        assert_eq!(task, de);
2016    }
2017
2018    #[test]
2019    fn test_create_table_task_debug_redacts_connection() {
2020        let schema = SchemaBuilder::default().build().unwrap();
2021        let table_info = test_table_info(1025, "foo", "bar", "baz", Arc::new(schema));
2022        let mut create_table = CreateTableExpr::default();
2023        create_table.table_options.insert(
2024            table::requests::RELATIONAL_META_KEY.to_string(),
2025            "{\"connection\":\"mysql://user:secret@127.0.0.1:3306/db\",\"remote_table\":\"sample\"}"
2026                .to_string(),
2027        );
2028        let task = CreateTableTask::new(create_table, Vec::new(), table_info);
2029        let debug = format!("{task:?}");
2030        assert!(!debug.contains("secret"));
2031        assert!(debug.contains("mysql://user:******@127.0.0.1:3306/db"));
2032    }
2033
2034    #[test]
2035    fn test_create_table_task_debug_redacts_credential() {
2036        let schema = SchemaBuilder::default().build().unwrap();
2037        let table_info = test_table_info(1026, "foo", "bar", "baz", Arc::new(schema));
2038        let mut create_table = CreateTableExpr::default();
2039        create_table
2040            .table_options
2041            .insert("secret_access_key".to_string(), "my-secret".to_string());
2042        create_table
2043            .table_options
2044            .insert("session_token".to_string(), "my-token".to_string());
2045        let task = CreateTableTask::new(create_table, Vec::new(), table_info);
2046        let debug = format!("{task:?}");
2047        assert!(!debug.contains("my-secret"));
2048        assert!(!debug.contains("my-token"));
2049        assert!(debug.contains("******"));
2050    }
2051
2052    #[test]
2053    fn test_basic_ser_de_alter_table_task() {
2054        let task = AlterTableTask {
2055            alter_table: AlterTableExpr::default(),
2056        };
2057
2058        let output = serde_json::to_vec(&task).unwrap();
2059
2060        let de = serde_json::from_slice(&output).unwrap();
2061        assert_eq!(task, de);
2062    }
2063
2064    #[test]
2065    fn test_undrop_table_task_pb_roundtrip() {
2066        let expected = UndropTableTask { table_id: 1024 };
2067        let request = SubmitDdlTaskRequest::new(DdlTask::UndropTable(expected.clone()));
2068
2069        let pb = PbDdlTaskRequest::try_from(request).unwrap();
2070        let pb_task = pb.task.unwrap();
2071        let de = DdlTask::try_from(pb_task).unwrap();
2072
2073        assert!(matches!(de, DdlTask::UndropTable(task) if task == expected));
2074    }
2075
2076    #[test]
2077    fn test_purge_dropped_table_task_pb_roundtrip() {
2078        let expected = PurgeDroppedTableTask { table_id: 1024 };
2079        let request = SubmitDdlTaskRequest::new(DdlTask::PurgeDroppedTable(expected.clone()));
2080
2081        let pb = PbDdlTaskRequest::try_from(request).unwrap();
2082        let pb_task = pb.task.unwrap();
2083        let de = DdlTask::try_from(pb_task).unwrap();
2084
2085        assert!(matches!(de, DdlTask::PurgeDroppedTable(task) if task == expected));
2086    }
2087
2088    #[test]
2089    fn test_undrop_table_task_json_roundtrip() {
2090        let task = UndropTableTask { table_id: 1024 };
2091
2092        let output = serde_json::to_vec(&task).unwrap();
2093
2094        let de = serde_json::from_slice(&output).unwrap();
2095        assert_eq!(task, de);
2096    }
2097
2098    #[test]
2099    fn test_purge_dropped_table_task_json_roundtrip() {
2100        let task = PurgeDroppedTableTask { table_id: 1024 };
2101
2102        let output = serde_json::to_vec(&task).unwrap();
2103
2104        let de = serde_json::from_slice(&output).unwrap();
2105        assert_eq!(task, de);
2106    }
2107
2108    #[test]
2109    fn test_sort_columns() {
2110        // construct RawSchema
2111        let schema = Arc::new(Schema::new(vec![
2112            ColumnSchema::new(
2113                "column3".to_string(),
2114                ConcreteDataType::string_datatype(),
2115                true,
2116            ),
2117            ColumnSchema::new(
2118                "column1".to_string(),
2119                ConcreteDataType::timestamp_millisecond_datatype(),
2120                false,
2121            )
2122            .with_time_index(true),
2123            ColumnSchema::new(
2124                "column2".to_string(),
2125                ConcreteDataType::float64_datatype(),
2126                true,
2127            ),
2128        ]));
2129
2130        // construct RawTableMeta
2131        let meta = TableMeta {
2132            schema,
2133            primary_key_indices: vec![0],
2134            value_indices: vec![2],
2135            engine: METRIC_ENGINE_NAME.to_string(),
2136            next_column_id: 0,
2137            options: Default::default(),
2138            created_on: Default::default(),
2139            updated_on: Default::default(),
2140            partition_key_indices: Default::default(),
2141            column_ids: Default::default(),
2142        };
2143
2144        // construct TableInfo
2145        let raw_table_info = TableInfo {
2146            ident: Default::default(),
2147            meta,
2148            name: Default::default(),
2149            desc: Default::default(),
2150            catalog_name: Default::default(),
2151            schema_name: Default::default(),
2152            table_type: TableType::Base,
2153        };
2154
2155        // construct create table expr
2156        let create_table_expr = CreateTableExpr {
2157            column_defs: vec![
2158                ColumnDef {
2159                    name: "column3".to_string(),
2160                    semantic_type: SemanticType::Tag as i32,
2161                    ..Default::default()
2162                },
2163                ColumnDef {
2164                    name: "column1".to_string(),
2165                    semantic_type: SemanticType::Timestamp as i32,
2166                    ..Default::default()
2167                },
2168                ColumnDef {
2169                    name: "column2".to_string(),
2170                    semantic_type: SemanticType::Field as i32,
2171                    ..Default::default()
2172                },
2173            ],
2174            primary_keys: vec!["column3".to_string()],
2175            ..Default::default()
2176        };
2177
2178        let mut create_table_task =
2179            CreateTableTask::new(create_table_expr, Vec::new(), raw_table_info);
2180
2181        // Call the sort_columns method
2182        create_table_task.sort_columns();
2183
2184        // Assert that the columns are sorted correctly
2185        assert_eq!(
2186            create_table_task.create_table.column_defs[0].name,
2187            "column1".to_string()
2188        );
2189        assert_eq!(
2190            create_table_task.create_table.column_defs[1].name,
2191            "column2".to_string()
2192        );
2193        assert_eq!(
2194            create_table_task.create_table.column_defs[2].name,
2195            "column3".to_string()
2196        );
2197
2198        // Assert that the table_info is updated correctly
2199        assert_eq!(
2200            create_table_task.table_info.meta.schema.timestamp_index(),
2201            Some(0)
2202        );
2203        assert_eq!(
2204            create_table_task.table_info.meta.primary_key_indices,
2205            vec![2]
2206        );
2207        assert_eq!(create_table_task.table_info.meta.value_indices, vec![0, 1]);
2208    }
2209
2210    #[test]
2211    fn test_flow_query_context_conversion_from_query_context() {
2212        use std::collections::HashMap;
2213        let mut extensions = HashMap::new();
2214        extensions.insert("key1".to_string(), "value1".to_string());
2215        extensions.insert("key2".to_string(), "value2".to_string());
2216
2217        let query_ctx = QueryContext {
2218            current_catalog: "test_catalog".to_string(),
2219            current_schema: "test_schema".to_string(),
2220            timezone: "UTC".to_string(),
2221            extensions,
2222            channel: 5,
2223            snapshot_seqs: HashMap::from([(10, 100)]),
2224            sst_min_sequences: HashMap::from([(10, 90)]),
2225        };
2226
2227        let flow_ctx: FlowQueryContext = query_ctx.into();
2228
2229        assert_eq!(flow_ctx.catalog, "test_catalog");
2230        assert_eq!(flow_ctx.schema, "test_schema");
2231        assert_eq!(flow_ctx.timezone, "UTC");
2232        assert_eq!(flow_ctx.channel, 5);
2233        assert_eq!(flow_ctx.snapshot_seqs, HashMap::from([(10, 100)]));
2234        assert_eq!(flow_ctx.sst_min_sequences, HashMap::from([(10, 90)]));
2235    }
2236
2237    #[test]
2238    fn test_flow_query_context_conversion_to_query_context() {
2239        let flow_ctx = FlowQueryContext {
2240            catalog: "prod_catalog".to_string(),
2241            schema: "public".to_string(),
2242            timezone: "America/New_York".to_string(),
2243            extensions: HashMap::from([("k".to_string(), "v".to_string())]),
2244            channel: 7,
2245            snapshot_seqs: HashMap::from([(11, 111)]),
2246            sst_min_sequences: HashMap::from([(11, 101)]),
2247        };
2248
2249        let query_ctx: QueryContext = flow_ctx.clone().into();
2250
2251        assert_eq!(query_ctx.current_catalog, "prod_catalog");
2252        assert_eq!(query_ctx.current_schema, "public");
2253        assert_eq!(query_ctx.timezone, "America/New_York");
2254        assert_eq!(
2255            query_ctx.extensions,
2256            HashMap::from([("k".to_string(), "v".to_string())])
2257        );
2258        assert_eq!(query_ctx.channel, 7);
2259        assert_eq!(query_ctx.snapshot_seqs, HashMap::from([(11, 111)]));
2260        assert_eq!(query_ctx.sst_min_sequences, HashMap::from([(11, 101)]));
2261
2262        // Test roundtrip conversion
2263        let flow_ctx_roundtrip: FlowQueryContext = query_ctx.into();
2264        assert_eq!(flow_ctx, flow_ctx_roundtrip);
2265    }
2266
2267    #[test]
2268    fn test_flow_query_context_serialization() {
2269        let flow_ctx = FlowQueryContext {
2270            catalog: "test_catalog".to_string(),
2271            schema: "test_schema".to_string(),
2272            timezone: "UTC".to_string(),
2273            extensions: HashMap::new(),
2274            channel: 0,
2275            snapshot_seqs: HashMap::new(),
2276            sst_min_sequences: HashMap::new(),
2277        };
2278
2279        let serialized = serde_json::to_string(&flow_ctx).unwrap();
2280        let deserialized: FlowQueryContext = serde_json::from_str(&serialized).unwrap();
2281
2282        assert_eq!(flow_ctx, deserialized);
2283
2284        // Verify JSON structure
2285        let json_value: serde_json::Value = serde_json::from_str(&serialized).unwrap();
2286        assert_eq!(json_value["catalog"], "test_catalog");
2287        assert_eq!(json_value["schema"], "test_schema");
2288        assert_eq!(json_value["timezone"], "UTC");
2289    }
2290
2291    #[test]
2292    fn test_flow_query_context_conversion_to_pb() {
2293        let flow_ctx = FlowQueryContext {
2294            catalog: "pb_catalog".to_string(),
2295            schema: "pb_schema".to_string(),
2296            timezone: "Asia/Tokyo".to_string(),
2297            extensions: HashMap::from([("x".to_string(), "y".to_string())]),
2298            channel: 6,
2299            snapshot_seqs: HashMap::from([(3, 30)]),
2300            sst_min_sequences: HashMap::from([(3, 21)]),
2301        };
2302
2303        let pb_ctx: PbQueryContext = flow_ctx.into();
2304
2305        assert_eq!(pb_ctx.current_catalog, "pb_catalog");
2306        assert_eq!(pb_ctx.current_schema, "pb_schema");
2307        assert_eq!(pb_ctx.timezone, "Asia/Tokyo");
2308        assert_eq!(
2309            pb_ctx.extensions,
2310            HashMap::from([("x".to_string(), "y".to_string())])
2311        );
2312        assert_eq!(pb_ctx.channel, 6);
2313        assert_eq!(
2314            pb_ctx.snapshot_seqs,
2315            Some(api::v1::SnapshotSequences {
2316                snapshot_seqs: HashMap::from([(3, 30)]),
2317                sst_min_sequences: HashMap::from([(3, 21)]),
2318            })
2319        );
2320        assert!(pb_ctx.explain.is_none());
2321    }
2322
2323    #[test]
2324    fn test_pb_query_context_roundtrip_with_snapshot_sequences() {
2325        let pb = PbQueryContext {
2326            current_catalog: "c1".to_string(),
2327            current_schema: "s1".to_string(),
2328            timezone: "UTC".to_string(),
2329            extensions: HashMap::from([("flow.return_region_seq".to_string(), "true".to_string())]),
2330            channel: 3,
2331            snapshot_seqs: Some(api::v1::SnapshotSequences {
2332                snapshot_seqs: HashMap::from([(1, 100)]),
2333                sst_min_sequences: HashMap::from([(1, 90)]),
2334            }),
2335            explain: None,
2336        };
2337
2338        let query_ctx: QueryContext = pb.clone().into();
2339        let pb_roundtrip: PbQueryContext = query_ctx.into();
2340
2341        assert_eq!(pb_roundtrip.current_catalog, pb.current_catalog);
2342        assert_eq!(pb_roundtrip.current_schema, pb.current_schema);
2343        assert_eq!(pb_roundtrip.timezone, pb.timezone);
2344        assert_eq!(pb_roundtrip.extensions, pb.extensions);
2345        assert_eq!(pb_roundtrip.channel, pb.channel);
2346        assert_eq!(pb_roundtrip.snapshot_seqs, pb.snapshot_seqs);
2347    }
2348
2349    #[test]
2350    fn test_trigger_reason_deserializes_unknown_value() {
2351        let reason: TriggerReason = serde_json::from_str("\"future_reason\"").unwrap();
2352        assert_eq!(TriggerReason::Unknown, reason);
2353    }
2354}