From 93374564e18dfbac7e85aa08e385867c5feddefd Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Tue, 12 Feb 2019 22:55:18 +0530 Subject: [PATCH 1/5] added Feature StandardSQLTypeName in Field.newBuilder() --- .../java/com/google/cloud/bigquery/Field.java | 10 +++ .../cloud/bigquery/LegacySQLTypeName.java | 14 +++++ .../com/google/cloud/bigquery/FieldTest.java | 63 +++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java index ba0e45e5f358..50885f53412c 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java @@ -241,11 +241,21 @@ public static Builder newBuilder(String name, LegacySQLTypeName type, Field... s return new Builder().setName(name).setType(type, subFields); } + /** Returns a builder for a Field object with given name and type. */ + public static Builder newBuilder(String name, StandardSQLTypeName type, Field... subFields) { + return new Builder().setName(name).setType(LegacySQLTypeName.legacySQLTypeName(type), subFields); + } + /** Returns a builder for a Field object with given name and type. */ public static Builder newBuilder(String name, LegacySQLTypeName type, FieldList subFields) { return new Builder().setName(name).setType(type, subFields); } + /** Returns a builder for a Field object with given name and type. */ + public static Builder newBuilder(String name, StandardSQLTypeName type, FieldList subFields) { + return new Builder().setName(name).setType(LegacySQLTypeName.legacySQLTypeName(type), subFields); + } + TableFieldSchema toPb() { TableFieldSchema fieldSchemaPb = new TableFieldSchema(); fieldSchemaPb.setName(name); diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java index 9c9b12ac8529..5391d56e268d 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java @@ -19,6 +19,8 @@ import com.google.api.core.ApiFunction; import com.google.cloud.StringEnumType; import com.google.cloud.StringEnumValue; +import java.util.HashMap; +import java.util.Map; /** * A type used in legacy SQL contexts. NOTE: some contexts use a mix of types; for example, for @@ -84,6 +86,14 @@ public LegacySQLTypeName apply(String constant) { public static final LegacySQLTypeName RECORD = type.createAndRegister("RECORD").setStandardType(StandardSQLTypeName.STRUCT); + private static Map standardToLegacyMap = new HashMap<>(); + + static { + for (LegacySQLTypeName legacySqlTypeName : LegacySQLTypeName.values()) { + standardToLegacyMap.put(legacySqlTypeName.equivalent, legacySqlTypeName); + } + } + private StandardSQLTypeName equivalent; private LegacySQLTypeName setStandardType(StandardSQLTypeName equivalent) { @@ -96,6 +106,10 @@ public StandardSQLTypeName getStandardType() { return equivalent; } + public static LegacySQLTypeName legacySQLTypeName(StandardSQLTypeName type) { + return standardToLegacyMap.get(type); + } + private LegacySQLTypeName(String constant) { super(constant); } diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/FieldTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/FieldTest.java index 4ba5ad0929a0..14900cc5d423 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/FieldTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/FieldTest.java @@ -54,6 +54,25 @@ public class FieldTest { .setMode(FIELD_MODE3) .setDescription(FIELD_DESCRIPTION3) .build(); + private static final Field STANDARD_FIELD_SCHEMA1 = + Field.newBuilder(FIELD_NAME1, StandardSQLTypeName.STRING) + .setMode(FIELD_MODE1) + .setDescription(FIELD_DESCRIPTION1) + .build(); + private static final Field STANDARD_FIELD_SCHEMA2 = + Field.newBuilder(FIELD_NAME2, StandardSQLTypeName.INT64) + .setMode(FIELD_MODE2) + .setDescription(FIELD_DESCRIPTION2) + .build(); + private static final Field STANDARD_FIELD_SCHEMA3 = + Field.newBuilder( + FIELD_NAME3, + StandardSQLTypeName.STRUCT, + STANDARD_FIELD_SCHEMA1, + STANDARD_FIELD_SCHEMA2) + .setMode(FIELD_MODE3) + .setDescription(FIELD_DESCRIPTION3) + .build(); @Test public void testToBuilder() { @@ -66,6 +85,17 @@ public void testToBuilder() { compareFieldSchemas(FIELD_SCHEMA1, field); } + @Test + public void testToBuilderWithStandardSQLTypeName() { + compareFieldSchemas(STANDARD_FIELD_SCHEMA1, STANDARD_FIELD_SCHEMA1.toBuilder().build()); + compareFieldSchemas(STANDARD_FIELD_SCHEMA2, STANDARD_FIELD_SCHEMA2.toBuilder().build()); + compareFieldSchemas(STANDARD_FIELD_SCHEMA3, STANDARD_FIELD_SCHEMA3.toBuilder().build()); + Field field = STANDARD_FIELD_SCHEMA1.toBuilder().setDescription("New Description").build(); + assertEquals("New Description", field.getDescription()); + field = field.toBuilder().setDescription(FIELD_DESCRIPTION1).build(); + compareFieldSchemas(STANDARD_FIELD_SCHEMA1, field); + } + @Test public void testToBuilderIncomplete() { Field field = Field.of(FIELD_NAME1, FIELD_TYPE1); @@ -74,6 +104,14 @@ public void testToBuilderIncomplete() { compareFieldSchemas(field, field.toBuilder().build()); } + @Test + public void testToBuilderIncompleteWithStandardSQLTypeName() { + Field field = Field.of(FIELD_NAME1, FIELD_TYPE1); + compareFieldSchemas(field, field.toBuilder().build()); + field = Field.of(FIELD_NAME2, FIELD_TYPE3, STANDARD_FIELD_SCHEMA1, STANDARD_FIELD_SCHEMA2); + compareFieldSchemas(field, field.toBuilder().build()); + } + @Test public void testBuilder() { assertEquals(FIELD_NAME1, FIELD_SCHEMA1.getName()); @@ -88,6 +126,22 @@ public void testBuilder() { assertEquals(FieldList.of(FIELD_SCHEMA1, FIELD_SCHEMA2), FIELD_SCHEMA3.getSubFields()); } + @Test + public void testBuilderWithStandardSQLTypeName() { + assertEquals(FIELD_NAME1, STANDARD_FIELD_SCHEMA1.getName()); + assertEquals(FIELD_TYPE1, STANDARD_FIELD_SCHEMA1.getType()); + assertEquals(FIELD_MODE1, STANDARD_FIELD_SCHEMA1.getMode()); + assertEquals(FIELD_DESCRIPTION1, STANDARD_FIELD_SCHEMA1.getDescription()); + assertEquals(null, STANDARD_FIELD_SCHEMA1.getSubFields()); + assertEquals(FIELD_NAME3, STANDARD_FIELD_SCHEMA3.getName()); + assertEquals(FIELD_TYPE3, STANDARD_FIELD_SCHEMA3.getType()); + assertEquals(FIELD_MODE3, STANDARD_FIELD_SCHEMA3.getMode()); + assertEquals(FIELD_DESCRIPTION3, STANDARD_FIELD_SCHEMA3.getDescription()); + assertEquals( + FieldList.of(STANDARD_FIELD_SCHEMA1, STANDARD_FIELD_SCHEMA2), + STANDARD_FIELD_SCHEMA3.getSubFields()); + } + @Test public void testToAndFromPb() { compareFieldSchemas(FIELD_SCHEMA1, Field.fromPb(FIELD_SCHEMA1.toPb())); @@ -97,6 +151,15 @@ public void testToAndFromPb() { compareFieldSchemas(field, Field.fromPb(field.toPb())); } + @Test + public void testToAndFromPbWithStandardSQLTypeName() { + compareFieldSchemas(STANDARD_FIELD_SCHEMA1, Field.fromPb(STANDARD_FIELD_SCHEMA1.toPb())); + compareFieldSchemas(STANDARD_FIELD_SCHEMA2, Field.fromPb(STANDARD_FIELD_SCHEMA2.toPb())); + compareFieldSchemas(STANDARD_FIELD_SCHEMA3, Field.fromPb(STANDARD_FIELD_SCHEMA3.toPb())); + Field field = Field.newBuilder(FIELD_NAME1, FIELD_TYPE1).build(); + compareFieldSchemas(field, Field.fromPb(field.toPb())); + } + @Test public void testSubFieldWithClonedType() throws Exception { LegacySQLTypeName record = LegacySQLTypeName.RECORD; From 007ffab5d10a0df030449f08b17e7c9ccadf83a2 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Tue, 12 Feb 2019 23:43:18 +0530 Subject: [PATCH 2/5] modified methods --- .../java/com/google/cloud/bigquery/Field.java | 49 ++++++++++++++++++- .../cloud/bigquery/LegacySQLTypeName.java | 1 + 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java index 50885f53412c..f5b931b3b314 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java @@ -112,6 +112,22 @@ public Builder setType(LegacySQLTypeName type, Field... subFields) { return setType(type, subFields.length > 0 ? FieldList.of(subFields) : null); } + /** + * Sets the type of the field. + * + * @param type BigQuery data type + * @param subFields nested schema fields in case if {@code type} is {@link + * StandardSQLTypeName#STRUCT}, empty otherwise + * @throws IllegalArgumentException if {@code type == StandardSQLTypeName.STRUCT && + * subFields.length == 0} or if {@code type != StandardSQLTypeName.STRUCT && + * subFields.length != 0} + * @see Data + * Types + */ + public Builder setType(StandardSQLTypeName type, Field... subFields) { + return setType(type, subFields.length > 0 ? FieldList.of(subFields) : null); + } + /** * Sets the type of the field. * @@ -142,6 +158,35 @@ public Builder setType(LegacySQLTypeName type, FieldList subFields) { return this; } + /** + * Sets the type of the field. + * + * @param type BigQuery data type + * @param subFields nested schema fields, in case if {@code type} is {@link + * StandardSQLTypeName#STRUCT}, {@code null} otherwise. + * @throws IllegalArgumentException if {@code type == StandardSQLTypeName.STRUCT && (subFields + * == null || subFields.isEmpty())} or if {@code type != StandardSQLTypeName.STRUCT && + * subFields != null} + * @see Data + * Types + */ + public Builder setType(StandardSQLTypeName type, FieldList subFields) { + if (StandardSQLTypeName.STRUCT.equals(type)) { + if (subFields == null || subFields.isEmpty()) { + throw new IllegalArgumentException( + "The " + type + " field must have at least one sub-field"); + } + } else { + if (subFields != null) { + throw new IllegalArgumentException( + "Only " + StandardSQLTypeName.STRUCT + " fields can have sub-fields"); + } + } + this.type = LegacySQLTypeName.legacySQLTypeName(type); + this.subFields = subFields; + return this; + } + /** Sets the mode of the field. When not specified {@link Mode#NULLABLE} is used. */ public Builder setMode(Mode mode) { this.mode = mode != null ? mode.name() : Data.nullOf(String.class); @@ -243,7 +288,7 @@ public static Builder newBuilder(String name, LegacySQLTypeName type, Field... s /** Returns a builder for a Field object with given name and type. */ public static Builder newBuilder(String name, StandardSQLTypeName type, Field... subFields) { - return new Builder().setName(name).setType(LegacySQLTypeName.legacySQLTypeName(type), subFields); + return new Builder().setName(name).setType(type, subFields); } /** Returns a builder for a Field object with given name and type. */ @@ -253,7 +298,7 @@ public static Builder newBuilder(String name, LegacySQLTypeName type, FieldList /** Returns a builder for a Field object with given name and type. */ public static Builder newBuilder(String name, StandardSQLTypeName type, FieldList subFields) { - return new Builder().setName(name).setType(LegacySQLTypeName.legacySQLTypeName(type), subFields); + return new Builder().setName(name).setType(type, subFields); } TableFieldSchema toPb() { diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java index 5391d56e268d..d25e23ed6d0f 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java @@ -106,6 +106,7 @@ public StandardSQLTypeName getStandardType() { return equivalent; } + /** Represents LegacySQLTypeName from StandardSQLTypeName */ public static LegacySQLTypeName legacySQLTypeName(StandardSQLTypeName type) { return standardToLegacyMap.get(type); } From 07cd27bc72102aec98e5c2a8e3b0f4c850039422 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Tue, 12 Feb 2019 23:58:07 +0530 Subject: [PATCH 3/5] updated method --- .../java/com/google/cloud/bigquery/Field.java | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java index f5b931b3b314..54fb7bd53d46 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java @@ -125,7 +125,7 @@ public Builder setType(LegacySQLTypeName type, Field... subFields) { * Types */ public Builder setType(StandardSQLTypeName type, Field... subFields) { - return setType(type, subFields.length > 0 ? FieldList.of(subFields) : null); + return setType(LegacySQLTypeName.legacySQLTypeName(type), subFields.length > 0 ? FieldList.of(subFields) : null); } /** @@ -162,29 +162,16 @@ public Builder setType(LegacySQLTypeName type, FieldList subFields) { * Sets the type of the field. * * @param type BigQuery data type - * @param subFields nested schema fields, in case if {@code type} is {@link - * StandardSQLTypeName#STRUCT}, {@code null} otherwise. - * @throws IllegalArgumentException if {@code type == StandardSQLTypeName.STRUCT && (subFields - * == null || subFields.isEmpty())} or if {@code type != StandardSQLTypeName.STRUCT && - * subFields != null} + * @param subFields nested schema fields in case if {@code type} is {@link + * StandardSQLTypeName#STRUCT}, empty otherwise + * @throws IllegalArgumentException if {@code type == StandardSQLTypeName.STRUCT && + * subFields.length == 0} or if {@code type != StandardSQLTypeName.STRUCT && + * subFields.length != 0} * @see Data * Types */ public Builder setType(StandardSQLTypeName type, FieldList subFields) { - if (StandardSQLTypeName.STRUCT.equals(type)) { - if (subFields == null || subFields.isEmpty()) { - throw new IllegalArgumentException( - "The " + type + " field must have at least one sub-field"); - } - } else { - if (subFields != null) { - throw new IllegalArgumentException( - "Only " + StandardSQLTypeName.STRUCT + " fields can have sub-fields"); - } - } - this.type = LegacySQLTypeName.legacySQLTypeName(type); - this.subFields = subFields; - return this; + return setType(LegacySQLTypeName.legacySQLTypeName(type), subFields); } /** Sets the mode of the field. When not specified {@link Mode#NULLABLE} is used. */ From 959c16e1851e8aa36d116af9d60b6eadde7afbb1 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Wed, 13 Feb 2019 00:27:12 +0530 Subject: [PATCH 4/5] fix format --- .../src/main/java/com/google/cloud/bigquery/Field.java | 4 +++- .../java/com/google/cloud/bigquery/LegacySQLTypeName.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java index 54fb7bd53d46..ffc8b3779aa6 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Field.java @@ -125,7 +125,9 @@ public Builder setType(LegacySQLTypeName type, Field... subFields) { * Types */ public Builder setType(StandardSQLTypeName type, Field... subFields) { - return setType(LegacySQLTypeName.legacySQLTypeName(type), subFields.length > 0 ? FieldList.of(subFields) : null); + return setType( + LegacySQLTypeName.legacySQLTypeName(type), + subFields.length > 0 ? FieldList.of(subFields) : null); } /** diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java index d25e23ed6d0f..1eabef945f97 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java @@ -108,7 +108,7 @@ public StandardSQLTypeName getStandardType() { /** Represents LegacySQLTypeName from StandardSQLTypeName */ public static LegacySQLTypeName legacySQLTypeName(StandardSQLTypeName type) { - return standardToLegacyMap.get(type); + return standardToLegacyMap.get(type); } private LegacySQLTypeName(String constant) { From 8d805f59eefb47c9ca63109302bbfe4400138b02 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Wed, 13 Feb 2019 21:39:16 +0530 Subject: [PATCH 5/5] update comment --- .../main/java/com/google/cloud/bigquery/LegacySQLTypeName.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java index 1eabef945f97..035ae8ed9cb0 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LegacySQLTypeName.java @@ -106,7 +106,7 @@ public StandardSQLTypeName getStandardType() { return equivalent; } - /** Represents LegacySQLTypeName from StandardSQLTypeName */ + /** Converts StandardSQLTypeName to LegacySQLTypeName */ public static LegacySQLTypeName legacySQLTypeName(StandardSQLTypeName type) { return standardToLegacyMap.get(type); }