diff --git a/docs/querying/sql-functions.md b/docs/querying/sql-functions.md index 82f597b963a..b020d48bf59 100644 --- a/docs/querying/sql-functions.md +++ b/docs/querying/sql-functions.md @@ -1546,11 +1546,42 @@ Returns the following: ## LOOKUP -`LOOKUP(, [, ])` +Searches for `expr` in a registered [query-time lookup table](lookups.md) named `lookupName` and returns the mapped value. If `expr` is null or not contained in the lookup, returns `defaultValue` if supplied, otherwise returns null. -**Function type:** [Scalar, string](sql-scalar.md#string-functions) +* **Syntax:** `LOOKUP(expr, lookupName[, defaultValue])` +* **Function type:** Scalar, string -Looks up the expression in a registered query-time lookup table. +
Example + +The following example uses a `map` type lookup table named `code_to_name`, which contains the following key-value pairs: + +```json +{ + "SJU": "Luis Munoz Marin International Airport", + "IAD": "Dulles International Airport" +} +``` + +The example uses `code_to_name` to map the `Origin` column from the `flight-carriers` datasource to the corresponding full airport name. Returns `key not found` if no matching key exists in the lookup table. + +```sql +SELECT + "Origin" AS "origin_airport", + LOOKUP("Origin", 'code_to_name','key not found') AS "full_airport_name" +FROM "flight-carriers" +LIMIT 2 +``` + +Returns the following: + +| `origin_airport` | `full_airport_name` | +| -- | -- | +| `SJU` | `Luis Munoz Marin International Airport` | +| `BOS` | `key not found` | + +
+ +[Learn more](sql-scalar.md#string-functions) ## LOWER diff --git a/docs/querying/sql-scalar.md b/docs/querying/sql-scalar.md index f590d97d0f8..ee94ca769da 100644 --- a/docs/querying/sql-scalar.md +++ b/docs/querying/sql-scalar.md @@ -102,7 +102,7 @@ String functions accept strings and return a type appropriate to the function. |`CHAR_LENGTH(expr)`|Alias for `LENGTH`.| |`CHARACTER_LENGTH(expr)`|Alias for `LENGTH`.| |`STRLEN(expr)`|Alias for `LENGTH`.| -|`LOOKUP(expr, lookupName[, replaceMissingValueWith])`|Looks up `expr` in an existing [query-time lookup table](lookups.md) that has the name `lookupName`. Returns the optional constant `replaceMissingValueWith` when `expr` is null or when the lookup does not contain a value for `expr`.

You can query lookups directly using the [`lookup` schema](sql.md#from).| +|`LOOKUP(expr, lookupName[, replaceMissingValueWith])`|Searches for `expr` in a registered [query-time lookup table](lookups.md) named `lookupName` and returns the mapped value. If `expr` is null or not contained in the lookup, returns `replaceMissingValueWith` if supplied, otherwise returns null.

You can query lookups directly using the [`lookup` schema](sql.md#from).| |`LOWER(expr)`|Returns `expr` in all lowercase.| |`UPPER(expr)`|Returns `expr` in all uppercase.| |`LPAD(expr, length[, chars])`|Returns a string of `length` from `expr`. If `expr` is shorter than `length`, left pads `expr` with `chars`, which defaults to space characters. If `expr` exceeds `length`, truncates `expr` to equal `length`. If `chars` is an empty string, no padding is added. Returns `null` if either `expr` or `chars` is null.|