示例#1
0
    def _set_description(self):
        if not self.result:
            return

        self._columns = list(
            map(
                lambda x: self.connection.lowlevel.result_fetch(
                    self.result, x), range(self.result.ncols)))

        # we import this late, otherwise the whole monetdbe project is unimportable if we don't have access to monetdbe.so
        from monetdbe._cffi.convert import make_string, monet_numpy_map

        name = (make_string(rcol.name) for rcol in self._columns)
        type_code = (monet_numpy_map[rcol.type][2] for rcol in self._columns)

        display_size = repeat(None)
        internal_size = repeat(None)
        precision = repeat(None)
        scale = repeat(None)
        null_ok = repeat(None)

        descriptions = list(
            zip(name, type_code, display_size, internal_size, precision, scale,
                null_ok))
        self.description = [Description(*i) for i in descriptions]
示例#2
0
def result_fetch_numpy(result: monetdbe_result) -> Mapping[str, np.ndarray]:
    result_dict: Dict[str, np.ndarray] = {}
    for c in range(result.ncols):
        rcol = result_fetch(result, c)
        name = make_string(rcol.name)
        type_info = monet_c_type_map[rcol.type]

        np_mask = np.ma.nomask  # type: ignore[attr-defined]
        # for non float/int we for now first make a numpy object array which we then convert to the right numpy type
        if type_info.numpy_type.type == np.object_:
            values = [extract(rcol, r) for r in range(result.nrows)]
            np_col: np.ndarray = np.array(values)
            np_mask = np.array([v is None for v in values])
            if rcol.type == lib.monetdbe_str:
                np_col = np_col.astype(str)
            elif rcol.type == lib.monetdbe_date:
                np_col = np_col.astype('datetime64[D]')  # type: ignore
            elif rcol.type == lib.monetdbe_time:
                warn(
                    "Not converting column with type column since no proper numpy equivalent"
                )
            elif rcol.type == lib.monetdbe_timestamp:
                np_col = np_col.astype('datetime64[ms]')  # type: ignore
        else:
            buffer_size = result.nrows * type_info.numpy_type.itemsize  # type: ignore
            c_buffer = ffi.buffer(rcol.data, buffer_size)
            np_col = np.frombuffer(c_buffer,
                                   dtype=type_info.numpy_type)  # type: ignore
            np_mask = np_col == get_null_value(rcol)

        masked: np.ndarray = np.ma.masked_array(np_col, mask=np_mask)

        result_dict[name] = masked
    return result_dict
示例#3
0
    def result_fetch_numpy(monetdbe_result: ffi.CData) -> Mapping[str, np.ma.MaskedArray]:

        result = {}
        for c in range(monetdbe_result.ncols):
            rcol = Frontend.result_fetch(monetdbe_result, c)
            name = make_string(rcol.name)
            cast_string, cast_function, numpy_type, monetdbe_null = monet_numpy_map[rcol.type]

            # for non float/int we for now first make a numpy object array which we then convert to the right numpy type
            if numpy_type.type == np.object_:
                np_col: np.ndarray = np.array([extract(rcol, r) for r in range(monetdbe_result.nrows)])
                if rcol.type == lib.monetdbe_str:
                    np_col = np_col.astype(str)
                elif rcol.type == lib.monetdbe_date:
                    np_col = np_col.astype('datetime64[D]')  # type: ignore
                elif rcol.type == lib.monetdbe_time:
                    warn("Not converting column with type column since no proper numpy equivalent")
                elif rcol.type == lib.monetdbe_timestamp:
                    np_col = np_col.astype('datetime64[ns]')  # type: ignore
            else:
                buffer_size = monetdbe_result.nrows * numpy_type.itemsize  # type: ignore
                c_buffer = ffi.buffer(rcol.data, buffer_size)
                np_col = np.frombuffer(c_buffer, dtype=numpy_type)  # type: ignore

            if monetdbe_null:
                mask = np_col == monetdbe_null
            else:
                mask = np.ma.nomask  # type: ignore

            masked = np.ma.masked_array(np_col, mask=mask)

            result[name] = masked
        return result
示例#4
0
    def get_description(self):
        # we import this late, otherwise the whole monetdbe project is unimportable
        # if we don't have access to monetdbe shared library
        from monetdbe._cffi.convert import make_string, monet_c_type_map
        from monetdbe._cffi.internal import result_fetch

        if not self.result:
            return

        columns = list(map(lambda x: result_fetch(self.result, x), range(self.result.ncols)))
        name = (make_string(rcol.name) for rcol in columns)
        type_code = (monet_c_type_map[rcol.type].sql_type for rcol in columns)
        display_size = repeat(None)
        internal_size = repeat(None)
        precision = repeat(None)
        scale = repeat(None)
        null_ok = repeat(None)
        descriptions = list(zip(name, type_code, display_size, internal_size, precision, scale, null_ok))
        return [Description(*i) for i in descriptions]