-
Notifications
You must be signed in to change notification settings - Fork 117
/
column.py
108 lines (84 loc) · 2.99 KB
/
column.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from typing import TypeVar, Any
import re
from dataclasses import dataclass
from dbt.adapters.base.column import Column
from dbt.exceptions import RuntimeException
Self = TypeVar('Self', bound='ClickhouseColumn')
@dataclass
class ClickhouseColumn(Column):
TYPE_LABELS = {
'STRING': 'String',
'TIMESTAMP': 'DateTime',
'FLOAT': 'Float64',
'INTEGER': 'Int64',
}
is_nullable: bool = False
_brackets_regex = re.compile(r'(Nullable|LowCardinality)\((.*?)\)')
_fix_size_regex = re.compile(r'FixedString\((.*?)\)')
_decimal_regex = re.compile(r'Decimal\((\d+), (\d+)\)')
def __init__(
self,
column: str,
dtype: str,
is_nullable: bool = False,
) -> None:
char_size = None
numeric_precision = None
numeric_scale = None
match_brackets = self._brackets_regex.search(dtype)
if match_brackets:
self.is_nullable = True
dtype = match_brackets.group(2)
if dtype.lower().startswith('fixedstring'):
match_sized = self._fix_size_regex.search(dtype)
char_size = int(match_sized.group(2))
if dtype.lower().startswith('decimal'):
match_dec = self._decimal_regex.search(dtype)
numeric_precision = int(match_dec.group(1))
numeric_scale = int(match_dec.group(2))
super().__init__(column, dtype, char_size, numeric_precision, numeric_scale)
def __repr__(self) -> str:
return f'<ClickhouseColumn {self.name} ({self.data_type}, is nullable: {self.is_nullable})>'
@property
def quoted(self) -> str:
return self.column
def is_string(self) -> bool:
return self.dtype.lower() in [
'string',
'fixedstring',
'longblob',
'longtext',
'tinytext',
'text',
'varchar',
'mediumblob',
'blob',
'tinyblob',
'char',
'mediumtext',
]
def is_integer(self) -> bool:
return self.dtype.lower().startswith('int') or self.dtype.lower().startswith(
'uint'
)
def is_numeric(self) -> bool:
return self.dtype.lower().startswith('decimal')
def is_float(self) -> bool:
return self.dtype.lower().startswith('float')
def string_size(self) -> int:
if not self.is_string():
raise RuntimeException('Called string_size() on non-string field!')
if self.dtype.lower() != 'fixedstring' or self.char_size is None:
return 256
else:
return int(self.char_size)
@classmethod
def string_type(cls, size: int) -> str:
return 'String'
@classmethod
def numeric_type(cls, dtype: str, precision: Any, scale: Any) -> str:
return dtype
def literal(self, value):
return f'to{self.dtype}({value})'
def can_expand_to(self: Self, other_column: Self) -> bool:
return self.is_string() and other_column.is_string()