Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DBMON-3185] Create sqlserver test suite #31

Merged
merged 5 commits into from
Nov 21, 2023
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
1 change: 1 addition & 0 deletions dbms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestQueriesPerDBMS(t *testing.T) {
dbmsTypes := []DBMSType{
DBMSPostgres,
DBMSOracle,
DBMSSQLServer,
}

for _, dbms := range dbmsTypes {
Expand Down
2 changes: 1 addition & 1 deletion obfuscator.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (o *Obfuscator) ObfuscateTokenValue(token Token, lexerOpts ...lexerOption)
} else {
return token.Value
}
case IDENT:
case IDENT, QUOTED_IDENT:
if o.config.ReplaceBoolean && isBoolean(token.Value) {
return StringPlaceholder
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "WITH ComplexCTE AS (SELECT t1.id, t2.amount, ROW_NUMBER() OVER(PARTITION BY t1.customer_id ORDER BY t2.amount DESC) AS rn FROM (SELECT id, customer_id, status FROM orders WHERE YEAR(order_date) = YEAR(GETDATE()) AND status NOT IN ('Cancelled', 'Returned')) t1 INNER JOIN (SELECT order_id, SUM(amount) AS amount FROM order_details GROUP BY order_id) t2 ON t1.id = t2.order_id WHERE t2.amount > 500), SecondCTE AS (SELECT c1.*, c2.name, c2.region FROM ComplexCTE c1 INNER JOIN customers c2 ON c1.customer_id = c2.id WHERE c2.region IN ('East', 'West') AND c1.rn < 5) SELECT s.id, s.name, s.amount, p.product_name, CASE WHEN s.amount > 1000 THEN 'High' ELSE 'Low' END AS ValueCategory FROM SecondCTE s LEFT JOIN (SELECT DISTINCT p1.order_id, p2.product_name FROM order_products p1 INNER JOIN products p2 ON p1.product_id = p2.id) p ON s.id = p.order_id WHERE s.region = 'East' AND s.status LIKE '%Active%' ORDER BY s.amount DESC, s.name;",
"outputs": [
{
"expected": "WITH ComplexCTE AS ( SELECT t?.id, t?.amount, ROW_NUMBER ( ) OVER ( PARTITION BY t?.customer_id ORDER BY t?.amount DESC ) FROM ( SELECT id, customer_id, status FROM orders WHERE YEAR ( order_date ) = YEAR ( GETDATE ( ) ) AND status NOT IN ( ? ) ) t? INNER JOIN ( SELECT order_id, SUM ( amount ) FROM order_details GROUP BY order_id ) t? ON t?.id = t?.order_id WHERE t?.amount > ? ), SecondCTE AS ( SELECT c?. *, c?.name, c?.region FROM ComplexCTE c? INNER JOIN customers c? ON c?.customer_id = c?.id WHERE c?.region IN ( ? ) AND c?.rn < ? ) SELECT s.id, s.name, s.amount, p.product_name, CASE WHEN s.amount > ? THEN ? ELSE ? END FROM SecondCTE s LEFT JOIN ( SELECT DISTINCT p?.order_id, p?.product_name FROM order_products p? INNER JOIN products p? ON p?.product_id = p?.id ) p ON s.id = p.order_id WHERE s.region = ? AND s.status LIKE ? ORDER BY s.amount DESC, s.name",
"statement_metadata": {
"size": 79,
"tables": ["orders", "order_details", "ComplexCTE", "customers", "SecondCTE", "order_products", "products"],
"commands": ["SELECT", "JOIN"],
"comments": [],
"procedures": []
}
}
]
}

17 changes: 17 additions & 0 deletions testdata/mssql/complex/indexed-views.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"input": "CREATE VIEW dbo.OrderSummary WITH SCHEMABINDING AS SELECT customer_id, COUNT_BIG(*) AS TotalOrders, SUM(amount) AS TotalAmount FROM dbo.orders GROUP BY customer_id; CREATE UNIQUE CLUSTERED INDEX IDX_V1 ON dbo.OrderSummary(customer_id);",
"outputs": [
{
"expected": "CREATE VIEW dbo.OrderSummary WITH SCHEMABINDING AS SELECT customer_id, COUNT_BIG ( * ), SUM ( amount ) FROM dbo.orders GROUP BY customer_id; CREATE UNIQUE CLUSTERED INDEX IDX_V? ON dbo.OrderSummary ( customer_id )",
"statement_metadata": {
"size": 22,
"tables": ["dbo.orders"],
"commands": ["CREATE", "SELECT"],
"comments": [],
"procedures": [],
"views": ["dbo.OrderSummary"]
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/complex/partitioned-tables-indexes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "CREATE PARTITION FUNCTION myRangePF1 (INT) AS RANGE LEFT FOR VALUES (1, 100, 1000); CREATE PARTITION SCHEME myScheme AS PARTITION myRangePF1 TO ([PRIMARY], [SECONDARY], [TERTIARY]); CREATE TABLE partitionedTable (id INT) ON myScheme(id);",
"outputs": [
{
"expected": "CREATE PARTITION FUNCTION myRangePF? ( INT ) LEFT FOR VALUES ( ? ); CREATE PARTITION SCHEME myScheme myRangePF? TO ( PRIMARY, SECONDARY, TERTIARY ); CREATE TABLE partitionedTable ( id INT ) ON myScheme ( id )",
"statement_metadata": {
"size": 22,
"tables": ["partitionedTable"],
"commands": ["CREATE"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/complex/super-complex-poorly-written-sql.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "SELECT a.*, b.totalAmount, CASE WHEN c.id IS NOT NULL THEN d.description ELSE 'N/A' END AS description FROM (SELECT id, name, status, customer_id FROM orders WHERE order_date > DATEADD(month, -6, GETDATE()) AND status IN ('Pending', 'Completed') AND customer_id IN (SELECT customer_id FROM customers WHERE region IN ('East', 'West') AND last_order_date > DATEADD(year, -1, GETDATE())) ORDER BY name DESC) a INNER JOIN (SELECT order_id, SUM(amount) AS totalAmount FROM order_details GROUP BY order_id) b ON a.id = b.order_id LEFT JOIN audit_log c ON a.id = c.order_id LEFT JOIN (SELECT DISTINCT status, description FROM status_descriptions) d ON a.status = d.status WHERE a.name LIKE '%test%' AND (b.totalAmount > 1000 OR b.totalAmount IS NULL) ORDER BY a.order_date DESC, a.name;",
"outputs": [
{
"expected": "SELECT a. *, b.totalAmount, CASE WHEN c.id IS NOT ? THEN d.description ELSE ? END FROM ( SELECT id, name, status, customer_id FROM orders WHERE order_date > DATEADD ( month, ?, GETDATE ( ) ) AND status IN ( ? ) AND customer_id IN ( SELECT customer_id FROM customers WHERE region IN ( ? ) AND last_order_date > DATEADD ( year, ?, GETDATE ( ) ) ) ORDER BY name DESC ) a INNER JOIN ( SELECT order_id, SUM ( amount ) FROM order_details GROUP BY order_id ) b ON a.id = b.order_id LEFT JOIN audit_log c ON a.id = c.order_id LEFT JOIN ( SELECT DISTINCT status, description FROM status_descriptions ) d ON a.status = d.status WHERE a.name LIKE ? AND ( b.totalAmount > ? OR b.totalAmount IS ? ) ORDER BY a.order_date DESC, a.name",
"statement_metadata": {
"size": 66,
"tables": ["orders", "customers", "order_details", "audit_log", "status_descriptions"],
"commands": ["SELECT", "JOIN"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/delete/conditional-delete-case.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "DELETE FROM orders WHERE status = CASE WHEN order_date < GETDATE() - 90 THEN 'Expired' ELSE 'Active' END;",
"outputs": [
{
"expected": "DELETE FROM orders WHERE status = CASE WHEN order_date < GETDATE ( ) - ? THEN ? ELSE ? END",
"statement_metadata": {
"size": 12,
"tables": ["orders"],
"commands": ["DELETE"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/delete/delete-basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "DELETE FROM orders WHERE status = 'Cancelled';",
"outputs": [
{
"expected": "DELETE FROM orders WHERE status = ?",
"statement_metadata": {
"size": 12,
"tables": ["orders"],
"commands": ["DELETE"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/delete/delete-cascade.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "DELETE FROM customers WHERE region = 'North'; -- Assuming CASCADE DELETE is set up on the foreign key in the orders table",
"outputs": [
{
"expected": "DELETE FROM customers WHERE region = ?",
"statement_metadata": {
"size": 90,
"tables": ["customers"],
"commands": ["DELETE"],
"comments": ["-- Assuming CASCADE DELETE is set up on the foreign key in the orders table"],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/delete/delete-rowlock-hint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "DELETE FROM orders WITH (ROWLOCK) WHERE status = 'Pending';",
"outputs": [
{
"expected": "DELETE FROM orders WITH ( ROWLOCK ) WHERE status = ?",
"statement_metadata": {
"size": 12,
"tables": ["orders"],
"commands": ["DELETE"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/delete/delete-using-subquery.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "DELETE FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE region = 'West');",
"outputs": [
{
"expected": "DELETE FROM orders WHERE customer_id IN ( SELECT id FROM customers WHERE region = ? )",
"statement_metadata": {
"size": 27,
"tables": ["orders", "customers"],
"commands": ["DELETE", "SELECT"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/delete/delete-using-table-variable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "DECLARE @ExpiredOrders TABLE (id INT); INSERT INTO @ExpiredOrders (id) SELECT id FROM orders WHERE order_date < GETDATE() - 365; DELETE FROM orders WHERE id IN (SELECT id FROM @ExpiredOrders);",
"outputs": [
{
"expected": "DECLARE @ExpiredOrders TABLE ( id INT ); INSERT INTO @ExpiredOrders ( id ) SELECT id FROM orders WHERE order_date < GETDATE ( ) - ?; DELETE FROM orders WHERE id IN ( SELECT id FROM @ExpiredOrders )",
"statement_metadata": {
"size": 24,
"tables": ["orders"],
"commands": ["INSERT", "SELECT", "DELETE"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/delete/delete-with-cte.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "WITH OldOrders AS (SELECT id FROM orders WHERE order_date < '2022-01-01') DELETE FROM orders WHERE id IN (SELECT id FROM OldOrders);",
"outputs": [
{
"expected": "WITH OldOrders AS ( SELECT id FROM orders WHERE order_date < ? ) DELETE FROM orders WHERE id IN ( SELECT id FROM OldOrders )",
"statement_metadata": {
"size": 27,
"tables": ["orders", "OldOrders"],
"commands": ["SELECT", "DELETE"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/delete/delete-with-join.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "DELETE o FROM orders o INNER JOIN customers c ON o.customer_id = c.id WHERE c.region = 'East' AND o.status = 'Pending';",
"outputs": [
{
"expected": "DELETE o FROM orders o INNER JOIN customers c ON o.customer_id = c.id WHERE c.region = ? AND o.status = ?",
"statement_metadata": {
"size": 25,
"tables": ["orders", "customers"],
"commands": ["DELETE", "JOIN"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/delete/delete-with-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "DELETE FROM orders OUTPUT DELETED.* WHERE status = 'Shipped';",
"outputs": [
{
"expected": "DELETE FROM orders OUTPUT DELETED. * WHERE status = ?",
"statement_metadata": {
"size": 12,
"tables": ["orders"],
"commands": ["DELETE"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/delete/delete-with-top.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "DELETE TOP (10) FROM orders WHERE status = 'Pending';",
"outputs": [
{
"expected": "DELETE TOP ( ? ) FROM orders WHERE status = ?",
"statement_metadata": {
"size": 12,
"tables": ["orders"],
"commands": ["DELETE"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/insert/insert-basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "INSERT INTO orders (customer_id, order_date, status) VALUES (1, GETDATE(), 'Pending');",
"outputs": [
{
"expected": "INSERT INTO orders ( customer_id, order_date, status ) VALUES ( ?, GETDATE ( ), ? )",
"statement_metadata": {
"size": 12,
"tables": ["orders"],
"commands": ["INSERT"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/insert/insert-default-values.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "INSERT INTO orders DEFAULT VALUES;",
"outputs": [
{
"expected": "INSERT INTO orders DEFAULT VALUES",
"statement_metadata": {
"size": 12,
"tables": ["orders"],
"commands": ["INSERT"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/insert/insert-identity-insert.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "SET IDENTITY_INSERT orders ON; INSERT INTO orders (id, customer_id, order_date, status) VALUES (100, 3, GETDATE(), 'Pending'); SET IDENTITY_INSERT orders OFF;",
"outputs": [
{
"expected": "SET IDENTITY_INSERT orders ON; INSERT INTO orders ( id, customer_id, order_date, status ) VALUES ( ?, GETDATE ( ), ? ); SET IDENTITY_INSERT orders OFF",
"statement_metadata": {
"size": 12,
"tables": ["orders"],
"commands": ["INSERT"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/insert/insert-merge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "MERGE INTO orders AS target USING (SELECT customer_id, order_date, status FROM incoming_orders) AS source ON target.id = source.id WHEN NOT MATCHED THEN INSERT (customer_id, order_date, status) VALUES (source.customer_id, source.order_date, source.status);",
"outputs": [
{
"expected": "MERGE INTO orders USING ( SELECT customer_id, order_date, status FROM incoming_orders ) ON target.id = source.id WHEN NOT MATCHED THEN INSERT ( customer_id, order_date, status ) VALUES ( source.customer_id, source.order_date, source.status )",
"statement_metadata": {
"size": 38,
"tables": ["orders", "incoming_orders"],
"commands": ["MERGE", "SELECT", "INSERT"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/insert/insert-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "INSERT INTO orders (customer_id, order_date, status) OUTPUT INSERTED.id VALUES (3, GETDATE(), 'Processing');",
"outputs": [
{
"expected": "INSERT INTO orders ( customer_id, order_date, status ) OUTPUT INSERTED.id VALUES ( ?, GETDATE ( ), ? )",
"statement_metadata": {
"size": 12,
"tables": ["orders"],
"commands": ["INSERT"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/insert/insert-select-into.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "SELECT * INTO new_orders FROM orders WHERE status = 'Pending';",
"outputs": [
{
"expected": "SELECT * INTO new_orders FROM orders WHERE status = ?",
"statement_metadata": {
"size": 22,
"tables": ["new_orders", "orders"],
"commands": ["SELECT"],
"comments": [],
"procedures": []
}
}
]
}

16 changes: 16 additions & 0 deletions testdata/mssql/insert/insert-subquery-values.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"input": "INSERT INTO order_totals (order_id, total_amount) VALUES ((SELECT MAX(id) FROM orders), 500);",
"outputs": [
{
"expected": "INSERT INTO order_totals ( order_id, total_amount ) VALUES ( ( SELECT MAX ( id ) FROM orders ), ? )",
"statement_metadata": {
"size": 30,
"tables": ["order_totals", "orders"],
"commands": ["INSERT", "SELECT"],
"comments": [],
"procedures": []
}
}
]
}

Loading