vector/sinks/databend/
encoding.rs

1use vector_lib::{
2    codecs::{
3        CsvSerializerConfig, EncodingConfig, JsonSerializerConfig, Transformer,
4        encoding::SerializerConfig,
5    },
6    configurable::configurable_component,
7};
8
9/// Serializer configuration for Databend.
10#[configurable_component]
11#[derive(Clone, Debug)]
12#[serde(tag = "codec", rename_all = "snake_case")]
13#[configurable(metadata(docs::enum_tag_description = "The codec to use for encoding events."))]
14pub(super) enum DatabendSerializerConfig {
15    /// Encodes an event as a CSV message.
16    ///
17    /// This codec must be configured with fields to encode.
18    ///
19    Csv(
20        /// Options for the CSV encoder.
21        CsvSerializerConfig,
22    ),
23
24    /// Encodes an event as [JSON][json].
25    ///
26    /// [json]: https://www.json.org/
27    Json(
28        /// Encoding options specific to the Json serializer.
29        JsonSerializerConfig,
30    ),
31}
32
33impl From<DatabendSerializerConfig> for SerializerConfig {
34    fn from(config: DatabendSerializerConfig) -> Self {
35        match config {
36            DatabendSerializerConfig::Csv(config) => Self::Csv(config),
37            DatabendSerializerConfig::Json(config) => Self::Json(config),
38        }
39    }
40}
41
42impl Default for DatabendSerializerConfig {
43    fn default() -> Self {
44        Self::Json(JsonSerializerConfig::default())
45    }
46}
47
48/// Encoding configuration for Databend.
49#[configurable_component]
50#[derive(Clone, Debug, Default)]
51#[configurable(description = "Configures how events are encoded into raw bytes.")]
52pub struct DatabendEncodingConfig {
53    #[serde(flatten)]
54    encoding: DatabendSerializerConfig,
55
56    #[serde(flatten)]
57    transformer: Transformer,
58}
59
60impl From<DatabendEncodingConfig> for EncodingConfig {
61    fn from(encoding: DatabendEncodingConfig) -> Self {
62        Self::new(encoding.encoding.into(), encoding.transformer)
63    }
64}
65
66impl DatabendEncodingConfig {
67    /// Get the encoding configuration.
68    pub(super) const fn config(&self) -> &DatabendSerializerConfig {
69        &self.encoding
70    }
71}
72
73/// Defines how missing fields are handled for NDJson.
74/// Refer to https://docs.databend.com/sql/sql-reference/file-format-options#null_field_as
75#[configurable_component]
76#[derive(Clone, Debug)]
77#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
78#[configurable(metadata(docs::enum_tag_description = "How to handle missing fields for NDJson."))]
79#[derive(Default)]
80pub enum DatabendMissingFieldAS {
81    /// Generates an error if a missing field is encountered.
82    Error,
83
84    /// Interprets missing fields as NULL values. An error will be generated for non-nullable fields.
85    #[default]
86    Null,
87
88    /// Uses the default value of the field for missing fields.
89    FieldDefault,
90
91    /// Uses the default value of the field's data type for missing fields.
92    TypeDefault,
93}
94
95impl DatabendMissingFieldAS {
96    pub(super) const fn as_str(&self) -> &'static str {
97        match self {
98            Self::Error => "ERROR",
99            Self::Null => "NULL",
100            Self::FieldDefault => "FIELD_DEFAULT",
101            Self::TypeDefault => "TYPE_DEFAULT",
102        }
103    }
104}