Skip to content

Commit 13138f4

Browse files
EliSauderCopilot
andcommitted
feat(mssql): type LEFT, RIGHT and TRIM(chars FROM string)
LEFT and RIGHT, which the parser gives nodes of their own, were not converted, so their results went untyped, and TRIM(chars FROM string) took the type of the characters. LEFT and RIGHT are now called, taking their length as the int SQL Server does, and TRIM given the characters returns the string's type. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 34bc380 commit 13138f4

6 files changed

Lines changed: 90 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "analyze",
3+
"args": ["--dialect", "mssql", "--schema", "schema.sql", "query.sql"],
4+
"contexts": ["base"]
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: Trimmed :many
2+
SELECT LEFT(a, 3) AS lf, RIGHT(c, @n) AS rt, TRIM(N'x' FROM a) AS tr, TRIM(LEADING 'x' FROM c) AS tl FROM p;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE p (a NVARCHAR(10) NOT NULL, b NVARCHAR(20) NOT NULL, c VARCHAR(5) NOT NULL, d DECIMAL(10,2) NOT NULL, e DECIMAL(5,3) NOT NULL, i INT NOT NULL);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[
2+
{
3+
"name": "Trimmed",
4+
"cmd": ":many",
5+
"columns": [
6+
{
7+
"name": "lf",
8+
"type": {
9+
"name": "nvarchar",
10+
"nullable": true
11+
}
12+
},
13+
{
14+
"name": "rt",
15+
"type": {
16+
"name": "varchar",
17+
"nullable": true
18+
}
19+
},
20+
{
21+
"name": "tr",
22+
"type": {
23+
"name": "nvarchar",
24+
"args": [
25+
{
26+
"int": 10
27+
}
28+
]
29+
}
30+
},
31+
{
32+
"name": "tl",
33+
"type": {
34+
"name": "varchar",
35+
"args": [
36+
{
37+
"int": 5
38+
}
39+
]
40+
}
41+
}
42+
],
43+
"params": [
44+
{
45+
"number": 1,
46+
"column": {
47+
"name": "n",
48+
"type": {
49+
"name": "int"
50+
}
51+
}
52+
}
53+
]
54+
}
55+
]

‎internal/engine/mssql/convert.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,10 @@ func (c *cc) convertScalarExpression(expr tsql.ScalarExpression) ast.Node {
667667
},
668668
Location: c.loc(e),
669669
}
670+
case *tsql.LeftFunctionCall:
671+
return c.namedCall("left", e.Parameters, e)
672+
case *tsql.RightFunctionCall:
673+
return c.namedCall("right", e.Parameters, e)
670674
case *tsql.ParameterlessCall:
671675
return &ast.FuncCall{
672676
Funcname: &ast.List{
@@ -1333,6 +1337,20 @@ func (c *cc) convertAlterTableAddTableElementStatement(n *tsql.AlterTableAddTabl
13331337
return stmt
13341338
}
13351339

1340+
// namedCall is a call of name with args, for a function the parser gives a
1341+
// node of its own, as LEFT and RIGHT, which are keywords too.
1342+
func (c *cc) namedCall(name string, args []tsql.ScalarExpression, n tsql.Node) *ast.FuncCall {
1343+
call := &ast.FuncCall{
1344+
Funcname: &ast.List{Items: []ast.Node{&ast.String{Str: name}}},
1345+
Args: &ast.List{},
1346+
Location: c.loc(n),
1347+
}
1348+
for _, arg := range args {
1349+
call.Args.Items = append(call.Args.Items, c.convertScalarExpression(arg))
1350+
}
1351+
return call
1352+
}
1353+
13361354
func (c *cc) convertAlterTableDropTableElementStatement(n *tsql.AlterTableDropTableElementStatement) ast.Node {
13371355
if n.SchemaObjectName == nil {
13381356
return todo(n)

‎internal/engine/mssql/dialect/functions.jsonl‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@
110110
{"name": "substring", "args": [{"type": "nchar"}, {"type": "bigint"}, {"type": "bigint"}], "returns": "nvarchar", "nullable": true}
111111
{"name": "substring", "args": [{"type": "varbinary"}, {"type": "bigint"}, {"type": "bigint"}], "returns": "varbinary", "nullable": true}
112112
{"name": "substring", "args": [{"type": "binary"}, {"type": "bigint"}, {"type": "bigint"}], "returns": "varbinary", "nullable": true}
113-
{"name": "left", "args": [{"type": "varchar"}, {"type": "bigint"}], "returns": "varchar", "nullable": true}
114-
{"name": "left", "args": [{"type": "char"}, {"type": "bigint"}], "returns": "varchar", "nullable": true}
115-
{"name": "left", "args": [{"type": "nvarchar"}, {"type": "bigint"}], "returns": "nvarchar", "nullable": true}
116-
{"name": "left", "args": [{"type": "nchar"}, {"type": "bigint"}], "returns": "nvarchar", "nullable": true}
117-
{"name": "right", "args": [{"type": "varchar"}, {"type": "bigint"}], "returns": "varchar", "nullable": true}
118-
{"name": "right", "args": [{"type": "char"}, {"type": "bigint"}], "returns": "varchar", "nullable": true}
119-
{"name": "right", "args": [{"type": "nvarchar"}, {"type": "bigint"}], "returns": "nvarchar", "nullable": true}
120-
{"name": "right", "args": [{"type": "nchar"}, {"type": "bigint"}], "returns": "nvarchar", "nullable": true}
113+
{"name": "left", "args": [{"type": "varchar"}, {"type": "int"}], "returns": "varchar", "nullable": true}
114+
{"name": "left", "args": [{"type": "char"}, {"type": "int"}], "returns": "varchar", "nullable": true}
115+
{"name": "left", "args": [{"type": "nvarchar"}, {"type": "int"}], "returns": "nvarchar", "nullable": true}
116+
{"name": "left", "args": [{"type": "nchar"}, {"type": "int"}], "returns": "nvarchar", "nullable": true}
117+
{"name": "right", "args": [{"type": "varchar"}, {"type": "int"}], "returns": "varchar", "nullable": true}
118+
{"name": "right", "args": [{"type": "char"}, {"type": "int"}], "returns": "varchar", "nullable": true}
119+
{"name": "right", "args": [{"type": "nvarchar"}, {"type": "int"}], "returns": "nvarchar", "nullable": true}
120+
{"name": "right", "args": [{"type": "nchar"}, {"type": "int"}], "returns": "nvarchar", "nullable": true}
121121
{"name": "replace", "args": [{"type": "varchar"}, {"type": "any"}, {"type": "any"}], "returns": "varchar(8000)", "nullable": true}
122122
{"name": "replace", "args": [{"type": "char"}, {"type": "any"}, {"type": "any"}], "returns": "varchar(8000)", "nullable": true}
123123
{"name": "replace", "args": [{"type": "nvarchar"}, {"type": "any"}, {"type": "any"}], "returns": "nvarchar(4000)", "nullable": true}
@@ -382,3 +382,4 @@
382382
{"name": "regexp_instr", "args": [{"type": "any"}, {"type": "any"}, {"type": "any", "mode": "v"}], "returns": "int", "nullable": true}
383383
{"name": "regexp_count", "args": [{"type": "any"}, {"type": "any"}, {"type": "any", "mode": "v"}], "returns": "int", "nullable": true}
384384
{"name": "vector_distance", "args": [{"type": "varchar"}, {"type": "vector"}, {"type": "vector"}], "returns": "float", "nullable": true}
385+
{"name": "trim", "args": [{"type": "any"}, {"type": "any"}], "returns": "$2"}

0 commit comments

Comments
 (0)