Skip to content

Commit 2cb43f6

Browse files
committed
extend + refactor unit test
1 parent 7e9cd2c commit 2cb43f6

File tree

5 files changed

+94
-25
lines changed

5 files changed

+94
-25
lines changed

coral-schema/src/test/java/com/linkedin/coral/schema/avro/TestUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.linkedin.coral.common.HiveMetastoreClient;
3333
import com.linkedin.coral.common.HiveMscAdapter;
3434
import com.linkedin.coral.common.functions.FunctionReturnTypes;
35+
import com.linkedin.coral.hive.hive2rel.functions.OrdinalReturnTypeInferenceV2;
3536
import com.linkedin.coral.hive.hive2rel.functions.StaticHiveFunctionRegistry;
3637

3738
import static org.apache.calcite.sql.type.OperandTypes.*;
@@ -82,7 +83,7 @@ public static void registerUdfs() {
8283
.rowOf(ImmutableList.of("isEven", "number"), ImmutableList.of(SqlTypeName.BOOLEAN, SqlTypeName.INTEGER)),
8384
family(SqlTypeFamily.INTEGER));
8485
StaticHiveFunctionRegistry.createAddUserDefinedFunction(
85-
"com.linkedin.coral.hive.hive2rel.CoralTestUDFReturnSecondArg", ReturnTypes.ARG1,
86+
"com.linkedin.coral.hive.hive2rel.CoralTestUDFReturnSecondArg", new OrdinalReturnTypeInferenceV2(1),
8687
family(SqlTypeFamily.STRING, SqlTypeFamily.ANY));
8788
}
8889

@@ -107,7 +108,7 @@ private static void initializeTables() {
107108
String baseComplexNullableWithDefaults = loadSchema("base-complex-nullable-with-defaults.avsc");
108109
String basePrimitive = loadSchema("base-primitive.avsc");
109110
String baseComplexNestedStructSameName = loadSchema("base-complex-nested-struct-same-name.avsc");
110-
String testNestedStructSchema = loadSchema("testNestedStructInnerField.avsc");
111+
String baseComplexMixedNullabilities = loadSchema("base-complex-mixed-nullabilities.avsc");
111112

112113
executeCreateTableQuery("default", "basecomplex", baseComplexSchema);
113114
executeCreateTableQuery("default", "basecomplexunioncompatible", baseComplexUnionCompatible);
@@ -129,7 +130,7 @@ private static void initializeTables() {
129130
executeCreateTableWithPartitionQuery("default", "basenestedcomplex", baseNestedComplexSchema);
130131
executeCreateTableWithPartitionQuery("default", "basecomplexnullablewithdefaults", baseComplexNullableWithDefaults);
131132
executeCreateTableWithPartitionQuery("default", "basecomplexnonnullable", baseComplexNonNullable);
132-
executeCreateTableWithPartitionQuery("default", "nestedStructInnerField", testNestedStructSchema);
133+
executeCreateTableWithPartitionQuery("default", "basecomplexmixednullabilities", baseComplexMixedNullabilities);
133134

134135
String baseComplexSchemaWithDoc = loadSchema("docTestResources/base-complex-with-doc.avsc");
135136
String baseEnumSchemaWithDoc = loadSchema("docTestResources/base-enum-with-doc.avsc");

coral-schema/src/test/java/com/linkedin/coral/schema/avro/ViewToAvroSchemaConverterTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,22 @@ public void testUdfLessThanHundred() {
234234
}
235235

236236
@Test
237-
public void testInnerFieldNullabilityAfterApplyingUDF() {
237+
public void testPreserveNullabilitiesAfterApplyingOrdinalReturnTypeUDF() {
238238
String viewSql = "CREATE VIEW innerfield_with_udf "
239239
+ "tblproperties('functions' = 'ReturnInnerStuct:com.linkedin.coral.hive.hive2rel.CoralTestUDFReturnSecondArg', "
240240
+ " 'dependencies' = 'ivy://com.linkedin:udf:1.0') " + "AS "
241241
+ "SELECT default_innerfield_with_udf_ReturnInnerStuct('foo', innerRecord) AS innerRecord "
242-
+ "FROM nestedStructInnerField";
242+
+ "FROM basecomplexmixednullabilities";
243243

244244
TestUtils.executeCreateViewQuery("default", "innerfield_with_udf", viewSql);
245245

246246
ViewToAvroSchemaConverter viewToAvroSchemaConverter = ViewToAvroSchemaConverter.create(hiveMetastoreClient);
247247
Schema actualSchema = viewToAvroSchemaConverter.toAvroSchema("default", "innerfield_with_udf");
248-
System.out.println(actualSchema);
248+
249+
// Expect all fields to retain their nullability after applying the UDF, CoralTestUDFReturnSecondArg, that simply
250+
// returns the second argument as is
251+
Assert.assertEquals(actualSchema.toString(true),
252+
TestUtils.loadSchema("testPreserveNullabilitiesAfterApplyingOrdinalReturnTypeUDF-expected.avsc"));
249253
}
250254

251255
@Test
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"type": "record",
3+
"name": "OuterRecord",
4+
"fields": [
5+
{
6+
"name": "innerRecord",
7+
"type": {
8+
"type": "record",
9+
"name": "InnerRecord",
10+
"fields": [
11+
{
12+
"name": "String_Field_Non_Nullable",
13+
"type": "string"
14+
},
15+
{
16+
"name": "String_Field_Nullable",
17+
"type": [ "string", "null" ]
18+
},
19+
{
20+
"name" : "Int_Field_Non_Nullable",
21+
"type" : "int"
22+
},
23+
{
24+
"name" : "Int_Field_Nullable",
25+
"type" : [ "int", "null" ]
26+
},
27+
{
28+
"name" : "Array_Col_Non_Nullable",
29+
"type" : {
30+
"type" : "array",
31+
"items" : "string"
32+
}
33+
},
34+
{
35+
"name" : "Array_Col_Nullable",
36+
"type" : [ "null", {
37+
"type" : "array",
38+
"items" : [ "null", "string" ]
39+
} ]
40+
}
41+
]
42+
}
43+
}
44+
]
45+
}

coral-schema/src/test/resources/testNestedStructInnerField.avsc

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"type" : "record",
3+
"name" : "innerfield_with_udf",
4+
"namespace" : "default.innerfield_with_udf",
5+
"fields" : [ {
6+
"name" : "innerRecord",
7+
"type" : {
8+
"type" : "record",
9+
"name" : "InnerRecord",
10+
"namespace" : "default.innerfield_with_udf.innerfield_with_udf",
11+
"fields" : [ {
12+
"name" : "String_Field_Non_Nullable",
13+
"type" : "string"
14+
}, {
15+
"name" : "String_Field_Nullable",
16+
"type" : [ "string", "null" ]
17+
}, {
18+
"name" : "Int_Field_Non_Nullable",
19+
"type" : "int"
20+
}, {
21+
"name" : "Int_Field_Nullable",
22+
"type" : [ "int", "null" ]
23+
}, {
24+
"name" : "Array_Col_Non_Nullable",
25+
"type" : {
26+
"type" : "array",
27+
"items" : "string"
28+
}
29+
}, {
30+
"name" : "Array_Col_Nullable",
31+
"type" : [ "null", {
32+
"type" : "array",
33+
"items" : [ "null", "string" ]
34+
} ]
35+
} ]
36+
}
37+
} ]
38+
}

0 commit comments

Comments
 (0)