File size: 6,133 Bytes
8247f19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
diff --git a/DESCRIPTION.md b/DESCRIPTION.md
index 5e6684a2..0f626e3a 100644
--- a/DESCRIPTION.md
+++ b/DESCRIPTION.md
@@ -11,6 +11,7 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne
 - v2.8.2(November 18,2022)
 
   - Improved performance of OCSP response caching
+  - Improved performance of regexes used for PUT/GET SQL statement detection
 
 - v2.8.1(October 30,2022)
 
diff --git a/src/snowflake/connector/_sql_util.py b/src/snowflake/connector/_sql_util.py
new file mode 100644
index 00000000..51a9d090
--- /dev/null
+++ b/src/snowflake/connector/_sql_util.py
@@ -0,0 +1,52 @@
+#
+# Copyright (c) 2012-2021 Snowflake Computing Inc. All rights reserved.
+#
+
+from __future__ import annotations
+
+import re
+
+from .constants import FileTransferType
+
+COMMENT_START_SQL_RE = re.compile(
+    r"""
+                                  ^\s*(?:
+                                      /\*[\w\W]*?\*/
+                                  )""",
+    re.VERBOSE,
+)
+
+PUT_SQL_RE = re.compile(r"^\s*put", flags=re.IGNORECASE)
+GET_SQL_RE = re.compile(r"^\s*get", flags=re.IGNORECASE)
+
+
+def remove_starting_comments(sql: str) -> str:
+    """Remove all comments from the start of a SQL statement."""
+    commentless_sql = sql
+    while True:
+        start_comment = COMMENT_START_SQL_RE.match(commentless_sql)
+        if start_comment is None:
+            break
+        commentless_sql = commentless_sql[start_comment.end() :]
+    return commentless_sql
+
+
+def get_file_transfer_type(sql: str) -> FileTransferType | None:
+    """Decide whether a SQL is a file transfer and return its type.
+
+    None is returned if the SQL isn't a file transfer so that this function can be
+    used in an if-statement.
+    """
+    commentless_sql = remove_starting_comments(sql)
+    if PUT_SQL_RE.match(commentless_sql):
+        return FileTransferType.PUT
+    elif GET_SQL_RE.match(commentless_sql):
+        return FileTransferType.GET
+
+
+def is_put_statement(sql: str) -> bool:
+    return get_file_transfer_type(sql) == FileTransferType.PUT
+
+
+def is_get_statement(sql: str) -> bool:
+    return get_file_transfer_type(sql) == FileTransferType.GET
diff --git a/src/snowflake/connector/cursor.py b/src/snowflake/connector/cursor.py
index 2a8089b0..a367a987 100644
--- a/src/snowflake/connector/cursor.py
+++ b/src/snowflake/connector/cursor.py
@@ -32,6 +32,7 @@ from snowflake.connector.result_batch import create_batches_from_response
 from snowflake.connector.result_set import ResultSet
 
 from . import compat
+from ._sql_util import get_file_transfer_type
 from .bind_upload_agent import BindUploadAgent, BindUploadError
 from .constants import (
     FIELD_NAME_TO_ID,
@@ -183,8 +184,6 @@ class SnowflakeCursor:
         Calling a function is expensive in Python and most of these getters are unnecessary.
     """
 
-    PUT_SQL_RE = re.compile(r"^(?:/\*.*\*/\s*)*put\s+", flags=re.IGNORECASE)
-    GET_SQL_RE = re.compile(r"^(?:/\*.*\*/\s*)*get\s+", flags=re.IGNORECASE)
     INSERT_SQL_RE = re.compile(r"^insert\s+into", flags=re.IGNORECASE)
     COMMENT_SQL_RE = re.compile(r"/\*.*\*/")
     INSERT_SQL_VALUES_RE = re.compile(
@@ -202,11 +201,7 @@ class SnowflakeCursor:
         None is returned if the SQL isn't a file transfer so that this function can be
         used in an if-statement.
         """
-        if SnowflakeCursor.PUT_SQL_RE.match(sql):
-            return FileTransferType.PUT
-        elif SnowflakeCursor.GET_SQL_RE.match(sql):
-            return FileTransferType.GET
-        return None
+        return get_file_transfer_type(sql)
 
     def __init__(
         self,
@@ -464,9 +459,7 @@ class SnowflakeCursor:
             self._is_file_transfer = _is_put_get
         else:
             # or detect it.
-            self._is_file_transfer = self.PUT_SQL_RE.match(
-                query
-            ) or self.GET_SQL_RE.match(query)
+            self._is_file_transfer = get_file_transfer_type(query) is not None
         logger.debug("is_file_transfer: %s", self._is_file_transfer is not None)
 
         real_timeout = (
diff --git a/src/snowflake/connector/gcs_storage_client.py b/src/snowflake/connector/gcs_storage_client.py
index b3709be3..e113e459 100644
--- a/src/snowflake/connector/gcs_storage_client.py
+++ b/src/snowflake/connector/gcs_storage_client.py
@@ -10,6 +10,7 @@ import os
 from logging import getLogger
 from typing import TYPE_CHECKING, Any, NamedTuple
 
+from ._sql_util import is_put_statement
 from .compat import quote
 from .constants import (
     FILE_PROTOCOL,
@@ -269,9 +270,7 @@ class SnowflakeGCSRestClient(SnowflakeStorageClient):
             The local file path.
         """
         command = self._command
-        if FILE_PROTOCOL not in self._command or not self._cursor.PUT_SQL_RE.match(
-            command
-        ):
+        if FILE_PROTOCOL not in self._command or not is_put_statement(command):
             return None
 
         file_path_begin_index = command.find(FILE_PROTOCOL)
diff --git a/test/unit/test_cursor.py b/test/unit/test_cursor.py
index 37c118c1..bc43a559 100644
--- a/test/unit/test_cursor.py
+++ b/test/unit/test_cursor.py
@@ -27,9 +27,24 @@ class FakeConnection(SnowflakeConnection):
 @pytest.mark.parametrize(
     "sql,_type",
     (
+        ("", None),
+        ("select 1;", None),
         ("PUT file:///tmp/data/mydata.csv @my_int_stage;", FileTransferType.PUT),
         ("GET @%mytable file:///tmp/data/;", FileTransferType.GET),
-        ("select 1;", None),
+        ("/**/PUT file:///tmp/data/mydata.csv @my_int_stage;", FileTransferType.PUT),
+        ("/**/ GET @%mytable file:///tmp/data/;", FileTransferType.GET),
+        pytest.param(
+            "/**/\n"
+            + "\t/*/get\t*/\t/**/\n" * 10000
+            + "\t*/get @~/test.csv file:///tmp\n",
+            None,
+            id="long_incorrect",
+        ),
+        pytest.param(
+            "/**/\n" + "\t/*/put\t*/\t/**/\n" * 10000 + "put file:///tmp/data.csv @~",
+            FileTransferType.PUT,
+            id="long_correct",
+        ),
     ),
 )
 def test_get_filetransfer_type(sql, _type):