Skip to content

Commit 8cdb2df

Browse files
authored
Merge pull request #3149 from cds-astro/fix-query-objects
fix: query objects
2 parents 054526a + 7d96ebd commit 8cdb2df

File tree

6 files changed

+61
-38
lines changed

6 files changed

+61
-38
lines changed

CHANGES.rst

+3
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

+25-17
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"))
@@ -716,28 +718,29 @@ def query_region(self, coordinates, radius=2*u.arcmin, *,
716718
Examples
717719
--------
718720
719-
Look for large galaxies in two cones
721+
Look for largest galaxies in two cones
720722
721723
>>> from astroquery.simbad import Simbad
722724
>>> from astropy.coordinates import SkyCoord
723725
>>> simbad = Simbad()
724726
>>> simbad.ROW_LIMIT = 5
725-
>>> simbad.add_votable_fields("otype") # doctest: +REMOTE_DATA
727+
>>> simbad.add_votable_fields("otype", "dim") # doctest: +REMOTE_DATA
726728
>>> coordinates = SkyCoord([SkyCoord(186.6, 12.7, unit=("deg", "deg")),
727729
... SkyCoord(170.75, 23.9, unit=("deg", "deg"))])
728730
>>> result = simbad.query_region(coordinates, radius="2d5m",
729-
... criteria="otype = 'Galaxy..' AND galdim_majaxis>8") # doctest: +REMOTE_DATA
730-
>>> result.sort("main_id") # doctest: +REMOTE_DATA
731-
>>> result["main_id", "otype"] # doctest: +REMOTE_DATA
731+
... criteria="otype = 'Galaxy..' AND galdim_majaxis>8.5") # doctest: +REMOTE_DATA
732+
>>> result.sort("galdim_majaxis", reverse=True) # doctest: +REMOTE_DATA
733+
>>> result["main_id", "otype", "galdim_majaxis"] # doctest: +REMOTE_DATA
732734
<Table length=5>
733-
main_id otype
734-
object object
735-
------------ ------
736-
LEDA 40577 GiG
737-
LEDA 41362 GiC
738-
M 86 GiG
739-
M 87 AGN
740-
NGC 4438 LIN
735+
main_id otype galdim_majaxis
736+
arcmin
737+
object object float32
738+
------------ ------ --------------
739+
LEDA 41362 GiC 11.0
740+
M 86 GiG 10.47
741+
LEDA 40917 AG? 10.3
742+
M 87 AGN 9.12
743+
NGC 4438 LIN 8.91
741744
742745
Notes
743746
-----
@@ -1332,7 +1335,7 @@ def query_tap(self, query: str, *, maxrec=10000, get_query_payload=False, **uplo
13321335
... my_table_name=letters_table) # doctest: +REMOTE_DATA
13331336
<Table length=3>
13341337
alphabet
1335-
object
1338+
str1
13361339
--------
13371340
a
13381341
b
@@ -1415,7 +1418,12 @@ def _query(self, top, columns, joins, criteria, from_table="basic",
14151418
else:
14161419
unique_joins = []
14171420
[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}."'
1421+
# the joined tables can have an alias. We handle the two cases here
1422+
join = " " + " ".join([(f'{join.join_type} {join.table} AS {join.alias} '
1423+
f'ON {join.column_left.table}."{join.column_left.name}" = '
1424+
f'{join.alias}."{join.column_right.name}"')
1425+
if join.alias is not None else
1426+
(f'{join.join_type} {join.table} ON {join.column_left.table}."'
14191427
f'{join.column_left.name}" = {join.column_right.table}."'
14201428
f'{join.column_right.name}"') for join in unique_joins])
14211429

astroquery/simbad/tests/test_simbad.py

+4-3
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

+7
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")

docs/simbad/query_tap.rst

+12-9
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,25 @@ that is the measurement table for rotations. Their common column is ``oidref``.
195195
.. doctest-remote-data::
196196

197197
>>> from astroquery.simbad import Simbad
198-
>>> query = """SELECT bibcode AS "Rotation Measurements Bibcodes"
198+
>>> query = """SELECT DISTINCT bibcode AS "Rotation Measurements Bibcodes"
199199
... FROM ident JOIN mesrot USING(oidref)
200200
... WHERE id = 'Sirius';
201201
... """
202-
>>> Simbad.query_tap(query)
203-
<Table length=7>
202+
>>> bibcodes = Simbad.query_tap(query)
203+
>>> bibcodes.sort("Rotation Measurements Bibcodes")
204+
>>> bibcodes
205+
<Table length=8>
204206
Rotation Measurements Bibcodes
205-
object
207+
object
206208
------------------------------
207-
2023ApJS..266...11B
208-
2016A&A...589A..83G
209-
2002A&A...393..897R
210-
1995ApJS...99..135A
211-
1970CoKwa.189....0U
212209
1970CoAsi.239....1B
210+
1970CoKwa.189....0U
211+
1995ApJS...99..135A
212+
2002A&A...393..897R
213+
2005yCat.3244....0G
213214
2011A&A...531A.143D
215+
2016A&A...589A..83G
216+
2023ApJS..266...11B
214217

215218
This returns six papers in which the SIMBAD team found rotation data for Sirius.
216219

docs/simbad/simbad.rst

+10-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ to query the messier object M1:
6666
main_id ra dec ... coo_wavelength coo_bibcode matched_id
6767
deg deg ...
6868
------- ------- ------- ... -------------- ------------------- ----------
69-
M 1 83.6287 22.0147 ... R 1995AuJPh..48..143S M 1
69+
M 1 83.6324 22.0174 ... X 2022A&A...661A..38P M 1
7070

7171
`Wildcards`_ are supported. Note that this makes the query case-sensitive.
7272
This allows, for instance, to query messier objects from 1 through 9:
@@ -203,12 +203,12 @@ If the center is defined by coordinates, then the best solution is to use a
203203
>>> Simbad.query_region(SkyCoord(31.0087, 14.0627, unit=(u.deg, u.deg),
204204
... frame='galactic'), radius=2 * u.arcsec)
205205
<Table length=2>
206-
main_id ra ... coo_wavelength coo_bibcode
207-
deg ...
208-
object float64 ... str1 object
209-
------------------- ----------------- ... -------------- -------------------
210-
GJ 699 b 269.4520769586187 ... O 2020yCat.1350....0G
211-
NAME Barnard's star 269.4520769586187 ... O 2020yCat.1350....0G
206+
main_id ra ... coo_wavelength coo_bibcode
207+
deg ...
208+
object float64 ... str1 object
209+
--------------------- ----------------- ... -------------- -------------------
210+
NAME Barnard's Star b 269.4520769586187 ... O 2020yCat.1350....0G
211+
NAME Barnard's star 269.4520769586187 ... O 2020yCat.1350....0G
212212

213213
.. Note::
214214

@@ -466,7 +466,7 @@ with:
466466

467467
>>> from astroquery.simbad import Simbad
468468
>>> Simbad.list_votable_fields()[["name", "description"]]
469-
<Table length=115>
469+
<Table length=...>
470470
name description
471471
object object
472472
----------- -------------------------------------------------------
@@ -584,11 +584,12 @@ constraint on the first character of the ``mespm.bibcode`` column
584584
>>> simbad.add_votable_fields("mesPM", "otype")
585585
>>> pm_measurements = simbad.query_object("BD+30 2512", criteria=criteria)
586586
>>> pm_measurements[["main_id", "mespm.pmra", "mespm.pmde", "mespm.bibcode"]]
587-
<Table length=6>
587+
<Table length=7>
588588
main_id mespm.pmra mespm.pmde mespm.bibcode
589589
mas / yr mas / yr
590590
object float32 float32 object
591591
----------- ---------- ---------- -------------------
592+
BD+30 2512 -631.6 -289.5 2016ApJ...817..112S
592593
BD+30 2512 -631.662 -308.469 2020yCat.1350....0G
593594
BD+30 2512 -631.6 -289.5 2016ApJS..224...36K
594595
BD+30 2512 -631.625 -308.495 2018yCat.1345....0G

0 commit comments

Comments
 (0)