|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using Microsoft.Spark.Sql; |
| 9 | +using Microsoft.Spark.Sql.Types; |
| 10 | +using Xunit; |
| 11 | +using static Microsoft.Spark.Sql.Functions; |
| 12 | + |
| 13 | +namespace Microsoft.Spark.E2ETest.UdfTests |
| 14 | +{ |
| 15 | + [Collection("Spark E2E Tests")] |
| 16 | + public class UdfSimpleTypesTests |
| 17 | + { |
| 18 | + private readonly SparkSession _spark; |
| 19 | + private readonly DataFrame _df; |
| 20 | + |
| 21 | + public UdfSimpleTypesTests(SparkFixture fixture) |
| 22 | + { |
| 23 | + _spark = fixture.Spark; |
| 24 | + _df = _spark |
| 25 | + .Read() |
| 26 | + .Schema("name STRING, age INT, date DATE") |
| 27 | + .Json(Path.Combine($"{TestEnvironment.ResourceDirectory}people.json")); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// UDF that takes in Date type. |
| 32 | + /// </summary> |
| 33 | + [Fact] |
| 34 | + public void TestUdfWithDateType() |
| 35 | + { |
| 36 | + Func<Column, Column> udf = Udf<Date, string>(date => date.ToString()); |
| 37 | + |
| 38 | + Row[] rows = _df.Select(udf(_df["date"])).Collect().ToArray(); |
| 39 | + Assert.Equal(3, rows.Length); |
| 40 | + |
| 41 | + var expected = new string[] { "2020-01-01", "2020-01-02", "2020-01-03" }; |
| 42 | + string[] rowsToArray = rows.Select(x => x[0].ToString()).ToArray(); |
| 43 | + Assert.Equal(expected, rowsToArray); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// UDF that returns Date type. |
| 48 | + /// </summary> |
| 49 | + [Fact] |
| 50 | + public void TestUdfWithReturnAsDateType() |
| 51 | + { |
| 52 | + Func<Column, Column> udf1 = Udf<int?, Date>( |
| 53 | + s => new Date(2020 + s.GetValueOrDefault(), 1, 4)); |
| 54 | + Func<Column, Column> udf2 = Udf<Date, string>(date => date.ToString()); |
| 55 | + |
| 56 | + // Test UDF that returns a Date object. |
| 57 | + { |
| 58 | + Row[] rows = _df.Select(udf1(_df["age"]).Alias("col")).Collect().ToArray(); |
| 59 | + Assert.Equal(3, rows.Length); |
| 60 | + |
| 61 | + var expected = new Date[] |
| 62 | + { |
| 63 | + new Date(2020, 1, 4), |
| 64 | + new Date(2050, 1, 4), |
| 65 | + new Date(2039, 1, 4) |
| 66 | + }; |
| 67 | + for (int i = 0; i < rows.Length; ++i) |
| 68 | + { |
| 69 | + Assert.Equal(1, rows[i].Size()); |
| 70 | + Assert.Equal(expected[i], rows[i].GetAs<Date>("col")); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Chained UDFs. |
| 75 | + { |
| 76 | + Row[] rows = _df.Select(udf2(udf1(_df["age"]))).Collect().ToArray(); |
| 77 | + Assert.Equal(3, rows.Length); |
| 78 | + |
| 79 | + var expected = new string[] { "2020-01-04", "2050-01-04", "2039-01-04" }; |
| 80 | + for (int i = 0; i < rows.Length; ++i) |
| 81 | + { |
| 82 | + Assert.Equal(1, rows[i].Size()); |
| 83 | + Assert.Equal(expected[i], rows[i].GetAs<string>(0)); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments