|
| 1 | +# A script to create the automatic casts for overlaps and intersects |
| 2 | +# between MOCs and spolys/scircles. |
| 3 | +# |
| 4 | +# This has originally been used to create pg_sphere--1.2.0--1.2.1.sql. |
| 5 | +# Before 1.2.1 is release, this can be fixed to improve that SQL. |
| 6 | +# After the 1.2.1 release, this is just documentation on how MOC |
| 7 | +# casts were generated that is perhaps just a bit more readable than |
| 8 | +# that bunch of SQL. |
| 9 | + |
| 10 | +import datetime |
| 11 | +import re |
| 12 | +import sys |
| 13 | + |
| 14 | + |
| 15 | +OVERLAP_DEFS = [ |
| 16 | + # func_stem, operator, commutator |
| 17 | + ('subset', '<@', '@>'), |
| 18 | + ('not_subset', '!<@', '!@>'), |
| 19 | + ('superset', '@>', '<@'), |
| 20 | + ('not_superset', '!@>', '!<@'), |
| 21 | +] |
| 22 | + |
| 23 | +INTERSECT_DEFS = [ |
| 24 | + # func_stem, operator, commutator |
| 25 | + ('intersect', '&&', '&&'), |
| 26 | + ('not_intersect', '!&&', '!&&'), |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +GEO_TYPES = ["scircle", "spoly"] |
| 31 | + |
| 32 | +OP_DEFS = OVERLAP_DEFS |
| 33 | + |
| 34 | + |
| 35 | +class Accum: |
| 36 | + """an accumulator for our output. |
| 37 | + """ |
| 38 | + def __init__(self): |
| 39 | + self.parts = [] |
| 40 | + |
| 41 | + @property |
| 42 | + def content(self): |
| 43 | + return "".join(self.parts) |
| 44 | + |
| 45 | + def write(self, s): |
| 46 | + self.parts.append(s) |
| 47 | + |
| 48 | + def writeln(self, *strings): |
| 49 | + self.parts.append("\n".join(strings)+"\n") |
| 50 | + |
| 51 | + def replace_last(self, subs): |
| 52 | + """replaces the last non-whitespace char with the string subs. |
| 53 | + """ |
| 54 | + for index, part in enumerate(reversed(self.parts)): |
| 55 | + if part.strip(): |
| 56 | + break |
| 57 | + else: |
| 58 | + # nothing to replace |
| 59 | + return |
| 60 | + |
| 61 | + index = -1-index |
| 62 | + self.parts[index] = re.sub("[^\s](\s*)$", |
| 63 | + lambda mat: subs+mat.group(1), |
| 64 | + self.parts[index]) |
| 65 | + |
| 66 | + def introduce_section(self, sec_name): |
| 67 | + self.writeln() |
| 68 | + self.writeln("-- #################################") |
| 69 | + self.writeln(f"-- {sec_name}") |
| 70 | + |
| 71 | + |
| 72 | +def emit_drop_code(accum): |
| 73 | + accum.introduce_section("Cleanup") |
| 74 | + |
| 75 | + accum.writeln("DROP OPERATOR IF EXISTS") |
| 76 | + for _, op, _ in OP_DEFS: |
| 77 | + for geo_type in GEO_TYPES: |
| 78 | + accum.writeln(f" {op} (smoc, {geo_type}),") |
| 79 | + accum.writeln(f" {op} ({geo_type}, smoc),") |
| 80 | + accum.replace_last(";") |
| 81 | + |
| 82 | + |
| 83 | +def make_negator(op): |
| 84 | + if op.startswith("!"): |
| 85 | + return op[1:] |
| 86 | + else: |
| 87 | + return "!"+op |
| 88 | + |
| 89 | + |
| 90 | +def emit_op_def(accum, operator, leftarg, rightarg, procedure, commutator): |
| 91 | + accum.writeln( |
| 92 | + f"CREATE OPERATOR {operator} (", |
| 93 | + f" LEFTARG = {leftarg},", |
| 94 | + f" RIGHTARG = {rightarg},", |
| 95 | + f" PROCEDURE = {procedure},", |
| 96 | + f" COMMUTATOR = '{commutator}',", |
| 97 | + f" NEGATOR = '{make_negator(operator)}',", |
| 98 | + f" RESTRICT = contsel,", |
| 99 | + f" JOIN = contjoinsel", |
| 100 | + f");") |
| 101 | + |
| 102 | + |
| 103 | +def emit_op_and_func(accum, op_def): |
| 104 | + func_stem, operator, commutator = op_def |
| 105 | + for geo_type in GEO_TYPES: |
| 106 | + func_name = f"{geo_type}_{func_stem}_smoc" |
| 107 | + accum.writeln( |
| 108 | + f"CREATE OR REPLACE FUNCTION {func_name}(", |
| 109 | + f" geo_arg {geo_type}, a_moc smoc) RETURNS BOOL AS $body$", |
| 110 | + f" SELECT smoc(max_order(a_moc), geo_arg) {operator} a_moc", |
| 111 | + f" $body$ LANGUAGE SQL STABLE;") |
| 112 | + emit_op_def(accum, operator, |
| 113 | + geo_type, "smoc", |
| 114 | + func_name, |
| 115 | + commutator) |
| 116 | + |
| 117 | + accum.writeln() |
| 118 | + |
| 119 | + func_name = f"smoc_{func_stem}_{geo_type}" |
| 120 | + accum.writeln( |
| 121 | + f"CREATE OR REPLACE FUNCTION {func_name}(", |
| 122 | + f" a_moc smoc, geo_arg {geo_type}) RETURNS BOOL AS $body$", |
| 123 | + f" SELECT a_moc {operator} smoc(max_order(a_moc), geo_arg)", |
| 124 | + f" $body$ LANGUAGE SQL STABLE;") |
| 125 | + emit_op_def(accum, operator, |
| 126 | + "smoc", geo_type, |
| 127 | + func_name, |
| 128 | + commutator) |
| 129 | + |
| 130 | + accum.writeln() |
| 131 | + |
| 132 | + |
| 133 | +def main(): |
| 134 | + accum = Accum() |
| 135 | + |
| 136 | + accum.writeln("-- MOC/geometry automatic casts.") |
| 137 | + accum.writeln(f"-- Generated {datetime.date.today()} by {sys.argv[0]}.") |
| 138 | + accum.writeln(f"-- Re-generation needs to be triggered manually.") |
| 139 | + accum.writeln() |
| 140 | + emit_drop_code(accum) |
| 141 | + |
| 142 | + accum.introduce_section(" smoc/geo OVERLAPS") |
| 143 | + for op_def in OVERLAP_DEFS: |
| 144 | + emit_op_and_func(accum, op_def) |
| 145 | + accum.writeln() |
| 146 | + |
| 147 | + accum.introduce_section(" smoc/geo INTERSECTS") |
| 148 | + for op_def in INTERSECT_DEFS: |
| 149 | + emit_op_and_func(accum, op_def) |
| 150 | + accum.writeln() |
| 151 | + |
| 152 | + print(accum.content) |
| 153 | + |
| 154 | + |
| 155 | +if __name__=="__main__": |
| 156 | + main() |
0 commit comments