sort_values¶
sort_values(arr)
Description¶
Return sorted array (ascending)
Usage¶
Call or Deploy sort_values
?
Call sort_values
directly
The easiest way to use bigfunctions
sort_values
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 sort_values
in your project
Why deploy?
- You may prefer to deploy
sort_values
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
sort_values
function can be deployed with:
pip install bigfunctions
bigfun get sort_values
bigfun deploy sort_values
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] |
+--------------+
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.
Need help or Found a bug?
Get help using sort_values
The community can help! Engage the conversation on Slack
We also provide professional suppport.
Report a bug about 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.
We also provide professional suppport.