Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 80abbc3

Browse files
authored
ts_replace() speed improves for empty input arrays
1 parent 4f73d3c commit 80abbc3

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

‎functions/ts_replace.sql‎

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@ create or replace function public.ts_replace(
1212
cost 1
1313
as
1414
$function$
15-
16-
with s as (
17-
select case when lexeme = str_from then str_to else lexeme end as new_lexeme,
18-
unnest(positions) as position,
19-
unnest(weights) as weight
20-
from unnest(vector) as t(lexeme, positions, weights)
21-
)
22-
--https://www.postgrespro.ru/docs/postgresql/12/datatype-textsearch#DATATYPE-TSVECTOR
23-
select array_to_string(array(
24-
select concat(
25-
concat($$'$,ドル replace(replace(new_lexeme, $$'$,ドル $$''$$), $$\$,ドル $$\\$$), $$'$$),
26-
':',
27-
string_agg(concat(position, weight), ',')
28-
)
29-
from s
30-
group by new_lexeme
31-
), ' ')::tsvector;
32-
15+
with s as (
16+
select case when lexeme = str_from then str_to
17+
else lexeme
18+
end as new_lexeme,
19+
unnest(positions) as position,
20+
unnest(weights) as weight
21+
from unnest(vector) as t(lexeme, positions, weights)
22+
)
23+
--https://www.postgrespro.ru/docs/postgresql/12/datatype-textsearch#DATATYPE-TSVECTOR
24+
select array_to_string(array(
25+
select concat(
26+
concat($$'$,ドル
27+
replace(replace(new_lexeme, $$'$,ドル $$''$$), $$\$,ドル $$\\$$),
28+
$$'$$),
29+
':',
30+
string_agg(concat(position, weight), ',')
31+
)
32+
from s
33+
group by new_lexeme
34+
), ' ')::tsvector;
3335
$function$;
3436
3537
comment on function public.ts_replace(vector tsvector, str_from text, str_to text) is 'Заменяет заданную лексему в векторе';
@@ -59,31 +61,45 @@ create or replace function public.ts_replace(
5961
)
6062
returns tsvector
6163
stable
62-
returns null on null input
63-
parallel safe
64-
language sql
64+
strict -- returns null if any parameter is null
65+
parallel safe -- Postgres 10 or later
66+
language plpgsql
6567
set search_path = ''
6668
cost 1
6769
as
6870
$function$
71+
begin
72+
-- speed improves for empty input arrays
73+
if cardinality(arr_from) = 0 or
74+
cardinality(arr_to) = 0
75+
then
76+
return vector;
77+
end if;
6978
70-
with s as (
71-
select coalesce(arr_to[array_position(arr_from, lexeme)], lexeme) as new_lexeme,
72-
unnest(positions) as position,
73-
unnest(weights) as weight
74-
from unnest(vector) as t(lexeme, positions, weights)
75-
)
76-
--https://www.postgrespro.ru/docs/postgresql/12/datatype-textsearch#DATATYPE-TSVECTOR
77-
select array_to_string(array(
78-
select concat(
79-
concat($$'$,ドル replace(replace(new_lexeme, $$'$,ドル $$''$$), $$\$,ドル $$\\$$), $$'$$),
80-
':',
81-
string_agg(concat(position, weight), ',')
82-
)
83-
from s
84-
group by new_lexeme
85-
), ' ')::tsvector;
86-
79+
return (
80+
with s as (
81+
select coalesce(
82+
arr_to[array_position(arr_from, lexeme)],
83+
lexeme
84+
) as new_lexeme,
85+
unnest(positions) as position,
86+
unnest(weights) as weight
87+
from unnest(vector) as t(lexeme, positions, weights)
88+
)
89+
--https://www.postgrespro.ru/docs/postgresql/12/datatype-textsearch#DATATYPE-TSVECTOR
90+
select array_to_string(array(
91+
select concat(
92+
concat($$'$,ドル
93+
replace(replace(new_lexeme, $$'$,ドル $$''$$), $$\$,ドル $$\\$$),
94+
$$'$$),
95+
':',
96+
string_agg(concat(position, weight), ',')
97+
)
98+
from s
99+
group by new_lexeme
100+
), ' ')::tsvector
101+
);
102+
end;
87103
$function$;
88104

89105
comment on function public.ts_replace(vector tsvector, arr_from text[], arr_to text[]) is 'Заменяет заданные лексемы в векторе';

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /