Skip to content

array_union

array_union(array11, array2)

Description

Returns the union of two arrays.

Usage

Call or Deploy array_union ?
Call array_union directly

The easiest way to use bigfunctions

  • array_union 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 array_union in your project

Why deploy?

  • You may prefer to deploy array_union 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

array_union function can be deployed with:

pip install bigfunctions
bigfun get array_union
bigfun deploy array_union

Examples

select bigfunctions.eu.array_union([1, 2, 3], [2, 6, 7])
select bigfunctions.us.array_union([1, 2, 3], [2, 6, 7])
select bigfunctions.europe_west1.array_union([1, 2, 3], [2, 6, 7])
+-----------------+
| result          |
+-----------------+
| [1, 2, 3, 6, 7] |
+-----------------+

Use cases

Use Case: Combining Product Categories

Imagine you have an e-commerce platform, and you store product categories as arrays in a BigQuery table. You want to display all unique categories associated with a product, even if they come from different sources.

Table Schema:

CREATE OR REPLACE TABLE `your_project.your_dataset.products` (
  product_id STRING,
  categories_source1 ARRAY<STRING>,
  categories_source2 ARRAY<STRING>
);

INSERT INTO `your_project.your_dataset.products` (product_id, categories_source1, categories_source2) VALUES
('product1', ['Electronics', 'Smartphones'], ['Mobile Phones', 'Gadgets']),
('product2', ['Clothing', 'Shoes'], ['Footwear', 'Accessories']);

Query with array_union:

SELECT
    product_id,
    bigfunctions.your_region.array_union(categories_source1, categories_source2) AS all_categories
  FROM
    `your_project.your_dataset.products`;

Result:

+-----------+-------------------------------------+
| product_id | all_categories                     |
+-----------+-------------------------------------+
| product1   | ['Electronics', 'Smartphones', 'Mobile Phones', 'Gadgets'] |
| product2   | ['Clothing', 'Shoes', 'Footwear', 'Accessories']        |
+-----------+-------------------------------------+

Explanation:

The array_union function effectively combines the arrays from categories_source1 and categories_source2, eliminating duplicate category names. This gives you a single array (all_categories) containing all unique categories associated with each product. This can be beneficial for filtering, faceting, or displaying comprehensive product information on your website.

Other Use Cases:

  • Merging User Interests: Combining user interests from different sources (e.g., browsing history, explicit preferences) into a single unified list.
  • Consolidating Tags: Merging tags or keywords assigned to articles or other content from multiple sources.
  • Combining Lists of Features: Merging lists of product features from different databases or APIs. Basically anytime you need to create a distinct list from multiple lists, array_union is a good choice.

Need help or Found a bug?
Get help using array_union

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about array_union

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