risingwave_connector_codec/decoder/mod.rs
1// Copyright 2025 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
15pub mod avro;
16pub mod json;
17pub mod protobuf;
18pub mod utils;
19
20use risingwave_common::error::NotImplemented;
21use risingwave_common::types::{DataType, Datum, DatumCow, ToOwnedDatum};
22use thiserror::Error;
23use thiserror_ext::Macro;
24
25#[derive(Error, Debug, Macro)]
26#[thiserror_ext(macro(mangle, path = "crate::decoder"))]
27pub enum AccessError {
28 #[error("Undefined field `{name}` at `{path}`")]
29 Undefined { name: String, path: String },
30 #[error("Cannot parse value `{value}` with type `{got}` into expected type `{expected}`")]
31 TypeError {
32 expected: String,
33 got: String,
34 value: String,
35 },
36 #[error("Unsupported data type `{ty}`")]
37 UnsupportedType { ty: String },
38
39 /// CDC auto schema change specific error that may include table context
40 #[error("CDC auto schema change error: unsupported data type `{ty}` in table `{table_name}`")]
41 CdcAutoSchemaChangeError { ty: String, table_name: String },
42
43 #[error("Unsupported additional column `{name}`")]
44 UnsupportedAdditionalColumn { name: String },
45
46 #[error("Fail to convert protobuf Any into jsonb: {0}")]
47 ProtobufAnyToJson(#[source] serde_json::Error),
48
49 /// Errors that are not categorized into variants above.
50 #[error("{message}")]
51 Uncategorized { message: String },
52
53 #[error(transparent)]
54 NotImplemented(#[from] NotImplemented),
55 // NOTE: We intentionally don't embed `anyhow::Error` in `AccessError` since it happens
56 // in record-level and it might be too heavy to capture the backtrace
57 // when creating a new `anyhow::Error`.
58}
59
60pub type AccessResult<T = Datum> = std::result::Result<T, AccessError>;
61
62/// Access to a field in the data structure. Created by `AccessBuilder`.
63///
64/// It's the `ENCODE ...` part in `FORMAT ... ENCODE ...`
65pub trait Access {
66 /// Accesses `path` in the data structure (*parsed* Avro/JSON/Protobuf data),
67 /// and then converts it to RisingWave `Datum`.
68 ///
69 /// `type_expected` might or might not be used during the conversion depending on the implementation.
70 ///
71 /// # Path
72 ///
73 /// We usually expect the data (`Access` instance) is a record (struct), and `path` represents field path.
74 /// The data (or part of the data) represents the whole row (`Vec<Datum>`),
75 /// and we use different `path` to access one column at a time.
76 ///
77 /// TODO: the meaning of `path` is a little confusing and maybe over-abstracted.
78 /// `access` does not need to serve arbitrarily deep `path` access, but just "top-level" access.
79 /// The API creates an illusion that arbitrary access is supported, but it's not.
80 /// Perhaps we should separate out another trait like `ToDatum`,
81 /// which only does type mapping, without caring about the path. And `path` itself is only an `enum` instead of `&[&str]`.
82 ///
83 /// What `path` to access is decided by the CDC layer, i.e., the `FORMAT ...` part (`ChangeEvent`).
84 /// e.g.,
85 /// - `DebeziumChangeEvent` accesses `["before", "col_name"]` for value,
86 /// `["source", "db"]`, `["source", "table"]` etc. for additional columns' values,
87 /// `["op"]` for op type.
88 /// - `MaxwellChangeEvent` accesses `["data", "col_name"]` for value, `["type"]` for op type.
89 /// - In the simplest case, for `FORMAT PLAIN/UPSERT` (`KvEvent`), they just access `["col_name"]` for value, and op type is derived.
90 ///
91 /// # Returns
92 ///
93 /// The implementation should prefer to return a borrowed [`DatumRef`](risingwave_common::types::DatumRef)
94 /// through [`DatumCow::Borrowed`] to avoid unnecessary allocation if possible, especially for fields
95 /// with string or bytes data. If that's not the case, it may return an owned [`Datum`] through
96 /// [`DatumCow::Owned`].
97 fn access<'a>(&'a self, path: &[&str], type_expected: &DataType) -> AccessResult<DatumCow<'a>>;
98}
99
100// Note: made an extension trait to disallow implementing or overriding `access_owned`.
101#[easy_ext::ext(AccessExt)]
102impl<A: Access> A {
103 /// Similar to `access`, but always returns an owned [`Datum`]. See [`Access::access`] for more details.
104 ///
105 /// Always prefer calling `access` directly if possible to avoid unnecessary allocation.
106 pub fn access_owned(&self, path: &[&str], type_expected: &DataType) -> AccessResult<Datum> {
107 self.access(path, type_expected)
108 .map(ToOwnedDatum::to_owned_datum)
109 }
110}