support pgsql versions up to 11
[smlar.git] / sql / smlar.sql
1 CREATE EXTENSION smlar;
2
3 --sanity check
4 SELECT 
5         opc.opcname, 
6         t.typname, 
7         opc.opcdefault  
8 FROM 
9         pg_opclass opc, 
10         pg_am am, 
11         pg_type t, 
12         pg_type k 
13 WHERE 
14         opc.opcmethod = am.oid AND 
15         am.amname='gist' AND 
16         opc.opcintype = t.oid AND 
17         opc.opckeytype = k.oid AND 
18         k.typname='gsmlsign'
19 ORDER BY opc.opcname;
20
21 SELECT 
22         opc.opcname, 
23         t.typname, 
24         opc.opcdefault  
25 FROM 
26         pg_opclass opc, 
27         pg_am am, 
28         pg_type t
29 WHERE 
30         opc.opcmethod = am.oid AND 
31         am.amname='gin' AND 
32         opc.opcintype = t.oid AND 
33         opc.opcname ~ '_sml_ops$'
34 ORDER BY opc.opcname;
35
36 SELECT 
37     trim( leading '_'  from t.typname ) || '[]' AS "Array Type",
38     gin.opcname AS "GIN operator class",
39     gist.opcname AS "GiST operator class"
40 FROM
41     (
42         SELECT *
43         FROM
44             pg_catalog.pg_opclass,
45             pg_catalog.pg_am
46         WHERE
47             pg_opclass.opcmethod = pg_am.oid AND
48             pg_am.amname = 'gin' AND
49             pg_opclass.opcname ~ '_sml_ops$'
50     ) AS gin
51     FULL OUTER JOIN
52         (
53             SELECT *
54             FROM
55                 pg_catalog.pg_opclass,
56                 pg_catalog.pg_am
57             WHERE
58                 pg_opclass.opcmethod = pg_am.oid AND
59                 pg_am.amname = 'gist' AND
60                 pg_opclass.opcname ~ '_sml_ops$'
61         ) AS gist
62         ON (
63             gist.opcname = gin.opcname AND 
64             gist.opcintype = gin.opcintype 
65         ),
66     pg_catalog.pg_type t
67 WHERE
68     t.oid = COALESCE(gist.opcintype, gin.opcintype) AND
69     t.typarray = 0
70 ORDER BY
71     "Array Type" ASC 
72 ;
73
74 SELECT set_smlar_limit(0.1);
75 SET smlar.threshold = 0.6;
76
77 --First checks
78 SELECT smlar('{3,2}'::int[], '{3,2,1}');
79 SELECT smlar('{2,1}'::int[], '{3,2,1}');
80 SELECT smlar('{}'::int[], '{3,2,1}');
81 SELECT smlar('{12,10}'::int[], '{3,2,1}');
82 SELECT smlar('{123}'::int[], '{}');
83 SELECT smlar('{1,4,6}'::int[], '{1,4,6}');
84 SELECT smlar('{1,4,6}'::int[], '{6,4,1}');
85 SELECT smlar('{1,4,6}'::int[], '{5,4,6}');
86 SELECT smlar('{1,4,6}'::int[], '{5,4,6}');
87 SELECT smlar('{1,2}'::int[], '{2,2,2,2,2,1}');
88 SELECT smlar('{1,2}'::int[], '{1,2,2,2,2,2}');
89 SELECT smlar('{}'::int[], '{3}');
90 SELECT smlar('{3}'::int[], '{3}');
91 SELECT smlar('{2}'::int[], '{3}');
92
93
94 SELECT smlar('{1,4,6}'::int[], '{5,4,6}', 'N.i / (N.a + N.b)' );
95 SELECT smlar('{1,4,6}'::int[], '{5,4,6}', 'N.i / sqrt(N.a * N.b)' );
96
97 SELECT tsvector2textarray('qwe:12 asd:45');
98
99 SELECT array_unique('{12,12,1,4,1,16}'::int4[]);
100 SELECT array_unique('{12,12,1,4,1,16}'::bigint[]);
101 SELECT array_unique('{12,12,1,4,1,16}'::smallint[]);
102 SELECT array_unique('{12,12,1,4,1,16}'::text[]);
103 SELECT array_unique('{12,12,1,4,1,16}'::varchar[]);
104
105 SELECT inarray('{12,12,1,4,1,16}'::int[], 13::int);
106 SELECT inarray('{12,12,1,4,1,16}'::int[], 12::int);
107 SELECT inarray('{12,12,1,4,1,16}'::text[], 13::text);
108 SELECT inarray('{12,12,1,4,1,16}'::text[], 12::text);
109 SELECT inarray('{12,12,1,4,1,16}'::int[], 13::int, 0.9, 0.1);
110 SELECT inarray('{12,12,1,4,1,16}'::int[], 12::int, 0.9, 0.1);
111 SELECT inarray('{12,12,1,4,1,16}'::text[], 13::text, 0.9, 0.1);
112 SELECT inarray('{12,12,1,4,1,16}'::text[], 12::text, 0.9, 0.1);
113
114 --testing function
115 CREATE OR REPLACE FUNCTION epoch2timestamp(int4)
116 RETURNS timestamp AS $$
117     SELECT ('1970-01-01 00:00:00'::timestamp +   ( ($1*3600*24 + $1) ::text || ' seconds' )::interval)::timestamp;
118         $$ LANGUAGE SQL RETURNS NULL ON NULL INPUT IMMUTABLE;
119
120 CREATE OR REPLACE FUNCTION to_tsp_array(_int4)
121 RETURNS _timestamp AS $$
122         SELECT ARRAY( 
123                 SELECT 
124                         epoch2timestamp( $1[n] )
125                 FROM
126                         generate_series( 1, array_upper( $1, 1) - array_lower( $1, 1 ) + 1 ) AS n
127         );
128         $$ LANGUAGE SQL RETURNS NULL ON NULL INPUT IMMUTABLE;
129
130 CREATE OR REPLACE FUNCTION array_to_col(anyarray)
131 RETURNS SETOF anyelement AS
132 $$
133     SELECT $1[n] FROM generate_series( 1, array_upper( $1, 1) - array_lower( $1, 1 ) + 1 ) AS n;
134 $$ LANGUAGE SQL RETURNS NULL ON NULL INPUT IMMUTABLE;
135