vector/sinks/databend/
encoding.rs

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