{"id":8671,"date":"2026-09-17T07:01:06","date_gmt":"2026-09-17T07:01:06","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=8671"},"modified":"2026-09-18T17:17:39","modified_gmt":"2026-09-18T17:17:39","slug":"creating-a-function-in-bigquery","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/","title":{"rendered":"Creating a function in BigQuery"},"content":{"rendered":"\r\n<h1>User Defined Functions in BigQuery<\/h1>\r\n<p class=\"wp-block-paragraph\">A <strong><b>user defined function<\/b><\/strong>\u00a0or UDF, lets you package a piece of logic, written in SQL or JavaScript, into something you can call from inside a query, the same way you would call a built in function such as ROUND or UPPER. It takes columns of input, runs your logic against them, and returns a value.<\/p>\r\n<p>User-defined functions (UDFs) in BigQuery represent a pivotal tool in the arsenal of data analysts and engineers, facilitating the customization and enhancement of data processing pipelines within Google&#8217;s powerful data warehouse solution. UDFs empower users to extend the functionality of BigQuery by encapsulating custom logic into reusable code snippets, thereby enabling complex transformations, calculations, and analyses that may not be achievable through standard SQL queries alone.<\/p>\r\n<p>At its core, a UDF is a piece of code authored by the user, typically written in languages such as JavaScript or SQL, which can be invoked within BigQuery SQL queries to perform specific operations on data. These functions can range from simple mathematical computations to intricate data manipulations, providing a versatile mechanism for tailoring data processing workflows to meet the unique requirements of each use case.<\/p>\r\n<h2>A Temporary JavaScript Function<\/h2>\r\n<p>Paste the following into the query editor:<\/p>\r\n<p>CREATE TEMP FUNCTION multiplyput(x FLOAT64, y FLOAT64)<br \/>RETURNS FLOAT64<br \/>LANGUAGE js AS &#8220;&#8221;&#8221;<br \/>return x*y;<br \/>&#8220;&#8221;&#8221;;<br \/><br \/>WITH numbers AS<br \/>\u00a0\u00a0(SELECT 1 AS x, 5 AS y<br \/>\u00a0\u00a0UNION ALL<br \/>\u00a0\u00a0SELECT 2 AS x, 10 AS y)<br \/>SELECT x, y, multiplyput(x, y) AS product<br \/>FROM numbers;<\/p>\r\n<p>Click Run.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"550\" class=\"wp-image-8672\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-74.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-74.png 590w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-74-300x280.png 300w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/figure>\r\n<p>This function only exists for the query it was defined in. Once the query finishes, multiplyput is gone, and typing it into your next query means writing the whole thing out again.<\/p>\r\n<h2>Prefer SQL Over JavaScript When You Can<\/h2>\r\n<p>The example above uses LANGUAGE js, but the same logic can be written as a plain SQL function instead:<\/p>\r\n<p>CREATE TEMP FUNCTION multiplyput(x FLOAT64, y FLOAT64) AS (x * y);<br \/><br \/>WITH numbers AS<br \/>\u00a0\u00a0(SELECT 1 AS x, 5 AS y<br \/>\u00a0\u00a0UNION ALL<br \/>\u00a0\u00a0SELECT 2 AS x, 10 AS y)<br \/>SELECT x, y, multiplyput(x, y) AS product<br \/>FROM numbers;<\/p>\r\n<p>This is not just a style preference. Google&#8217;s own performance guidance for BigQuery states that a JavaScript UDF has to spin up a subprocess to run, which uses meaningfully more slot resources than the same logic written in SQL, and slows the query down as a result. Whenever your logic can be expressed as a SQL expression, a SQL UDF is the faster choice. JavaScript earns its place for genuinely complex logic, such as parsing an awkward text format, that SQL cannot express cleanly on its own.<\/p>\r\n<h2>Making a Function Persistent<\/h2>\r\n<p>A temporary function only lives inside the query that defines it. A <strong><b>persistent<\/b><\/strong>\u00a0function, created with CREATE FUNCTION instead of CREATE TEMP FUNCTION, is saved into a dataset and can be called from any query afterward, the same as a built in function.<\/p>\r\n<p>CREATE FUNCTION your_dataset.multiplyput(x FLOAT64, y FLOAT64) AS (x * y);<br \/><br \/>SELECT your_dataset.multiplyput(3, 4) AS product;<\/p>\r\n<p>Google&#8217;s own best practice guidance recommends exactly this, building a shared dataset of persistent SQL and JavaScript functions that your whole team can call, rather than pasting the same temporary function definition into query after query. To remove a persistent function later, use DROP FUNCTION your_dataset.multiplyput.<\/p>\r\n<h2>Other Kinds of Routines Worth Knowing About<\/h2>\r\n<p>A scalar UDF, returning a single value, covers most everyday needs, but BigQuery supports a few related routine types worth knowing exist:<\/p>\r\n<ul>\r\n<li><b><\/b><strong><b>Table functions, <\/b><\/strong>which return an entire table instead of a single value, useful when a piece of logic needs to produce multiple rows and columns rather than one result.<\/li>\r\n<li><b><\/b><strong><b>Python UDFs, <\/b><\/strong>which let you write the function body in Python instead of SQL or JavaScript, including the option to use third party libraries from PyPI.<\/li>\r\n<li><b><\/b><strong><b>Aggregate functions, <\/b><\/strong>which work like SUM or COUNT, combining many rows into a single summarized result, rather than transforming one row at a time.<\/li>\r\n<\/ul>\r\n<h2>Common Mistakes to Avoid<\/h2>\r\n<ul>\r\n<li>Writing a JavaScript UDF for logic that SQL could express directly. Check whether a plain SQL function does the same job first, since it will almost always run faster.<\/li>\r\n<li>Copying a triple quoted JavaScript body from a document without checking the quote characters. Curly quotes inserted automatically by a word processor will not work, only straight quotes delimit a JavaScript UDF body correctly.<\/li>\r\n<li>Recreating the same temporary function in every query. If a function is used more than once, make it persistent in a shared dataset instead, so everyone calls the same definition and a fix only needs to happen in one place.<\/li>\r\n<\/ul>\r\n<p>That covers creating a BigQuery function, choosing SQL over JavaScript when you can, and making one persistent so your whole team can reuse it. To go further, explore <a href=\"https:\/\/prwatech.in\/gcp-training-institutes-in-pune\/\"><strong><b>Prwatech&#8217;s Google Cloud training<\/b><\/strong><\/a>\u00a0program, which includes placement assistance.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>User Defined Functions in BigQuery A user defined function\u00a0or UDF, lets you package a piece of logic, written in SQL or JavaScript, into something you can call from inside a query, the same way you would call a built in function such as ROUND or UPPER. It takes columns of input, runs your logic against [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1632,1],"tags":[728,1401,1403,736,732,1402,605,729,699,700,1399,683,684,685,611,692],"class_list":["post-8671","post","type-post","status-publish","format-standard","hentry","category-bigquery","category-google-cloud-platform","tag-bigquery","tag-bigquery-console","tag-bigquery-documentation","tag-bigquery-in-cloud","tag-bigquery-sql","tag-bigquery-tutorial","tag-gcp","tag-gcp-bigquery","tag-gcp-certification","tag-gcp-cloud-console","tag-gcp-console","tag-google-cloud-certification","tag-google-cloud-console","tag-google-cloud-courses","tag-google-cloud-platform","tag-google-cloud-training"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>BigQuery User Defined Functions: SQL, JavaScript, Persistent<\/title>\n<meta name=\"description\" content=\"Create a BigQuery UDF the right way, choosing SQL over JavaScript for performance, and making it persistent instead of temporary.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"BigQuery User Defined Functions: SQL, JavaScript, Persistent\" \/>\n<meta property=\"og:description\" content=\"Create a BigQuery UDF the right way, choosing SQL over JavaScript for performance, and making it persistent instead of temporary.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-17T07:01:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-09-18T17:17:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-74.png\" \/>\n\t<meta property=\"og:image:width\" content=\"590\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/\",\"url\":\"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/\",\"name\":\"BigQuery User Defined Functions: SQL, JavaScript, Persistent\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-74.png\",\"datePublished\":\"2026-09-17T07:01:06+00:00\",\"dateModified\":\"2026-09-18T17:17:39+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Create a BigQuery UDF the right way, choosing SQL over JavaScript for performance, and making it persistent instead of temporary.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-74.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-74.png\",\"width\":590,\"height\":550},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating a function in BigQuery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"BigQuery User Defined Functions: SQL, JavaScript, Persistent","description":"Create a BigQuery UDF the right way, choosing SQL over JavaScript for performance, and making it persistent instead of temporary.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/","og_locale":"en_US","og_type":"article","og_title":"BigQuery User Defined Functions: SQL, JavaScript, Persistent","og_description":"Create a BigQuery UDF the right way, choosing SQL over JavaScript for performance, and making it persistent instead of temporary.","og_url":"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2026-09-17T07:01:06+00:00","article_modified_time":"2026-09-18T17:17:39+00:00","og_image":[{"width":590,"height":550,"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-74.png","type":"image\/png"}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/","url":"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/","name":"BigQuery User Defined Functions: SQL, JavaScript, Persistent","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-74.png","datePublished":"2026-09-17T07:01:06+00:00","dateModified":"2026-09-18T17:17:39+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Create a BigQuery UDF the right way, choosing SQL over JavaScript for performance, and making it persistent instead of temporary.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-74.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-74.png","width":590,"height":550},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/google-cloud-platform\/bigquery\/creating-a-function-in-bigquery\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Creating a function in BigQuery"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=8671"}],"version-history":[{"count":7,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8671\/revisions"}],"predecessor-version":[{"id":11793,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8671\/revisions\/11793"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=8671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=8671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=8671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}