vector/sinks/databend/
encoding.rs

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