Skip to content

Commit 04a52c5

Browse files
fix(staging): recognize transfers after nested SQL comments
Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>
1 parent 5df354a commit 04a52c5

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

‎src/databricks/sql/backend/kernel/client.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,15 @@ def _strip_leading_sql_comments(sql: str) -> str:
9797
nl = sql.find("\n", i)
9898
i = n if nl == -1 else nl + 1
9999
elif sql.startswith("/*", i):
100-
# Block comment: skip to closing */ (or end if unterminated).
101-
close = sql.find("*/", i + 2)
102-
i = n if close == -1 else close + 2
100+
depth = 1
101+
i += 2
102+
for delimiter in re.finditer(r"/\*|\*/", sql[i:]):
103+
depth += 1 if delimiter.group() == "/*" else -1
104+
if depth == 0:
105+
i += delimiter.end()
106+
break
107+
else:
108+
i = n
103109
else:
104110
break
105111
return sql[i:]

‎tests/unit/test_kernel_client.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,10 @@ def test_execute_command_forwards_row_limit(row_limit):
822822
("PUT '/tmp/files/input' INTO '/Volumes/c/s/v/input'", True),
823823
("GET '/Volumes/c/s/v/input' TO '/tmp/files/output'", True),
824824
("/* download */ GET '/Volumes/c/s/v/input' TO '/tmp/files/output'", True),
825+
(
826+
"/* outer /* inner */ outer */ GET '/Volumes/c/s/v/input' TO '/tmp/files/output'",
827+
True,
828+
),
825829
("REMOVE '/Volumes/c/s/v/input'", False),
826830
("SELECT 1", False),
827831
],
@@ -918,6 +922,9 @@ def execute():
918922
("-- upload the file\nPUT '/f' INTO '/v'", "PUT"),
919923
("/* staging */ PUT '/f' INTO '/v'", "PUT"),
920924
("/* c1 */\n -- c2\n get '/v' to '/f'", "GET"),
925+
("/* outer /* inner */ outer */ PUT '/f' INTO '/v'", "PUT"),
926+
("/* a /* b /* c */ b */ a */ GET '/v' TO '/f'", "GET"),
927+
("/* outer /* closed */ PUT '/f' INTO '/v'", None),
921928
(" \n\t PUT '/f' INTO '/v'", "PUT"),
922929
("SELECT 'GET' AS x", None),
923930
("SELECT * FROM puts", None),
@@ -944,6 +951,7 @@ def test_staging_verb_matches_only_the_leading_statement(operation, expected):
944951
"PUT '__input_stream__'INTO '/Volumes/c/s/v/file'",
945952
'PUT"__input_stream__"INTO "/Volumes/c/s/v/file"',
946953
"/* upload */ PUT '__input_stream__' INTO '/Volumes/c/s/v/file'",
954+
"/* outer /* inner */ outer */ PUT '__input_stream__' INTO '/Volumes/c/s/v/file'",
947955
"PUT'__input_stream__' INTO '/Volumes/c/s/v/file'",
948956
"/* upload */ PUT\"__input_stream__\" INTO '/Volumes/c/s/v/file'",
949957
],

0 commit comments

Comments
 (0)