Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for user defined TVFs (#1278)
Allow BigQuery client library users to define table-valued functions (TVFs) that return table data.
- Loading branch information
1 parent
a58dd7c
commit 89958e9
Showing
7 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
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
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
70 changes: 70 additions & 0 deletions
70
google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/StandardSQLTableType.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,70 @@ | ||
/* | ||
* 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.StandardSqlTableType; | ||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.Lists; | ||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
/** Represents Standard SQL table type information. */ | ||
@AutoValue | ||
public abstract class StandardSQLTableType implements Serializable { | ||
|
||
@AutoValue.Builder | ||
public abstract static class Builder { | ||
|
||
/** Sets the columns in this table type. */ | ||
public abstract Builder setColumns(List<StandardSQLField> columns); | ||
|
||
/** Creates a {@code StandardSQLTableType} object. */ | ||
public abstract StandardSQLTableType build(); | ||
} | ||
|
||
/** Returns the columns in this table type. */ | ||
public abstract List<StandardSQLField> getColumns(); | ||
|
||
public abstract Builder toBuilder(); | ||
|
||
/** Returns a builder for a {@code StandardSQLTableType} object. */ | ||
public static Builder newBuilder() { | ||
return new AutoValue_StandardSQLTableType.Builder(); | ||
} | ||
|
||
/** Returns a builder for a {@code StandardSQLTableType} object with the specified columns. */ | ||
public static StandardSQLTableType.Builder newBuilder(List<StandardSQLField> columns) { | ||
return newBuilder().setColumns(columns); | ||
} | ||
|
||
static StandardSQLTableType fromPb( | ||
com.google.api.services.bigquery.model.StandardSqlTableType tableTypePb) { | ||
StandardSQLTableType.Builder builder = newBuilder(); | ||
if (tableTypePb.getColumns() != null) { | ||
builder.setColumns( | ||
Lists.transform(tableTypePb.getColumns(), StandardSQLField.FROM_PB_FUNCTION)); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
StandardSqlTableType toPb() { | ||
StandardSqlTableType tableType = new StandardSqlTableType(); | ||
if (getColumns() != null) { | ||
tableType.setColumns(Lists.transform(getColumns(), StandardSQLField.TO_PB_FUNCTION)); | ||
} | ||
return tableType; | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardSQLTableTypeTest.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,60 @@ | ||
/* | ||
* 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.*; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.List; | ||
import org.junit.Test; | ||
|
||
public class StandardSQLTableTypeTest { | ||
|
||
private static final StandardSQLField COLUMN_1 = | ||
StandardSQLField.newBuilder("COLUMN_1", StandardSQLDataType.newBuilder("STRING").build()) | ||
.build(); | ||
private static final StandardSQLField COLUMN_2 = | ||
StandardSQLField.newBuilder("COLUMN_2", StandardSQLDataType.newBuilder("FLOAT64").build()) | ||
.build(); | ||
|
||
private static final List<StandardSQLField> COLUMN_LIST = ImmutableList.of(COLUMN_1, COLUMN_2); | ||
private static final StandardSQLTableType TABLE_TYPE = | ||
StandardSQLTableType.newBuilder(COLUMN_LIST).build(); | ||
|
||
@Test | ||
public void testToBuilder() { | ||
compareStandardSQLTableType(TABLE_TYPE, TABLE_TYPE.toBuilder().build()); | ||
} | ||
|
||
@Test | ||
public void testBuilder() { | ||
assertEquals(COLUMN_1, TABLE_TYPE.getColumns().get(0)); | ||
assertEquals(COLUMN_2, TABLE_TYPE.getColumns().get(1)); | ||
} | ||
|
||
@Test | ||
public void testToAndFromPb() { | ||
compareStandardSQLTableType(TABLE_TYPE, StandardSQLTableType.fromPb(TABLE_TYPE.toPb())); | ||
} | ||
|
||
private void compareStandardSQLTableType( | ||
StandardSQLTableType expected, StandardSQLTableType value) { | ||
assertEquals(expected, value); | ||
assertEquals(expected.getColumns(), value.getColumns()); | ||
assertEquals(expected.hashCode(), value.hashCode()); | ||
} | ||
} |
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