Skip to content

Commit 39dfc97

Browse files
committed
Make wait() method wait for query execution
With Impala the `\ThriftSQL\ImpalaQuery` object is ready as soon as the query is parsed / accepted to match up with `wait()` method with Hive fetch up to the first 2 rows during the `wait()` method so that the query gets executed.
1 parent e01d929 commit 39dfc97

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

ThriftSQL.phar

-28.3 KB
Binary file not shown.

src/ThriftSQL/HiveQuery.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public function __construct( $response, $client ) {
1515
}
1616

1717
public function wait() {
18+
if ( $this->_ready ) {
19+
return $this;
20+
}
21+
1822
// Wait for results
1923
$sleeper = new \ThriftSQL\Utils\Sleeper();
2024
$sleeper->reset();

src/ThriftSQL/ImpalaQuery.php

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class ImpalaQuery implements \ThriftSQLQuery {
77
private $_handle;
88
private $_client;
99
private $_ready;
10+
private $_lastResponse;
1011

1112
public function __construct( $queryStr, $client ) {
1213
$queryCleaner = new \ThriftSQL\Utils\QueryCleaner();
@@ -19,14 +20,15 @@ public function __construct( $queryStr, $client ) {
1920
}
2021

2122
public function wait() {
22-
$sleeper = new \ThriftSQL\Utils\Sleeper();
23+
if ( $this->_ready ) {
24+
return $this;
25+
}
2326

24-
// Wait for results
27+
// Wait for query to be ready
28+
$sleeper = new \ThriftSQL\Utils\Sleeper();
2529
$sleeper->reset();
2630
do {
27-
2831
$slept = $sleeper->sleep()->getSleptSecs();
29-
3032
if ( $slept > 18000 ) { // 5 Hours
3133
// TODO: Actually kill the query then throw exception.
3234
throw new \ThriftSQL\Exception( 'Impala Query Killed!' );
@@ -51,35 +53,47 @@ public function wait() {
5153

5254
} while (true);
5355

56+
// Wait for results by fetching some rows -- triggers query to run
57+
$this->_fetchResponse( 2 );
58+
5459
$this->_ready = true;
5560
return $this;
5661
}
5762

5863
public function fetch( $maxRows ) {
64+
$result = array();
5965
if ( !$this->_ready ) {
6066
throw new \ThriftSQL\Exception( "Query is not ready. Call `->wait()` before `->fetch()`" );
6167
}
6268
try {
63-
$sleeper = new \ThriftSQL\Utils\Sleeper();
64-
$sleeper->reset();
65-
66-
do {
67-
$response = $this->_client->fetch( $this->_handle, false, $maxRows );
68-
if ( $response->ready ) {
69-
break;
70-
}
71-
$slept = $sleeper->sleep()->getSleptSecs();
72-
73-
if ( $slept > 60 ) { // 1 minute
74-
throw new \ThriftSQL\Exception( 'Impala Query took too long to fetch!' );
75-
}
76-
77-
} while ( true );
69+
if ( !( $this->_lastResponse instanceof \ThriftSQL\Results ) ) {
70+
$this->_fetchResponse( $maxRows );
71+
}
7872

79-
return $this->_parseResponse( $response );
73+
$result = $this->_parseResponse( $this->_lastResponse );
74+
$this->_lastResponse = null;
8075
} catch( Exception $e ) {
8176
throw new \ThriftSQL\Exception( $e->getMessage() );
8277
}
78+
79+
return $result;
80+
}
81+
82+
private function _fetchResponse( $maxRows ) {
83+
$sleeper = new \ThriftSQL\Utils\Sleeper();
84+
$sleeper->reset();
85+
86+
do {
87+
$this->_lastResponse = $this->_client->fetch( $this->_handle, false, $maxRows );
88+
if ( $this->_lastResponse->ready ) {
89+
return;
90+
}
91+
92+
if ( $sleeper->sleep()->getSleptSecs() > 60 ) { // 1 minute
93+
throw new \ThriftSQL\Exception( 'Impala Query took too long to fetch!' );
94+
}
95+
96+
} while ( true );
8397
}
8498

8599
private function _parseResponse( $response ) {

0 commit comments

Comments
 (0)