def create_ad_hoc_field(cls, db_type): import infi.clickhouse_orm.fields as orm_fields # Enums if db_type.startswith('Enum'): return orm_fields.BaseEnumField.create_ad_hoc_field(db_type) # DateTime with timezone if db_type.startswith('DateTime('): timezone = db_type[9:-1] return orm_fields.DateTimeField( timezone=timezone[1:-1] if timezone else None ) # DateTime64 if db_type.startswith('DateTime64('): precision, *timezone = [s.strip() for s in db_type[11:-1].split(',')] return orm_fields.DateTime64Field( precision=int(precision), timezone=timezone[0][1:-1] if timezone else None ) # Arrays if db_type.startswith('Array'): inner_field = cls.create_ad_hoc_field(db_type[6 : -1]) return orm_fields.ArrayField(inner_field) # Tuples (poor man's version - convert to array) if db_type.startswith('Tuple'): types = [s.strip() for s in db_type[6 : -1].split(',')] assert len(set(types)) == 1, 'No support for mixed types in tuples - ' + db_type inner_field = cls.create_ad_hoc_field(types[0]) return orm_fields.ArrayField(inner_field) # FixedString if db_type.startswith('FixedString'): length = int(db_type[12 : -1]) return orm_fields.FixedStringField(length) # Decimal / Decimal32 / Decimal64 / Decimal128 if db_type.startswith('Decimal'): p = db_type.index('(') args = [int(n.strip()) for n in db_type[p + 1 : -1].split(',')] field_class = getattr(orm_fields, db_type[:p] + 'Field') return field_class(*args) # Nullable if db_type.startswith('Nullable'): inner_field = cls.create_ad_hoc_field(db_type[9 : -1]) return orm_fields.NullableField(inner_field) # LowCardinality if db_type.startswith('LowCardinality'): inner_field = cls.create_ad_hoc_field(db_type[15 : -1]) return orm_fields.LowCardinalityField(inner_field) # Simple fields name = db_type + 'Field' if not hasattr(orm_fields, name): raise NotImplementedError('No field class for %s' % db_type) return getattr(orm_fields, name)()
def create_ad_hoc_field(cls, db_type): import infi.clickhouse_orm.fields as orm_fields # Enums if db_type.startswith('Enum'): db_type = 'String' # enum.Eum is not comparable # Arrays if db_type.startswith('Array'): inner_field = cls.create_ad_hoc_field(db_type[6:-1]) return orm_fields.ArrayField(inner_field) # FixedString if db_type.startswith('FixedString'): db_type = 'String' if db_type == 'LowCardinality(String)': db_type = 'String' if db_type.startswith('DateTime'): db_type = 'DateTime' if db_type.startswith('Nullable'): inner_field = cls.create_ad_hoc_field(db_type[9:-1]) return orm_fields.NullableField(inner_field) # db_type for Deimal comes like 'Decimal(P, S) string where P is precision and S is scale' if db_type.startswith('Decimal'): nums = [int(n) for n in db_type[8:-1].split(',')] return orm_fields.DecimalField(nums[0], nums[1]) # Simple fields name = db_type + 'Field' if not hasattr(orm_fields, name): raise NotImplementedError('No field class for %s' % db_type) return getattr(orm_fields, name)()
def create_ad_hoc_field(cls, db_type): import infi.clickhouse_orm.fields as orm_fields # Enums if db_type.startswith('Enum'): return orm_fields.BaseEnumField.create_ad_hoc_field(db_type) # DateTime with timezone if db_type.startswith('DateTime('): # Some functions return DateTimeField with timezone in brackets return orm_fields.DateTimeField() # Arrays if db_type.startswith('Array'): inner_field = cls.create_ad_hoc_field(db_type[6:-1]) return orm_fields.ArrayField(inner_field) # FixedString if db_type.startswith('FixedString'): length = int(db_type[12:-1]) return orm_fields.FixedStringField(length) # Decimal if db_type.startswith('Decimal'): precision, scale = [ int(n.strip()) for n in db_type[8:-1].split(',') ] return orm_fields.DecimalField(precision, scale) # Nullable if db_type.startswith('Nullable'): inner_field = cls.create_ad_hoc_field(db_type[9:-1]) return orm_fields.NullableField(inner_field) # Simple fields name = db_type + 'Field' if not hasattr(orm_fields, name): raise NotImplementedError('No field class for %s' % db_type) return getattr(orm_fields, name)()
def create_ad_hoc_field(cls, db_type): import infi.clickhouse_orm.fields as orm_fields # Enums if db_type.startswith('Enum'): db_type = 'String' # enum.Eum is not comparable # Arrays if db_type.startswith('Array'): inner_field = cls.create_ad_hoc_field(db_type[6 : -1]) return orm_fields.ArrayField(inner_field) # FixedString if db_type.startswith('FixedString'): db_type = 'String' # DateTime if db_type.startswith('DateTime'): db_type = 'DateTime' if db_type.startswith('Nullable'): inner_field = cls.create_ad_hoc_field(db_type[9 : -1]) return orm_fields.NullableField(inner_field) # Simple fields name = db_type + 'Field' if not hasattr(orm_fields, name): raise NotImplementedError('No field class for %s' % db_type) return getattr(orm_fields, name)()
class ClickhouseAllFields(chm.Model): id = chf.Int64Field() timestamp = chf.DateTimeField() timestamp_date = chf.DateField() string_field = chf.StringField() intfield = chf.Int32Field() floatfield = chf.Float32Field() null_field = chf.NullableField(chf.StringField()) enum_field = chf.Enum16Field(enum_) array_field = chf.ArrayField(chf.Int16Field()) engine = MergeTree('timestamp_date', ['id'])
def create_ad_hoc_field(cls, db_type): import infi.clickhouse_orm.fields as orm_fields # Enums if db_type.startswith('Enum'): return orm_fields.BaseEnumField.create_ad_hoc_field(db_type) # Arrays if db_type.startswith('Array'): inner_field = cls.create_ad_hoc_field(db_type[6 : -1]) return orm_fields.ArrayField(inner_field) # Simple fields name = db_type + 'Field' if not hasattr(orm_fields, name): raise NotImplementedError('No field class for %s' % db_type) return getattr(orm_fields, name)()