/
BigQueryTypeIT.scala
215 lines (179 loc) · 7.58 KB
/
BigQueryTypeIT.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright 2016 Spotify AB.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.spotify.scio.bigquery.types
import com.spotify.scio.bigquery.BigQueryClient
import org.scalatest.{Assertion, FlatSpec, Matchers}
import scala.annotation.StaticAnnotation
import scala.collection.JavaConverters._
import scala.reflect.runtime.universe._
object BigQueryTypeIT {
@BigQueryType.fromQuery(
"SELECT word, word_count FROM [bigquery-public-data:samples.shakespeare] WHERE word = 'Romeo'")
class LegacyT
@BigQueryType.fromQuery(
"SELECT word, word_count FROM `bigquery-public-data.samples.shakespeare` WHERE word = 'Romeo'")
class SqlT
@BigQueryType.fromTable("bigquery-public-data:samples.shakespeare")
class FromTableT
@BigQueryType.fromQuery(
"SELECT word, word_count FROM [data-integration-test:partition_a.table_%s]", "$LATEST")
class LegacyLatestT
@BigQueryType.fromQuery(
"SELECT word, word_count FROM `data-integration-test.partition_a.table_%s`", "$LATEST")
class SqlLatestT
@BigQueryType.fromQuery(
"SELECT word, word_count FROM `data-integration-test.partition_a.table_%s` LIMIT %d", "$LATEST", 1)
class SqlLatestTWithMultiArgs
@BigQueryType.fromTable("data-integration-test:partition_a.table_%s", "$LATEST")
class FromTableLatestT
@BigQueryType.toTable
case class ToTableT(word: String, word_count: Int)
class Annotation1 extends StaticAnnotation
class Annotation2 extends StaticAnnotation
@Annotation1
@BigQueryType.fromTable("bigquery-public-data:samples.shakespeare")
@Annotation2
class ShakespeareWithSurroundingAnnotations
@BigQueryType.fromTable("bigquery-public-data:samples.shakespeare")
@Annotation1
@Annotation2
class ShakespeareWithSequentialAnnotations
// run this to re-populate tables used for this test and BigQueryPartitionUtilIT
def main(args: Array[String]): Unit = {
val bq = BigQueryClient.defaultInstance()
val data = List(ToTableT("a", 1), ToTableT("b", 2))
bq.writeTypedRows("data-integration-test:partition_a.table_20170101", data)
bq.writeTypedRows("data-integration-test:partition_a.table_20170102", data)
bq.writeTypedRows("data-integration-test:partition_a.table_20170103", data)
bq.writeTypedRows("data-integration-test:partition_b.table_20170101", data)
bq.writeTypedRows("data-integration-test:partition_b.table_20170102", data)
bq.writeTypedRows("data-integration-test:partition_c.table_20170104", data)
}
}
class BigQueryTypeIT extends FlatSpec with Matchers {
import BigQueryTypeIT._
val bq = BigQueryClient.defaultInstance()
val legacyQuery =
"SELECT word, word_count FROM [bigquery-public-data:samples.shakespeare] WHERE word = 'Romeo'"
val sqlQuery =
"SELECT word, word_count FROM `bigquery-public-data.samples.shakespeare` WHERE word = 'Romeo'"
val legacyLatestQuery =
"SELECT word, word_count FROM [data-integration-test:partition_a.table_%s]"
val sqlLatestQuery =
"SELECT word, word_count FROM `data-integration-test.partition_a.table_%s`"
"fromQuery" should "work with legacy syntax" in {
val bqt = BigQueryType[LegacyT]
bqt.isQuery shouldBe true
bqt.isTable shouldBe false
bqt.query shouldBe Some(legacyQuery)
bqt.table shouldBe None
val fields = bqt.schema.getFields.asScala
fields.size shouldBe 2
fields.map(_.getName) shouldBe Seq("word", "word_count")
fields.map(_.getType) shouldBe Seq("STRING", "INTEGER")
fields.map(_.getMode) shouldBe Seq("REQUIRED", "REQUIRED")
}
it should "work with SQL syntax" in {
val bqt = BigQueryType[SqlT]
bqt.isQuery shouldBe true
bqt.isTable shouldBe false
bqt.query shouldBe Some(sqlQuery)
bqt.table shouldBe None
val fields = bqt.schema.getFields.asScala
fields.size shouldBe 2
fields.map(_.getName) shouldBe Seq("word", "word_count")
fields.map(_.getType) shouldBe Seq("STRING", "INTEGER")
fields.map(_.getMode) shouldBe Seq("NULLABLE", "NULLABLE")
}
it should "round trip rows with legacy syntax" in {
val bqt = BigQueryType[LegacyT]
val rows = bq.getQueryRows(legacyQuery).toList
val typed = Seq(LegacyT("Romeo", 117L))
rows.map(bqt.fromTableRow) shouldBe typed
typed.map(bqt.toTableRow).map(bqt.fromTableRow) shouldBe typed
}
it should "round trip rows with SQL syntax" in {
val bqt = BigQueryType[SqlT]
val rows = bq.getQueryRows(sqlQuery).toList
val typed = Seq(SqlT(Some("Romeo"), Some(117L)))
rows.map(bqt.fromTableRow) shouldBe typed
typed.map(bqt.toTableRow).map(bqt.fromTableRow) shouldBe typed
}
it should "work with legacy syntax with $LATEST" in {
BigQueryType[LegacyLatestT].query shouldBe Some(legacyLatestQuery)
}
it should "work with SQL syntax with $LATEST" in {
BigQueryType[SqlLatestT].query shouldBe Some(sqlLatestQuery)
}
it should "have query fn" in {
"""LegacyLatestT.query("TABLE")""" should compile
"""SqlLatestT.query("TABLE")""" should compile
}
it should "have query fn with only 1 argument" in {
"""LegacyLatestT.query("TABLE", 1)""" shouldNot typeCheck
"""SqlLatestT.query("TABLE", 1)""" shouldNot typeCheck
}
it should "have query fn with multiple arguments" in {
"""SqlLatestTWithMultiArgs.query("TABLE", 1)""" should compile
"""SqlLatestTWithMultiArgs.query(1, "TABLE")""" shouldNot typeCheck
}
it should "format query" in {
LegacyLatestT.query("TABLE") shouldBe legacyLatestQuery.format("TABLE")
SqlLatestT.query("TABLE") shouldBe sqlLatestQuery.format("TABLE")
}
"fromTable" should "work" in {
val bqt = BigQueryType[FromTableT]
bqt.isQuery shouldBe false
bqt.isTable shouldBe true
bqt.query shouldBe None
bqt.table shouldBe Some("bigquery-public-data:samples.shakespeare")
val fields = bqt.schema.getFields.asScala
fields.size shouldBe 4
fields.map(_.getName) shouldBe Seq("word", "word_count", "corpus", "corpus_date")
fields.map(_.getType) shouldBe Seq("STRING", "INTEGER", "STRING", "INTEGER")
fields.map(_.getMode) shouldBe Seq("REQUIRED", "REQUIRED", "REQUIRED", "REQUIRED")
}
it should "work with $LATEST" in {
BigQueryType[FromTableLatestT].table shouldBe Some("data-integration-test:partition_a.table_%s")
}
def containsAllAnnotTypes[T: TypeTag]: Assertion = {
val types = typeOf[T]
.typeSymbol
.annotations
.map(_.tree.tpe)
Seq(typeOf[Annotation1], typeOf[Annotation2])
.forall(lt => types.exists(rt => lt =:= rt)) shouldBe true
}
it should "preserve surrounding user defined annotations" in {
containsAllAnnotTypes[ShakespeareWithSurroundingAnnotations]
}
it should "preserve sequential user defined annotations" in {
containsAllAnnotTypes[ShakespeareWithSequentialAnnotations]
}
"toTable" should "work" in {
val bqt = BigQueryType[ToTableT]
bqt.isQuery shouldBe false
bqt.isTable shouldBe false
bqt.query shouldBe None
bqt.table shouldBe None
val fields = bqt.schema.getFields.asScala
fields.size shouldBe 2
fields.map(_.getName) shouldBe Seq("word", "word_count")
fields.map(_.getType) shouldBe Seq("STRING", "INTEGER")
fields.map(_.getMode) shouldBe Seq("REQUIRED", "REQUIRED")
}
}