Skip to content

Commit

Permalink
Support nullable parameters (#343)
Browse files Browse the repository at this point in the history
* Create test that have nullable constructor values

* Check for null when validating

* Package version and release notes updated

* Use fluent syntax for asserting
  • Loading branch information
Sondergaard authored Sep 4, 2024
1 parent d0ca5de commit b1efa39
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
4 changes: 4 additions & 0 deletions source/Databricks/documents/release-notes/release-notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Databricks Release Notes

## Version 11.1.1

- Fix handling of null values in constructor when using `StronglyTypedApacheArrowFormat`

## Version 11.1.0

- Update NuGet package dependencies and refactor code accordingly.
Expand Down
2 changes: 1 addition & 1 deletion source/Databricks/source/Jobs/Jobs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ limitations under the License.

<PropertyGroup>
<PackageId>Energinet.DataHub.Core.Databricks.Jobs</PackageId>
<PackageVersion>11.1.0$(VersionSuffix)</PackageVersion>
<PackageVersion>11.1.1$(VersionSuffix)</PackageVersion>
<Title>Databricks Jobs</Title>
<Company>Energinet-DataHub</Company>
<Authors>Energinet-DataHub</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class ObjectCreationTests : IClassFixture<DatabricksSqlWarehouseFixture>
('Jack N' , 16) AS data(name, age)")
.Build();

private static DatabricksStatement NullStatement => DatabricksStatement.FromRawSql(@"SELECT * FROM VALUES
('Jack N' , 16, 'Developer'),
('Null N' , 27, NULL) AS data(name, age, title)")
.Build();

public ObjectCreationTests(DatabricksSqlWarehouseFixture sqlWarehouseFixture)
{
_sqlWarehouseFixture = sqlWarehouseFixture;
Expand Down Expand Up @@ -78,6 +83,17 @@ public async Task GivenAClassWithTwoParameters_WhenOnlyOneIsMapped_ThenException
await act.Should().ThrowAsync<ArgumentException>();
}

[Fact]
public async Task AResponseWithNullValues_WhenMappedToANullableProperty_ThenTheObjectIsCreated()
{
var client = _sqlWarehouseFixture.CreateSqlStatementClient();

var result = client.ExecuteStatementAsync<PersonWithTitle>(NullStatement);
var persons = await result.ToListAsync();

persons.Count.Should().Be(2);
}

public class ReallyBadPerson
{
public string Name { get; private set; }
Expand Down Expand Up @@ -105,4 +121,9 @@ public BadPerson()
public record Person(
[property: ArrowField("name", 1)] string Name,
[property: ArrowField("age", 2)] int Age);

public record PersonWithTitle(
[property: ArrowField("name", 1)] string Name,
[property: ArrowField("age", 2)] int Age,
[property: ArrowField("title", 3)] string? Title);
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,18 @@ private static Func<bool> ValidateConstructorForType()
var index = Expression.Constant(i);
var parameterType = parameters[i].ParameterType;
var accessor = Expression.ArrayIndex(param, index);
var typeMatch = Expression.TypeIs(accessor, parameterType);
checks[i] = typeMatch;

// Check if the parameter type is nullable
if (parameterType.IsValueType && Nullable.GetUnderlyingType(parameterType) == null)
{
checks[i] = Expression.TypeIs(accessor, parameterType);
}
else
{
checks[i] = Expression.OrElse(
Expression.Equal(accessor, Expression.Constant(null)),
Expression.TypeIs(accessor, parameterType));
}
}

var allChecks = checks.Aggregate(Expression.AndAlso);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ limitations under the License.

<PropertyGroup>
<PackageId>Energinet.DataHub.Core.Databricks.SqlStatementExecution</PackageId>
<PackageVersion>11.1.0$(VersionSuffix)</PackageVersion>
<PackageVersion>11.1.1$(VersionSuffix)</PackageVersion>
<Title>Databricks SQL Statement Execution</Title>
<Company>Energinet-DataHub</Company>
<Authors>Energinet-DataHub</Authors>
Expand Down

0 comments on commit b1efa39

Please sign in to comment.