Skip to content

Commit

Permalink
Merge pull request #1654 from taozhi8833998/fix-join-mssql
Browse files Browse the repository at this point in the history
fix: inner join in mssql
  • Loading branch information
taozhi8833998 authored Nov 9, 2023
2 parents 0dfb1c1 + b91a8b8 commit bdaa0d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pegjs/transactsql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,8 @@ table_base

join_op
= a:(KW_LEFT / KW_RIGHT / KW_FULL) __ s:KW_OUTER? __ KW_JOIN { return [a[0].toUpperCase(), s && s[0], 'JOIN'].filter(v => v).join(' '); }
/ a:(KW_INNER / KW_CROSS) __ KW_JOIN { return `${a[0].toUpperCase()} JOIN` }
/ KW_CROSS __ KW_JOIN { return 'CROSS JOIN' }
/ a:(KW_INNER)? __ KW_JOIN { return a ? 'INNER JOIN' : 'JOIN' }

table_name
= server:ident __ DOT __ db:ident __ DOT __ schema:ident __ DOT __ table:ident {
Expand Down
27 changes: 27 additions & 0 deletions test/transactsql.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,33 @@ describe('transactsql', () => {
expect(parser.sqlify(ast, tsqlOpt)).to.equal("SELECT [trpriv_seq], [trpriv_titulo], [trpriv_id], [trprivc_data] FROM [termos_privacidade] LEFT JOIN [termos_privacidade_versoes] ON ([trprivv_trpriv_id] = [trpriv_id] AND [trprivv_unidg_id] IS NULL AND [trprivv_inicio] <= '2022-08-16T15:00:04.832Z' AND ([trprivv_fim] >= '2022-08-16T15:00:04.832Z' OR [trprivv_fim] IS NULL)) LEFT JOIN [termos_privacidade_consentimentos] ON ([trprivc_trprivv_id] = [trprivv_id] AND [trpriv_individual] = 0 AND [trprivc_pes_id] = 'null') WHERE 1 = 1 AND [trprivv_id] IS NOT NULL AND 1 = 2 ORDER BY 1 ASC, 2 ASC")
})

it('should support inner join without inner', () => {
const sql = `WITH t1 AS
(SELECT date_sold,
amount_sold AS cake_sold
FROM desserts
WHERE product = 'Cake' ),
t2 AS
(SELECT date_sold,
isnull(amount_sold, 0) AS pie_sold
FROM desserts
WHERE product = 'Pie' ),
t3 AS
(SELECT t1.date_sold,
t1.cake_sold,
t2.pie_sold
FROM t1
JOIN t2 ON t1.date_sold = t2.date_sold)
SELECT date_sold,
ABS(CAST(cake_sold AS BIGINT) - CAST(pie_sold AS BIGINT)) AS difference,
CASE
WHEN cake_sold > pie_sold THEN 'Cake'
ELSE 'Pie'
END AS sold_more
FROM t3`
expect(getParsedSql(sql)).to.equal("WITH [t1] AS (SELECT [date_sold], [amount_sold] AS [cake_sold] FROM [desserts] WHERE [product] = 'Cake'), [t2] AS (SELECT [date_sold], isnull([amount_sold], 0) AS [pie_sold] FROM [desserts] WHERE [product] = 'Pie'), [t3] AS (SELECT [t1].[date_sold], [t1].[cake_sold], [t2].[pie_sold] FROM [t1] JOIN [t2] ON [t1].[date_sold] = [t2].[date_sold]) SELECT [date_sold], ABS(CAST([cake_sold] AS BIGINT) - CAST([pie_sold] AS BIGINT)) AS [difference], CASE WHEN [cake_sold] > [pie_sold] THEN 'Cake' ELSE 'Pie' END AS [sold_more] FROM [t3]")
})

it('should support table schema', () => {
let sql = `INSERT INTO source.dbo.movie (genre_id, title, release_date)
VALUES (@param1, @param2, @param3), (@param1, @param2, @param3);`
Expand Down

0 comments on commit bdaa0d9

Please sign in to comment.