diff --git a/compiler/semantic/expr.go b/compiler/semantic/expr.go index 190b21014..d98539f10 100644 --- a/compiler/semantic/expr.go +++ b/compiler/semantic/expr.go @@ -309,18 +309,19 @@ func (t *translator) expr(e ast.Expr) sem.Expr { } // XXX type checker should remove this check when it finds it redundant is := sem.NewCall(e, "is", []sem.Expr{expr, &sem.LiteralExpr{Node: e.Expr, Value: ""}}) + indexBase := t.scope.indexBase() slice := &sem.SliceExpr{ Node: e, Expr: expr, From: t.exprNullable(e.From), - Base1: true, + Base1: indexBase == 1, } if e.For != nil { to := t.expr(e.For) if slice.From != nil { slice.To = sem.NewBinaryExpr(e, "+", slice.From, to) } else { - slice.To = sem.NewBinaryExpr(e, "+", to, &sem.LiteralExpr{Node: e, Value: "1"}) + slice.To = sem.NewBinaryExpr(e, "+", to, &sem.LiteralExpr{Node: e, Value: sup.FormatValue(super.NewInt64(int64(indexBase)))}) } } serr := sem.NewStructuredError(e, "SUBSTRING: string value required", expr) diff --git a/compiler/ztests/pragma-index-base-substring.yaml b/compiler/ztests/pragma-index-base-substring.yaml new file mode 100644 index 000000000..db2e6397b --- /dev/null +++ b/compiler/ztests/pragma-index-base-substring.yaml @@ -0,0 +1,8 @@ +spq: | + pragma index_base = 1 + values substring("foobar" FROM 4) + +vector: true + +output: | + "bar" diff --git a/compiler/ztests/sql/substring.yaml b/compiler/ztests/sql/substring.yaml index 75da6da22..3b8488123 100644 --- a/compiler/ztests/sql/substring.yaml +++ b/compiler/ztests/sql/substring.yaml @@ -1,4 +1,4 @@ -spq: values SUBSTRING(this FROM 4 FOR 2), SUBSTRING(this FROM 4), SUBSTRING(this FOR 3) +spq: values SUBSTRING(this FROM 3 FOR 2), SUBSTRING(this FROM 3), SUBSTRING(this FOR 3) vector: true