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

validate tag timestamp #3703

Merged
merged 6 commits into from
Aug 7, 2023
Merged
Changes from all commits
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
126 changes: 126 additions & 0 deletions quickwit/quickwit-doc-mapper/src/default_doc_mapper/default_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ fn validate_timestamp_field(
timestamp_field_path: &str,
mapping_root_node: &MappingNode,
) -> anyhow::Result<()> {
if timestamp_field_path.starts_with('.') || timestamp_field_path.starts_with("\\.") {
bail!("Timestamp field `{timestamp_field_path}` should not start with a `.`.");
}
if timestamp_field_path.ends_with('.') {
bail!("Timestamp field `{timestamp_field_path}` should not end with a `.`.");
}
let Some(timestamp_field_type) =
mapping_root_node.find_field_mapping_type(timestamp_field_path)
else {
Expand Down Expand Up @@ -262,6 +268,12 @@ impl TryFrom<DefaultDocMapperBuilder> for DefaultDocMapper {
/// - if str, the field must use the `raw` tokenizer for indexing.
/// - the field must be indexed.
fn validate_tag(tag_field_name: &str, schema: &Schema) -> Result<(), anyhow::Error> {
if tag_field_name.starts_with('.') || tag_field_name.starts_with("\\.") {
bail!("Tag field `{tag_field_name}` should not start with a `.`.");
}
if tag_field_name.ends_with('.') {
bail!("Tag field `{tag_field_name}` should not end with a `.`.");
}
let field = schema
.get_field(tag_field_name)
.with_context(|| format!("Unknown tag field: `{tag_field_name}`"))?;
Expand Down Expand Up @@ -821,6 +833,120 @@ mod tests {
.unwrap();
}

#[test]
fn test_timestamp_field_that_start_with_dot_is_invalid() {
assert_eq!(
serde_json::from_str::<DefaultDocMapper>(
r#"{
"field_mappings": [
{
"name": "my.timestamp",
"type": "datetime",
"fast": true
}
],
"timestamp_field": ".my.timestamp"
}"#,
)
.unwrap_err()
.to_string(),
"Timestamp field `.my.timestamp` should not start with a `.`.",
);

assert_eq!(
serde_json::from_str::<DefaultDocMapper>(
r#"{
"field_mappings": [
{
"name": "my.timestamp",
"type": "datetime",
"fast": true
}
],
"timestamp_field": "\\.my\\.timestamp"
}"#,
)
.unwrap_err()
.to_string(),
"Timestamp field `\\.my\\.timestamp` should not start with a `.`.",
)
}

#[test]
fn test_timestamp_field_that_ends_with_dot_is_invalid() {
assert_eq!(
serde_json::from_str::<DefaultDocMapper>(
r#"{
"timestamp_field": "my.timestamp."
}"#,
)
.unwrap_err()
.to_string(),
"Timestamp field `my.timestamp.` should not end with a `.`.",
);

assert_eq!(
serde_json::from_str::<DefaultDocMapper>(
r#"{
"timestamp_field": "my\\.timestamp\\."
}"#,
)
.unwrap_err()
.to_string(),
"Timestamp field `my\\.timestamp\\.` should not end with a `.`.",
)
}

#[test]
fn test_tag_field_name_that_starts_with_dot_is_invalid() {
assert_eq!(
serde_json::from_str::<DefaultDocMapper>(
r#"{
"tag_fields": [".my.tag"]
}"#,
)
.unwrap_err()
.to_string(),
"Tag field `.my.tag` should not start with a `.`.",
);

assert_eq!(
serde_json::from_str::<DefaultDocMapper>(
r#"{
"tag_fields": ["\\.my\\.tag"]
}"#,
)
.unwrap_err()
.to_string(),
"Tag field `\\.my\\.tag` should not start with a `.`.",
)
}

#[test]
fn test_tag_field_name_that_ends_with_dot_is_invalid() {
assert_eq!(
serde_json::from_str::<DefaultDocMapper>(
r#"{
"tag_fields": ["my.tag."]
}"#,
)
.unwrap_err()
.to_string(),
"Tag field `my.tag.` should not end with a `.`.",
);

assert_eq!(
serde_json::from_str::<DefaultDocMapper>(
r#"{
"tag_fields": ["my\\.tag\\."]
}"#,
)
.unwrap_err()
.to_string(),
"Tag field `my\\.tag\\.` should not end with a `.`.",
)
}

#[test]
fn test_fail_to_build_doc_mapper_with_timestamp_field_with_multivalues_cardinality() {
let doc_mapper = r#"{
Expand Down
Loading