Skip to content

Commit a9bd00a

Browse files
committed
improvements
1 parent cba39f9 commit a9bd00a

File tree

7 files changed

+86
-38
lines changed

7 files changed

+86
-38
lines changed

.github/workflows/dotnet.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ jobs:
3030
env:
3131
DOTNET_NOLOGO: 1
3232
DOTNET_CLI_TELEMETRY_OPTOUT: 1
33-
# - name: Setup dotnet ${{ matrix.dotnet-version }}
34-
# uses: actions/setup-dotnet@v3
35-
# with:
36-
# dotnet-version: ${{ matrix.dotnet-version }}
37-
3833
# - name: Cache libchdb.so
3934
# id: cache-libchdb
4035
# uses: actions/cache@v4
@@ -64,9 +59,10 @@ jobs:
6459
path: TestResults-${{ matrix.dotnet-version }}
6560
# Use always() to always run this step to publish test results when there are test failures
6661
if: ${{ always() }}
62+
- name: Test chdb-tool
63+
run: |
64+
dotnet tool install --global chdb-tool --version 0.0.1
65+
chdb --help
66+
chdb "select version()" PrettyCompact
6767
- name: Pack
68-
run: dotnet pack -c Release --include-symbols
69-
# - name: Publish the package to nuget.org
70-
# run: dotnet nuget push nupkg/*.nupkg -k $NUGET_AUTH_TOKEN -s https://api.nuget.org/v3/index.json
71-
# env:
72-
# NUGET_AUTH_TOKEN: ${{ secrets.NUGET_TOKEN }}
68+
run: dotnet pack -c Release --include-symbols

README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# chdb
22

3-
[![Build Status](https://dev.azure.com/ChDB/ChDB/_apis/build/status/ChDB.chdb?branchName=master)](https://dev.azure.com/ChDB/ChDB/_build/latest?definitionId=1&branchName=master)
4-
[![NuGet](https://img.shields.io/nuget/v/ChDB.svg)](https://www.nuget.org/packages/ChDB/)
5-
[![NuGet](https://img.shields.io/nuget/dt/ChDB.svg)](https://www.nuget.org/packages/ChDB/)
6-
[![License](https://img.shields.io/github/license/ChDB/chdb.svg)](https://github.com/vilinski/chdb/LICENSE.md)
3+
![GitHub License](https://img.shields.io/github/license/chdb-io/chdb-dotnet)
4+
![example workflow](https://github.com/chdb-io/chdb-dotnet/actions/workflows/dotnet.yml/badge.svg)
5+
76

87
## chdb NuGet package
98

9+
![NuGet Version](https://img.shields.io/nuget/vpre/chdb)
10+
![NuGet Downloads](https://img.shields.io/nuget/dt/chdb)
11+
12+
1013
A .NET Core binding for [chdb](https://doc.chdb.io) library.
1114

1215

@@ -22,7 +25,7 @@ dotnet add package chdb
2225
using ChDb;
2326

2427
var result = ChDb.Query("select version()");
25-
Console.WriteLine(result);
28+
Console.WriteLine(result.Buf);
2629
// 23.10.1.1
2730
var result = ChDb.Query("select * from system.formats where is_output = 1", "PrettyCompact");
2831
// ┌─name───────────────────────────────────────┬─is_input─┬─is_output─┬─supports_parallel_parsing─┬─supports_parallel_formatting─┐
@@ -33,18 +36,25 @@ var result = ChDb.Query("select * from system.formats where is_output = 1", "Pre
3336
// │ ODBCDriver2 │ 0 │ 1 │ 0 │ 0 │
3437
// ...
3538
var result = ChDb.Query("DESCRIBE s3('https://datasets-documentation.s3.eu-west-3.amazonaws.com/house_parquet/house_0.parquet')");
36-
Console.WriteLine(result.Buf);
39+
Console.WriteLine(result.Buf);
3740
```
3841

3942
## chdb-tool
4043

41-
This is a dotnet tool for [chdb](https://doc.chdb.io) library.
42-
Actually you better just install clickhouse client and run `clickhouse local`
44+
![NuGet Version](https://img.shields.io/nuget/vpre/chdb-tool)
45+
![NuGet Downloads](https://img.shields.io/nuget/dt/chdb-tool)
46+
47+
This is a dotnet tool, running [chdb](https://doc.chdb.io) library.
48+
Actually you better install the clickhouse client and run `clickhouse local`, but maybe it is more useful for some cases.
49+
50+
>Note for windows users - there is no windows bild in sight, but you can use it in WSL.
4351
4452
### Installation
4553

54+
Requires .NET SDK 6.0 or later.
55+
4656
```bash
47-
dotnet tool install -g chdb-tool
57+
dotnet tool install --global chdb-tool
4858
```
4959

5060
### Usage
@@ -55,15 +65,15 @@ Try any of this commands lines to see which output you get.
5565
chdb
5666
chdb --version
5767
chdb --help
58-
chdb "select version()"
68+
chdb "select version()"
5969
chdb "select * from system.formats where is_output = 1" PrettyCompact
6070
```
6171

6272
# Build
6373

6474
```bash
6575
# update latest chdb version
66-
./update_libchdb.sh
76+
./update_libchdb.sh [v1.2.1]
6777
# install versionbump tool
6878
dotnet tool install -g BumpVersion
6979
# bump version
@@ -73,4 +83,4 @@ git push --foloow-tags
7383

7484
## Authors
7585

76-
* [Andreas Vilinski](https://github.com/vilinski)
86+
* [Andreas Vilinski](https://github.com/vilinski)

src/chdb-tool/Program.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ void PrintHelp()
3535
return; // TODO behavior changed in 1.2.1
3636
Console.WriteLine(result.Buf);
3737
if (!args.Contains("--quiet") && !args.Contains("-q"))
38-
Console.WriteLine($"Elapsed: {result.Elapsed} s, read {result.RowsRead} rows, {result.BytesRead} bytes");
39-
if (!string.IsNullOrWhiteSpace(result.ErrorMessage))
40-
Console.Error.WriteLine("Error message: " + result.ErrorMessage);
38+
{
39+
Console.WriteLine($"Elapsed: {result.Elapsed} s, read {result.RowsRead} rows, {result.BytesRead} bytes" +
40+
$", {result.RowsRead / result.Elapsed.TotalSeconds:F0} rows/s, {result.BytesRead / result.Elapsed.TotalSeconds:F0} bytes/s");
41+
if (!string.IsNullOrWhiteSpace(result.ErrorMessage))
42+
Console.Error.WriteLine("Error message: " + result.ErrorMessage);
43+
}
4144
}
4245
catch (ArgumentException e)
4346
{

src/chdb/ChDb.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@ public static class ChDb
6767
}
6868
catch (RuntimeWrappedException e)
6969
{
70-
Console.Error.WriteLine($"Native error {e.Message}", e);
71-
throw;
70+
var s = e.WrappedException as string;
71+
if (s != null)
72+
throw new ArgumentException(s);
73+
else
74+
Console.Error.WriteLine($"Unmanaged error {e.WrappedException}");
7275
}
7376
catch (Exception e)
7477
{
7578
Console.Error.WriteLine($"Managed error {e.Message}", e);
7679
throw;
7780
}
81+
return null;
7882
}
7983
}

src/chdb/Session.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
namespace ChDb;
22

3-
public record Session
3+
public record Session : IDisposable
44
{
55
public string? Format { get; init; }
6-
public string? DataPath { get; init; }
7-
public string? UdfPath { get; init; }
6+
public string? DataPath { get; set; }
7+
// public string? UdfPath { get; init; }
88
public string? LogLevel { get; init; }
9+
public bool IsTemp { get; init; } = true;
10+
11+
public void Dispose()
12+
{
13+
if (!IsTemp && DataPath?.EndsWith("chdb_") == true && Directory.Exists(DataPath))
14+
Directory.Delete(DataPath, true);
15+
}
916

1017
public LocalResult? Execute(string query, string? format = null)
1118
{
19+
if (IsTemp && DataPath is null) {
20+
DataPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "chdb_");
21+
}
22+
1223
var argv = new[] {
1324
"clickhouse",
1425
"--multiquery",
1526
$"--query={query}",
1627
$"--output-format={format ?? Format ?? "TabSeparated"}", //$"--path={DataPath}",
1728
$"--path={DataPath}",
18-
// $"--udf-path={UdfPath}",
29+
// $"--user_scripts_path={UdfPath}", $"--user_defined_executable_functions_config={UdfPath}/*.xml",
1930
$"--log-level={LogLevel ?? "trace"}",
2031
};
2132
try

src/chdb/chdb.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<None Update="libchdb.so" CopyToOutputDirectory="PreserveNewest" />
11+
<None Update="libchdb.so" CopyToOutputDirectory="PreserveNewest" Condition="$([MSBuild]::IsOSPlatform('OSX'))" />
1212
</ItemGroup>
1313
</Project>

test/ChDbTest/ChDbTest.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
namespace test;
2-
3-
using ChDb;
1+
namespace ChDb;
42

53
[TestClass]
64
public class ChDbTest
@@ -116,15 +114,41 @@ FROM s3('https://datasets-documentation.s3.eu-west-3.amazonaws.com/house_parquet
116114
Assert.AreEqual(2772030, count);
117115
}
118116

117+
[TestMethod]
118+
[Ignore("TODO")]
119+
public void CsvTest() {
120+
var csv = """
121+
Name, Age, City
122+
John, 25, New York
123+
Alice, 30, London
124+
Bob, 22, Tokyo
125+
Eva, 28, Paris
126+
""";
127+
var dataPath = "/tmp/chdb/data";
128+
Directory.CreateDirectory(dataPath);
129+
File.WriteAllText(Path.Combine(".", "test.csv"), csv);
130+
var session = new Session
131+
{
132+
Format = "PrettyCompact",
133+
DataPath = dataPath,
134+
// UdfPath = "/tmp/chdb/udf",
135+
LogLevel = "trace",
136+
};
137+
var result = session.Execute("SELECT * FROM 'test.csv'", "CSVWithNamesAndTypes");
138+
Assert.IsNotNull(result);
139+
Assert.AreEqual(4UL, result.RowsRead);
140+
Assert.AreEqual(155UL, result.BytesRead);
141+
Assert.AreEqual("Name\tString\nAge\tUInt8\nCity\tString\nJohn\t25\tNew York\nAlice\t30\tLondon\nBob\t22\tTokyo\nEva\t28\tParis\n", result.Buf);
142+
StringAssert.StartsWith(result.Buf, """"Name","Age","City"""");
143+
}
144+
119145
[TestMethod]
120146
[Ignore("Bugfix is in v1.2.1")]
121147
public void SessionTest()
122148
{
123-
var session = new Session
149+
using var session = new Session
124150
{
125151
Format = "PrettyCompact",
126-
DataPath = "/tmp/chdb/data",
127-
UdfPath = "/tmp/chdb/udf",
128152
LogLevel = "trace",
129153
};
130154
// var r1 = session.Execute("select 1");

0 commit comments

Comments
 (0)