find_greater_value¶
find_greater_value(arr, x)
Description¶
Return the offset
(zero-based index) of the first value
in arr
where value >= x
(or null
if no value
is greater than x
).
Usage¶
Call or Deploy find_greater_value
?
Call find_greater_value
directly
The easiest way to use bigfunctions
find_greater_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_greater_value
in your project
Why deploy?
- You may prefer to deploy
find_greater_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_greater_value
function can be deployed with:
pip install bigfunctions
bigfun get find_greater_value
bigfun deploy find_greater_value
Examples¶
1. When a strictly greater value
exists in array
select bigfunctions.eu.find_greater_value([0, 20, 50, 80, 100], 25)
select bigfunctions.us.find_greater_value([0, 20, 50, 80, 100], 25)
select bigfunctions.europe_west1.find_greater_value([0, 20, 50, 80, 100], 25)
+--------+
| offset |
+--------+
| 2 |
+--------+
2. When an identical value
exists in array
select bigfunctions.eu.find_greater_value([0, 20, 50, 80, 100], 20)
select bigfunctions.us.find_greater_value([0, 20, 50, 80, 100], 20)
select bigfunctions.europe_west1.find_greater_value([0, 20, 50, 80, 100], 20)
+--------+
| offset |
+--------+
| 1 |
+--------+
3. When a greater value
does NOT exist in array
select bigfunctions.eu.find_greater_value([0, 20, 50, 80, 100], 110)
select bigfunctions.us.find_greater_value([0, 20, 50, 80, 100], 110)
select bigfunctions.europe_west1.find_greater_value([0, 20, 50, 80, 100], 110)
+--------+
| offset |
+--------+
| null |
+--------+
Use cases¶
Use Case: Finding the appropriate pricing tier
Imagine you have a table of pricing tiers for a product, with each tier defined by a usage threshold and a corresponding price. You could use find_greater_value
to efficiently determine the correct pricing tier for a given customer's usage.
Example Scenario:
A software company offers different pricing tiers based on the number of API calls made per month:
Tier | API Calls | Price |
---|---|---|
Free | 0-1,000 | $0 |
Basic | 1,001-5,000 | $25 |
Premium | 5,001-10,000 | $50 |
Enterprise | > 10,000 | $100 |
BigQuery Implementation:
WITH PricingTiers AS (
SELECT [1000, 5000, 10000] AS api_call_thresholds,
[0, 25, 50, 100] AS prices
),
CustomerUsage AS (
SELECT 'customer_A' AS customer_id, 7500 AS api_calls
)
SELECT
CustomerUsage.customer_id,
CustomerUsage.api_calls,
PricingTiers.prices[SAFE_OFFSET(bigfunctions.YOUR_REGION.find_greater_value(PricingTiers.api_call_thresholds, CustomerUsage.api_calls))] AS price
FROM CustomerUsage
CROSS JOIN PricingTiers;
Explanation:
PricingTiers
CTE: This CTE stores the API call thresholds and corresponding prices as arrays.CustomerUsage
CTE: This CTE represents the customer's API usage.- Main Query:
- It joins
CustomerUsage
andPricingTiers
. find_greater_value
searches theapi_call_thresholds
array for the first value greater than or equal to the customer'sapi_calls
. This returns the index (offset) of the appropriate tier.SAFE_OFFSET
handles cases where the usage exceeds all defined tiers (e.g., > 10,000), returning the last price in the array.
Result:
customer_id | api_calls | price |
---|---|---|
customer_A | 7500 | 50 |
This example demonstrates how find_greater_value
can be used for efficient lookups in tiered data, eliminating the need for complex CASE
statements or joins. This approach is particularly useful when dealing with a large number of tiers or when the tiers are subject to change, as updating the arrays is much simpler than modifying numerous CASE
conditions. You could extend this to other use cases such as tax brackets, shipping costs based on weight, or commission rates based on sales volume. Remember to replace YOUR_REGION
with your BigQuery region (e.g. us
, eu
, us-central1
).
Need help or Found a bug?
Get help using find_greater_value
The community can help! Engage the conversation on Slack
We also provide professional suppport.
Report a bug about find_greater_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.