Skip to content

Commit

Permalink
Added an option to re-create the table if it already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyIvon committed Jul 9, 2022
1 parent 817c3c6 commit 4ad4b04
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 16 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ Supported values for `DatabaseType` parameter are:
* `PostgresJsonb` - stores all queryable "logical" columns in a single JSONB column indexed with GIN index of jsonb_path_ops type. Supports only `Equals` and `In` primitive operators.
* `SqlServer`

This command has a few database-specific parameters:
There is a parameter specific to the `create` command:
* `DropExisting` - specifies whether to re-create the table if it already exists.

This command also has a few database-specific parameters:
* `ClickHouse.Engine` - table engine. Default is `MergeTree()`.
* `ClickHouse.OrderBy` - table sort order. Default is `tuple()`.
* `MySql.Engine` - table engine. Default is `InnoDB`.
Expand Down Expand Up @@ -106,7 +109,7 @@ DatabaseBenchmark query --DatabaseType=MongoDb --ConnectionString="mongodb://loc

The same commands can be executed with `--QueryFilePath=SalesAggregateQuery.json` to make sure it works fine with all databases we are going to benchmark.

There are some parameters specific to the query command:
There are some parameters specific to the `query` command:
* `BenchmarkName` - benchmark name to be printed in the results table.
* `QueryParallelism` - number of parallel threads to be run.
* `QueryCount` - number of query executions on each thread.
Expand Down
2 changes: 1 addition & 1 deletion src/DatabaseBenchmark/Commands/CreateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Execute()
table.Name = options.TableName;
}

database.CreateTable(table);
database.CreateTable(table, options.DropExisting);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ public class CreateCommandOptions

[Option("Trace queries text and parameters")]
public bool TraceQueries { get; set; } = false;

[Option("Drop table if already exists")]
public bool DropExisting { get; set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ public ClickHouseDatabase(
_optionsProvider = optionsProvider;
}

public void CreateTable(Table table)
public void CreateTable(Table table, bool dropExisting)
{
using var connection = new ClickHouseConnection(_connectionString);
connection.Open();

if (dropExisting)
{
connection.DropTableIfExists(table.Name);
}

var tableBuilder = new ClickHouseTableBuilder(_optionsProvider);
var commandText = tableBuilder.Build(table);
var command = connection.CreateCommand(commandText);
Expand Down
14 changes: 13 additions & 1 deletion src/DatabaseBenchmark/Databases/CosmosDb/CosmosDbDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CosmosDbDatabase(
_optionsProvider = optionsProvider;
}

public void CreateTable(Table table)
public void CreateTable(Table table, bool dropExisting)
{
if (table.Columns.Any(c => c.DatabaseGenerated))
{
Expand All @@ -42,6 +42,18 @@ public void CreateTable(Table table)
using var client = new CosmosClient(_connectionString);
var database = client.GetDatabase(_databaseName);

if (dropExisting)
{
try
{
var container = database.GetContainer(table.Name);
container.DeleteContainerAsync().Wait();
}
catch
{
}
}

var partitionKeyName = GetPartitionKeyName(table);
database.CreateContainerIfNotExistsAsync(table.Name, "/" + partitionKeyName).Wait();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,35 @@ public ElasticsearchDatabase(string connectionString, IExecutionEnvironment envi
_environment = environment;
}

public void CreateTable(Table table)
public void CreateTable(Table table, bool dropExisting)
{
table = NormalizeNames(table);

var client = CreateClient();

if (dropExisting)
{
var exists = client.Indices.Exists(table.Name);
if (exists.Exists)
{
client.Indices.Delete(table.Name);
}
}

client.Indices.CreateAsync(table.Name, ci => ci
.Map(md => md
.Properties(pd => BuildProperties(table, pd)))).Wait();
}

public ImportResult ImportData(Table table, IDataSource source, int batchSize)
{
table = NormalizeNames(table);

if (batchSize <= 0)
{
batchSize = DefaultImportBatchSize;
}

var client = CreateClient();

var buffer = new List<object>();
Expand Down Expand Up @@ -84,7 +97,7 @@ public ImportResult ImportData(Table table, IDataSource source, int batchSize)
}

public IQueryExecutorFactory CreateQueryExecutorFactory(Table table, Query query) =>
new ElasticsearchQueryExecutorFactory(CreateClient, table, query);
new ElasticsearchQueryExecutorFactory(CreateClient, NormalizeNames(table), query);

public IQueryExecutorFactory CreateRawQueryExecutorFactory(RawQuery query) =>
new ElasticsearchRawQueryExecutorFactory(CreateClient, query);
Expand Down Expand Up @@ -165,5 +178,11 @@ private PropertiesDescriptor<object> BuildProperties(Table table, PropertiesDesc

return propertiesDescriptor;
}

private static Table NormalizeNames(Table table)
{
table.Name = table.Name.ToLower();
return table;
}
}
}
2 changes: 1 addition & 1 deletion src/DatabaseBenchmark/Databases/Interfaces/IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace DatabaseBenchmark.Databases.Interfaces
{
public interface IDatabase
{
void CreateTable(Table table);
void CreateTable(Table table, bool dropExisting);

ImportResult ImportData(Table table, IDataSource source, int batchSize);

Expand Down
7 changes: 6 additions & 1 deletion src/DatabaseBenchmark/Databases/MonetDb/MonetDbDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ public MonetDbDatabase(string connectionString, IExecutionEnvironment environmen
_environment = environment;
}

public void CreateTable(Table table)
public void CreateTable(Table table, bool dropExisting)
{
using var connection = new MonetDbConnection(_connectionString);
connection.Open();

if (dropExisting)
{
connection.DropTableIfExists(table.Name);
}

var tableBuilder = new MonetDbTableBuilder();
var commandText = tableBuilder.Build(table);
var command = new MonetDbCommand(commandText, connection);
Expand Down
12 changes: 11 additions & 1 deletion src/DatabaseBenchmark/Databases/MongoDb/MongoDbDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public MongoDbDatabase(
_optionsProvider = optionsProvider;
}

public void CreateTable(Table table)
public void CreateTable(Table table, bool dropExisting)
{
//TODO: allow _id field to be marked as database-generated
if (table.Columns.Any(c => c.DatabaseGenerated))
Expand All @@ -39,6 +39,16 @@ public void CreateTable(Table table)
}

var database = GetDatabase();

if (dropExisting)
{
var collection = database.GetCollection<BsonDocument>(table.Name);
if (collection != null)
{
database.DropCollection(table.Name);
}
}

database.CreateCollection(table.Name);
}

Expand Down
7 changes: 6 additions & 1 deletion src/DatabaseBenchmark/Databases/MySql/MySqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ public MySqlDatabase(
_optionsProvider = optionsProvider;
}

public void CreateTable(Table table)
public void CreateTable(Table table, bool dropExisting)
{
using var connection = new MySqlConnection(_connectionString);
connection.Open();

if (dropExisting)
{
connection.DropTableIfExists(table.Name);
}

var tableBuilder = new MySqlTableBuilder(_optionsProvider);
var commandText = tableBuilder.Build(table);
var command = new MySqlCommand(commandText, connection);
Expand Down
7 changes: 6 additions & 1 deletion src/DatabaseBenchmark/Databases/Oracle/OracleDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ public OracleDatabase(string connectionString, IExecutionEnvironment environment
_environment = environment;
}

public void CreateTable(Table table)
public void CreateTable(Table table, bool dropExisting)
{
using var connection = new OracleConnection(_connectionString);
connection.Open();

if (dropExisting)
{
connection.DropTableIfExists(table.Name);
}

var tableBuilder = new OracleTableBuilder();
var commandText = tableBuilder.Build(table);
var command = new OracleCommand(commandText, connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ public PostgreSqlDatabase(string connectionString, IExecutionEnvironment environ
_environment = environment;
}

public void CreateTable(Table table)
public void CreateTable(Table table, bool dropExisting)
{
using var connection = new NpgsqlConnection(_connectionString);
connection.Open();

if (dropExisting)
{
connection.DropTableIfExists(table.Name);
}

var tableBuilder = new PostgreSqlTableBuilder();
var commandText = tableBuilder.Build(table);
var command = new NpgsqlCommand(commandText, connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ public PostgreSqlJsonbDatabase(string connectionString, IExecutionEnvironment en
_environment = environment;
}

public void CreateTable(Table table)
public void CreateTable(Table table, bool dropExisting)
{
using var connection = new NpgsqlConnection(_connectionString);
connection.Open();

if (dropExisting)
{
connection.DropTableIfExists(table.Name);
}

var tableBuilder = new PostgreSqlJsonbTableBuilder();
var commandText = tableBuilder.BuildCreateTableCommandText(table);
var command = new NpgsqlCommand(commandText, connection);
Expand Down
20 changes: 20 additions & 0 deletions src/DatabaseBenchmark/Databases/Sql/DbConnectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Data;

namespace DatabaseBenchmark.Databases.Sql
{
public static class DbConnectionExtensions
{
public static void DropTableIfExists(this IDbConnection connection, string tableName)
{
try
{
var dropCommand = connection.CreateCommand();
dropCommand.CommandText = $"DROP TABLE {tableName}";
dropCommand.ExecuteNonQuery();
}
catch
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ public SqlServerDatabase(string connectionString, IExecutionEnvironment environm
_environment = environment;
}

public void CreateTable(Table table)
public void CreateTable(Table table, bool dropExisting)
{
using var connection = new SqlConnection(_connectionString);
connection.Open();

if (dropExisting)
{
connection.DropTableIfExists(table.Name);
}

var tableBuilder = new SqlServerTableBuilder();
var commandText = tableBuilder.Build(table);
var command = new SqlCommand(commandText, connection);
Expand Down

0 comments on commit 4ad4b04

Please sign in to comment.