Built with Tutify.io

    AIStudyLabs

    SQL - Built-in Functions

    SQL - Built-in Functions

    Introduction

    In this lesson, we will explore the various built-in functions available in Spark SQL using PySpark. Understanding these functions is essential for effectively transforming and manipulating data within Spark applications. We will delve into string, date, and numeric functions, as well as null handling functions and conditional logic using the when statement. Additionally, we will discuss expression-based transformations which can greatly enhance data processing tasks.

    String Functions

    String functions in PySpark allow users to perform a variety of operations on string data. Here are some commonly used string functions:

    1. CONCAT

    The concat function is used to concatenate two or more strings together.

    from pyspark.sql import SparkSession
    from pyspark.sql.functions import concat, lit
    
    spark = SparkSession.builder.appName("example").getOrCreate()
    employees_df = spark.createDataFrame([
        ("John", "Doe"),
        ("Jane", "Smith")
    ], ["first_name", "last_name"])
    
    employees_df.select(concat(employees_df.first_name, lit(' '), employees_df.last_name).alias("full_name")).show()
    

    This command combines the first and last names of employees into a single full_name field.

    2. LENGTH

    The length function returns the number of characters in a string.

    from pyspark.sql.functions import length
    
    employees_df.select(length(employees_df.first_name).alias("name_length")).show()
    

    This will yield the length of each employee's first name.

    3. UPPER and LOWER

    The upper and lower functions convert a string to upper or lower case, respectively.

    from pyspark.sql.functions import upper, lower
    
    employees_df.select(upper(employees_df.first_name).alias("upper_name"), lower(employees_df.last_name).alias("lower_name")).show()
    

    These examples will return the first name in uppercase and the last name in lowercase.

    Date Functions

    Date functions in PySpark facilitate the manipulation of date and time values. Important date functions include:

    1. CURRENT_DATE

    The current_date function returns the current date.

    from pyspark.sql.functions import current_date
    
    employees_df.select(current_date().alias("today")).show()
    

    This returns today's date from the system.

    2. DATE_ADD

    The date_add function adds a specified number of days to a given date.

    from pyspark.sql.functions import date_add
    
    employees_df = spark.createDataFrame([
        ("2023-01-01",),
        ("2023-02-01",)
    ], ["hire_date"])
    
    employees_df.select(date_add(employees_df.hire_date, 30).alias("promotion_date")).show()
    

    In this example, we calculate the promotion date by adding 30 days to the hire date.

    3. DATEDIFF

    The datediff function computes the difference in days between two dates.

    from pyspark.sql.functions import datediff
    
    employees_df.select(datediff(current_date(), employees_df.hire_date).alias("days_since_hired")).show()
    

    This calculation will return the number of days since an employee was hired.

    Numeric Functions

    Numerical functions perform calculations on numeric data types. Key numeric functions include:

    1. ABS

    The abs function returns the absolute value of a number.

    from pyspark.sql.functions import abs
    
    employees_df = spark.createDataFrame([
        (1, -5000),
        (2, 7500)
    ], ["employee_id", "salary"])
    
    employees_df.select("employee_id", abs(employees_df.salary).alias("abs_salary")).show()
    

    Here, the absolute value of the salary will be displayed.

    2. ROUND

    The round function is used to round a numeric value to a specified number of decimal places.

    from pyspark.sql.functions import round
    
    employees_df.select("employee_id", round(employees_df.salary, 2).alias("rounded_salary")).show()
    

    This results in the salary being rounded to two decimal places.

    3. SUM

    The sum function provides the total of a numeric column.

    employees_df.groupBy().sum("salary").show()
    

    This statement will yield the total salary of all employees.

    Null Handling Functions

    Handling null values is crucial in data processing. PySpark provides various functions for this purpose:

    1. COALESCE

    The coalesce function returns the first non-null value among its arguments.

    from pyspark.sql.functions import coalesce
    
    employees_df.select("employee_id", coalesce(employees_df.salary, lit(0)).alias("salary")).show()
    

    In this example, if an employee's salary is null, 0 is returned instead.

    2. IFNULL (Using when and isNull)

    PySpark does not have an IFNULL directly, but similar functionality can be achieved using when and isNull.

    from pyspark.sql.functions import when, col
    
    employees_df.select("employee_id", when(employees_df.salary.isNull(), 'Not Specified').otherwise(employees_df.salary).alias("salary_info")).show()
    

    This will specify 'Not Specified' in the case of a null salary.

    Conditional Logic with CASE WHEN

    The when construct allows for conditional logic in PySpark DataFrame operations. This is highly useful for categorizing data based on specific conditions.

    Example of when

    employees_df.select("employee_id",  
                        when(employees_df.salary <= 40000, 'Low')
                        .when(employees_df.salary <= 80000, 'Medium')
                        .otherwise('High')
                        .alias("salary_category")).show()
    

    This query categorizes employees into 'Low', 'Medium', and 'High' salary brackets based on their salary values.

    Expression-based Transformations

    Expression-based transformations leverage built-in functions and custom expressions to create new columns or data insights from existing data. These transformations are useful in data engineering and analysis to generate additional value from raw data.

    Example of Expression-based Transformation

    employees_df.select("employee_id", "salary",  
                        round(employees_df.salary * 0.10, 2).alias("bonus")).show()
    

    In this case, we compute a 10% bonus based on an employee's salary, rounded to two decimal points.

    Summary

    In this lesson, we explored the extensive built-in functions that PySpark provides, enabling users to manipulate strings, dates, and numbers adeptly in PySpark. We discussed null handling functionalities to safeguard data quality, employing coalesce and when with isNull. The use of conditional logic via when as well as expression-based transformations enhances data processing capabilities in PySpark, allowing for insightful data analysis and reporting. Understanding these functions equips learners to effectively utilize PySpark in data engineering roles.

    Introduction to PySpark for Data Engineering — PySpark SQL

    Introduction to PySpark

    Setup PySpark Environment

    SparkSession & SparkContext

    Resilient Distributed Datasets (RDDs)

    Apache Spark Concepts

    Cache, Persistence and Shared Variables

    DataFrame Introduction

    DataFrame Narrow Transformation

    DataFrame Wider Transformation

    DataFrame Actions

    PySpark SQL Functions

    Advanced DataFrame Features

    PySpark SQL

    Spark Read & Write CSV Files

    Working with JSON Files

    Working with Parquet Files

    What Next?

    Tutorial Not Found