Skip to content

fastflowtransform.executors.budget.runtime.snowflake_snowpark

SnowflakeSnowparkBudgetRuntime

Bases: BaseBudgetRuntime[SnowflakeSnowparkBudgetExecutor]

Snowflake Snowpark budget runtime using EXPLAIN for estimation.

Source code in src/fastflowtransform/executors/budget/runtime/snowflake_snowpark.py
 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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class SnowflakeSnowparkBudgetRuntime(BaseBudgetRuntime[SnowflakeSnowparkBudgetExecutor]):
    """Snowflake Snowpark budget runtime using EXPLAIN for estimation."""

    DEFAULT_GUARD = BudgetGuard(
        env_var="FF_SF_MAX_BYTES",
        estimator_attr="runtime_budget_estimate_query_bytes",
        engine_label="Snowflake",
        what="query",
    )

    def estimate_query_bytes(self, sql: str) -> int | None:
        """
        Best-effort Snowflake bytes estimation using EXPLAIN USING JSON.
        Mirrors the previous executor-side logic.
        """
        try:
            body = self.executor._selectable_body(sql)
        except Exception:
            body = sql

        try:
            rows = self.executor.session.sql(f"EXPLAIN USING JSON {body}").collect()
            if not rows:
                return None

            parts: list[str] = []
            for r in rows:
                try:
                    parts.append(str(r[0]))
                except Exception:
                    as_dict: dict[str, Any] = getattr(r, "asDict", lambda: {})()
                    if as_dict:
                        parts.extend(str(v) for v in as_dict.values())

            plan_text = "\n".join(parts).strip()
            if not plan_text:
                return None

            try:
                plan_data = json.loads(plan_text)
            except Exception:
                return None

            bytes_val = self._extract_bytes_from_plan(plan_data)
            if bytes_val is None or bytes_val <= 0:
                return None
            return bytes_val
        except Exception:
            # Any parsing / EXPLAIN issues → no estimate, guard skipped
            return None

    def dataframe_bytes(self, df: Any) -> int | None:
        """
        Best-effort bytes estimate for a Snowpark DataFrame.
        """
        try:
            sql_text = self._snowpark_df_sql(df)
            if not isinstance(sql_text, str) or not sql_text.strip():
                return None
            return self.estimate_query_bytes(sql_text)
        except Exception:
            return None

    def _extract_bytes_from_plan(self, plan_data: Any) -> int | None:
        def _to_int(value: Any) -> int | None:
            if value is None:
                return None
            try:
                return int(value)
            except Exception:
                return None

        if isinstance(plan_data, dict):
            global_stats = plan_data.get("GlobalStats") or plan_data.get("globalStats")
            if isinstance(global_stats, dict):
                candidate = _to_int(
                    global_stats.get("bytesAssigned") or global_stats.get("bytes_assigned")
                )
                if candidate:
                    return candidate
            for val in plan_data.values():
                bytes_val = self._extract_bytes_from_plan(val)
                if bytes_val:
                    return bytes_val
        elif isinstance(plan_data, list):
            for item in plan_data:
                bytes_val = self._extract_bytes_from_plan(item)
                if bytes_val:
                    return bytes_val
        return None

    def _snowpark_df_sql(self, df: Any) -> str | None:
        """
        Extract the main SQL statement for a Snowpark DataFrame.

        Uses the documented public APIs:
        - DataFrame.queries -> {"queries": [sql1, sql2, ...], "post_actions": [...]}
        - Optionally falls back to df._plan.sql() if needed.
        """
        queries_dict = getattr(df, "queries", None)

        if isinstance(queries_dict, dict):
            queries = queries_dict.get("queries")
            if isinstance(queries, list) and queries:
                candidates = [q.strip() for q in queries if isinstance(q, str) and q.strip()]
                if candidates:
                    return max(candidates, key=len)

        plan = getattr(df, "_plan", None)
        if plan is not None:
            with suppress(Exception):
                simplify = getattr(plan, "simplify", None)
                if callable(simplify):
                    simplified = simplify()
                    to_sql = getattr(simplified, "sql", None)
                    if callable(to_sql):
                        sql = to_sql()
                        if isinstance(sql, str) and sql.strip():
                            return sql.strip()

            with suppress(Exception):
                to_sql = getattr(plan, "sql", None)
                if callable(to_sql):
                    sql = to_sql()
                    if isinstance(sql, str) and sql.strip():
                        return sql.strip()

        return None

estimate_query_bytes

estimate_query_bytes(sql)

Best-effort Snowflake bytes estimation using EXPLAIN USING JSON. Mirrors the previous executor-side logic.

Source code in src/fastflowtransform/executors/budget/runtime/snowflake_snowpark.py
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
60
61
62
63
64
65
66
def estimate_query_bytes(self, sql: str) -> int | None:
    """
    Best-effort Snowflake bytes estimation using EXPLAIN USING JSON.
    Mirrors the previous executor-side logic.
    """
    try:
        body = self.executor._selectable_body(sql)
    except Exception:
        body = sql

    try:
        rows = self.executor.session.sql(f"EXPLAIN USING JSON {body}").collect()
        if not rows:
            return None

        parts: list[str] = []
        for r in rows:
            try:
                parts.append(str(r[0]))
            except Exception:
                as_dict: dict[str, Any] = getattr(r, "asDict", lambda: {})()
                if as_dict:
                    parts.extend(str(v) for v in as_dict.values())

        plan_text = "\n".join(parts).strip()
        if not plan_text:
            return None

        try:
            plan_data = json.loads(plan_text)
        except Exception:
            return None

        bytes_val = self._extract_bytes_from_plan(plan_data)
        if bytes_val is None or bytes_val <= 0:
            return None
        return bytes_val
    except Exception:
        # Any parsing / EXPLAIN issues → no estimate, guard skipped
        return None

dataframe_bytes

dataframe_bytes(df)

Best-effort bytes estimate for a Snowpark DataFrame.

Source code in src/fastflowtransform/executors/budget/runtime/snowflake_snowpark.py
68
69
70
71
72
73
74
75
76
77
78
def dataframe_bytes(self, df: Any) -> int | None:
    """
    Best-effort bytes estimate for a Snowpark DataFrame.
    """
    try:
        sql_text = self._snowpark_df_sql(df)
        if not isinstance(sql_text, str) or not sql_text.strip():
            return None
        return self.estimate_query_bytes(sql_text)
    except Exception:
        return None