forked from googleapis/google-cloud-ruby
-
Notifications
You must be signed in to change notification settings - Fork 2
/
table.rb
3720 lines (3554 loc) · 152 KB
/
table.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2015 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "google/cloud/errors"
require "google/cloud/bigquery/service"
require "google/cloud/bigquery/data"
require "google/cloud/bigquery/table/list"
require "google/cloud/bigquery/schema"
require "google/cloud/bigquery/encryption_configuration"
require "google/cloud/bigquery/external"
require "google/cloud/bigquery/insert_response"
require "google/cloud/bigquery/table/async_inserter"
require "google/cloud/bigquery/convert"
require "google/cloud/bigquery/policy"
require "google/apis/bigquery_v2"
module Google
module Cloud
module Bigquery
##
# # Table
#
# A named resource representing a BigQuery table that holds zero or more
# records. Every table is defined by a schema that may contain nested and
# repeated fields.
#
# The Table class can also represent a
# [logical view](https://cloud.google.com/bigquery/docs/views), which is a virtual
# table defined by a SQL query (see {#view?} and {Dataset#create_view}); or a
# [materialized view](https://cloud.google.com/bigquery/docs/materialized-views-intro),
# which is a precomputed view that periodically caches results of a query for increased
# performance and efficiency (see {#materialized_view?} and {Dataset#create_materialized_view}).
#
# @see https://cloud.google.com/bigquery/docs/loading-data#loading_denormalized_nested_and_repeated_data
# Loading denormalized, nested, and repeated data
# @see https://cloud.google.com/bigquery/docs/views Creating views
# @see https://cloud.google.com/bigquery/docs/materialized-views-intro Introduction to materialized views
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
#
# table = dataset.create_table "my_table" do |schema|
# schema.string "first_name", mode: :required
# schema.record "cities_lived", mode: :repeated do |nested_schema|
# nested_schema.string "place", mode: :required
# nested_schema.integer "number_of_years", mode: :required
# end
# end
#
# row = {
# "first_name" => "Alice",
# "cities_lived" => [
# {
# "place" => "Seattle",
# "number_of_years" => 5
# },
# {
# "place" => "Stockholm",
# "number_of_years" => 6
# }
# ]
# }
# table.insert row
#
# @example Creating a logical view:
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# view = dataset.create_view "my_view",
# "SELECT name, age FROM `my_project.my_dataset.my_table`"
# view.view? # true
#
# @example Creating a materialized view:
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# view = dataset.create_materialized_view "my_materialized_view",
# "SELECT name, age FROM `my_project.my_dataset.my_table`"
# view.materialized_view? # true
#
class Table
##
# @private The Service object.
attr_accessor :service
##
# @private The Google API Client object.
attr_accessor :gapi
##
# @private A Google API Client Table Reference object.
attr_reader :reference
##
# @private Create an empty Table object.
def initialize
@service = nil
@gapi = nil
@reference = nil
end
##
# A unique ID for this table.
#
# @return [String] The ID must contain only letters (a-z, A-Z), numbers
# (0-9), or underscores (_). The maximum length is 1,024 characters.
#
# @!group Attributes
#
def table_id
return reference.table_id if reference?
@gapi.table_reference.table_id
end
##
# The ID of the `Dataset` containing this table.
#
# @return [String] The ID must contain only letters (a-z, A-Z), numbers
# (0-9), or underscores (_). The maximum length is 1,024 characters.
#
# @!group Attributes
#
def dataset_id
return reference.dataset_id if reference?
@gapi.table_reference.dataset_id
end
##
# The ID of the `Project` containing this table.
#
# @return [String] The project ID.
#
# @!group Attributes
#
def project_id
return reference.project_id if reference?
@gapi.table_reference.project_id
end
##
# @private The gapi fragment containing the Project ID, Dataset ID, and
# Table ID.
#
# @return [Google::Apis::BigqueryV2::TableReference]
#
def table_ref
reference? ? reference : @gapi.table_reference
end
###
# Checks if the table is range partitioned. See [Creating and using integer range partitioned
# tables](https://cloud.google.com/bigquery/docs/creating-integer-range-partitions).
#
# @return [Boolean, nil] `true` when the table is range partitioned, or
# `false` otherwise, if the object is a resource (see {#resource?});
# `nil` if the object is a reference (see {#reference?}).
#
# @!group Attributes
#
def range_partitioning?
return nil if reference?
!@gapi.range_partitioning.nil?
end
###
# The field on which the table is 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](https://cloud.google.com/bigquery/docs/creating-integer-range-partitions).
#
# @return [Integer, nil] The range partition field, or `nil` if not range partitioned or the object is a
# reference (see {#reference?}).
#
# @!group Attributes
#
def range_partitioning_field
return nil if reference?
ensure_full_data!
@gapi.range_partitioning.field if range_partitioning?
end
###
# The start of range partitioning, inclusive. See [Creating and using integer range partitioned
# tables](https://cloud.google.com/bigquery/docs/creating-integer-range-partitions).
#
# @return [Integer, nil] The start of range partitioning, inclusive, or `nil` if not range partitioned or the
# object is a reference (see {#reference?}).
#
# @!group Attributes
#
def range_partitioning_start
return nil if reference?
ensure_full_data!
@gapi.range_partitioning.range.start if range_partitioning?
end
###
# The width of each interval. See [Creating and using integer range partitioned
# tables](https://cloud.google.com/bigquery/docs/creating-integer-range-partitions).
#
# @return [Integer, nil] The width of each interval, for data in range partitions, or `nil` if not range
# partitioned or the object is a reference (see {#reference?}).
#
# @!group Attributes
#
def range_partitioning_interval
return nil if reference?
ensure_full_data!
return nil unless range_partitioning?
@gapi.range_partitioning.range.interval
end
###
# The end of range partitioning, exclusive. See [Creating and using integer range partitioned
# tables](https://cloud.google.com/bigquery/docs/creating-integer-range-partitions).
#
# @return [Integer, nil] The end of range partitioning, exclusive, or `nil` if not range partitioned or the
# object is a reference (see {#reference?}).
#
# @!group Attributes
#
def range_partitioning_end
return nil if reference?
ensure_full_data!
@gapi.range_partitioning.range.end if range_partitioning?
end
###
# Checks if the table is time partitioned. See [Partitioned
# Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# @return [Boolean, nil] `true` when the table is time partitioned, or
# `false` otherwise, if the object is a resource (see {#resource?});
# `nil` if the object is a reference (see {#reference?}).
#
# @!group Attributes
#
def time_partitioning?
return nil if reference?
!@gapi.time_partitioning.nil?
end
###
# The period for which the table is time partitioned, if any. See
# [Partitioned Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# @return [String, nil] The time partition type. The supported types are `DAY`,
# `HOUR`, `MONTH`, and `YEAR`, which will generate one partition per day,
# hour, month, and year, respectively; or `nil` if not set or the object is a
# reference (see {#reference?}).
#
# @!group Attributes
#
def time_partitioning_type
return nil if reference?
ensure_full_data!
@gapi.time_partitioning.type if time_partitioning?
end
##
# Sets the time partitioning type for the table. See [Partitioned
# Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
# The supported types are `DAY`, `HOUR`, `MONTH`, and `YEAR`, which will
# generate one partition per day, hour, month, and year, respectively.
#
# You can only set time partitioning when creating a table as in
# the example below. BigQuery does not allow you to change time partitioning
# on an existing table.
#
# @param [String] type The time partition type. The supported types are `DAY`,
# `HOUR`, `MONTH`, and `YEAR`, which will generate one partition per day,
# hour, month, and year, respectively.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# table = dataset.create_table "my_table" do |t|
# t.schema do |schema|
# schema.timestamp "dob", mode: :required
# end
# t.time_partitioning_type = "DAY"
# t.time_partitioning_field = "dob"
# end
#
# @!group Attributes
#
def time_partitioning_type= type
reload! unless resource_full?
@gapi.time_partitioning ||= Google::Apis::BigqueryV2::TimePartitioning.new
@gapi.time_partitioning.type = type
patch_gapi! :time_partitioning
end
###
# The field on which the table is time partitioned, if any. If not
# set, the destination table is time partitioned by pseudo column
# `_PARTITIONTIME`; if set, the table is time partitioned by this field. See
# [Partitioned Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# @return [String, nil] The time partition field, if a field was configured.
# `nil` if not time partitioned, not set (time partitioned by pseudo column
# '_PARTITIONTIME') or the object is a reference (see {#reference?}).
#
# @!group Attributes
#
def time_partitioning_field
return nil if reference?
ensure_full_data!
@gapi.time_partitioning.field if time_partitioning?
end
##
# Sets the field on which to time partition the table. If not
# set, the destination table is time partitioned by pseudo column
# `_PARTITIONTIME`; if set, the table is time partitioned by this field. See
# [Partitioned Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
# The table must also be time partitioned.
#
# See {Table#time_partitioning_type=}.
#
# You can only set the time partitioning field while creating a table as in
# the example below. BigQuery does not allow you to change time partitioning
# on an existing table.
#
# @param [String] field The time partition field. The field must be a
# top-level TIMESTAMP or DATE field. Its mode must be NULLABLE or
# REQUIRED.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# table = dataset.create_table "my_table" do |t|
# t.schema do |schema|
# schema.timestamp "dob", mode: :required
# end
# t.time_partitioning_type = "DAY"
# t.time_partitioning_field = "dob"
# end
#
# @!group Attributes
#
def time_partitioning_field= field
reload! unless resource_full?
@gapi.time_partitioning ||= Google::Apis::BigqueryV2::TimePartitioning.new
@gapi.time_partitioning.field = field
patch_gapi! :time_partitioning
end
###
# The expiration for the time partitions, if any, in seconds. See
# [Partitioned Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# @return [Integer, nil] The expiration time, in seconds, for data in
# time partitions, or `nil` if not present or the object is a reference
# (see {#reference?}).
#
# @!group Attributes
#
def time_partitioning_expiration
return nil if reference?
ensure_full_data!
return nil unless time_partitioning?
return nil if @gapi.time_partitioning.expiration_ms.nil?
@gapi.time_partitioning.expiration_ms / 1_000
end
##
# Sets the time partition expiration for the table. See [Partitioned
# Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
# The table must also be time partitioned.
#
# See {Table#time_partitioning_type=}.
#
# If the table is not a full resource representation (see
# {#resource_full?}), the full representation will be retrieved before
# the update to comply with ETag-based optimistic concurrency control.
#
# @param [Integer, nil] expiration An expiration time, in seconds,
# for data in time partitions, , or `nil` to indicate no expiration time.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# table = dataset.create_table "my_table" do |t|
# t.schema do |schema|
# schema.timestamp "dob", mode: :required
# end
# t.time_partitioning_type = "DAY"
# t.time_partitioning_field = "dob"
# t.time_partitioning_expiration = 86_400
# end
#
# @!group Attributes
#
def time_partitioning_expiration= expiration
reload! unless resource_full?
expiration_ms = expiration * 1000 if expiration
@gapi.time_partitioning ||= Google::Apis::BigqueryV2::TimePartitioning.new
@gapi.time_partitioning.expiration_ms = expiration_ms
patch_gapi! :time_partitioning
end
###
# Whether queries over this table require a partition filter that can be
# used for partition elimination to be specified. See [Partitioned
# Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# @return [Boolean, nil] `true` when a partition filter will be
# required, `false` otherwise, or `nil` if the object is a reference
# (see {#reference?}).
#
# @!group Attributes
#
def require_partition_filter
return nil if reference?
ensure_full_data!
@gapi.require_partition_filter
end
##
# Sets whether queries over this table require a partition filter. See
# [Partitioned
# Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# If the table is not a full resource representation (see
# {#resource_full?}), the full representation will be retrieved before
# the update to comply with ETag-based optimistic concurrency control.
#
# @param [Boolean] new_require Whether queries over this table require a
# partition filter.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# table = dataset.create_table "my_table" do |t|
# t.require_partition_filter = true
# end
#
# @!group Attributes
#
def require_partition_filter= new_require
reload! unless resource_full?
@gapi.require_partition_filter = new_require
patch_gapi! :require_partition_filter
end
###
# Checks if the table is clustered.
#
# See {Table::Updater#clustering_fields=}, {Table#clustering_fields} and
# {Table#clustering_fields=}.
#
# @see https://cloud.google.com/bigquery/docs/clustered-tables
# Introduction to clustered tables
# @see https://cloud.google.com/bigquery/docs/creating-clustered-tables
# Creating and using clustered tables
#
# @return [Boolean, nil] `true` when the table is clustered, or
# `false` otherwise, if the object is a resource (see {#resource?});
# `nil` if the object is a reference (see {#reference?}).
#
# @!group Attributes
#
def clustering?
return nil if reference?
!@gapi.clustering.nil?
end
###
# One or more fields on which data should be clustered. Must be
# specified with time 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 {Table::Updater#clustering_fields=}, {Table#clustering_fields=} and
# {Table#clustering?}.
#
# @see https://cloud.google.com/bigquery/docs/clustered-tables
# Introduction to clustered tables
# @see https://cloud.google.com/bigquery/docs/creating-clustered-tables
# Creating and using clustered tables
#
# @return [Array<String>, nil] The clustering fields, or `nil` if the
# table is not clustered or if the table is a reference (see
# {#reference?}).
#
# @!group Attributes
#
def clustering_fields
return nil if reference?
ensure_full_data!
@gapi.clustering.fields if clustering?
end
##
# Updates the list of fields on which data should be clustered.
#
# Only top-level, non-repeated, simple-type fields are supported. When
# you cluster a table using multiple columns, the order of columns you
# specify is important. The order of the specified columns determines
# the sort order of the data.
#
# BigQuery supports clustering for both partitioned and non-partitioned
# tables.
#
# See {Table::Updater#clustering_fields=}, {Table#clustering_fields} and
# {Table#clustering?}.
#
# @see https://cloud.google.com/bigquery/docs/clustered-tables
# Introduction to clustered tables
# @see https://cloud.google.com/bigquery/docs/creating-clustered-tables
# Creating and using clustered tables
# @see https://cloud.google.com/bigquery/docs/creating-clustered-tables#modifying-cluster-spec
# Modifying clustering specification
#
# @param [Array<String>, nil] fields The clustering fields, or `nil` to
# remove the clustering configuration. Only top-level, non-repeated,
# simple-type fields are supported.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# table = dataset.table "my_table"
#
# table.clustering_fields = ["last_name", "first_name"]
#
# @!group Attributes
#
def clustering_fields= fields
reload! unless resource_full?
if fields
@gapi.clustering ||= Google::Apis::BigqueryV2::Clustering.new
@gapi.clustering.fields = fields
else
@gapi.clustering = nil
end
patch_gapi! :clustering
end
##
# The combined Project ID, Dataset ID, and Table ID for this table, in
# the format specified by the [Legacy SQL Query
# Reference](https://cloud.google.com/bigquery/query-reference#from)
# (`project-name:dataset_id.table_id`). This is useful for referencing
# tables in other projects and datasets. To use this value in queries
# see {#query_id}.
#
# @return [String, nil] The combined ID, or `nil` if the object is a
# reference (see {#reference?}).
#
# @!group Attributes
#
def id
return nil if reference?
@gapi.id
end
##
# The value returned by {#id}, wrapped in backticks (Standard SQL) or s
# quare brackets (Legacy SQL) to accommodate project IDs
# containing dashes. Useful in queries.
#
# @param [Boolean] standard_sql Specifies whether to use BigQuery's
# [standard
# SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
# dialect. Optional. The default value is true.
# @param [Boolean] legacy_sql Specifies whether to use BigQuery's
# [legacy
# SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
# dialect. Optional. The default value is false.
#
# @return [String] The appropriate table ID for use in queries,
# depending on SQL type.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# table = dataset.table "my_table"
#
# data = bigquery.query "SELECT first_name FROM #{table.query_id}"
#
# @!group Attributes
#
def query_id standard_sql: nil, legacy_sql: nil
if Convert.resolve_legacy_sql standard_sql, legacy_sql
"[#{project_id}:#{dataset_id}.#{table_id}]"
else
"`#{project_id}.#{dataset_id}.#{table_id}`"
end
end
##
# The name of the table.
#
# @return [String, nil] The friendly name, or `nil` if the object is a
# reference (see {#reference?}).
#
# @!group Attributes
#
def name
return nil if reference?
@gapi.friendly_name
end
##
# Updates the name of the table.
#
# If the table is not a full resource representation (see
# {#resource_full?}), the full representation will be retrieved before
# the update to comply with ETag-based optimistic concurrency control.
#
# @param [String] new_name The new friendly name.
#
# @!group Attributes
#
def name= new_name
reload! unless resource_full?
@gapi.update! friendly_name: new_name
patch_gapi! :friendly_name
end
##
# The ETag hash of the table.
#
# @return [String, nil] The ETag hash, or `nil` if the object is a
# reference (see {#reference?}).
#
# @!group Attributes
#
def etag
return nil if reference?
ensure_full_data!
@gapi.etag
end
##
# A URL that can be used to access the table using the REST API.
#
# @return [String, nil] A REST URL for the resource, or `nil` if the
# object is a reference (see {#reference?}).
#
# @!group Attributes
#
def api_url
return nil if reference?
ensure_full_data!
@gapi.self_link
end
##
# A user-friendly description of the table.
#
# @return [String, nil] The description, or `nil` if the object is a
# reference (see {#reference?}).
#
# @!group Attributes
#
def description
return nil if reference?
ensure_full_data!
@gapi.description
end
##
# Updates the user-friendly description of the table.
#
# If the table is not a full resource representation (see
# {#resource_full?}), the full representation will be retrieved before
# the update to comply with ETag-based optimistic concurrency control.
#
# @param [String] new_description The new user-friendly description.
#
# @!group Attributes
#
def description= new_description
reload! unless resource_full?
@gapi.update! description: new_description
patch_gapi! :description
end
##
# The number of bytes in the table.
#
# @return [Integer, nil] The count of bytes in the table, or `nil` if
# the object is a reference (see {#reference?}).
#
# @!group Data
#
def bytes_count
return nil if reference?
ensure_full_data!
begin
Integer @gapi.num_bytes
rescue StandardError
nil
end
end
##
# The number of rows in the table.
#
# @return [Integer, nil] The count of rows in the table, or `nil` if the
# object is a reference (see {#reference?}).
#
# @!group Data
#
def rows_count
return nil if reference?
ensure_full_data!
begin
Integer @gapi.num_rows
rescue StandardError
nil
end
end
##
# The time when this table was created.
#
# @return [Time, nil] The creation time, or `nil` if the object is a
# reference (see {#reference?}).
#
# @!group Attributes
#
def created_at
return nil if reference?
ensure_full_data!
Convert.millis_to_time @gapi.creation_time
end
##
# The time when this table expires.
# If not present, the table will persist indefinitely.
# Expired tables will be deleted and their storage reclaimed.
#
# @return [Time, nil] The expiration time, or `nil` if not present or
# the object is a reference (see {#reference?}).
#
# @!group Attributes
#
def expires_at
return nil if reference?
ensure_full_data!
Convert.millis_to_time @gapi.expiration_time
end
##
# The date when this table was last modified.
#
# @return [Time, nil] The last modified time, or `nil` if not present or
# the object is a reference (see {#reference?}).
#
# @!group Attributes
#
def modified_at
return nil if reference?
ensure_full_data!
Convert.millis_to_time @gapi.last_modified_time
end
##
# Checks if the table's type is `TABLE`.
#
# @return [Boolean, nil] `true` when the type is `TABLE`, `false`
# otherwise, if the object is a resource (see {#resource?}); `nil` if
# the object is a reference (see {#reference?}).
#
# @!group Attributes
#
def table?
return nil if reference?
@gapi.type == "TABLE"
end
##
# Checks if the table's type is `VIEW`, indicating that the table
# represents a BigQuery logical view. See {Dataset#create_view}.
#
# @see https://cloud.google.com/bigquery/docs/views Creating views
#
# @return [Boolean, nil] `true` when the type is `VIEW`, `false`
# otherwise, if the object is a resource (see {#resource?}); `nil` if
# the object is a reference (see {#reference?}).
#
# @!group Attributes
#
def view?
return nil if reference?
@gapi.type == "VIEW"
end
##
# Checks if the table's type is `MATERIALIZED_VIEW`, indicating that
# the table represents a BigQuery materialized view.
# See {Dataset#create_materialized_view}.
#
# @see https://cloud.google.com/bigquery/docs/materialized-views-intro Introduction to materialized views
#
# @return [Boolean, nil] `true` when the type is `MATERIALIZED_VIEW`,
# `false` otherwise, if the object is a resource (see {#resource?});
# `nil` if the object is a reference (see {#reference?}).
#
# @!group Attributes
#
def materialized_view?
return nil if reference?
@gapi.type == "MATERIALIZED_VIEW"
end
##
# Checks if the table's type is `EXTERNAL`, indicating that the table
# represents an External Data Source. See {#external?} and
# {External::DataSource}.
#
# @return [Boolean, nil] `true` when the type is `EXTERNAL`, `false`
# otherwise, if the object is a resource (see {#resource?}); `nil` if
# the object is a reference (see {#reference?}).
#
# @!group Attributes
#
def external?
return nil if reference?
@gapi.type == "EXTERNAL"
end
##
# The geographic location where the table should reside. Possible
# values include `EU` and `US`. The default value is `US`.
#
# @return [String, nil] The location code.
#
# @!group Attributes
#
def location
return nil if reference?
ensure_full_data!
@gapi.location
end
##
# A hash of user-provided labels associated with this table. Labels
# are used to organize and group tables. See [Using
# Labels](https://cloud.google.com/bigquery/docs/labels).
#
# The returned hash is frozen and changes are not allowed. Use
# {#labels=} to replace the entire hash.
#
# @return [Hash<String, String>, nil] A hash containing key/value pairs.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# table = dataset.table "my_table"
#
# labels = table.labels
# labels["department"] #=> "shipping"
#
# @!group Attributes
#
def labels
return nil if reference?
m = @gapi.labels
m = m.to_h if m.respond_to? :to_h
m.dup.freeze
end
##
# Updates the hash of user-provided labels associated with this table.
# Labels are used to organize and group tables. See [Using
# Labels](https://cloud.google.com/bigquery/docs/labels).
#
# If the table is not a full resource representation (see
# {#resource_full?}), the full representation will be retrieved before
# the update to comply with ETag-based optimistic concurrency control.
#
# @param [Hash<String, String>] labels A hash containing key/value
# pairs.
#
# The labels applied to a resource must meet the following requirements:
#
# * Each resource can have multiple labels, up to a maximum of 64.
# * Each label must be a key-value pair.
# * Keys have a minimum length of 1 character and a maximum length of
# 63 characters, and cannot be empty. Values can be empty, and have
# a maximum length of 63 characters.
# * Keys and values can contain only lowercase letters, numeric characters,
# underscores, and dashes. All characters must use UTF-8 encoding, and
# international characters are allowed.
# * The key portion of a label must be unique. However, you can use the
# same key with multiple resources.
# * Keys must start with a lowercase letter or international character.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# table = dataset.table "my_table"
#
# table.labels = { "department" => "shipping" }
#
# @!group Attributes
#
def labels= labels
reload! unless resource_full?
@gapi.labels = labels
patch_gapi! :labels
end
##
# Returns the table's schema. If the table is not a view (See {#view?}),
# this method can also be used to set, replace, or add to the schema by
# passing a block. See {Schema} for available methods.
#
# If the table is not a full resource representation (see
# {#resource_full?}), the full representation will be retrieved.
#
# @param [Boolean] replace Whether to replace the existing schema with
# the new schema. If `true`, the fields will replace the existing
# schema. If `false`, the fields will be added to the existing schema.
# When a table already contains data, schema changes must be additive.
# Thus, the default value is `false`.
# When loading from a file this will always replace the schema, no
# matter what `replace` is set to. You can update the schema (for
# example, for a table that already contains data) by providing a
# schema file that includes the existing schema plus any new
# fields.
# @yield [schema] a block for setting the schema
# @yieldparam [Schema] schema the object accepting the schema
#
# @return [Google::Cloud::Bigquery::Schema, nil] A frozen schema object.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# table = dataset.create_table "my_table"
#
# table.schema do |schema|
# schema.string "first_name", mode: :required
# schema.record "cities_lived", mode: :repeated do |nested_schema|
# nested_schema.string "place", mode: :required
# nested_schema.integer "number_of_years", mode: :required
# end
# end
#
# @example Load the schema from a file
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# table = dataset.create_table "my_table"
# table.schema do |schema|
# schema.load File.open("schema.json")
# end
#
# @!group Attributes
#
def schema replace: false
return nil if reference? && !block_given?
reload! unless resource_full?
schema_builder = Schema.from_gapi @gapi.schema
if block_given?
schema_builder = Schema.from_gapi if replace
yield schema_builder
if schema_builder.changed?