1
1
import ast
2
2
import logging
3
3
import os
4
+ import re
4
5
from functools import partial
5
6
from typing import Any , Union
6
7
@@ -159,6 +160,24 @@ def wrapped_read_function(location, column_information, **kwargs):
159
160
df = wrapped_read_function (location , column_information , ** kwargs )
160
161
return df
161
162
163
+ def _escape_partition (self , partition : str ): # pragma: no cover
164
+ """
165
+ Given a partition string like `key=value` escape the string properly for Hive.
166
+ Wrap anything but digits in quotes. Don't wrap the column name.
167
+ """
168
+ contains_only_digits = re .compile (r"^\d+$" )
169
+
170
+ try :
171
+ k , v = partition .split ("=" )
172
+ if re .match (contains_only_digits , v ):
173
+ escaped_value = v
174
+ else :
175
+ escaped_value = f'"{ v } "'
176
+ return f"{ k } ={ escaped_value } "
177
+ except ValueError :
178
+ logger .warning (f"{ partition } didn't contain a `=`" )
179
+ return partition
180
+
162
181
def _parse_hive_table_description (
163
182
self ,
164
183
cursor : Union ["sqlalchemy.engine.base.Connection" , "hive.Cursor" ],
@@ -173,6 +192,7 @@ def _parse_hive_table_description(
173
192
"""
174
193
cursor .execute (f"USE { schema } " )
175
194
if partition :
195
+ partition = self ._escape_partition (partition )
176
196
result = self ._fetch_all_results (
177
197
cursor , f"DESCRIBE FORMATTED { table_name } PARTITION ({ partition } )"
178
198
)
0 commit comments