Skip to content

get_value

get_value(key_value_items, search_key)

Description

Return the first value with a key search_key from key_value_items (or return null if search_key does not exist in key_value_items).

Usage

Call or Deploy get_value ?
Call get_value directly

The easiest way to use bigfunctions

  • get_value function is deployed in 39 public datasets for all of the 39 BigQuery regions.
  • It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)

Public BigFunctions Datasets

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...
Deploy get_value in your project

Why deploy?

  • You may prefer to deploy get_value in your own project to build and manage your own catalog of functions.
  • This is particularly useful if you want to create private functions (for example calling your internal APIs).
  • Get started by reading the framework page

Deployment

get_value function can be deployed with:

pip install bigfunctions
bigfun get get_value
bigfun deploy get_value

Examples

select bigfunctions.eu.get_value([struct('a' as key, 8 as value), struct('b' as key, 9 as value)], a)
select bigfunctions.us.get_value([struct('a' as key, 8 as value), struct('b' as key, 9 as value)], a)
select bigfunctions.europe_west1.get_value([struct('a' as key, 8 as value), struct('b' as key, 9 as value)], a)
+-------+
| value |
+-------+
| 8     |
+-------+

select bigfunctions.eu.get_value([struct('a' as key, 8 as value), struct('b' as key, 9 as value)], c)
select bigfunctions.us.get_value([struct('a' as key, 8 as value), struct('b' as key, 9 as value)], c)
select bigfunctions.europe_west1.get_value([struct('a' as key, 8 as value), struct('b' as key, 9 as value)], c)
+-------+
| value |
+-------+
| null  |
+-------+

3. When there are multiple occurences of search_key, return the first found value

select bigfunctions.eu.get_value([struct('a' as key, 8 as value), struct('a' as key, 9 as value)], a)
select bigfunctions.us.get_value([struct('a' as key, 8 as value), struct('a' as key, 9 as value)], a)
select bigfunctions.europe_west1.get_value([struct('a' as key, 8 as value), struct('a' as key, 9 as value)], a)
+-------+
| value |
+-------+
| 8     |
+-------+

Use cases

This get_value function is useful for extracting values from arrays of key-value pairs (structs). Here are a few use cases:

1. Parameter Extraction: Imagine you have a table storing event data, where one column contains parameters as an array of key-value structs:

| event_id | parameters                                      |
|----------|-------------------------------------------------|
| 1        | [{'key': 'user_id', 'value': '123'}, {'key': 'event_type', 'value': 'purchase'}] |
| 2        | [{'key': 'item_id', 'value': '456'}, {'key': 'user_id', 'value': '789'}] |

You could use get_value to extract the user_id for each event:

SELECT
    event_id,
    bigfunctions.YOUR_REGION.get_value(parameters, 'user_id') AS user_id
FROM
    your_table;

This would give you:

| event_id | user_id |
|----------|---------|
| 1        | 123     |
| 2        | 789     |

2. Configuration Management: Suppose you store configuration settings as key-value pairs in a table:

| setting_group | settings                                       |
|---------------|-------------------------------------------------|
| website       | [{'key': 'theme', 'value': 'dark'}, {'key': 'font_size', 'value': '16px'}] |
| mobile_app    | [{'key': 'version', 'value': '1.2.3'}, {'key': 'platform', 'value': 'ios'}] |

You could retrieve specific settings using get_value:

SELECT
    setting_group,
    bigfunctions.YOUR_REGION.get_value(settings, 'theme') AS theme
FROM
    your_config_table
WHERE setting_group = 'website';

3. Product Attributes: E-commerce platforms often store product attributes as key-value pairs. get_value can help extract these attributes:

| product_id | attributes                                     |
|------------|-------------------------------------------------|
| 1          | [{'key': 'color', 'value': 'red'}, {'key': 'size', 'value': 'L'}] |
| 2          | [{'key': 'weight', 'value': '1kg'}, {'key': 'material', 'value': 'cotton'}] |

You can extract the color of product 1 with:

SELECT bigfunctions.YOUR_REGION.get_value(attributes, 'color') AS color
FROM your_product_table
WHERE product_id = 1;

These are just a few examples. Anytime you have data stored as an array of key-value pairs in BigQuery, get_value can be a convenient way to access the values associated with specific keys. Remember to replace YOUR_REGION with the appropriate BigQuery region for your data.


Need help or Found a bug?
Get help using get_value

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about get_value

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

We also provide professional suppport.


Show your ❤ by adding a ⭐ on