Skip to content

Commit 10aa45f

Browse files
committed
Make substring obey pragma index_base
1 parent d64b887 commit 10aa45f

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

compiler/semantic/expr.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,19 @@ func (t *translator) expr(e ast.Expr) sem.Expr {
309309
}
310310
// XXX type checker should remove this check when it finds it redundant
311311
is := sem.NewCall(e, "is", []sem.Expr{expr, &sem.LiteralExpr{Node: e.Expr, Value: "<string>"}})
312+
indexBase := t.scope.indexBase()
312313
slice := &sem.SliceExpr{
313314
Node: e,
314315
Expr: expr,
315316
From: t.exprNullable(e.From),
316-
Base1: true,
317+
Base1: indexBase == 1,
317318
}
318319
if e.For != nil {
319320
to := t.expr(e.For)
320321
if slice.From != nil {
321322
slice.To = sem.NewBinaryExpr(e, "+", slice.From, to)
322323
} else {
323-
slice.To = sem.NewBinaryExpr(e, "+", to, &sem.LiteralExpr{Node: e, Value: "1"})
324+
slice.To = sem.NewBinaryExpr(e, "+", to, &sem.LiteralExpr{Node: e, Value: sup.FormatValue(super.NewInt64(int64(indexBase)))})
324325
}
325326
}
326327
serr := sem.NewStructuredError(e, "SUBSTRING: string value required", expr)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spq: |
2+
pragma index_base = 1
3+
values substring("foobar" FROM 4)
4+
5+
vector: true
6+
7+
output: |
8+
"bar"

compiler/ztests/sql/substring.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
spq: values SUBSTRING(this FROM 4 FOR 2), SUBSTRING(this FROM 4), SUBSTRING(this FOR 3)
1+
spq: values SUBSTRING(this FROM 3 FOR 2), SUBSTRING(this FROM 3), SUBSTRING(this FOR 3)
22

33
vector: true
44

0 commit comments

Comments
 (0)