-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`SUBSTRING` can be constructed in several forms: SUBSTRING( value FROM start_position [ FOR string_length ] [ USING { CHARACTERS | OCTETS } ] ) `start_position` starts at 1 for the first character or byte. If `start_position` is out of bounds (either before the start or after the end) the returned value will be empty. If `string_length` is not provided, all characters or bytes until the end will be included. Otherwise, only `string_length` will be included. If `string_length` goes beyond the end of the string it will only be used until the end. If `CHARACTERS` is specified the `start_position` and `string_length` will count in characters (this works with multibyte characters) whereas `OCTETS` will strictly count in bytes. If `USING` is not provided, `CHARACTERS` will be used.
- Loading branch information
1 parent
25d4b4c
commit f84fb96
Showing
9 changed files
with
496 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
EXPLAIN VALUES SUBSTRING('hello' FROM 3); | ||
-- EXPLAIN: VALUES (COL1 CHARACTER VARYING) = ROW(SUBSTRING('hello' FROM 3 USING CHARACTERS)) | ||
|
||
EXPLAIN VALUES SUBSTRING('hello world' FROM 3 FOR 5); | ||
-- EXPLAIN: VALUES (COL1 CHARACTER VARYING) = ROW(SUBSTRING('hello world' FROM 3 FOR 5 USING CHARACTERS)) | ||
|
||
EXPLAIN VALUES SUBSTRING('hello world' FROM 3 USING CHARACTERS); | ||
-- EXPLAIN: VALUES (COL1 CHARACTER VARYING) = ROW(SUBSTRING('hello world' FROM 3 USING CHARACTERS)) | ||
|
||
EXPLAIN VALUES SUBSTRING('hello world' FROM 3 FOR 5 USING CHARACTERS); | ||
-- EXPLAIN: VALUES (COL1 CHARACTER VARYING) = ROW(SUBSTRING('hello world' FROM 3 FOR 5 USING CHARACTERS)) | ||
|
||
EXPLAIN VALUES SUBSTRING('hello world' FROM 3 FOR 2 + 3 USING OCTETS); | ||
-- EXPLAIN: VALUES (COL1 CHARACTER VARYING) = ROW(SUBSTRING('hello world' FROM 3 FOR 2 + 3 USING OCTETS)) | ||
|
||
VALUES SUBSTRING('hello' FROM 0); | ||
-- COL1: | ||
|
||
VALUES SUBSTRING('hello' FROM 2); | ||
-- COL1: ello | ||
|
||
VALUES SUBSTRING('hello' FROM 4); | ||
-- COL1: lo | ||
|
||
VALUES SUBSTRING('hello' FROM 5); | ||
-- COL1: o | ||
|
||
VALUES SUBSTRING('hello' FROM 6); | ||
-- COL1: | ||
|
||
VALUES SUBSTRING('hello' FROM 20); | ||
-- COL1: | ||
|
||
VALUES SUBSTRING('hello' FROM -1); | ||
-- COL1: | ||
|
||
VALUES SUBSTRING('hello world' FROM 3 FOR 5); | ||
-- COL1: llo w | ||
|
||
VALUES SUBSTRING('hello world' FROM 3 USING CHARACTERS); | ||
-- COL1: llo world | ||
|
||
VALUES SUBSTRING('hello world' FROM 3 FOR 5 USING CHARACTERS); | ||
-- COL1: llo w | ||
|
||
VALUES SUBSTRING('Жabڣc' FROM 1); | ||
-- COL1: Жabڣc | ||
|
||
VALUES SUBSTRING('Жabڣc' FROM 2); | ||
-- COL1: abڣc | ||
|
||
VALUES SUBSTRING('Жabڣc' FROM 1 FOR 1); | ||
-- COL1: Ж | ||
|
||
VALUES SUBSTRING('Жabڣc' FROM 1 FOR 2); | ||
-- COL1: Жa | ||
|
||
VALUES SUBSTRING('Жabڣc' FROM 3 USING OCTETS); | ||
-- COL1: abڣc | ||
|
||
VALUES SUBSTRING('Жabڣc' FROM 4 USING OCTETS); | ||
-- COL1: bڣc | ||
|
||
VALUES SUBSTRING('Жabڣc' FROM 1 FOR 2 USING OCTETS); | ||
-- COL1: Ж | ||
|
||
VALUES SUBSTRING('Жabڣc' FROM 3 FOR 2 USING OCTETS); | ||
-- COL1: ab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ type Expr = BetweenExpr | |
| QueryExpression | ||
| RowExpr | ||
| SimilarExpr | ||
| SubstringExpr | ||
| UnaryExpr | ||
| Value | ||
|
||
|
@@ -99,6 +100,9 @@ fn (e Expr) pstr(params map[string]Value) string { | |
SimilarExpr { | ||
e.pstr(params) | ||
} | ||
SubstringExpr { | ||
e.pstr(params) | ||
} | ||
UnaryExpr { | ||
e.pstr(params) | ||
} | ||
|
@@ -524,3 +528,28 @@ struct DropSchemaStmt { | |
schema_name Identifier | ||
behavior string // CASCADE or RESTRICT | ||
} | ||
|
||
struct SubstringExpr { | ||
value Expr | ||
from Expr // NoExpr when missing | ||
@for Expr // NoExpr when missing | ||
using string // CHARACTERS or OCTETS or '' | ||
} | ||
|
||
fn (e SubstringExpr) str() string { | ||
return e.pstr(map[string]Value{}) | ||
} | ||
|
||
fn (e SubstringExpr) pstr(params map[string]Value) string { | ||
mut s := 'SUBSTRING(${e.value.pstr(params)}' | ||
|
||
if e.from !is NoExpr { | ||
s += ' FROM ${e.from.pstr(params)}' | ||
} | ||
|
||
if e.@for !is NoExpr { | ||
s += ' FOR ${[email protected](params)}' | ||
} | ||
|
||
return s + ' USING $e.using)' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.