MCPcopy Create free account
hub / github.com/rilldata/rill / TestAST_RewriteFunctionTableRefs

Function TestAST_RewriteFunctionTableRefs

runtime/pkg/duckdbsql/ast_test.go:306–408  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

304}
305
306func TestAST_RewriteFunctionTableRefs(t *testing.T) {
307 sqlVariations := []struct {
308 title string
309 sql string
310 expectedSql string
311 }{
312 {
313 "no replace",
314 `select * from AdBid a join AdImp i on a.id=i.id where a='1' group by b limit 2`,
315 `SELECT * FROM AdBid AS a INNER JOIN AdImp AS i ON ((a.id = i.id)) WHERE (a = '1') GROUP BY b LIMIT 2`,
316 },
317 {
318 "simple replace",
319 `select * from read_csv( 'AdBids.csv', delim='|', columns={'timestamp':'TIMESTAMP'})`,
320 `SELECT * FROM AdBids`,
321 },
322 {
323 "replace with join and alias",
324 `
325select * from
326 read_csv( 'AdBids.csv', delim='|', columns={'timestamp':'TIMESTAMP'}) as b join
327 AdImpressions i on b.id=i.id
328`,
329 `SELECT * FROM AdBids AS b INNER JOIN AdImpressions AS i ON ((b.id = i.id))`,
330 },
331 {
332 "join with sub query",
333 `
334select * from
335 read_csv( 'AdBids.csv', delim='|', columns={'timestamp':'TIMESTAMP'}) a join
336 (select * from read_csv( 'AdImpressions.csv', delim='|', columns={'timestamp':'TIMESTAMP'}) i1 where i1.city='Bengaluru') i on a.id=i.id
337where a='1' group by b limit 2`,
338 `SELECT * FROM AdBids AS a INNER JOIN (SELECT * FROM AdImpressions AS i1 WHERE (i1.city = 'Bengaluru')) AS i ON ((a.id = i.id)) WHERE (a = '1') GROUP BY b LIMIT 2`,
339 },
340 {
341 "replace with CTEs",
342 `
343with
344 tbl2 as (select col1 from read_csv( 'AdBids.csv', delim='|', columns={'timestamp':'TIMESTAMP'})),
345 tbl3 as (select col1 from read_csv( 'AdImpressions.csv', delim='|', columns={'timestamp':'TIMESTAMP'}))
346select col1 from tbl2 join tbl3 on tbl2.id = tbl3.id
347`,
348 `WITH tbl2 AS (SELECT col1 FROM AdBids), tbl3 AS (SELECT col1 FROM AdImpressions)SELECT col1 FROM tbl2 INNER JOIN tbl3 ON ((tbl2.id = tbl3.id))`,
349 },
350 {
351 "replace with CTEs and unions",
352 `
353with
354 tbl2 as (select col1 from read_csv( 'AdBids_May.csv', delim='|', columns={'timestamp':'TIMESTAMP'})),
355 tbl3 as (select col1 from read_csv( 'AdBids_June.csv', delim='|', columns={'timestamp':'TIMESTAMP'}))
356select col1 from tbl2 union all select col1 from tbl3 union all select col1 from read_csv( 'AdBids_July.csv', delim='|', columns={'timestamp':'TIMESTAMP'})
357`,
358 `WITH tbl2 AS (SELECT col1 FROM AdBids_May), tbl3 AS (SELECT col1 FROM AdBids_June)(SELECT col1 FROM tbl2) UNION ALL ((SELECT col1 FROM tbl3) UNION ALL (SELECT col1 FROM AdBids_July))`,
359 },
360 {
361 "replace with pivot statement",
362 `
363pivot

Callers

nothing calls this directly

Calls 5

StemFunction · 0.92
RewriteTableRefsMethod · 0.80
ParseFunction · 0.70
FormatMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected