Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public DbCommand Command
{
_command = Connection.CreateCommand();
_command.CommandText = _source.CommandText;
_command.CommandTimeout = _source.CommandTimeoutInSeconds;
}
return _command;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.ML.Data/DataLoadSave/Database/DatabaseSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,29 @@ public DatabaseSource(DbProviderFactory providerFactory, string connectionString
CommandText = commandText;
}

/// <summary>Creates a new instance of the <see cref="DatabaseSource" /> class.</summary>
/// <param name="providerFactory">The factory used to create the <see cref="DbConnection"/>..</param>

@eerhardt eerhardt Dec 9, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the double ending .. ? #Resolved

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing here and in the above constructor.


In reply to: 355678098 [](ancestors = 355678098)

/// <param name="connectionString">The string used to open the connection.</param>
/// <param name="commandText">The text command to run against the data source.</param>
/// <param name="commandTimeoutInSeconds">The time in seconds to wait for the command to execute.</param>
public DatabaseSource(DbProviderFactory providerFactory, string connectionString, string commandText, int commandTimeoutInSeconds)
{
Contracts.CheckValue(providerFactory, nameof(providerFactory));
Contracts.CheckValue(connectionString, nameof(connectionString));
Contracts.CheckValue(commandText, nameof(commandText));

ProviderFactory = providerFactory;
ConnectionString = connectionString;
CommandText = commandText;
CommandTimeoutInSeconds = commandTimeoutInSeconds;
}

/// <summary>Gets the text command to run against the data source.</summary>
public string CommandText { get; }

/// <summary>Gets the command timeout.</summary>
public int CommandTimeoutInSeconds { get; }

/// <summary>Gets the string used to open the connection.</summary>
public string ConnectionString { get; }

Expand Down
54 changes: 54 additions & 0 deletions test/Microsoft.ML.Tests/DatabaseLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
Expand Down Expand Up @@ -79,6 +80,59 @@ public void IrisLightGbm()
}).PredictedLabel);
}

[LightGBMFact]
public void IrisLightGbmCommandTimeout()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// https://github.com/dotnet/machinelearning/issues/4156
return;
}

var mlContext = new MLContext(seed: 1);

var connectionString = GetConnectionString(TestDatasets.irisDb.name);
var commandText = $@"WAITFOR DELAY '00:00:02'; SELECT * FROM ""{TestDatasets.irisDb.trainFilename}""";
var commandTimeout = 1;

var loaderColumns = new DatabaseLoader.Column[]
{
new DatabaseLoader.Column() { Name = "Label", Type = DbType.Int32 },
new DatabaseLoader.Column() { Name = "SepalLength", Type = DbType.Single },
new DatabaseLoader.Column() { Name = "SepalWidth", Type = DbType.Single },
new DatabaseLoader.Column() { Name = "PetalLength", Type = DbType.Single },
new DatabaseLoader.Column() { Name = "PetalWidth", Type = DbType.Single }
};

var loader = mlContext.Data.CreateDatabaseLoader(loaderColumns);

var databaseSource = new DatabaseSource(SqlClientFactory.Instance, connectionString, commandText, commandTimeout);

bool passed=false;

try
{
var trainingData = loader.Load(databaseSource);

IEstimator<ITransformer> pipeline = mlContext.Transforms.Conversion.MapValueToKey("Label")
.Append(mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
.AppendCacheCheckpoint(mlContext)
.Append(mlContext.MulticlassClassification.Trainers.LightGbm())
.Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));

var model = pipeline.Fit(trainingData);
}
catch(Exception e)

@eerhardt eerhardt Dec 9, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Assert.Throws instead.

Also - you should ensure the correct type of exception is thrown. #Resolved

{
if (e.InnerException.Message.Contains("Timeout"))
passed = true;
else
throw;
}

Assert.True(passed);
}

[LightGBMFact]
public void IrisLightGbmWithLoadColumnName()
{
Expand Down