【dbt】Python with dbt ~ rows_affected ~

■ はじめに

dbt の rows_affected を1日中調査してたので
徐々に纏めておく。

目次

【1】rows_affected
【2】Snowflake におけるrows_affected
 2)トラブル:CREATE TABLE ... AS SELECT の場合、rows_affected=1を返す

【1】rows_affected

* 実行されたSQLによって更新された行数
 => クエリの行数を決定できない or 適用できない場合(ビューの作成時など)
  rowcountには標準値の-1が返す。

cf. affected = 影響を受ける

https://docs.getdbt.com/reference/dbt-classes#result-objects

【2】Snowflake におけるrows_affected

https://github.com/dbt-labs/dbt-snowflake/blob/3fbc0749491f40d34014336457753b140e1fb1ee/dbt/adapters/snowflake/connections.py#L458

    @classmethod
    def get_response(cls, cursor) -> SnowflakeAdapterResponse:
        code = cursor.sqlstate

        if code is None:
            code = "SUCCESS"

        return SnowflakeAdapterResponse(
            _message="{} {}".format(code, cursor.rowcount),
            rows_affected=cursor.rowcount, # ★カーソルのRowCountを返している★
            code=code,
            query_id=cursor.sfqid,
        )

2)トラブル:CREATE TABLE ... AS SELECT の場合、rows_affected=1を返す

 Snowflake において
CREATE TABLE ... AS SELECT (CTAS)の場合、
rows_affected=1を返す

(今のところの)結論

* 以下のissue で挙げられている

https://github.com/snowflakedb/snowflake-connector-python/issues/645

関連記事

dbt ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2023/06/30/000000
dbt ~ 環境設定編 ~
https://dk521123.hatenablog.com/entry/2023/12/16/152147
dbt ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/05/30/151003
dbt ~ dbt CLI
https://dk521123.hatenablog.com/entry/2024/07/21/234811
Python with dbt ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/07/20/034930