bigfunctions > sort_values
sort_values¶
Call or Deploy sort_values
?
✅ You can call this sort_values
bigfunction directly from your Google Cloud Project (no install required).
- This
sort_values
function is deployed inbigfunctions
GCP project in 39 datasets for all of the 39 BigQuery regions. You need to use the dataset in the same region as your datasets (otherwise you may have a function not found error). - Function is public, so it can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
- You may prefer to deploy the BigFunction in your own project if you want 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). Discover the framework
Public BigFunctions Datasets:
Region | Dataset |
---|---|
eu |
bigfunctions.eu |
us |
bigfunctions.us |
europe-west1 |
bigfunctions.europe_west1 |
asia-east1 |
bigfunctions.asia_east1 |
... | ... |
Description¶
Signature
sort_values(arr)
Description
Return sorted array (ascending)
Examples¶
select bigfunctions.eu.sort_values([1, 4, 3])
select bigfunctions.us.sort_values([1, 4, 3])
select bigfunctions.europe_west1.sort_values([1, 4, 3])
+--------------+
| sorted_array |
+--------------+
| [1, 3, 4] |
+--------------+
Need help using sort_values
?
The community can help! Engage the conversation on Slack
For professional suppport, don't hesitate to chat with us.
Found a bug using sort_values
?
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.
For professional suppport, don't hesitate to chat with us.
Use cases¶
A use case for the sort_values
function is preparing data for aggregation or other operations where the order of elements within an array matters.
Scenario: You have a table storing the daily sales for different products, and you want to find the median sales value for each product over a week.
Table:
product_id | daily_sales |
---|---|
1 | [10, 12, 8, 15, 11, 9, 13] |
2 | [5, 7, 6, 8, 4, 9, 10] |
3 | [20, 18, 22, 19, 21, 17, 23] |
Query:
SELECT
product_id,
(
SELECT
CAST(daily_sales[OFFSET(CAST(ARRAY_LENGTH(daily_sales) / 2 AS INT64))] AS BIGNUMERIC)
FROM
UNNEST([bigfunctions.YOUR_REGION.sort_values(daily_sales)]) AS daily_sales
) AS median_sales
FROM
`your_project.your_dataset.your_table`
Explanation:
bigfunctions.YOUR_REGION.sort_values(daily_sales)
: This sorts thedaily_sales
array in ascending order for each product. ReplaceYOUR_REGION
with your BigQuery region (e.g.,us
,eu
,us-central1
).UNNEST(...) AS daily_sales
: This unnests the sorted array, creating a separate row for each daily sales value. However, since we're putting it inside a subquery and immediately re-aggregating it, we're using UNNEST here as a trick to access elements of the now-sorted array by index.ARRAY_LENGTH(daily_sales) / 2
: This calculates the middle index of the sorted array.daily_sales[OFFSET(CAST(... AS INT64))]
: This retrieves the element at the calculated middle index, effectively giving you the median value. We cast to INT64 because ARRAY_LENGTH returns an INT64 and OFFSET requires an INT64.CAST(... AS BIGNUMERIC)
: This is just to handle potential overflow if your sales numbers are very large. Adjust the data type as needed for your data.
By sorting the array first, you can easily find the median value using the array's middle index. This wouldn't be reliable with the unsorted data. Similar logic could be used to calculate other quantiles or perform operations sensitive to the order of elements within the array.
Spread the word¶
BigFunctions is fully open-source. Help make it a success by spreading the word!