Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-981562: Expose is_temp_table_for_cleanup to Session.table #2784

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/snowflake/snowpark/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2253,7 +2253,12 @@ def update_query_tag(self, tag: dict) -> None:
)

@publicapi
def table(self, name: Union[str, Iterable[str]], _emit_ast: bool = True) -> Table:
def table(
self,
name: Union[str, Iterable[str]],
is_temp_table_for_cleanup: bool = False,
_emit_ast: bool = True,
) -> Table:
"""
Returns a Table that points the specified table.

Expand Down Expand Up @@ -2287,13 +2292,20 @@ def table(self, name: Union[str, Iterable[str]], _emit_ast: bool = True) -> Tabl
elif isinstance(name, Iterable):
ast.name.sp_table_name_structured.name.extend(name)
ast.variant.sp_session_table = True
ast.is_temp_table_for_cleanup = is_temp_table_for_cleanup
else:
stmt = None

if not isinstance(name, str) and isinstance(name, Iterable):
name = ".".join(name)
validate_object_name(name)
t = Table(name, session=self, _ast_stmt=stmt, _emit_ast=_emit_ast)
t = Table(
name,
session=self,
is_temp_table_for_cleanup=is_temp_table_for_cleanup,
_ast_stmt=stmt,
_emit_ast=_emit_ast,
)
# Replace API call origin for table
set_api_call_source(t, "Session.table")
return t
Expand Down
174 changes: 174 additions & 0 deletions tests/ast/data/session_table_temp_table_cleanup.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
## TEST CASE

df1 = session.table(f"mock_schema.{tables.table1}", is_temp_table_for_cleanup=True)
df2 = session.table(f"mock_schema.{tables.table1}", is_temp_table_for_cleanup=False)
df = df1.union_all(df2).select("num")

## EXPECTED UNPARSER OUTPUT

df1 = session.table("mock_schema.table1", is_temp_table_for_cleanup=True)

df2 = session.table("mock_schema.table1")

df = df1.union_all(df2)

df = df.select(col("num"))

## EXPECTED ENCODED AST

body {
assign {
expr {
sp_table {
is_temp_table_for_cleanup: true
name {
sp_table_name_flat {
name: "mock_schema.table1"
}
}
src {
file: "SRC_POSITION_TEST_MODE"
start_line: 25
}
variant {
sp_session_table: true
}
}
}
symbol {
value: "df1"
}
uid: 1
var_id {
bitfield1: 1
}
}
}
body {
assign {
expr {
sp_table {
name {
sp_table_name_flat {
name: "mock_schema.table1"
}
}
src {
file: "SRC_POSITION_TEST_MODE"
start_line: 26
}
variant {
sp_session_table: true
}
}
}
symbol {
value: "df2"
}
uid: 2
var_id {
bitfield1: 2
}
}
}
body {
assign {
expr {
sp_dataframe_union_all {
df {
sp_dataframe_ref {
id {
bitfield1: 1
}
}
}
other {
sp_dataframe_ref {
id {
bitfield1: 2
}
}
}
src {
file: "SRC_POSITION_TEST_MODE"
start_line: 27
}
}
}
symbol {
value: "df"
}
uid: 3
var_id {
bitfield1: 3
}
}
}
body {
assign {
expr {
sp_dataframe_select__columns {
cols {
apply_expr {
fn {
builtin_fn {
name {
fn_name_flat {
name: "col"
}
}
}
}
pos_args {
string_val {
src {
file: "SRC_POSITION_TEST_MODE"
start_line: 27
}
v: "num"
}
}
src {
file: "SRC_POSITION_TEST_MODE"
start_line: 27
}
}
}
df {
sp_dataframe_ref {
id {
bitfield1: 3
}
}
}
src {
file: "SRC_POSITION_TEST_MODE"
start_line: 27
}
variadic: true
}
}
symbol {
value: "df"
}
uid: 4
var_id {
bitfield1: 4
}
}
}
client_ast_version: 1
client_language {
python_language {
version {
label: "final"
major: 3
minor: 9
patch: 1
}
}
}
client_version {
major: 1
minor: 26
}
Loading