@@ -24,7 +24,9 @@ use sql::{
24
24
} ;
25
25
26
26
use crate :: handlers:: {
27
- error:: { CreatePlan , InterpreterExec , ParseSql , QueryBlock , QueryTimeout , TooMuchStmt } ,
27
+ error:: {
28
+ CreatePlan , InterpreterExec , ParseInfluxql , ParseSql , QueryBlock , QueryTimeout , TooMuchStmt ,
29
+ } ,
28
30
prelude:: * ,
29
31
} ;
30
32
@@ -104,18 +106,30 @@ impl From<Bytes> for Request {
104
106
}
105
107
}
106
108
107
- pub async fn handle_sql < Q : QueryExecutor + ' static > (
109
+ #[ derive( Debug ) ]
110
+ pub enum QueryType {
111
+ Sql ,
112
+ Influxql ,
113
+ }
114
+
115
+ #[ derive( Debug ) ]
116
+ pub struct QueryRequest {
117
+ pub query_type : QueryType ,
118
+ pub request : Request ,
119
+ }
120
+
121
+ pub async fn handle_query < Q : QueryExecutor + ' static > (
108
122
ctx : & RequestContext ,
109
123
instance : InstanceRef < Q > ,
110
- request : Request ,
124
+ query_request : QueryRequest ,
111
125
) -> Result < Output > {
112
126
let request_id = RequestId :: next_id ( ) ;
113
127
let begin_instant = Instant :: now ( ) ;
114
128
let deadline = ctx. timeout . map ( |t| begin_instant + t) ;
115
129
116
130
info ! (
117
- "sql handler try to process request, request_id:{}, request:{:?}" ,
118
- request_id, request
131
+ "Query handler try to process request, request_id:{}, request:{:?}" ,
132
+ request_id, query_request
119
133
) ;
120
134
121
135
// TODO(yingwen): Privilege check, cannot access data of other tenant
@@ -127,35 +141,67 @@ pub async fn handle_sql<Q: QueryExecutor + 'static>(
127
141
function_registry : & * instance. function_registry ,
128
142
} ;
129
143
let frontend = Frontend :: new ( provider) ;
130
-
131
144
let mut sql_ctx = SqlContext :: new ( request_id, deadline) ;
132
- // Parse sql, frontend error of invalid sql already contains sql
133
- // TODO(yingwen): Maybe move sql from frontend error to outer error
134
- let mut stmts = frontend
135
- . parse_sql ( & mut sql_ctx, & request. query )
136
- . context ( ParseSql ) ?;
137
-
138
- if stmts. is_empty ( ) {
139
- return Ok ( Output :: AffectedRows ( 0 ) ) ;
140
- }
141
145
142
- // TODO(yingwen): For simplicity, we only support executing one statement now
143
- // TODO(yingwen): INSERT/UPDATE/DELETE can be batched
144
- ensure ! (
145
- stmts. len( ) == 1 ,
146
- TooMuchStmt {
147
- len: stmts. len( ) ,
148
- query: request. query,
146
+ let QueryRequest {
147
+ request,
148
+ query_type,
149
+ } = query_request;
150
+ let plan = match query_type {
151
+ QueryType :: Sql => {
152
+ // Parse sql, frontend error of invalid sql already contains sql
153
+ // TODO(yingwen): Maybe move sql from frontend error to outer error
154
+ let mut stmts = frontend
155
+ . parse_sql ( & mut sql_ctx, & request. query )
156
+ . context ( ParseSql ) ?;
157
+
158
+ if stmts. is_empty ( ) {
159
+ return Ok ( Output :: AffectedRows ( 0 ) ) ;
160
+ }
161
+
162
+ // TODO(yingwen): For simplicity, we only support executing one statement now
163
+ // TODO(yingwen): INSERT/UPDATE/DELETE can be batched
164
+ ensure ! (
165
+ stmts. len( ) == 1 ,
166
+ TooMuchStmt {
167
+ len: stmts. len( ) ,
168
+ query: request. query,
169
+ }
170
+ ) ;
171
+
172
+ // Create logical plan
173
+ // Note: Remember to store sql in error when creating logical plan
174
+ frontend
175
+ . statement_to_plan ( & mut sql_ctx, stmts. remove ( 0 ) )
176
+ . context ( CreatePlan {
177
+ query : & request. query ,
178
+ } ) ?
149
179
}
150
- ) ;
151
180
152
- // Create logical plan
153
- // Note: Remember to store sql in error when creating logical plan
154
- let plan = frontend
155
- . statement_to_plan ( & mut sql_ctx, stmts. remove ( 0 ) )
156
- . context ( CreatePlan {
157
- query : & request. query ,
158
- } ) ?;
181
+ QueryType :: Influxql => {
182
+ let mut stmts = frontend
183
+ . parse_influxql ( & mut sql_ctx, & request. query )
184
+ . context ( ParseInfluxql ) ?;
185
+
186
+ if stmts. is_empty ( ) {
187
+ return Ok ( Output :: AffectedRows ( 0 ) ) ;
188
+ }
189
+
190
+ ensure ! (
191
+ stmts. len( ) == 1 ,
192
+ TooMuchStmt {
193
+ len: stmts. len( ) ,
194
+ query: request. query,
195
+ }
196
+ ) ;
197
+
198
+ frontend
199
+ . influxql_stmt_to_plan ( & mut sql_ctx, stmts. remove ( 0 ) )
200
+ . context ( CreatePlan {
201
+ query : & request. query ,
202
+ } ) ?
203
+ }
204
+ } ;
159
205
160
206
instance. limiter . try_limit ( & plan) . context ( QueryBlock {
161
207
query : & request. query ,
@@ -201,7 +247,7 @@ pub async fn handle_sql<Q: QueryExecutor + 'static>(
201
247
} ;
202
248
203
249
info ! (
204
- "sql handler finished, request_id:{}, cost:{}ms, request:{:?}" ,
250
+ "Query handler finished, request_id:{}, cost:{}ms, request:{:?}" ,
205
251
request_id,
206
252
begin_instant. saturating_elapsed( ) . as_millis( ) ,
207
253
request
0 commit comments