|
| 1 | +import decimal |
| 2 | +from abc import ABC, abstractmethod |
| 3 | +from typing import Tuple, Union |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +import attrs |
| 7 | + |
| 8 | +from data_diff.utils import ArithAlphanumeric, ArithUUID, Unknown |
| 9 | + |
| 10 | + |
| 11 | +DbPath = Tuple[str, ...] |
| 12 | +DbKey = Union[int, str, bytes, ArithUUID, ArithAlphanumeric] |
| 13 | +DbTime = datetime |
| 14 | + |
| 15 | + |
| 16 | +@attrs.define(frozen=True) |
| 17 | +class ColType: |
| 18 | + @property |
| 19 | + def supported(self) -> bool: |
| 20 | + return True |
| 21 | + |
| 22 | + |
| 23 | +@attrs.define(frozen=True) |
| 24 | +class PrecisionType(ColType): |
| 25 | + precision: int |
| 26 | + rounds: Union[bool, Unknown] = Unknown |
| 27 | + |
| 28 | + |
| 29 | +@attrs.define(frozen=True) |
| 30 | +class Boolean(ColType): |
| 31 | + precision = 0 |
| 32 | + |
| 33 | + |
| 34 | +@attrs.define(frozen=True) |
| 35 | +class TemporalType(PrecisionType): |
| 36 | + pass |
| 37 | + |
| 38 | + |
| 39 | +@attrs.define(frozen=True) |
| 40 | +class Timestamp(TemporalType): |
| 41 | + pass |
| 42 | + |
| 43 | + |
| 44 | +@attrs.define(frozen=True) |
| 45 | +class TimestampTZ(TemporalType): |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +@attrs.define(frozen=True) |
| 50 | +class Datetime(TemporalType): |
| 51 | + pass |
| 52 | + |
| 53 | + |
| 54 | +@attrs.define(frozen=True) |
| 55 | +class Date(TemporalType): |
| 56 | + pass |
| 57 | + |
| 58 | + |
| 59 | +@attrs.define(frozen=True) |
| 60 | +class NumericType(ColType): |
| 61 | + # 'precision' signifies how many fractional digits (after the dot) we want to compare |
| 62 | + precision: int |
| 63 | + |
| 64 | + |
| 65 | +@attrs.define(frozen=True) |
| 66 | +class FractionalType(NumericType): |
| 67 | + pass |
| 68 | + |
| 69 | + |
| 70 | +@attrs.define(frozen=True) |
| 71 | +class Float(FractionalType): |
| 72 | + python_type = float |
| 73 | + |
| 74 | + |
| 75 | +@attrs.define(frozen=True) |
| 76 | +class IKey(ABC): |
| 77 | + "Interface for ColType, for using a column as a key in table." |
| 78 | + |
| 79 | + @property |
| 80 | + @abstractmethod |
| 81 | + def python_type(self) -> type: |
| 82 | + "Return the equivalent Python type of the key" |
| 83 | + |
| 84 | + def make_value(self, value): |
| 85 | + return self.python_type(value) |
| 86 | + |
| 87 | + |
| 88 | +@attrs.define(frozen=True) |
| 89 | +class Decimal(FractionalType, IKey): # Snowflake may use Decimal as a key |
| 90 | + @property |
| 91 | + def python_type(self) -> type: |
| 92 | + if self.precision == 0: |
| 93 | + return int |
| 94 | + return decimal.Decimal |
| 95 | + |
| 96 | + |
| 97 | +@attrs.define(frozen=True) |
| 98 | +class StringType(ColType): |
| 99 | + python_type = str |
| 100 | + |
| 101 | + |
| 102 | +@attrs.define(frozen=True) |
| 103 | +class ColType_UUID(ColType, IKey): |
| 104 | + python_type = ArithUUID |
| 105 | + |
| 106 | + |
| 107 | +@attrs.define(frozen=True) |
| 108 | +class ColType_Alphanum(ColType, IKey): |
| 109 | + python_type = ArithAlphanumeric |
| 110 | + |
| 111 | + |
| 112 | +@attrs.define(frozen=True) |
| 113 | +class Native_UUID(ColType_UUID): |
| 114 | + pass |
| 115 | + |
| 116 | + |
| 117 | +@attrs.define(frozen=True) |
| 118 | +class String_UUID(ColType_UUID, StringType): |
| 119 | + pass |
| 120 | + |
| 121 | + |
| 122 | +@attrs.define(frozen=True) |
| 123 | +class String_Alphanum(ColType_Alphanum, StringType): |
| 124 | + @staticmethod |
| 125 | + def test_value(value: str) -> bool: |
| 126 | + try: |
| 127 | + ArithAlphanumeric(value) |
| 128 | + return True |
| 129 | + except ValueError: |
| 130 | + return False |
| 131 | + |
| 132 | + def make_value(self, value): |
| 133 | + return self.python_type(value) |
| 134 | + |
| 135 | + |
| 136 | +@attrs.define(frozen=True) |
| 137 | +class String_VaryingAlphanum(String_Alphanum): |
| 138 | + pass |
| 139 | + |
| 140 | + |
| 141 | +@attrs.define(frozen=True) |
| 142 | +class String_FixedAlphanum(String_Alphanum): |
| 143 | + length: int |
| 144 | + |
| 145 | + def make_value(self, value): |
| 146 | + if len(value) != self.length: |
| 147 | + raise ValueError(f"Expected alphanumeric value of length {self.length}, but got '{value}'.") |
| 148 | + return self.python_type(value, max_len=self.length) |
| 149 | + |
| 150 | + |
| 151 | +@attrs.define(frozen=True) |
| 152 | +class Text(StringType): |
| 153 | + @property |
| 154 | + def supported(self) -> bool: |
| 155 | + return False |
| 156 | + |
| 157 | + |
| 158 | +# In majority of DBMSes, it is called JSON/JSONB. Only in Snowflake, it is OBJECT. |
| 159 | +@attrs.define(frozen=True) |
| 160 | +class JSON(ColType): |
| 161 | + pass |
| 162 | + |
| 163 | + |
| 164 | +@attrs.define(frozen=True) |
| 165 | +class Array(ColType): |
| 166 | + item_type: ColType |
| 167 | + |
| 168 | + |
| 169 | +# Unlike JSON, structs are not free-form and have a very specific set of fields and their types. |
| 170 | +# We do not parse & use those fields now, but we can do this later. |
| 171 | +# For example, in BigQuery: |
| 172 | +# - https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type |
| 173 | +# - https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#struct_literals |
| 174 | +@attrs.define(frozen=True) |
| 175 | +class Struct(ColType): |
| 176 | + pass |
| 177 | + |
| 178 | + |
| 179 | +@attrs.define(frozen=True) |
| 180 | +class Integer(NumericType, IKey): |
| 181 | + precision: int = 0 |
| 182 | + python_type: type = int |
| 183 | + |
| 184 | + def __attrs_post_init__(self): |
| 185 | + assert self.precision == 0 |
| 186 | + |
| 187 | + |
| 188 | +@attrs.define(frozen=True) |
| 189 | +class UnknownColType(ColType): |
| 190 | + text: str |
| 191 | + |
| 192 | + @property |
| 193 | + def supported(self) -> bool: |
| 194 | + return False |
0 commit comments