pganalyze / libpg_query

C library for accessing the PostgreSQL parser outside of the server environment
BSD 3-Clause "New" or "Revised" License
1.21k stars 182 forks source link

Allow alternate definitions of NAMEDATALEN identifier limit #268

Open david-h-muller opened 1 month ago

david-h-muller commented 1 month ago

What

This PR surrounds the definition of the NAMEDATALEN constant with an #ifndef.

Why

Increasing NAMEDATALEN allows you to, for example, avoid the truncation normally produced by the postgres parser on this 65 character identifier:

SELECT aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa12345
{
  "stmts": [
    {
      "stmt": {
        "SelectStmt": {
          "limitOption": "LIMIT_OPTION_DEFAULT",
          "op": "SETOP_NONE",
          "targetList": [
            {
              "ResTarget": {
                "location": 7,
                "val": {
                  "ColumnRef": {
                    "fields": [
                      {
                        "String": {
- "sval": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa123"
+ "sval": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa12345"
                        }
                      }
                    ],
                    "location": 7
                  }
                }
              }
            }
          ]
        }
      }
    }
  ],
  "version": 150001
}

Notes

To generate this PR and validate it, I needed to run make extract_source.

To get make extract_source working, I needed these 3 pre-requisites:

  1. macOS.
  2. ruby 3.1.2.
  3. gem ffi-clang 0.8.0

    • gem install ffi-clang --version'0.8.0'
    • I did not see the ffi-clang requirement pinned or documented anywhere, but versions of ffi-clang newer than 0.8.0 move the visit_children method and cause a runtime error in extract_source.rb:
      
      extract_source.rb:260:in `analyze_file': undefined method `visit_children' for an instance of FFI::Clang::Cursor (NoMethodError)

    cursor.visit_children do |cursor, parent| ^^^^^^^^^^^^^^^