-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathtest_trace_sql_errors_to_df.py
More file actions
487 lines (423 loc) · 18.5 KB
/
Copy pathtest_trace_sql_errors_to_df.py
File metadata and controls
487 lines (423 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#
# Copyright (c) 2012-2025 Snowflake Computing Inc. All rights reserved.
#
import os
import sys
import pytest
import snowflake.snowpark.context as context
from snowflake.snowpark._internal.utils import set_ast_state, AstFlagSource
from snowflake.snowpark.exceptions import SnowparkSQLException
from snowflake.snowpark.functions import (
col,
sum,
)
from snowflake.snowpark._internal.utils import TempObjectType
from snowflake.snowpark.window import Window
from tests.utils import Utils
pytestmark = [
pytest.mark.xfail(
"config.getoption('local_testing_mode', default=False)",
reason="This is a SQL test suite",
run=False,
),
pytest.mark.skipif(
sys.version_info < (3, 11),
reason="Line numbers are flaky before Python 3.11",
run=False,
),
pytest.mark.skipif(
"FIPS_TEST" in os.environ,
reason="SNOW-2204213: Reading source file location is not correct in FIPS mode",
run=False,
),
]
@pytest.fixture(autouse=True)
def setup(request, session):
original = session.ast_enabled
set_ast_state(AstFlagSource.TEST, True)
context.configure_development_features(enable_trace_sql_errors_to_dataframe=True)
yield
context.configure_development_features(enable_trace_sql_errors_to_dataframe=False)
set_ast_state(AstFlagSource.TEST, original)
def test_python_source_location_in_sql_error(session):
df = session.create_dataframe([[1, 2], [3, 4]], schema=["a", "b"])
df1 = df.select(col("a").alias("x"), col("b").alias("y"))
df2 = df1.select(col("x") + col("a"))
with pytest.raises(SnowparkSQLException) as ex:
df2.collect()
assert "SQL compilation error corresponds to Python source" in str(
ex.value.debug_context
)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 6}" in str(ex.value.debug_context)
def test_python_source_location_in_session_sql(session):
if not session.sql_simplifier_enabled:
pytest.skip("SQL simplifier must be enabled for this test")
df3 = session.sql(
"SELECT a, b, c + '5' AS invalid_operation FROM (select 1 as a, 2 as b, array_construct(1, 2, 3) as c) WHERE a > 0"
)
with pytest.raises(SnowparkSQLException) as ex:
df3.show()
assert "SQL compilation error corresponds to Python source" in str(
ex.value.debug_context
)
line_number = Utils.get_current_line_number_sys()
assert f"lines {line_number - 8}-{line_number - 6}" in str(ex.value.debug_context)
def test_join_ambiguous_column_error(session):
df1 = session.create_dataframe([[1, "a"], [2, "b"]], schema=["id", "value"])
df2 = session.create_dataframe([[1, "x"], [2, "y"]], schema=["id", "data"])
joined = df1.join(df2, df1["id"] == df2["id"])
df_error = joined.select(col("id"))
with pytest.raises(SnowparkSQLException) as ex:
df_error.collect()
assert "SQL compilation error corresponds to Python source" in str(
ex.value.debug_context
)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 6}" in str(ex.value.debug_context)
def test_window_function_error(session):
df = session.create_dataframe(
[[1, 10], [2, 20], [3, 30]], schema=["group_id", "value"]
)
window_spec = Window.partition_by("nonexistent_column").order_by("value")
df_error = df.select(col("value"), sum(col("value")).over(window_spec))
with pytest.raises(SnowparkSQLException) as ex:
df_error.collect()
assert "SQL compilation error corresponds to Python source" in str(
ex.value.debug_context
)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 6}" in str(ex.value.debug_context)
def test_invalid_identifier_error_message(session):
# we reuse tests from test_snowflake_plan_suite.py since AST is not enabled in that test suite
df = session.create_dataframe([[1, 2, 3]], schema=['"abc"', '"abd"', '"def"'])
with pytest.raises(SnowparkSQLException) as ex:
df.select("abc").collect()
assert ex.value.sql_error_code == 904
assert "invalid identifier 'ABC'" in str(ex.value)
assert (
"There are existing quoted column identifiers: ['\"abc\"', '\"abd\"', '\"def\"']"
in str(ex.value)
)
assert "Do you mean '\"abc\"'?" in str(ex.value)
assert "SQL compilation error corresponds to Python source" in str(ex.value)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 9}" in str(ex.value)
with pytest.raises(SnowparkSQLException) as ex:
df.select("_ab").collect()
assert "invalid identifier '_AB'" in str(ex.value)
assert (
"There are existing quoted column identifiers: ['\"abc\"', '\"abd\"', '\"def\"']"
in str(ex.value)
)
assert "Do you mean '\"abd\"' or '\"abc\"'?" in str(ex.value)
assert "SQL compilation error corresponds to Python source" in str(ex.value)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 8}" in str(ex.value)
with pytest.raises(SnowparkSQLException) as ex:
df.select('"abC"').collect()
assert "invalid identifier '\"abC\"'" in str(ex.value)
assert (
"There are existing quoted column identifiers: ['\"abc\"', '\"abd\"', '\"def\"']"
in str(ex.value)
)
assert "Do you mean" not in str(ex.value)
assert "SQL compilation error corresponds to Python source" in str(ex.value)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 8}" in str(ex.value)
df = session.create_dataframe([list(range(20))], schema=[str(i) for i in range(20)])
with pytest.raises(
SnowparkSQLException, match="There are existing quoted column identifiers:*..."
) as ex:
df.select("20").collect()
assert "SQL compilation error corresponds to Python source" in str(ex.value)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 2}" in str(ex.value)
df = session.create_dataframe([1, 2, 3], schema=["A"])
with pytest.raises(
SnowparkSQLException, match="There are existing quoted column identifiers:*..."
) as ex:
df.select("B").schema
assert "There are existing quoted column identifiers: ['\"A\"']" in str(ex.value)
assert "SQL compilation error corresponds to Python source" in str(ex.value)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 3}" in str(ex.value)
def test_missing_table_with_session_table(session):
with pytest.raises(SnowparkSQLException) as ex:
session.table("NON_EXISTENT_TABLE").collect()
assert "Missing object 'NON_EXISTENT_TABLE' corresponds to Python source" in str(
ex.value.debug_context
)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 5}" in str(ex.value)
def test_missing_table_context_with_session_sql(session):
with pytest.raises(SnowparkSQLException) as ex:
session.sql("SELECT * FROM NON_EXISTENT_TABLE").collect()
assert "Missing object 'NON_EXISTENT_TABLE' corresponds to Python source" in str(
ex.value.debug_context
)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 5}" in str(ex.value.debug_context)
@pytest.mark.parametrize(
"operation_name,operation_func",
[
(
"select",
lambda session: session.table("NON_EXISTENT_TABLE")
.schema("a", "b")
.select(col("a")),
),
(
"filter",
lambda session: session.table("NON_EXISTENT_TABLE")
.schema("a", "b")
.filter(col("a") > 0),
),
(
"sort",
lambda session: session.table("NON_EXISTENT_TABLE")
.schema("a", "b")
.sort(col("a")),
),
(
"group_by",
lambda session: session.table("NON_EXISTENT_TABLE")
.schema("a", "b")
.group_by(col("a")),
),
(
"join",
lambda session: session.table("NON_EXISTENT_TABLE")
.schema("a", "b")
.join(
session.create_dataframe([[1, 2]], schema=["x", "y"]),
col("a") == col("x"),
),
),
(
"union",
lambda session: session.table("NON_EXISTENT_TABLE")
.schema("a", "b")
.union(session.table("ANOTHER_NON_EXISTENT_TABLE")),
),
],
)
def test_missing_table_with_dataframe_operations(
session, operation_name, operation_func
):
"""Test that missing table errors are traced properly across various DataFrame operations."""
with pytest.raises(SnowparkSQLException) as ex:
df = operation_func(session)
df.collect()
assert "Missing object 'NON_EXISTENT_TABLE' corresponds to Python source" in str(
ex.value.debug_context
), f"Missing object trace not found for operation: {operation_name}"
def test_existing_table_with_save_as_table(session):
table_name = Utils.random_name_for_temp_object(TempObjectType.TABLE)
df = session.create_dataframe([{"a": 1, "b": 2}])
df.write.save_as_table(table_name, mode="overwrite")
with pytest.raises(SnowparkSQLException) as ex:
df.write.save_as_table(table_name)
assert f"Object '{table_name}' was first referenced" in str(ex.value.debug_context)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 5}" in str(ex.value.debug_context)
Utils.drop_table(session, table_name)
@pytest.mark.parametrize(
"create_sql_template,conflicting_sql_template,object_type",
[
(
"CREATE TABLE {name} (a INT, b INT)",
"CREATE TABLE {name} (a INT, b INT)",
"TABLE",
),
(
"CREATE OR REPLACE TABLE {name} (a INT, b INT)",
"CREATE TABLE {name} (a INT, b INT)",
"TABLE",
),
(
"CREATE TEMP TABLE {name} (a INT, b INT)",
"CREATE TEMP TABLE {name} (a INT, b INT)",
"TABLE",
),
(
"CREATE TEMPORARY TABLE {name} (a INT, b INT)",
"CREATE TEMPORARY TABLE {name} (a INT, b INT)",
"TABLE",
),
(
"CREATE TABLE IF NOT EXISTS {name} (a INT, b INT)",
"CREATE TABLE {name} (a INT, b INT)",
"TABLE",
),
(
"CREATE VIEW {name} AS SELECT 1 AS a, 2 AS b",
"CREATE VIEW {name} AS SELECT 3 AS a, 4 AS b",
"VIEW",
),
(
"CREATE OR REPLACE VIEW {name} AS SELECT 1 AS a, 2 AS b",
"CREATE VIEW {name} AS SELECT 3 AS a, 4 AS b",
"VIEW",
),
(
"CREATE TEMP VIEW {name} AS SELECT 1 AS a, 2 AS b",
"CREATE TEMP VIEW {name} AS SELECT 3 AS a, 4 AS b",
"VIEW",
),
(
"CREATE VIEW IF NOT EXISTS {name} AS SELECT 1 AS a, 2 AS b",
"CREATE VIEW {name} AS SELECT 3 AS a, 4 AS b",
"VIEW",
),
# Quoted table names
(
'CREATE TABLE "{name}" (a INT, b INT)',
'CREATE TABLE "{name}" (a INT, b INT)',
"TABLE",
),
# Mixed quoted/unquoted (should still match)
(
'CREATE TABLE "{name}" (a INT, b INT)',
"CREATE TABLE {name} (a INT, b INT)",
"TABLE",
),
],
)
def test_existing_object_with_session_sql_create_statements(
session, create_sql_template, conflicting_sql_template, object_type
):
"""Test that get_existing_object_context properly identifies objects created via session.sql() with various CREATE statement patterns."""
if object_type == "TABLE":
object_name = Utils.random_name_for_temp_object(TempObjectType.TABLE)
else:
object_name = Utils.random_name_for_temp_object(TempObjectType.VIEW)
session.sql(create_sql_template.format(name=object_name)).collect()
# Second CREATE statement should fail with "already exists" error
with pytest.raises(SnowparkSQLException) as ex:
session.sql(conflicting_sql_template.format(name=object_name)).collect()
expected_message = f"Object '{object_name}' was first referenced"
assert expected_message in str(
ex.value.debug_context
), f"Expected message '{expected_message}' not found in debug context: {ex.value.debug_context}"
if object_type == "TABLE":
Utils.drop_table(session, object_name)
else:
Utils.drop_view(session, object_name)
def test_existing_object_with_schema_qualified_names(session):
temp_table_name = Utils.random_name_for_temp_object(TempObjectType.TABLE)
db = session.get_current_database()
sc = session.get_current_schema()
df = session.create_dataframe([{"a": 1, "b": 2}])
df.write.save_as_table([db, sc, temp_table_name], mode="overwrite")
df2 = session.create_dataframe([{"a": 3, "b": 4}])
with pytest.raises(SnowparkSQLException) as ex:
df2.write.save_as_table([db, sc, temp_table_name], mode="errorifexists")
db = db.strip('"')
sc = sc.strip('"')
expected_message = f"Object '{db}.{sc}.{temp_table_name}' was first referenced"
assert expected_message in str(ex.value.debug_context)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 9}" in str(ex.value.debug_context)
Utils.drop_table(session, temp_table_name)
def test_existing_object_with_schema_qualified_names_using_session_sql(session):
temp_table_name = Utils.random_name_for_temp_object(TempObjectType.TABLE)
db = session.get_current_database()
sc = session.get_current_schema()
session.sql(f"CREATE TABLE {db}.{sc}.{temp_table_name} (a INT, b INT)").collect()
with pytest.raises(SnowparkSQLException) as ex:
session.sql(
f"CREATE TABLE {db}.{sc}.{temp_table_name} (a INT, b INT)"
).collect()
db = db.strip('"')
sc = sc.strip('"')
expected_message = f"Object '{db}.{sc}.{temp_table_name}' was first referenced"
assert expected_message in str(ex.value.debug_context)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 11}" in str(ex.value.debug_context)
Utils.drop_table(session, temp_table_name)
def test_existing_view_with_schema_qualified_names_using_session_sql(session):
temp_view_name = Utils.random_name_for_temp_object(TempObjectType.VIEW)
db = session.get_current_database()
sc = session.get_current_schema()
session.sql(
f"CREATE VIEW {db}.{sc}.{temp_view_name} AS SELECT 1 AS a, 2 AS b"
).collect()
with pytest.raises(SnowparkSQLException) as ex:
session.sql(
f"CREATE VIEW {db}.{sc}.{temp_view_name} AS SELECT 3 AS a, 4 AS b"
).collect()
db = db.strip('"')
sc = sc.strip('"')
expected_message = f"Object '{db}.{sc}.{temp_view_name}' was first referenced"
assert expected_message in str(ex.value.debug_context)
line_number = Utils.get_current_line_number_sys()
assert f"lines {line_number - 13}-{line_number - 11}" in str(ex.value.debug_context)
Utils.drop_view(session, temp_view_name)
def test_existing_view_with_schema_qualified_names_using_dataframe_methods(session):
temp_view_name = Utils.random_name_for_temp_object(TempObjectType.VIEW)
db = session.get_current_database()
sc = session.get_current_schema()
df = session.create_dataframe([{"a": 1, "b": 2}])
df.create_temp_view([db, sc, temp_view_name])
df2 = session.create_dataframe([{"a": 3, "b": 4}])
with pytest.raises(SnowparkSQLException) as ex:
df2.create_temp_view([db, sc, temp_view_name])
db = db.strip('"')
sc = sc.strip('"')
expected_message = f"Object '{db}.{sc}.{temp_view_name}' was first referenced"
assert expected_message in str(ex.value.debug_context)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 9}" in str(ex.value.debug_context)
Utils.drop_view(session, temp_view_name)
def test_existing_object_debug_context_when_ast_disabled(session):
"""Test that the debug context function gracefully handles the case when AST is disabled."""
original_ast_state = session.ast_enabled
set_ast_state(AstFlagSource.TEST, False)
temp_table_name = Utils.random_name_for_temp_object(TempObjectType.TABLE)
session.sql(f"CREATE TABLE {temp_table_name} (a INT, b INT)").collect()
with pytest.raises(SnowparkSQLException) as ex:
session.sql(f"CREATE TABLE {temp_table_name} (a INT, b INT)").collect()
assert f"Object '{temp_table_name}' already exists" in str(ex.value)
Utils.drop_table(session, temp_table_name)
set_ast_state(AstFlagSource.TEST, original_ast_state)
def test_existing_table_with_dataframe_write_operations(session):
table_name = Utils.random_name_for_temp_object(TempObjectType.TABLE)
df1 = session.create_dataframe([{"a": 1, "b": 2}])
df1.write.save_as_table(table_name, mode="overwrite")
df2 = session.create_dataframe([{"a": 3, "b": 4}])
with pytest.raises(SnowparkSQLException) as ex:
df2.write.save_as_table(table_name, mode="errorifexists")
assert f"Object '{table_name}' was first referenced" in str(ex.value.debug_context)
line_number = Utils.get_current_line_number_sys()
assert f"line {line_number - 6}" in str(ex.value.debug_context)
Utils.drop_table(session, table_name)
@pytest.mark.parametrize(
"view_method",
[
"create_temp_view",
"create_or_replace_view",
"create_or_replace_temp_view",
],
)
def test_existing_view_with_all_dataframe_methods(session, view_method):
"""Test that get_existing_object_context works with all DataFrame view creation methods."""
view_name = Utils.random_name_for_temp_object(TempObjectType.VIEW)
df1 = session.create_dataframe([{"a": 1, "b": 2}])
df2 = session.create_dataframe([{"a": 3, "b": 4}])
getattr(df1, view_method)(view_name)
if view_method == "create_or_replace_view":
with pytest.raises(SnowparkSQLException) as ex:
session.sql(f"CREATE VIEW {view_name} AS SELECT 5 AS a, 6 AS b").collect()
elif view_method == "create_or_replace_temp_view":
with pytest.raises(SnowparkSQLException) as ex:
session.sql(
f"CREATE TEMP VIEW {view_name} AS SELECT 5 AS a, 6 AS b"
).collect()
else:
with pytest.raises(SnowparkSQLException) as ex:
getattr(df2, view_method)(view_name)
assert f"Object '{view_name}' was first referenced" in str(
ex.value.debug_context
), f"Expected object reference not found in debug context for {view_method}: {ex.value.debug_context}"
Utils.drop_view(session, view_name)