 
 
 
-(defn query [service query {:keys [max-results dry-run? max-wait-millis use-cache? default-dataset] :as query-opts}]
+(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]




(defn job [service {:keys [project-id job-id] :as job}]
  (to-clojure (.getJob service (JobId/of project-id job-id) (into-array BigQuery$JobOption []))))

(def create-dispositions {:needed JobInfo$CreateDisposition/CREATE_IF_NEEDED
                          :never  JobInfo$CreateDisposition/CREATE_NEVER})

  Schema
  (to-clojure [x] (map to-clojure (.fields x)))
  QueryResponse
  (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)
