From fda2d19b8823918106f10b01ba9af54c5fe5a4e8 Mon Sep 17 00:00:00 2001 From: Edgar Melendrez Date: Thu, 22 Aug 2024 11:11:16 -0700 Subject: [PATCH] [Docs] Batch09: only `lookup` (#16878) * [Docs] Batch09: only `lookup` * slight changes * Apply suggestions from code review Co-authored-by: Katya Macedo <38017980+ektravel@users.noreply.github.com> * applying suggestiontions * Apply suggestions from code review Co-authored-by: Victoria Lim * otherwise null -> otherwise returns null * updating definition in sql-scalar.md * Apply suggestions from code review Co-authored-by: Charles Smith * hoping to re-run web checks * change replaceMissingValueWith -> defaultValue * Update docs/querying/sql-scalar.md Co-authored-by: Katya Macedo <38017980+ektravel@users.noreply.github.com> * acronym_to_name -> airportcode_to_name * shortens `airportcode_to_name` to `code_to_name` --------- Co-authored-by: Katya Macedo <38017980+ektravel@users.noreply.github.com> Co-authored-by: Victoria Lim Co-authored-by: Charles Smith --- docs/querying/sql-functions.md | 37 +++++++++++++++++++++++++++++++--- docs/querying/sql-scalar.md | 2 +- 2 files changed, 35 insertions(+), 4 deletions(-) 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.|