snowflakedb / snowflakedb/snowpark-python

SNOW-1619160: Support for patching table functions

Open
#2,055 4 comments 5 reactions 1 assignee View on GitHub

@sfc-gh-jrose is already working on this.

Since Aug 13, 2024.

feature local testing
Dominant language
Python
Stars
341
Forks
155
Avg merge
4d 16h
Merged PRs (30d)
27

Description

What is the current behavior?

Table functions, including builtin snowpark ones like flatten, raise an exception in tests: NotImplementedError: [Local Testing] table_function.TableFunctionJoin is not supported. When I try to patch it, the patched function is passed a normal Column and not a ColumnEmulator with the underlying rows series:

# path/to/snowpark_job.py
from snowflake.snowpark import Session
from snowflake.snowpark.functions import flatten


def snowpark_job(session: Session):
    df = session.create_dataframe([[[1, 2, 3], [4, 5], []]], schema=["lists"])
    flattened_df = df.select(flatten(df.lists))
    flattened_df.show()

Nor can I patch the builtin table function. As far as I can tell, patching functions using snowflake.snowpark.mock does not support returning 0, 1, or many rows per input row. But more specifically, when I put a debugger inside patch_flatten() it is being passed a normal Column and not a ColumnEmulator so I can't interact with the underlying series of rows.

# path/to/test.py
from unittest import mock
from uuid import uuid4

from snowflake.snowpark import Session
from snowflake.snowpark.functions import flatten
from snowflake.snowpark.mock import ColumnEmulator, ColumnType
from snowflake.snowpark.mock import patch as snowpark_patch
from snowflake.snowpark.types import IntegerType

from path.to.snowpark_job import snowpark_job


@snowpark_patch(flatten)
def patch_flatten(column: ColumnEmulator, *args, **kwargs) -> ColumnEmulator:
    ret_data = [integer for row in column for integer in row]
    ret_column = ColumnEmulator(data=ret_data)
    ret_column.sf_type = ColumnType(IntegerType(), True)
    return ret_column


@mock.patch(
    "path.to.snowpark_job.flatten",
    new=patch_flatten,
)
def test_snowpark_job():
    session = Session.builder.config("local_testing", True).create()
    snowpark_job(session)

What is the desired behavior?

Ideally builtin table functions like flatten have test implementations, but more generally it might be more practical to support patching table functions.

If this is not an existing feature in snowflake-snowpark-python. How would this impact/improve non local testing mode?

Table functions are a fairly common use case when transforming semi structured data into structured data, so it would make the library more robust.

References, Other Background

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.