|
| 1 | +import typing as t |
| 2 | +import logging |
| 3 | + |
| 4 | +logger = logging.getLogger(__name__) |
| 5 | + |
| 6 | + |
| 7 | +def to_sqlmesh(dbt_select: t.Collection[str], dbt_exclude: t.Collection[str]) -> t.Optional[str]: |
| 8 | + """ |
| 9 | + Given selectors defined in the format of the dbt cli --select and --exclude arguments, convert them into a selector expression that |
| 10 | + the SQLMesh selector engine can understand. |
| 11 | +
|
| 12 | + Note that actually implementing compatible dbt selector syntax and maintaining compatibility with existing dbt selectors is considered out of scope |
| 13 | + at this stage, so the incoming selectors are expected to follow the SQLMesh syntax. |
| 14 | +
|
| 15 | + The main things being mapped are: |
| 16 | + - set union (" " between items within the same selector string) |
| 17 | + - `--exclude`. The SQLMesh selector engine does not treat this as a separate parameter and rather treats exclusion as a normal selector |
| 18 | + that just happens to contain negation syntax, so we generate these by negating each expression |
| 19 | +
|
| 20 | + Things that are *not* being mapped include: |
| 21 | + - set intersection ("," between items) as the SQLMesh selector engine doesnt support this |
| 22 | + - selectors based on file paths |
| 23 | + - selectors based on partially qualified names like "model_a". The SQLMesh selector engine requires either: |
| 24 | + - wildcards, eg "*model_a*" |
| 25 | + - the full model name qualified with the schema, eg "staging.model_a" |
| 26 | +
|
| 27 | + Examples: |
| 28 | + --select "main.model_a" |
| 29 | + -> "main.model_a" |
| 30 | + --select "main.model_a" --select "main.model_b" |
| 31 | + -> "main.model_a & main.model_b" |
| 32 | + --select "main.model_a main.model_b" |
| 33 | + -> "main.model_a & main.model_b" |
| 34 | + --select "(main.model_a | ^main.model_b)" |
| 35 | + -> "(main.model_a | ^main.model_b)" |
| 36 | + --select "+main.model_a" --exclude "raw.src_data" |
| 37 | + -> "+main.model_a & ^(raw.src_data)" |
| 38 | + --select "+main.model_a" --select "main.*b+" --exclude "raw.src_data" |
| 39 | + -> "(+main.model_a & main.*b+) & ^(raw.src_data)" |
| 40 | + """ |
| 41 | + if not dbt_select and not dbt_exclude: |
| 42 | + return None |
| 43 | + |
| 44 | + def _is_wrapped_in_parenthesis(test: str) -> bool: |
| 45 | + return test.strip().startswith("(") and test.strip().endswith(")") |
| 46 | + |
| 47 | + # expand space-separated items like: "my_first_model my_second_model" into multiple items |
| 48 | + # but take into account brackets, eg "(my_first_model & my_second_model)" should not be split |
| 49 | + def _split_selector_string(selector_str: str) -> t.List[str]: |
| 50 | + splits = [] |
| 51 | + buf = "" |
| 52 | + stack = 0 |
| 53 | + |
| 54 | + for char in selector_str: |
| 55 | + if char == " " and stack <= 0: |
| 56 | + # only split on a space if we are not within parenthesis |
| 57 | + splits.append(buf) |
| 58 | + buf = "" |
| 59 | + continue |
| 60 | + elif char == "(": |
| 61 | + stack += 1 |
| 62 | + elif char == ")": |
| 63 | + stack -= 1 |
| 64 | + |
| 65 | + buf += char |
| 66 | + |
| 67 | + if buf: |
| 68 | + splits.append(buf) |
| 69 | + |
| 70 | + return splits |
| 71 | + |
| 72 | + split_dbt_select = [item for s in dbt_select for item in _split_selector_string(s)] |
| 73 | + |
| 74 | + split_dbt_exclude = [item for s in dbt_exclude for item in _split_selector_string(s)] |
| 75 | + |
| 76 | + main_expr = " & ".join(split_dbt_select) |
| 77 | + |
| 78 | + if split_dbt_exclude: |
| 79 | + negated_dbt_exclude = [ |
| 80 | + f"^{e}" if _is_wrapped_in_parenthesis(e) else f"^({e})" for e in split_dbt_exclude |
| 81 | + ] |
| 82 | + negated_expr = " & ".join(negated_dbt_exclude) |
| 83 | + |
| 84 | + # only wrap in extra parenthesis if there was more than 1 exclusion expression with some inclusion expressioons |
| 85 | + # otherwise it can stand by itself with no parenthesis |
| 86 | + if len(split_dbt_exclude) > 1 and split_dbt_select: |
| 87 | + negated_expr = f"({negated_expr})" |
| 88 | + |
| 89 | + if len(split_dbt_select) > 1: |
| 90 | + main_expr = f"({main_expr})" |
| 91 | + |
| 92 | + if main_expr: |
| 93 | + main_expr = f"{main_expr} & {negated_expr}" |
| 94 | + else: |
| 95 | + main_expr = negated_expr |
| 96 | + |
| 97 | + logger.debug( |
| 98 | + f"Expanded dbt select: {dbt_select}, exclude: {dbt_exclude} into SQLMesh: {main_expr}" |
| 99 | + ) |
| 100 | + |
| 101 | + return main_expr |
0 commit comments