Skip to main content

risingwave_connector/source/
ingest_mode.rs

1// Copyright 2026 RisingWave Labs
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
15use risingwave_common::bail;
16use risingwave_pb::plan_common::PbSourceIngestMode;
17use risingwave_pb::plan_common::source_ingest_mode::source_ingest_mode_bounded::Input as PbBoundedInput;
18use risingwave_pb::plan_common::source_ingest_mode::{
19    PbIngestMode, SourceIngestModeBounded as PbBounded, SourceIngestModeFullReload as PbFullReload,
20    SourceIngestModeStreaming as PbStreaming,
21};
22
23use crate::error::ConnectorResult;
24use crate::source::iceberg::IcebergBoundedScan;
25
26/// Decoded from [`PbSourceIngestMode`].
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
28pub enum IngestMode {
29    #[default]
30    Streaming,
31    FullReload {
32        interval_sec: Option<i64>,
33    },
34    Bounded(BoundedInput),
35}
36
37/// Decoded from [`PbBounded`].
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
39pub enum BoundedInput {
40    Iceberg(IcebergBoundedScan),
41}
42
43impl IngestMode {
44    pub fn from_proto(mode: Option<&PbSourceIngestMode>) -> ConnectorResult<Self> {
45        let Some(mode) = mode else {
46            return Ok(Self::Streaming);
47        };
48
49        Ok(match &mode.ingest_mode {
50            None | Some(PbIngestMode::Streaming(_)) => Self::Streaming,
51            Some(PbIngestMode::FullReload(full_reload)) => Self::FullReload {
52                interval_sec: full_reload.refresh_interval_sec,
53            },
54            Some(PbIngestMode::Bounded(bounded)) => {
55                Self::Bounded(BoundedInput::from_proto(bounded)?)
56            }
57        })
58    }
59
60    pub fn to_proto(self) -> PbSourceIngestMode {
61        let ingest_mode = match self {
62            Self::Streaming => PbIngestMode::Streaming(PbStreaming {}),
63            Self::FullReload { interval_sec } => PbIngestMode::FullReload(PbFullReload {
64                refresh_interval_sec: interval_sec,
65            }),
66            Self::Bounded(input) => PbIngestMode::Bounded(input.to_proto()),
67        };
68
69        PbSourceIngestMode {
70            ingest_mode: Some(ingest_mode),
71        }
72    }
73
74    pub fn is_full_reload(&self) -> bool {
75        matches!(self, Self::FullReload { .. })
76    }
77
78    pub fn is_bounded(&self) -> bool {
79        matches!(self, Self::Bounded(_))
80    }
81
82    /// The interval of periodic full reloads, if any.
83    pub fn refresh_interval_sec(&self) -> Option<i64> {
84        match self {
85            Self::FullReload { interval_sec } => *interval_sec,
86            Self::Streaming | Self::Bounded(_) => None,
87        }
88    }
89
90    pub fn bounded_input(&self) -> Option<BoundedInput> {
91        match self {
92            Self::Bounded(input) => Some(*input),
93            Self::Streaming | Self::FullReload { .. } => None,
94        }
95    }
96
97    pub fn iceberg_bounded_scan(&self) -> Option<IcebergBoundedScan> {
98        self.bounded_input().map(|BoundedInput::Iceberg(scan)| scan)
99    }
100}
101
102impl BoundedInput {
103    pub fn from_proto(bounded: &PbBounded) -> ConnectorResult<Self> {
104        match bounded.input {
105            Some(PbBoundedInput::Iceberg(scan)) => {
106                Ok(Self::Iceberg(IcebergBoundedScan::from_proto(scan)?))
107            }
108            None => bail!("bounded ingest mode requires a pinned input"),
109        }
110    }
111
112    pub fn to_proto(self) -> PbBounded {
113        match self {
114            Self::Iceberg(scan) => PbBounded {
115                input: Some(PbBoundedInput::Iceberg(scan.to_proto())),
116            },
117        }
118    }
119
120    pub fn iceberg_snapshot_id(&self) -> i64 {
121        match self {
122            Self::Iceberg(IcebergBoundedScan::Snapshot { snapshot_id }) => *snapshot_id,
123        }
124    }
125}