Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f3f387b

Browse files
PhongChuonggcf-owl-bot[bot]
andauthoredFeb 14, 2024
feat: add MetadataCacheStatistics to Job QueryStatistics (#3133)
* Feat: Add MetadataCacheStatistics to Job QueryStatistics * Add integration test * Add integration test * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update documentation. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent e1947c1 commit f3f387b

File tree

8 files changed

+403
-3
lines changed

8 files changed

+403
-3
lines changed
 

‎README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ implementation 'com.google.cloud:google-cloud-bigquery'
6060
If you are using Gradle without BOM, add this to your dependencies:
6161

6262
```Groovy
63-
implementation 'com.google.cloud:google-cloud-bigquery:2.37.1'
63+
implementation 'com.google.cloud:google-cloud-bigquery:2.37.2'
6464
```
6565

6666
If you are using SBT, add this to your dependencies:
6767

6868
```Scala
69-
libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.37.1"
69+
libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.37.2"
7070
```
7171
<!-- {x-version-update-end} -->
7272

@@ -351,7 +351,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
351351
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-bigquery/java11.html
352352
[stability-image]: https://img.shields.io/badge/stability-stable-green
353353
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-bigquery.svg
354-
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.37.1
354+
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.37.2
355355
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
356356
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
357357
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles

‎google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java

+23
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ public static class QueryStatistics extends JobStatistics {
358358
private final List<TimelineSample> timeline;
359359
private final Schema schema;
360360
private final SearchStats searchStats;
361+
private final MetadataCacheStats metadataCacheStats;
361362
private final List<QueryParameter> queryParameters;
362363

363364
/**
@@ -444,6 +445,8 @@ static final class Builder extends JobStatistics.Builder<QueryStatistics, Builde
444445
private List<QueryParameter> queryParameters;
445446
private SearchStats searchStats;
446447

448+
private MetadataCacheStats metadataCacheStats;
449+
447450
private Builder() {}
448451

449452
private Builder(com.google.api.services.bigquery.model.JobStatistics statisticsPb) {
@@ -493,6 +496,10 @@ private Builder(com.google.api.services.bigquery.model.JobStatistics statisticsP
493496
if (statisticsPb.getQuery().getSearchStatistics() != null) {
494497
this.searchStats = SearchStats.fromPb(statisticsPb.getQuery().getSearchStatistics());
495498
}
499+
if (statisticsPb.getQuery().getMetadataCacheStatistics() != null) {
500+
this.metadataCacheStats =
501+
MetadataCacheStats.fromPb(statisticsPb.getQuery().getMetadataCacheStatistics());
502+
}
496503
if (statisticsPb.getQuery().getDmlStats() != null) {
497504
this.dmlStats = DmlStats.fromPb(statisticsPb.getQuery().getDmlStats());
498505
}
@@ -599,6 +606,11 @@ Builder setSearchStats(SearchStats searchStats) {
599606
return self();
600607
}
601608

609+
Builder setMetadataCacheStats(MetadataCacheStats metadataCacheStats) {
610+
this.metadataCacheStats = metadataCacheStats;
611+
return self();
612+
}
613+
602614
Builder setQueryParameters(List<QueryParameter> queryParameters) {
603615
this.queryParameters = queryParameters;
604616
return self();
@@ -631,6 +643,7 @@ private QueryStatistics(Builder builder) {
631643
this.timeline = builder.timeline;
632644
this.schema = builder.schema;
633645
this.searchStats = builder.searchStats;
646+
this.metadataCacheStats = builder.metadataCacheStats;
634647
this.queryParameters = builder.queryParameters;
635648
}
636649

@@ -761,6 +774,11 @@ public SearchStats getSearchStats() {
761774
return searchStats;
762775
}
763776

777+
/** Statistics for metadata caching in BigLake tables. */
778+
public MetadataCacheStats getMetadataCacheStats() {
779+
return metadataCacheStats;
780+
}
781+
764782
/**
765783
* Standard SQL only: Returns a list of undeclared query parameters detected during a dry run
766784
* validation.
@@ -781,6 +799,7 @@ ToStringHelper toStringHelper() {
781799
.add("timeline", timeline)
782800
.add("schema", schema)
783801
.add("searchStats", searchStats)
802+
.add("metadataCacheStats", metadataCacheStats)
784803
.add("queryParameters", queryParameters);
785804
}
786805

@@ -804,6 +823,7 @@ public final int hashCode() {
804823
queryPlan,
805824
schema,
806825
searchStats,
826+
metadataCacheStats,
807827
queryParameters);
808828
}
809829

@@ -849,6 +869,9 @@ com.google.api.services.bigquery.model.JobStatistics toPb() {
849869
if (searchStats != null) {
850870
queryStatisticsPb.setSearchStatistics(searchStats.toPb());
851871
}
872+
if (metadataCacheStats != null) {
873+
queryStatisticsPb.setMetadataCacheStatistics(metadataCacheStats.toPb());
874+
}
852875
if (queryParameters != null) {
853876
queryStatisticsPb.setUndeclaredQueryParameters(queryParameters);
854877
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigquery;
18+
19+
import com.google.api.services.bigquery.model.MetadataCacheStatistics;
20+
import com.google.auto.value.AutoValue;
21+
import java.io.Serializable;
22+
import java.util.List;
23+
import java.util.stream.Collectors;
24+
import javax.annotation.Nullable;
25+
26+
/**
27+
* Represents statistics for metadata caching in BigLake tables.
28+
*
29+
* @see <a href="https://cloud.google.com/bigquery/docs/biglake-intro">BigLake Tables</a>
30+
*/
31+
@AutoValue
32+
public abstract class MetadataCacheStats implements Serializable {
33+
34+
private static final long serialVersionUID = 1L;
35+
36+
@AutoValue.Builder
37+
public abstract static class Builder {
38+
/** Sets the free form human-readable reason metadata caching was unused for the job. */
39+
public abstract MetadataCacheStats.Builder setTableMetadataCacheUsage(
40+
List<TableMetadataCacheUsage> tableMetadataCacheUsage);
41+
42+
/** Creates a @code MetadataCacheStats} object. */
43+
public abstract MetadataCacheStats build();
44+
}
45+
46+
public abstract Builder toBuilder();
47+
48+
public static Builder newBuilder() {
49+
return new AutoValue_MetadataCacheStats.Builder();
50+
}
51+
52+
@Nullable
53+
public abstract List<TableMetadataCacheUsage> getTableMetadataCacheUsage();
54+
55+
MetadataCacheStatistics toPb() {
56+
MetadataCacheStatistics metadataCacheStatistics = new MetadataCacheStatistics();
57+
if (getTableMetadataCacheUsage() != null) {
58+
metadataCacheStatistics.setTableMetadataCacheUsage(
59+
getTableMetadataCacheUsage().stream()
60+
.map(TableMetadataCacheUsage::toPb)
61+
.collect(Collectors.toList()));
62+
}
63+
return metadataCacheStatistics;
64+
}
65+
66+
static MetadataCacheStats fromPb(MetadataCacheStatistics metadataCacheStatistics) {
67+
Builder builder = newBuilder();
68+
if (metadataCacheStatistics.getTableMetadataCacheUsage() != null) {
69+
builder.setTableMetadataCacheUsage(
70+
metadataCacheStatistics.getTableMetadataCacheUsage().stream()
71+
.map(TableMetadataCacheUsage::fromPb)
72+
.collect(Collectors.toList()));
73+
}
74+
return builder.build();
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigquery;
18+
19+
import com.google.auto.value.AutoValue;
20+
import java.io.Serializable;
21+
import javax.annotation.Nullable;
22+
23+
/** Represents Table level detail on the usage of metadata caching. */
24+
@AutoValue
25+
public abstract class TableMetadataCacheUsage implements Serializable {
26+
27+
private static final long serialVersionUID = 1L;
28+
29+
/** Reason for not using metadata caching for the table. */
30+
public enum UnusedReason {
31+
/** Unused reasons not specified. */
32+
UNUSED_REASON_UNSPECIFIED,
33+
34+
/** Metadata cache was outside the table's maxStaleness. */
35+
EXCEEDED_MAX_STALENESS,
36+
37+
/**
38+
* Metadata caching feature is not enabled. Update BigLake tables to enable the metadata
39+
* caching.
40+
*/
41+
METADATA_CACHING_NOT_ENABLED,
42+
43+
/** Other unknown reason. */
44+
OTHER_REASON
45+
}
46+
47+
@AutoValue.Builder
48+
public abstract static class Builder {
49+
/** Sets the free form human-readable reason metadata caching was unused for the job. */
50+
public abstract TableMetadataCacheUsage.Builder setExplanation(String explanation);
51+
52+
/** Sets the metadata caching eligible table referenced in the query. */
53+
public abstract TableMetadataCacheUsage.Builder setTableReference(TableId tableReference);
54+
55+
/** Sets the table type. */
56+
public abstract TableMetadataCacheUsage.Builder setTableType(String tableType);
57+
58+
/** Sets reason for not using metadata caching for the table. */
59+
public abstract TableMetadataCacheUsage.Builder setUnusedReason(UnusedReason unusedReason);
60+
61+
/** Creates a @code TableMetadataCacheUsage} object. */
62+
public abstract TableMetadataCacheUsage build();
63+
}
64+
65+
public abstract Builder toBuilder();
66+
67+
public static Builder newBuilder() {
68+
return new AutoValue_TableMetadataCacheUsage.Builder();
69+
}
70+
71+
@Nullable
72+
public abstract String getExplanation();
73+
74+
@Nullable
75+
public abstract TableId getTableReference();
76+
77+
@Nullable
78+
public abstract String getTableType();
79+
80+
@Nullable
81+
public abstract UnusedReason getUnusedReason();
82+
83+
com.google.api.services.bigquery.model.TableMetadataCacheUsage toPb() {
84+
com.google.api.services.bigquery.model.TableMetadataCacheUsage tableMetadataCacheUsage =
85+
new com.google.api.services.bigquery.model.TableMetadataCacheUsage();
86+
if (getExplanation() != null) {
87+
tableMetadataCacheUsage.setExplanation(getExplanation());
88+
}
89+
if (getTableReference() != null) {
90+
tableMetadataCacheUsage.setTableReference(getTableReference().toPb());
91+
}
92+
if (getTableType() != null) {
93+
tableMetadataCacheUsage.setTableType(getTableType());
94+
}
95+
if (getUnusedReason() != null) {
96+
tableMetadataCacheUsage.setUnusedReason(getUnusedReason().toString());
97+
}
98+
return tableMetadataCacheUsage;
99+
}
100+
101+
static TableMetadataCacheUsage fromPb(
102+
com.google.api.services.bigquery.model.TableMetadataCacheUsage tableMetadataCacheUsage) {
103+
Builder builder = newBuilder();
104+
if (tableMetadataCacheUsage.getExplanation() != null) {
105+
builder.setExplanation(tableMetadataCacheUsage.getExplanation());
106+
}
107+
if (tableMetadataCacheUsage.getTableReference() != null) {
108+
builder.setTableReference(TableId.fromPb(tableMetadataCacheUsage.getTableReference()));
109+
}
110+
if (tableMetadataCacheUsage.getTableType() != null) {
111+
builder.setTableType(tableMetadataCacheUsage.getTableType());
112+
}
113+
if (tableMetadataCacheUsage.getUnusedReason() != null) {
114+
builder.setUnusedReason(UnusedReason.valueOf(tableMetadataCacheUsage.getUnusedReason()));
115+
}
116+
return builder.build();
117+
}
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigquery;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import com.google.api.services.bigquery.model.MetadataCacheStatistics;
22+
import com.google.common.collect.ImmutableList;
23+
import com.google.common.truth.Truth;
24+
import java.util.List;
25+
import java.util.stream.Collectors;
26+
import org.junit.Test;
27+
28+
public class MetadataCacheStatsTest {
29+
private static List<com.google.api.services.bigquery.model.TableMetadataCacheUsage>
30+
TABLE_METADATA_CACHE_USAGE_PB_LIST =
31+
ImmutableList.of(
32+
new com.google.api.services.bigquery.model.TableMetadataCacheUsage()
33+
.setExplanation("test explanation"));
34+
35+
private static final MetadataCacheStats METADATA_CACHE_STATS =
36+
MetadataCacheStats.newBuilder()
37+
.setTableMetadataCacheUsage(
38+
TABLE_METADATA_CACHE_USAGE_PB_LIST.stream()
39+
.map(TableMetadataCacheUsage::fromPb)
40+
.collect(Collectors.toList()))
41+
.build();
42+
43+
private static final MetadataCacheStatistics METADATA_CACHE_STATISTICS_PB =
44+
new MetadataCacheStatistics().setTableMetadataCacheUsage(TABLE_METADATA_CACHE_USAGE_PB_LIST);
45+
46+
@Test
47+
public void testToPbAndFromPb() {
48+
assertEquals(METADATA_CACHE_STATISTICS_PB, METADATA_CACHE_STATS.toPb());
49+
compareMetadataCacheStats(
50+
METADATA_CACHE_STATS, MetadataCacheStats.fromPb(METADATA_CACHE_STATISTICS_PB));
51+
}
52+
53+
private void compareMetadataCacheStats(MetadataCacheStats expected, MetadataCacheStats value) {
54+
assertEquals(expected, value);
55+
assertEquals(expected.hashCode(), value.hashCode());
56+
assertEquals(expected.toString(), value.toString());
57+
Truth.assertThat(
58+
expected.getTableMetadataCacheUsage().containsAll(value.getTableMetadataCacheUsage()));
59+
}
60+
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.