Class: Google::Cloud::Bigquery::QueryJob
- Defined in:
- lib/google/cloud/bigquery/query_job.rb
Overview
QueryJob
A Job subclass representing a query operation that may be performed on a Table. A QueryJob instance is created when you call Project#query_job, Dataset#query_job.
Direct Known Subclasses
Defined Under Namespace
Attributes collapse
-
#clustering? ⇒ Boolean
Checks if the destination table will be clustered.
-
#clustering_fields ⇒ Array<String>?
One or more fields on which the destination table should be clustered.
-
#data(token: nil, max: nil, start: nil) ⇒ Google::Cloud::Bigquery::Data
(also: #query_results)
Retrieves the query results for the job.
-
#encryption ⇒ Google::Cloud::BigQuery::EncryptionConfiguration
The encryption configuration of the destination table.
-
#range_partitioning? ⇒ Boolean
Checks if the destination table will be range partitioned.
-
#range_partitioning_end ⇒ Integer?
The end of range partitioning, exclusive.
-
#range_partitioning_field ⇒ String?
The field on which the destination table will be range partitioned, if any.
-
#range_partitioning_interval ⇒ Integer?
The width of each interval.
-
#range_partitioning_start ⇒ Integer?
The start of range partitioning, inclusive.
-
#time_partitioning? ⇒ Boolean
Checks if the destination table will be time-partitioned.
-
#time_partitioning_expiration ⇒ Integer?
The expiration for the destination table partitions, if any, in seconds.
-
#time_partitioning_field ⇒ String?
The field on which the destination table will be partitioned, if any.
-
#time_partitioning_require_filter? ⇒ Boolean
If set to true, queries over the destination table will require a partition filter that can be used for partition elimination to be specified.
-
#time_partitioning_type ⇒ String?
The period for which the destination table will be partitioned, if any.
-
#wait_until_done! ⇒ Object
Refreshes the job until the job is
DONE
.
Instance Method Summary collapse
-
#batch? ⇒ Boolean
Checks if the priority for the query is
BATCH
. -
#bytes_processed ⇒ Integer?
The number of bytes processed by the query.
-
#cache? ⇒ Boolean
Checks if the query job looks for an existing result in the query cache.
-
#cache_hit? ⇒ Boolean
Checks if the query results are from the query cache.
-
#ddl? ⇒ Boolean
Whether the query is a DDL statement.
-
#ddl_operation_performed ⇒ String?
The DDL operation performed, possibly dependent on the pre-existence of the DDL target.
-
#ddl_target_routine ⇒ Google::Cloud::Bigquery::Routine?
The DDL target routine, in reference state.
-
#ddl_target_table ⇒ Google::Cloud::Bigquery::Table?
The DDL target table, in reference state.
-
#deleted_row_count ⇒ Integer?
The number of deleted rows.
-
#destination(view: nil) ⇒ Table
The table in which the query results are stored.
-
#dml? ⇒ Boolean
Whether the query is a DML statement.
-
#dryrun? ⇒ Boolean
(also: #dryrun, #dry_run, #dry_run?)
If set, don't actually run this job.
-
#flatten? ⇒ Boolean
Checks if the query job flattens nested and repeated fields in the query results.
-
#inserted_row_count ⇒ Integer?
The number of inserted rows.
-
#interactive? ⇒ Boolean
Checks if the priority for the query is
INTERACTIVE
. -
#large_results? ⇒ Boolean
Checks if the the query job allows arbitrarily large results at a slight cost to performance.
-
#legacy_sql? ⇒ Boolean
Checks if the query job is using legacy sql.
-
#maximum_billing_tier ⇒ Integer?
Limits the billing tier for this job.
-
#maximum_bytes_billed ⇒ Integer?
Limits the bytes billed for this job.
-
#num_dml_affected_rows ⇒ Integer?
The number of rows affected by a DML statement.
-
#query_plan ⇒ Array<Google::Cloud::Bigquery::QueryJob::Stage>?
Describes the execution plan for the query.
-
#standard_sql? ⇒ Boolean
Checks if the query job is using standard sql.
-
#statement_type ⇒ String?
The type of query statement, if valid.
-
#udfs ⇒ Array<String>
The user-defined function resources used in the query.
-
#updated_row_count ⇒ Integer?
The number of updated rows.
Methods inherited from Job
#cancel, #configuration, #created_at, #delete, #done?, #ended_at, #error, #errors, #failed?, #job_id, #labels, #location, #num_child_jobs, #parent_job_id, #pending?, #project_id, #reload!, #rerun!, #reservation_usage, #running?, #script_statistics, #session_id, #started_at, #state, #statistics, #status, #transaction_id, #user_email
Instance Method Details
#batch? ⇒ Boolean
Checks if the priority for the query is BATCH
.
96 97 98 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 96 def batch? @gapi.configuration.query.priority == "BATCH" end |
#bytes_processed ⇒ Integer?
The number of bytes processed by the query.
213 214 215 216 217 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 213 def bytes_processed Integer @gapi.statistics.query.total_bytes_processed rescue StandardError nil end |
#cache? ⇒ Boolean
Checks if the query job looks for an existing result in the query cache. For more information, see Query Caching.
133 134 135 136 137 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 133 def cache? val = @gapi.configuration.query.use_query_cache return false if val.nil? val end |
#cache_hit? ⇒ Boolean
Checks if the query results are from the query cache.
203 204 205 206 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 203 def cache_hit? return false unless @gapi.statistics.query @gapi.statistics.query.cache_hit end |
#clustering? ⇒ Boolean
Checks if the destination table will be clustered.
See Google::Cloud::Bigquery::QueryJob::Updater#clustering_fields=, Table#clustering_fields and Table#clustering_fields=.
660 661 662 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 660 def clustering? !@gapi.configuration.query.clustering.nil? end |
#clustering_fields ⇒ Array<String>?
One or more fields on which the destination table should be clustered. Must be specified with time-based partitioning, data in the table will be first partitioned and subsequently clustered. The order of the returned fields determines the sort order of the data.
BigQuery supports clustering for both partitioned and non-partitioned tables.
See Google::Cloud::Bigquery::QueryJob::Updater#clustering_fields=, Table#clustering_fields and Table#clustering_fields=.
686 687 688 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 686 def clustering_fields @gapi.configuration.query.clustering.fields if clustering? end |
#data(token: nil, max: nil, start: nil) ⇒ Google::Cloud::Bigquery::Data Also known as: query_results
Retrieves the query results for the job.
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 748 def data token: nil, max: nil, start: nil return nil unless done? return Data.from_gapi_json({ rows: [] }, nil, @gapi, service) if dryrun? if ddl? || dml? || !ensure_schema! data_hash = { totalRows: nil, rows: [] } return Data.from_gapi_json data_hash, nil, @gapi, service end data_hash = service.list_tabledata destination_table_dataset_id, destination_table_table_id, token: token, max: max, start: start Data.from_gapi_json data_hash, destination_table_gapi, @gapi, service end |
#ddl? ⇒ Boolean
Whether the query is a DDL statement.
299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 299 def ddl? [ "ALTER_TABLE", "CREATE_MODEL", "CREATE_TABLE", "CREATE_TABLE_AS_SELECT", "CREATE_VIEW", "DROP_MODEL", "DROP_TABLE", "DROP_VIEW" ].include? statement_type end |
#ddl_operation_performed ⇒ String?
The DDL operation performed, possibly dependent on the pre-existence of the DDL target. (See #ddl_target_table.) Possible values (new values might be added in the future):
- "CREATE": The query created the DDL target.
- "SKIP": No-op. Example cases: the query is
CREATE TABLE IF NOT EXISTS
while the table already exists, or the query isDROP TABLE IF EXISTS
while the table does not exist. - "REPLACE": The query replaced the DDL target. Example case: the
query is
CREATE OR REPLACE TABLE
, and the table already exists. - "DROP": The query deleted the DDL target.
355 356 357 358 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 355 def ddl_operation_performed return nil unless @gapi.statistics.query @gapi.statistics.query.ddl_operation_performed end |
#ddl_target_routine ⇒ Google::Cloud::Bigquery::Routine?
The DDL target routine, in reference state. (See Routine#reference?.)
Present only for CREATE/DROP FUNCTION/PROCEDURE
queries. (See
#statement_type.)
368 369 370 371 372 373 374 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 368 def ddl_target_routine return nil unless @gapi.statistics.query ensure_service! routine = @gapi.statistics.query.ddl_target_routine return nil unless routine Google::Cloud::Bigquery::Routine.new_reference_from_gapi routine, service end |
#ddl_target_table ⇒ Google::Cloud::Bigquery::Table?
The DDL target table, in reference state. (See Table#reference?.)
Present only for CREATE/DROP TABLE/VIEW
queries. (See
#statement_type.)
384 385 386 387 388 389 390 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 384 def ddl_target_table return nil unless @gapi.statistics.query ensure_service! table = @gapi.statistics.query.ddl_target_table return nil unless table Google::Cloud::Bigquery::Table.new_reference_from_gapi table, service end |
#deleted_row_count ⇒ Integer?
The number of deleted rows. Present only for DML statements DELETE
,
MERGE
and TRUNCATE
. (See #statement_type.)
411 412 413 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 411 def deleted_row_count @gapi.statistics.query&.dml_stats&.deleted_row_count end |
#destination(view: nil) ⇒ Table
The table in which the query results are stored.
448 449 450 451 452 453 454 455 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 448 def destination view: nil table = @gapi.configuration.query.destination_table return nil unless table retrieve_table table.project_id, table.dataset_id, table.table_id, metadata_view: view end |
#dml? ⇒ Boolean
Whether the query is a DML statement.
331 332 333 334 335 336 337 338 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 331 def dml? [ "INSERT", "UPDATE", "MERGE", "DELETE" ].include? statement_type end |
#dryrun? ⇒ Boolean Also known as: dryrun, dry_run, dry_run?
If set, don't actually run this job. A valid query will return a mostly empty response with some processing statistics, while an invalid query will return the same error it would if it wasn't a dry run.
148 149 150 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 148 def dryrun? @gapi.configuration.dry_run end |
#encryption ⇒ Google::Cloud::BigQuery::EncryptionConfiguration
The encryption configuration of the destination table.
501 502 503 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 501 def encryption EncryptionConfiguration.from_gapi @gapi.configuration.query.destination_encryption_configuration end |
#flatten? ⇒ Boolean
Checks if the query job flattens nested and repeated fields in the
query results. The default is true
. If the value is false
,
large_results? should return true
.
163 164 165 166 167 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 163 def flatten? val = @gapi.configuration.query.flatten_results return true if val.nil? val end |
#inserted_row_count ⇒ Integer?
The number of inserted rows. Present only for DML statements INSERT
and MERGE
. (See #statement_type.)
422 423 424 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 422 def inserted_row_count @gapi.statistics.query&.dml_stats&.inserted_row_count end |
#interactive? ⇒ Boolean
Checks if the priority for the query is INTERACTIVE
.
106 107 108 109 110 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 106 def interactive? val = @gapi.configuration.query.priority return true if val.nil? val == "INTERACTIVE" end |
#large_results? ⇒ Boolean
Checks if the the query job allows arbitrarily large results at a slight cost to performance.
119 120 121 122 123 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 119 def large_results? val = @gapi.configuration.query.allow_large_results return false if val.nil? val end |
#legacy_sql? ⇒ Boolean
Checks if the query job is using legacy sql.
462 463 464 465 466 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 462 def legacy_sql? val = @gapi.configuration.query.use_legacy_sql return true if val.nil? val end |
#maximum_billing_tier ⇒ Integer?
Limits the billing tier for this job. Queries that have resource usage beyond this tier will raise (without incurring a charge). If unspecified, this will be set to your project default. For more information, see High-Compute queries.
179 180 181 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 179 def maximum_billing_tier @gapi.configuration.query.maximum_billing_tier end |
#maximum_bytes_billed ⇒ Integer?
Limits the bytes billed for this job. Queries that will have bytes
billed beyond this limit will raise (without incurring a charge). If
nil
, this will be set to your project default.
191 192 193 194 195 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 191 def maximum_bytes_billed Integer @gapi.configuration.query.maximum_bytes_billed rescue StandardError nil end |
#num_dml_affected_rows ⇒ Integer?
The number of rows affected by a DML statement. Present only for DML
statements INSERT
, UPDATE
or DELETE
. (See #statement_type.)
399 400 401 402 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 399 def num_dml_affected_rows return nil unless @gapi.statistics.query @gapi.statistics.query.num_dml_affected_rows end |
#query_plan ⇒ Array<Google::Cloud::Bigquery::QueryJob::Stage>?
Describes the execution plan for the query.
244 245 246 247 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 244 def query_plan return nil unless @gapi&.statistics&.query&.query_plan Array(@gapi.statistics.query.query_plan).map { |stage| Stage.from_gapi stage } end |
#range_partitioning? ⇒ Boolean
Checks if the destination table will be range partitioned. See Creating and using integer range partitioned tables.
513 514 515 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 513 def range_partitioning? !@gapi.configuration.query.range_partitioning.nil? end |
#range_partitioning_end ⇒ Integer?
The end of range partitioning, exclusive. See Creating and using integer range partitioned tables.
564 565 566 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 564 def range_partitioning_end @gapi.configuration.query.range_partitioning.range.end if range_partitioning? end |
#range_partitioning_field ⇒ String?
The field on which the destination table will be range partitioned, if any. The field must be a
top-level NULLABLE/REQUIRED
field. The only supported type is INTEGER/INT64
. See
Creating and using integer range partitioned
tables.
527 528 529 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 527 def range_partitioning_field @gapi.configuration.query.range_partitioning.field if range_partitioning? end |
#range_partitioning_interval ⇒ Integer?
The width of each interval. See Creating and using integer range partitioned tables.
552 553 554 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 552 def range_partitioning_interval @gapi.configuration.query.range_partitioning.range.interval if range_partitioning? end |
#range_partitioning_start ⇒ Integer?
The start of range partitioning, inclusive. See Creating and using integer range partitioned tables.
539 540 541 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 539 def range_partitioning_start @gapi.configuration.query.range_partitioning.range.start if range_partitioning? end |
#standard_sql? ⇒ Boolean
Checks if the query job is using standard sql.
473 474 475 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 473 def standard_sql? !legacy_sql? end |
#statement_type ⇒ String?
The type of query statement, if valid. Possible values (new values might be added in the future):
- "ALTER_TABLE": DDL statement, see Using Data Definition Language Statements
- "CREATE_MODEL": DDL statement, see Using Data Definition Language Statements
- "CREATE_TABLE": DDL statement, see Using Data Definition Language Statements
- "CREATE_TABLE_AS_SELECT": DDL statement, see Using Data Definition Language Statements
- "CREATE_VIEW": DDL statement, see Using Data Definition Language Statements
- "DELETE": DML statement, see Data Manipulation Language Syntax
- "DROP_MODEL": DDL statement, see Using Data Definition Language Statements
- "DROP_TABLE": DDL statement, see Using Data Definition Language Statements
- "DROP_VIEW": DDL statement, see Using Data Definition Language Statements
- "INSERT": DML statement, see Data Manipulation Language Syntax
- "MERGE": DML statement, see Data Manipulation Language Syntax
- "SELECT": SQL query, see Standard SQL Query Syntax
- "UPDATE": DML statement, see Data Manipulation Language Syntax
277 278 279 280 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 277 def statement_type return nil unless @gapi.statistics.query @gapi.statistics.query.statement_type end |
#time_partitioning? ⇒ Boolean
Checks if the destination table will be time-partitioned. See Partitioned Tables.
577 578 579 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 577 def time_partitioning? !@gapi.configuration.query.time_partitioning.nil? end |
#time_partitioning_expiration ⇒ Integer?
The expiration for the destination table partitions, if any, in seconds. See Partitioned Tables.
622 623 624 625 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 622 def time_partitioning_expiration tp = @gapi.configuration.query.time_partitioning tp.expiration_ms / 1_000 if tp && !tp.expiration_ms.nil? end |
#time_partitioning_field ⇒ String?
The field on which the destination table will be partitioned, if any.
If not set, the destination table will be partitioned by pseudo column
_PARTITIONTIME
; if set, the table will be partitioned by this field.
See Partitioned Tables.
607 608 609 610 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 607 def time_partitioning_field return nil unless time_partitioning? @gapi.configuration.query.time_partitioning.field end |
#time_partitioning_require_filter? ⇒ Boolean
If set to true, queries over the destination table will require a partition filter that can be used for partition elimination to be specified. See Partitioned Tables.
638 639 640 641 642 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 638 def time_partitioning_require_filter? tp = @gapi.configuration.query.time_partitioning return false if tp.nil? || tp.require_partition_filter.nil? tp.require_partition_filter end |
#time_partitioning_type ⇒ String?
The period for which the destination table will be partitioned, if any. See Partitioned Tables.
591 592 593 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 591 def time_partitioning_type @gapi.configuration.query.time_partitioning.type if time_partitioning? end |
#udfs ⇒ Array<String>
The user-defined function resources used in the query. May be either a
code resource to load from a Google Cloud Storage URI
(gs://bucket/path
), or an inline resource that contains code for a
user-defined function (UDF). Providing an inline code resource is
equivalent to providing a URI for a file containing the same code. See
User-Defined Functions.
488 489 490 491 492 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 488 def udfs udfs_gapi = @gapi.configuration.query.user_defined_function_resources return nil unless udfs_gapi Array(udfs_gapi).map { |udf| udf.inline_code || udf.resource_uri } end |
#updated_row_count ⇒ Integer?
The number of updated rows. Present only for DML statements UPDATE
and MERGE
. (See #statement_type.)
433 434 435 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 433 def updated_row_count @gapi.statistics.query&.dml_stats&.updated_row_count end |
#wait_until_done! ⇒ Object
Refreshes the job until the job is DONE
.
The delay between refreshes will incrementally increase.
705 706 707 708 709 710 711 712 713 714 715 716 717 |
# File 'lib/google/cloud/bigquery/query_job.rb', line 705 def wait_until_done! return if done? ensure_service! loop do query_results_gapi = service.job_query_results job_id, location: location, max: 0 if query_results_gapi.job_complete @destination_schema_gapi = query_results_gapi.schema break end end reload! end |