Skip to content

Commit cdc603e

Browse files
author
William Cox
committed
Add better partition string escaping
1 parent bbfee5f commit cdc603e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

dask_sql/input_utils/hive.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ast
22
import logging
33
import os
4+
import re
45
from functools import partial
56
from typing import Any, Union
67

@@ -159,6 +160,24 @@ def wrapped_read_function(location, column_information, **kwargs):
159160
df = wrapped_read_function(location, column_information, **kwargs)
160161
return df
161162

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+
162181
def _parse_hive_table_description(
163182
self,
164183
cursor: Union["sqlalchemy.engine.base.Connection", "hive.Cursor"],
@@ -173,6 +192,7 @@ def _parse_hive_table_description(
173192
"""
174193
cursor.execute(f"USE {schema}")
175194
if partition:
195+
partition = self._escape_partition(partition)
176196
result = self._fetch_all_results(
177197
cursor, f"DESCRIBE FORMATTED {table_name} PARTITION ({partition})"
178198
)

0 commit comments

Comments
 (0)