Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
- [unflatten](super-sql/functions/records/unflatten.md)
- [values](super-sql/functions/records/values.md)
- [Strings](super-sql/functions/strings/intro.md)
- [concat](super-sql/functions/strings/concat.md)
- [grep](super-sql/functions/strings/grep.md)
- [join](super-sql/functions/strings/join.md)
- [json_stringify](super-sql/functions/strings/json_stringify.md)
Expand Down
3 changes: 3 additions & 0 deletions book/src/super-sql/expressions/concat.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ where `<expr>` is any [expression](intro.md) that results in a string value.
It is an error to concatenate non-string values.
Values may be converted to string using a [cast](cast.md) to type string.

The [concat](../functions/strings/concat.md) function also concatenates
strings and takes any number of arguments.

## Examples

---
Expand Down
65 changes: 65 additions & 0 deletions book/src/super-sql/functions/strings/concat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# concat

concatenate strings

## Synopsis

```
concat(s: string [, ... s: string]) -> string
```

## Description

The `concat` function concatenates its string arguments, in the order given,
into a single string. Arguments that are `null` are skipped, and if every
argument is `null`, the result is an empty string.

It is an error to concatenate non-string values.
Values may be converted to string using a [cast](../../expressions/cast.md)
to type string.

The [concatenation operator](../../expressions/concat.md) `||` also
concatenates strings.

## Examples

---

_Concatenate fields and a literal_

```mdtest-spq
# spq
values concat(first, " ", last)
# input
{first:"Ada",last:"Lovelace"}
# expected output
"Ada Lovelace"
```

---

_Null arguments are skipped_

```mdtest-spq
# spq
values {input:this, s:concat(a, "-", b)}
# input
{a:"x",b:"y"}
{a:"x",b:null}
# expected output
{input:{a:"x",b:"y"},s:"x-y"}
{input:{a:"x",b:null},s:"x-"}
```

---

_Cast non-string values to concatenate them_

```mdtest-spq
# spq
values {input:this, s:concat("port ", port), cast:concat("port ", port::string)}
# input
{port:8080}
# expected output
{input:{port:8080},s:error({message:"concat: string arg required",on:8080}),cast:"port 8080"}
```
29 changes: 25 additions & 4 deletions book/src/super-sql/functions/strings/grep.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ search strings inside of values
## Synopsis

```
grep(re: string e: any) -> bool
grep(re: string, e: any) -> bool
```

## Description
Expand All @@ -16,9 +16,12 @@ The `grep` function searches all of the strings in its input value `e`
If the pattern matches for any string, then the result is `true`. Otherwise, it is `false`.

>[!NOTE]
> String matches are case insensitive while regular expression
> and glob matches are case sensitive. In a forthcoming release, case sensitivity
> will be expressible for all three pattern types.
> If `re` can match only one fixed string, as when it has no regular
> expression metacharacters (e.g., `"baz"`), `grep` looks for that string as
> a substring and ignores case, as a
> [keyword search](../../operators/search.md#keyword) does.
> Otherwise, the match is case-sensitive, so
> to ignore case in a regular expression, start it with `(?i)`.

The entire input value is traversed:
* for records, each field value is traversed or descended if a complex type,
Expand Down Expand Up @@ -85,3 +88,21 @@ grep('b.*', s.ok())
{s:"bar"}
{s:"baz"}
```

---

_A pattern without metacharacters ignores case, but a regular expression doesn't_

```mdtest-spq
# spq
values {
input: this,
plain: grep("BAZ", this),
regexp: grep("B.Z", this),
ignore_case: grep("(?i)B.Z", this)
}
# input
"baz"
# expected output
{input:"baz",plain:true,regexp:false,ignore_case:true}
```
18 changes: 16 additions & 2 deletions book/src/super-sql/functions/strings/join.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ concatenate array of strings with a separator
## Synopsis

```
join(val: [string], sep: string) -> string
join(val: [string] [, sep: string]) -> string
```

## Description

The `join` function concatenates the elements of string array `val` to create a single
string. The string `sep` is placed between each value in the resulting string.
string. If the string `sep` is given, it is placed between each value in the
resulting string.

## Examples

Expand Down Expand Up @@ -42,3 +43,16 @@ values join(cast(this, <[string]>), "...")
"1...2...3"
"10.0.0.1...10.0.0.2"
```

---

_The separator is optional_

```mdtest-spq
# spq
values join(this)
# input
["a","b","c"]
# expected output
"abc"
```
33 changes: 33 additions & 0 deletions book/src/super-sql/functions/strings/position.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ position(sub: string IN s:string) -> int64

The `position` function returns the 1-based index where string `sub` first
occurs in string `s`. If `sub` is not a sub-string of `s` then 0 is returned.
The index is always 1-based, whatever the
[index_base](../../declarations/pragmas.md) pragma is set to.

The second form in the synopsis is SQL syntax, and it works in pipe queries
too.

## Examples

Expand All @@ -26,3 +31,31 @@ values position(s, sub)
# expected output
4
```

---

_0 means the substring wasn't found_

```mdtest-spq
# spq
values {input:this, pos:position(this, "bar")}
# input
"foobar"
"foo"
# expected output
{input:"foobar",pos:4}
{input:"foo",pos:0}
```

---

_The SQL syntax_

```mdtest-spq
# spq
SELECT position('bar' IN s) AS pos
# input
{s:"foobar"}
# expected output
{pos:4}
```
20 changes: 18 additions & 2 deletions book/src/super-sql/functions/strings/substring.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ substring(s: string [ FROM start: number ] [ FOR len: number ]) -> string
The `substring` function returns a slice of a string using
the anachronistic SQL syntax which includes the `FROM` and `FOR` keywords
inside of the call arguments. The function returns a string of length `len`
comprising the unicode code points starting at offset `start`.
comprising the Unicode code points starting at offset `start`.
At least one of `FROM` and `FOR` is required. Without `FROM`, the slice
starts at the beginning of `s`, and without `FOR`, it runs to the end of `s`.
A negative `start` counts back from the end of `s`.

Indexing is 0-based by default but can be 1-based by the use of
a [pragma](../../declarations/pragmas.md) as with
Expand All @@ -27,7 +30,7 @@ a [pragma](../../declarations/pragmas.md) as with
## Examples

---
_Simple substring call from in a SQL operator_
_Simple substring call in a SQL operator_

```mdtest-spq
# spq
Expand Down Expand Up @@ -64,3 +67,16 @@ SELECT SUBSTRING(this FROM 3) AS s
# expected output
{s:"SuperDB = "}
```

---

_A negative start counts back from the end_

```mdtest-spq
# spq
values substring(this FROM -2)
# input
"SuperDB"
# expected output
"DB"
```
Loading