Skip to content

Commit 204a59d

Browse files
committed
add AvroFunctionsSuite
1 parent 9c185d5 commit 204a59d

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.avro
19+
20+
import org.apache.avro.Schema
21+
22+
import org.apache.spark.sql.QueryTest
23+
import org.apache.spark.sql.functions.struct
24+
import org.apache.spark.sql.test.SharedSQLContext
25+
26+
class AvroFunctionsSuite extends QueryTest with SharedSQLContext {
27+
import testImplicits._
28+
29+
test("roundtrip in to_avro and from_avro - int and string") {
30+
val df = spark.range(10).select('id, 'id.cast("string").as("str"))
31+
32+
val avroDF = df.select(to_avro('id).as("a"), to_avro('str).as("b"))
33+
val avroTypeLong = new Schema.Parser().parse(
34+
s"""
35+
|{
36+
| "type": "long",
37+
| "name": "id"
38+
|}
39+
""".stripMargin)
40+
val avroTypeStr = new Schema.Parser().parse(
41+
s"""
42+
|{
43+
| "type": "string",
44+
| "name": "str"
45+
|}
46+
""".stripMargin)
47+
checkAnswer(avroDF.select(from_avro('a, avroTypeLong), from_avro('b, avroTypeStr)), df)
48+
}
49+
50+
test("roundtrip in to_avro and from_avro - struct") {
51+
val df = spark.range(10).select(struct('id, 'id.cast("string").as("str")).as("struct"))
52+
val avroStructDF = df.select(to_avro('struct).as("avro"))
53+
val avroTypeStruct = new Schema.Parser().parse(
54+
s"""
55+
|{
56+
| "type": "record",
57+
| "name": "struct",
58+
| "fields": [
59+
| {"name": "col1", "type": "long"},
60+
| {"name": "col2", "type": "string"}
61+
| ]
62+
|}
63+
""".stripMargin)
64+
checkAnswer(avroStructDF.select(from_avro('avro, avroTypeStruct)), df)
65+
}
66+
67+
test("roundtrip in to_avro and from_avro - array with null") {
68+
val dfOne = Seq(Tuple1(Tuple1(1) :: Nil), Tuple1(null :: Nil)).toDF("array")
69+
val avroTypeArrStruct = new Schema.Parser().parse(
70+
s"""
71+
|[ {
72+
| "type" : "array",
73+
| "items" : [ {
74+
| "type" : "record",
75+
| "name" : "x",
76+
| "fields" : [ {
77+
| "name" : "y",
78+
| "type" : "int"
79+
| } ]
80+
| }, "null" ]
81+
|}, "null" ]
82+
""".stripMargin)
83+
val readBackOne = dfOne.select(to_avro($"array").as("avro"))
84+
.select(from_avro($"avro", avroTypeArrStruct).as("array"))
85+
checkAnswer(dfOne, readBackOne)
86+
}
87+
}

0 commit comments

Comments
 (0)