Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better partition string escaping #180

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions dask_sql/input_utils/hive.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ast
import logging
import os
import re
from functools import partial
from typing import Any, Union

Expand Down Expand Up @@ -184,6 +185,24 @@ def wrapped_read_function(location, column_information, **kwargs):
df = wrapped_read_function(location, column_information, **kwargs)
return df

def _escape_partition(self, partition: str): # pragma: no cover
"""
Given a partition string like `key=value` escape the string properly for Hive.
Wrap anything but digits in quotes. Don't wrap the column name.
"""
contains_only_digits = re.compile(r"^\d+$")

try:
k, v = partition.split("=")
if re.match(contains_only_digits, v):
escaped_value = v
else:
escaped_value = f'"{v}"'
return f"{k}={escaped_value}"
except ValueError:
logger.warning(f"{partition} didn't contain a `=`")
return partition

def _parse_hive_table_description(
self,
cursor: Union["sqlalchemy.engine.base.Connection", "hive.Cursor"],
Expand All @@ -198,6 +217,7 @@ def _parse_hive_table_description(
"""
cursor.execute(f"USE {schema}")
if partition:
partition = self._escape_partition(partition)
result = self._fetch_all_results(
cursor, f"DESCRIBE FORMATTED {table_name} PARTITION ({partition})"
)
Expand Down