@@ -22,6 +22,7 @@ use super::cluster::{sync_streams_with_ingestors, INTERNAL_STREAM_NAME};
2222use super :: ingest:: create_stream_if_not_exists;
2323use super :: modal:: utils:: logstream_utils:: create_update_stream;
2424use crate :: alerts:: Alerts ;
25+ use crate :: event:: format:: update_data_type_to_datetime;
2526use crate :: handlers:: STREAM_TYPE_KEY ;
2627use crate :: hottier:: { HotTierManager , StreamHotTier , CURRENT_HOT_TIER_VERSION } ;
2728use crate :: metadata:: STREAM_INFO ;
@@ -36,6 +37,7 @@ use crate::{metadata, validator};
3637use actix_web:: http:: header:: { self , HeaderMap } ;
3738use actix_web:: http:: StatusCode ;
3839use actix_web:: { web, HttpRequest , Responder } ;
40+ use arrow_json:: reader:: infer_json_schema_from_iterator;
3941use arrow_schema:: { Field , Schema } ;
4042use bytes:: Bytes ;
4143use chrono:: Utc ;
@@ -89,6 +91,26 @@ pub async fn list(_: HttpRequest) -> impl Responder {
8991 web:: Json ( res)
9092}
9193
94+ pub async fn detect_schema ( body : Bytes ) -> Result < impl Responder , StreamError > {
95+ let body_val: Value = serde_json:: from_slice ( & body) ?;
96+ let value_arr: Vec < Value > = match body_val {
97+ Value :: Array ( arr) => arr,
98+ value @ Value :: Object ( _) => vec ! [ value] ,
99+ _ => {
100+ return Err ( StreamError :: Custom {
101+ msg : "please send json events as part of the request" . to_string ( ) ,
102+ status : StatusCode :: BAD_REQUEST ,
103+ } )
104+ }
105+ } ;
106+
107+ let mut schema = infer_json_schema_from_iterator ( value_arr. iter ( ) . map ( Ok ) ) . unwrap ( ) ;
108+ for value in value_arr {
109+ schema = update_data_type_to_datetime ( schema, value) ;
110+ }
111+ Ok ( ( web:: Json ( schema) , StatusCode :: OK ) )
112+ }
113+
92114pub async fn schema ( req : HttpRequest ) -> Result < impl Responder , StreamError > {
93115 let stream_name: String = req. match_info ( ) . get ( "logstream" ) . unwrap ( ) . parse ( ) . unwrap ( ) ;
94116 let schema = STREAM_INFO . schema ( & stream_name) ?;
0 commit comments