diff options
Diffstat (limited to '')
-rw-r--r-- | sqlglot/helper.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/sqlglot/helper.py b/sqlglot/helper.py index 9799fe2..35a4586 100644 --- a/sqlglot/helper.py +++ b/sqlglot/helper.py @@ -6,7 +6,7 @@ import logging import re import sys import typing as t -from collections.abc import Collection +from collections.abc import Collection, Set from contextlib import contextmanager from copy import copy from enum import Enum @@ -496,3 +496,31 @@ DATE_UNITS = {"day", "week", "month", "quarter", "year", "year_month"} def is_date_unit(expression: t.Optional[exp.Expression]) -> bool: return expression is not None and expression.name.lower() in DATE_UNITS + + +K = t.TypeVar("K") +V = t.TypeVar("V") + + +class SingleValuedMapping(t.Mapping[K, V]): + """ + Mapping where all keys return the same value. + + This rigamarole is meant to avoid copying keys, which was originally intended + as an optimization while qualifying columns for tables with lots of columns. + """ + + def __init__(self, keys: t.Collection[K], value: V): + self._keys = keys if isinstance(keys, Set) else set(keys) + self._value = value + + def __getitem__(self, key: K) -> V: + if key in self._keys: + return self._value + raise KeyError(key) + + def __len__(self) -> int: + return len(self._keys) + + def __iter__(self) -> t.Iterator[K]: + return iter(self._keys) |