/
queries.py
606 lines (465 loc) · 23.9 KB
/
queries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# batteries-included sqlalchemy queries for sqlite3 db
import functools
import logging
import sqlalchemy as sa
from sqlalchemy import select
from sqlalchemy.orm import aliased
from ._globals import ENGINE
from . import _tools
from . import backend as _backend
from .backend import views as _views
from .models import (LEVEL, FAMILY, LANGUAGE, DIALECT,
SPECIAL_FAMILIES, BOOKKEEPING,
ALTNAME_PROVIDER, IDENTIFIER_SITE,
Languoid,
languoid_macroarea,
languoid_country, Country,
Link, Source, SourceProvider, Timespan, Bibfile, Bibitem,
Altname, AltnameProvider, Trigger,
Identifier, IdentifierSite,
ClassificationComment, ClassificationRef,
Endangerment, EndangermentSource,
EthnologueComment,
IsoRetirement, IsoRetirementChangeTo)
__all__ = ['get_stats_query',
'get_query',
'get_json_query',
'iterdescendants']
log = logging.getLogger(__name__)
@_views.register_view('stats')
def get_stats_query():
# cf. https://glottolog.org/glottolog/glottologinformation
def languoid_count(kind, cls=Languoid, fromclause=Languoid,
level=None, is_root=None):
select_nrows = (select(sa.literal(kind).label('kind'),
sa.func.count().label('n'))
.select_from(fromclause))
if level is not None:
select_nrows = select_nrows.where(cls.level == level)
if is_root is not None:
cond = (cls.parent == None) if is_root else (cls.parent != None)
select_nrows = select_nrows.where(cond)
return select_nrows
Root, Child, root_child = Languoid.parent_descendant(innerjoin='reflexive',
parent_root=True)
language_count = functools.partial(languoid_count,
cls=Child, fromclause=root_child,
level=LANGUAGE)
def iterselects():
yield languoid_count('languoids')
yield languoid_count('families', level=FAMILY, is_root=True)
yield languoid_count('isolates', level=LANGUAGE, is_root=True)
yield languoid_count('roots', is_root=True)
yield languoid_count('languages', level=LANGUAGE)
yield languoid_count('subfamilies', level=FAMILY, is_root=False)
yield languoid_count('dialects', level=DIALECT)
yield (language_count('Spoken L1 Languages')
.where(Root.name.notin_(SPECIAL_FAMILIES + (BOOKKEEPING,))))
for name in SPECIAL_FAMILIES:
yield language_count(name).where(Root.name == name)
yield language_count('All').where(Root.name != BOOKKEEPING)
yield language_count(BOOKKEEPING).where(Root.name == BOOKKEEPING)
return sa.union_all(*iterselects())
def _ordered_by(select_languoid, path, *, ordered):
if ordered is False:
pass
elif ordered in (True, 'id'):
select_languoid = select_languoid.order_by(Languoid.id)
elif ordered == 'path':
select_languoid = select_languoid.order_by(path)
else:
raise ValueError(f'ordered={ordered!r} not implemented')
return select_languoid
@_views.register_view('example')
def get_query(*, ordered='id', separator=', '):
"""Return example sqlalchemy core query (one denormalized row per languoid)."""
group_concat = lambda x: sa.func.group_concat(x, separator)
path, family, language = Languoid.path_family_language()
select_languoid = (select(Languoid.id,
Languoid.name,
Languoid.level,
Languoid.parent_id,
path.label('path'),
family.label('family_id'),
language.label('dialect_language_id'),
Languoid.hid,
Languoid.iso639_3,
Languoid.latitude,
Languoid.longitude)
.select_from(Languoid))
select_languoid = _ordered_by(select_languoid, path, ordered=ordered)
macroareas = (select(languoid_macroarea.c.macroarea_name)
.select_from(languoid_macroarea)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.order_by('macroarea_name')
.alias('lang_ma'))
macroareas = (select(group_concat(macroareas.c.macroarea_name)
.label('macroareas'))
.label('macroareas'))
select_languoid = select_languoid.add_columns(macroareas)
countries = (select(languoid_country.c.country_id)
.select_from(languoid_country)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.order_by('country_id')
.alias('lang_country'))
countries = (select(group_concat(countries.c.country_id).label('countries'))
.label('countries'))
select_languoid = select_languoid.add_columns(countries)
links = (select(Link.printf())
.select_from(Link)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.order_by(Link.ord)
.alias('lang_link'))
links = (select(group_concat(links.c.printf).label('links'))
.label('links'))
select_languoid = select_languoid.add_columns(links)
source_gl = aliased(Source, name='source_glottolog')
s_provider = aliased(SourceProvider, name='source_provider')
s_bibfile = aliased(Bibfile, name='source_bibfile')
s_bibitem = aliased(Bibitem, name='source_bibitem')
sources_glottolog = (select(source_gl.printf(s_bibfile, s_bibitem))
.select_from(source_gl)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.filter_by(provider_id=s_provider.id)
.where(s_provider.name == 'glottolog')
.filter_by(bibitem_id=s_bibitem.id)
.where(s_bibitem.bibfile_id == s_bibfile.id)
.order_by(s_bibfile.name, s_bibitem.bibkey)
.alias('lang_source_glottolog'))
sources_glottolog = (select(group_concat(sources_glottolog.c.printf)
.label('sources_glottolog'))
.label('sources_glottolog'))
select_languoid = select_languoid.add_columns(sources_glottolog)
altnames = {p: (aliased(Altname, name='altname_' + p),
aliased(AltnameProvider, name='altname_' + p + '_provider'))
for p in sorted(ALTNAME_PROVIDER)}
altnames = {p: (select(a.printf())
.select_from(a)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.filter_by(provider_id=ap.id)
.where(ap.name == p)
.order_by(a.name, a.lang)
.alias('lang_altname_' + p))
for p, (a, ap) in altnames.items()}
altnames = [select(group_concat(a.c.printf).label('altnames_' + p))
.label('altnames_' + p) for p, a in altnames.items()]
select_languoid = select_languoid.add_columns(*altnames)
triggers = {f: aliased(Trigger, name='trigger_' + f) for f in ('lgcode', 'inlg')}
triggers = {f: (select(t.trigger)
.select_from(t)
.filter_by(field=f)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.order_by(t.ord)
.alias(f'lang_trigger_{f}'))
for f, t in triggers.items()}
triggers = [select(group_concat(t.c.trigger).label('triggers_' + f))
.label('triggers_' + f) for f, t in triggers.items()]
select_languoid = select_languoid.add_columns(*triggers)
idents = {s: (aliased(Identifier, name='ident_' + s),
aliased(IdentifierSite, name='ident_' + s + '_site'))
for s in sorted(IDENTIFIER_SITE)}
identifiers = [i.identifier.label('identifier_' + s)
for s, (i, _) in idents.items()]
select_languoid = select_languoid.add_columns(*identifiers)
for s, (i, is_) in idents.items():
select_languoid = (select_languoid
.outerjoin(sa.join(i, is_, i.site_id == is_.id),
sa.and_(is_.name == s,
i.languoid_id == Languoid.id)))
subc, famc = (aliased(ClassificationComment, name='cc_' + n) for n in ('sub', 'fam'))
classification_sub = subc.comment.label('classification_sub')
classification_family = famc.comment.label('classification_family')
def crefs(kind):
ref = aliased(ClassificationRef, name='cr_' + kind)
r_bibfile = aliased(Bibfile, name='bibfile_cr_' + kind)
r_bibitem = aliased(Bibitem, name='bibitem_cr_' + kind)
refs = (select(ref.printf(r_bibfile, r_bibitem))
.select_from(ref)
.filter_by(kind=kind)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.filter_by(bibitem_id=r_bibitem.id)
.where(r_bibitem.bibfile_id == r_bibfile.id)
.order_by(ref.ord)
.alias('lang_cref_' + kind))
label = f'classification_{kind}refs'
return select(group_concat(refs.c.printf).label(label)).label(label)
classification_subrefs, classification_familyrefs = map(crefs, ('sub', 'family'))
select_languoid = (select_languoid.add_columns(classification_sub,
classification_subrefs,
classification_family,
classification_familyrefs)
.outerjoin(subc, sa.and_(subc.kind == 'sub',
subc.languoid_id == Languoid.id))
.outerjoin(famc, sa.and_(famc.kind == 'family',
famc.languoid_id == Languoid.id)))
def get_cols(model, label='{name}', ignore='id'):
cols = model.__table__.columns
if ignore:
ignore_suffix = '_' + ignore
cols = [c for c in cols if c.name != ignore
and not c.name.endswith(ignore_suffix)]
return [c.label(label.format(name=c.name)) for c in cols]
select_languoid = (select_languoid
.add_columns(*get_cols(Endangerment,
label='endangerment_{name}')))
e_bibfile = aliased(Bibfile, name='bibfile_e')
e_bibitem = aliased(Bibitem, name='bibitem_e')
endangerment_source = (EndangermentSource.printf(e_bibfile, e_bibitem)
.label('endangerment_source'))
select_languoid = (select_languoid.add_columns(endangerment_source)
.outerjoin(sa.join(Endangerment, EndangermentSource))
.outerjoin(sa.join(e_bibitem, e_bibfile)))
select_languoid = (select_languoid
.add_columns(*get_cols(EthnologueComment,
label='elcomment_{name}'))
.outerjoin(EthnologueComment))
select_languoid = (select_languoid
.add_columns(*get_cols(IsoRetirement,
label='iso_retirement_{name}'))
.outerjoin(IsoRetirement))
iso_retirement_change_to = (select(IsoRetirementChangeTo.code)
.select_from(IsoRetirementChangeTo)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.order_by(IsoRetirementChangeTo.ord)
.alias('lang_irct'))
iso_retirement_change_to = (select(group_concat(iso_retirement_change_to.c.code)
.label('iso_retirement_change_to'))
.label('iso_retirement_change_to'))
select_languoid = select_languoid.add_columns(iso_retirement_change_to)
return select_languoid
@_views.register_view('path_json', as_rows=True, load_json=False)
def get_json_query(*, ordered='id', as_rows=True, load_json=True,
path_label='path', languoid_label='json'):
# Windows, Python < 3.9: https://www.sqlite.org/download.html
json_object = sa.func.json_object
group_array = sa.func.json_group_array
group_object = sa.func.json_group_object
languoid = {'id': Languoid.id,
'parent_id': Languoid.parent_id,
'level': Languoid.level,
'name': Languoid.name,
'hid': Languoid.hid,
'iso639_3': Languoid.iso639_3,
'latitude': Languoid.latitude,
'longitude': Languoid.longitude}
macroareas = (select(languoid_macroarea.c.macroarea_name)
.select_from(languoid_macroarea)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.order_by('macroarea_name')
.alias('lang_ma'))
macroareas = (select(group_array(macroareas.c.macroarea_name)
.label('macroareas'))
.scalar_subquery())
languoid['macroareas'] = macroareas
countries = (select(Country.jsonf())
.join_from(languoid_country, Country)
.where(languoid_country.c.languoid_id == Languoid.id)
.correlate(Languoid)
.order_by(Country.printf())
.alias('lang_country'))
countries = (select(group_array(sa.func.json(countries.c.jsonf))
.label('countries'))
.scalar_subquery())
languoid['countries'] = countries
links = (select(Link.jsonf())
.select_from(Link)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.order_by(Link.ord)
.alias('lang_link'))
links = (select(group_array(links.c.jsonf).label('links'))
.scalar_subquery())
languoid['links'] = links
timespan = (select(Timespan.jsonf())
.select_from(Timespan)
.filter_by(languoid_id=Languoid.id)
.scalar_subquery())
languoid['timespan'] = timespan
s_provider = aliased(SourceProvider, name='source_provider')
s_bibfile = aliased(Bibfile, name='source_bibfile')
s_bibitem = aliased(Bibitem, name='source_bibitem')
sources = (select(s_provider.name.label('provider'),
Source.jsonf(s_bibfile, s_bibitem))
.select_from(Source)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.filter_by(provider_id=s_provider.id)
.filter_by(bibitem_id=s_bibitem.id)
.where(s_bibitem.bibfile_id == s_bibfile.id)
.order_by(s_provider.name, s_bibfile.name, s_bibitem.bibkey)
.alias('lang_source'))
sources = (select(sources.c.provider.label('key'),
group_array(sa.func.json(sources.c.jsonf)).label('value'))
.group_by(sources.c.provider)
.alias('lang_sources'))
sources = (select(sa.func.nullif(group_object(sources.c.key,
sa.func.json(sources.c.value)), '{}')
.label('sources'))
.scalar_subquery())
languoid['sources'] = sources
a_provider = aliased(AltnameProvider, name='altname_provider')
altnames = (select(a_provider.name.label('provider'), Altname.jsonf())
.select_from(Altname)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.filter_by(provider_id=a_provider.id)
.order_by(a_provider.name, Altname.printf())
.alias('lang_altname'))
altnames = (select(altnames.c.provider.label('key'),
group_array(sa.func.json(altnames.c.jsonf))
.label('value'))
.group_by(altnames.c.provider)
.alias('lang_altnames'))
altnames = (select(sa.func.nullif(group_object(altnames.c.key,
sa.func.json(altnames.c.value)), '{}')
.label('altnames'))
.scalar_subquery())
languoid['altnames'] = altnames
triggers = (select(Trigger.field, Trigger.trigger)
.select_from(Trigger)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.order_by('field', Trigger.ord)
.alias('lang_trigger'))
triggers = (select(triggers.c.field.label('key'),
group_array(triggers.c.trigger).label('value'))
.group_by(triggers.c.field)
.alias('lang_triggers'))
triggers = (select(sa.func.nullif(group_object(triggers.c.key,
triggers.c.value),
'{}').label('triggers'))
.scalar_subquery())
languoid['triggers'] = triggers
identifier = (select(sa.func.nullif(group_object(IdentifierSite.name.label('site'),
Identifier.identifier),
'{}').label('identifier'))
.select_from(Identifier)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.filter_by(site_id=IdentifierSite.id)
.scalar_subquery())
languoid['identifier'] = identifier
classification_comment = (select(ClassificationComment.kind.label('key'),
ClassificationComment.comment.label('value'))
.select_from(ClassificationComment)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.scalar_subquery())
cr_bibfile = aliased(Bibfile, name='bibfile_cr')
cr_bibitem = aliased(Bibitem, name='bibitem_cr')
classification_refs = (select((ClassificationRef.kind + 'refs').label('key'),
ClassificationRef.jsonf(cr_bibfile, cr_bibitem))
.select_from(ClassificationRef)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.filter_by(bibitem_id=cr_bibitem.id)
.where(cr_bibitem.bibfile_id == cr_bibfile.id)
.order_by(ClassificationRef.kind, ClassificationRef.ord)
.alias('lang_cref'))
classification_refs = (select(classification_refs.c.key,
group_array(sa.func.json(classification_refs.c.jsonf))
.label('value'))
.group_by(classification_refs.c.key))
classification = (classification_comment
.union_all(classification_refs)
.alias('lang_classifciation'))
classification = (select(sa.func.nullif(group_object(classification.c.key,
classification.c.value),
'{}').label('classification'))
.select_from(classification)
.scalar_subquery())
languoid['classification'] = classification
e_bibfile = aliased(Bibfile, name='bibfile_e')
e_bibitem = aliased(Bibitem, name='bibitem_e')
endangerment = (select(Endangerment.jsonf(EndangermentSource,
e_bibfile, e_bibitem,
label='endangerment'))
.join_from(Endangerment, EndangermentSource)
.outerjoin(sa.join(e_bibitem, e_bibfile))
.where(Endangerment.languoid_id == Languoid.id)
.correlate(Languoid)
.scalar_subquery())
languoid['endangerment'] = endangerment
hh_ethnologue_comment = (select(EthnologueComment
.jsonf(label='hh_ethnologue_comment'))
.select_from(EthnologueComment)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.scalar_subquery())
languoid['hh_ethnologue_comment'] = hh_ethnologue_comment
irct = aliased(IsoRetirementChangeTo, name='irct')
change_to = (select(irct.code)
.select_from(irct)
.filter_by(languoid_id=IsoRetirement.languoid_id)
.correlate(IsoRetirement)
.order_by(irct.ord)
.alias('lang_irct'))
change_to = (select(group_array(change_to.c.code).label('change_to'))
.scalar_subquery())
iso_retirement = (select(IsoRetirement.jsonf(change_to=change_to,
optional=True,
label='iso_retirement'))
.select_from(IsoRetirement)
.filter_by(languoid_id=Languoid.id)
.correlate(Languoid)
.scalar_subquery())
languoid['iso_retirement'] = iso_retirement
languoid = json_object(*(x for kv in languoid.items() for x in kv))
if as_rows:
path = Languoid.path()
if load_json:
languoid = sa.type_coerce(languoid, sa.types.JSON)
columns = [path.label(path_label),
languoid.label(languoid_label)]
else:
path = Languoid.path_json()
path_json = json_object(path_label, path,
languoid_label, languoid)
if load_json:
path_json = sa.type_coerce(path_json, sa.types.JSON)
columns = [path_json]
select_json = select(*columns).select_from(Languoid)
select_json = _ordered_by(select_json, path, ordered=ordered)
return select_json
def iterdescendants(parent_level=None, child_level=None, *, bind=ENGINE):
"""Yield pairs of (parent id, sorted list of their descendant ids)."""
# TODO: implement ancestors/descendants as sa.orm.relationship()
# see https://bitbucket.org/zzzeek/sqlalchemy/issues/4165
parent_root = None
if parent_level is None:
pass
elif parent_level == 'top':
parent_root = True
parent_level = None
elif parent_level in LEVEL:
pass
else:
raise ValueError(f'invalid parent_level: {parent_level!r}')
Parent, Child, parent_child = Languoid.parent_descendant(parent_root=parent_root,
parent_level=parent_level)
select_pairs = (select(Parent.id.label('parent_id'),
Child.id.label('child_id'))
.select_from(parent_child)
.order_by('parent_id', 'child_id'))
if child_level is not None:
if child_level not in LEVEL:
raise ValueError(f'invalid child_level: {child_level!r}')
select_pairs = select_pairs.where(sa.or_(Child.level == None,
Child.level == child_level))
rows = _backend.iterrows(select_pairs, bind=bind)
for parent_id, grp in _tools.groupby_attrgetter('parent_id')(rows):
_, c = next(grp)
if c is None:
descendants = []
else:
descendants = [c] + [c for _, c in grp]
yield parent_id, descendants