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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make all fields strings except _timestamp #3087

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ pub struct Common {
help = "Show docs count and stream dates"
)]
pub show_stream_dates_doc_num: bool,

#[env_config(name = "ZO_SCHEMA_FIELDS_TYPE_STRING", default = false)]
pub all_schema_fields_type_string: bool,
}

#[derive(EnvConfig)]
Expand Down
161 changes: 110 additions & 51 deletions src/service/logs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,72 +159,131 @@ async fn add_valid_record(
need_trigger: bool,
) -> Result<Option<TriggerAlertData>> {
let mut trigger: TriggerAlertData = Vec::new();
let schema_cache;
let timestamp: i64 = record_val
.get(&CONFIG.common.column_timestamp)
.unwrap()
.as_i64()
.unwrap();

// check schema
let (schema_evolution, _) = check_for_schema(
&stream_meta.org_id,
&stream_meta.stream_name,
StreamType::Logs,
stream_schema_map,
&record_val,
timestamp,
)
.await?;
let (rec_schema, hour_key) = if !CONFIG.common.all_schema_fields_type_string {
// check schema
let (schema_evolution, _) = check_for_schema(
&stream_meta.org_id,
&stream_meta.stream_name,
StreamType::Logs,
stream_schema_map,
&record_val,
timestamp,
)
.await?;

// get schema
let rec_schema = stream_schema_map.get(&stream_meta.stream_name).unwrap();
let schema_key = rec_schema.hash_key();
// get schema
let rec_schema = stream_schema_map.get(&stream_meta.stream_name).unwrap();
let schema_key = rec_schema.hash_key();

// get hour key
let hour_key = get_wal_time_key(
timestamp,
stream_meta.partition_keys,
unwrap_partition_time_level(*stream_meta.partition_time_level, StreamType::Logs),
&record_val,
Some(schema_key),
);
// get hour key
let hour_key = get_wal_time_key(
timestamp,
stream_meta.partition_keys,
unwrap_partition_time_level(*stream_meta.partition_time_level, StreamType::Logs),
&record_val,
Some(schema_key),
);

if !schema_evolution.schema_compatible {
status.failed += 1;
return Ok(None);
}
if !schema_evolution.schema_compatible {
status.failed += 1;
return Ok(None);
}

let valid_record = match schema_evolution.types_delta {
None => true,
Some(delta) => {
let ret_val = if !CONFIG.common.widening_schema_evolution
|| !schema_evolution.is_schema_changed
{
cast_to_type(&mut record_val, delta)
} else {
let local_delta = delta
.into_iter()
.filter(|x| x.metadata().contains_key("zo_cast"))
.collect::<Vec<_>>();
if !local_delta.is_empty() {
cast_to_type(&mut record_val, local_delta)
let valid_record = match schema_evolution.types_delta {
None => true,
Some(delta) => {
let ret_val = if !CONFIG.common.widening_schema_evolution
|| !schema_evolution.is_schema_changed
{
cast_to_type(&mut record_val, delta)
} else {
Ok(())
}
};
match ret_val {
Ok(_) => true,
Err(e) => {
status.failed += 1;
status.error = e.to_string();
false
let local_delta = delta
.into_iter()
.filter(|x| x.metadata().contains_key("zo_cast"))
.collect::<Vec<_>>();
if !local_delta.is_empty() {
cast_to_type(&mut record_val, local_delta)
} else {
Ok(())
}
};
match ret_val {
Ok(_) => true,
Err(e) => {
status.failed += 1;
status.error = e.to_string();
false
}
}
}
};
if !valid_record {
return Ok(None);
}
(rec_schema, hour_key)
} else {
// cast all fields to string
for (key, v) in record_val.iter_mut() {
if !v.is_string() && key != &CONFIG.common.column_timestamp {
*v = Value::String(v.to_string());
}
}
// get schema

let value_iter = [record_val.clone()].into_iter();
let inferred_schema =
config::utils::schema::infer_json_schema_from_map(value_iter, StreamType::Logs)
.unwrap();
let mut rec_schema = stream_schema_map.get(&stream_meta.stream_name).unwrap();
if rec_schema.schema().fields().is_empty() {
// get infer schema
let mut metadata = HashMap::with_capacity(1);
metadata.insert("created_at".to_string(), timestamp.to_string());
let fields_map = inferred_schema
.fields()
.iter()
.enumerate()
.map(|(i, f)| (f.name().to_owned(), i))
.collect();
schema_cache = SchemaCache::new(
inferred_schema.clone().with_metadata(metadata.clone()),
fields_map,
);
rec_schema = &schema_cache;

super::db::schema::set(
&stream_meta.org_id,
&stream_meta.stream_name,
StreamType::Logs,
&inferred_schema.clone().with_metadata(metadata),
Some(timestamp),
false,
)
.await
.unwrap();

stream_schema_map.insert(stream_meta.stream_name.to_owned(), schema_cache.clone());
}
let schema_key = rec_schema.hash_key();

// get hour key
let hour_key = get_wal_time_key(
timestamp,
stream_meta.partition_keys,
unwrap_partition_time_level(*stream_meta.partition_time_level, StreamType::Logs),
&record_val,
Some(schema_key),
);

(rec_schema, hour_key)
};
if !valid_record {
return Ok(None);
}

if need_trigger && !stream_meta.stream_alerts_map.is_empty() {
// Start check for alert trigger
Expand Down
1 change: 1 addition & 0 deletions src/service/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ fn is_widening_conversion(from: &DataType, to: &DataType) -> bool {
allowed_type.contains(to)
}

#[derive(Clone)]
pub struct SchemaCache {
schema: Schema,
fields_map: HashMap<String, usize>,
Expand Down
4 changes: 2 additions & 2 deletions web/src/composables/useLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1552,9 +1552,9 @@ const useLogs = () => {
: [...schemaInterestingFields];

let environmentInterestingFields = [];
if (store.state.zoConfig.hasOwnProperty("quick_mode_fields")) {
if (store.state.zoConfig.hasOwnProperty("default_quick_mode_fields")) {
environmentInterestingFields =
store.state?.zoConfig?.quick_mode_fields.split(",");
store.state?.zoConfig?.default_quick_mode_fields.split(",");
}
let index = -1;
// queryResult.forEach((row: any) => {
Expand Down
Loading