mirror of
https://github.com/hanzo-ds/driver.git
synced 2026-10-30 20:32:40 +00:00
Imports read `datastore_driver`, distribution `datastore-driver`, so nothing has to import another product's name to talk to Hanzo Datastore. Cut from upstream 0.2.9 — the version this stack already ran — rather than from upstream HEAD, so it is a PURE RENAME with no behaviour change. Forking from HEAD would have dragged versions along with the name (connect alone jumped 0.8.17 -> 1.6.0, a major), which is a different and much riskier change wearing a rename's clothes. X-ClickHouse-* headers are untouched: they are wire protocol the server sends and expects, and a different token shape from the module name.
79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
import numpy as np
|
|
|
|
from ... import defines
|
|
from .base import NumpyColumn
|
|
|
|
|
|
class NumpyStringColumn(NumpyColumn):
|
|
null_value = ''
|
|
|
|
default_encoding = defines.STRINGS_ENCODING
|
|
|
|
def __init__(self, encoding=default_encoding, **kwargs):
|
|
self.encoding = encoding
|
|
super(NumpyStringColumn, self).__init__(**kwargs)
|
|
|
|
def read_items(self, n_items, buf):
|
|
return np.array(
|
|
buf.read_strings(n_items, encoding=self.encoding), dtype=self.dtype
|
|
)
|
|
|
|
def write_items(self, items, buf):
|
|
return buf.write_strings(items.tolist(), encoding=self.encoding)
|
|
|
|
|
|
class NumpyByteStringColumn(NumpyColumn):
|
|
null_value = b''
|
|
|
|
def read_items(self, n_items, buf):
|
|
return np.array(buf.read_strings(n_items), dtype=self.dtype)
|
|
|
|
def write_items(self, items, buf):
|
|
return buf.write_strings(items.tolist())
|
|
|
|
|
|
class NumpyFixedString(NumpyStringColumn):
|
|
def __init__(self, length, **kwargs):
|
|
self.length = length
|
|
super(NumpyFixedString, self).__init__(**kwargs)
|
|
|
|
def read_items(self, n_items, buf):
|
|
return np.array(buf.read_fixed_strings(
|
|
n_items, self.length, encoding=self.encoding
|
|
), dtype=self.dtype)
|
|
|
|
def write_items(self, items, buf):
|
|
return buf.write_fixed_strings(
|
|
items.tolist(), self.length, encoding=self.encoding
|
|
)
|
|
|
|
|
|
class NumpyByteFixedString(NumpyByteStringColumn):
|
|
def __init__(self, length, **kwargs):
|
|
self.length = length
|
|
super(NumpyByteFixedString, self).__init__(**kwargs)
|
|
|
|
def read_items(self, n_items, buf):
|
|
return np.array(
|
|
buf.read_fixed_strings(n_items, self.length), dtype=self.dtype
|
|
)
|
|
|
|
def write_items(self, items, buf):
|
|
return buf.write_fixed_strings(items.tolist(), self.length)
|
|
|
|
|
|
def create_string_column(spec, column_options):
|
|
client_settings = column_options['context'].client_settings
|
|
strings_as_bytes = client_settings['strings_as_bytes']
|
|
encoding = client_settings.get(
|
|
'strings_encoding', NumpyStringColumn.default_encoding
|
|
)
|
|
|
|
if spec == 'String':
|
|
cls = NumpyByteStringColumn if strings_as_bytes else NumpyStringColumn
|
|
return cls(encoding=encoding, **column_options)
|
|
else:
|
|
length = int(spec[12:-1])
|
|
cls = NumpyByteFixedString if strings_as_bytes else NumpyFixedString
|
|
return cls(length, encoding=encoding, **column_options)
|