Skip to content

Commit cd3b71b

Browse files
committed
BUG27535063: Fix wrong error message when specifying the collation for a non TEXT field
When creating an index on a collection, if a collation is specified but the field is not of the type TEXT, then an error message is generated but with a wrong field type. This patch fixes the issue by setting the correct field type. Previously it always raised with "GEOJSON" for all field types. Tests were added for regression.
1 parent 675eea6 commit cd3b71b

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

lib/mysqlx/statement.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2016, 2020, Oracle and/or its affiliates.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0, as
@@ -1405,25 +1405,23 @@ def execute(self):
14051405
if not constraint["type"].upper().startswith("TEXT"):
14061406
raise ProgrammingError(
14071407
"The 'collation' member can only be used when "
1408-
"field type is set to 'GEOJSON'")
1409-
else:
1410-
constraint["collation"] = field_desc.pop("collation")
1408+
"field type is set to '{}'"
1409+
"".format(constraint["type"].upper()))
1410+
constraint["collation"] = field_desc.pop("collation")
14111411
# "options" and "srid" fields in IndexField can be
14121412
# present only if "type" is set to "GEOJSON"
14131413
if "options" in field_desc:
14141414
if constraint["type"].upper() != "GEOJSON":
14151415
raise ProgrammingError(
14161416
"The 'options' member can only be used when "
14171417
"index type is set to 'GEOJSON'")
1418-
else:
1419-
constraint["options"] = field_desc.pop("options")
1418+
constraint["options"] = field_desc.pop("options")
14201419
if "srid" in field_desc:
14211420
if constraint["type"].upper() != "GEOJSON":
14221421
raise ProgrammingError(
14231422
"The 'srid' member can only be used when index "
14241423
"type is set to 'GEOJSON'")
1425-
else:
1426-
constraint["srid"] = field_desc.pop("srid")
1424+
constraint["srid"] = field_desc.pop("srid")
14271425
args["constraint"].append(constraint)
14281426
except KeyError as err:
14291427
raise ProgrammingError("Required inner member {} not found in "

tests/test_mysqlx_crud.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2016, 2020, Oracle and/or its affiliates.
44
#
55
# This program is free software; you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License, version 2.0, as
@@ -2152,6 +2152,29 @@ def test_create_index(self):
21522152
create_index = collection.create_index(index_name, index_desc)
21532153
self.assertRaises(TypeError, create_index.execute)
21542154

2155+
# check error message for wrong collation
2156+
err_msg = ("The 'collation' member can only be used when field type "
2157+
"is set to '{}'")
2158+
index_name = "age_idx"
2159+
index_desc = {"fields": [{"field": "$.age", "type": "INT",
2160+
"collation": "utf8mb4_0900_ai_ci"}],
2161+
"type": "INDEX"}
2162+
create_index = collection.create_index(index_name, index_desc)
2163+
try:
2164+
create_index.execute()
2165+
except mysqlx.ProgrammingError as err:
2166+
self.assertEqual(err.msg, err_msg.format("INT"))
2167+
2168+
index_name = "emails_idx"
2169+
index_desc = {"fields": [{"field": "$.emails", "type": "CHAR(128)",
2170+
"collation": "utf8mb4_0900_ai_ci"}],
2171+
"type": "INDEX"}
2172+
create_index = collection.create_index(index_name, index_desc)
2173+
try:
2174+
create_index.execute()
2175+
except mysqlx.ProgrammingError as err:
2176+
self.assertEqual(err.msg, err_msg.format("CHAR(128)"))
2177+
21552178
self.schema.drop_collection(collection_name)
21562179

21572180
@unittest.skipIf(tests.MYSQL_VERSION < (8, 0, 4), "Dev API change")

0 commit comments

Comments
 (0)