@@ -33,23 +33,22 @@ use crate::{
33
33
error:: { ErrNoCause , ErrWithCause , Internal , InternalNoCause , Result } ,
34
34
execute_plan,
35
35
forward:: ForwardResult ,
36
- handlers:: influxdb:: InfluxqlRequest ,
37
36
Proxy ,
38
37
} ;
39
38
40
39
impl < Q : QueryExecutor + ' static > Proxy < Q > {
41
- pub async fn handle_query (
40
+ pub async fn handle_http_sql_query (
42
41
& self ,
43
42
ctx : & RequestContext ,
44
- query_request : QueryRequest ,
43
+ req : Request ,
45
44
) -> Result < Output > {
46
45
let request_id = RequestId :: next_id ( ) ;
47
46
let begin_instant = Instant :: now ( ) ;
48
47
let deadline = ctx. timeout . map ( |t| begin_instant + t) ;
49
48
50
49
info ! (
51
50
"Query handler try to process request, request_id:{}, request:{:?}" ,
52
- request_id, query_request
51
+ request_id, req
53
52
) ;
54
53
55
54
// TODO(yingwen): Privilege check, cannot access data of other tenant
@@ -63,110 +62,66 @@ impl<Q: QueryExecutor + 'static> Proxy<Q> {
63
62
let frontend = Frontend :: new ( provider) ;
64
63
let mut sql_ctx = SqlContext :: new ( request_id, deadline) ;
65
64
66
- let plan = match & query_request {
67
- QueryRequest :: Sql ( request) => {
68
- // Parse sql, frontend error of invalid sql already contains sql
69
- // TODO(yingwen): Maybe move sql from frontend error to outer error
70
- let mut stmts = frontend
71
- . parse_sql ( & mut sql_ctx, & request. query )
72
- . box_err ( )
73
- . with_context ( || ErrWithCause {
74
- code : StatusCode :: BAD_REQUEST ,
75
- msg : format ! ( "Failed to parse sql, query:{}" , request. query) ,
76
- } ) ?;
77
-
78
- if stmts. is_empty ( ) {
79
- return Ok ( Output :: AffectedRows ( 0 ) ) ;
80
- }
81
-
82
- // TODO(yingwen): For simplicity, we only support executing one statement now
83
- // TODO(yingwen): INSERT/UPDATE/DELETE can be batched
84
- ensure ! (
85
- stmts. len( ) == 1 ,
86
- ErrNoCause {
87
- code: StatusCode :: BAD_REQUEST ,
88
- msg: format!(
89
- "Only support execute one statement now, current num:{}, query:{}." ,
90
- stmts. len( ) ,
91
- request. query
92
- ) ,
93
- }
94
- ) ;
95
-
96
- let sql_query_request = SqlQueryRequest {
97
- context : Some ( GrpcRequestContext {
98
- database : ctx. schema . clone ( ) ,
99
- } ) ,
100
- tables : vec ! [ ] ,
101
- sql : request. query . clone ( ) ,
102
- } ;
103
-
104
- if let Some ( resp) = self . maybe_forward_sql_query ( & sql_query_request) . await ? {
105
- match resp {
106
- ForwardResult :: Forwarded ( resp) => {
107
- return convert_sql_response_to_output ( resp?)
108
- }
109
- ForwardResult :: Local => ( ) ,
110
- }
111
- } ;
112
-
113
- // Open partition table if needed.
114
- let table_name = frontend:: parse_table_name ( & stmts) ;
115
- if let Some ( table_name) = table_name {
116
- self . maybe_open_partition_table_if_not_exist (
117
- & ctx. catalog ,
118
- & ctx. schema ,
119
- & table_name,
120
- )
121
- . await ?;
122
- }
123
-
124
- // Create logical plan
125
- // Note: Remember to store sql in error when creating logical plan
126
- frontend
127
- . statement_to_plan ( & mut sql_ctx, stmts. remove ( 0 ) )
128
- . box_err ( )
129
- . with_context ( || ErrWithCause {
130
- code : StatusCode :: BAD_REQUEST ,
131
- msg : format ! ( "Failed to build plan, query:{}" , request. query) ,
132
- } ) ?
65
+ // Parse sql, frontend error of invalid sql already contains sql
66
+ // TODO(yingwen): Maybe move sql from frontend error to outer error
67
+ let mut stmts = frontend
68
+ . parse_sql ( & mut sql_ctx, & req. query )
69
+ . box_err ( )
70
+ . with_context ( || ErrWithCause {
71
+ code : StatusCode :: BAD_REQUEST ,
72
+ msg : format ! ( "Failed to parse sql, query:{}" , req. query) ,
73
+ } ) ?;
74
+
75
+ if stmts. is_empty ( ) {
76
+ return Ok ( Output :: AffectedRows ( 0 ) ) ;
77
+ }
78
+
79
+ // TODO(yingwen): For simplicity, we only support executing one statement now
80
+ // TODO(yingwen): INSERT/UPDATE/DELETE can be batched
81
+ ensure ! (
82
+ stmts. len( ) == 1 ,
83
+ ErrNoCause {
84
+ code: StatusCode :: BAD_REQUEST ,
85
+ msg: format!(
86
+ "Only support execute one statement now, current num:{}, query:{}." ,
87
+ stmts. len( ) ,
88
+ req. query
89
+ ) ,
133
90
}
91
+ ) ;
92
+
93
+ let sql_query_request = SqlQueryRequest {
94
+ context : Some ( GrpcRequestContext {
95
+ database : ctx. schema . clone ( ) ,
96
+ } ) ,
97
+ tables : vec ! [ ] ,
98
+ sql : req. query . clone ( ) ,
99
+ } ;
134
100
135
- QueryRequest :: Influxql ( request) => {
136
- let mut stmts = frontend
137
- . parse_influxql ( & mut sql_ctx, & request. query )
138
- . box_err ( )
139
- . with_context ( || ErrWithCause {
140
- code : StatusCode :: BAD_REQUEST ,
141
- msg : format ! ( "Failed to parse influxql, query:{}" , request. query) ,
142
- } ) ?;
143
-
144
- if stmts. is_empty ( ) {
145
- return Ok ( Output :: AffectedRows ( 0 ) ) ;
146
- }
147
-
148
- ensure ! (
149
- stmts. len( ) == 1 ,
150
- ErrNoCause {
151
- code: StatusCode :: BAD_REQUEST ,
152
- msg: format!(
153
- "Only support execute one statement now, current num:{}, query:{}." ,
154
- stmts. len( ) ,
155
- request. query
156
- ) ,
157
- }
158
- ) ;
159
-
160
- frontend
161
- . influxql_stmt_to_plan ( & mut sql_ctx, stmts. remove ( 0 ) )
162
- . box_err ( )
163
- . with_context ( || ErrWithCause {
164
- code : StatusCode :: BAD_REQUEST ,
165
- msg : format ! ( "Failed to build plan, query:{}" , request. query) ,
166
- } ) ?
101
+ if let Some ( resp) = self . maybe_forward_sql_query ( & sql_query_request) . await ? {
102
+ match resp {
103
+ ForwardResult :: Forwarded ( resp) => return convert_sql_response_to_output ( resp?) ,
104
+ ForwardResult :: Local => ( ) ,
167
105
}
168
106
} ;
169
107
108
+ // Open partition table if needed.
109
+ let table_name = frontend:: parse_table_name ( & stmts) ;
110
+ if let Some ( table_name) = table_name {
111
+ self . maybe_open_partition_table_if_not_exist ( & ctx. catalog , & ctx. schema , & table_name)
112
+ . await ?;
113
+ }
114
+
115
+ // Create logical plan
116
+ // Note: Remember to store sql in error when creating logical plan
117
+ let plan = frontend
118
+ . statement_to_plan ( & mut sql_ctx, stmts. remove ( 0 ) )
119
+ . box_err ( )
120
+ . with_context ( || ErrWithCause {
121
+ code : StatusCode :: BAD_REQUEST ,
122
+ msg : format ! ( "Failed to build plan, query:{}" , req. query) ,
123
+ } ) ?;
124
+
170
125
self . instance
171
126
. limiter
172
127
. try_limit ( & plan)
@@ -189,7 +144,7 @@ impl<Q: QueryExecutor + 'static> Proxy<Q> {
189
144
"Query handler finished, request_id:{}, cost:{}ms, request:{:?}" ,
190
145
request_id,
191
146
begin_instant. saturating_elapsed( ) . as_millis( ) ,
192
- query_request
147
+ req
193
148
) ;
194
149
195
150
Ok ( output)
@@ -259,15 +214,6 @@ impl Serialize for ResponseRows {
259
214
}
260
215
}
261
216
262
- #[ derive( Debug ) ]
263
- pub enum QueryRequest {
264
- Sql ( Request ) ,
265
- // TODO: influxql include more parameters, we should add it in later.
266
- // TODO: remove dead_code after implement influxql with proxy
267
- #[ allow( dead_code) ]
268
- Influxql ( InfluxqlRequest ) ,
269
- }
270
-
271
217
// Convert output to json
272
218
pub fn convert_output ( output : Output ) -> Response {
273
219
match output {
0 commit comments