Skip to content
Permalink
Browse files
fix: BQTableSchemaToProtoDescriptor change type mapping (#402)
Change BQ Date to map to Proto Int32.
  • Loading branch information
allenc3 committed Jul 14, 2020
1 parent 85c571b commit 413d6f03f288fa21511daaa3442fc1fde9ca246f
@@ -28,4 +28,9 @@
<method>com.google.cloud.bigquery.storage.v1alpha2.WriterCache getTestInstance(com.google.cloud.bigquery.storage.v1alpha2.BigQueryWriteClient, int, com.google.cloud.bigquery.storage.v1alpha2.SchemaCompact) </method>
<to>com.google.cloud.bigquery.storage.v1alpha2.WriterCache getTestInstance(com.google.cloud.bigquery.storage.v1alpha2.BigQueryWriteClient, int, com.google.cloud.bigquery.storage.v1alpha2.SchemaCompatibility)</to>
</difference>
<difference>
<differenceType>7002</differenceType>
<className>com/google/cloud/bigquery/storage/v1alpha2/BQTableSchemaToProtoDescriptor</className>
<method>com.google.protobuf.Descriptors$Descriptor ConvertBQTableSchemaToProtoDescriptor(com.google.cloud.bigquery.storage.v1alpha2.Table$TableSchema)</method>
</difference>
</differences>
@@ -44,7 +44,7 @@ public class BQTableSchemaToProtoDescriptor {
new ImmutableMap.Builder<Table.TableFieldSchema.Type, FieldDescriptorProto.Type>()
.put(Table.TableFieldSchema.Type.BOOL, FieldDescriptorProto.Type.TYPE_BOOL)
.put(Table.TableFieldSchema.Type.BYTES, FieldDescriptorProto.Type.TYPE_BYTES)
.put(Table.TableFieldSchema.Type.DATE, FieldDescriptorProto.Type.TYPE_INT64)
.put(Table.TableFieldSchema.Type.DATE, FieldDescriptorProto.Type.TYPE_INT32)
.put(Table.TableFieldSchema.Type.DATETIME, FieldDescriptorProto.Type.TYPE_INT64)
.put(Table.TableFieldSchema.Type.DOUBLE, FieldDescriptorProto.Type.TYPE_DOUBLE)
.put(Table.TableFieldSchema.Type.GEOGRAPHY, FieldDescriptorProto.Type.TYPE_BYTES)
@@ -62,9 +62,9 @@ public class BQTableSchemaToProtoDescriptor {
* @param BQTableSchema
* @throws Descriptors.DescriptorValidationException
*/
public static Descriptor ConvertBQTableSchemaToProtoDescriptor(Table.TableSchema BQTableSchema)
public static Descriptor convertBQTableSchemaToProtoDescriptor(Table.TableSchema BQTableSchema)
throws Descriptors.DescriptorValidationException {
return ConvertBQTableSchemaToProtoDescriptorImpl(
return convertBQTableSchemaToProtoDescriptorImpl(
BQTableSchema, "root", new HashMap<ImmutableList<Table.TableFieldSchema>, Descriptor>());
}

@@ -77,7 +77,7 @@ public static Descriptor ConvertBQTableSchemaToProtoDescriptor(Table.TableSchema
* @param dependencyMap Stores already constructed descriptors to prevent reconstruction
* @throws Descriptors.DescriptorValidationException
*/
private static Descriptor ConvertBQTableSchemaToProtoDescriptorImpl(
private static Descriptor convertBQTableSchemaToProtoDescriptorImpl(
Table.TableSchema BQTableSchema,
String scope,
HashMap<ImmutableList<Table.TableFieldSchema>, Descriptor> dependencyMap)
@@ -93,19 +93,19 @@ private static Descriptor ConvertBQTableSchemaToProtoDescriptorImpl(
if (dependencyMap.containsKey(fieldList)) {
Descriptor descriptor = dependencyMap.get(fieldList);
dependenciesList.add(descriptor.getFile());
fields.add(ConvertBQTableFieldToProtoField(BQTableField, index++, descriptor.getName()));
fields.add(convertBQTableFieldToProtoField(BQTableField, index++, descriptor.getName()));
} else {
Descriptor descriptor =
ConvertBQTableSchemaToProtoDescriptorImpl(
convertBQTableSchemaToProtoDescriptorImpl(
Table.TableSchema.newBuilder().addAllFields(fieldList).build(),
currentScope,
dependencyMap);
dependenciesList.add(descriptor.getFile());
dependencyMap.put(fieldList, descriptor);
fields.add(ConvertBQTableFieldToProtoField(BQTableField, index++, currentScope));
fields.add(convertBQTableFieldToProtoField(BQTableField, index++, currentScope));
}
} else {
fields.add(ConvertBQTableFieldToProtoField(BQTableField, index++, currentScope));
fields.add(convertBQTableFieldToProtoField(BQTableField, index++, currentScope));
}
}
FileDescriptor[] dependenciesArray = new FileDescriptor[dependenciesList.size()];
@@ -127,7 +127,7 @@ private static Descriptor ConvertBQTableSchemaToProtoDescriptorImpl(
* @param index Index for protobuf fields.
* @param scope used to name descriptors
*/
private static FieldDescriptorProto ConvertBQTableFieldToProtoField(
private static FieldDescriptorProto convertBQTableFieldToProtoField(
Table.TableFieldSchema BQTableField, int index, String scope) {
Table.TableFieldSchema.Mode mode = BQTableField.getMode();
String fieldName = BQTableField.getName();
@@ -38,7 +38,7 @@ public class BQTableSchemaToProtoDescriptorTest {
new ImmutableMap.Builder<Table.TableFieldSchema.Type, Descriptor>()
.put(Table.TableFieldSchema.Type.BOOL, BoolType.getDescriptor())
.put(Table.TableFieldSchema.Type.BYTES, BytesType.getDescriptor())
.put(Table.TableFieldSchema.Type.DATE, Int64Type.getDescriptor())
.put(Table.TableFieldSchema.Type.DATE, Int32Type.getDescriptor())
.put(Table.TableFieldSchema.Type.DATETIME, Int64Type.getDescriptor())
.put(Table.TableFieldSchema.Type.DOUBLE, DoubleType.getDescriptor())
.put(Table.TableFieldSchema.Type.GEOGRAPHY, BytesType.getDescriptor())
@@ -101,7 +101,7 @@ public void testSimpleTypes() throws Exception {
final Table.TableSchema tableSchema =
Table.TableSchema.newBuilder().addFields(0, tableFieldSchema).build();
final Descriptor descriptor =
BQTableSchemaToProtoDescriptor.ConvertBQTableSchemaToProtoDescriptor(tableSchema);
BQTableSchemaToProtoDescriptor.convertBQTableSchemaToProtoDescriptor(tableSchema);
isDescriptorEqual(descriptor, entry.getValue());
}
}
@@ -124,7 +124,7 @@ public void testStructSimple() throws Exception {
final Table.TableSchema tableSchema =
Table.TableSchema.newBuilder().addFields(0, tableFieldSchema).build();
final Descriptor descriptor =
BQTableSchemaToProtoDescriptor.ConvertBQTableSchemaToProtoDescriptor(tableSchema);
BQTableSchemaToProtoDescriptor.convertBQTableSchemaToProtoDescriptor(tableSchema);
isDescriptorEqual(descriptor, MessageType.getDescriptor());
}

@@ -160,6 +160,12 @@ public void testStructComplex() throws Exception {
.setMode(Table.TableFieldSchema.Mode.REPEATED)
.setName("test_double")
.build();
final Table.TableFieldSchema test_date =
Table.TableFieldSchema.newBuilder()
.setType(Table.TableFieldSchema.Type.DATE)
.setMode(Table.TableFieldSchema.Mode.REQUIRED)
.setName("test_date")
.build();
final Table.TableFieldSchema ComplexLvl2 =
Table.TableFieldSchema.newBuilder()
.setType(Table.TableFieldSchema.Type.STRUCT)
@@ -182,11 +188,12 @@ public void testStructComplex() throws Exception {
.addFields(2, test_bytes)
.addFields(3, test_bool)
.addFields(4, test_double)
.addFields(5, ComplexLvl1)
.addFields(6, ComplexLvl2)
.addFields(5, test_date)
.addFields(6, ComplexLvl1)
.addFields(7, ComplexLvl2)
.build();
final Descriptor descriptor =
BQTableSchemaToProtoDescriptor.ConvertBQTableSchemaToProtoDescriptor(tableSchema);
BQTableSchemaToProtoDescriptor.convertBQTableSchemaToProtoDescriptor(tableSchema);
isDescriptorEqual(descriptor, ComplexRoot.getDescriptor());
}

@@ -217,7 +224,7 @@ public void testOptions() throws Exception {
.addFields(2, optional)
.build();
final Descriptor descriptor =
BQTableSchemaToProtoDescriptor.ConvertBQTableSchemaToProtoDescriptor(tableSchema);
BQTableSchemaToProtoDescriptor.convertBQTableSchemaToProtoDescriptor(tableSchema);
isDescriptorEqual(descriptor, OptionTest.getDescriptor());
}

@@ -267,7 +274,7 @@ public void testDescriptorReuseDuringCreation() throws Exception {
.addFields(2, reuse_lvl1_2)
.build();
final Descriptor descriptor =
BQTableSchemaToProtoDescriptor.ConvertBQTableSchemaToProtoDescriptor(tableSchema);
BQTableSchemaToProtoDescriptor.convertBQTableSchemaToProtoDescriptor(tableSchema);
HashMap<String, Integer> descriptorToCount = new HashMap<String, Integer>();
mapDescriptorToCount(descriptor, descriptorToCount);
assertEquals(descriptorToCount.size(), 2);
@@ -8,8 +8,9 @@ message ComplexRoot {
required bytes test_bytes = 3;
optional bool test_bool = 4;
repeated double test_double = 5;
required ComplexLvl1 complexLvl1 = 6;
required ComplexLvl2 complexLvl2 = 7;
required int32 test_date = 6;
required ComplexLvl1 complexLvl1 = 7;
required ComplexLvl2 complexLvl2 = 8;
}

message ComplexLvl1 {

0 comments on commit 413d6f0

Please sign in to comment.