Skip to content

sql_converter

sql_converter

SQL conversion helpers for DomoDataflow actions.

These helpers provide a lightweight approximation layer that maps Magic ETL action tiles to SQL snippets. The output is intended for analysis and documentation, not direct execution without review.

convert_entire_workflow_to_sql_tiles

convert_entire_workflow_to_sql_tiles(
    actions: list[Any],
    recommendations: RecommendationSet | None = None,
) -> list[dict[str, Any]]

Convert a full action list into ordered SQL tile approximations.

Each result dict contains

step_number, step_name, tile_id, tile_name, tile_type, input_steps, input_names, output_steps, output_names, conversion_status, sql, applied_rules, placement

applied_rules lists each recommendation rule applied to the tile (rule_id, directive, value, why, source, level); placement is the join placement advice, else None.

Source code in src/crew_dcs/classes/DomoDataflow/action/sql_converter.py
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
def convert_entire_workflow_to_sql_tiles(
    actions: list[Any], recommendations: RecommendationSet | None = None
) -> list[dict[str, Any]]:
    """Convert a full action list into ordered SQL tile approximations.

    Each result dict contains:
        step_number, step_name, tile_id, tile_name, tile_type,
        input_steps, input_names, output_steps, output_names,
        conversion_status, sql, applied_rules, placement

    ``applied_rules`` lists each recommendation rule applied to the tile (rule_id, directive,
    value, why, source, level); ``placement`` is the join placement advice, else None.
    """
    ordered = _topo_sort_actions(actions)
    step_lookup = {
        _tile_id(action): f"step_{idx}" for idx, action in enumerate(ordered, start=1)
    }
    name_lookup = {_tile_id(action): _tile_name(action) for action in ordered}

    # Build reverse edge map: tile_id → list of step_names that consume it
    _children: dict[str, list[str]] = defaultdict(list)
    _child_names: dict[str, list[str]] = defaultdict(list)
    for action in ordered:
        aid = _tile_id(action)
        raw = _as_raw_dict(action)
        depends_on = getattr(action, "depends_on", None)
        if depends_on is None:
            depends_on = raw.get("dependsOn", [])
        for pid in depends_on or []:
            child_step = step_lookup.get(aid, aid)
            child_name = name_lookup.get(aid, aid)
            parent_id = str(pid)
            if child_step not in _children[parent_id]:
                _children[parent_id].append(child_step)
            if child_name not in _child_names[parent_id]:
                _child_names[parent_id].append(child_name)

    result: list[dict[str, Any]] = []
    for idx, action in enumerate(ordered, start=1):
        aid = _tile_id(action)
        raw = _as_raw_dict(action)
        depends_on = getattr(action, "depends_on", None)
        if depends_on is None:
            depends_on = raw.get("dependsOn", [])

        input_steps = [
            step_lookup.get(str(pid), str(pid)) for pid in (depends_on or [])
        ] or ["None"]
        input_names = [
            name_lookup.get(str(pid), str(pid)) for pid in (depends_on or [])
        ] or ["None"]

        output_steps = _children.get(aid) or ["Final / Output"]
        output_names = _child_names.get(aid) or ["Final / Output"]

        previous_step = "source_dataset"
        if input_steps and input_steps[0] != "None":
            previous_step = input_steps[0]

        converted = _convert_tile(
            action,
            previous_step=previous_step,
            input_steps=input_steps,
            input_names=input_names,
            recommendations=recommendations,
            output_step=f"step_{idx}",
        )

        result.append(
            {
                "step_number": idx,
                "step_name": f"step_{idx}",
                "tile_id": aid,
                "tile_name": _tile_name(action),
                "tile_type": _tile_type(action),
                "input_steps": input_steps,
                "input_names": input_names,
                "output_steps": output_steps,
                "output_names": output_names,
                "conversion_status": conversion_status(action),
                "sql": converted.sql,
                "applied_rules": converted.applied_rules,
                "placement": converted.placement,
            }
        )

    return result

format_conversion_report

format_conversion_report(
    tiles: list[dict[str, Any]],
) -> str

Markdown report of a conversion: one line per tile, each applied rule with its reason.

Source code in src/crew_dcs/classes/DomoDataflow/action/sql_converter.py
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
def format_conversion_report(tiles: list[dict[str, Any]]) -> str:
    """Markdown report of a conversion: one line per tile, each applied rule with its reason."""
    lines = ["# Magic ETL SQL conversion report", ""]
    for tile in tiles:
        lines.append(
            f"- step {tile['step_number']} `{tile['tile_name']}` "
            f"({tile['tile_type']}): {tile['conversion_status']}"
        )
        for rule in tile.get("applied_rules", []):
            lines.append(
                f"  - rule `{rule['rule_id']}` ({rule['source']}) "
                f"{rule['directive']}={rule['value']}"
            )
            lines.append(f"    why: {rule['why']}")
    return "\n".join(lines) + "\n"

tile_operation

tile_operation(action_or_dict: Any) -> str | None

Return the operation a tile performs (formula, join, alter, ...), or None if unclassified.

Source code in src/crew_dcs/classes/DomoDataflow/action/sql_converter.py
46
47
48
def tile_operation(action_or_dict: Any) -> str | None:
    """Return the operation a tile performs (formula, join, alter, ...), or None if unclassified."""
    return _TILE_OPERATIONS.get(_tile_type(action_or_dict).lower())

tile_to_sql

tile_to_sql(
    action_or_dict: Any,
    *,
    previous_step: str = "source_dataset",
    input_steps: list[str] | None = None,
    input_names: list[str] | None = None,
    recommendations: RecommendationSet | None = None,
    output_step: str = "step_out",
) -> str

Convert a single tile/action into SQL approximation.

output_step names the table an ALTER-style tile builds (copy, then alter the copy).

recommendations (see load_recommendations) applies YAML rules; the SQL carries a comment citing each rule applied. With None, no rule is applied.

Source code in src/crew_dcs/classes/DomoDataflow/action/sql_converter.py
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
def tile_to_sql(
    action_or_dict: Any,
    *,
    previous_step: str = "source_dataset",
    input_steps: list[str] | None = None,
    input_names: list[str] | None = None,
    recommendations: RecommendationSet | None = None,
    output_step: str = "step_out",
) -> str:
    """Convert a single tile/action into SQL approximation.

    ``output_step`` names the table an ALTER-style tile builds (copy, then alter the copy).

    ``recommendations`` (see ``load_recommendations``) applies YAML rules; the SQL carries a
    comment citing each rule applied. With None, no rule is applied.
    """
    return _convert_tile(
        action_or_dict,
        previous_step=previous_step,
        input_steps=input_steps,
        input_names=input_names,
        recommendations=recommendations,
        output_step=output_step,
    ).sql