# pysparky.spark_ext

*module*

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/spark_ext.py#L1-L1)

## pysparky.spark_ext.check_table_exists

*function*

```python
def check_table_exists(spark: SparkSession, catalog: str, database: str, table_name: str) -> bool
```

Checks if a specific table exists in the given catalog.

**Parameters**

- `spark` (`SparkSession`) — The Spark session.
- `catalog` (`str`) — The catalog name.
- `table_name` (`str`) — The name of the table to check.

**Returns**

- `bool` (`bool`) — True if the table exists, False otherwise.

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/spark_ext.py#L213-L233)

## pysparky.spark_ext.column_function

*function*

```python
def column_function(spark: SparkSession, column_obj: Column) -> DataFrame
```

Evaluates a Column expression in the context of a single-row DataFrame.

This function creates a DataFrame with a single row and applies the given Column
expression to it. This is particularly useful for testing Column expressions,
evaluating complex transformations, or creating sample data based on Column operations.

**Parameters**

- `spark` (`pyspark.sql.SparkSession`) — The SparkSession object.
- `column_obj` (`pyspark.sql.Column`) — The Column object or expression to evaluate.

**Returns**

- (`DataFrame`) — pyspark.sql.DataFrame: A single-row DataFrame containing the result of the Column expression.

> **Example**
>
> ``` py
> from pyspark.sql import SparkSession, functions as F
> spark = SparkSession.builder.getOrCreate()
> ```
> # Simple column expression
> ``` py
> result = spark.column_function(F.lit("Hello, World!"))
> result.show()
> +-------------+
> |         col0|
> +-------------+
> |Hello, World!|
> +-------------+
> ```
> 
> # Complex column expression
> ``` py
> import datetime
> complex_col = F.when(F.current_date() > F.lit(datetime.date(2023, 1, 1)), "Future")
> ...                .otherwise("Past")
> result = spark.column_function(complex_col)
> result.show()
> +------+
> |  col0|
> +------+
> |Future|
> +------+
> ```
> 
> # Using with user-defined functions (UDFs)
> ``` py
> from pyspark.sql.types import IntegerType
> square_udf = F.udf(lambda x: x * x, IntegerType())
> result = spark.column_function(square_udf(F.lit(5)))
> result.show()
> +----+
> |col0|
> +----+
> |  25|
> +----+
> ```

> **Notes**
>
> - This function is particularly useful for debugging or testing Column expressions
>   without the need to create a full DataFrame.
> - The resulting DataFrame will always have a single column named 'col0' unless
>   the input Column object has a specific alias.
> - Be cautious when using this with resource-intensive operations, as it still
>   creates a distributed DataFrame operation.

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/spark_ext.py#L9-L76)

## pysparky.spark_ext.convert_1d_list_to_dataframe

*function*

```python
def convert_1d_list_to_dataframe(spark: SparkSession, list_: list[Any], column_names: str | list[str], axis: str = 'column') -> DataFrame
```

Converts a 1-dimensional list into a PySpark DataFrame.

This function takes a 1-dimensional list and converts it into a PySpark DataFrame
with the specified column names. The list can be converted into a DataFrame with
either a single column or a single row, based on the specified axis.

**Parameters**

- `spark` (`SparkSession`) — The Spark session to use for creating the DataFrame.
- `list_` (`list`) — The 1-dimensional list to convert.
- `column_names` (`str or list of str`) — The name(s) of the column(s) for the DataFrame.
- `axis` (`str`) (default: `'column'`) — Specifies whether to convert the list into a single column or a single row.         Acceptable values are "column" (default) and "row".

**Returns**

- `DataFrame` (`DataFrame`) — A PySpark DataFrame created from the 1-dimensional list.

**Raises**

- `AttributeError` — If the axis parameter is not "column" or "row".

> **Example**
>
> ``` py
> >>> spark = SparkSession.builder.appName("example").getOrCreate()
> >>> list_ = [1, 2, 3, 4]
> >>> column_names = ["numbers"]
> >>> df = convert_1d_list_to_dataframe(spark, list_, column_names, axis="column")
> >>> df.show()
> +-------+
> |numbers|
> +-------+
> |      1|
> |      2|
> |      3|
> |      4|
> +-------+
> >>> column_names = ["ID1", "ID2", "ID3", "ID4"]
> >>> df = convert_1d_list_to_dataframe(spark, list_, column_names, axis="row")
> >>> df.show()
> +---+---+---+---+
> |ID1|ID2|ID3|ID4|
> +---+---+---+---+
> |  1|  2|  3|  4|
> +---+---+---+---+
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/spark_ext.py#L126-L193)

## pysparky.spark_ext.convert_dict_to_dataframe

*function*

```python
def convert_dict_to_dataframe(spark: SparkSession, dict_: dict[str, Any], column_names: list[str], explode: bool = False) -> DataFrame
```

Transforms a dictionary with list values into a Spark DataFrame.

**Parameters**

- `dict_` (`dict`) — The dictionary to transform. Keys will become the first column, and values will become the second column.
- `column_names` (`list[str]`) — A list containing the names of the columns.

**Returns**

- (`DataFrame`) — pyspark.sql.DataFrame: A DataFrame with the dictionary keys and their corresponding exploded list values.

> **Example**
>
> ``` py
> datadict_ = {
>     "key1": 1,
>     "key2": 2
> }
> column_names = ["keys", "values"]
> df = convert_dict_to_dataframe(datadict_, column_names)
> display(df)
> # key1,1
> # key2,2
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/spark_ext.py#L79-L123)

## pysparky.spark_ext.createDataFrame_from_dict

*function*

```python
def createDataFrame_from_dict(spark: SparkSession, dict_: dict) -> DataFrame
```

Creates a Spark DataFrame from a dictionary in a pandas-like style.

**Parameters**

- `spark` (`SparkSession`) — The SparkSession object.
- `dict_` (`dict`) — The dictionary to convert, where keys are column names and values are lists of column data.

**Returns**

- `DataFrame` (`DataFrame`) — The resulting Spark DataFrame.

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/spark_ext.py#L196-L210)
