Skip to content

Commit fe55ab3

Browse files
EliSauderCopilot
andcommitted
fix(mssql): keep the placeholders of TABLESAMPLE
TABLESAMPLE (@n PERCENT) and REPEATABLE (@seed) dropped their placeholders. They are now analyzed with the query's WHERE clause, as FOR SYSTEM_TIME's are: a percentage as a float, a count of rows and a seed as a bigint. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 043680e commit fe55ab3

5 files changed

Lines changed: 96 additions & 0 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- name: Sampled :many
2+
SELECT id FROM t TABLESAMPLE (@pct PERCENT) WHERE id > @min;
3+
4+
-- name: SampledRows :many
5+
SELECT id FROM t TABLESAMPLE SYSTEM (@n ROWS) REPEATABLE (@seed);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE t (id INT NOT NULL, name NVARCHAR(50) NULL, at DATETIME2 NOT NULL);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[
2+
{
3+
"name": "Sampled",
4+
"cmd": ":many",
5+
"columns": [
6+
{
7+
"name": "id",
8+
"type": {
9+
"name": "int"
10+
},
11+
"table": "t"
12+
}
13+
],
14+
"params": [
15+
{
16+
"number": 1,
17+
"column": {
18+
"name": "pct",
19+
"type": {
20+
"name": "float"
21+
}
22+
}
23+
},
24+
{
25+
"number": 2,
26+
"column": {
27+
"name": "min",
28+
"type": {
29+
"name": "int"
30+
},
31+
"table": "t"
32+
}
33+
}
34+
]
35+
},
36+
{
37+
"name": "SampledRows",
38+
"cmd": ":many",
39+
"columns": [
40+
{
41+
"name": "id",
42+
"type": {
43+
"name": "int"
44+
},
45+
"table": "t"
46+
}
47+
],
48+
"params": [
49+
{
50+
"number": 1,
51+
"column": {
52+
"name": "n",
53+
"type": {
54+
"name": "bigint"
55+
}
56+
}
57+
},
58+
{
59+
"number": 2,
60+
"column": {
61+
"name": "seed",
62+
"type": {
63+
"name": "bigint"
64+
}
65+
}
66+
}
67+
]
68+
}
69+
]

‎internal/engine/mssql/convert.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,22 @@ func (c *cc) convertTableReference(ref tsql.TableReference) ast.Node {
406406
}
407407
}
408408
}
409+
// TABLESAMPLE takes a percentage as a float and a count of rows
410+
// or a REPEATABLE seed as a bigint.
411+
if ts := t.TableSampleClause; ts != nil {
412+
size := "float"
413+
if ts.TableSampleClauseOption == "Rows" {
414+
size = "bigint"
415+
}
416+
for _, e := range []struct {
417+
expr tsql.ScalarExpression
418+
typ string
419+
}{{ts.SampleNumber, size}, {ts.RepeatSeed, "bigint"}} {
420+
if e.expr != nil {
421+
c.fromExprs = append(c.fromExprs, &ast.TypeCast{Arg: c.convertScalarExpression(e.expr), TypeName: &ast.TypeName{Name: e.typ}})
422+
}
423+
}
424+
}
409425
return rv
410426
case *tsql.QueryDerivedTable:
411427
sub := &ast.RangeSubselect{

0 commit comments

Comments
 (0)