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