Skip to content

Added option to take over column’s description and policy_tags when in replace mode #100

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add retain_column_descriptions option
  • Loading branch information
chikamura committed Apr 7, 2025
commit 3a19beed3b6f320a6176687f604ab9d28e10412c
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Under construction
| auto_create_gcs_bucket (x) | boolean | optional | false | See [GCS Bucket](#gcs-bucket) |
| progress_log_interval (x) | float | optional | nil (Disabled) | Progress log interval. The progress log is disabled by nil (default). NOTE: This option may be removed in a future because a filter plugin can achieve the same goal |
| before_load | string | optional | nil | if set, this SQL will be executed before loading all records in append mode. In replace mode, SQL is not executed. |
| retain_column_descriptions | boolean | optional | false | In case of replace mode, the column's descriptions are taken over. |


Client or request options
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/org/embulk/output/bigquery_java/BigqueryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import com.google.cloud.bigquery.Dataset;
import com.google.cloud.bigquery.DatasetInfo;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.FieldList;
import com.google.cloud.bigquery.FieldValue;
import com.google.cloud.bigquery.FormatOptions;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobId;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.JobStatistics;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.PolicyTags;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.StandardTableDefinition;
Expand Down Expand Up @@ -75,15 +77,16 @@ public class BigqueryClient {
private PluginTask task;
private Schema schema;
private List<BigqueryColumnOption> columnOptions;
private Table srcTable;

public BigqueryClient(PluginTask task, Schema schema) {
this.task = task;
this.schema = schema;
this.dataset = task.getDataset();
if (task.getLocation().isPresent()){
if (task.getLocation().isPresent()) {
this.location = task.getLocation().get();
this.locationForLog = task.getLocation().get();
}else{
} else {
this.locationForLog = "us/eu";
}
this.columnOptions = this.task.getColumnOptions().orElse(Collections.emptyList());
Expand All @@ -92,6 +95,7 @@ public BigqueryClient(PluginTask task, Schema schema) {
} catch (IOException e) {
throw new RuntimeException(e);
}
this.srcTable = (task.getMode().equals("replace") && task.getRetainColumnDescriptions()) ? getTable(task.getTable()) : null;
}

private static BigQuery getClientWithJsonKey(String key) throws IOException {
Expand Down Expand Up @@ -616,6 +620,15 @@ protected com.google.cloud.bigquery.Schema buildSchema(Schema schema, List<Bigqu
Optional<BigqueryColumnOption> columnOption = BigqueryUtil.findColumnOption(col.getName(), columnOptions);
Field.Builder fieldBuilder = createFieldBuilder(task, col, columnOption);

if(srcTable != null && task.getRetainColumnDescriptions()){
FieldList srcFields = srcTable.getDefinition().getSchema().getFields();
if(srcFields != null) {
srcFields.stream().filter(x -> x.getName().equals(col.getName()))
.findFirst()
.ifPresent(field -> fieldBuilder.setDescription(field.getDescription()));
}
}

if (columnOption.isPresent()) {
BigqueryColumnOption colOpt = columnOption.get();
if (!colOpt.getMode().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,8 @@ public interface PluginTask
@Config("merge_rule")
@ConfigDefault("null")
Optional<List<String>> getMergeRule();

@Config("retain_column_descriptions")
@ConfigDefault("false")
Boolean getRetainColumnDescriptions();
}