11use super :: config:: AdapterConfig ;
22use super :: traits:: * ;
3+ use crate :: QueryResult ;
34use crate :: data_model:: QueryLanguage ;
45use async_trait:: async_trait;
56use axum:: {
@@ -11,7 +12,7 @@ use axum::{
1112use serde_json:: { json, Value } ;
1213use std:: collections:: HashMap ;
1314use std:: sync:: Arc ;
14- use tracing:: { debug, error} ;
15+ use tracing:: { debug, error, info } ;
1516
1617/// Elasticsearch HTTP protocol adapter
1718pub struct ElasticHttpAdapter {
@@ -26,34 +27,30 @@ impl ElasticHttpAdapter {
2627
2728 /// Parse Elasticsearch query from JSON body
2829 fn parse_elasticsearch_query ( & self , body : & Bytes ) -> Result < ParsedQueryRequest , AdapterError > {
29- debug ! (
30- "Elasticsearch adapter: parsing query for language {:?}" ,
31- self . config. language
32- ) ;
33-
34- // Parse the JSON body
3530 let json_body: Value = serde_json:: from_slice ( body)
3631 . map_err ( |e| AdapterError :: ParseError ( format ! ( "Invalid JSON: {}" , e) ) ) ?;
3732
38- // Store the entire query as a JSON string
39- let query = serde_json:: to_string ( & json_body)
40- . map_err ( |e| AdapterError :: ParseError ( format ! ( "Failed to serialize query: {}" , e) ) ) ?;
33+ // Extract the SQL string from the "query" field
34+ let query = match & self . config . language {
35+ QueryLanguage :: elastic_sql => {
36+ json_body
37+ . get ( "query" )
38+ . and_then ( |v| v. as_str ( ) )
39+ . ok_or_else ( || AdapterError :: MissingParameter ( "query" . to_string ( ) ) ) ?
40+ . to_string ( )
41+ }
42+ _ => {
43+ // For QueryDSL, keep the full JSON as before
44+ serde_json:: to_string ( & json_body)
45+ . map_err ( |e| AdapterError :: ParseError ( format ! ( "Failed to serialize query: {}" , e) ) ) ?
46+ }
47+ } ;
4148
4249 let time = std:: time:: SystemTime :: now ( )
4350 . duration_since ( std:: time:: UNIX_EPOCH )
4451 . unwrap_or_default ( )
4552 . as_secs_f64 ( ) ;
4653
47- debug ! (
48- "Elasticsearch adapter: parsed {} query with time={}" ,
49- if matches!( self . config. language, QueryLanguage :: elastic_sql) {
50- "SQL"
51- } else {
52- "Query DSL"
53- } ,
54- time
55- ) ;
56-
5754 Ok ( ParsedQueryRequest { query, time } )
5855 }
5956}
@@ -125,35 +122,53 @@ impl QueryRequestAdapter for ElasticHttpAdapter {
125122#[ async_trait]
126123impl QueryResponseAdapter for ElasticHttpAdapter {
127124 async fn format_success_response (
128- & self ,
129- _result : & QueryExecutionResult ,
130- ) -> Result < Response , StatusCode > {
131- debug ! ( "Elasticsearch adapter: formatting success response" ) ;
132-
133- // For now, since we're falling back for every query,
134- // the result from the fallback will be passed through
135- // In the future, this could transform local execution results
136- // to Elasticsearch format
125+ & self ,
126+ result : & QueryExecutionResult ,
127+ ) -> Result < Response , StatusCode > {
128+ info ! ( "SKETCH HIT: serving {} rows from precomputed sketches" ,
129+ match & result. query_result {
130+ QueryResult :: Vector ( v) => v. values. len( ) ,
131+ QueryResult :: Matrix ( _) => 0 ,
132+ } ) ;
133+
134+ let label_names = & result. query_output_labels . labels ;
135+
136+ let hits: Vec < Value > = match & result. query_result {
137+ QueryResult :: Vector ( instant_vector) => {
138+ instant_vector
139+ . values
140+ . iter ( )
141+ . map ( |element| {
142+ let mut source = serde_json:: Map :: new ( ) ;
143+ for ( i, label_name) in label_names. iter ( ) . enumerate ( ) {
144+ let label_value =
145+ element. labels . get ( i) . map ( |s| s. as_str ( ) ) . unwrap_or ( "" ) ;
146+ source. insert ( label_name. clone ( ) , json ! ( label_value) ) ;
147+ }
148+ source. insert ( "value" . to_string ( ) , json ! ( element. value) ) ;
149+ json ! ( {
150+ "_source" : source
151+ } )
152+ } )
153+ . collect ( )
154+ }
155+ QueryResult :: Matrix ( _) => {
156+ return Err ( StatusCode :: NOT_IMPLEMENTED ) ;
157+ }
158+ } ;
137159
138- // Return a stub Elasticsearch-style response for now.
139160 let response = json ! ( {
140161 "took" : 0 ,
141162 "timed_out" : false ,
142163 "hits" : {
143164 "total" : {
144- "value" : 0 ,
165+ "value" : hits . len ( ) ,
145166 "relation" : "eq"
146167 } ,
147- "hits" : [ ]
168+ "hits" : hits
148169 }
149170 } ) ;
150171
151- debug ! (
152- "Elasticsearch adapter: returning stub response: {}" ,
153- serde_json:: to_string_pretty( & response)
154- . unwrap_or_else( |_| "Unable to format" . to_string( ) )
155- ) ;
156-
157172 Ok ( Json ( response) . into_response ( ) )
158173 }
159174
0 commit comments