Skip to content

Using latest pganalyze/libpg_query vs not latest pg_query.proto fails Protobuf via NuGet #2

Description

@svdgriendt

After typing all of the below, I think I spotted the issue
In the main.yaml you're building against the latest version of https://github.com/pganalyze/libpg_query.git, but that's not the version of which the proto file is. Maybe it's better to pin these to specific commit hashes, to prevent these mismatches? My assumption is that updating the protobuf file and running the C# codegen again will fix this issue.

It's that I don't know how exactly you generated the the C# code out of it, it seems automatic in some way as I can't find the PgQuery.cs file anywhere...

Also a small hint: the NATIVE_SETUP.md is now outdated that the Examples/pg_query.dll is no longer there 😉.


I am not entirely sure where you fish this pg_query.dll from. I was surprised that my check-out with a submodule was working, but with the NuGet version it was not. I noticed this, because after peeking a bit beyond the examples, I noticed you already implemented a protobuf version, which covers a lot of what I was talking about earlier for "strongly typed classes/structs" in #1

I noticed that in 824e098 there were two pg_query.dll files, but they're actually different ones! When I include the one from Examples, I get the same behavior as with the current NuGet version. It's also exactly the same in size. When I use the one from Npgquery.Tests (which I did before with the submodule), it works as expected.

Some code to reproduce this with (using the NuGet package)

public static void DoIt()
{
  var query = """
              CREATE TABLE foo
              (
                id INT PRIMARY KEY
              );
              """;

  var tmp = Parser.QuickParse(query);
  var parsed = Parser.QuickParseProtobuf(query);
  if (parsed.IsError)
    return;

  var statement = parsed.ParseTree.Stmts[0].Stmt;
  switch (statement.NodeCase)
  {
    case Node.NodeOneofCase.CreateStmt:
      var createStatement = statement.CreateStmt!;
      var catalogName = createStatement.Relation.Catalogname;
      var schemaName = createStatement.Relation.Schemaname;
      var tableName = createStatement.Relation.Relname;
      Console.WriteLine($"Table = {catalogName}.{schemaName}.{tableName}");
      break;
  }
}

What you'll see when you use the NuGet or the Examples version, which is 3,732,480 bytes, that the protobuf version comes back with a DropTableSpaceStmt. When you use the Npgquery.Tests version, which is 3,558,400 bytes, that the protobuf version comes back with a CreateStmt, which is expected here.

Also, when I add this with the latest commit available currently (f57f798), I just passes. But again, this uses a different version of pg_query.dll (for win-x64 at least) than that's packaged in NuGet.

    [Fact]
    public void ParseProtobuf_CreateTable_ReturnsCreateStmt()
    {
        // Arrange
        var query = "CREATE TABLE foo (id INT PRIMARY KEY);";

        // Act
        var result = _parser.ParseProtobuf(query);

        // Assert
        Assert.True(result.IsSuccess);
        Assert.NotNull(result.ParseTree);
        Assert.Null(result.Error);
        Assert.Equal(query, result.Query);
        Assert.NotNull(result.ParseTree.Stmts);
        Assert.Single(result.ParseTree.Stmts);
        Assert.Equal(Node.NodeOneofCase.CreateStmt, result.ParseTree.Stmts[0].Stmt.NodeCase);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions