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