@@ -423,43 +423,15 @@ def process(self, http_response):
423
423
)
424
424
425
425
426
- class PrestoResult (object ):
426
+ class PrestoQuery (object ):
427
427
"""
428
- Represent the result of a Presto query as an iterator on rows .
428
+ Represent the execution of a SQL statement by Presto .
429
429
430
- This class implements the iterator protocol as a generator type
430
+ Results of the query can be extracted by iterating over this class, since it
431
+ implements the iterator protocol as a generator type
431
432
https://docs.python.org/3/library/stdtypes.html#generator-types
432
433
"""
433
434
434
- def __init__ (self , query , rows = None ):
435
- self ._query = query
436
- self ._rows = rows or []
437
- self ._rownumber = 0
438
-
439
- @property
440
- def rownumber (self ):
441
- # type: () -> int
442
- return self ._rownumber
443
-
444
- def __iter__ (self ):
445
- # Initial fetch from the first POST request
446
- for row in self ._rows :
447
- self ._rownumber += 1
448
- yield row
449
- self ._rows = None
450
-
451
- # Subsequent fetches from GET requests until next_uri is empty.
452
- while not self ._query .is_finished ():
453
- rows = self ._query .fetch ()
454
- for row in rows :
455
- self ._rownumber += 1
456
- logger .debug ("row {}" .format (row ))
457
- yield row
458
-
459
-
460
- class PrestoQuery (object ):
461
- """Represent the execution of a SQL statement by Presto."""
462
-
463
435
def __init__ (
464
436
self ,
465
437
request , # type: PrestoRequest
@@ -476,7 +448,9 @@ def __init__(
476
448
self ._cancelled = False
477
449
self ._request = request
478
450
self ._sql = sql
479
- self ._result = PrestoResult (self )
451
+
452
+ self ._rows = []
453
+ self ._rownumber = 0
480
454
481
455
@property
482
456
def columns (self ):
@@ -490,10 +464,6 @@ def stats(self):
490
464
def warnings (self ):
491
465
return self ._warnings
492
466
493
- @property
494
- def result (self ):
495
- return self ._result
496
-
497
467
def execute (self ):
498
468
# type: () -> PrestoResult
499
469
"""Initiate a Presto query by sending the SQL statement
@@ -514,10 +484,10 @@ def execute(self):
514
484
self ._warnings = getattr (status , "warnings" , [])
515
485
if status .next_uri is None :
516
486
self ._finished = True
517
- self ._result = PrestoResult ( self , status .rows )
518
- return self . _result
487
+ self ._rows = status .rows
488
+ return self
519
489
520
- def fetch (self ):
490
+ def _fetch (self ):
521
491
# type: () -> List[List[Any]]
522
492
"""Continue fetching data for the current query_id"""
523
493
response = self ._request .get (self ._request .next_uri )
@@ -530,6 +500,14 @@ def fetch(self):
530
500
self ._finished = True
531
501
return status .rows
532
502
503
+ def poll (self ):
504
+ # type: () -> Dict
505
+ """Retrieve the current status of a presto query, caching any results."""
506
+ if not self .query_id or self ._finished :
507
+ return self .stats
508
+ self ._rows .extend (self ._fetch ())
509
+ return self .stats
510
+
533
511
def cancel (self ):
534
512
# type: () -> None
535
513
"""Cancel the current query"""
@@ -549,3 +527,12 @@ def cancel(self):
549
527
def is_finished (self ):
550
528
# type: () -> bool
551
529
return self ._finished
530
+
531
+ def __iter__ (self ):
532
+ while self ._rows or not self .is_finished ():
533
+ for row in self ._rows :
534
+ self ._rownumber += 1
535
+ logger .debug ('row {}' .format (row ))
536
+ yield row
537
+ self ._rows = []
538
+ self .poll ()
0 commit comments