Segfault with binary column loader
This produces a segfault with master as of yesterday:
import pymapd # to create the connection. Not using the client otherwise
from mapd.ttypes import TColumn, TColumnData
con = pymapd.connect(user='mapd', password='HyperInteractive',
host='localhost', dbname='mapd')
print(con)
con.execute("drop table if exists segfault;")
con.execute("create table segfault (f DECIMAL(10), g INT);")
c, s = con._client, con._session
nulls = [False] * 3
data = [
TColumn(data=TColumnData(real_col=[1.0, 2.0, 3.0]), nulls=nulls),
TColumn(data=TColumnData(int_col=[1, 2, 3]), nulls=nulls)
]
c.load_table_binary_columnar(s, 'segfault', data)
Heres the traceback:
Traceback (most recent call last):
File "bug.py", line 18, in <module>
c.load_table_binary_columnar(s, 'segfault', data)
File "/Users/taugspurger/sandbox/pymapd/mapd/MapD.py", line 1572, in load_table_binary_columnar
self.recv_load_table_binary_columnar()
File "/Users/taugspurger/sandbox/pymapd/mapd/MapD.py", line 1586, in recv_load_table_binary_columnar
(fname, mtype, rseqid) = iprot.readMessageBegin()
File "/Users/taugspurger/miniconda3/envs/mapd-dev/lib/python3.6/site-packages/thrift/protocol/TBinaryProtocol.py", line 134, in readMessageBegin
sz = self.readI32()
File "/Users/taugspurger/miniconda3/envs/mapd-dev/lib/python3.6/site-packages/thrift/protocol/TBinaryProtocol.py", line 217, in readI32
buff = self.trans.readAll(4)
File "/Users/taugspurger/miniconda3/envs/mapd-dev/lib/python3.6/site-packages/thrift/transport/TTransport.py", line 60, in readAll
chunk = self.read(sz - have)
File "/Users/taugspurger/miniconda3/envs/mapd-dev/lib/python3.6/site-packages/thrift/transport/TTransport.py", line 161, in read
self.__rbuf = BufferIO(self.__trans.read(max(sz, self.__rbuf_size)))
File "/Users/taugspurger/miniconda3/envs/mapd-dev/lib/python3.6/site-packages/thrift/transport/TSocket.py", line 132, in read
message='TSocket read 0 bytes')
thrift.transport.TTransport.TTransportException: TSocket read 0 bytes
The output from the server's console is Segmentation fault: 11
Perhaps interestingly, the order seems to matter.
# change the order of the create
con.execute("create table segfault (g INT, f DECIMAL(10));")
# and the data
data = [
TColumn(data=TColumnData(int_col=[1, 2, 3]), nulls=nulls),
TColumn(data=TColumnData(real_col=[1.0, 2.0, 3.0]), nulls=nulls),
]
Does not produce a segfault.
Small update: it seems like it's using the size of the first column's data to allocate the buffers for the rest, which is problematic if the columns somehow are different lengths. Should be a quick fix, throwing if the lengths are different.
The ordering appeared to matter because DECIMAL columns should actually be integers / stored in int_col. So putting the decimal column first with the data in a real_col made it appear like there were zero rows to import.
Ah, I see.
I was surprised to see that Thrift used the real_val field for decimals, since they aren't going to actually be fixed precision:
In [15]: import pymapd
In [16]: con = pymapd.connect(user='mapd', password='HyperInteractive', host='localhost', dbname='mapd')
In [18]: con.execute("create table decimal_test (a DECIMAL(8));")
Out[18]: <pymapd.cursor.Cursor at 0x1143e8a20>
In [19]: con.execute("INSERT INTO decimal_test VALUES ('1.123');")
Out[19]: <pymapd.cursor.Cursor at 0x114411da0>
In [20]: c, s = con._client, con._session
In [21]: c.sql_execute(s, "select * from decimal_test", 0, None, -1).row_set.rows
Out[21]: [TRow(cols=[TDatum(val=TDatumVal(int_val=0, real_val=1.0, str_val=''), is_null=False)])]
Does that look like a bug?
What is confusing here is that the behaviour going into MapD is different than coming back.
When receiving a DECIMAL field it comes back as a REAL
When sending in a DECIMAL field it is sent as a LONG with the number modified based on the scale. So if a DECIMAL(10,2) is declare what load_table_binary_columnar would expect is a LONG If the number 23.56 was being loaded we would expect the LONG to contain 2356.
The code has also been changed to detect inconsistent lengths in the fields being loaded to avoid this kind of segfault.
Will close this issue.