-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dmlStatistics support (#1431)
Provides detailed statistics for DML statements Present only for DML statements INSERT, UPDATE, DELETE or TRUNCATE. Towards b/186432630
- Loading branch information
1 parent
94ce14f
commit 9d67e05
Showing
5 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DmlStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2021 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
package com.google.cloud.bigquery; | ||
|
||
import com.google.api.services.bigquery.model.DmlStatistics; | ||
import com.google.auto.value.AutoValue; | ||
import java.io.Serializable; | ||
import javax.annotation.Nullable; | ||
|
||
/** Represents DML statistics information. */ | ||
@AutoValue | ||
public abstract class DmlStats implements Serializable { | ||
|
||
@AutoValue.Builder | ||
public abstract static class Builder { | ||
/** | ||
* Number of deleted Rows. populated by DML DELETE, MERGE and TRUNCATE statements. | ||
* | ||
* @param deletedRowCount deletedRowCount or {@code null} for none | ||
*/ | ||
public abstract Builder setDeletedRowCount(Long deletedRowCount); | ||
|
||
/** | ||
* Number of inserted Rows. Populated by DML INSERT and MERGE statements. | ||
* | ||
* @param insertedRowCount insertedRowCount or {@code null} for none | ||
*/ | ||
public abstract Builder setInsertedRowCount(Long insertedRowCount); | ||
|
||
/** | ||
* Number of updated Rows. Populated by DML UPDATE and MERGE statements. | ||
* | ||
* @param updatedRowCount updatedRowCount or {@code null} for none | ||
*/ | ||
public abstract Builder setUpdatedRowCount(Long updatedRowCount); | ||
|
||
/** Creates a {@code DmlStats} object. */ | ||
public abstract DmlStats build(); | ||
} | ||
|
||
/** | ||
* Returns number of deleted Rows. populated by DML DELETE, MERGE and TRUNCATE statements. | ||
* | ||
* @return value or {@code null} for none | ||
*/ | ||
@Nullable | ||
public abstract Long getDeletedRowCount(); | ||
|
||
/** | ||
* Returns number of inserted Rows. Populated by DML INSERT and MERGE statements. | ||
* | ||
* @return value or {@code null} for none | ||
*/ | ||
@Nullable | ||
public abstract Long getInsertedRowCount(); | ||
|
||
/** | ||
* Returns number of updated Rows. Populated by DML UPDATE and MERGE statements. | ||
* | ||
* @return value or {@code null} for none | ||
*/ | ||
@Nullable | ||
public abstract Long getUpdatedRowCount(); | ||
|
||
public abstract Builder toBuilder(); | ||
|
||
public static Builder newBuilder() { | ||
return new AutoValue_DmlStats.Builder(); | ||
} | ||
|
||
DmlStatistics toPb() { | ||
DmlStatistics dmlStatisticsPb = new DmlStatistics(); | ||
if (getDeletedRowCount() != null) { | ||
dmlStatisticsPb.setDeletedRowCount(getDeletedRowCount()); | ||
} | ||
if (getInsertedRowCount() != null) { | ||
dmlStatisticsPb.setInsertedRowCount(getInsertedRowCount()); | ||
} | ||
if (getUpdatedRowCount() != null) { | ||
dmlStatisticsPb.setUpdatedRowCount(getUpdatedRowCount()); | ||
} | ||
return dmlStatisticsPb; | ||
} | ||
|
||
static DmlStats fromPb(DmlStatistics dmlStatisticsPb) { | ||
Builder builder = newBuilder(); | ||
if (dmlStatisticsPb.getDeletedRowCount() != null) { | ||
builder.setDeletedRowCount(dmlStatisticsPb.getDeletedRowCount()); | ||
} | ||
if (dmlStatisticsPb.getInsertedRowCount() != null) { | ||
builder.setInsertedRowCount(dmlStatisticsPb.getInsertedRowCount()); | ||
} | ||
if (dmlStatisticsPb.getUpdatedRowCount() != null) { | ||
builder.setUpdatedRowCount(dmlStatisticsPb.getUpdatedRowCount()); | ||
} | ||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/DmlStatsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2021 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
package com.google.cloud.bigquery; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class DmlStatsTest { | ||
|
||
private static final Long DELETED_ROW_COUNT = 10L; | ||
private static final Long INSERTED_ROW_COUNT = 20L; | ||
private static final Long UPDATED_ROW_COUNT = 30L; | ||
private static final DmlStats DML_STATS = | ||
DmlStats.newBuilder() | ||
.setDeletedRowCount(DELETED_ROW_COUNT) | ||
.setInsertedRowCount(INSERTED_ROW_COUNT) | ||
.setUpdatedRowCount(UPDATED_ROW_COUNT) | ||
.build(); | ||
|
||
@Test | ||
public void testBuilder() { | ||
assertEquals(DELETED_ROW_COUNT, DML_STATS.getDeletedRowCount()); | ||
assertEquals(UPDATED_ROW_COUNT, DML_STATS.getUpdatedRowCount()); | ||
assertEquals(INSERTED_ROW_COUNT, DML_STATS.getInsertedRowCount()); | ||
} | ||
|
||
@Test | ||
public void testToPbAndFromPb() { | ||
compareDmlStats(DML_STATS, DmlStats.fromPb(DML_STATS.toPb())); | ||
} | ||
|
||
private void compareDmlStats(DmlStats expected, DmlStats actual) { | ||
assertEquals(expected, actual); | ||
assertEquals(expected.hashCode(), actual.hashCode()); | ||
assertEquals(expected.toString(), actual.toString()); | ||
assertEquals(expected.getDeletedRowCount(), actual.getDeletedRowCount()); | ||
assertEquals(expected.getInsertedRowCount(), actual.getInsertedRowCount()); | ||
assertEquals(expected.getUpdatedRowCount(), actual.getUpdatedRowCount()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters