Skip to content

Commit ce98a0d

Browse files
EliSauderCopilot
andcommitted
test(mssql): cover conversion and literal type analysis
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d1ad515 commit ce98a0d

10 files changed

Lines changed: 70 additions & 7 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"command": "analyze",
33
"args": ["--dialect", "mssql", "--schema", "schema.sql", "query.sql"],
4-
"contexts": ["base"]
4+
"contexts": ["base", "core"]
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
-- name: Literals :many
2-
SELECT N'hello' AS a1, N'' AS a2, 'hi' AS a3, a + N'xy' AS a4 FROM p WHERE a LIKE N'%x%' AND b = @b;
2+
SELECT N'hello' AS a1, N'' AS a2, 'hi' AS a3, a + N'xy' AS a4, N'😀' AS a5 FROM p WHERE a LIKE N'%x%' AND b = @b;

‎internal/endtoend/testdata/analyze_national_literals/mssql/stdout.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@
3636
"type": {
3737
"name": "nvarchar"
3838
}
39+
},
40+
{
41+
"name": "a5",
42+
"type": {
43+
"name": "nvarchar",
44+
"args": [
45+
{
46+
"int": 2
47+
}
48+
]
49+
}
3950
}
4051
],
4152
"params": [
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"command": "analyze",
33
"args": ["--dialect", "mssql", "--schema", "schema.sql", "query.sql"],
4-
"contexts": ["base"]
4+
"contexts": ["base", "core"]
55
}

‎internal/endtoend/testdata/analyze_try_conversions/mssql/query.sql‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
SELECT id,
33
TRY_CAST(raw AS INT) AS as_int,
44
TRY_CONVERT(DECIMAL(5,1), raw) AS as_decimal,
5+
TRY_CONVERT(DATETIME2(3), raw, 126) AS as_datetime,
56
TRY_CAST(note AS NVARCHAR(MAX)) AS as_text
67
FROM readings
78
WHERE TRY_CAST(@min AS INT) > 0;

‎internal/endtoend/testdata/analyze_try_conversions/mssql/stdout.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
3232
]
3333
}
3434
},
35+
{
36+
"name": "as_datetime",
37+
"type": {
38+
"name": "datetime2",
39+
"nullable": true,
40+
"args": [
41+
{
42+
"int": 3
43+
}
44+
]
45+
}
46+
},
3547
{
3648
"name": "as_text",
3749
"type": {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"command": "analyze",
33
"args": ["--dialect", "mssql", "--schema", "schema.sql", "query.sql"],
4-
"contexts": ["base"]
4+
"contexts": ["base", "core"]
55
}

‎internal/endtoend/testdata/analyze_types/mssql/query.sql‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ WHERE CAST(@a AS DECIMAL(5,2)) > 0
2222
-- name: Params :one
2323
SELECT id FROM things
2424
WHERE price = @price AND body = @body AND phone = @phone AND offset_at = @offset_at;
25+
26+
-- name: NumericLiterals :one
27+
SELECT 2147483648 AS over_int, 9223372036854775808 AS over_bigint;

‎internal/endtoend/testdata/analyze_types/mssql/stdout.json‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,5 +596,40 @@
596596
}
597597
}
598598
]
599+
},
600+
{
601+
"name": "NumericLiterals",
602+
"cmd": ":one",
603+
"columns": [
604+
{
605+
"name": "over_int",
606+
"type": {
607+
"name": "decimal",
608+
"args": [
609+
{
610+
"int": 10
611+
},
612+
{
613+
"int": 0
614+
}
615+
]
616+
}
617+
},
618+
{
619+
"name": "over_bigint",
620+
"type": {
621+
"name": "decimal",
622+
"args": [
623+
{
624+
"int": 19
625+
},
626+
{
627+
"int": 0
628+
}
629+
]
630+
}
631+
}
632+
],
633+
"params": []
599634
}
600635
]

‎internal/engine/mssql/convert.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -982,9 +982,10 @@ func (c *cc) convertScalarExpression(expr tsql.ScalarExpression) ast.Node {
982982
}
983983
}
984984

985-
// tryCast converts TRY_CAST and TRY_CONVERT, which yield NULL for a value
986-
// they cannot convert, so their result is nullable whatever the operand is.
987-
// A placeholder cast this way is typed by the cast as it is by CAST.
985+
// tryCast models the result type of TRY_CAST and TRY_CONVERT. The source SQL
986+
// is unchanged; a TRY_CONVERT style affects parsing, not the result type.
987+
// Failed conversions can yield NULL, so results from non-parameter operands
988+
// are nullable. A placeholder is typed by the cast as it is by CAST.
988989
func (c *cc) tryCast(arg tsql.ScalarExpression, dataType tsql.DataTypeReference, n tsql.Node) ast.Node {
989990
return &ast.TypeCast{
990991
Arg: c.convertScalarExpression(arg),

0 commit comments

Comments
 (0)