-
Notifications
You must be signed in to change notification settings - Fork 2
/
postgres.go
59 lines (52 loc) · 1.27 KB
/
postgres.go
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
package difference_digest
import (
"database/sql"
"strings"
)
var postgresQueries = map[string]string{
"ibf": `
SELECT
pg_temp.f_hash(idx, %[2]s) %% %[3]d AS cell,
pg_temp.f_bit_xor(%[2]s) AS id_sum,
pg_temp.f_bit_xor(pg_temp.f_hash(3 + 0, %[2]s)) AS hash_sum,
Count(%[2]s) AS Count
FROM (
SELECT 0 AS idx, * FROM %[1]s UNION
SELECT 1, * FROM %[1]s UNION
SELECT 2, * FROM %[1]s
) s
GROUP BY 1
`,
"strata_estimator": `
SELECT
pg_temp.f_trailing_zeros(pg_temp.f_hash(3 + 1, %[2]s)) AS estimator,
pg_temp.f_hash(idx, %[2]s) %% %[3]d AS cell,
pg_temp.f_bit_xor(%[2]s) AS id_sum,
pg_temp.f_bit_xor(pg_temp.f_hash(3 + 0, %[2]s)) AS hash_sum,
Count(%[2]s) AS Count
FROM (
SELECT 0 AS idx, * FROM %[1]s UNION
SELECT 1, * FROM %[1]s UNION
SELECT 2, * FROM %[1]s
) s
GROUP BY 1, 2
`,
}
// PostgresSetup creates the necessary aggregates and functions for running difference_digest SQL queries in PostgreSQL
func PostgresSetup(db *sql.DB) error {
file, err := Asset("udfs/postgres.sql")
if err != nil {
return err
}
requests := strings.Split(string(file), ";")
for _, request := range requests {
if strings.TrimSpace(request) == "" {
continue
}
_, err := db.Exec(request)
if err != nil {
return err
}
}
return nil
}