diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 3be9b8ef2..5bffc8ee5 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -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) diff --git a/book/src/super-sql/expressions/concat.md b/book/src/super-sql/expressions/concat.md index 60e090f94..b05fa3689 100644 --- a/book/src/super-sql/expressions/concat.md +++ b/book/src/super-sql/expressions/concat.md @@ -9,6 +9,9 @@ where `` 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 --- diff --git a/book/src/super-sql/functions/strings/concat.md b/book/src/super-sql/functions/strings/concat.md new file mode 100644 index 000000000..481839393 --- /dev/null +++ b/book/src/super-sql/functions/strings/concat.md @@ -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"} +``` diff --git a/book/src/super-sql/functions/strings/grep.md b/book/src/super-sql/functions/strings/grep.md index 301f9cab4..16d8c48f1 100644 --- a/book/src/super-sql/functions/strings/grep.md +++ b/book/src/super-sql/functions/strings/grep.md @@ -5,7 +5,7 @@ search strings inside of values ## Synopsis ``` -grep(re: string e: any) -> bool +grep(re: string, e: any) -> bool ``` ## Description @@ -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, @@ -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} +``` diff --git a/book/src/super-sql/functions/strings/join.md b/book/src/super-sql/functions/strings/join.md index e2d33c9e3..c90eb9cc8 100644 --- a/book/src/super-sql/functions/strings/join.md +++ b/book/src/super-sql/functions/strings/join.md @@ -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 @@ -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" +``` diff --git a/book/src/super-sql/functions/strings/position.md b/book/src/super-sql/functions/strings/position.md index ff1261ddd..a8c2d213f 100644 --- a/book/src/super-sql/functions/strings/position.md +++ b/book/src/super-sql/functions/strings/position.md @@ -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 @@ -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} +``` diff --git a/book/src/super-sql/functions/strings/substring.md b/book/src/super-sql/functions/strings/substring.md index fcdbe1211..b725538e6 100644 --- a/book/src/super-sql/functions/strings/substring.md +++ b/book/src/super-sql/functions/strings/substring.md @@ -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 @@ -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 @@ -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" +```