   Schema
   (to-clojure [x] (map to-clojure (.fields x)))
   QueryResponse
-  (to-clojure [x] {:completed? (.jobCompleted x)
-                   :errors     (->> (.executionErrors x) (map to-clojure) (seq))
-                   :job-id     (to-clojure (.jobId x))
-                   :results    (map (fn [fields] (map to-clojure fields))
-                                    (iterator-seq (.. x result iterateAll)))
-                   :schema     (to-clojure (.. x result schema))
-                   :cache-hit  (.. x result cacheHit)})
+  (to-clojure [x] (let [completed (.jobCompleted x)]
+                    {:completed? completed
+                     :errors     (->> (.executionErrors x) (map to-clojure) (seq))
+                     :job-id     (to-clojure (.jobId x))
+                     :results    (when completed
+                                   (map (fn [fields] (map to-clojure fields))
+                                        (iterator-seq (.. x result iterateAll))))
+                     :schema     (when completed
+                                   (to-clojure (.. x result schema)))
+                     :cache-hit  (when completed
+                                   (.. x result cacheHit))}))
   JobStatistics$LoadStatistics
   (to-clojure [x] {:input-bytes  (.inputBytes x)
                    :input-files  (.inputFiles x)

(ns gclouj.bigquery
  (:require [clojure.walk :as walk])
  (:import [com.google.cloud.bigquery BigQueryOptions BigQuery$DatasetListOption DatasetInfo DatasetId BigQuery$TableListOption StandardTableDefinition TableId BigQuery$DatasetOption BigQuery$TableOption Schema Field Field$Type Field$Mode StandardTableDefinition$StreamingBuffer InsertAllRequest InsertAllRequest$RowToInsert InsertAllResponse BigQueryError BigQuery$DatasetDeleteOption QueryRequest QueryResponse QueryResult JobId Field Field$Type$Value FieldValue FieldValue$Attribute LoadConfiguration BigQuery$JobOption JobInfo$CreateDisposition JobInfo$WriteDisposition JobStatistics JobStatistics$LoadStatistics JobStatus JobStatus$State FormatOptions UserDefinedFunction JobInfo LoadJobConfiguration QueryJobConfiguration QueryJobConfiguration$Priority Table]
           [com.google.common.hash Hashing]
           [java.util List Collections]))





(defn query
  "Executes a query. BigQuery will create a Query Job and block for the
  specified timeout. If the query returns within the time the results
  will be returned. Otherwise, results need to be retrieved separately
  using query-results. Status of the job can be checked using the job
  function, and checking completed?"
  [service query {:keys [max-results dry-run? max-wait-millis use-cache? default-dataset] :as query-opts}]
  (let [builder (QueryRequest/builder query)]
    (when default-dataset
      (let [{:keys [project-id dataset-id]} default-dataset]
