|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from ..utils import match_regexps |
| 4 | +from .base import ( |
| 5 | + CHECKSUM_HEXDIGITS, |
| 6 | + MD5_HEXDIGITS, |
| 7 | + TIMESTAMP_PRECISION_POS, |
| 8 | + ConnectError, |
| 9 | + DbPath, |
| 10 | + ColType, |
| 11 | + ColType_UUID, |
| 12 | + ThreadedDatabase, |
| 13 | + import_helper, |
| 14 | +) |
| 15 | +from .database_types import Decimal, Float, FractionalType, Integer, TemporalType, Text, Timestamp, TimestampTZ |
| 16 | + |
| 17 | + |
| 18 | +@import_helper("vertica") |
| 19 | +def import_vertica(): |
| 20 | + import vertica_python |
| 21 | + |
| 22 | + return vertica_python |
| 23 | + |
| 24 | + |
| 25 | +class Vertica(ThreadedDatabase): |
| 26 | + default_schema = "public" |
| 27 | + |
| 28 | + TYPE_CLASSES = { |
| 29 | + # Timestamps |
| 30 | + "timestamp": Timestamp, |
| 31 | + "timestamptz": TimestampTZ, |
| 32 | + # Numbers |
| 33 | + "numeric": Decimal, |
| 34 | + "int": Integer, |
| 35 | + "float": Float, |
| 36 | + # Text |
| 37 | + "char": Text, |
| 38 | + "varchar": Text, |
| 39 | + } |
| 40 | + |
| 41 | + ROUNDS_ON_PREC_LOSS = True |
| 42 | + |
| 43 | + def __init__(self, *, thread_count, **kw): |
| 44 | + self._args = kw |
| 45 | + self._args["AUTOCOMMIT"] = False |
| 46 | + |
| 47 | + super().__init__(thread_count=thread_count) |
| 48 | + |
| 49 | + def create_connection(self): |
| 50 | + vertica = import_vertica() |
| 51 | + try: |
| 52 | + c = vertica.connect(**self._args) |
| 53 | + return c |
| 54 | + except vertica.errors.ConnectionError as e: |
| 55 | + raise ConnectError(*e.args) from e |
| 56 | + |
| 57 | + def _parse_type( |
| 58 | + self, |
| 59 | + table_path: DbPath, |
| 60 | + col_name: str, |
| 61 | + type_repr: str, |
| 62 | + datetime_precision: int = None, |
| 63 | + numeric_precision: int = None, |
| 64 | + numeric_scale: int = None, |
| 65 | + ) -> ColType: |
| 66 | + timestamp_regexps = { |
| 67 | + r"timestamp\(?(\d?)\)?": Timestamp, |
| 68 | + r"timestamptz\(?(\d?)\)?": TimestampTZ, |
| 69 | + } |
| 70 | + for m, t_cls in match_regexps(timestamp_regexps, type_repr): |
| 71 | + precision = int(m.group(1)) if m.group(1) else 6 |
| 72 | + return t_cls(precision=precision, rounds=self.ROUNDS_ON_PREC_LOSS) |
| 73 | + |
| 74 | + number_regexps = { |
| 75 | + r"numeric\((\d+),(\d+)\)": Decimal, |
| 76 | + } |
| 77 | + for m, n_cls in match_regexps(number_regexps, type_repr): |
| 78 | + _prec, scale = map(int, m.groups()) |
| 79 | + return n_cls(scale) |
| 80 | + |
| 81 | + string_regexps = { |
| 82 | + r"varchar\((\d+)\)": Text, |
| 83 | + r"char\((\d+)\)": Text, |
| 84 | + } |
| 85 | + for m, n_cls in match_regexps(string_regexps, type_repr): |
| 86 | + return n_cls() |
| 87 | + |
| 88 | + return super()._parse_type(table_path, col_name, type_repr, datetime_precision, numeric_precision) |
| 89 | + |
| 90 | + def select_table_schema(self, path: DbPath) -> str: |
| 91 | + schema, table = self._normalize_table_path(path) |
| 92 | + |
| 93 | + return ( |
| 94 | + "SELECT column_name, data_type, datetime_precision, numeric_precision, numeric_scale " |
| 95 | + "FROM V_CATALOG.COLUMNS " |
| 96 | + f"WHERE table_name = '{table}' AND table_schema = '{schema}'" |
| 97 | + ) |
| 98 | + |
| 99 | + def quote(self, s: str): |
| 100 | + return f'"{s}"' |
| 101 | + |
| 102 | + def concat(self, l: List[str]) -> str: |
| 103 | + return " || ".join(l) |
| 104 | + |
| 105 | + def md5_to_int(self, s: str) -> str: |
| 106 | + return f"CAST(HEX_TO_INTEGER(SUBSTRING(MD5({s}), {1 + MD5_HEXDIGITS - CHECKSUM_HEXDIGITS})) AS NUMERIC(38, 0))" |
| 107 | + |
| 108 | + def to_string(self, s: str) -> str: |
| 109 | + return f"CAST({s} AS VARCHAR)" |
| 110 | + |
| 111 | + def normalize_timestamp(self, value: str, coltype: TemporalType) -> str: |
| 112 | + if coltype.rounds: |
| 113 | + return f"TO_CHAR({value}::TIMESTAMP({coltype.precision}), 'YYYY-MM-DD HH24:MI:SS.US')" |
| 114 | + |
| 115 | + timestamp6 = f"TO_CHAR({value}::TIMESTAMP(6), 'YYYY-MM-DD HH24:MI:SS.US')" |
| 116 | + return ( |
| 117 | + f"RPAD(LEFT({timestamp6}, {TIMESTAMP_PRECISION_POS+coltype.precision}), {TIMESTAMP_PRECISION_POS+6}, '0')" |
| 118 | + ) |
| 119 | + |
| 120 | + def normalize_number(self, value: str, coltype: FractionalType) -> str: |
| 121 | + return self.to_string(f"CAST({value} AS NUMERIC(38, {coltype.precision}))") |
| 122 | + |
| 123 | + def normalize_uuid(self, value: str, coltype: ColType_UUID) -> str: |
| 124 | + # Trim doesn't work on CHAR type |
| 125 | + return f"TRIM(CAST({value} AS VARCHAR))" |
0 commit comments