Skip to main content

risingwave_connector/source/cdc/
sql_server.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 std::collections::BTreeMap;
16
17use risingwave_common::license::{Feature, FeatureNotAvailable};
18use risingwave_pb::secret::PbSecretRef;
19
20use crate::error::ConnectorResult;
21use crate::source::cdc::external::{DATABASE_NAME_KEY, SchemaTableName};
22use crate::source::cdc::{
23    CdcSourceDdlValidator, CdcSourceExternalTableIdentifier, CdcSourceRuntimeFlags,
24    CdcSourceTypeTrait, SqlServer, check_alter_with_options_inner, check_common_with_options,
25    split_quoted_identifier_inner,
26};
27
28// ------- Runtime flags -------
29
30impl CdcSourceRuntimeFlags for SqlServer {
31    const CAN_BACKFILL: bool = true;
32    const NEEDS_NON_EMPTY_INITIAL_OFFSET: bool = true;
33    // `CAN_USE_TRANSACTION_METADATA` stays false: Debezium only sends a transaction's `END` event
34    // when the next transaction starts, which delays the newest events.
35    // See https://debezium.io/documentation/reference/2.6/connectors/sqlserver.html#sqlserver-transaction-metadata
36}
37
38// ------- DDL validation -------
39
40impl CdcSourceDdlValidator for SqlServer {
41    fn check_and_fill_create_with_options(
42        props: &mut BTreeMap<String, String>,
43        secret_refs: &BTreeMap<String, PbSecretRef>,
44    ) -> ConnectorResult<()> {
45        check_common_with_options::<Self>(props, secret_refs)?;
46        if !props.contains_key("schema.name") {
47            // Default schema name is "dbo"
48            props.insert("schema.name".into(), "dbo".into());
49        }
50        Ok(())
51    }
52
53    fn check_license() -> Result<(), FeatureNotAvailable> {
54        Feature::SqlServerCdcSource.check_available()
55    }
56
57    fn check_alter_with_options(
58        props: &BTreeMap<String, String>,
59        secret_refs: &BTreeMap<String, PbSecretRef>,
60    ) -> ConnectorResult<()> {
61        check_common_with_options::<Self>(props, secret_refs)?;
62        check_alter_with_options_inner(
63            Self::CDC_CONNECTOR_NAME,
64            props,
65            secret_refs,
66            &[
67                "cdc.source.wait.streaming.start.timeout",
68                "debezium.max.queue.size",
69                "debezium.queue.memory.ratio",
70                "hostname",
71                "port",
72                "password",
73            ],
74        )
75    }
76}
77
78// ------- External table identifier -------
79
80impl CdcSourceExternalTableIdentifier for SqlServer {
81    /// Debezium SQL Server always names topics in multi-partition mode, so the topic suffix
82    /// starts with the database: `database.schema.table`. The database part must be the name
83    /// SQL Server reports, which the Java validator guarantees by checking `database.name`
84    /// against `DB_NAME()` exactly.
85    fn canonical_name(connect_properties: &BTreeMap<String, String>) -> String {
86        let SchemaTableName {
87            schema_name,
88            table_name,
89        } = SchemaTableName::from_properties(connect_properties);
90        let database_name = connect_properties
91            .get(DATABASE_NAME_KEY)
92            .map(String::as_str)
93            .unwrap_or_default();
94        format!("{database_name}.{schema_name}.{table_name}")
95    }
96
97    fn split_quoted_identifier(name: &str) -> Option<Vec<String>> {
98        split_quoted_identifier_inner(name, '[', ']')
99    }
100}