Skip to content

Commit

Permalink
Fixes for array columns support for ClickHouse
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyIvon committed Oct 20, 2024
1 parent d27eaf2 commit 3308c2e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public void CreateTable(Table table, bool dropExisting)
public IDataImporter CreateDataImporter(Table table, IDataSource source, int batchSize) =>
new SqlDataImporterBuilder(table, source, batchSize, DefaultImportBatchSize)
.Connection<ClickHouseConnection>(ConnectionString)
.ParametersBuilder<SqlNoParametersBuilder>()
.ParametersBuilder(() => new SqlParametersBuilder())
.ParametersBuilder<ClickHouseNoParametersBuilder>()

Check failure on line 60 in src/DatabaseBenchmark/Databases/ClickHouse/ClickHouseDatabase.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

The type or namespace name 'ClickHouseNoParametersBuilder' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 60 in src/DatabaseBenchmark/Databases/ClickHouse/ClickHouseDatabase.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

The type or namespace name 'ClickHouseNoParametersBuilder' could not be found (are you missing a using directive or an assembly reference?)
.ParameterAdapter<ClickHouseParameterAdapter>()
.OptionsProvider(_optionsProvider)
.Environment(_environment)
Expand All @@ -77,6 +78,7 @@ public IQueryExecutorFactory CreateRawQueryExecutorFactory(RawQuery query) =>

public IQueryExecutorFactory CreateInsertExecutorFactory(Table table, IDataSource source, int batchSize) =>
new SqlInsertExecutorFactory<ClickHouseConnection>(this, table, source, batchSize, _environment)
.Customize<ISqlParametersBuilder, ClickHouseNoParametersBuilder>()

Check failure on line 81 in src/DatabaseBenchmark/Databases/ClickHouse/ClickHouseDatabase.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

The type or namespace name 'ClickHouseNoParametersBuilder' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 81 in src/DatabaseBenchmark/Databases/ClickHouse/ClickHouseDatabase.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

The type or namespace name 'ClickHouseNoParametersBuilder' could not be found (are you missing a using directive or an assembly reference?)
.Customize<ISqlParameterAdapter, ClickHouseParameterAdapter>();

public void ExecuteScript(string script) => throw new InputArgumentException("Custom scripts are not supported for ClickHouse");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,23 @@ public void Populate(SqlQueryParameter source, IDbDataParameter target)

if (source.Array)
{
clickHouseTarget.ClickHouseDbType |= ClickHouseDbType.Array;
clickHouseTarget.IsArray = true;
clickHouseTarget.ArrayRank = 1;

//An array parameter must have a specific element type
var arrayValue = (object[])target.Value;
target.Value = source.Type switch
{
ColumnType.Boolean => arrayValue.Cast<bool>().ToArray(),
ColumnType.Integer => arrayValue.Cast<int>().ToArray(),
ColumnType.Long => arrayValue.Cast<long>().ToArray(),
ColumnType.Double => arrayValue.Cast<double>().ToArray(),
ColumnType.DateTime => arrayValue.Cast<DateTime>().ToArray(),
ColumnType.Guid => arrayValue.Cast<Guid>().ToArray(),
ColumnType.String => arrayValue.Cast<string>().ToArray(),
ColumnType.Text => arrayValue.Cast<string>().ToArray(),
_ => throw new InputArgumentException($"Parameter type {source.Type} is not supported")
};
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/DatabaseBenchmark/Databases/Sql/SqlNoParametersBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DatabaseBenchmark.Databases.Sql.Interfaces;
using DatabaseBenchmark.Common;
using DatabaseBenchmark.Databases.Sql.Interfaces;
using DatabaseBenchmark.Model;

namespace DatabaseBenchmark.Databases.Sql
Expand All @@ -7,11 +8,11 @@ public class SqlNoParametersBuilder : ISqlParametersBuilder
{
public IEnumerable<SqlQueryParameter> Parameters { get; } = Enumerable.Empty<SqlQueryParameter>();

public string Append(object value, ColumnType type, bool array) =>
public virtual string Append(object value, ColumnType type, bool array) =>
value switch
{
IEnumerable<object> => throw new InputArgumentException("Array literals are not supported"),
null => "NULL",
IEnumerable<object> arrayValue => $"[{string.Join(", ", arrayValue.Select(v => $"'{v}'"))}]", //TODO: double-check
bool boolValue => boolValue.ToString().ToLower(), //Different databases may accept different Boolean format
int intValue => intValue.ToString(),
long longValue => longValue.ToString(),
Expand Down

0 comments on commit 3308c2e

Please sign in to comment.