diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-02-16 05:45:52 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-02-16 05:45:52 +0000 |
commit | 3d48060515ba25b4c49d975a520ee0682327d1b7 (patch) | |
tree | e8730f509026e866d77c459f74a384505425363a /sqlglot/helper.py | |
parent | Releasing debian version 21.0.2-1. (diff) | |
download | sqlglot-3d48060515ba25b4c49d975a520ee0682327d1b7.tar.xz sqlglot-3d48060515ba25b4c49d975a520ee0682327d1b7.zip |
Merging upstream version 21.1.1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/helper.py')
-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) |