Class: Olaf::BigQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/olaf/drivers/big_query.rb

Constant Summary collapse

GIGABYTE =
1024**3
DEFAULT_MAXIMUM_BYTES_BILLED =

Restrictive on purpose: a query that scans more says so with driver :big_query, maximum_bytes_billed: ....

GIGABYTE
LABEL_FORMAT =

Labels only accept lowercase letters, numbers, dashes and underscores.

/[^a-z0-9_-]/.freeze
LABEL_MAX_LENGTH =
63

Instance Method Summary collapse

Constructor Details

#initialize(project: nil, credentials: nil, maximum_bytes_billed: DEFAULT_MAXIMUM_BYTES_BILLED, labels: {}, client: nil) ⇒ BigQuery

Credentials default to the ambient Google ones, and the client is built from the arguments unless one is given.



22
23
24
25
26
27
28
29
# File 'lib/olaf/drivers/big_query.rb', line 22

def initialize(project: nil, credentials: nil, maximum_bytes_billed: DEFAULT_MAXIMUM_BYTES_BILLED,
               labels: {}, client: nil)
  @project = project
  @credentials = credentials
  @maximum_bytes_billed = maximum_bytes_billed
  @labels = labels
  @client = client
end

Instance Method Details

#fetch(olaf_query) ⇒ Object



31
32
33
# File 'lib/olaf/drivers/big_query.rb', line 31

def fetch(olaf_query)
  run(olaf_query).data.all.to_a
end

#run(olaf_query) ⇒ Object

Returns the finished job, so callers can read its stats (bytes billed, cache hit) on top of its rows.

@raises Olaf::QueryExecutionError


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/olaf/drivers/big_query.rb', line 39

def run(olaf_query)
  job = client.query_job(
    sql(olaf_query),
    params: params(olaf_query),
    maximum_bytes_billed: maximum_bytes_billed(olaf_query),
    labels: labels(olaf_query)
  )
  job.wait_until_done!

  raise QueryExecutionError.new(job.error.to_h['message'].to_s, olaf_query) if job.failed?

  job
rescue *wrapped_errors => error
  raise QueryExecutionError.new(error.message, olaf_query)
end