Skip to content

Commit

Permalink
small fixed, add unittest for issue #83
Browse files Browse the repository at this point in the history
  • Loading branch information
gijzelaerr committed Aug 21, 2020
1 parent be7d77f commit 6334436
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
16 changes: 8 additions & 8 deletions monetdbe/_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def cleanup_result(self, result: ffi.CData):
if result and self._connection:
check_error(lib.monetdbe_cleanup_result(self._connection, result))

@staticmethod
def open(
self,
dbdir: Optional[Path] = None,
memorylimit: int = 0,
querytimeout: int = 0,
Expand Down Expand Up @@ -240,7 +240,6 @@ def query(self, query: str, make_result: bool = False) -> Tuple[Optional[Any], i
p_result = ffi.NULL

affected_rows = ffi.new("monetdbe_cnt *")

check_error(lib.monetdbe_query(self._connection, query.encode(), p_result, affected_rows))

if make_result:
Expand All @@ -250,18 +249,18 @@ def query(self, query: str, make_result: bool = False) -> Tuple[Optional[Any], i

return result, affected_rows[0]

def result_fetch(self, result: ffi.CData, column: int):
@staticmethod
def result_fetch(result: ffi.CData, column: int):
p_rcol = ffi.new("monetdbe_column **")
check_error(lib.monetdbe_result_fetch(result, p_rcol, column))
return p_rcol[0]

def result_fetch_numpy(self, monetdbe_result: ffi.CData):
@staticmethod
def result_fetch_numpy(monetdbe_result: ffi.CData):

result = {}
for c in range(monetdbe_result.ncols):
p_rcol = ffi.new("monetdbe_column **")
check_error(lib.monetdbe_result_fetch(monetdbe_result, p_rcol, c))
rcol = p_rcol[0]
rcol = MonetEmbedded.result_fetch(monetdbe_result, c)
name = make_string(rcol.name)
cast_string, cast_function, numpy_type, monetdbe_null = type_map[rcol.type]

Expand All @@ -287,7 +286,8 @@ def result_fetch_numpy(self, monetdbe_result: ffi.CData):
def set_autocommit(self, value: bool):
check_error(lib.monetdbe_set_autocommit(self._connection, int(value)))

def get_autocommit(self):
@staticmethod
def get_autocommit():
value = ffi.new("int *")
check_error(lib.monetdbe_get_autocommit(value))
return value[0]
Expand Down
36 changes: 11 additions & 25 deletions monetdbe/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@
import pandas as pd

from monetdbe.connection import Connection
from monetdbe.exceptions import ProgrammingError, Warning, InterfaceError
from monetdbe.exceptions import ProgrammingError, InterfaceError
from monetdbe.formatting import format_query, strip_split_and_clean
from monetdbe.monetize import monet_identifier_escape

Description = namedtuple('Description', ('name', 'type_code', 'display_size', 'internal_size', 'precision', 'scale',
'null_ok'))


def __convert_pandas_to_numpy_dict__(df):
if type(df) == pd.DataFrame:
res = {}
for tpl in df.to_dict().items():
res[tpl[0]] = np.array(list(tpl[1].values()))
return res
return df
def _pandas_to_numpy_dict(df: pd.DataFrame):
return {k: np.array(list(v.values())) for k, v in df.to_dict().items()}


class Cursor:
Expand Down Expand Up @@ -314,7 +309,7 @@ def create(self, table, values, schema=None):
column_types = []

if not isinstance(values, dict):
values = __convert_pandas_to_numpy_dict__(values)
values = _pandas_to_numpy_dict(values)
else:
vals = {}
for tpl in values.items():
Expand Down Expand Up @@ -369,33 +364,24 @@ def insert(self, table: str, values: Union[pd.DataFrame, Dict[str, np.ndarray]],
schema: The SQL schema to use. If no schema is specified, the "sys" schema is used.
"""

if not isinstance(values, dict):
values = __convert_pandas_to_numpy_dict__(values)
else:
vals = {}
for tpl in values.items():
if isinstance(tpl[1], np.ma.core.MaskedArray):
vals[tpl[0]] = tpl[1]
else:
vals[tpl[0]] = np.array(tpl[1])
values = vals
if isinstance(values, pd.DataFrame):
values = _pandas_to_numpy_dict(values)

if isinstance(values, dict):
column_names = values.keys()
rows = values.values()
for key, value in values.items():
if not isinstance(value, np.ma.core.MaskedArray):
values[key] = np.array(value)

column_names, rows = zip(*values.items())
columns = ", ".join([str(i) for i in column_names])
rows_zipped = list(zip(*rows))

qmarks = ", ".join(['?'] * len(column_names))

query = f"insert into {schema}.{table} ({columns}) values ({qmarks})"
return self.executemany(query, rows_zipped)

elif isinstance(values, list):
rows_zipped = list(zip(*values))

qmarks = ", ".join(['?'] * len(values))

query = f"insert into {schema}.{table} values ({qmarks})"
return self.executemany(query, rows_zipped)

Expand Down
17 changes: 16 additions & 1 deletion tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@

import datetime
import functools
import gc
import unittest
import weakref
import gc

import pandas as pd
import numpy as np

import monetdbe as monetdbe

Expand Down Expand Up @@ -435,3 +438,15 @@ def test_proper_error_on_empty_query_issue63(self):

with self.assertRaises(monetdbe.ProgrammingError):
conn.execute(";")

def test_real_issue83(self):
conn = monetdbe.connect(':memory:')
cursor = conn.cursor()
cursor.execute('CREATE TABLE "test"("a" REAL);')

df = pd.DataFrame({'a': [1, 2, 3, 4]}, dtype=np.float64)
cursor.insert('test', df)

cursor.execute('SELECT * FROM "test"')
df_out = cursor.fetchdf()
pd.testing.assert_frame_equal(df, df_out)

0 comments on commit 6334436

Please sign in to comment.