Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Google BigQuery

You're reading from   Learning Google BigQuery A beginner's guide to mining massive datasets through interactive analysis

Arrow left icon
Product type Paperback
Published in Dec 2017
Publisher Packt
ISBN-13 9781787288591
Length 264 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (3):
Arrow left icon
Eric Brown Eric Brown
Author Profile Icon Eric Brown
Eric Brown
Thirukkumaran Haridass Thirukkumaran Haridass
Author Profile Icon Thirukkumaran Haridass
Thirukkumaran Haridass
Mikhail Berlyant Mikhail Berlyant
Author Profile Icon Mikhail Berlyant
Mikhail Berlyant
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Google Cloud and Google BigQuery 2. Google Cloud SDK FREE CHAPTER 3. Google BigQuery Data Types 4. BigQuery SQL Basic 5. BigQuery SQL Advanced 6. Google BigQuery API 7. Visualizing BigQuery Data 8. Google Cloud Pub/Sub

User-defined functions

User-defined functions can be written in JavaScript or SQL in BigQuery. These functions can be called in queries to obtain results. The following are the supported datatypes that can be passed to and returned by the functions:

  • ARRAY
  • BOOL
  • BYTES
  • DATE
  • FLOAT64
  • STRING
  • STRUCT
  • TIMESTAMP

The following is a simple function written in JavaScript to return the sum of two numbers, and it is used in the query. This query passes the tip_amount and tolls_amount values for each row from the table to the function and gets the sum:

#standardSQL
CREATE TEMPORARY FUNCTION GetOtherCharges(tipamount FLOAT64, tollsamount FLOAT64)
RETURNS INT64
LANGUAGE js AS """
return tipamount + tollsamount;
""";

SELECT vendor_id, GetOtherCharges( tip_amount, tolls_amount )
FROM `bigquery-public-data.new_york.tlc_green_trips_2013`
Custom external JavaScript libraries...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime