BigQuery
Installation
Enabling SQLFrame
SQLFrame can be used in two ways:
- Directly importing the
sqlframe.bigquerypackage - Using the activate function to allow for continuing to use
pyspark.sqlbut have it use SQLFrame behind the scenes.
Import
If converting a PySpark pipeline, all pyspark.sql should be replaced with sqlframe.bigquery.
In addition, many classes will have a BigQuery prefix.
For example, BigQueryDataFrame instead of DataFrame.
# PySpark import
# from pyspark.sql import SparkSession
# from pyspark.sql import functions as F
# from pyspark.sql.dataframe import DataFrame
# SQLFrame import
from sqlframe.bigquery import BigQuerySession
from sqlframe.bigquery import functions as F
from sqlframe.bigquery import BigQueryDataFrame
Activate
If you would like to continue using pyspark.sql but have it use SQLFrame behind the scenes, you can use the activate function.
from sqlframe import activate
activate("bigquery", config={"default_dataset": "sqlframe.db1"})
from pyspark.sql import SparkSession
SparkSession will now be a SQLFrame BigQuerySession object and everything will be run on BigQuery directly.
See activate configuration for information on how to pass in a connection and config options.
Creating a Session
SQLFrame uses the BigQuery DBAPI Connection to connect to BigQuery.
A BigQuerySession, which implements the PySpark Session API, can be created by passing in a google.cloud.bigquery.dbapi.Connection object or by allowing SQLFrame to create a connection for you.
By default, SQLFrame will create a connection by inferring it from the environment (for example using gcloud auth).
Regardless of approach, it is recommended to configure default_dataset in the BigQuerySession constructor in order to make it easier to use the catalog methods (see example below).
import google.auth
from google.api_core import client_info
from google.oauth2 import service_account
from google.cloud.bigquery.dbapi import connect
from sqlframe.bigquery import BigQuerySession
creds = service_account.Credentials.from_service_account_file("path/to/credentials.json")
client = google.cloud.bigquery.Client(
project="my-project",
credentials=creds,
location="us-central1",
client_info=client_info.ClientInfo(user_agent="sqlframe"),
)
conn = connect(client=client)
session = BigQuerySession(conn=conn, default_dataset="sqlframe.db1")
import google.auth
from google.api_core import client_info
from google.oauth2 import service_account
from google.cloud.bigquery.dbapi import connect
from sqlframe import activate
creds = service_account.Credentials.from_service_account_file("path/to/credentials.json")
client = google.cloud.bigquery.Client(
project="my-project",
credentials=creds,
location="us-central1",
client_info=client_info.ClientInfo(user_agent="sqlframe"),
)
conn = connect(client=client)
activate("bigquery", conn=conn, config={"default_dataset": "sqlframe.db1"})
from pyspark.sql import SparkSession
session = SparkSession.builder.getOrCreate()
Using BigQuery Unique Functions
BigQuery may have a function that isn't represented within the PySpark API. If that is the case, you can call it directly using PySpark call_function function.
from sqlframe.bigquery import BigQuerySession
from sqlframe.bigquery import functions as F
session = BigQuerySession(default_dataset="sqlframe.db1")
(
session.table('"bigquery-public-data".samples.natality')
.select(F.call_function("FARM_FINGERPRINT", F.col("source")).alias("source_hash"))
.show()
)
Example Usage
from sqlframe.bigquery import BigQuerySession
from sqlframe.bigquery import functions as F
from sqlframe.bigquery import Window
session = BigQuerySession(default_dataset="sqlframe.db1")
table_path = '"bigquery-public-data".samples.natality'
# Get columns in the table
print(session.catalog.listColumns(table_path))
# Get the top 5 years with the greatest year-over-year % change in new families with a single child
(
session.table(table_path)
.where(F.col("ever_born") == 1)
.groupBy("year")
.agg(F.count("*").alias("num_single_child_families"))
.withColumn(
"last_year_num_single_child_families",
F.lag(F.col("num_single_child_families"), 1).over(Window.orderBy("year"))
)
.withColumn(
"percent_change",
(F.col("num_single_child_families") - F.col("last_year_num_single_child_families"))
/ F.col("last_year_num_single_child_families")
)
.orderBy(F.abs(F.col("percent_change")).desc())
.select(
F.col("year").alias("year"),
F.format_number("num_single_child_families", 0).alias("new families single child"),
F.format_number(F.col("percent_change") * 100, 2).alias("percent change"),
)
.limit(5)
.show()
)
"""
+------+---------------------------+----------------+
| year | new families single child | percent change |
+------+---------------------------+----------------+
| 1989 | 1,650,246 | 25.02 |
| 1974 | 783,448 | 14.49 |
| 1977 | 1,057,379 | 11.38 |
| 1985 | 1,308,476 | 11.15 |
| 1975 | 868,985 | 10.92 |
+------+---------------------------+----------------+
"""
Supported PySpark API Methods
See something that you would like to see supported? Open an issue!
Catalog Class
- add_table
- SQLFrame Specific: Adds a table to known schemas that SQLFrame tracks
- currentCatalog
- currentDatabase
- databaseExists
- functionExists
- getDatabase
- getFunction
- getTable
- get_columns
- SQLFrame Specific: Similar to
listColumnsbut returns SQLGlot expressions instead
- SQLFrame Specific: Similar to
- get_columns_from_schema
- SQLFrame Specific: Gets the columns from the known schemas to SQLFrame
- listCatalogs
- listColumns
- listDatabases
- listFunctions
- listTables
- setCurrentCatalog
- setCurrentDatabase
- tableExists
Column Class
- alias
- alias
- asc
- asc_nulls_first
- asc_nulls_last
- between
- cast
- contains
- The argument to
containsmust be a string - desc
- desc_nulls_first
- desc_nulls_last
- endswith
- ilike
- isNotNull
- isNull
- isin
- like
- otherwise
- over
- rlike
- sql
- SQLFrame Specific: Get the SQL representation of a given column
- startswith
- when
DataFrame Class
- agg
- alias
- approxQuantile
- cache
- coalesce
- collect
- columns
- copy
- corr
- count
- cov
- createOrReplaceTempView
- crossJoin
- cube
- distinct
- drop
- dropDuplicates
- drop_duplicates
- dropna
- exceptAll
- explain
- fillna
- filter
- first
- groupBy
- groupby
- head
- intersect
- intersectAll
- join
- limit
- lineage
- Get lineage for a specific column. Returns a SQLGlot Node. Can be used to get lineage SQL or HTML representation.
- na
- orderBy
- persist
- printSchema
- replace
- select
- schema
- show
- Vertical Argument is not Supported
- sort
- sql
- SQLFrame Specific: Get the SQL representation of a given DataFrame
- stat
- toDF
- toPandas
- union
- unionAll
- unionByName
- unpivot
- where
- withColumn
- withColumnRenamed
- withColumnsRenamed
- write
Functions
- abs
- acos
- acosh
- add_months
- any_value
- Always ignores nulls
- approxCountDistinct
- approx_count_distinct
- array
- array_contains
- array_distinct
- array_except
- array_join
- array_max
- array_min
- array_position
- array_remove
- array_reverse
- SQLFrame Specific: Functions like
reversebut for only arrays - array_size
- array_sort
- Arrays are not allowed to have None (NULL) values
- array_union
- asc
- asc_nulls_first
- asc_nulls_last
- ascii
- asin
- asinh
- atan
- atan2
- atanh
- avg
- base64
- bin
- bit_and
- bit_count
- bit_length
- Symbols are not supported
- bit_or
- bit_xor
- bitwiseNOT
- bitwise_not
- bool_and
- bool_or
- bround
- btrim
- call_function
- cbrt
- ceil
- ceiling
- char
- char_length
- character_length
- coalesce
- col
- collect_list
- collect_set
- concat
- Only works on strings (does not work on arrays)
- concat_ws
- contains
- The argument to
containsmust be a string - corr
- cos
- cosh
- cot
- count
- countDistinct
- Can only provide a single column
- count_distinct
- Can only provide a single column
- count_if
- covar_pop
- covar_samp
- csc
- cume_dist
- curdate
- current_date
- current_timestamp
- current_user
- date_add
- dateadd
- date_diff
- datediff
- date_format
- date_from_unix_date
- date_sub
- date_trunc
- dayofmonth
- dayofweek
- dayofyear
- degrees
- dense_rank
- desc
- desc_nulls_first
- desc_nulls_last
- e
- element_at
- Only works on strings (does not work on arrays)
- exp
- explode
- Doesn't support exploding maps
- explode_outer
- Doesn't support exploding maps
- expm1
- expr
- extract
- Some fields may start from 0 instead of 1 (like
week). Extract - factorial
- floor
- format_number
- format_string
- from_unixtime
- get_json_object
- Values are returned quoted while Spark strips the quotes
- greatest
- A null value returns a null result
- grouping
- hash
- Use a different hash algorithm than Spark
- hex
- Integers are not supported
- hour
- initcap
- input_file_name
- instr
- isnan
- isnull
- lag
- last_day
- last_value
- Must be used with a window function
- lcase
- lead
- least
- A null value returns a null result
- left
- length
- lit
- ln
- log
- log10
- log1p
- log2
- lower
- lpad
- ltrim
- make_date
- make_timestamp
- max
- max_by
- md5
- mean
- median
- min
- min_by
- minute
- month
- months_between
- nanvl
- next_day
- nth_value
- ntile
- nullifzero
- octet_length
- overlay
- percent_rank
- percentile_approx
- posexplode
- Default order of columns are
col,posinstead ofpos,col
- Default order of columns are
- posexplode_outer
- Default order of columns are
col,posinstead ofpos,col
- Default order of columns are
- position
- pow
- quarter
- radians
- rand
- rank
- regexp
- regexp_extract
- Single capture group is supported
- regexp_like
- regexp_replace
- repeat
- replace
- reverse
- Only works on strings (does not work on arrays). Use SQLFrame specific
array_reverseto reverse an array.
- Only works on strings (does not work on arrays). Use SQLFrame specific
- right
- rint
- rlike
- round
- row_number
- rpad
- rtrim
- sec
- second
- sequence
- session_user
- sha
- sha1
- shiftLeft
- shiftRight
- shiftleft
- shiftright
- sign
- signum
- sin
- sinh
- size
- slice
- sort_array
- Arrays are not allowed to have None (NULL) values
- soundex
- split
- Regular expressions not supported
- sqrt
- stddev
- stddev_pop
- stddev_samp
- struct
- substring
- substring_index
- sum
- sumDistinct
- sum_distinct
- tan
- tanh
- timestamp_add
- timestamp_seconds
- toDegrees
- toRadians
- to_date
- to_timestamp
- to_timestamp_ntz
- translate
- trim
- trunc
- Shorthand expressions not supported. Ex: Use
monthinstead ofmon
- Shorthand expressions not supported. Ex: Use
- try_divide
- try_to_timestamp
- typeof
- ucase
- unbase64
- unhex
- unix_micros
- unix_millis
- unix_seconds
- unix_timestamp
- uuid
- upper
- var_pop
- var_samp
- variance
- weekofyear
- when
- year
- zeroifnull
GroupedData Class
DataFrameReader Class
DataFrameWriter Class
- csv
- insertInto
- json
- mode
- parquet
- save
- saveAsTable
- sql
- SQLFrame Specific: Get the SQL representation of the DataFrame
SparkSession Class
DataTypes
- ArrayType
- BinaryType
- BooleanType
- ByteType
- CharType
- DataType
- DateType
- DecimalType
- DoubleType
- FloatType
- IntegerType
- LongType
- Row
- ShortType
- StringType
- StructField
- StructType
- TimestampNTZType
- TimestampType
- VarcharType
Window Class
WindowSpec Class
- orderBy
- partitionBy
- rangeBetween
- rowsBetween
- sql
- SQLFrame Specific: Get the SQL representation of the WindowSpec
Extra Functionality not Present in PySpark
SQLFrame supports the following extra functionality not in PySpark
Table Class
SQLFrame provides a Table class that supports extra DML operations like update, delete and merge. This class is returned when using the table function from the DataFrameReader class.
import google.auth
from google.api_core import client_info
from google.oauth2 import service_account
from google.cloud.bigquery.dbapi import connect
from sqlframe.bigquery import BigQuerySession
from sqlframe.base.table import WhenMatched, WhenNotMatched, WhenNotMatchedBySource
creds = service_account.Credentials.from_service_account_file("path/to/credentials.json")
client = google.cloud.bigquery.Client(
project="my-project",
credentials=creds,
location="us-central1",
client_info=client_info.ClientInfo(user_agent="sqlframe"),
)
conn = connect(client=client)
session = BigQuerySession(conn=conn, default_dataset="sqlframe.db1")
df_employee = session.createDataFrame(
[
{"id": 1, "fname": "Jack", "lname": "Shephard", "age": 37, "store_id": 1},
{"id": 2, "fname": "John", "lname": "Locke", "age": 65, "store_id": 2},
{"id": 3, "fname": "Kate", "lname": "Austen", "age": 37, "store_id": 3},
{"id": 4, "fname": "Claire", "lname": "Littleton", "age": 27, "store_id": 1},
{"id": 5, "fname": "Hugo", "lname": "Reyes", "age": 29, "store_id": 3},
]
)
df_employee.write.mode("overwrite").saveAsTable("employee")
table_employee = session.table("employee") # This object is of Type BigqueryTable
Update Statement
The update method of the Table class is equivalent to the UPDATE table_name statement used in standard sql.
# Generates a `LazyExpression` object which can be executed using the `execute` method
update_expr = table_employee.update(
set_={"age": table_employee["age"] + 1},
where=table_employee["id"] == 1,
)
# Executes the update statement
update_expr.execute()
# Show the result
table_employee.show()
Output:
+----+--------+-----------+-----+----------+
| id | fname | lname | age | store_id |
+----+--------+-----------+-----+----------+
| 1 | Jack | Shephard | 38 | 1 |
| 2 | John | Locke | 65 | 2 |
| 3 | Kate | Austen | 37 | 3 |
| 4 | Claire | Littleton | 27 | 1 |
| 5 | Hugo | Reyes | 29 | 3 |
+----+--------+-----------+-----+----------+
Delete Statement
The delete method of the Table class is equivalent to the DELETE FROM table_name statement used in standard sql.
# Generates a `LazyExpression` object which can be executed using the `execute` method
delete_expr = table_employee.delete(
where=table_employee["id"] == 1,
)
# Executes the delete statement
delete_expr.execute()
# Show the result
table_employee.show()
Output:
+----+--------+-----------+-----+----------+
| id | fname | lname | age | store_id |
+----+--------+-----------+-----+----------+
| 2 | John | Locke | 65 | 2 |
| 3 | Kate | Austen | 37 | 3 |
| 4 | Claire | Littleton | 27 | 1 |
| 5 | Hugo | Reyes | 29 | 3 |
+----+--------+-----------+-----+----------+
Merge Statement
The merge method of the Table class is equivalent to the MERGE INTO table_name statement used in some sql engines.
df_new_employee = session.createDataFrame(
[
{"id": 1, "fname": "Jack", "lname": "Shephard", "age": 38, "store_id": 1, "delete": False},
{"id": 2, "fname": "Cate", "lname": "Austen", "age": 39, "store_id": 5, "delete": False},
{"id": 5, "fname": "Ugo", "lname": "Reyes", "age": 29, "store_id": 3, "delete": True},
{"id": 6, "fname": "Sun-Hwa", "lname": "Kwon", "age": 27, "store_id": 5, "delete": False},
]
)
# Generates a `LazyExpression` object which can be executed using the `execute` method
merge_expr = table_employee.merge(
df_new_employee,
condition=table_employee["id"] == df_new_employee["id"],
clauses=[
WhenMatched(condition=table_employee["fname"] == df_new_employee["fname"]).update(
set_={
"age": df_new_employee["age"],
}
),
WhenMatched(condition=df_new_employee["delete"]).delete(),
WhenNotMatched().insert(
values={
"id": df_new_employee["id"],
"fname": df_new_employee["fname"],
"lname": df_new_employee["lname"],
"age": df_new_employee["age"],
"store_id": df_new_employee["store_id"],
}
),
],
)
# Executes the merge statement
merge_expr.execute()
# Show the result
table_employee.show()
Output:
+----+---------+-----------+-----+----------+
| id | fname | lname | age | store_id |
+----+---------+-----------+-----+----------+
| 1 | Jack | Shephard | 38 | 1 |
| 2 | John | Locke | 65 | 2 |
| 3 | Kate | Austen | 37 | 3 |
| 4 | Claire | Littleton | 27 | 1 |
| 6 | Sun-Hwa | Kwon | 27 | 5 |
+----+---------+-----------+-----+----------+
Some engines like BigQuery support an extra clause inside the merge statement which is WHEN NOT MATCHED BY SOURCE THEN DELETE.
df_new_employee = session.createDataFrame(
[
{"id": 1, "fname": "Jack", "lname": "Shephard", "age": 38, "store_id": 1},
{"id": 2, "fname": "Cate", "lname": "Austen", "age": 39, "store_id": 5},
{"id": 5, "fname": "Hugo", "lname": "Reyes", "age": 29, "store_id": 3},
{"id": 6, "fname": "Sun-Hwa", "lname": "Kwon", "age": 27, "store_id": 5},
]
)
# Generates a `LazyExpression` object which can be executed using the `execute` method
merge_expr = table_employee.merge(
df_new_employee,
condition=table_employee["id"] == df_new_employee["id"],
clauses=[
WhenMatched(condition=table_employee["fname"] == df_new_employee["fname"]).update(
set_={
"age": df_new_employee["age"],
}
),
WhenNotMatched().insert(
values={
"id": df_new_employee["id"],
"fname": df_new_employee["fname"],
"lname": df_new_employee["lname"],
"age": df_new_employee["age"],
"store_id": df_new_employee["store_id"],
}
),
WhenNotMatchedBySource().delete(),
],
)
# Executes the merge statement
merge_expr.execute()
# Show the result
table_employee.show()
Output:
+----+---------+-----------+-----+----------+
| id | fname | lname | age | store_id |
+----+---------+-----------+-----+----------+
| 1 | Jack | Shephard | 38 | 1 |
| 2 | John | Locke | 65 | 2 |
| 5 | Hugo | Reyes | 29 | 3 |
| 6 | Sun-Hwa | Kwon | 27 | 5 |
+----+---------+-----------+-----+----------+