Skip to content

Commit c59a9f6

Browse files
authored
Merge pull request #57 from koichiro/feature/fix-dml-stat-warning
Suppress the warning about missing DML statistics
2 parents 93ff24e + 8eeb32a commit c59a9f6

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

lib/airbnb_payous/bigquery_gateway.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,32 @@ def load_and_merge!(rows:)
7777
if target_table.nil?
7878
@logger.info("Target table #{qualified_table_name(table_id)} does not exist. Path: :create_table")
7979
copy_job = @bigquery.copy_job qualified_table_name(staging_table_id), qualified_table_name(table_id), write: "truncate"
80+
@logger.info("Started copy_job (ID: #{copy_job.job_id}). Waiting for completion...")
8081
copy_job.wait_until_done!
8182
raise copy_job.error if copy_job.failed?
82-
@logger.info("Target table created via copy_job.")
83+
@logger.info("Target table created successfully via copy_job.")
8384

8485
result[:mode] = :create_table
8586
result[:inserted_count] = total_rows_loaded
8687
else
8788
@logger.info("Target table #{qualified_table_name(table_id)} exists. Path: :merge")
8889
merge_sql = build_merge_query(rows.first.keys)
8990
query_job = @bigquery.query_job(merge_sql)
91+
@logger.info("Started query_job (ID: #{query_job.job_id}). Waiting for completion...")
9092
query_job.wait_until_done!
9193
raise query_job.error if query_job.failed?
9294

93-
# Capture DML stats
94-
if query_job.respond_to?(:dml_stats) && query_job.dml_stats
95+
# Capture DML stats with robust fallbacks
96+
if query_job.dml_stats
9597
result[:inserted_count] = query_job.dml_stats.inserted_row_count.to_i
9698
result[:updated_count] = query_job.dml_stats.updated_row_count.to_i
9799
@logger.info("DML Stats - Inserted: #{result[:inserted_count]}, Updated: #{result[:updated_count]}")
98-
else
99-
# Fallback: if dml_stats is nil, try to get from raw statistics or use total affected
100-
affected = query_job.num_dml_affected_rows.to_i
101-
@logger.warn("DML Stats not found. num_dml_affected_rows: #{affected}")
100+
elsif (affected = query_job.num_dml_affected_rows.to_i) >= 0
101+
# For our current MERGE query (which only has INSERT), all affected rows are inserts
102102
result[:inserted_count] = affected
103+
@logger.info("DML Stats not found, but num_dml_affected_rows is #{affected}. Assuming all are inserts.")
104+
else
105+
@logger.warn("No DML statistics available for query job #{query_job.job_id}.")
103106
end
104107

105108
result[:mode] = :merge

test/bigquery_gateway_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ def numeric(name, mode:) = @fields << [:numeric, name, mode]
6161
end
6262

6363
class FakeLoadJob
64-
attr_reader :waited
64+
attr_reader :waited, :job_id
6565

66-
def initialize(output_rows: 1)
66+
def initialize(output_rows: 1, job_id: "job_123")
6767
@waited = false
6868
@output_rows = output_rows
69+
@job_id = job_id
6970
end
7071

7172
def wait_until_done!

0 commit comments

Comments
 (0)