Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -75,6 +75,10 @@ public DbCommand Command
{
_command = Connection.CreateCommand();
_command.CommandText = _source.CommandText;
if (_source.CommandTimeOut is {})
Comment thread
gh-yewang marked this conversation as resolved.
Outdated
{
_command.CommandTimeout = _source.CommandTimeOut;
}
}
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,6 +25,26 @@ 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="commandTimeOut">The timeout(in seconds) for database command.</param>
public DatabaseSource(DbProviderFactory providerFactory, string connectionString, string commandText, int commandTimeOut)
Comment thread
gh-yewang marked this conversation as resolved.
Outdated
{
Comment thread
gh-yewang marked this conversation as resolved.
Contracts.CheckValue(providerFactory, nameof(providerFactory));
Contracts.CheckValue(connectionString, nameof(connectionString));
Contracts.CheckValue(commandText, nameof(commandText));

ProviderFactory = providerFactory;
ConnectionString = connectionString;
CommandText = commandText;
CommandTimeOut = commandTimeOut;
}

/// <summary>Gets the timeout for database command.</summary>
public int CommandTimeOut { get; }

Comment thread
gh-yewang marked this conversation as resolved.
/// <summary>Gets the text command to run against the data source.</summary>
public string CommandText { get; }

Expand Down
33 changes: 32 additions & 1 deletion test/Microsoft.ML.Tests/DatabaseLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ public DatabaseLoaderTests(ITestOutputHelper output)

[LightGBMFact]
public void IrisLightGbm()
{
DatabaseSource dbs = GetIrisDatabaseSource("SELECT * FROM {0}");
IrisLightGbmImpl(dbs);
}

[LightGBMFact]
public void IrisLightGbmWithTimeOut()
{
DatabaseSource dbs = GetIrisDatabaseSourceWithTimeOut("WAITFOR DELAY '00:00:05'; SELECT * FROM {0}", 1);
var ex = Assert.Throws<System.Reflection.TargetInvocationException>(() => IrisLightGbmImpl(dbs));
Assert.Contains("Timeout expired", ex.InnerException.Message);
Comment thread
gh-yewang marked this conversation as resolved.
Outdated

}

private void IrisLightGbmImpl(DatabaseSource dbs)
{
var mlContext = new MLContext(seed: 1);

Expand All @@ -41,7 +56,7 @@ public void IrisLightGbm()

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

var trainingData = loader.Load(GetIrisDatabaseSource("SELECT * FROM {0}"));
var trainingData = loader.Load(dbs);

IEstimator<ITransformer> pipeline = mlContext.Transforms.Conversion.MapValueToKey("Label")
.Append(mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
Expand Down Expand Up @@ -225,6 +240,22 @@ private DatabaseSource GetIrisDatabaseSource(string command)
String.Format(command, TestDatasets.irisDbSQLite.trainFilename));
}

private DatabaseSource GetIrisDatabaseSourceWithTimeOut(string command, int commandTimeOut)
Comment thread
gh-yewang marked this conversation as resolved.
Outdated
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return new DatabaseSource(
SqlClientFactory.Instance,
GetMSSQLConnectionString(TestDatasets.irisDb.name),
String.Format(command, $@"""{TestDatasets.irisDb.trainFilename}"""),
commandTimeOut);
else
return new DatabaseSource(
SQLiteFactory.Instance,
GetSQLiteConnectionString(TestDatasets.irisDbSQLite.name),
String.Format(command, TestDatasets.irisDbSQLite.trainFilename),
commandTimeOut);
}

private string GetMSSQLConnectionString(string databaseName)
{
var databaseFile = Path.GetFullPath(Path.Combine("TestDatabases", $"{databaseName}.mdf"));
Expand Down