Skip to content

Commit

Permalink
Support Json
Browse files Browse the repository at this point in the history
  • Loading branch information
psainics committed Mar 29, 2024
1 parent ff50316 commit 8728d58
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ private static void addToRecordBuilder(StructuredRecord.Builder recordBuilder, S
*/
public static Object convertValue(Field field, FieldValue fieldValue) {
LegacySQLTypeName type = field.getType();
StandardSQLTypeName standardType = type.getStandardType();
LegacySQLTypeName typeJson = LegacySQLTypeName.valueOf("JSON");
// Treat JSON as string
StandardSQLTypeName standardType = type.equals(typeJson) ? StandardSQLTypeName.STRING : type.getStandardType();
switch (standardType) {
case TIME:
return LocalTime.parse(fieldValue.getStringValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ public static Schema convertFieldType(Field field, @Nullable FailureCollector co
public static Schema convertFieldType(Field field, @Nullable FailureCollector collector,
@Nullable String recordPrefix) {
LegacySQLTypeName type = field.getType();
StandardSQLTypeName standardType = type.getStandardType();
LegacySQLTypeName typeJson = LegacySQLTypeName.valueOf("JSON");
// Treat JSON as string
StandardSQLTypeName standardType = type.equals(typeJson) ? StandardSQLTypeName.STRING : type.getStandardType();
switch (standardType) {
case FLOAT64:
// float is a float64, so corresponding type becomes double
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.cloud.bigquery.FieldList;
import com.google.cloud.bigquery.FieldValue;
import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.TableResult;
import com.google.common.io.BaseEncoding;
Expand Down Expand Up @@ -159,4 +160,13 @@ private String paddHeaddingZero(String value, int length) {
}
return Strings.repeat('0', length - value.length()) + value;
}

@Test
public void testJsonFieldConversionToString() {
Field field = Field.newBuilder("demo", LegacySQLTypeName.valueOf("JSON")).build();
String jsonValue = "{\"key\":\"value\"}";
FieldValue fieldValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, jsonValue);
Object result = BigQueryDataParser.convertValue(field, fieldValue);
Assert.assertEquals(jsonValue, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.cdap.plugin.gcp.bigquery.util;

import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.StandardSQLTypeName;
import io.cdap.cdap.api.data.schema.Schema;
import io.cdap.cdap.etl.api.FailureCollector;
Expand Down Expand Up @@ -470,4 +471,11 @@ public void testJobLabelWithValueStartingWithCaptialLetter() {
collector.getValidationFailures().get(0).getMessage());
}

@Test
public void testConvertFieldTypeJsonToString() {
Field field = Field.newBuilder("demo", LegacySQLTypeName.valueOf("JSON")).build();
Schema expectedSchema = Schema.of(Schema.Type.STRING);
Schema result = BigQueryUtil.convertFieldType(field, null, null);
Assert.assertEquals(expectedSchema, result);
}
}

0 comments on commit 8728d58

Please sign in to comment.