Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug of mongo_fdw in Mar-2022 #159

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion expected/select.out
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,15 @@ SELECT a FROM f_test_tbl6 ORDER BY 1;
(4 rows)

SELECT a FROM f_test_tbl7 ORDER BY 1;
ERROR: value "9999999999" is out of range for type integer
a
------------
0
25
25
25
1410065407
(5 rows)

-- Cleanup
DELETE FROM f_mongo_test WHERE a != 0;
DROP TABLE l_test_tbl1;
Expand Down
32 changes: 26 additions & 6 deletions mongo_fdw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,8 @@ column_types_compatible(BSON_TYPE bsonType, Oid columnTypeId)
* object identifier. We can safely overload this 64-byte data
* type since it's reserved for internal use in PostgreSQL.
*/
if (bsonType == BSON_TYPE_OID)
if (bsonType == BSON_TYPE_OID ||
bsonType == BSON_TYPE_UTF8)
compatibleTypes = true;
break;
case DATEOID:
Expand Down Expand Up @@ -2073,14 +2074,33 @@ column_value(BSON_ITERATOR *bsonIterator, Oid columnTypeId,
break;
case NAMEOID:
{
char value[NAMEDATALEN];
const char *name_val;
BSON_TYPE bsonType = bsonIterType(bsonIterator);
Datum valueDatum = 0;

bson_oid_t *bsonObjectId = (bson_oid_t *) bsonIterOid(bsonIterator);

bson_oid_to_string(bsonObjectId, value);
switch (bsonType)
{
case BSON_TYPE_OID:
{
char value[NAMEDATALEN];
bson_oid_t *bsonObjectId = (bson_oid_t *) bsonIterOid(bsonIterator);

valueDatum = CStringGetDatum(value);
bson_oid_to_string(bsonObjectId, value);
name_val = value;
}
break;
case BSON_TYPE_UTF8:
{
name_val = bsonIterString(bsonIterator);
}
break;
default:
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("cannot convert BSON type to column type"),
errhint("Column type: %u", (uint32) columnTypeId)));
}
valueDatum = CStringGetDatum(name_val);
columnValue = DirectFunctionCall3(namein, valueDatum,
ObjectIdGetDatum(InvalidOid),
Int32GetDatum(columnTypeMod));
Expand Down
4 changes: 2 additions & 2 deletions mongo_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ bsonIterInt32(BSON_ITERATOR *it)
{
int64 val = bson_iterator_long_raw(it);

if (val < PG_INT32_MIN || val > PG_INT32_MAX)
if (val < PG_INT64_MIN || val > PG_INT64_MAX)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%ld\" is out of range for type integer",
val)));

return (int32) val;
return (int64) val;
}
case BSON_INT:
return bson_iterator_int_raw(it);
Expand Down
4 changes: 2 additions & 2 deletions mongo_wrapper_meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,13 @@ bsonIterInt32(BSON_ITERATOR *it)
{
int64 val = bson_iter_int64(it);

if (val < PG_INT32_MIN || val > PG_INT32_MAX)
if (val < PG_INT64_MIN || val > PG_INT64_MAX)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%ld\" is out of range for type integer",
val)));

return (int32) val;
return (int64) val;
}
case BSON_TYPE_INT32:
return bson_iter_int32(it);
Expand Down