Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigQueryV2: Allow passing an empty array as a value for params #4269

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion google-cloud-bigquery/lib/google/cloud/bigquery/convert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,16 @@ def self.to_query_param value
)
elsif Array === value
array_params = value.map { |param| Convert.to_query_param param }
array_type =
if array_params.empty?
Google::Apis::BigqueryV2::QueryParameterType.new(type: "STRING")
else
array_params.first.parameter_type
end
return Google::Apis::BigqueryV2::QueryParameter.new(
parameter_type: Google::Apis::BigqueryV2::QueryParameterType.new(
type: "ARRAY",
array_type: array_params.first.parameter_type
array_type: array_type
),
parameter_value: Google::Apis::BigqueryV2::QueryParameterValue.new(
array_values: array_params.map(&:parameter_value)
Expand Down
20 changes: 20 additions & 0 deletions google-cloud-bigquery/test/google/cloud/bigquery/convert_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,24 @@
f.must_be :nan?
end
end

describe :to_query_param do
it "converts an array correctly" do
f = Google::Cloud::Bigquery::Convert.to_query_param([1, 2])
f.must_be_kind_of Google::Apis::BigqueryV2::QueryParameter
f.to_h.must_equal({
parameter_type: { array_type: { type: "INT64" }, type: "ARRAY" },
parameter_value: { array_values: [{ value: 1 }, { value: 2 }] }
})
end

it "converts an empty array correctly" do
f = Google::Cloud::Bigquery::Convert.to_query_param([])
f.must_be_kind_of Google::Apis::BigqueryV2::QueryParameter
f.to_h.must_equal({
parameter_type: { array_type: { type: "STRING" }, type: "ARRAY" },
parameter_value: { array_values: [] }
})
end
end
end