Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make is_dictionary optional #1074

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ serde = { workspace = true }
serde_json = { workspace = true }
snafu = { workspace = true }
sqlparser = { workspace = true }

[dev-dependencies]
common_util = { workspace = true }
69 changes: 63 additions & 6 deletions common_types/src/column_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub enum ReadOp {
}

/// Meta data of the arrow field.
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, PartialEq)]
struct ArrowFieldMeta {
id: u32,
is_tag: bool,
Expand All @@ -146,6 +146,12 @@ impl ArrowFieldMetaKey {
ArrowFieldMetaKey::Comment => "field::comment",
}
}

// Only id,is_tag,comment are required fields, other fields should be optional
// to keep backward compatible
fn is_required(&self) -> bool {
matches!(self, Self::Id | Self::IsTag | Self::Comment)
}
tanruixiang marked this conversation as resolved.
Show resolved Hide resolved
}

impl ToString for ArrowFieldMetaKey {
Expand Down Expand Up @@ -352,17 +358,25 @@ impl From<&ColumnSchema> for Field {
}
}

fn parse_arrow_field_meta_value<T>(
fn parse_arrow_field_meta_value<T: Default>(
meta: &HashMap<String, String>,
key: ArrowFieldMetaKey,
) -> Result<T>
where
T: FromStr,
T::Err: std::error::Error + Send + Sync + 'static,
{
let raw_value = meta
.get(key.as_str())
.context(ArrowFieldMetaKeyNotFound { key })?;
let raw_value = match meta.get(key.as_str()) {
None => {
if key.is_required() {
return ArrowFieldMetaKeyNotFound { key }.fail();
} else {
return Ok(T::default());
}
}
Some(v) => v,
};

T::from_str(raw_value.as_str())
.map_err(|e| Box::new(e) as _)
.context(InvalidArrowFieldMetaValue { key, raw_value })
Expand All @@ -375,8 +389,8 @@ fn decode_arrow_field_meta_data(meta: &HashMap<String, String>) -> Result<ArrowF
Ok(ArrowFieldMeta {
id: parse_arrow_field_meta_value(meta, ArrowFieldMetaKey::Id)?,
is_tag: parse_arrow_field_meta_value(meta, ArrowFieldMetaKey::IsTag)?,
is_dictionary: parse_arrow_field_meta_value(meta, ArrowFieldMetaKey::IsDictionary)?,
comment: parse_arrow_field_meta_value(meta, ArrowFieldMetaKey::Comment)?,
is_dictionary: parse_arrow_field_meta_value(meta, ArrowFieldMetaKey::IsDictionary)?,
})
}
}
Expand Down Expand Up @@ -524,6 +538,7 @@ impl From<ColumnSchema> for schema_pb::ColumnSchema {

#[cfg(test)]
mod tests {
use common_util::hash_map;
use sqlparser::ast::Value;

use super::*;
Expand Down Expand Up @@ -596,4 +611,46 @@ mod tests {
);
}
}

#[test]
fn test_decode_arrow_field_meta_data() {
let testcases = [
(
hash_map! {
"field::id".to_string() => "1".to_string(),
"field::is_tag".to_string() => "true".to_string(),
"field::comment".to_string() => "".to_string()
},
ArrowFieldMeta {
id: 1,
is_tag: true,
is_dictionary: false,
comment: "".to_string(),
chunshao90 marked this conversation as resolved.
Show resolved Hide resolved
},
),
(
hash_map! {
"field::id".to_string() => "1".to_string(),
"field::is_tag".to_string() => "false".to_string(),
"field::comment".to_string() => "abc".to_string(),
"field::is_dictionary".to_string() => "true".to_string()
},
ArrowFieldMeta {
id: 1,
is_tag: false,
is_dictionary: true,
comment: "abc".to_string(),
chunshao90 marked this conversation as resolved.
Show resolved Hide resolved
},
),
];

for (meta, expected) in &testcases {
assert_eq!(expected, &decode_arrow_field_meta_data(meta).unwrap())
}

let meta = hash_map! {
"field::id".to_string() => "1".to_string()
};
assert!(decode_arrow_field_meta_data(&meta).is_err());
}
}