Skip to content

Commit

Permalink
fix: resolve compatibility issues with Java and Rust clients (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoyException authored Dec 6, 2024
1 parent 69db038 commit 340611d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/clients-compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
sudo mv duckdb /usr/local/bin
cd compatibility/pg/
# curl -L -o ./java/postgresql-42.7.4.jar https://jdbc.postgresql.org/download/postgresql-42.7.4.jar
curl -L -o ./java/postgresql-42.7.4.jar https://jdbc.postgresql.org/download/postgresql-42.7.4.jar
npm install pg
sudo cpanm --notest DBD::Pg
pip3 install "psycopg[binary]" pandas pyarrow polars
Expand Down
26 changes: 11 additions & 15 deletions compatibility/pg/csharp/PGTest.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Data.SqlClient;
using Npgsql;
using System.IO;

public class PGTest
{
public class Tests
{
private SqlConnection conn;
private SqlCommand cmd;
private NpgsqlConnection conn;
private NpgsqlCommand cmd;
private List<Test> tests = new List<Test>();

public void Connect(string ip, int port, string user, string password)
{
string connectionString = $"Server={ip},{port};User Id={user};Password={password};";
string connectionString = $"Host={ip};Port={port};Username={user};Password={password};Database=postgres;";
try
{
conn = new SqlConnection(connectionString);
conn = new NpgsqlConnection(connectionString);
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
}
catch (SqlException e)
catch (NpgsqlException e)
{
throw new Exception($"Error connecting to database: {e.Message}", e);
}
Expand All @@ -35,7 +35,7 @@ public void Disconnect()
cmd.Dispose();
conn.Close();
}
catch (SqlException e)
catch (NpgsqlException e)
{
throw new Exception(e.Message);
}
Expand Down Expand Up @@ -97,7 +97,7 @@ public Test(string query, string[][] expectedResults)
this.expectedResults = expectedResults;
}

public bool Run(SqlCommand cmd)
public bool Run(NpgsqlCommand cmd)
{
try
{
Expand Down Expand Up @@ -125,15 +125,11 @@ public bool Run(SqlCommand cmd)
{
for (int col = 0; col < expectedResults[rows].Length; col++)
{
string result = reader.GetString(col);
string result = reader.GetValue(col).ToString().Trim();
if (expectedResults[rows][col] != result)
{
Console.Error.WriteLine($"Expected:\n'{expectedResults[rows][col]}'");
Console.Error.WriteLine($"Result:\n'{result}'\nRest of the results:");
while (reader.Read())
{
Console.Error.WriteLine(reader.GetString(0));
}
Console.Error.WriteLine($"Result:\n'{result}'");
return false;
}
}
Expand All @@ -148,7 +144,7 @@ public bool Run(SqlCommand cmd)
return true;
}
}
catch (SqlException e)
catch (NpgsqlException e)
{
Console.Error.WriteLine(e.Message);
return false;
Expand Down
2 changes: 1 addition & 1 deletion compatibility/pg/csharp/PGTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2" />
<PackageReference Include="Npgsql" Version="7.0.0" />
</ItemGroup>

</Project>
7 changes: 2 additions & 5 deletions compatibility/pg/java/PGTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,10 @@ public boolean run() {
while (rs.next()) {
int cols = 0;
for (String expected : expectedResults[rows]) {
String result = rs.getString(cols + 1);
String result = rs.getString(cols + 1).trim();
if (!expected.equals(result)) {
System.err.println("Expected:\n'" + expected + "'");
System.err.println("Result:\n'" + result + "'\nRest of the results:");
while (rs.next()) {
System.err.println(rs.getString(1));
}
System.err.println("Result:\n'" + result + "'");
return false;
}
cols++;
Expand Down
13 changes: 8 additions & 5 deletions compatibility/pg/rust/pg_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ impl Test {
}
for (i, row) in rows.iter().enumerate() {
for (j, expected) in self.expected_results[i].iter().enumerate() {
let result: String = row.get(j);
let result: String = row.try_get::<usize, String>(j)
.or_else(|_| row.try_get::<usize, i32>(j).map(|v| v.to_string()))
.or_else(|_| row.try_get::<usize, i64>(j).map(|v| v.to_string()))
.or_else(|_| row.try_get::<usize, f64>(j).map(|v| v.to_string()))
.unwrap_or_default()
.trim()
.to_string();
if expected != &result {
eprintln!("Expected:\n'{}'", expected);
eprintln!("Result:\n'{}'\nRest of the results:", result);
for row in rows.iter().skip(i + 1) {
eprintln!("{}", row.get::<usize, String>(0));
}
eprintln!("Result:\n'{}'", result);
return false;
}
}
Expand Down
20 changes: 9 additions & 11 deletions compatibility/pg/test.bats
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bats

setup() {
psql -h 127.0.0.1 -p 5432 -U postgres -c "DROP SCHEMA IF EXISTS test CASCADE;"
psql -h 127.0.0.1 -p 5432 -U postgres -d postgres -c "DROP SCHEMA IF EXISTS test CASCADE;"
touch /tmp/test_pids
}

Expand Down Expand Up @@ -52,12 +52,10 @@ start_process() {
start_process $BATS_TEST_DIRNAME/go/pg 127.0.0.1 5432 postgres "" $BATS_TEST_DIRNAME/test.data
}

# Before you uncomment the following tests, you need to uncomment the corresponding lines in
# .github/workflows/clients-compatibility.yml, which will download the necessary dependencies.
# @test "pg-java" {
# start_process javac $BATS_TEST_DIRNAME/java/PGTest.java
# start_process java -cp $BATS_TEST_DIRNAME/java:$BATS_TEST_DIRNAME/java/postgresql-42.7.4.jar PGTest 127.0.0.1 5432 postgres "" $BATS_TEST_DIRNAME/test.data
# }
@test "pg-java" {
start_process javac $BATS_TEST_DIRNAME/java/PGTest.java
start_process java -cp $BATS_TEST_DIRNAME/java:$BATS_TEST_DIRNAME/java/postgresql-42.7.4.jar PGTest 127.0.0.1 5432 postgres "" $BATS_TEST_DIRNAME/test.data
}

@test "pg-node" {
start_process node $BATS_TEST_DIRNAME/node/pg_test.js 127.0.0.1 5432 postgres "" $BATS_TEST_DIRNAME/test.data
Expand All @@ -83,7 +81,7 @@ start_process() {
start_process ruby $BATS_TEST_DIRNAME/ruby/pg_test.rb 127.0.0.1 5432 postgres "" $BATS_TEST_DIRNAME/test.data
}

# @test "pg-rust" {
# start_process cargo build --release --manifest-path $BATS_TEST_DIRNAME/rust/Cargo.toml
# start_process $BATS_TEST_DIRNAME/rust/target/release/pg_test 127.0.0.1 5432 postgres "" $BATS_TEST_DIRNAME/test.data
# }
@test "pg-rust" {
start_process cargo build --release --manifest-path $BATS_TEST_DIRNAME/rust/Cargo.toml
start_process $BATS_TEST_DIRNAME/rust/target/release/pg_test 127.0.0.1 5432 postgres "" $BATS_TEST_DIRNAME/test.data
}
2 changes: 1 addition & 1 deletion compatibility/pg/test.data
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE SCHEMA test

CREATE TABLE test.tb1 (id int, value float, c1 char(10), primary key(id))
CREATE TABLE test.tb1 (id int, value double precision, c1 char(10), primary key(id))

INSERT INTO test.tb1 VALUES (1, 1.1, 'a'), (2, 2.2, 'b')

Expand Down
6 changes: 5 additions & 1 deletion pgserver/connection_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (h *ConnectionHandler) sendClientStartupMessages() error {
// which lists all the available parameters in PostgreSQL. In that case,
// we will use a mock value for that parameter. e.g. "on" for "is_superuser".
{"in_hot_standby", nil},
{"integer_datetimes", nil},
{"integer_datetimes", "on"},
{"TimeZone", nil},
{"IntervalStyle", nil},
{"is_superuser", "on"}, // This is not specified in postgresConfigParameters now.
Expand Down Expand Up @@ -641,6 +641,10 @@ func (h *ConnectionHandler) handleDescribe(message *pgproto3.Describe) error {
bindvarTypes = preparedStatementData.BindVarTypes
tag = preparedStatementData.Query.StatementTag
}

if bindvarTypes == nil {
bindvarTypes = make([]uint32, 0)
}
} else {
portalData, ok := h.portals[message.Name]
if !ok {
Expand Down

0 comments on commit 340611d

Please sign in to comment.