Creates a view that is a join over one or more sources or tables
| 89 | |
| 90 | |
| 91 | class CreateView(Action): |
| 92 | """Creates a view that is a join over one or more sources or tables""" |
| 93 | |
| 94 | def __init__(self, capabilities: Capabilities, view: ViewExists) -> None: |
| 95 | self.view = view |
| 96 | super().__init__(capabilities) |
| 97 | |
| 98 | def run(self, c: Composition, state: State) -> None: |
| 99 | first_input = self.view.inputs[0] |
| 100 | outer_join = " ".join( |
| 101 | f"JOIN {f.get_name_for_query()} USING (f1)" for f in self.view.inputs[1:] |
| 102 | ) |
| 103 | |
| 104 | index = ( |
| 105 | f"> CREATE DEFAULT INDEX ON {self.view.name}" if self.view.has_index else "" |
| 106 | ) |
| 107 | |
| 108 | aggregates = [f"COUNT({first_input.get_name_for_query()}.f1) AS count_all"] |
| 109 | |
| 110 | if self.view.expensive_aggregates: |
| 111 | aggregates.extend( |
| 112 | [ |
| 113 | f"COUNT(DISTINCT {first_input.get_name_for_query()}.f1) AS count_distinct", |
| 114 | f"MIN({first_input.get_name_for_query()}.f1) AS min_value", |
| 115 | f"MAX({first_input.get_name_for_query()}.f1) AS max_value", |
| 116 | ] |
| 117 | ) |
| 118 | |
| 119 | aggregates = ", ".join(aggregates) |
| 120 | |
| 121 | refresh = random.choice( |
| 122 | ["ON COMMIT", f"EVERY '{random.randint(1, 5)} seconds'"] |
| 123 | ) |
| 124 | |
| 125 | c.testdrive( |
| 126 | dedent(f""" |
| 127 | > CREATE MATERIALIZED VIEW {self.view.name} |
| 128 | WITH (REFRESH {refresh}) AS |
| 129 | SELECT {aggregates} |
| 130 | FROM {first_input.get_name_for_query()} |
| 131 | {outer_join} |
| 132 | """) + index, |
| 133 | mz_service=state.mz_service, |
| 134 | ) |
| 135 | |
| 136 | def provides(self) -> list[Capability]: |
| 137 | return [self.view] |
| 138 | |
| 139 | |
| 140 | class ValidateView(Action): |
no outgoing calls
no test coverage detected