sqlglot icon indicating copy to clipboard operation
sqlglot copied to clipboard

Postgres: transpile writes a function parameter mode twice

Open rileythomp opened this issue 3 weeks ago • 1 comments

Read dialect: postgres Write dialect: postgres The issue still exists on main (539d7b9). Reproduced on 30.31.0.

Fully reproducible code snippet

import sqlglot

sql = "CREATE FUNCTION foo(IN a INT)"

print(sqlglot.transpile(sql, read="postgres", write="postgres"))

ast = sqlglot.parse_one(sql, read="postgres")
print(ast.sql(dialect="postgres", copy=False))
print(ast.sql(dialect="postgres"))

Actual output:

['CREATE FUNCTION foo(IN a INT IN)']
CREATE FUNCTION foo(IN a INT IN)
CREATE FUNCTION foo(IN a INT)

Expected output:

['CREATE FUNCTION foo(IN a INT)']
CREATE FUNCTION foo(IN a INT)
CREATE FUNCTION foo(IN a INT)

Only the default copy=True gives the correct output. The same happens with OUT, INOUT and VARIADIC.

Official Documentation https://www.postgresql.org/docs/current/sql-createfunction.html

rileythomp avatar Nov 11 '26 23:11 rileythomp

Reproduced on main (539d7b9) with the exact snippet. The parameter-mode constraint is attached to ColumnDef.args["constraints"] without a parent link, so param_constraint.pop() is a no-op when copy=False and the constraint is rendered twice. I have a fix that sets the parent link in the parser and restores the constraint around generation; tests cover IN/OUT/INOUT/VARIADIC, mixed parameters, and DEFAULT. PR coming.

agustin18 avatar Nov 12 '26 00:11 agustin18