1#![allow(unfulfilled_lint_expectations)]
16#![allow(clippy::doc_overindented_list_items)]
17#![expect(clippy::doc_markdown)]
19#![expect(clippy::upper_case_acronyms)]
20#![expect(clippy::needless_lifetimes)]
21#![expect(clippy::disallowed_methods)]
23#![expect(clippy::enum_variant_names)]
24#![expect(clippy::module_inception)]
25#![expect(clippy::large_enum_variant)]
27
28use std::str::FromStr;
29
30use event_recovery::RecoveryEvent;
31use plan_common::AdditionalColumn;
32pub use prost::Message;
33use risingwave_error::tonic::ToTonicStatus;
34use thiserror::Error;
35
36use crate::common::WorkerType;
37use crate::meta::event_log::event_recovery;
38use crate::stream_plan::PbStreamScanType;
39
40#[rustfmt::skip]
41#[cfg_attr(madsim, path = "sim/catalog.rs")]
42pub mod catalog;
43#[rustfmt::skip]
44#[cfg_attr(madsim, path = "sim/common.rs")]
45pub mod common;
46#[rustfmt::skip]
47#[cfg_attr(madsim, path = "sim/compute.rs")]
48pub mod compute;
49#[rustfmt::skip]
50#[cfg_attr(madsim, path = "sim/cloud_service.rs")]
51pub mod cloud_service;
52#[rustfmt::skip]
53#[cfg_attr(madsim, path = "sim/data.rs")]
54pub mod data;
55#[rustfmt::skip]
56#[cfg_attr(madsim, path = "sim/ddl_service.rs")]
57pub mod ddl_service;
58#[rustfmt::skip]
59#[cfg_attr(madsim, path = "sim/expr.rs")]
60pub mod expr;
61#[rustfmt::skip]
62#[cfg_attr(madsim, path = "sim/meta.rs")]
63pub mod meta;
64#[rustfmt::skip]
65#[cfg_attr(madsim, path = "sim/plan_common.rs")]
66pub mod plan_common;
67#[rustfmt::skip]
68#[cfg_attr(madsim, path = "sim/batch_plan.rs")]
69pub mod batch_plan;
70#[rustfmt::skip]
71#[cfg_attr(madsim, path = "sim/task_service.rs")]
72pub mod task_service;
73#[rustfmt::skip]
74#[cfg_attr(madsim, path = "sim/connector_service.rs")]
75pub mod connector_service;
76#[rustfmt::skip]
77#[cfg_attr(madsim, path = "sim/stream_plan.rs")]
78pub mod stream_plan;
79#[rustfmt::skip]
80#[cfg_attr(madsim, path = "sim/stream_service.rs")]
81pub mod stream_service;
82#[rustfmt::skip]
83#[cfg_attr(madsim, path = "sim/hummock.rs")]
84pub mod hummock;
85#[rustfmt::skip]
86#[cfg_attr(madsim, path = "sim/compactor.rs")]
87pub mod compactor;
88#[rustfmt::skip]
89#[cfg_attr(madsim, path = "sim/user.rs")]
90pub mod user;
91#[rustfmt::skip]
92#[cfg_attr(madsim, path = "sim/source.rs")]
93pub mod source;
94#[rustfmt::skip]
95#[cfg_attr(madsim, path = "sim/monitor_service.rs")]
96pub mod monitor_service;
97#[rustfmt::skip]
98#[cfg_attr(madsim, path = "sim/backup_service.rs")]
99pub mod backup_service;
100#[rustfmt::skip]
101#[cfg_attr(madsim, path = "sim/serverless_backfill_controller.rs")]
102pub mod serverless_backfill_controller;
103#[rustfmt::skip]
104#[cfg_attr(madsim, path = "sim/frontend_service.rs")]
105pub mod frontend_service;
106#[rustfmt::skip]
107#[cfg_attr(madsim, path = "sim/java_binding.rs")]
108pub mod java_binding;
109#[rustfmt::skip]
110#[cfg_attr(madsim, path = "sim/health.rs")]
111pub mod health;
112#[rustfmt::skip]
113#[path = "sim/telemetry.rs"]
114pub mod telemetry;
115#[rustfmt::skip]
116#[cfg_attr(madsim, path = "sim/iceberg_compaction.rs")]
117pub mod iceberg_compaction;
118
119#[rustfmt::skip]
120#[path = "sim/secret.rs"]
121pub mod secret;
122#[rustfmt::skip]
123#[path = "connector_service.serde.rs"]
124pub mod connector_service_serde;
125#[rustfmt::skip]
126#[path = "catalog.serde.rs"]
127pub mod catalog_serde;
128#[rustfmt::skip]
129#[path = "common.serde.rs"]
130pub mod common_serde;
131#[rustfmt::skip]
132#[path = "compute.serde.rs"]
133pub mod compute_serde;
134#[rustfmt::skip]
135#[path = "cloud_service.serde.rs"]
136pub mod cloud_service_serde;
137#[rustfmt::skip]
138#[path = "data.serde.rs"]
139pub mod data_serde;
140#[rustfmt::skip]
141#[path = "ddl_service.serde.rs"]
142pub mod ddl_service_serde;
143#[rustfmt::skip]
144#[path = "expr.serde.rs"]
145pub mod expr_serde;
146#[rustfmt::skip]
147#[path = "meta.serde.rs"]
148pub mod meta_serde;
149#[rustfmt::skip]
150#[path = "plan_common.serde.rs"]
151pub mod plan_common_serde;
152#[rustfmt::skip]
153#[path = "batch_plan.serde.rs"]
154pub mod batch_plan_serde;
155#[rustfmt::skip]
156#[path = "task_service.serde.rs"]
157pub mod task_service_serde;
158#[rustfmt::skip]
159#[path = "stream_plan.serde.rs"]
160pub mod stream_plan_serde;
161#[rustfmt::skip]
162#[path = "stream_service.serde.rs"]
163pub mod stream_service_serde;
164#[rustfmt::skip]
165#[path = "hummock.serde.rs"]
166pub mod hummock_serde;
167#[rustfmt::skip]
168#[path = "compactor.serde.rs"]
169pub mod compactor_serde;
170#[rustfmt::skip]
171#[path = "user.serde.rs"]
172pub mod user_serde;
173#[rustfmt::skip]
174#[path = "source.serde.rs"]
175pub mod source_serde;
176#[rustfmt::skip]
177#[path = "monitor_service.serde.rs"]
178pub mod monitor_service_serde;
179#[rustfmt::skip]
180#[path = "backup_service.serde.rs"]
181pub mod backup_service_serde;
182#[rustfmt::skip]
183#[path = "java_binding.serde.rs"]
184pub mod java_binding_serde;
185#[rustfmt::skip]
186#[path = "telemetry.serde.rs"]
187pub mod telemetry_serde;
188
189#[rustfmt::skip]
190#[path = "secret.serde.rs"]
191pub mod secret_serde;
192#[rustfmt::skip]
193#[path = "serverless_backfill_controller.serde.rs"]
194pub mod serverless_backfill_controller_serde;
195
196#[derive(Clone, PartialEq, Eq, Debug, Error)]
197#[error("field `{0}` not found")]
198pub struct PbFieldNotFound(pub &'static str);
199
200impl From<PbFieldNotFound> for tonic::Status {
201 fn from(e: PbFieldNotFound) -> Self {
202 e.to_status_unnamed(tonic::Code::Internal)
203 }
204}
205
206impl FromStr for crate::expr::table_function::PbType {
207 type Err = ();
208
209 fn from_str(s: &str) -> Result<Self, Self::Err> {
210 Self::from_str_name(&s.to_uppercase()).ok_or(())
211 }
212}
213
214impl FromStr for crate::expr::agg_call::PbKind {
215 type Err = ();
216
217 fn from_str(s: &str) -> Result<Self, Self::Err> {
218 Self::from_str_name(&s.to_uppercase()).ok_or(())
219 }
220}
221
222impl stream_plan::MaterializeNode {
223 pub fn dist_key_indices(&self) -> Vec<u32> {
224 self.get_table()
225 .unwrap()
226 .distribution_key
227 .iter()
228 .map(|i| *i as u32)
229 .collect()
230 }
231
232 pub fn column_ids(&self) -> Vec<i32> {
233 self.get_table()
234 .unwrap()
235 .columns
236 .iter()
237 .map(|c| c.get_column_desc().unwrap().column_id)
238 .collect()
239 }
240}
241
242impl stream_plan::SourceBackfillNode {
243 pub fn column_ids(&self) -> Vec<i32> {
244 self.columns
245 .iter()
246 .map(|c| c.column_desc.as_ref().unwrap().column_id)
247 .collect()
248 }
249}
250
251impl common::WorkerNode {
253 pub fn compute_node_parallelism(&self) -> usize {
254 assert_eq!(self.r#type(), WorkerType::ComputeNode);
255 self.property
256 .as_ref()
257 .expect("property should be exist")
258 .parallelism as usize
259 }
260
261 pub fn parallelism(&self) -> Option<usize> {
262 if WorkerType::ComputeNode == self.r#type() {
263 Some(self.compute_node_parallelism())
264 } else {
265 None
266 }
267 }
268
269 pub fn resource_group(&self) -> Option<String> {
270 self.property
271 .as_ref()
272 .and_then(|p| p.resource_group.clone())
273 }
274}
275
276impl stream_plan::SourceNode {
277 pub fn column_ids(&self) -> Option<Vec<i32>> {
278 Some(
279 self.source_inner
280 .as_ref()?
281 .columns
282 .iter()
283 .map(|c| c.get_column_desc().unwrap().column_id)
284 .collect(),
285 )
286 }
287}
288
289impl meta::table_fragments::ActorStatus {
290 pub fn worker_id(&self) -> u32 {
291 self.location
292 .as_ref()
293 .expect("actor location should be exist")
294 .worker_node_id
295 }
296}
297
298impl common::WorkerNode {
299 pub fn is_streaming_schedulable(&self) -> bool {
300 let property = self.property.as_ref();
301 property.is_some_and(|p| p.is_streaming) && !property.is_some_and(|p| p.is_unschedulable)
302 }
303}
304
305impl common::ActorLocation {
306 pub fn from_worker(worker_node_id: u32) -> Option<Self> {
307 Some(Self { worker_node_id })
308 }
309}
310
311impl meta::event_log::EventRecovery {
312 pub fn event_type(&self) -> &str {
313 match self.recovery_event.as_ref() {
314 Some(RecoveryEvent::DatabaseStart(_)) => "DATABASE_RECOVERY_START",
315 Some(RecoveryEvent::DatabaseSuccess(_)) => "DATABASE_RECOVERY_SUCCESS",
316 Some(RecoveryEvent::DatabaseFailure(_)) => "DATABASE_RECOVERY_FAILURE",
317 Some(RecoveryEvent::GlobalStart(_)) => "GLOBAL_RECOVERY_START",
318 Some(RecoveryEvent::GlobalSuccess(_)) => "GLOBAL_RECOVERY_SUCCESS",
319 Some(RecoveryEvent::GlobalFailure(_)) => "GLOBAL_RECOVERY_FAILURE",
320 None => "UNKNOWN_RECOVERY_EVENT",
321 }
322 }
323
324 pub fn database_recovery_start(database_id: u32) -> Self {
325 Self {
326 recovery_event: Some(RecoveryEvent::DatabaseStart(
327 event_recovery::DatabaseRecoveryStart { database_id },
328 )),
329 }
330 }
331
332 pub fn database_recovery_failure(database_id: u32) -> Self {
333 Self {
334 recovery_event: Some(RecoveryEvent::DatabaseFailure(
335 event_recovery::DatabaseRecoveryFailure { database_id },
336 )),
337 }
338 }
339
340 pub fn database_recovery_success(database_id: u32) -> Self {
341 Self {
342 recovery_event: Some(RecoveryEvent::DatabaseSuccess(
343 event_recovery::DatabaseRecoverySuccess { database_id },
344 )),
345 }
346 }
347
348 pub fn global_recovery_start(reason: String) -> Self {
349 Self {
350 recovery_event: Some(RecoveryEvent::GlobalStart(
351 event_recovery::GlobalRecoveryStart { reason },
352 )),
353 }
354 }
355
356 pub fn global_recovery_success(
357 reason: String,
358 duration_secs: f32,
359 running_database_ids: Vec<u32>,
360 recovering_database_ids: Vec<u32>,
361 ) -> Self {
362 Self {
363 recovery_event: Some(RecoveryEvent::GlobalSuccess(
364 event_recovery::GlobalRecoverySuccess {
365 reason,
366 duration_secs,
367 running_database_ids,
368 recovering_database_ids,
369 },
370 )),
371 }
372 }
373
374 pub fn global_recovery_failure(reason: String, error: String) -> Self {
375 Self {
376 recovery_event: Some(RecoveryEvent::GlobalFailure(
377 event_recovery::GlobalRecoveryFailure { reason, error },
378 )),
379 }
380 }
381}
382
383impl stream_plan::StreamNode {
384 pub fn find_stream_source(&self) -> Option<u32> {
388 if let Some(crate::stream_plan::stream_node::NodeBody::Source(source)) =
389 self.node_body.as_ref()
390 {
391 if let Some(inner) = &source.source_inner {
392 return Some(inner.source_id);
393 }
394 }
395
396 for child in &self.input {
397 if let Some(source) = child.find_stream_source() {
398 return Some(source);
399 }
400 }
401
402 None
403 }
404
405 pub fn find_source_backfill(&self) -> Option<(u32, u32)> {
413 if let Some(crate::stream_plan::stream_node::NodeBody::SourceBackfill(source)) =
414 self.node_body.as_ref()
415 {
416 if let crate::stream_plan::stream_node::NodeBody::Merge(merge) =
417 self.input[0].node_body.as_ref().unwrap()
418 {
419 return Some((source.upstream_source_id, merge.upstream_fragment_id));
422 } else {
423 unreachable!(
424 "source backfill must have a merge node as its input: {:?}",
425 self
426 );
427 }
428 }
429
430 for child in &self.input {
431 if let Some(source) = child.find_source_backfill() {
432 return Some(source);
433 }
434 }
435
436 None
437 }
438}
439
440impl stream_plan::FragmentTypeFlag {
441 pub fn backfill_rate_limit_fragments() -> i32 {
443 stream_plan::FragmentTypeFlag::SourceScan as i32
444 | stream_plan::FragmentTypeFlag::StreamScan as i32
445 }
446
447 pub fn source_rate_limit_fragments() -> i32 {
450 stream_plan::FragmentTypeFlag::Source as i32 | stream_plan::FragmentTypeFlag::FsFetch as i32
451 }
452
453 pub fn sink_rate_limit_fragments() -> i32 {
455 stream_plan::FragmentTypeFlag::Sink as i32
456 }
457
458 pub fn rate_limit_fragments() -> i32 {
460 Self::backfill_rate_limit_fragments()
461 | Self::source_rate_limit_fragments()
462 | Self::sink_rate_limit_fragments()
463 }
464
465 pub fn dml_rate_limit_fragments() -> i32 {
466 stream_plan::FragmentTypeFlag::Dml as i32
467 }
468}
469
470impl stream_plan::Dispatcher {
471 pub fn as_strategy(&self) -> stream_plan::DispatchStrategy {
472 stream_plan::DispatchStrategy {
473 r#type: self.r#type,
474 dist_key_indices: self.dist_key_indices.clone(),
475 output_indices: self.output_indices.clone(),
476 }
477 }
478}
479
480impl catalog::StreamSourceInfo {
481 pub fn is_shared(&self) -> bool {
483 self.cdc_source_job
484 }
485}
486
487impl stream_plan::PbStreamScanType {
488 pub fn is_reschedulable(&self) -> bool {
489 match self {
490 PbStreamScanType::UpstreamOnly => false,
492 PbStreamScanType::ArrangementBackfill => true,
493 PbStreamScanType::CrossDbSnapshotBackfill => true,
494 PbStreamScanType::SnapshotBackfill => true,
495 _ => false,
496 }
497 }
498}
499
500impl catalog::Sink {
501 pub const UNIQUE_IDENTITY_FOR_CREATING_TABLE_SINK: &'static str = "PLACE_HOLDER";
504
505 pub fn unique_identity(&self) -> String {
506 format!("{}", self.id)
508 }
509}
510
511impl std::fmt::Debug for meta::SystemParams {
512 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
516 f.debug_struct("SystemParams").finish_non_exhaustive()
517 }
518}
519
520impl std::fmt::Debug for data::DataType {
523 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
524 let data::DataType {
525 precision,
526 scale,
527 interval_type,
528 field_type,
529 field_names,
530 field_ids,
531 type_name,
532 is_nullable: _,
534 } = self;
535
536 let type_name = data::data_type::TypeName::try_from(*type_name)
537 .map(|t| t.as_str_name())
538 .unwrap_or("Unknown");
539
540 let mut s = f.debug_struct(type_name);
541 if self.precision != 0 {
542 s.field("precision", precision);
543 }
544 if self.scale != 0 {
545 s.field("scale", scale);
546 }
547 if self.interval_type != 0 {
548 s.field("interval_type", interval_type);
549 }
550 if !self.field_type.is_empty() {
551 s.field("field_type", field_type);
552 }
553 if !self.field_names.is_empty() {
554 s.field("field_names", field_names);
555 }
556 if !self.field_ids.is_empty() {
557 s.field("field_ids", field_ids);
558 }
559 s.finish()
560 }
561}
562
563impl std::fmt::Debug for plan_common::column_desc::GeneratedOrDefaultColumn {
564 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
565 match self {
566 Self::GeneratedColumn(arg0) => f.debug_tuple("GeneratedColumn").field(arg0).finish(),
567 Self::DefaultColumn(arg0) => f.debug_tuple("DefaultColumn").field(arg0).finish(),
568 }
569 }
570}
571
572impl std::fmt::Debug for plan_common::ColumnDesc {
573 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
574 let plan_common::ColumnDesc {
576 column_type,
577 column_id,
578 name,
579 description,
580 additional_column_type,
581 additional_column,
582 generated_or_default_column,
583 version,
584 nullable,
585 } = self;
586
587 let mut s = f.debug_struct("ColumnDesc");
588 if let Some(column_type) = column_type {
589 s.field("column_type", column_type);
590 } else {
591 s.field("column_type", &"Unknown");
592 }
593 s.field("column_id", column_id).field("name", name);
594 if let Some(description) = description {
595 s.field("description", description);
596 }
597 if self.additional_column_type != 0 {
598 s.field("additional_column_type", additional_column_type);
599 }
600 s.field("version", version);
601 if let Some(AdditionalColumn {
602 column_type: Some(column_type),
603 }) = additional_column
604 {
605 s.field("additional_column", &column_type);
607 }
608 if let Some(generated_or_default_column) = generated_or_default_column {
609 s.field("generated_or_default_column", &generated_or_default_column);
610 }
611 s.field("nullable", nullable);
612 s.finish()
613 }
614}
615
616impl expr::UserDefinedFunction {
617 pub fn name_in_runtime(&self) -> Option<&str> {
618 if self.version() < expr::UdfExprVersion::NameInRuntime {
619 if self.language == "rust" || self.language == "wasm" {
620 Some(&self.name)
624 } else {
625 self.identifier.as_deref()
627 }
628 } else {
629 self.identifier.as_deref()
631 }
632 }
633}
634
635impl expr::UserDefinedFunctionMetadata {
636 pub fn name_in_runtime(&self) -> Option<&str> {
637 if self.version() < expr::UdfExprVersion::NameInRuntime {
638 if self.language == "rust" || self.language == "wasm" {
639 let old_identifier = self
644 .identifier
645 .as_ref()
646 .expect("Rust/WASM UDF must have identifier");
647 Some(
648 old_identifier
649 .split_once("(")
650 .expect("the old identifier must contain `(`")
651 .0,
652 )
653 } else {
654 self.identifier.as_deref()
656 }
657 } else {
658 self.identifier.as_deref()
660 }
661 }
662}
663
664#[cfg(test)]
665mod tests {
666 use crate::data::{DataType, data_type};
667 use crate::plan_common::Field;
668 use crate::stream_plan::stream_node::NodeBody;
669
670 #[test]
671 fn test_getter() {
672 let data_type: DataType = DataType {
673 is_nullable: true,
674 ..Default::default()
675 };
676 let field = Field {
677 data_type: Some(data_type),
678 name: "".to_owned(),
679 };
680 assert!(field.get_data_type().unwrap().is_nullable);
681 }
682
683 #[test]
684 fn test_enum_getter() {
685 let mut data_type: DataType = DataType::default();
686 data_type.type_name = data_type::TypeName::Double as i32;
687 assert_eq!(
688 data_type::TypeName::Double,
689 data_type.get_type_name().unwrap()
690 );
691 }
692
693 #[test]
694 fn test_enum_unspecified() {
695 let mut data_type: DataType = DataType::default();
696 data_type.type_name = data_type::TypeName::TypeUnspecified as i32;
697 assert!(data_type.get_type_name().is_err());
698 }
699
700 #[test]
701 fn test_primitive_getter() {
702 let data_type: DataType = DataType::default();
703 let new_data_type = DataType {
704 is_nullable: data_type.get_is_nullable(),
705 ..Default::default()
706 };
707 assert!(!new_data_type.is_nullable);
708 }
709
710 #[test]
711 fn test_size() {
712 use static_assertions::const_assert_eq;
713 const_assert_eq!(std::mem::size_of::<NodeBody>(), 16);
716 }
717}