-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental object creation for Databricks statements (#273)
* Convert timestamp * Bump version * Update docs * Add support for object creation * Update documentation * Add release notes and bump package version * Fix license header * Fix documentation and add remark * Adjust naming and document method * Adjust naming * Add requirements for the record/class * Throw exception if constructor mismatch * Enhance with exceptions if there is a mismatch * Format documentation for readability * Reduce allocation * Review suggestion Co-authored-by: Dan Stenrøjl <[email protected]> --------- Co-authored-by: Henrik Sommer <[email protected]> Co-authored-by: Dan Stenrøjl <[email protected]>
- Loading branch information
1 parent
769f9bc
commit e5d0229
Showing
10 changed files
with
433 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...ce/Databricks/source/SqlStatementExecution.IntegrationTests/Client/ObjectCreationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// 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. | ||
|
||
using Energinet.DataHub.Core.Databricks.SqlStatementExecution.Formats; | ||
using Energinet.DataHub.Core.Databricks.SqlStatementExecution.IntegrationTests.Fixtures; | ||
using FluentAssertions; | ||
|
||
namespace Energinet.DataHub.Core.Databricks.SqlStatementExecution.IntegrationTests.Client; | ||
|
||
public class ObjectCreationTests : IClassFixture<DatabricksSqlWarehouseFixture> | ||
{ | ||
private readonly DatabricksSqlWarehouseFixture _sqlWarehouseFixture; | ||
|
||
private static DatabricksStatement PersonsStatement => DatabricksStatement.FromRawSql(@"SELECT * FROM VALUES | ||
('Zen Hui', 25), | ||
('Anil B' , 18), | ||
('Shone S', 16), | ||
('Mike A' , 25), | ||
('John A' , 18), | ||
('Jack N' , 16) AS data(name, age)") | ||
.Build(); | ||
|
||
public ObjectCreationTests(DatabricksSqlWarehouseFixture sqlWarehouseFixture) | ||
{ | ||
_sqlWarehouseFixture = sqlWarehouseFixture; | ||
} | ||
|
||
[Fact] | ||
public async Task CanMapToRecord() | ||
{ | ||
// Arrange | ||
var client = _sqlWarehouseFixture.CreateSqlStatementClient(); | ||
|
||
// Act | ||
var result = client.ExecuteStatementAsync<Person>(PersonsStatement); | ||
var persons = await result.ToListAsync(); | ||
|
||
// Assert | ||
persons.Should().Contain(new Person("John A", 18)); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenAClassWithMultipleConstructors_WhenConstructingObject_ThenExceptionIsThrown() | ||
{ | ||
// Arrange | ||
var client = _sqlWarehouseFixture.CreateSqlStatementClient(); | ||
|
||
// Act | ||
var result = client.ExecuteStatementAsync<BadPerson>(PersonsStatement); | ||
Func<Task> act = async () => await result.ToListAsync(); | ||
|
||
// Assert | ||
await act.Should().ThrowAsync<InvalidOperationException>(); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenAClassWithTwoParameters_WhenOnlyOneIsMapped_ThenExceptionIsThrown() | ||
{ | ||
// Arrange | ||
var client = _sqlWarehouseFixture.CreateSqlStatementClient(); | ||
|
||
// Act | ||
var result = client.ExecuteStatementAsync<ReallyBadPerson>(PersonsStatement); | ||
Func<Task> act = async () => await result.ToListAsync(); | ||
|
||
// Assert | ||
await act.Should().ThrowAsync<ArgumentException>(); | ||
} | ||
|
||
public class ReallyBadPerson | ||
{ | ||
public string Name { get; private set; } | ||
|
||
[ArrowField("age", 2)] | ||
public int Age { get; private set; } | ||
|
||
public ReallyBadPerson(string name, int age) | ||
{ | ||
Name = name; | ||
Age = age; | ||
} | ||
} | ||
|
||
public class BadPerson | ||
{ | ||
public BadPerson() | ||
: this(string.Empty) { } | ||
|
||
public BadPerson(string name) => Name = name; | ||
|
||
public string Name { get; set; } | ||
} | ||
|
||
public record Person( | ||
[property: ArrowField("name", 1)] string Name, | ||
[property: ArrowField("age", 2)] int Age); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
source/Databricks/source/SqlStatementExecution/Formats/ArrowFieldAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// 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. | ||
|
||
using System; | ||
|
||
namespace Energinet.DataHub.Core.Databricks.SqlStatementExecution.Formats; | ||
|
||
[AttributeUsage(AttributeTargets.Property)] | ||
public sealed class ArrowFieldAttribute : Attribute | ||
{ | ||
public ArrowFieldAttribute(string name, int constructorOrder) | ||
{ | ||
Name = name; | ||
ConstructorOrder = constructorOrder; | ||
} | ||
|
||
public string Name { get; } | ||
|
||
public int ConstructorOrder { get; } | ||
} |
29 changes: 29 additions & 0 deletions
29
source/Databricks/source/SqlStatementExecution/Formats/RecordBatchExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// 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. | ||
|
||
using System.Linq; | ||
using Apache.Arrow; | ||
|
||
namespace Energinet.DataHub.Core.Databricks.SqlStatementExecution.Formats; | ||
|
||
internal static class RecordBatchExtensions | ||
{ | ||
public static T ReadRecord<T>(this RecordBatch batch, int row) | ||
where T : class | ||
{ | ||
var fieldNames = Reflections.GetArrowFieldNames<T>(); | ||
var values = fieldNames.Select(field => batch.Column(field).GetValue(row)).ToArray(); | ||
return Reflections.CreateInstance<T>(values); | ||
} | ||
} |
Oops, something went wrong.