Repository navigation
Expand file tree
/
Copy pathsql.py
More file actions
473 lines (407 loc) · 15.9 KB
/
Copy pathsql.py
File metadata and controls
473 lines (407 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pyignite.connection import AioConnection, Connection
from pyignite.datatypes import AnyDataArray, AnyDataObject, Bool, Int, Long, Map, Null, String, StructArray
from pyignite.datatypes.sql import StatementType
from pyignite.queries import Query, query_perform
from pyignite.queries.op_codes import (
OP_QUERY_SCAN, OP_QUERY_SCAN_CURSOR_GET_PAGE, OP_QUERY_SQL, OP_QUERY_SQL_CURSOR_GET_PAGE, OP_QUERY_SQL_FIELDS,
OP_QUERY_SQL_FIELDS_CURSOR_GET_PAGE, OP_RESOURCE_CLOSE
)
from pyignite.utils import deprecated
from .result import APIResult
from ..queries.cache_info import CacheInfo
from ..queries.response import SQLResponse
def scan(conn: 'Connection', cache_info: CacheInfo, page_size: int, partitions: int = -1,
local: bool = False) -> APIResult:
"""
Performs scan query.
:param conn: connection to Ignite server,
:param cache_info: cache meta info.
:param page_size: cursor page size,
:param partitions: (optional) number of partitions to query
(negative to query entire cache),
:param local: (optional) pass True if this query should be executed
on local node only. Defaults to False,
:return: API result data object. Contains zero status and a value
of type dict with results on success, non-zero status and an error
description otherwise.
Value dict is of following format:
* `cursor`: int, cursor ID,
* `data`: dict, result rows as key-value pairs,
* `more`: bool, True if more data is available for subsequent
‘scan_cursor_get_page’ calls.
"""
return __scan(conn, cache_info, page_size, partitions, local)
async def scan_async(conn: 'AioConnection', cache_info: CacheInfo, page_size: int, partitions: int = -1,
local: bool = False) -> APIResult:
"""
Async version of scan.
"""
return await __scan(conn, cache_info, page_size, partitions, local)
def __query_result_post_process(result):
if result.status == 0:
result.value = dict(result.value)
return result
def __scan(conn, cache_info, page_size, partitions, local):
query_struct = Query(
OP_QUERY_SCAN,
[
('cache_info', CacheInfo),
('filter', Null),
('page_size', Int),
('partitions', Int),
('local', Bool),
]
)
return query_perform(
query_struct, conn,
query_params={
'cache_info': cache_info,
'filter': None,
'page_size': page_size,
'partitions': partitions,
'local': 1 if local else 0,
},
response_config=[
('cursor', Long),
('data', Map),
('more', Bool),
],
post_process_fun=__query_result_post_process
)
def scan_cursor_get_page(conn: 'Connection', cursor: int) -> APIResult:
"""
Fetches the next scan query cursor page by cursor ID that is obtained
from `scan` function.
:param conn: connection to Ignite server,
:param cursor: cursor ID,
:return: API result data object. Contains zero status and a value
of type dict with results on success, non-zero status and an error
description otherwise.
Value dict is of following format:
* `data`: dict, result rows as key-value pairs,
* `more`: bool, True if more data is available for subsequent
‘scan_cursor_get_page’ calls.
"""
return __scan_cursor_get_page(conn, cursor)
async def scan_cursor_get_page_async(conn: 'AioConnection', cursor: int) -> APIResult:
return await __scan_cursor_get_page(conn, cursor)
def __scan_cursor_get_page(conn, cursor):
query_struct = Query(
OP_QUERY_SCAN_CURSOR_GET_PAGE,
[
('cursor', Long),
]
)
return query_perform(
query_struct, conn,
query_params={
'cursor': cursor,
},
response_config=[
('data', Map),
('more', Bool),
],
post_process_fun=__query_result_post_process
)
@deprecated(version='1.2.0', reason="This API is deprecated and will be removed in the following major release. "
"Use sql_fields instead")
def sql(
conn: 'Connection', cache_info: CacheInfo,
table_name: str, query_str: str, page_size: int, query_args=None,
distributed_joins: bool = False, replicated_only: bool = False,
local: bool = False, timeout: int = 0
) -> APIResult:
"""
Executes an SQL query over data stored in the cluster. The query returns
the whole record (key and value).
:param conn: connection to Ignite server,
:param cache_info: Cache meta info,
:param table_name: name of a type or SQL table,
:param query_str: SQL query string,
:param page_size: cursor page size,
:param query_args: (optional) query arguments,
:param distributed_joins: (optional) distributed joins. Defaults to False,
:param replicated_only: (optional) whether query contains only replicated
tables or not. Defaults to False,
:param local: (optional) pass True if this query should be executed
on local node only. Defaults to False,
:param timeout: (optional) non-negative timeout value in ms. Zero disables
timeout (default),
:return: API result data object. Contains zero status and a value
of type dict with results on success, non-zero status and an error
description otherwise.
Value dict is of following format:
* `cursor`: int, cursor ID,
* `data`: dict, result rows as key-value pairs,
* `more`: bool, True if more data is available for subsequent
‘sql_get_page’ calls.
"""
if query_args is None:
query_args = []
query_struct = Query(
OP_QUERY_SQL,
[
('cache_info', CacheInfo),
('table_name', String),
('query_str', String),
('query_args', AnyDataArray()),
('distributed_joins', Bool),
('local', Bool),
('replicated_only', Bool),
('page_size', Int),
('timeout', Long),
]
)
result = query_struct.perform(
conn,
query_params={
'cache_info': cache_info,
'table_name': table_name,
'query_str': query_str,
'query_args': query_args,
'distributed_joins': 1 if distributed_joins else 0,
'local': 1 if local else 0,
'replicated_only': 1 if replicated_only else 0,
'page_size': page_size,
'timeout': timeout,
},
response_config=[
('cursor', Long),
('data', Map),
('more', Bool),
],
)
if result.status == 0:
result.value = dict(result.value)
return result
@deprecated(version='1.2.0', reason="This API is deprecated and will be removed in the following major release. "
"Use sql_fields instead")
def sql_cursor_get_page(conn: 'Connection', cursor: int) -> APIResult:
"""
Retrieves the next SQL query cursor page by cursor ID from `sql`.
:param conn: connection to Ignite server,
:param cursor: cursor ID,
:return: API result data object. Contains zero status and a value
of type dict with results on success, non-zero status and an error
description otherwise.
Value dict is of following format:
* `data`: dict, result rows as key-value pairs,
* `more`: bool, True if more data is available for subsequent
‘sql_cursor_get_page’ calls.
"""
query_struct = Query(
OP_QUERY_SQL_CURSOR_GET_PAGE,
[
('cursor', Long),
]
)
result = query_struct.perform(
conn,
query_params={
'cursor': cursor,
},
response_config=[
('data', Map),
('more', Bool),
],
)
if result.status == 0:
result.value = dict(result.value)
return result
def sql_fields(
conn: 'Connection', cache_info: CacheInfo,
query_str: str, page_size: int, query_args=None, schema: str = None,
statement_type: int = StatementType.ANY, distributed_joins: bool = False,
local: bool = False, replicated_only: bool = False,
enforce_join_order: bool = False, collocated: bool = False,
lazy: bool = False, include_field_names: bool = False, max_rows: int = -1,
timeout: int = 0
) -> APIResult:
"""
Performs SQL fields query.
:param conn: connection to Ignite server,
:param cache_info: cache meta info.
:param query_str: SQL query string,
:param page_size: cursor page size,
:param query_args: (optional) query arguments. List of values or
(value, type hint) tuples,
:param schema: schema for the query.
:param statement_type: (optional) statement type. Can be:
* StatementType.ALL − any type (default),
* StatementType.SELECT − select,
* StatementType.UPDATE − update.
:param distributed_joins: (optional) distributed joins.
:param local: (optional) pass True if this query should be executed
on local node only.
:param replicated_only: (optional) whether query contains only
replicated tables or not.
:param enforce_join_order: (optional) enforce join order.
:param collocated: (optional) whether your data is co-located or not.
:param lazy: (optional) lazy query execution.
:param include_field_names: (optional) include field names in result.
:param max_rows: (optional) query-wide maximum of rows.
:param timeout: (optional) non-negative timeout value in ms. Zero disables
timeout.
:return: API result data object. Contains zero status and a value
of type dict with results on success, non-zero status and an error
description otherwise.
Value dict is of following format:
* `cursor`: int, cursor ID,
* `data`: list, result values,
* `more`: bool, True if more data is available for subsequent
‘sql_fields_cursor_get_page’ calls.
"""
return __sql_fields(conn, cache_info, query_str, page_size, query_args, schema, statement_type, distributed_joins,
local, replicated_only, enforce_join_order, collocated, lazy, include_field_names, max_rows,
timeout)
async def sql_fields_async(
conn: 'AioConnection', cache_info: CacheInfo,
query_str: str, page_size: int, query_args=None, schema: str = None,
statement_type: int = StatementType.ANY, distributed_joins: bool = False,
local: bool = False, replicated_only: bool = False,
enforce_join_order: bool = False, collocated: bool = False,
lazy: bool = False, include_field_names: bool = False, max_rows: int = -1,
timeout: int = 0
) -> APIResult:
"""
Async version of sql_fields.
"""
return await __sql_fields(conn, cache_info, query_str, page_size, query_args, schema, statement_type,
distributed_joins, local, replicated_only, enforce_join_order, collocated, lazy,
include_field_names, max_rows, timeout)
def __sql_fields(
conn, cache_info, query_str, page_size, query_args, schema, statement_type, distributed_joins, local,
replicated_only, enforce_join_order, collocated, lazy, include_field_names, max_rows, timeout
):
if query_args is None:
query_args = []
query_struct = Query(
OP_QUERY_SQL_FIELDS,
[
('cache_info', CacheInfo),
('schema', String),
('page_size', Int),
('max_rows', Int),
('query_str', String),
('query_args', AnyDataArray()),
('statement_type', StatementType),
('distributed_joins', Bool),
('local', Bool),
('replicated_only', Bool),
('enforce_join_order', Bool),
('collocated', Bool),
('lazy', Bool),
('timeout', Long),
('include_field_names', Bool),
],
response_type=SQLResponse
)
return query_perform(
query_struct, conn,
query_params={
'cache_info': cache_info,
'schema': schema,
'page_size': page_size,
'max_rows': max_rows,
'query_str': query_str,
'query_args': query_args,
'statement_type': statement_type,
'distributed_joins': distributed_joins,
'local': local,
'replicated_only': replicated_only,
'enforce_join_order': enforce_join_order,
'collocated': collocated,
'lazy': lazy,
'timeout': timeout,
'include_field_names': include_field_names,
},
include_field_names=include_field_names,
has_cursor=True,
)
def sql_fields_cursor_get_page(conn: 'Connection', cursor: int, field_count: int) -> APIResult:
"""
Retrieves the next query result page by cursor ID from `sql_fields`.
:param conn: connection to Ignite server,
:param cursor: cursor ID,
:param field_count: a number of fields in a row,
:return: API result data object. Contains zero status and a value
of type dict with results on success, non-zero status and an error
description otherwise.
Value dict is of following format:
* `data`: list, result values,
* `more`: bool, True if more data is available for subsequent
‘sql_fields_cursor_get_page’ calls.
"""
return __sql_fields_cursor_get_page(conn, cursor, field_count)
async def sql_fields_cursor_get_page_async(conn: 'AioConnection', cursor: int, field_count: int) -> APIResult:
"""
Async version sql_fields_cursor_get_page.
"""
return await __sql_fields_cursor_get_page(conn, cursor, field_count)
def __sql_fields_cursor_get_page(conn, cursor, field_count):
query_struct = Query(
OP_QUERY_SQL_FIELDS_CURSOR_GET_PAGE,
[
('cursor', Long),
]
)
return query_perform(
query_struct, conn,
query_params={
'cursor': cursor,
},
response_config=[
('data', StructArray([(f'field_{i}', AnyDataObject) for i in range(field_count)])),
('more', Bool),
],
post_process_fun=__post_process_sql_fields_cursor
)
def __post_process_sql_fields_cursor(result):
if result.status != 0:
return result
value = result.value
result.value = {
'data': [],
'more': value['more']
}
for row_dict in value['data']:
result.value['data'].append(list(row_dict.values()))
return result
def resource_close(conn: 'Connection', cursor: int) -> APIResult:
"""
Closes a resource, such as query cursor.
:param conn: connection to Ignite server,
:param cursor: cursor ID,
:return: API result data object. Contains zero status on success,
non-zero status and an error description otherwise.
"""
return __resource_close(conn, cursor)
async def resource_close_async(conn: 'AioConnection', cursor: int) -> APIResult:
return await __resource_close(conn, cursor)
def __resource_close(conn, cursor):
query_struct = Query(
OP_RESOURCE_CLOSE,
[
('cursor', Long),
]
)
return query_perform(
query_struct, conn,
query_params={
'cursor': cursor,
}
)