Skip to content

Npgsql: Add software tests validating all data types of CrateDB #772

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

Merged
merged 6 commits into from
Dec 18, 2024
Merged
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
11 changes: 8 additions & 3 deletions by-language/csharp-npgsql/DemoProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ await Parser.Default.ParseArguments<Options>(args)
await DatabaseWorkloads.SystemQueryExample(conn);
await DatabaseWorkloads.BasicConversationExample(conn);
await DatabaseWorkloads.UnnestExample(conn);
await DatabaseWorkloadsMore.AllTypesExample(conn);
conn.Close();
});

Expand Down Expand Up @@ -57,15 +58,19 @@ public static async Task<List<string>> SystemQueryExample(NpgsqlConnection conn)
{
Console.WriteLine("Running SystemQueryExample");
var mountains = new List<string>();
await using (var cmd = new NpgsqlCommand("SELECT mountain FROM sys.summits ORDER BY 1 LIMIT 25", conn))
const string sql = "SELECT mountain, height, coordinates FROM sys.summits ORDER BY height DESC LIMIT 25";
await using (var cmd = new NpgsqlCommand(sql, conn))
await using (var reader = cmd.ExecuteReader())
{
while (await reader.ReadAsync())
{
mountains.Add(reader.GetString(0));
mountains.Add(
reader["mountain"].ToString() + " - " +
reader["height"].ToString() + " - " +
reader["coordinates"].ToString());
}

Console.WriteLine($"Mountains: {string.Join(",", mountains)}");
Console.WriteLine($"Mountains:\n{string.Join("\n", mountains)}");
}

Console.WriteLine();
Expand Down
249 changes: 249 additions & 0 deletions by-language/csharp-npgsql/DemoTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Npgsql;
using NpgsqlTypes;

namespace demo
{

public class AllTypesRecord
{
[JsonProperty("null_integer", NullValueHandling = NullValueHandling.Ignore)]
public int? NullInteger { get; set; }
[JsonProperty("integer")]
public int? Integer { get; set; }
[JsonProperty("bigint")]
public long? Bigint { get; set; }
[JsonProperty("float")]
public float? Float { get; set; }
[JsonProperty("double")]
public double? Double { get; set; }
[JsonProperty("decimal")]
public decimal? Decimal { get; set; }
//[JsonProperty("bit")]
//public BitArray Bit { get; set; }
[JsonProperty("bool")]
public bool? Bool { get; set; }
[JsonProperty("text")]
public string? Text { get; set; }
[JsonProperty("char")]
public string? Char { get; set; }
[JsonProperty("timestamp_tz")]
public string? Timestamp { get; set; }
[JsonProperty("timestamp_notz")]
public string? TimestampNoTz { get; set; }
[JsonProperty("ip")]
public string? Ip { get; set; }

[JsonProperty("array")]
public IList<string>? Array { get; set; }
[JsonProperty("object")]
public Dictionary<string, string>? Object { get; set; }
[JsonProperty("geopoint")]
public Dictionary<string, double>? Geopoint { get; set; }
[JsonProperty("geoshape")]
public Dictionary<string, string>? Geoshape { get; set; }
[JsonProperty("float_vector")]
public IList<string>? FloatVector { get; set; }
}

public class DatabaseWorkloadsMore
{

public static async Task<DataTable> AllTypesExample(NpgsqlConnection conn)
{
Console.WriteLine("Running AllTypesExample");

// Submit DDL, create database schema.
await using (var cmd = new NpgsqlCommand("DROP TABLE IF EXISTS testdrive.example", conn))
{
cmd.ExecuteNonQuery();
}

await using (var cmd = new NpgsqlCommand(@"
CREATE TABLE testdrive.example (
-- Numeric types
null_integer INT,
integer INT,
bigint BIGINT,
float FLOAT,
double DOUBLE,
decimal DECIMAL(8, 2),
-- Other scalar types
bit BIT(8),
bool BOOLEAN,
text TEXT,
char CHARACTER(5),
timestamp_tz TIMESTAMP WITH TIME ZONE,
timestamp_notz TIMESTAMP WITHOUT TIME ZONE,
ip IP,
-- Container types
""array"" ARRAY(STRING),
""object"" OBJECT(DYNAMIC),
-- Geospatial types
geopoint GEO_POINT,
geoshape GEO_SHAPE,
-- Vector type
""float_vector"" FLOAT_VECTOR(3)
);
", conn))
{
cmd.ExecuteNonQuery();
}

// Insert single data point.
await using (var cmd = new NpgsqlCommand(@"
INSERT INTO testdrive.example (
null_integer,
integer,
bigint,
float,
double,
decimal,
bit,
bool,
text,
char,
timestamp_tz,
timestamp_notz,
ip,
""array"",
""object"",
geopoint,
geoshape,
float_vector
) VALUES (
@null_integer,
@integer,
@bigint,
@float,
@double,
@decimal,
@bit,
@bool,
@text,
@char,
@timestamp_tz,
@timestamp_notz,
@ip,
@array,
@object,
@geopoint,
@geoshape,
@float_vector
);
", conn))
{
Console.WriteLine(cmd);
cmd.Parameters.AddWithValue("null_integer", DBNull.Value);
cmd.Parameters.AddWithValue("integer", 42);
cmd.Parameters.AddWithValue("bigint", 42);
cmd.Parameters.AddWithValue("float", 42.42);
cmd.Parameters.AddWithValue("double", 42.42);
cmd.Parameters.AddWithValue("decimal", 42.42);
cmd.Parameters.AddWithValue("bit", "01010101");
cmd.Parameters.AddWithValue("bool", true);
cmd.Parameters.AddWithValue("text", "foobar");
cmd.Parameters.AddWithValue("char", "foo");
cmd.Parameters.AddWithValue("timestamp_tz", "1970-01-02T00:00:00+01:00");
cmd.Parameters.AddWithValue("timestamp_notz", "1970-01-02T00:00:00");
Comment on lines +154 to +155
Copy link
Member Author

@amotl amotl Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about those, because apparently, they work in this context, but it is also just a very basic test anyway. Let me reference this report, to have a complete list of evalations here, and to reduce the surface of unknown unknowns.

It might indicate a flaw we may investigate closer and possibly resolve while modernizing the driver.

cmd.Parameters.AddWithValue("ip", "127.0.0.1");
cmd.Parameters.AddWithValue("array", new List<string>{"foo", "bar"});
// FIXME: System.NotSupportedException: Cannot resolve 'hstore' to a fully qualified datatype name. The datatype was not found in the current database info.
// https://github.com/crate/zk/issues/26
// cmd.Parameters.AddWithValue("object", new Dictionary<string, string>(){{"foo", "bar"}});
cmd.Parameters.AddWithValue("object", @"{""foo"": ""bar""}");
cmd.Parameters.AddWithValue("geopoint", new List<double>{85.43, 66.23});
cmd.Parameters.AddWithValue("geoshape", "POLYGON ((5 5, 10 5, 10 10, 5 10, 5 5))");
cmd.Parameters.AddWithValue("float_vector", new List<double> {1.1, 2.2, 3.3});
cmd.ExecuteNonQuery();
}

// Flush data.
await using (var cmd = new NpgsqlCommand("REFRESH TABLE testdrive.example", conn))
{
cmd.ExecuteNonQuery();
}

// Query back data.
await using (var cmd = new NpgsqlCommand("SELECT * FROM testdrive.example", conn))
await using (var reader = cmd.ExecuteReader())
{
var dataTable = new DataTable();
dataTable.Load(reader);
var payload = JsonConvert.SerializeObject(dataTable);
Console.WriteLine(payload);
return (DataTable) dataTable;
}

}

public static async Task<DataTable> ContainerTypesExample(NpgsqlConnection conn)
{
Console.WriteLine("Running AllTypesExample");

// Submit DDL, create database schema.
await using (var cmd = new NpgsqlCommand("DROP TABLE IF EXISTS testdrive.container", conn))
{
cmd.ExecuteNonQuery();
}

await using (var cmd = new NpgsqlCommand(@"
CREATE TABLE testdrive.container (
-- Container types
""array"" ARRAY(STRING),
""object"" OBJECT(DYNAMIC)
);
", conn))
{
cmd.ExecuteNonQuery();
}

// Insert single data point.
await using (var cmd = new NpgsqlCommand(@"
INSERT INTO testdrive.container (
""array"",
""object""
) VALUES (
@array,
@object
);
", conn))
{
Console.WriteLine(cmd);
// FIXME: While doing conversations with ARRAY types works natively,
// it doesn't work for OBJECT types.
// Yet, they can be submitted as STRING in JSON format.
cmd.Parameters.AddWithValue("array", new List<string>{"foo", "bar"});
cmd.Parameters.AddWithValue("object", @"{""foo"": ""bar""}");
cmd.ExecuteNonQuery();
}

// Flush data.
await using (var cmd = new NpgsqlCommand("REFRESH TABLE testdrive.container", conn))
{
cmd.ExecuteNonQuery();
}

// Query back data.
await using (var cmd = new NpgsqlCommand("SELECT * FROM testdrive.container", conn))
await using (var reader = cmd.ExecuteReader())
{
var dataTable = new DataTable();
dataTable.Load(reader);
var payload = JsonConvert.SerializeObject(dataTable);
Console.WriteLine(payload);
return (DataTable) dataTable;
}

}

}

}
15 changes: 15 additions & 0 deletions by-language/csharp-npgsql/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ For running tests selectively, use::
dotnet test --framework=net8.0 --filter SystemQueryExample


Troubleshooting
===============

If you observe an error like this when invoking the program or test case::

Microsoft.PackageDependencyResolution.targets(266,5): error NETSDK1005:
Assets file '/path/to/csharp-npgsql/obj/project.assets.json' doesn't have a target for 'net9.0'.
Ensure that restore has run and that you have included 'net9.0' in the TargetFrameworks for your project.

please adjust ``demo.csproj`` like that::

- <TargetFrameworks>net6.0;net8.0</TargetFrameworks>
+ <TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>


.. _C#: https://en.wikipedia.org/wiki/C_Sharp_(programming_language)
.. _crate-npgsql: https://github.com/crate/crate-npgsql
.. _Npgsql - .NET Access to PostgreSQL: https://github.com/npgsql/npgsql
Loading