package compiler import ( "encoding/json" "testing" pgquery "github.com/wasilibs/go-pgquery" "gotest.tools/v3/assert" ) func TestFormatSQLLayout(t *testing.T) { for _, tc := range []struct{ name, sql, want string }{ { name: "select joins and conditions", sql: "select b.id,b.title,a.name from books b left join authors a on a.id=b.author_id where b.id>1 and b.title<>'' order by b.id desc limit 10", want: ` SELECT b.id, b.title, a.name FROM books b LEFT JOIN authors a ON a.id = b.author_id WHERE b.id > 1 AND b.title <> '' ORDER BY b.id DESC LIMIT 10 `, }, { name: "nested queries and CTEs", sql: "with recent as (select id,title from books where id>10), chosen as (select id from recent) select r.id from recent r where exists(select 1 from chosen c where c.id=r.id)", want: ` WITH recent AS ( SELECT id, title FROM books WHERE id > 10 ), chosen AS ( SELECT id FROM recent ) SELECT r.id FROM recent r WHERE EXISTS ( SELECT 1 FROM chosen c WHERE c.id = r.id ) `, }, { name: "update", sql: "update books set title='Dune',author_id=2 where id=1 returning id,title", want: ` UPDATE books SET title = 'Dune', author_id = 2 WHERE id = 1 RETURNING id, title `, }, { name: "insert", sql: "insert into books(title,author_id) values('Dune',2),('Children',2) returning id", want: ` INSERT INTO books (title, author_id) VALUES ('Dune', 2), ('Children', 2) RETURNING id `, }, } { t.Run(tc.name, func(t *testing.T) { source := "package library\nquery Q() {" + tc.sql + "}\n" formatted, err := FormatSource("sql.stitchql", source) assert.NilError(t, err) file, err := parseSource("sql.stitchql", string(formatted)) assert.NilError(t, err) assert.Equal(t, file.queries[0].sql, "\n"+tc.want) }) } } func TestFormatSQLSemantics(t *testing.T) { for _, sql := range []string{ "select b.id,b.title from books b where id between 1 and 5 and title<>''", "select 'first'\n'second' /* continuation */ as title from books", "select 1- -2, - -3, 1e-2, (array[1,2])[1:2], b.id::int from books b", "select x + (y * 2), -(x+1) from books", "select count(*) filter (where id>2), row_number() over(partition by author_id order by id) from books", "select case when id>1 and id<5 then 'yes' else 'no' end as label from books", "select /* projection */ id, -- first\n title from books -- table\n where id>1", "select 'a\n b', E'escaped \\' quote', $tag$\n { untouched }\n$tag$ from books", "with recursive tree as(select id from books union all select b.id from books b join tree t on b.id=t.id+1) select id from tree", "update books set title='Dune', author_id=2 where id=1 returning id,title", "insert into books(id,title) values(1,'Dune') on conflict(id) do update set title=excluded.title returning id", "delete from books where id=1 or id=2 returning id", "create table books(id bigint primary key,title text default 'untitled',author_id bigint references authors(id)); create index by_title on books(title);", "create type status as enum('draft','published'); create materialized view totals as select author_id,count(*) from books group by author_id with no data;", } { t.Run(sql, func(t *testing.T) { source := "package library\nschema {" + sql + "}\n" formatted, err := FormatSource("sql.stitchql", source) assert.NilError(t, err) file, err := parseSource("sql.stitchql", string(formatted)) assert.NilError(t, err) assert.DeepEqual(t, sqlTree(t, sql), sqlTree(t, file.schemas[0].sql)) again, err := FormatSource("sql.stitchql", string(formatted)) assert.NilError(t, err) assert.Equal(t, string(again), string(formatted)) }) } } // Locations and statement lengths are the only AST fields formatting may change. func sqlTree(t *testing.T, sql string) any { t.Helper() text, err := pgquery.ParseToJSON(sql) assert.NilError(t, err) var tree any assert.NilError(t, json.Unmarshal([]byte(text), &tree)) var strip func(any) strip = func(node any) { switch node := node.(type) { case map[string]any: delete(node, "location") delete(node, "stmt_location") delete(node, "stmt_len") for _, child := range node { strip(child) } case []any: for _, child := range node { strip(child) } } } strip(tree) return tree }