Skip to content

Commit bb7767b

Browse files
SQL: suppress warning for BIGINT with sqlite and sqlalchemy<0.8.2 (GH7433)
1 parent 9d71aae commit bb7767b

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

pandas/io/sql.py

+34-12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ class DatabaseError(IOError):
2929
#------------------------------------------------------------------------------
3030
# Helper functions
3131

32+
_SQLALCHEMY_INSTALLED = None
33+
34+
def _is_sqlalchemy_engine(con):
35+
global _SQLALCHEMY_INSTALLED
36+
if _SQLALCHEMY_INSTALLED is None:
37+
try:
38+
import sqlalchemy
39+
_SQLALCHEMY_INSTALLED = True
40+
41+
from distutils.version import LooseVersion
42+
ver = LooseVersion(sqlalchemy.__version__)
43+
# For sqlalchemy versions < 0.8.2, the BIGINT type is recognized
44+
# for a sqlite engine, which results in a warning when trying to
45+
# read/write a DataFrame with int64 values. (GH7433)
46+
if ver < '0.8.2':
47+
from sqlalchemy import BigInteger
48+
from sqlalchemy.ext.compiler import compiles
49+
50+
@compiles(BigInteger, 'sqlite')
51+
def compile_big_int_sqlite(type_, compiler, **kw):
52+
return 'INTEGER'
53+
except ImportError:
54+
_SQLALCHEMY_INSTALLED = False
55+
56+
if _SQLALCHEMY_INSTALLED:
57+
import sqlalchemy
58+
return isinstance(con, sqlalchemy.engine.Engine)
59+
else:
60+
return False
61+
62+
3263
def _convert_params(sql, params):
3364
"""convert sql and params args to DBAPI2.0 compliant format"""
3465
args = [sql]
@@ -76,17 +107,6 @@ def _parse_date_columns(data_frame, parse_dates):
76107
return data_frame
77108

78109

79-
def _is_sqlalchemy_engine(con):
80-
try:
81-
import sqlalchemy
82-
if isinstance(con, sqlalchemy.engine.Engine):
83-
return True
84-
else:
85-
return False
86-
except ImportError:
87-
return False
88-
89-
90110
def execute(sql, con, cur=None, params=None):
91111
"""
92112
Execute the given SQL query using the provided connection object.
@@ -271,8 +291,10 @@ def read_sql_table(table_name, con, index_col=None, coerce_float=True,
271291
read_sql_query : Read SQL query into a DataFrame.
272292
read_sql
273293
274-
275294
"""
295+
if not _is_sqlalchemy_engine(con):
296+
raise NotImplementedError("read_sql_table only supported for "
297+
"SQLAlchemy engines.")
276298
import sqlalchemy
277299
from sqlalchemy.schema import MetaData
278300
meta = MetaData(con)

pandas/io/tests/test_sql.py

+10
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,16 @@ def test_default_date_load(self):
10791079
self.assertFalse(issubclass(df.DateCol.dtype.type, np.datetime64),
10801080
"DateCol loaded with incorrect type")
10811081

1082+
def test_bigint_warning(self):
1083+
# test no warning for BIGINT (to support int64) is raised (GH7433)
1084+
df = DataFrame({'a':[1,2]}, dtype='int64')
1085+
df.to_sql('test_bigintwarning', self.conn, index=False)
1086+
1087+
with warnings.catch_warnings(record=True) as w:
1088+
warnings.simplefilter("always")
1089+
sql.read_sql_table('test_bigintwarning', self.conn)
1090+
self.assertEqual(len(w), 0, "Warning triggered for other table")
1091+
10821092

10831093
class TestMySQLAlchemy(_TestSQLAlchemy):
10841094
"""

0 commit comments

Comments
 (0)