Allison-E / pg-age

Apache License 2.0
13 stars 6 forks source link

Not working with the docker version of Age #3

Closed paule96 closed 6 months ago

paule96 commented 9 months ago

hi,

I currently try to play around with pg-age and dotnet. So I found this library. Because playing around with docker is easy, I use the official docker image apache/age. This has a preconfigured postgre with the age extension installed.

I started to try the extension and couldn't read the data:

System.InvalidCastException: Reading as 'System.Object' is not supported for fields having DataTypeName 'ag_catalog.agtype'
   at Npgsql.Internal.AdoSerializerHelpers.<GetTypeInfoForReading>g__ThrowReadingNotSupported|0_0(Type type, String displayName, Exception inner)
   at Npgsql.Internal.AdoSerializerHelpers.GetTypeInfoForReading(Type type, PostgresType postgresType, PgSerializerOptions options)
   at Npgsql.BackendMessages.FieldDescription.<GetInfo>g__GetInfoSlow|50_0(Type type, ColumnInfo& lastColumnInfo)
   at Npgsql.BackendMessages.FieldDescription.GetInfo(Type type, ColumnInfo& lastColumnInfo)
   at Npgsql.BackendMessages.FieldDescription.get_ObjectOrDefaultInfo()
   at Npgsql.NpgsqlDataReader.GetValue(Int32 ordinal)
   at Npgsql.NpgsqlDataReader.GetValues(Object[] values)
   at ApacheAGE.AgeDataReader.GetValues()
   at Program.<>c__DisplayClass0_0.<<<Main>$>b__2>d.MoveNext() in D:\labs\postgreGraphDemo\postgreGraphDemo.ApiService\Program.cs:line 69
--- End of stack trace from previous location ---
   at Program.<>c__DisplayClass0_0.<<<Main>$>b__2>d.MoveNext() in D:\labs\postgreGraphDemo\postgreGraphDemo.ApiService\Program.cs:line 83
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Http.RequestDelegateFactory.<ExecuteTaskOfTFast>g__ExecuteAwaited|132_0[T](Task`1 task, HttpContext httpContext, JsonTypeInfo`1 jsonTypeInfo)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.<Invoke>g__Awaited|10_0(ExceptionHandlerMiddlewareImpl middleware, HttpContext context, Task task)

it seams like the the getValue methods try to read things as objects but are not able todo so.

glazkovalex commented 9 months ago

Hello, @paule96! So that the author can quickly understand what exactly is not working, could you provide the code of your case? How you add data and how you try to read it. What do you have there in "postgreGraphDemo.Api Service.Program.cs"?

paule96 commented 9 months ago

Oh right I forgot to mention that. I used the sample of the Readme from that repo ✌️

paule96 commented 9 months ago

Basically the following lines are the issue:

// Read the result row by row.
while(await reader.ReadAsync())
{
    var agtypeResult = reader.GetValue(0);
    Vertex person = agtypeResult.GetVertex();

    // Do something with vertex.
}
glazkovalex commented 9 months ago

I have this example, on Apache AGE installed in Postgres according to this instruction, it also gives the same error. Apparently, the problem is Npgsql, which does not support "agtype". I don't know how to fix it yet, but this problem is most likely related to this issues: "Is there any plan to support Apache AGE extension? #4514".

In theory, Npgsql can somehow be extended to support additional data types. Here, for example, a similar thing was done.

Allison-E commented 9 months ago

Yes, @glazkovalex. The issue is that Npgsql doesn't automatically map to AGE's 'agtype'. I will work on creating a mapping.

NinoFloris commented 8 months ago

Yes, @glazkovalex. The issue is that Npgsql doesn't automatically map to AGE's 'agtype'. I will work on creating a mapping.

I'm one of the maintainers of Npgsql, if you need help with this let me know.

glazkovalex commented 8 months ago

Hello, @NinoFloris!

Yes, @glazkovalex. The issue is that Npgsql doesn't automatically map to AGE's 'agtype'. I will work on creating a mapping.

I'm one of the maintainers of Npgsql, if you need help with this let me know.

You probably need to clarify the context of the problem. The Apache Age extension usually returns objects of its own type agtype as a result of the request, the data structure of which is described in detail here. Here is an example of what returns a request from the discussed Quickstart directly to pgAdmin: image See, this is a special type of agtype and not a string or json because of ":vertex". @Allison-E described the 'agtype' data types in C#, but Npsgql does not know how to convert 'agtype' to the data types described by @Allison-E. An exception occurs: System.InvalidCastException: "Reading as 'System.Object' is not supported for fields having DataTypeName 'ag_catalog.agtype'"

In your issue 4514 on https://github.com/npgsql/npgsql/issues/4514#issuecomment-1167617467 @roji wrote about this issue:

@FrancescoSillani that's good - it means that the AGE extension does support binary I/O for the agtype type, and can therefore be supported in Npgsql. But someone would still need to write the Npsgql type handlers which encode/decode the binary representation etc.

Have you ever come across information or an example of how you can write these handlers for unsupported in Npgsql agtype types which encode/decode the binary representation?

Allison-E commented 6 months ago

This has now been fixed in ApacheAGE v1.0.0. The System.InvalidCastException being thrown was resolved by creating a custom converter and resolver to add in-built support for the ag_catalog.agtype PostgreSQL type in Npgsql.