GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add support for missing value interpretation (#428) · googleapis/google-cloud-node@22cd6b3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handwritten/bigquery-storage
    • src/managedwriter
      • json_writer.ts
      • writer.ts
    • system-test
      • managed_writer_client_test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handwritten/bigquery-storage/src/managedwriter/json_writer.ts‎

Copy file name to clipboardExpand all lines: handwritten/bigquery-storage/src/managedwriter/json_writer.ts
+34-11Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414

1515
import * as protos from '../../protos/protos';
1616
import {PendingWrite} from './pending_write';
17-
import {StreamConnection, RemoveListener} from './stream_connection';
17+
import {RemoveListener} from './stream_connection';
1818
import * as adapt from '../adapt';
19-
import {Writer} from './writer';
19+
import {Writer, WriterOptions} from './writer';
2020
import {JSONEncoder} from './encoder';
2121

2222
type TableSchema = protos.google.cloud.bigquery.storage.v1.ITableSchema;
23+
type MissingValueInterpretation =
24+
protos.google.cloud.bigquery.storage.v1.AppendRowsRequest['defaultMissingValueInterpretation'];
25+
type MissingValueInterpretationMap = {
26+
[column: string]: MissingValueInterpretation;
27+
};
2328
type IInt64Value = protos.google.protobuf.IInt64Value;
2429
type IDescriptorProto = protos.google.protobuf.IDescriptorProto;
2530
export type JSONPrimitive = string | number | boolean | Date | null;
@@ -49,16 +54,10 @@ export class JSONWriter {
4954
/**
5055
* Creates a new JSONWriter instance.
5156
*
52-
* @param {Object} params - The parameters for the JSONWriter.
53-
* @param {StreamConnection} params.connection - The stream connection
54-
* to the BigQuery streaming insert operation.
55-
* @param {IDescriptorProto} params.protoDescriptor - The proto descriptor
56-
* for the JSON rows.
57+
* @param {WriterOptions} params - The parameters for the JSONWriter.
58+
* See WriterOptions docs for more information.
5759
*/
58-
constructor(params: {
59-
connection: StreamConnection;
60-
protoDescriptor: IDescriptorProto;
61-
}) {
60+
constructor(params: WriterOptions) {
6261
const {connection, protoDescriptor} = params;
6362
this._writer = new Writer(params);
6463
this._encoder = new JSONEncoder({
@@ -88,6 +87,30 @@ export class JSONWriter {
8887
this._encoder.setProtoDescriptor(protoDescriptor);
8988
}
9089

90+
/**
91+
* Update how missing values are interpreted for the given stream.
92+
*
93+
* @param {MissingValueInterpretation} defaultMissingValueInterpretation
94+
*/
95+
setDefaultMissingValueInterpretation(
96+
defaultMissingValueInterpretation: MissingValueInterpretation
97+
) {
98+
this._writer.setDefaultMissingValueInterpretation(
99+
defaultMissingValueInterpretation
100+
);
101+
}
102+
103+
/**
104+
* Update how missing values are interpreted for individual columns.
105+
*
106+
* @param {MissingValueInterpretationMap} missingValueInterpretations
107+
*/
108+
setMissingValueInterpretations(
109+
missingValueInterpretations: MissingValueInterpretationMap
110+
) {
111+
this._writer.setMissingValueInterpretations(missingValueInterpretations);
112+
}
113+
91114
/**
92115
* Writes a JSONList that contains objects to be written to the BigQuery table by first converting
93116
* the JSON data to protobuf messages, then using Writer's appendRows() to write the data at current end
Collapse file

‎handwritten/bigquery-storage/src/managedwriter/writer.ts‎

Copy file name to clipboardExpand all lines: handwritten/bigquery-storage/src/managedwriter/writer.ts
+90-10Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,60 @@ type ProtoData =
2424
protos.google.cloud.bigquery.storage.v1.AppendRowsRequest.IProtoData;
2525
type IDescriptorProto = protos.google.protobuf.IDescriptorProto;
2626
type DescriptorProto = protos.google.protobuf.DescriptorProto;
27+
type MissingValueInterpretation =
28+
AppendRowRequest['defaultMissingValueInterpretation'];
29+
type MissingValueInterpretationMap = {
30+
[column: string]: MissingValueInterpretation;
31+
};
2732

2833
const DescriptorProto = protos.google.protobuf.DescriptorProto;
2934

35+
export interface WriterOptions {
36+
/** The stream connection to the BigQuery streaming insert operation. */
37+
connection: StreamConnection;
38+
39+
/** The proto descriptor for the stream. */
40+
protoDescriptor: IDescriptorProto;
41+
42+
/**
43+
* Controls how missing values are interpreted for a given stream.
44+
* `missingValueInterpretations` set for individual columns can override the default chosen
45+
* with this option.
46+
*
47+
* For example, if you want to write
48+
* `NULL` instead of using default values for some columns, you can set
49+
* `defaultMissingValueInterpretation` to `DEFAULT_VALUE` and at the same
50+
* time, set `missingValueInterpretations` to `NULL_VALUE` on those columns.
51+
*/
52+
defaultMissingValueInterpretation?: MissingValueInterpretation;
53+
54+
/**
55+
* Control how missing values are interpreted for individual columns.
56+
*
57+
* You must provide an object to indicate how to interpret missing value for some fields. Missing
58+
* values are fields present in user schema but missing in rows. The key is
59+
* the field name. The value is the interpretation of missing values for the
60+
* field.
61+
*
62+
* For example, the following option would indicate that missing values in the "foo"
63+
* column are interpreted as null, whereas missing values in the "bar" column are
64+
* treated as the default value:
65+
*
66+
* {
67+
* "foo": 'DEFAULT_VALUE',
68+
* "bar": 'NULL_VALUE',
69+
* }
70+
*
71+
* If a field is not in this object and has missing values, the missing values
72+
* in this field are interpreted as NULL unless overridden with a default missing
73+
* value interpretation.
74+
*
75+
* Currently, field name can only be top-level column name, can't be a struct
76+
* field path like 'foo.bar'.
77+
*/
78+
missingValueInterpretations?: MissingValueInterpretationMap;
79+
}
80+
3081
/**
3182
* A BigQuery Storage API Writer that can be used to write data into BigQuery Table
3283
* using the Storage API.
@@ -37,23 +88,26 @@ const DescriptorProto = protos.google.protobuf.DescriptorProto;
3788
export class Writer {
3889
private _protoDescriptor: DescriptorProto;
3990
private _streamConnection: StreamConnection;
91+
private _defaultMissingValueInterpretation?: MissingValueInterpretation;
92+
private _missingValueInterpretations?: MissingValueInterpretationMap;
4093

4194
/**
4295
* Creates a new Writer instance.
4396
*
44-
* @param {Object} params - The parameters for the JSONWriter.
45-
* @param {StreamConnection} params.connection - The stream connection
46-
* to the BigQuery streaming insert operation.
47-
* @param {IDescriptorProto} params.protoDescriptor - The proto descriptor
48-
* for the JSON rows.
97+
* @param {WriterOptions} params - The parameters for the Writer.
98+
* See WriterOptions docs for more information.
4999
*/
50-
constructor(params: {
51-
connection: StreamConnection;
52-
protoDescriptor: IDescriptorProto;
53-
}) {
54-
const {connection, protoDescriptor} = params;
100+
constructor(params: WriterOptions) {
101+
const {
102+
connection,
103+
protoDescriptor,
104+
missingValueInterpretations,
105+
defaultMissingValueInterpretation,
106+
} = params;
55107
this._streamConnection = connection;
56108
this._protoDescriptor = new DescriptorProto(protoDescriptor);
109+
this._defaultMissingValueInterpretation = defaultMissingValueInterpretation;
110+
this._missingValueInterpretations = missingValueInterpretations;
57111
}
58112

59113
/**
@@ -72,6 +126,28 @@ export class Writer {
72126
}
73127
}
74128

129+
/**
130+
* Update how missing values are interpreted for the given stream.
131+
*
132+
* @param {MissingValueInterpretation} defaultMissingValueInterpretation
133+
*/
134+
setDefaultMissingValueInterpretation(
135+
defaultMissingValueInterpretation: MissingValueInterpretation
136+
) {
137+
this._defaultMissingValueInterpretation = defaultMissingValueInterpretation;
138+
}
139+
140+
/**
141+
* Update how missing values are interpreted for individual columns.
142+
*
143+
* @param {MissingValueInterpretationMap} missingValueInterpretations
144+
*/
145+
setMissingValueInterpretations(
146+
missingValueInterpretations: MissingValueInterpretationMap
147+
) {
148+
this._missingValueInterpretations = missingValueInterpretations;
149+
}
150+
75151
/**
76152
* Schedules the writing of rows at given offset.
77153
*
@@ -97,6 +173,10 @@ export class Writer {
97173
protoDescriptor: this._protoDescriptor.toJSON(),
98174
},
99175
},
176+
defaultMissingValueInterpretation:
177+
this._defaultMissingValueInterpretation,
178+
missingValueInterpretations: this
179+
._missingValueInterpretations as AppendRowRequest['missingValueInterpretations'],
100180
offset,
101181
};
102182

Collapse file

‎handwritten/bigquery-storage/system-test/managed_writer_client_test.ts‎

Copy file name to clipboardExpand all lines: handwritten/bigquery-storage/system-test/managed_writer_client_test.ts
+136Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,142 @@ describe('managedwriter.WriterClient', () => {
629629
}).timeout(30 * 1000);
630630
});
631631

632+
it('should fill default values when MissingValuesInterpretation is set', async () => {
633+
bqWriteClient.initialize();
634+
const client = new WriterClient();
635+
client.setClient(bqWriteClient);
636+
637+
const schema: TableSchema = {
638+
fields: [
639+
{
640+
name: 'customer_name',
641+
type: 'STRING',
642+
mode: 'REQUIRED',
643+
},
644+
{
645+
name: 'row_num',
646+
type: 'INTEGER',
647+
mode: 'REQUIRED',
648+
},
649+
{
650+
name: 'id',
651+
type: 'STRING',
652+
defaultValueExpression: 'GENERATE_UUID()',
653+
},
654+
{
655+
name: 'created_at',
656+
type: 'TIMESTAMP',
657+
defaultValueExpression: 'CURRENT_TIMESTAMP()',
658+
},
659+
{
660+
name: 'updated_at',
661+
type: 'TIMESTAMP',
662+
defaultValueExpression: 'CURRENT_TIMESTAMP()',
663+
},
664+
],
665+
};
666+
const [table] = await bigquery
667+
.dataset(datasetId)
668+
.createTable(tableId + '_default_values', {schema});
669+
const parent = `projects/${projectId}/datasets/${datasetId}/tables/${table.id}`;
670+
671+
const storageSchema =
672+
adapt.convertBigQuerySchemaToStorageTableSchema(schema);
673+
const protoDescriptor: DescriptorProto =
674+
adapt.convertStorageSchemaToProto2Descriptor(storageSchema, 'root');
675+
676+
const row1 = {
677+
customer_name: 'Ada Lovelace',
678+
row_num: 1,
679+
};
680+
681+
const row2 = {
682+
customer_name: 'Alan Turing',
683+
row_num: 2,
684+
};
685+
686+
try {
687+
const connection = await client.createStreamConnection({
688+
streamType: managedwriter.PendingStream,
689+
destinationTable: parent,
690+
});
691+
692+
const streamId = connection.getStreamId();
693+
const writer = new JSONWriter({
694+
connection,
695+
protoDescriptor,
696+
defaultMissingValueInterpretation: 'DEFAULT_VALUE',
697+
missingValueInterpretations: {
698+
updated_at: 'NULL_VALUE',
699+
},
700+
});
701+
702+
let pw = writer.appendRows([row1, row2], 0);
703+
let result = await pw.getResult();
704+
705+
// change MVI config
706+
writer.setDefaultMissingValueInterpretation('NULL_VALUE');
707+
writer.setMissingValueInterpretations({
708+
updated_at: 'DEFAULT_VALUE',
709+
});
710+
711+
const row3 = {
712+
customer_name: 'Charles Babbage',
713+
row_num: 3,
714+
};
715+
716+
const row4 = {
717+
customer_name: 'Lord Byron',
718+
row_num: 4,
719+
};
720+
721+
pw = writer.appendRows([row3, row4], 2);
722+
result = await pw.getResult();
723+
724+
assert.equal(result.error, null);
725+
726+
const res = await connection.finalize();
727+
connection.close();
728+
assert.equal(res?.rowCount, 4);
729+
730+
const commitResponse = await client.batchCommitWriteStream({
731+
parent,
732+
writeStreams: [streamId],
733+
});
734+
assert.equal(commitResponse.streamErrors?.length, 0);
735+
736+
const [rows] = await bigquery.query(
737+
`SELECT * FROM \`${projectId}.${datasetId}.${table.id}\` order by row_num`
738+
);
739+
assert.strictEqual(rows.length, 4);
740+
741+
const first = rows[0];
742+
assert.notEqual(first.id, null);
743+
assert.notEqual(first.created_at, null);
744+
assert.equal(first.updated_at, null);
745+
746+
const second = rows[1];
747+
assert.notEqual(second.id, null);
748+
assert.notEqual(second.created_at, null);
749+
assert.equal(second.updated_at, null);
750+
751+
// After change on MVI config
752+
const third = rows[2];
753+
assert.equal(third.id, null);
754+
assert.equal(third.created_at, null);
755+
assert.notEqual(third.updated_at, null);
756+
757+
const forth = rows[3];
758+
assert.equal(forth.id, null);
759+
assert.equal(forth.created_at, null);
760+
assert.notEqual(forth.updated_at, null);
761+
762+
writer.close();
763+
} finally {
764+
client.close();
765+
}
766+
});
767+
632768
describe('Error Scenarios', () => {
633769
it('send request with mismatched proto descriptor', async () => {
634770
bqWriteClient.initialize();

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.