tools/tpch-raw/15.R

qloadm("tools/tpch/001.qs")
duckdb <- asNamespace("duckdb")
drv <- duckdb::duckdb()
con <- DBI::dbConnect(drv)
experimental <- FALSE
invisible(duckdb$rapi_load_rfuns(drv@database_ref))
invisible(DBI::dbExecute(con, 'CREATE MACRO "=="(x, y) AS (x == y)'))
invisible(DBI::dbExecute(con, 'CREATE MACRO "___coalesce"(x, y) AS COALESCE(x, y)'))
df1 <- lineitem
"filter"
rel1 <- duckdb$rel_from_df(con, df1, experimental = experimental)
"filter"
rel2 <- duckdb$rel_filter(
  rel1,
  list(
    duckdb$expr_function(
      "r_base::>=",
      list(
        duckdb$expr_reference("l_shipdate"),
        if ("experimental" %in% names(formals(duckdb$expr_constant))) {
          duckdb$expr_constant(as.Date("1996-01-01"), experimental = experimental)
        } else {
          duckdb$expr_constant(as.Date("1996-01-01"))
        }
      )
    ),
    duckdb$expr_function(
      "r_base::<",
      list(
        duckdb$expr_reference("l_shipdate"),
        if ("experimental" %in% names(formals(duckdb$expr_constant))) {
          duckdb$expr_constant(as.Date("1996-04-01"), experimental = experimental)
        } else {
          duckdb$expr_constant(as.Date("1996-04-01"))
        }
      )
    )
  )
)
"summarise"
rel3 <- duckdb$rel_aggregate(
  rel2,
  groups = list(duckdb$expr_reference("l_suppkey")),
  aggregates = list(
    {
      tmp_expr <- duckdb$expr_function(
        "sum",
        list(
          duckdb$expr_function(
            "*",
            list(
              duckdb$expr_reference("l_extendedprice"),
              duckdb$expr_function(
                "-",
                list(
                  if ("experimental" %in% names(formals(duckdb$expr_constant))) {
                    duckdb$expr_constant(1, experimental = experimental)
                  } else {
                    duckdb$expr_constant(1)
                  },
                  duckdb$expr_reference("l_discount")
                )
              )
            )
          )
        )
      )
      duckdb$expr_set_alias(tmp_expr, "total_revenue")
      tmp_expr
    }
  )
)
"mutate"
rel4 <- duckdb$rel_project(
  rel3,
  list(
    {
      tmp_expr <- duckdb$expr_reference("l_suppkey")
      duckdb$expr_set_alias(tmp_expr, "l_suppkey")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("total_revenue")
      duckdb$expr_set_alias(tmp_expr, "total_revenue")
      tmp_expr
    },
    {
      tmp_expr <- if ("experimental" %in% names(formals(duckdb$expr_constant))) {
        duckdb$expr_constant(1L, experimental = experimental)
      } else {
        duckdb$expr_constant(1L)
      }
      duckdb$expr_set_alias(tmp_expr, "global_agr_key")
      tmp_expr
    }
  )
)
"summarise"
rel5 <- duckdb$rel_aggregate(
  rel4,
  groups = list(duckdb$expr_reference("global_agr_key")),
  aggregates = list(
    {
      tmp_expr <- duckdb$expr_function("max", list(duckdb$expr_reference("total_revenue")))
      duckdb$expr_set_alias(tmp_expr, "max_total_revenue")
      tmp_expr
    }
  )
)
"mutate"
rel6 <- duckdb$rel_project(
  rel3,
  list(
    {
      tmp_expr <- duckdb$expr_reference("l_suppkey")
      duckdb$expr_set_alias(tmp_expr, "l_suppkey")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("total_revenue")
      duckdb$expr_set_alias(tmp_expr, "total_revenue")
      tmp_expr
    },
    {
      tmp_expr <- if ("experimental" %in% names(formals(duckdb$expr_constant))) {
        duckdb$expr_constant(1L, experimental = experimental)
      } else {
        duckdb$expr_constant(1L)
      }
      duckdb$expr_set_alias(tmp_expr, "global_agr_key")
      tmp_expr
    }
  )
)
"inner_join"
rel7 <- duckdb$rel_set_alias(rel6, "lhs")
"inner_join"
rel8 <- duckdb$rel_set_alias(rel5, "rhs")
"inner_join"
rel9 <- duckdb$rel_project(
  rel7,
  list(
    {
      tmp_expr <- duckdb$expr_reference("l_suppkey")
      duckdb$expr_set_alias(tmp_expr, "l_suppkey_x")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("total_revenue")
      duckdb$expr_set_alias(tmp_expr, "total_revenue_x")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("global_agr_key")
      duckdb$expr_set_alias(tmp_expr, "global_agr_key_x")
      tmp_expr
    }
  )
)
"inner_join"
rel10 <- duckdb$rel_project(
  rel8,
  list(
    {
      tmp_expr <- duckdb$expr_reference("global_agr_key")
      duckdb$expr_set_alias(tmp_expr, "global_agr_key_y")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("max_total_revenue")
      duckdb$expr_set_alias(tmp_expr, "max_total_revenue_y")
      tmp_expr
    }
  )
)
"inner_join"
rel11 <- duckdb$rel_join(
  rel9,
  rel10,
  list(
    duckdb$expr_function(
      "==",
      list(duckdb$expr_reference("global_agr_key_x", rel9), duckdb$expr_reference("global_agr_key_y", rel10))
    )
  ),
  "inner"
)
"inner_join"
rel12 <- duckdb$rel_project(
  rel11,
  list(
    {
      tmp_expr <- duckdb$expr_reference("l_suppkey_x")
      duckdb$expr_set_alias(tmp_expr, "l_suppkey")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("total_revenue_x")
      duckdb$expr_set_alias(tmp_expr, "total_revenue")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_function(
        "___coalesce",
        list(duckdb$expr_reference("global_agr_key_x", rel9), duckdb$expr_reference("global_agr_key_y", rel10))
      )
      duckdb$expr_set_alias(tmp_expr, "global_agr_key")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("max_total_revenue_y")
      duckdb$expr_set_alias(tmp_expr, "max_total_revenue")
      tmp_expr
    }
  )
)
"filter"
rel13 <- duckdb$rel_filter(
  rel12,
  list(
    duckdb$expr_function(
      "r_base::<",
      list(
        duckdb$expr_function(
          "abs",
          list(
            duckdb$expr_function(
              "-",
              list(duckdb$expr_reference("total_revenue"), duckdb$expr_reference("max_total_revenue"))
            )
          )
        ),
        if ("experimental" %in% names(formals(duckdb$expr_constant))) {
          duckdb$expr_constant(1e-09, experimental = experimental)
        } else {
          duckdb$expr_constant(1e-09)
        }
      )
    )
  )
)
"inner_join"
rel14 <- duckdb$rel_set_alias(rel13, "lhs")
df2 <- supplier
"inner_join"
rel15 <- duckdb$rel_from_df(con, df2, experimental = experimental)
"inner_join"
rel16 <- duckdb$rel_set_alias(rel15, "rhs")
"inner_join"
rel17 <- duckdb$rel_join(
  rel14,
  rel16,
  list(
    duckdb$expr_function(
      "==",
      list(duckdb$expr_reference("l_suppkey", rel14), duckdb$expr_reference("s_suppkey", rel16))
    )
  ),
  "inner"
)
"inner_join"
rel18 <- duckdb$rel_project(
  rel17,
  list(
    {
      tmp_expr <- duckdb$expr_function(
        "___coalesce",
        list(duckdb$expr_reference("l_suppkey", rel14), duckdb$expr_reference("s_suppkey", rel16))
      )
      duckdb$expr_set_alias(tmp_expr, "l_suppkey")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("total_revenue")
      duckdb$expr_set_alias(tmp_expr, "total_revenue")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("global_agr_key")
      duckdb$expr_set_alias(tmp_expr, "global_agr_key")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("max_total_revenue")
      duckdb$expr_set_alias(tmp_expr, "max_total_revenue")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("s_name")
      duckdb$expr_set_alias(tmp_expr, "s_name")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("s_address")
      duckdb$expr_set_alias(tmp_expr, "s_address")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("s_nationkey")
      duckdb$expr_set_alias(tmp_expr, "s_nationkey")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("s_phone")
      duckdb$expr_set_alias(tmp_expr, "s_phone")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("s_acctbal")
      duckdb$expr_set_alias(tmp_expr, "s_acctbal")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("s_comment")
      duckdb$expr_set_alias(tmp_expr, "s_comment")
      tmp_expr
    }
  )
)
"select"
rel19 <- duckdb$rel_project(
  rel18,
  list(
    {
      tmp_expr <- duckdb$expr_reference("l_suppkey")
      duckdb$expr_set_alias(tmp_expr, "s_suppkey")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("s_name")
      duckdb$expr_set_alias(tmp_expr, "s_name")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("s_address")
      duckdb$expr_set_alias(tmp_expr, "s_address")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("s_phone")
      duckdb$expr_set_alias(tmp_expr, "s_phone")
      tmp_expr
    },
    {
      tmp_expr <- duckdb$expr_reference("total_revenue")
      duckdb$expr_set_alias(tmp_expr, "total_revenue")
      tmp_expr
    }
  )
)
rel19
duckdb$rel_to_altrep(rel19)
duckdblabs/duckplyr documentation built on Nov. 6, 2024, 10 p.m.