find_value¶
find_value(arr, value)
Description¶
Return the first offset
(zero-based index) of value
in array arr
(or null
if value
is not in arr
).
Usage¶
Call or Deploy find_value
?
Call find_value
directly
The easiest way to use bigfunctions
find_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 find_value
in your project
Why deploy?
- You may prefer to deploy
find_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
find_value
function can be deployed with:
pip install bigfunctions
bigfun get find_value
bigfun deploy find_value
Examples¶
1. When value
is in array
select bigfunctions.eu.find_value([3, 4], 4)
select bigfunctions.us.find_value([3, 4], 4)
select bigfunctions.europe_west1.find_value([3, 4], 4)
+--------+
| offset |
+--------+
| 1 |
+--------+
2. When value
is not in array
select bigfunctions.eu.find_value([3, 4], 7)
select bigfunctions.us.find_value([3, 4], 7)
select bigfunctions.europe_west1.find_value([3, 4], 7)
+--------+
| offset |
+--------+
| null |
+--------+
Use cases¶
You have a table of customer orders, and each order has an array of product IDs. You want to find the position of a specific product ID within each order's product array.
Table Schema:
CREATE OR REPLACE TABLE `your_project.your_dataset.orders` (
order_id INT64,
product_ids ARRAY<INT64>
);
INSERT INTO `your_project.your_dataset.orders` (order_id, product_ids) VALUES
(1, [101, 102, 103, 104]),
(2, [102, 105, 106]),
(3, [101, 103, 107, 102]);
Use Case: Finding the position of product ID 102:
SELECT
order_id,
bigfunctions.your_region.find_value(product_ids, 102) AS product_102_position
FROM
`your_project.your_dataset.orders`;
Result:
+---------+----------------------+
| order_id | product_102_position |
+---------+----------------------+
| 1 | 1 |
| 2 | 0 |
| 3 | 3 |
+---------+----------------------+
Explanation:
- The
find_value
function searches theproduct_ids
array for the value102
. - It returns the zero-based index (position) of the first occurrence of
102
. - For
order_id = 1
,102
is at index 1. - For
order_id = 2
,102
is at index 0. - For
order_id = 3
,102
is at index 3.
Other Potential Use Cases:
- Inventory Management: Find the location of a specific item within a warehouse represented as an array.
- Log Analysis: Find the first occurrence of a specific error code within a log entry containing an array of codes.
- User Behavior Analysis: Determine the position of a specific action within a user's sequence of actions on a website.
Remember to replace your_project
, your_dataset
, and your_region
with your actual values. For example, if your dataset is in the us-central1
region, you would use bigfunctions.us_central1
.
Need help or Found a bug?
Get help using find_value
The community can help! Engage the conversation on Slack
We also provide professional suppport.
Report a bug about find_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.