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
7 changes: 7 additions & 0 deletions __fixtures__/generated/generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -21242,6 +21242,10 @@
"misc/typecast-15.sql": "SELECT 'x'::pg_catalog.bpchar[]",
"misc/typecast-16.sql": "SELECT 'x'::bpchar",
"misc/typecast-17.sql": "SELECT 'x'::app.some_type",
"misc/typecast-18.sql": "SELECT 'x'::pg_catalog.\"char\"",
"misc/typecast-19.sql": "SELECT 'x'::\"char\"",
"misc/typecast-20.sql": "SELECT '{}'::pg_catalog.\"char\"[], '{}'::\"char\"[]",
"misc/typecast-21.sql": "CREATE TABLE char_types (qualified pg_catalog.\"char\", unqualified \"char\", standard char)",
"misc/transaction-chain-1.sql": "COMMIT AND CHAIN",
"misc/transaction-chain-2.sql": "ROLLBACK AND CHAIN",
"misc/transaction-chain-3.sql": "END AND CHAIN",
Expand Down Expand Up @@ -21620,6 +21624,9 @@
"misc/operator-names-1.sql": "SELECT 1 OPERATOR(\"My Schema\".+) 2",
"misc/operator-names-2.sql": "SELECT 1 OPERATOR(\"S\".=) ANY (ARRAY[1])",
"misc/operator-names-3.sql": "SELECT 1 OPERATOR(\"Say\"\"Hi\".=) ALL (SELECT 1)",
"misc/operator-names-4.sql": "SELECT x FROM (VALUES (1), (2)) AS t(x) ORDER BY x USING OPERATOR(pg_catalog.<)",
"misc/operator-names-5.sql": "SELECT x FROM (VALUES (1), (2)) AS t(x) ORDER BY x USING OPERATOR(\"Say\"\"Hi\".>) NULLS FIRST",
"misc/operator-names-6.sql": "SELECT x FROM (VALUES (1), (2)) AS t(x) ORDER BY x USING < NULLS LAST",
"misc/multiline-string-literals-1.sql": "CREATE TABLE t (memo text DEFAULT 'line one\nline two')",
"misc/multiline-string-literals-2.sql": "CREATE TABLE t (id int PRIMARY KEY, memo text NOT NULL DEFAULT 'line one\n line two\n line three', n int)",
"misc/multiline-string-literals-3.sql": "CREATE TABLE t (memo text DEFAULT E'tab\\there\nback\\\\slash\nquote''s')",
Expand Down
3 changes: 3 additions & 0 deletions __fixtures__/kitchen-sink/misc/operator-names.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
SELECT 1 OPERATOR("My Schema".+) 2;
SELECT 1 OPERATOR("S".=) ANY (ARRAY[1]);
SELECT 1 OPERATOR("Say""Hi".=) ALL (SELECT 1);
SELECT x FROM (VALUES (1), (2)) AS t(x) ORDER BY x USING OPERATOR(pg_catalog.<);
SELECT x FROM (VALUES (1), (2)) AS t(x) ORDER BY x USING OPERATOR("Say""Hi".>) NULLS FIRST;
SELECT x FROM (VALUES (1), (2)) AS t(x) ORDER BY x USING < NULLS LAST;
4 changes: 4 additions & 0 deletions __fixtures__/kitchen-sink/misc/typecast.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ SELECT 'x'::pg_catalog.bpchar(3);
SELECT 'x'::pg_catalog.bpchar[];
SELECT 'x'::bpchar;
SELECT 'x'::app.some_type;
SELECT 'x'::pg_catalog."char";
SELECT 'x'::"char";
SELECT '{}'::pg_catalog."char"[], '{}'::"char"[];
CREATE TABLE char_types (qualified pg_catalog."char", unqualified "char", standard char);
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ it('misc-operator-names', async () => {
await fixtures.runFixtureTests([
"misc/operator-names-1.sql",
"misc/operator-names-2.sql",
"misc/operator-names-3.sql"
"misc/operator-names-3.sql",
"misc/operator-names-4.sql",
"misc/operator-names-5.sql",
"misc/operator-names-6.sql"
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ it('misc-typecast', async () => {
"misc/typecast-14.sql",
"misc/typecast-15.sql",
"misc/typecast-16.sql",
"misc/typecast-17.sql"
"misc/typecast-17.sql",
"misc/typecast-18.sql",
"misc/typecast-19.sql",
"misc/typecast-20.sql",
"misc/typecast-21.sql"
]);
});
7 changes: 7 additions & 0 deletions packages/deparser/__tests__/misc/pg-catalog.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { expectParseDeparse } from '../../test-utils';

it('preserves the qualified internal "char" type in casts and column definitions', async () => {
const cast = await expectParseDeparse(`SELECT 'x'::pg_catalog."char"`);
const table = await expectParseDeparse('CREATE TABLE t (c pg_catalog."char")');
expect(cast).toContain('pg_catalog."char"');
expect(table).toContain('pg_catalog."char"');
});

it('should format pg_catalog.char with pretty option enabled', async () => {
const sql = `
CREATE TABLE dashboard_jobs.jobs (
Expand Down
34 changes: 9 additions & 25 deletions packages/deparser/src/deparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1984,16 +1984,10 @@ export class Deparser implements DeparserVisitor {
if (names.length === 1) {
const typeName = names[0];

if (typeName === 'char') {
if (context.parentNodeTypes.includes('TypeCast') && args === '1') {
output.push('"char"');
} else {
output.push(mods('"char"', args));
}
return output.join(' ');
}

let result = mods(QuoteUtils.quoteIdentifierTypeName(typeName), args);
const quotedName = typeName === 'char'
? QuoteUtils.quoteIdentifier(typeName)
: QuoteUtils.quoteIdentifierTypeName(typeName);
let result = mods(quotedName, args);

if (node.arrayBounds && node.arrayBounds.length > 0) {
result += formatArrayBounds(node.arrayBounds);
Expand All @@ -2006,15 +2000,12 @@ export class Deparser implements DeparserVisitor {
if (names.length === 2) {
const [catalog, type] = names;

if (catalog === 'pg_catalog' && type === 'char') {
output.push(mods('pg_catalog.char', args));
return output.join(' ');
}

if (catalog === 'pg_catalog') {
let typeName = `${catalog}.${type}`;

if (type === 'bpchar' && args) {
if (type === 'char') {
typeName = `${catalog}.${QuoteUtils.quoteIdentifier(type)}`;
} else if (type === 'bpchar' && args) {
typeName = 'char';
} else if (type === 'varchar') {
typeName = 'varchar';
Expand Down Expand Up @@ -3456,15 +3447,8 @@ export class Deparser implements DeparserVisitor {
}

if (node.sortby_dir === 'SORTBY_USING' && node.useOp) {
output.push('USING');
const useOp = ListUtils.unwrapList(node.useOp);
output.push(useOp.map(op => {
if (op.String && op.String.sval) {
return op.String.sval;
}
return this.visit(op, context);
}).join('.'));
}else if (node.sortby_dir === 'SORTBY_ASC') {
output.push('USING', this.deparseOperatorName(ListUtils.unwrapList(node.useOp), context));
} else if (node.sortby_dir === 'SORTBY_ASC') {
output.push('ASC');
} else if (node.sortby_dir === 'SORTBY_DESC') {
output.push('DESC');
Expand Down
Loading