BigQuery Introduces JavaScript User-Defined Aggregate Functions

February 07, 2025 at 6:18:15 AM

TL;DR Google has announced the general availability of JavaScript User-Defined Aggregate Functions (UDAF) in BigQuery, allowing users to create custom aggregate functions using JavaScript. The process requires JavaScript code as a quoted string and implementation of four essential functions: initialState, aggregate, merge, and finalize. Persistent UDAFs are reusable, secure, and maintain data integrity. Users must use the CREATE AGGREGATE FUNCTION statement and include the dataset in the path.

BigQuery Introduces JavaScript User-Defined Aggregate Functions

Google has announced the general availability (GA) of JavaScript User-Defined Aggregate Functions (UDAF) in BigQuery, allowing users to create custom aggregate functions using JavaScript.

The JavaScript UDAF creation process requires several mandatory components:

Core Function Requirements

  • JavaScript code must be provided as a quoted string literal
  • Implementation of four essential JavaScript functions:
  • initialState: Initializes the results
  • aggregate: Aggregates the data
  • merge: Merges results
  • finalize: Finalizes the results

Technical Specifications

  • Only specific type encodings are permitted
  • All values returned by initialState or remaining in state arguments must be serializable
  • Non-serializable aggregation data requires use of included serialize and deserialize functions

Persistent UDAF Features

The platform now supports persistent UDAFs with the following characteristics:

  • Reusable across multiple queries
  • Secure sharing between owners
  • No data mutation capabilities
  • No external system communication
  • No logging to Google Cloud Observability

To create a persistent UDAF, users must:

  • Use the CREATE AGGREGATE FUNCTION statement
  • Omit TEMP or TEMPORARY keywords
  • Include dataset in function path

These functions maintain data integrity and security while providing enhanced aggregation capabilities within BigQuery's environment.

CREATE OR REPLACE AGGREGATE FUNCTION my_project.my_dataset.SumPositive(x FLOAT64)
RETURNS FLOAT64
LANGUAGE js
AS r'''
 export function initialState() {
   return {sum: 0}
 }
 export function aggregate(state, x) {
   if (x > 0) {
     state.sum += x;
   }
 }
 export function merge(state, partialState) {
   state.sum += partialState.sum;
 }
 export function finalize(state) {
   return state.sum;
 }
''';

Have more questions on this topic? Ask our AI assistant for in-depth insights.

Read more from sources 👇

The Only Digital Marketing Feed You'll Ever Need.

Stay informed your way. Tailored updates when and how you want them. 100% Free.

10,000+ Users

500+ Sources

1000+ Tools

Or

Related Posts

Google Launches Gemini AI-Powered Join Operations in BigQuery Data Preparation

Google Launches Gemini AI-Powered Join Operations in BigQuery Data Preparation

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Tired of spending too much time creating audits for your clients?

Tired of spending too much time creating audits for your clients?

Featured
BigQuery Enhances ML Bucketing Function and Set Operation Features

BigQuery Enhances ML Bucketing Function and Set Operation Features

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source
BigQuery Adds Stored Column Usage Tracking for Vector Search Queries

BigQuery Adds Stored Column Usage Tracking for Vector Search Queries

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google Delays Paid Access Requirement for Gemini in BigQuery

Google Delays Paid Access Requirement for Gemini in BigQuery

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google Launches BigQuery Metastore for Unified Metadata Management

Google Launches BigQuery Metastore for Unified Metadata Management

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google Introduces Natural Language Data Preparation in BigQuery with Gemini

Google Introduces Natural Language Data Preparation in BigQuery with Gemini

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google Updates BigQuery with Multi-Time Series Forecasting and Enhanced Security Features

Google Updates BigQuery with Multi-Time Series Forecasting and Enhanced Security Features

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source

Related Tools

Marketing Auditor logo

Marketing Auditor

Verified Tool

Verified Tool

Marketing Auditor is a Verified Tool. Want to get this badge? Contact us.

Verified Tool

Automated audits for Google Ads and Analytics.

Get Featured Here

Showcase your tool in this list.

Contact Us
Databricks logo

Databricks

Generative AI-powered data intelligence platform

Data Engineering
GA4 SQL logo

GA4 SQL

Verified Tool

Verified Tool

GA4 SQL is a Verified Tool. Want to get this badge? Contact us.

Verified Tool

Generate GA4 BigQuery queries easily

Data Analysis
TapClicks logo

TapClicks

Automated marketing solutions powered by your data

Data Engineering
Stitch logo

Stitch

Automated cloud data pipelines, no coding needed

Data Engineering
Akkio logo

Akkio

AI-powered analytics for agencies

Data Analysis
NinjaCat logo

NinjaCat

AI-powered marketing data and analytics platform

Reporting
Funnel logo

Funnel

Aggregate and analyze marketing data seamlessly

Reporting
Fivetran logo

Fivetran

Effortlessly centralize and move data from any source

Data Engineering
Power My Analytics logo

Power My Analytics

Automate and integrate your marketing data

Reporting

Get Featured Here

Showcase your tool in this list.

Contact Us