# select after distinct produces subquery

    Code
      select(distinct(lf), x)
    Output
      <SQL>
      SELECT "x"
      FROM (
        SELECT DISTINCT *
        FROM "df"
      ) AS "q01"

# rename/relocate after distinct is inlined #1141

    Code
      rename(distinct(lf), z = y)
    Output
      <SQL>
      SELECT DISTINCT "x", "y" AS "z"
      FROM "df"
    Code
      relocate(distinct(lf), y)
    Output
      <SQL>
      SELECT DISTINCT "y", "x"
      FROM "df"

# select preserves grouping vars

    Code
      out <- collect(select(mf, a))
    Message
      Adding missing grouping variables: `b`

# select() after left_join() is inlined

    Code
      (out <- select(left_join(lf1, lf2, by = "x"), b, x))
    Output
      <SQL>
      SELECT "b", "lf1"."x" AS "x"
      FROM "lf1"
      LEFT JOIN "lf2"
        ON ("lf1"."x" = "lf2"."x")

---

    Code
      (out <- relocate(left_join(lf1, lf2, by = "x"), b))
    Output
      <SQL>
      SELECT "b", "lf1".*
      FROM "lf1"
      LEFT JOIN "lf2"
        ON ("lf1"."x" = "lf2"."x")

# select() after filter() on join preserves qualified where

    Code
      out
    Output
      <SQL>
      SELECT "x", "y"
      FROM (
        SELECT "t1".*, "z"
        FROM "t1"
        LEFT JOIN "t2"
          ON ("t1"."x" = "t2"."x")
        WHERE ("t2"."z" > 0.0)
      ) AS "q01"

---

    Code
      out
    Output
      <SQL>
      SELECT "x", "y", "z" AS "zz"
      FROM (
        SELECT "t1".*, "z"
        FROM "t1"
        LEFT JOIN "t2"
          ON ("t1"."x" = "t2"."x")
        WHERE ("t2"."z" > 0.0)
      ) AS "q01"

# select() after semi_join() is inlined

    Code
      (out <- select(semi_join(lf1, lf2, by = "x"), x, a2 = a))
    Output
      <SQL>
      SELECT "x", "a" AS "a2"
      FROM "lf1"
      WHERE EXISTS (
        SELECT 1 FROM "lf2"
        WHERE ("lf1"."x" = "lf2"."x")
      )

---

    Code
      (out <- relocate(anti_join(lf1, lf2, by = "x"), a))
    Output
      <SQL>
      SELECT "a", "x"
      FROM "lf1"
      WHERE NOT EXISTS (
        SELECT 1 FROM "lf2"
        WHERE ("lf1"."x" = "lf2"."x")
      )

# select() after join handles previous select

    Code
      print(lf)
    Output
      <SQL>
      SELECT "x" AS "x2", "y" AS "y3", "z"
      FROM "df" AS "df_LHS"
      WHERE EXISTS (
        SELECT 1 FROM "df" AS "df_RHS"
        WHERE ("df_LHS"."x" = "df_RHS"."x")
      )

---

    Code
      print(lf2)
    Output
      <SQL>
      SELECT "df_LHS"."x" AS "x2", "df_LHS"."y" AS "y3", "z"
      FROM "df" AS "df_LHS"
      LEFT JOIN "df" AS "df_RHS"
        ON ("df_LHS"."x" = "df_RHS"."x")

# select() produces nice error messages

    Code
      select(lf, non_existent)
    Condition
      Error in `select()`:
      ! Can't select columns that don't exist.
      x Column `non_existent` doesn't exist.
    Code
      select(lf, non_existent + 1)
    Condition
      Error in `select()`:
      i In argument: `non_existent + 1`.
      Caused by error:
      ! object 'non_existent' not found

---

    Code
      relocate(lf, non_existent)
    Condition
      Error in `relocate()`:
      ! Can't relocate columns that don't exist.
      x Column `non_existent` doesn't exist.
    Code
      relocate(lf, non_existent + 1)
    Condition
      Error in `relocate()`:
      i In argument: `non_existent + 1`.
      Caused by error:
      ! object 'non_existent' not found

---

    Code
      rename(lf, x)
    Condition
      Error in `rename()`:
      ! All renaming inputs must be named.
    Code
      rename(lf, y = non_existent)
    Condition
      Error in `rename()`:
      ! Can't rename columns that don't exist.
      x Column `non_existent` doesn't exist.
    Code
      rename(lf, y = non_existent + 1)
    Condition
      Error in `rename()`:
      i In argument: `non_existent + 1`.
      Caused by error:
      ! object 'non_existent' not found

---

    Code
      rename_with(lf, toupper, .cols = non_existent)
    Condition
      Error in `rename_with()`:
      ! Can't select columns that don't exist.
      x Column `non_existent` doesn't exist.
    Code
      rename_with(lf, toupper, .cols = non_existent + 1)
    Condition
      Error in `rename_with()`:
      i In argument: `non_existent + 1`.
      Caused by error:
      ! object 'non_existent' not found

# where() isn't suppored

    Code
      select(lf, where(is.integer))
    Condition
      Error in `select()`:
      ! This tidyselect interface doesn't support predicates.

# arranged computed columns are not inlined away

    Code
      select(arrange(mutate(lf, z = 1), x, z), x)
    Condition
      Warning:
      ORDER BY is ignored in subqueries without LIMIT
      i Do you need to move arrange() later in the pipeline or use window_order() instead?
    Output
      <SQL>
      SELECT "x"
      FROM (
        SELECT *, 1.0 AS "z"
        FROM "df"
      ) AS "q01"

# multiple selects are collapsed

    Code
      select(select(lf, 2:1), 2:1)
    Output
      <SQL>
      SELECT *
      FROM "df"

---

    Code
      select(select(select(lf, 2:1), 2:1), 2:1)
    Output
      <SQL>
      SELECT "y", "x"
      FROM "df"

---

    Code
      select(select(lf, x1 = x), x2 = x1)
    Output
      <SQL>
      SELECT "x" AS "x2"
      FROM "df"

# mutate collapses over nested select

    Code
      select(mutate(lf, a = 1, b = 2), a)
    Output
      <SQL>
      SELECT 1.0 AS "a"
      FROM "df"

---

    Code
      select(mutate(lf, a = 1, b = 2), x)
    Output
      <SQL>
      SELECT "x"
      FROM "df"

# output is styled

    Code
      show_query(out, sql_options = sql_options(cte = TRUE))
    Output
      <SQL>
      WITH "q01" AS (
        [34mSELECT[39m "x", AVG("y") OVER (PARTITION BY "x")[34m AS [39m"y", "z" + 1.0[34m AS [39m"z"
        [34mFROM[39m "df"
      ),
      "q02" AS (
        [34mSELECT[39m *
        [34mFROM[39m "q01"
        [34mWHERE[39m ("z" = 1.0)
      )
      [34mSELECT[39m
        "LHS"."x"[34m AS [39m"x",
        "LHS"."y"[34m AS [39m"y.x",
        "LHS"."z"[34m AS [39m"z.x",
        "df"."y"[34m AS [39m"y.y",
        "df"."z"[34m AS [39m"z.y"
      [34mFROM[39m "q02"[34m AS [39m"LHS"
      [34mLEFT JOIN[39m "df"
        [34mON[39m ("LHS"."x" = "df"."x")

