Skip to content

Commit fda99cf

Browse files
committed
fix: allow to add the votable field 'ident' in query_objects
this is fixed by setting an alias for the ident table in the join, so that the ambiguity is lifted with the join done by add_votable_field
1 parent d27ac56 commit fda99cf

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ simbad
149149
A helper method was added ``astroquery.simbad.utils.CriteriaTranslator`` to
150150
translate between the sim-script syntax and the TAP/ADQL syntax. [#2954]
151151

152+
- fixed ``query_objects`` that would not work in combination with the additional field
153+
``ident`` [#3149]
154+
152155
skyview
153156
^^^^^^^
154157

astroquery/simbad/core.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class _Join:
9090
column_left: Any
9191
column_right: Any
9292
join_type: str = field(default="JOIN")
93+
alias: str = field(default=None)
9394

9495

9596
class SimbadClass(BaseVOQuery):
@@ -670,10 +671,11 @@ def query_objects(self, object_names, *, wildcard=False, criteria=None,
670671
upload_name = "TAP_UPLOAD.script_infos"
671672
columns.append(_Column(upload_name, "*"))
672673

674+
# join on ident needs an alias in case the users want to add the votable field ident
673675
left_joins = [_Join("ident", _Column(upload_name, "user_specified_id"),
674-
_Column("ident", "id"), "LEFT JOIN"),
676+
_Column("ident", "id"), "LEFT JOIN", "ident_upload"),
675677
_Join("basic", _Column("basic", "oid"),
676-
_Column("ident", "oidref"), "LEFT JOIN")]
678+
_Column("ident_upload", "oidref"), "LEFT JOIN")]
677679
for join in joins:
678680
left_joins.append(_Join(join.table, join.column_left,
679681
join.column_right, "LEFT JOIN"))
@@ -1415,7 +1417,12 @@ def _query(self, top, columns, joins, criteria, from_table="basic",
14151417
else:
14161418
unique_joins = []
14171419
[unique_joins.append(join) for join in joins if join not in unique_joins]
1418-
join = " " + " ".join([(f'{join.join_type} {join.table} ON {join.column_left.table}."'
1420+
# the joined tables can have an alias. We handle the two cases here
1421+
join = " " + " ".join([(f'{join.join_type} {join.table} AS {join.alias} '
1422+
f'ON {join.column_left.table}."{join.column_left.name}" = '
1423+
f'{join.alias}."{join.column_right.name}"')
1424+
if join.alias is not None else
1425+
(f'{join.join_type} {join.table} ON {join.column_left.table}."'
14191426
f'{join.column_left.name}" = {join.column_right.table}."'
14201427
f'{join.column_right.name}"') for join in unique_joins])
14211428

astroquery/simbad/tests/test_simbad.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,10 @@ def test_query_objects():
408408
# no wildcard and additional criteria
409409
adql = simbad.core.Simbad.query_objects(("m1", "m2"), criteria="otype = 'Galaxy..'",
410410
get_query_payload=True)["QUERY"]
411-
expected = ('FROM TAP_UPLOAD.script_infos LEFT JOIN ident ON TAP_UPLOAD.script_infos.'
412-
'"user_specified_id" = ident."id" LEFT JOIN basic ON basic."oid" = ident."oidref"'
413-
' WHERE (otype = \'Galaxy..\')')
411+
expected = ('FROM TAP_UPLOAD.script_infos LEFT JOIN ident AS ident_upload '
412+
'ON TAP_UPLOAD.script_infos.'
413+
'"user_specified_id" = ident_upload."id" LEFT JOIN basic '
414+
'ON basic."oid" = ident_upload."oidref" WHERE (otype = \'Galaxy..\')')
414415
assert adql.endswith(expected)
415416
# with wildcard
416417
adql = simbad.core.Simbad.query_objects(("M *", "NGC *"), wildcard=True, get_query_payload=True)["QUERY"]

astroquery/simbad/tests/test_simbad_remote.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,10 @@ def test_add_votable_fields(self):
204204
simbad_instance.add_votable_fields("u")
205205
result = simbad_instance.query_object("HD 147933")
206206
assert all(filtername in result.colnames for filtername in {"u", "U", "V"})
207+
208+
def test_double_ident_in_query_objects(self):
209+
simbad = Simbad()
210+
simbad.add_votable_fields("ident")
211+
result = simbad.query_objects(['HD 1'])
212+
assert len(result) > 1
213+
assert all(result["main_id"] == "HD 1")

0 commit comments

Comments
 (0)