@@ -59,7 +59,6 @@ public BigQueryStatement(BigQueryConnection bigQueryConnection)
5959
6060 private GoogleCredential Credential => this . bigQueryConnection . Credential ?? throw new AdbcException ( "Credential cannot be null" ) ;
6161
62-
6362 public override QueryResult ExecuteQuery ( )
6463 {
6564 //Func<Task<QueryResult>> func = () => ExecuteQueryInternalAsync();
@@ -74,6 +73,8 @@ private async Task<QueryResult> ExecuteQueryInternalAsync()
7473
7574 BigQueryJob job = await this . Client . CreateQueryJobAsync ( SqlQuery , null , queryOptions ) ;
7675
76+ JobReference jobReference = job . Reference ;
77+
7778 GetQueryResultsOptions getQueryResultsOptions = new GetQueryResultsOptions ( ) ;
7879
7980 if ( this . Options ? . TryGetValue ( BigQueryParameters . GetQueryResultsOptionsTimeout , out string ? timeoutSeconds ) == true &&
@@ -83,16 +84,13 @@ private async Task<QueryResult> ExecuteQueryInternalAsync()
8384 getQueryResultsOptions . Timeout = TimeSpan . FromSeconds ( seconds ) ;
8485 }
8586
86- Task < BigQueryResults > resultsTask = job . GetQueryResultsAsync ( getQueryResultsOptions ) ;
87-
8887 DateTime start = DateTime . Now ;
8988
90- Func < Task < bool > > func = ( ) => Task . Run ( ( ) =>
89+ Func < Task < BigQueryJob > > checkJobStatus = async ( ) =>
9190 {
9291 while ( true )
9392 {
94- var jobWithStatus = this . Client . GetJob ( job . Reference ) ;
95- Debug . WriteLine ( $ "Job state is { jobWithStatus . State } ") ;
93+ var jobWithStatus = await this . Client . GetJobAsync ( jobReference ) ;
9694
9795 if ( jobWithStatus . State == JobState . Done )
9896 {
@@ -104,49 +102,57 @@ private async Task<QueryResult> ExecuteQueryInternalAsync()
104102 DateTime end = DateTime . Now ;
105103 TimeSpan duration = end - start ;
106104 Debug . WriteLine ( $ "Done at { end . ToString ( ) } after { duration . TotalMinutes } ") ;
107- return true ;
105+ return jobWithStatus ;
108106 }
109107 }
110- } ) ;
108+ } ;
111109
112110 Debug . WriteLine ( $ "Starting ExecuteWithRetriesAsync at { start . ToString ( ) } ") ;
113111
114- await AdbcRetryManager . ExecuteWithRetriesAsync < bool > ( this , func ) ;
112+ await AdbcRetryManager . ExecuteWithRetriesAsync < BigQueryJob > ( this , checkJobStatus ) ;
115113
116114 Debug . WriteLine ( $ "Getting results at { DateTime . Now . ToString ( ) } ") ;
117115
118- BigQueryResults results = resultsTask . Result ;
116+ Func < Task < BigQueryResults > > getJobResults = async ( ) =>
117+ {
118+ // if the authentication token was reset, then we need a new job with the latest token
119+ BigQueryJob completedJob = await this . Client . GetJobAsync ( jobReference ) ;
120+ return await completedJob . GetQueryResultsAsync ( ) ;
121+ } ;
122+
123+ BigQueryResults results = await AdbcRetryManager . ExecuteWithRetriesAsync ( this , getJobResults ) ;
119124
120125 Debug . WriteLine ( $ "Results received at { DateTime . Now . ToString ( ) } ") ;
121126
122127 TokenProtectedReadClientManger clientMgr = new TokenProtectedReadClientManger ( this . Credential ) ;
123128 clientMgr . UpdateToken = ( ) => Task . Run ( ( ) =>
124129 {
130+ Debug . WriteLine ( $ "TokenProtectedReadClientManger updating token at { DateTime . Now . ToString ( ) } ") ;
125131 this . bigQueryConnection . SetCredential ( ) ;
126132 clientMgr . UpdateCredential ( this . Credential ) ;
127133 } ) ;
128134
129- if ( results . TableReference == null )
135+ if ( results ? . TableReference == null )
130136 {
131137 // To get the results of all statements in a multi-statement query, enumerate the child jobs and call jobs.getQueryResults on each of them.
132138 // Related public docs: https://cloud.google.com/bigquery/docs/multi-statement-queries#get_all_executed_statements
133139 ListJobsOptions listJobsOptions = new ListJobsOptions ( ) ;
134- listJobsOptions . ParentJobId = results . JobReference . JobId ;
135- PagedEnumerable < JobList , BigQueryJob > joblist = this . Client . ListJobs ( listJobsOptions ) ;
140+ listJobsOptions . ParentJobId = results ? . JobReference . JobId ;
141+ PagedAsyncEnumerable < JobList , BigQueryJob > joblist = this . Client . ListJobsAsync ( listJobsOptions ) ;
136142 BigQueryJob firstQueryJob = new BigQueryJob ( this . Client , job . Resource ) ;
137- foreach ( BigQueryJob childJob in joblist )
143+ await foreach ( BigQueryJob childJob in joblist )
138144 {
139- var tempJob = this . Client . GetJob ( childJob . Reference ) ;
145+ var tempJob = await this . Client . GetJobAsync ( childJob . Reference ) ;
140146 var query = tempJob . Resource ? . Configuration ? . Query ;
141147 if ( query != null && query . DestinationTable != null && query . DestinationTable . ProjectId != null && query . DestinationTable . DatasetId != null && query . DestinationTable . TableId != null )
142148 {
143149 firstQueryJob = tempJob ;
144150 }
145151 }
146- results = firstQueryJob . GetQueryResults ( ) ;
152+ results = await firstQueryJob . GetQueryResultsAsync ( ) ;
147153 }
148154
149- if ( results . TableReference == null )
155+ if ( results ? . TableReference == null )
150156 {
151157 throw new AdbcException ( "There is no query statement" ) ;
152158 }
@@ -156,6 +162,7 @@ private async Task<QueryResult> ExecuteQueryInternalAsync()
156162 string table = $ "projects/{ results . TableReference . ProjectId } /datasets/{ results . TableReference . DatasetId } /tables/{ results . TableReference . TableId } ";
157163
158164 int maxStreamCount = 1 ;
165+
159166 if ( this . Options ? . TryGetValue ( BigQueryParameters . MaxFetchConcurrency , out string ? maxStreamCountString ) == true )
160167 {
161168 if ( int . TryParse ( maxStreamCountString , out int count ) )
@@ -166,28 +173,34 @@ private async Task<QueryResult> ExecuteQueryInternalAsync()
166173 }
167174 }
168175 }
176+
169177 ReadSession rs = new ReadSession { Table = table , DataFormat = DataFormat . Arrow } ;
170178 ReadSession rrs = clientMgr . ReadClient . CreateReadSession ( "projects/" + results . TableReference . ProjectId , rs , maxStreamCount ) ;
171179
172180 long totalRows = results . TotalRows == null ? - 1L : ( long ) results . TotalRows . Value ;
173181
182+ Debug . WriteLine ( $ "Starting to read streams at { DateTime . Now . ToString ( ) } ") ;
183+
174184 var readers = rrs . Streams
175185 . Select ( s => ReadChunkWithRetries ( clientMgr , s . Name ) )
176186 . Where ( chunk => chunk != null )
177187 . Cast < IArrowReader > ( ) ;
178188
189+ Debug . WriteLine ( $ "Creating Arrow stream at { DateTime . Now . ToString ( ) } ") ;
190+
179191 IArrowArrayStream stream = new MultiArrowReader ( TranslateSchema ( results . Schema ) , readers ) ;
180192
193+ Debug . WriteLine ( $ "Returning results at { DateTime . Now . ToString ( ) } ") ;
194+
181195 return new QueryResult ( totalRows , stream ) ;
182196 }
183197
184198 public override UpdateResult ExecuteUpdate ( )
185199 {
186- Func < Task < UpdateResult > > func = ( ) => Task . Run ( ( ) => ExecuteUpdateInternal ( ) ) ;
187- return AdbcRetryManager . ExecuteWithRetriesAsync < UpdateResult > ( this , func ) . Result ;
200+ return ExecuteUpdateInternalAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
188201 }
189202
190- private UpdateResult ExecuteUpdateInternal ( )
203+ private async Task < UpdateResult > ExecuteUpdateInternalAsync ( )
191204 {
192205 QueryOptions options = ValidateOptions ( ) ;
193206 GetQueryResultsOptions getQueryResultsOptions = new GetQueryResultsOptions ( ) ;
@@ -199,7 +212,7 @@ private UpdateResult ExecuteUpdateInternal()
199212 getQueryResultsOptions . Timeout = TimeSpan . FromSeconds ( seconds ) ;
200213 }
201214
202- BigQueryResults result = this . Client . ExecuteQuery (
215+ BigQueryResults result = await this . Client . ExecuteQueryAsync (
203216 SqlQuery ,
204217 parameters : null ,
205218 queryOptions : options ,
0 commit comments