Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions __fixtures__/generated/generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"pretty/misc-14.sql": "CREATE TRIGGER decrease_job_queue_count_on_delete \n AFTER DELETE ON dashboard_jobs.jobs \n FOR EACH ROW\n WHEN ( OLD.queue_name IS NOT NULL ) \n EXECUTE PROCEDURE dashboard_jobs.tg_decrease_job_queue_count ()",
"pretty/misc-15.sql": "ALTER DEFAULT PRIVILEGES IN SCHEMA dashboard_jobs \n GRANT EXECUTE ON FUNCTIONS TO administrator",
"pretty/misc-16.sql": "GRANT EXECUTE ON FUNCTION dashboard_private.uuid_generate_seeded_uuid TO PUBLIC",
"pretty/misc-17.sql": "SELECT CAST(t.date AT TIME ZONE $$America/New_York$$ AS text)::date FROM tbl t",
"pretty/cte-1.sql": "WITH regional_sales AS (SELECT region, SUM(sales_amount) as total_sales FROM sales GROUP BY region) SELECT * FROM regional_sales",
"pretty/cte-2.sql": "WITH regional_sales AS (SELECT region, SUM(sales_amount) as total_sales FROM sales GROUP BY region), top_regions AS (SELECT region FROM regional_sales WHERE total_sales > 1000000) SELECT * FROM top_regions",
"pretty/cte-3.sql": "WITH RECURSIVE employee_hierarchy AS (SELECT id, name, manager_id, 1 as level FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.id, e.name, e.manager_id, eh.level + 1 FROM employees e JOIN employee_hierarchy eh ON e.manager_id = eh.id) SELECT * FROM employee_hierarchy",
Expand Down Expand Up @@ -21285,6 +21286,7 @@
"misc/issues-14.sql": "SELECT (1 IS NOT NULL) IS DISTINCT FROM (2 IS NOT NULL)",
"misc/issues-15.sql": "select \"A\" from \"table_name\"",
"misc/issues-16.sql": "select \"AA\" from \"table_name\"",
"misc/issues-17.sql": "SELECT CAST(t.date AT TIME ZONE $$America/New_York$$ AS text)::date FROM tbl t",
"misc/inflection-1.sql": "CREATE SCHEMA inflection",
"misc/inflection-2.sql": "GRANT USAGE ON SCHEMA inflection TO PUBLIC",
"misc/inflection-3.sql": "ALTER DEFAULT PRIVILEGES IN SCHEMA inflection \n GRANT EXECUTE ON FUNCTIONS TO PUBLIC",
Expand Down
5 changes: 4 additions & 1 deletion __fixtures__/kitchen-sink/misc/issues.sql
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ SELECT (1 IS NOT NULL) IS DISTINCT FROM (2 IS NOT NULL);

-- https://github.com/launchql/pgsql-parser/issues/101
select "A" from "table_name";
select "AA" from "table_name";
select "AA" from "table_name";

-- https://github.com/launchql/pgsql-parser/issues/217
SELECT CAST(t.date AT TIME ZONE $$America/New_York$$ AS text)::date FROM tbl t;
4 changes: 4 additions & 0 deletions __fixtures__/kitchen-sink/pretty/misc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,7 @@ ALTER DEFAULT PRIVILEGES IN SCHEMA dashboard_jobs
-- 16. grant execute on function

GRANT EXECUTE ON FUNCTION dashboard_private.uuid_generate_seeded_uuid TO PUBLIC;


-- https://github.com/launchql/pgsql-parser/issues/217
SELECT CAST(t.date AT TIME ZONE $$America/New_York$$ AS text)::date FROM tbl t;
3 changes: 2 additions & 1 deletion packages/deparser/__tests__/kitchen-sink/misc-issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ it('misc-issues', async () => {
"misc/issues-13.sql",
"misc/issues-14.sql",
"misc/issues-15.sql",
"misc/issues-16.sql"
"misc/issues-16.sql",
"misc/issues-17.sql"
]);
});
3 changes: 2 additions & 1 deletion packages/deparser/__tests__/kitchen-sink/pretty-misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ it('pretty-misc', async () => {
"pretty/misc-13.sql",
"pretty/misc-14.sql",
"pretty/misc-15.sql",
"pretty/misc-16.sql"
"pretty/misc-16.sql",
"pretty/misc-17.sql"
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`should format pg_catalog.char with pretty option enabled 1`] = `
"CREATE TABLE dashboard_jobs.jobs (
id bigserial PRIMARY KEY,
queue_name text DEFAULT public.gen_random_uuid()::text,
queue_name text DEFAULT (public.gen_random_uuid())::text,
task_identifier text NOT NULL,
payload pg_catalog.json DEFAULT '{}'::json NOT NULL,
priority int DEFAULT 0 NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ exports[`non-pretty: pretty/misc-15.sql 1`] = `"ALTER DEFAULT PRIVILEGES IN SCHE

exports[`non-pretty: pretty/misc-16.sql 1`] = `"GRANT EXECUTE ON FUNCTION dashboard_private.uuid_generate_seeded_uuid TO PUBLIC"`;

exports[`non-pretty: pretty/misc-17.sql 1`] = `"SELECT CAST((t.date AT TIME ZONE 'America/New_York')::text AS date) FROM tbl AS t"`;

exports[`pretty: pretty/misc-1.sql 1`] = `
"WITH
recent_orders AS (SELECT
Expand Down Expand Up @@ -295,3 +297,8 @@ exports[`pretty: pretty/misc-15.sql 1`] = `
`;

exports[`pretty: pretty/misc-16.sql 1`] = `"GRANT EXECUTE ON FUNCTION dashboard_private.uuid_generate_seeded_uuid TO PUBLIC"`;

exports[`pretty: pretty/misc-17.sql 1`] = `
"SELECT CAST((t.date AT TIME ZONE 'America/New_York')::text AS date)
FROM tbl AS t"
`;
3 changes: 2 additions & 1 deletion packages/deparser/__tests__/pretty/misc-pretty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const testCases = [
'pretty/misc-13.sql',
'pretty/misc-14.sql',
'pretty/misc-15.sql',
'pretty/misc-16.sql'
'pretty/misc-16.sql',
'pretty/misc-17.sql',
];

const prettyTest = new PrettyTest(testCases);
Expand Down
9 changes: 7 additions & 2 deletions packages/deparser/src/deparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2255,9 +2255,14 @@ export class Deparser implements DeparserVisitor {

if (isSimpleArgument || isFunctionCall) {
// For simple arguments, avoid :: syntax if they have complex structure
if (isSimpleArgument && (arg.includes('(') || arg.startsWith('-'))) {
} else {
const shouldUseCastSyntax = isSimpleArgument && (arg.includes('(') || arg.startsWith('-'));

if (!shouldUseCastSyntax) {
const cleanTypeName = typeName.replace('pg_catalog.', '');
// Wrap FuncCall arguments in parentheses to prevent operator precedence issues
if (isFunctionCall) {
return `${context.parens(arg)}::${cleanTypeName}`;
}
return `${arg}::${cleanTypeName}`;
}
}
Expand Down