-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added BigQuerySchemas as a public API to return only Schemas from BigQuery, without creating tables in a real environment. Changed JavaConverters to accept Seqs * Seq in Java converters for Scala 2.13+ * BigQuerySchemas with `schema(value: A)` being A any Product type. * CHANGELOG.md and version * removing unused code. Test renamed to Spec
- Loading branch information
1 parent
2f530e7
commit 617371f
Showing
8 changed files
with
116 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
bigquery/src/main/scala/org/datatools/bigdatatypes/bigquery/BigQuerySchemas.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.datatools.bigdatatypes.bigquery | ||
|
||
import com.google.cloud.bigquery.Schema | ||
import org.datatools.bigdatatypes.bigquery.JavaConverters.toJava | ||
|
||
/** | ||
* Public API for generating BigQuery Schemas. | ||
* Any type implementing [[SqlTypeToBigQuery]] or [[SqlInstanceToBigQuery]] can be converted into a BigQuery [[Schema]] | ||
* If multiple types are given, the resulting schema will be the concatenation of them. | ||
*/ | ||
object BigQuerySchemas { | ||
|
||
/** | ||
* Given any type that implements [[SqlTypeToBigQuery]] returns the BigQuery Schema for that type | ||
* @tparam A is any type implementing [[SqlTypeToBigQuery]] | ||
* @return [[Schema]] ready to be used in BigQuery | ||
*/ | ||
def schema[A: SqlTypeToBigQuery]: Schema = BigQueryDefinitions.generateSchema[A] | ||
def schema[A: SqlTypeToBigQuery, B: SqlTypeToBigQuery]: Schema = BigQueryDefinitions.generateSchema[A, B] | ||
def schema[A: SqlTypeToBigQuery, B: SqlTypeToBigQuery, C: SqlTypeToBigQuery]: Schema = BigQueryDefinitions.generateSchema[A, B, C] | ||
def schema[A: SqlTypeToBigQuery, B: SqlTypeToBigQuery, C: SqlTypeToBigQuery, D: SqlTypeToBigQuery]: Schema = BigQueryDefinitions.generateSchema[A, B, C, D] | ||
def schema[A: SqlTypeToBigQuery, B: SqlTypeToBigQuery, C: SqlTypeToBigQuery, D: SqlTypeToBigQuery, E: SqlTypeToBigQuery]: Schema = BigQueryDefinitions.generateSchema[A, B, C, D, E] | ||
|
||
/** | ||
* Given an instance of a Product, extracts the BQ [[Schema]] from its type | ||
* @param value an instance of any Product | ||
* @tparam A is any Product type | ||
* @return [[Schema]] with the same structure as the given input | ||
*/ | ||
def schema[A <: Product](value: A)(implicit a: SqlTypeToBigQuery[A]): Schema = | ||
Schema.of(toJava(SqlTypeToBigQuery[A].bigQueryFields)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
bigquery/src/test/scala/org/datatools/bigdatatypes/bigquery/BigQuerySchemasSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.datatools.bigdatatypes.bigquery | ||
|
||
import com.google.cloud.bigquery.Field.Mode | ||
import com.google.cloud.bigquery.{Field, Schema, StandardSQLTypeName} | ||
import org.datatools.bigdatatypes.TestTypes.ListOfStruct | ||
import org.datatools.bigdatatypes.bigquery.JavaConverters.toJava | ||
import org.datatools.bigdatatypes.{BigQueryTestTypes, UnitSpec} | ||
import org.datatools.bigdatatypes.formats.Formats.implicitDefaultFormats | ||
import org.datatools.bigdatatypes.conversions.SqlTypeConversion.* | ||
|
||
class BigQuerySchemasSpec extends UnitSpec { | ||
|
||
val elements1: Seq[Field] = List( | ||
Field.newBuilder("a", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build() | ||
) | ||
val elements2: Seq[Field] = List( | ||
Field.newBuilder("a", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build(), | ||
Field.newBuilder("b", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build() | ||
) | ||
val elements3: Seq[Field] = List( | ||
Field.newBuilder("a", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build(), | ||
Field.newBuilder("b", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build(), | ||
Field.newBuilder("c", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build() | ||
) | ||
val elements4: Seq[Field] = List( | ||
Field.newBuilder("a", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build(), | ||
Field.newBuilder("b", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build(), | ||
Field.newBuilder("c", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build(), | ||
Field.newBuilder("d", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build() | ||
) | ||
val elements5: Seq[Field] = List( | ||
Field.newBuilder("a", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build(), | ||
Field.newBuilder("b", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build(), | ||
Field.newBuilder("c", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build(), | ||
Field.newBuilder("d", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build(), | ||
Field.newBuilder("e", StandardSQLTypeName.INT64).setMode(Mode.REQUIRED).build() | ||
) | ||
case class Simple1(a: Int) | ||
case class Simple2(b: Int) | ||
case class Simple3(c: Int) | ||
case class Simple4(d: Int) | ||
case class Simple5(e: Int) | ||
|
||
behavior of "BigQuerySchemas" | ||
|
||
"Case class with Struct List" should "be converted into BQ Schema" in { | ||
val fields: Schema = BigQuerySchemas.schema[ListOfStruct] | ||
fields shouldBe Schema.of(toJava(BigQueryTestTypes.basicNestedWithList)) | ||
} | ||
|
||
"2 classes" should "be converted into a BQ Schema" in { | ||
BigQuerySchemas.schema[Simple1, Simple2] shouldBe Schema.of(toJava(elements2)) | ||
} | ||
|
||
"3 classes" should "be converted into a BQ Schema" in { | ||
BigQuerySchemas.schema[Simple1, Simple2, Simple3] shouldBe Schema.of(toJava(elements3)) | ||
} | ||
|
||
"4 classes" should "be converted into a BQ Schema" in { | ||
BigQuerySchemas.schema[Simple1, Simple2, Simple3, Simple4] shouldBe Schema.of(toJava(elements4)) | ||
} | ||
|
||
"5 classes" should "be converted into a BQ Schema" in { | ||
BigQuerySchemas.schema[Simple1, Simple2, Simple3, Simple4, Simple5] shouldBe Schema.of(toJava(elements5)) | ||
} | ||
|
||
"An instance" should "be converted into a BQ Schema" in { | ||
val s = Simple1(1) | ||
BigQuerySchemas.schema[Simple1](s) shouldBe Schema.of(toJava(elements1)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters