Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQL command timeout option to database loader. #4494

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
22 changes: 21 additions & 1 deletion src/Microsoft.ML.Data/DataLoadSave/Database/DatabaseSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.ML.Data
public sealed class DatabaseSource
{
/// <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>
/// <param name="providerFactory">The factory used to create the <see cref="DbConnection"/>.</param>
/// <param name="connectionString">The string used to open the connection.</param>
/// <param name="commandText">The text command to run against the data source.</param>
public DatabaseSource(DbProviderFactory providerFactory, string connectionString, string commandText)
Expand All @@ -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>
/// <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
40 changes: 40 additions & 0 deletions test/Microsoft.ML.Tests/DatabaseLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,46 @@ 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);

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 ex = Assert.Throws<System.Reflection.TargetInvocationException>(() => pipeline.Fit(trainingData));
Assert.Contains("Timeout expired.", ex.InnerException.Message);
}

[LightGBMFact]
public void IrisLightGbmWithLoadColumnName()
{
Expand Down