Skip to content

Commit

Permalink
Fix for CONPY-116: Wrong type reported for SQL type JSON
Browse files Browse the repository at this point in the history
Beginning of MariaDB 10.5 metadata for JSON columns is stored
in extended field information, and the reported type is MYSQL_TYPE_BLOB.
We now check extended field information to return correct type and for
fetching values in correct format.
  • Loading branch information
9EOR9 committed Sep 29, 2020
1 parent 6ab09b2 commit 5d4a8d5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
8 changes: 8 additions & 0 deletions include/mariadb_python.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ enum enum_dataapi_groups
DBAPI_ROWID
};

enum enum_extended_field_type
{
EXT_TYPE_NONE=0,
EXT_TYPE_JSON=1
};

enum enum_result_format
{
RESULT_TUPLE= 0,
Expand Down Expand Up @@ -322,6 +328,8 @@ mariadb_throw_exception(void *handle,
const char *message,
...);

enum enum_extended_field_type mariadb_extended_field_type(const MYSQL_FIELD *field);

PyObject *
MrdbIndicator_Object(uint32_t type);

Expand Down
22 changes: 18 additions & 4 deletions mariadb/mariadb_codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ int codecs_datetime_init(void)
return 0;
}


enum enum_extended_field_type mariadb_extended_field_type(const MYSQL_FIELD *field)
{
MARIADB_CONST_STRING str;

if (!mariadb_field_attr(&str, field, MARIADB_FIELD_ATTR_FORMAT_NAME))
{
if (str.length == 4 && !strncmp(str.str, "json", 4))
return EXT_TYPE_JSON;
}
return EXT_TYPE_NONE;
}
/*
converts a Python date/time/datetime object to MYSQL_TIME
*/
Expand Down Expand Up @@ -503,8 +515,8 @@ field_fetch_fromtext(MrdbCursor *self, char *data, unsigned int column)
case MYSQL_TYPE_ENUM:
{
unsigned long len;

if ( self->fields[column].charsetnr == CHARSET_BINARY)
enum enum_extended_field_type ext_type= mariadb_extended_field_type(&self->fields[column]);
if ( self->fields[column].charsetnr == CHARSET_BINARY && ext_type != EXT_TYPE_JSON)
{
self->values[column]=
PyBytes_FromStringAndSize((const char *)data,
Expand Down Expand Up @@ -735,9 +747,11 @@ field_fetch_callback(void *data, unsigned int column, unsigned char **row)
unsigned long length;
unsigned long utf8len;
length= mysql_net_field_length(row);
enum enum_extended_field_type ext_type= mariadb_extended_field_type(&self->fields[column]);

if (self->fields[column].flags & BINARY_FLAG ||
self->fields[column].charsetnr == CHARSET_BINARY)
if ((self->fields[column].flags & BINARY_FLAG ||
self->fields[column].charsetnr == CHARSET_BINARY) &&
ext_type != EXT_TYPE_JSON)
{
self->values[column]=
PyBytes_FromStringAndSize((const char *)*row,
Expand Down
4 changes: 4 additions & 0 deletions mariadb/mariadb_cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ PyObject *MrdbCursor_description(MrdbCursor *self)
unsigned long display_length;
long packed_len= 0;
PyObject *desc;
enum enum_extended_field_type ext_type= mariadb_extended_field_type(&self->fields[i]);

display_length= self->fields[i].max_length > self->fields[i].length ?
self->fields[i].max_length : self->fields[i].length;
Expand All @@ -906,6 +907,9 @@ PyObject *MrdbCursor_description(MrdbCursor *self)
}
}

if (ext_type == EXT_TYPE_JSON)
self->fields[i].type= MYSQL_TYPE_JSON;

if (!(desc= Py_BuildValue("(sIIiIIOI)",
self->fields[i].name,
self->fields[i].type,
Expand Down
3 changes: 2 additions & 1 deletion testing/test/integration/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def test_xfield_types(self):
fieldinfo = mariadb.fieldinfo()
cursor.execute(
"CREATE TEMPORARY TABLE test_xfield_types (a tinyint not null auto_increment primary "
"key, b smallint, c int, d bigint, e float, f decimal, g double, h char(10), i varchar(255), j blob, index(b))");
"key, b smallint, c int, d bigint, e float, f decimal, g double, h char(10), i varchar(255), j blob, k json, index(b))");
info = cursor.description
self.assertEqual(info, None)
cursor.execute("SELECT * FROM test_xfield_types")
Expand All @@ -243,6 +243,7 @@ def test_xfield_types(self):
self.assertEqual(fieldinfo.type(info[7]), "STRING")
self.assertEqual(fieldinfo.type(info[8]), "VAR_STRING")
self.assertEqual(fieldinfo.type(info[9]), "BLOB")
self.assertEqual(fieldinfo.type(info[10]), "JSON")
self.assertEqual(fieldinfo.flag(info[0]),
"NOT_NULL | PRIMARY_KEY | AUTO_INCREMENT | NUMERIC")
self.assertEqual(fieldinfo.flag(info[1]), "PART_KEY | NUMERIC")
Expand Down

0 comments on commit 5d4a8d5

Please sign in to comment.