File: 406_create_function_bq_examples.sql

package info (click to toggle)
sqlfmt 0.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,580 kB
  • sloc: python: 10,007; sql: 5,626; makefile: 39
file content (78 lines) | stat: -rw-r--r-- 2,187 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- COPYRIGHT GOOGLE
-- SEE https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_function_statement

CREATE FUNCTION mydataset.multiply_Inputs(x FLOAT64, y FLOAT64)
RETURNS FLOAT64
AS (x * y);

CREATE TEMP FUNCTION multiplyInputs(x FLOAT64, y FLOAT64)
RETURNS FLOAT64
LANGUAGE js
AS r"""
  return x*y;
""";

SELECT multiplyInputs(a, b) FROM (SELECT 3 as a, 2 as b);

CREATE FUNCTION mydataset.remote_Multiply_Inputs(x FLOAT64, y FLOAT64)
RETURNS FLOAT64
REMOTE WITH CONNECTION us.myconnection
OPTIONS(endpoint="https://us-central1-myproject.cloudfunctions.net/multiply");

CREATE OR REPLACE TABLE FUNCTION mydataset.names_by_year(y INT64)
AS
  SELECT year, name, SUM(number) AS total
  FROM `bigquery-public-data.usa_names.usa_1910_current`
  WHERE year = y
  GROUP BY year, name;

CREATE OR REPLACE TABLE FUNCTION mydataset.names_by_year(y INT64)
RETURNS TABLE<name STRING, year INT64, total INT64>
AS
  SELECT year, name, SUM(number) AS total
  FROM `bigquery-public-data.usa_names.usa_1910_current`
  WHERE year = y
  GROUP BY year, name;
)))))__SQLFMT_OUTPUT__(((((
-- COPYRIGHT GOOGLE
-- SEE
-- https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_function_statement
create function mydataset.multiply_inputs(x float64, y float64)
returns float64
as (x * y)
;

create temp function multiplyinputs(x float64, y float64)
returns float64
language js
as r"""
  return x*y;
"""
;

select multiplyinputs(a, b)
from (select 3 as a, 2 as b)
;

create function mydataset.remote_multiply_inputs(x float64, y float64)
returns float64
remote with connection us.myconnection
options (endpoint = "https://us-central1-myproject.cloudfunctions.net/multiply")
;

create or replace table function mydataset.names_by_year(y int64)
as
select year, name, sum(number) as total
from `bigquery-public-data.usa_names.usa_1910_current`
where year = y
group by year, name
;

create or replace table function mydataset.names_by_year(y int64)
returns table<name string, year int64, total int64>
as
select year, name, sum(number) as total
from `bigquery-public-data.usa_names.usa_1910_current`
where year = y
group by year, name
;