Skip to content
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

BinaryExporter exception when read char values #5110

Closed
SoftStoneDevelop opened this issue Jun 18, 2023 · 2 comments · Fixed by #5111
Closed

BinaryExporter exception when read char values #5110

SoftStoneDevelop opened this issue Jun 18, 2023 · 2 comments · Fixed by #5111
Assignees
Labels
Milestone

Comments

@SoftStoneDevelop
Copy link
Contributor

Steps to reproduce

            using (var connection = new NpgsqlConnection(root.GetConnectionString("SqlConnection")))
            {
                connection.Open();
                var cmd = connection.CreateCommand();
                cmd.CommandText = @"
CREATE TABLE IF NOT EXISTS public.charTable
(
    id serial NOT NULL,
    value char NOT NULL,
    CONSTRAINT charTable_pkey PRIMARY KEY (id)
);
";
                cmd.ExecuteNonQuery();

                cmd.CommandText = @"
INSERT INTO public.charTable
(value)
VALUES
($1)
";
                var parametr = new NpgsqlParameter<char>();
                parametr.NpgsqlValue = 'd';
                cmd.Parameters.Add(parametr);
                cmd.ExecuteNonQuery();

                parametr.NpgsqlValue = 's';
                cmd.ExecuteNonQuery();

                using (var export = connection.BeginBinaryExport(@"

COPY public.charTable
(
    id,
    value
) TO STDOUT (FORMAT BINARY)

"))
                {
                    while (export.StartRow() != -1)
                    {
                        var id = export.Read<int>();//got NullReferenceException on second iteration
                        var value = export.Read<char>();
                    }
                }
            }

The issue

First iteration is works fine, but when i excpect on the second iteration where value must be 's' I get throw Exception(some times nullreference, some times Npgsql.NpgsqlException : Unknown message code: 100 seems 100 is 'd')

Exception message:
Stack trace:
System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Npgsql
  StackTrace:
   at Npgsql.NpgsqlBinaryExporter.<DoRead>d__29`1.MoveNext()
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at Npgsql.NpgsqlBinaryExporter.Read[T]()
   at NpgsqlBenchmark.Tests.CharExportTest.Test() in E:\Work\SoftStoneDevelop github\NpgsqlBinaryExporter\Src\NpgsqlBenchmark\Tests\CharExportTest.cs:line 57

  This exception was originally thrown at this call stack:
    [External Code]
    NpgsqlBenchmark.Tests.CharExportTest.Test() in CharExportTest.cs

Further technical details

Npgsql version: 7.0.4
PostgreSQL version: PostgreSQL 15.2, compiled by Visual C++ build 1914, 64-bit
Operating system:
Edition Windows 10 Pro
Version 22H2
OS build 19045.3086

@SoftStoneDevelop
Copy link
Contributor Author

Repository with test code: https://github.com/SoftStoneDevelop/NpgsqlBinaryExporter

@vonzshik
Copy link
Contributor

Hello. Yep, I can confirm there is indeed a bug with how we read char values. Thank you for raising this issue!

@vonzshik vonzshik self-assigned this Jun 18, 2023
@vonzshik vonzshik added the bug label Jun 18, 2023
@vonzshik vonzshik added this to the 7.0.6 milestone Jun 18, 2023
vonzshik added a commit that referenced this issue Jun 19, 2023
vonzshik added a commit that referenced this issue Jun 19, 2023
vonzshik added a commit that referenced this issue Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants