Skip to content

Commit bed47ba

Browse files
authored
impl(bigquery): InsertJob api prep work - Part 1 (#11753)
InsertJob api requires additional fields to be returned in the Job response. This PR and subsequent PRs will add the missing fields and info to Custom Job classes, as needed by the InsertJob api. These were not added in the initial PRs for the Job resource, as they were not needed for the read-only apis. I have divided the above changes into 4 PRs. The first two will add the missing fields and sub classes. The remaining two will mainly add the DebugString() member methods and unit tests, for the logging decorator . This PR includes the following changes: 1) Adds new JobConfigurationQuery class with following sub classes: SystemVariables, StandardSqlField, StandardSqlDataType, StandardSqlStructType, Struct and Value Fields are recursive and self-refrential. Protobuf oneof functionality is simulated by absl::variant. 2) Modifies JobConfiguration class with the query class abopve 3) Json Parsers and Unit Tests for all.
1 parent 1abbb23 commit bed47ba

File tree

9 files changed

+822
-138
lines changed

9 files changed

+822
-138
lines changed

google/cloud/bigquery/bigquery_rest.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ add_library(
5151
v2/minimal/internal/job_client.cc
5252
v2/minimal/internal/job_client.h
5353
v2/minimal/internal/job_configuration.h
54+
v2/minimal/internal/job_configuration_query.h
5455
v2/minimal/internal/job_connection.cc
5556
v2/minimal/internal/job_connection.h
5657
v2/minimal/internal/job_idempotency_policy.cc
@@ -198,6 +199,8 @@ function (bigquery_rest_define_tests)
198199
target_sources(
199200
bigquery_rest_testing
200201
INTERFACE
202+
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/testing/common_v2_test_utils.cc
203+
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/testing/common_v2_test_utils.h
201204
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/testing/metadata_test_utils.cc
202205
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/testing/metadata_test_utils.h
203206
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/testing/mock_dataset_rest_stub.h

google/cloud/bigquery/bigquery_rest_testing.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"""Automatically generated source lists for bigquery_rest_testing - DO NOT EDIT."""
1818

1919
bigquery_rest_testing_hdrs = [
20+
"v2/minimal/testing/common_v2_test_utils.h",
2021
"v2/minimal/testing/metadata_test_utils.h",
2122
"v2/minimal/testing/mock_dataset_rest_stub.h",
2223
"v2/minimal/testing/mock_job_rest_stub.h",
@@ -27,6 +28,7 @@ bigquery_rest_testing_hdrs = [
2728
]
2829

2930
bigquery_rest_testing_srcs = [
31+
"v2/minimal/testing/common_v2_test_utils.cc",
3032
"v2/minimal/testing/metadata_test_utils.cc",
3133
"v2/minimal/testing/project_test_utils.cc",
3234
"v2/minimal/testing/table_test_utils.cc",

google/cloud/bigquery/google_cloud_cpp_bigquery_rest.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ google_cloud_cpp_bigquery_rest_hdrs = [
3636
"v2/minimal/internal/job.h",
3737
"v2/minimal/internal/job_client.h",
3838
"v2/minimal/internal/job_configuration.h",
39+
"v2/minimal/internal/job_configuration_query.h",
3940
"v2/minimal/internal/job_connection.h",
4041
"v2/minimal/internal/job_idempotency_policy.h",
4142
"v2/minimal/internal/job_logging.h",

google/cloud/bigquery/v2/minimal/internal/common_v2_resources.cc

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "google/cloud/bigquery/v2/minimal/internal/common_v2_resources.h"
1616
#include "google/cloud/internal/debug_string.h"
1717
#include "google/cloud/internal/format_time_point.h"
18+
#include "google/cloud/log.h"
1819

1920
namespace google {
2021
namespace cloud {
@@ -79,6 +80,176 @@ void from_json(nlohmann::json const& j, QueryParameterValue& q) {
7980
if (j.contains("struct_values"))
8081
j.at("struct_values").get_to(q.struct_values);
8182
}
83+
84+
void to_json(nlohmann::json& j, StandardSqlField const& f) {
85+
if (f.type != nullptr) {
86+
j = nlohmann::json{{"name", f.name}, {"type", *f.type}};
87+
} else {
88+
j = nlohmann::json{{"name", f.name}};
89+
}
90+
}
91+
92+
void from_json(nlohmann::json const& j, StandardSqlField& f) {
93+
if (j.contains("name")) j.at("name").get_to(f.name);
94+
if (f.type == nullptr) {
95+
f.type = std::make_shared<StandardSqlDataType>();
96+
}
97+
if (j.contains("type")) j.at("type").get_to(*f.type);
98+
}
99+
100+
void to_json(nlohmann::json& j, StandardSqlStructType const& t) {
101+
j = nlohmann::json{{"fields", t.fields}};
102+
}
103+
104+
void from_json(nlohmann::json const& j, StandardSqlStructType& t) {
105+
if (j.contains("fields")) j.at("fields").get_to(t.fields);
106+
}
107+
108+
void to_json(nlohmann::json& j, StandardSqlDataType const& t) {
109+
if (t.sub_type.valueless_by_exception()) {
110+
j = nlohmann::json{{"type_kind", t.type_kind}};
111+
return;
112+
}
113+
114+
struct Visitor {
115+
TypeKind type_kind;
116+
nlohmann::json& j;
117+
118+
void operator()(absl::monostate const&) {
119+
j = nlohmann::json{{"type_kind", std::move(type_kind)}};
120+
}
121+
void operator()(std::shared_ptr<StandardSqlDataType> const& type) {
122+
j = nlohmann::json{{"type_kind", std::move(type_kind)},
123+
{"sub_type", *type},
124+
{"sub_type_index", 1}};
125+
}
126+
void operator()(StandardSqlStructType const& type) {
127+
j = nlohmann::json{{"type_kind", std::move(type_kind)},
128+
{"sub_type", type},
129+
{"sub_type_index", 2}};
130+
}
131+
};
132+
133+
absl::visit(Visitor{t.type_kind, j}, t.sub_type);
134+
}
135+
136+
void from_json(nlohmann::json const& j, StandardSqlDataType& t) {
137+
if (j.contains("type_kind")) j.at("type_kind").get_to(t.type_kind);
138+
if (j.contains("sub_type_index") && j.contains("sub_type")) {
139+
auto const index = j.at("sub_type_index").get<int>();
140+
switch (index) {
141+
case 1:
142+
t.sub_type = std::make_shared<StandardSqlDataType>(
143+
j.at("sub_type").get<StandardSqlDataType>());
144+
break;
145+
case 2:
146+
t.sub_type = j.at("sub_type").get<StandardSqlStructType>();
147+
break;
148+
default:
149+
break;
150+
}
151+
}
152+
}
153+
154+
void to_json(nlohmann::json& j, Value const& v) {
155+
if (v.value_kind.valueless_by_exception()) {
156+
return;
157+
}
158+
159+
struct Visitor {
160+
nlohmann::json& j;
161+
162+
void operator()(absl::monostate const&) {
163+
// Nothing to do.
164+
}
165+
void operator()(double const& val) {
166+
j = nlohmann::json{{"value_kind", val}, {"kind_index", 1}};
167+
}
168+
void operator()(std::string const& val) {
169+
j = nlohmann::json{{"value_kind", val}, {"kind_index", 2}};
170+
}
171+
void operator()(bool const& val) {
172+
j = nlohmann::json{{"value_kind", val}, {"kind_index", 3}};
173+
}
174+
void operator()(std::shared_ptr<Struct> const& val) {
175+
j = nlohmann::json{{"value_kind", *val}, {"kind_index", 4}};
176+
}
177+
void operator()(std::vector<Value> const& val) {
178+
j = nlohmann::json{{"value_kind", val}, {"kind_index", 5}};
179+
}
180+
};
181+
182+
absl::visit(Visitor{j}, v.value_kind);
183+
}
184+
185+
void from_json(nlohmann::json const& j, Value& v) {
186+
if (j.contains("kind_index") && j.contains("value_kind")) {
187+
auto const index = j.at("kind_index").get<int>();
188+
switch (index) {
189+
case 0:
190+
// Do not set any value
191+
break;
192+
case 1:
193+
v.value_kind = j.at("value_kind").get<double>();
194+
break;
195+
case 2:
196+
v.value_kind = j.at("value_kind").get<std::string>();
197+
break;
198+
case 3:
199+
v.value_kind = j.at("value_kind").get<bool>();
200+
break;
201+
case 4:
202+
v.value_kind =
203+
std::make_shared<Struct>(j.at("value_kind").get<Struct>());
204+
break;
205+
case 5:
206+
v.value_kind = j.at("value_kind").get<std::vector<Value>>();
207+
break;
208+
default:
209+
GCP_LOG(FATAL) << "Invalid kind_index for Value: " << index;
210+
break;
211+
}
212+
}
213+
}
214+
215+
void to_json(nlohmann::json& j, Struct const& s) {
216+
j = nlohmann::json{{"fields", s.fields}};
217+
}
218+
219+
void from_json(nlohmann::json const& j, Struct& s) {
220+
if (j.contains("fields")) j.at("fields").get_to(s.fields);
221+
}
222+
223+
void to_json(nlohmann::json& j, SystemVariables const& s) {
224+
j = nlohmann::json{{"types", s.types}, {"values", s.values}};
225+
}
226+
227+
void from_json(nlohmann::json const& j, SystemVariables& s) {
228+
if (j.contains("types")) j.at("types").get_to(s.types);
229+
if (j.contains("values")) j.at("values").get_to(s.values);
230+
}
231+
232+
bool operator==(StandardSqlStructType const& lhs,
233+
StandardSqlStructType const& rhs) {
234+
return std::equal(lhs.fields.begin(), lhs.fields.end(), rhs.fields.begin());
235+
}
236+
237+
bool operator==(StandardSqlField const& lhs, StandardSqlField const& rhs) {
238+
auto const eq_name = (lhs.name == rhs.name);
239+
if (lhs.type != nullptr && rhs.type != nullptr) {
240+
return eq_name && (*lhs.type == *rhs.type);
241+
}
242+
return eq_name && lhs.type == nullptr && rhs.type == nullptr;
243+
}
244+
245+
bool operator==(StandardSqlDataType const& lhs,
246+
StandardSqlDataType const& rhs) {
247+
return (lhs.type_kind.value == rhs.type_kind.value);
248+
}
249+
250+
bool operator==(Value const& lhs, Value const& rhs) {
251+
return (lhs.value_kind == rhs.value_kind);
252+
}
82253
// NOLINTEND
83254

84255
RoundingMode RoundingMode::UnSpecified() {
@@ -93,6 +264,52 @@ RoundingMode RoundingMode::RoundHalfEven() {
93264
return RoundingMode{"ROUND_HALF_EVEN"};
94265
}
95266

267+
KeyResultStatementKind KeyResultStatementKind::UnSpecified() {
268+
return KeyResultStatementKind{"KEY_RESULT_STATEMENT_KIND_UNSPECIFIED"};
269+
}
270+
271+
KeyResultStatementKind KeyResultStatementKind::Last() {
272+
return KeyResultStatementKind{"LAST"};
273+
}
274+
275+
KeyResultStatementKind KeyResultStatementKind::FirstSelect() {
276+
return KeyResultStatementKind{"FIRST_SELECT"};
277+
}
278+
279+
TypeKind TypeKind::UnSpecified() { return TypeKind{"TYPE_KIND_UNSPECIFIED"}; }
280+
281+
TypeKind TypeKind::Int64() { return TypeKind{"INT64"}; }
282+
283+
TypeKind TypeKind::Bool() { return TypeKind{"BOOL"}; }
284+
285+
TypeKind TypeKind::Float64() { return TypeKind{"FLOAT64"}; }
286+
287+
TypeKind TypeKind::String() { return TypeKind{"STRING"}; }
288+
289+
TypeKind TypeKind::Bytes() { return TypeKind{"BYTES"}; }
290+
291+
TypeKind TypeKind::Timestamp() { return TypeKind{"TIMESTAMP"}; }
292+
293+
TypeKind TypeKind::Date() { return TypeKind{"DATE"}; }
294+
295+
TypeKind TypeKind::Time() { return TypeKind{"TIME"}; }
296+
297+
TypeKind TypeKind::DateTime() { return TypeKind{"DATETIME"}; }
298+
299+
TypeKind TypeKind::Interval() { return TypeKind{"INTERVAL"}; }
300+
301+
TypeKind TypeKind::Geography() { return TypeKind{"GEOGRAPHY"}; }
302+
303+
TypeKind TypeKind::Numeric() { return TypeKind{"NUMERIC"}; }
304+
305+
TypeKind TypeKind::BigNumeric() { return TypeKind{"BIGNUMERIC"}; }
306+
307+
TypeKind TypeKind::Json() { return TypeKind{"JSON"}; }
308+
309+
TypeKind TypeKind::Array() { return TypeKind{"ARRAY"}; }
310+
311+
TypeKind TypeKind::Struct() { return TypeKind{"STRUCT"}; }
312+
96313
std::string ErrorProto::DebugString(absl::string_view name,
97314
TracingOptions const& options,
98315
int indent) const {

0 commit comments

Comments
 (0)