functions.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. GENERATE_ISSUE_TSVECTOR = """
  2. CREATE OR REPLACE FUNCTION generate_issue_tsvector(jsonb) RETURNS tsvector AS $$
  3. BEGIN
  4. RETURN strip(jsonb_to_tsvector($1, '["string"]'));
  5. EXCEPTION WHEN program_limit_exceeded THEN
  6. RETURN null;
  7. END;
  8. $$ LANGUAGE plpgsql;;
  9. """
  10. # https://stackoverflow.com/a/42998229/443457
  11. JSONB_RECURSIVE_MERGE = """
  12. create or replace function remove_dupes(p_array jsonb)
  13. returns jsonb
  14. as
  15. $$
  16. select jsonb_agg(distinct e)
  17. from jsonb_array_elements(p_array) as t(e);
  18. $$
  19. language sql;
  20. create or replace function jsonb_merge_deep(jsonb, jsonb)
  21. returns jsonb
  22. language sql
  23. immutable
  24. as $func$
  25. select case jsonb_typeof($1)
  26. when 'object' then case jsonb_typeof($2)
  27. when 'object' then (
  28. select jsonb_object_agg(k, case
  29. when e2.v is null then e1.v
  30. when e1.v is null then e2.v
  31. else jsonb_merge_deep(e1.v, e2.v)
  32. end)
  33. from jsonb_each($1) e1(k, v)
  34. full join jsonb_each($2) e2(k, v) using (k)
  35. )
  36. else $2
  37. end
  38. when 'array' then remove_dupes($1 || $2)
  39. else $2
  40. end
  41. $func$;
  42. """
  43. UPDATE_ISSUE_INDEX = """
  44. CREATE OR REPLACE FUNCTION concat_tsvector(tsvector, tsvector) RETURNS tsvector AS $$
  45. BEGIN
  46. RETURN $1 || $2;
  47. EXCEPTION WHEN program_limit_exceeded THEN
  48. RETURN $1;
  49. END;
  50. $$ LANGUAGE plpgsql;;
  51. CREATE OR REPLACE FUNCTION collect_tag (jsonb, hstore)
  52. returns jsonb language sql
  53. as $$
  54. SELECT jsonb_merge_deep(jsonb_object_agg(y.key, y.values), $1) FROM (
  55. SELECT (a).key, array_agg(distinct(a).value) as values FROM (
  56. select each($2) as a
  57. ) t GROUP by key
  58. ) y
  59. $$;
  60. CREATE OR REPLACE AGGREGATE agg_collect_tags (hstore) (
  61. sfunc = collect_tag,
  62. stype = jsonb,
  63. initcond = '{}'
  64. );
  65. DROP PROCEDURE IF EXISTS update_issue_index;
  66. CREATE OR REPLACE PROCEDURE update_issue_index(update_issue_id integer)
  67. LANGUAGE SQL
  68. AS $$
  69. WITH event_agg as (
  70. SELECT COUNT(events_event.event_id) as new_count,
  71. MAX(events_event.created) as new_last_seen,
  72. MAX(events_event.level) as new_level,
  73. agg_collect_tags(events_event.tags) as new_tags
  74. FROM events_event
  75. LEFT JOIN issues_issue ON issues_issue.id = events_event.issue_id
  76. WHERE events_event.issue_id=update_issue_id
  77. AND events_event.created > issues_issue.last_seen
  78. ), event_vector as (
  79. SELECT strip(COALESCE(generate_issue_tsvector(data), ''::tsvector)) as vector
  80. FROM events_event
  81. LEFT JOIN issues_issue on issues_issue.id = events_event.issue_id
  82. WHERE events_event.issue_id=update_issue_id
  83. AND events_event.created > issues_issue.last_seen
  84. limit 1
  85. )
  86. UPDATE issues_issue
  87. SET
  88. count = event_agg.new_count + issues_issue.count,
  89. last_seen = GREATEST(event_agg.new_last_seen, issues_issue.last_seen),
  90. level = GREATEST(event_agg.new_level, issues_issue.level),
  91. search_vector = concat_tsvector(COALESCE(search_vector, ''::tsvector), event_vector.vector),
  92. tags = COALESCE(jsonb_merge_deep(event_agg.new_tags, tags), '{}')
  93. FROM event_agg, event_vector
  94. WHERE issues_issue.id = update_issue_id;
  95. $$;
  96. """