Skip to content

Avoid touching the row names of a data frame if they are ALTREP #13540

Closed

Description

@krlmlr

The example below works differently in the RStudio IDE and in an R script.

The "materializing" message is triggered by the IDE accessing the "row.names" attribute of the df variable. The df is a special data frame, computation starts to happen (via ALTREP) when something tickles the row names. I suspect that this is because df is shown in the environment pane, with details. Can we avoid analyzing the dimensions (row count) of a data frame if the "row.names" attribute is ALTREP?

CC @DavisVaughan @hannes @kevinushey.

RStudio IDE

options(duckdb.materialize_message = TRUE)

con <- DBI::dbConnect(duckdb::duckdb())

rel <- duckdb:::rel_from_df(con, data.frame(a = 1))

df <- duckdb:::rel_to_altrep(rel)

class(df)
#> [1] "data.frame"
#> materializing:
#> ---------------------
#> --- Relation Tree ---
#> ---------------------
#> r_dataframe_scan(0x11e98bfd8)
#> 
#> ---------------------
#> -- Result Columns  --
#> ---------------------
#> - a (DOUBLE)
#> 

Faked on 2023-08-23 with reprex v2.0.2

R script

options(duckdb.materialize_message = TRUE)

con <- DBI::dbConnect(duckdb::duckdb())

rel <- duckdb:::rel_from_df(con, data.frame(a = 1))

df <- duckdb:::rel_to_altrep(rel)

class(df)
#> [1] "data.frame"

Created on 2023-08-23 with reprex v2.0.2

Activity

  1. added
    new
    on Aug 23, 2023
  2. DavisVaughan commented on Aug 23, 2023

    @DavisVaughan

    I do think we should try and fix this, but a "horrible hack" that at least makes it workable for now is to make the env pane update manually. As long as you don't use the env pane it won't materialize early.

    Screenshot 2023-08-23 at 10 02 54 AM
  3. DavisVaughan commented on Aug 23, 2023

    @DavisVaughan

    @kevinushey to be super clear, the issue is that this object is a typical subclassed data frame, but the row names attribute itself is an ALTREP object which we are materializing early with a dim() / nrow() call. In this particular case, the columns of the data frame aren't actually materialized, so we don't know how many rows are in the data frame, and requesting nrow() forces materialization of the full data frame to figure that out (which can be extremely expensive)


    I believe I've tracked it down to the dim() call here:

    return(paste(dim(obj)[1],
    "obs. of",
    dim(obj)[2],
    ifelse(dim(obj)[2] == 1, "variable", "variables"),
    sep=" "))
    }

    (hopefully it is the only issue)

    We should probably change this to do something like:

    n_cols <- length(obj)
    
    # Query internal row names attribute 
    has_altrep_rownames <- .rs.isAltrep(.row_names_info(obj, 0L))
    
    if (has_altrep_rownames) {
      n_row <- -1L
    } else {
      n_row <- nrow(obj)
    }

    And then build the string from there, possibly using ?? for the number of rows in the ALTREP case.


    The goal here would be for the data frame to be able to show up in the Env pane without early materialization until the user presses the "expand this object so i can see the columns" or "show this object in the viewer" buttons. i.e. the same behavior as
    #4764

    It seems to work pretty well with my hacked in proposal. When we expand the data frame we see the materialization message in the viewer but this only happens when you have that option set, and that is mainly for debugging, so I think that is okay?

    Here is my concrete proposal for that section of the code

      else if (is.data.frame(obj)) {
        # Query internal row names attribute 
        has_altrep_rownames <- .rs.isAltrep(.row_names_info(obj, 0L))
        
        if (has_altrep_rownames) {
          n_row <- "??"
        } else {
          n_row <- nrow(obj)
        }
        
        n_cols <- length(obj)
        
        if (n_cols == 1L) {
          variable <- "variable"
        } else {
          variable <- "variables"
        }
        
        return(paste(n_row, "obs. of", n_cols, variable, sep = " "))
      }
    Screen.Recording.2023-08-23.at.10.34.00.AM.mov
  4. removed
    new
    on Aug 23, 2023
  5. added this to the Desert Sunflower milestone on Aug 23, 2023
  6. self-assigned this
    on Aug 23, 2023
  7. kevinushey commented on Aug 23, 2023

    @kevinushey
    Collaborator

    Hmm, but:

    > df <- duckdb:::rel_to_altrep(rel)
    > class(df)
    [1] "data.frame"
    

    Should this have some other custom class here?

  8. kevinushey commented on Aug 23, 2023

    @kevinushey
    Collaborator

    Can we request the length of the vector without materializing it?

  9. DavisVaughan commented on Aug 23, 2023

    @DavisVaughan

    Should this have some other custom class here?

    Sorry I spoke incorrectly about that earlier. The data frame that duckdb produces there is a typical run of the mill data frame. But the row-names attribute is ALTREP and the vectors that make up the columns are ALTREP.

    You should be able to call length() on it to request the number of columns without materializing, yea

  10. kevinushey commented on Aug 23, 2023

    @kevinushey
    Collaborator

    I think duckdb needs to implement dataptr_or_null so that we can query the vector length after it's been materialized here: https://github.com/duckdb/duckdb/blob/3196df79cecac38c867af084f19924c78dfc9d29/tools/rpkg/src/reltoaltrep.cpp#L34-L38

  11. DavisVaughan commented on Aug 23, 2023

    @DavisVaughan

    @krlmlr do you think you can implement Dataptr_or_null ALTREP methods for your ALTREP types?

    Should be fairly straightforward, something like "if this object has been materialized before, return the DATAPTR() to it, otherwise return NULL to refuse to materialize it"

    That way RStudio can actually show the right number of rows if the data frame has been materialized

    The deferred string methods should be a good guide
    https://github.com/wch/r-source/blob/79e959e4c8ad12fbeeba31667b67e7fe8117aec7/src/main/altclasses.c#L764
    https://github.com/wch/r-source/blob/79e959e4c8ad12fbeeba31667b67e7fe8117aec7/src/main/altclasses.c#L855

  12. krlmlr commented on Aug 24, 2023

    @krlmlr
    ContributorAuthor

    I see what you mean, just using ALTREP() to query ALTREP-ness won't suffice, I suppose. I'll put something together.

  13. kevinushey commented on Aug 24, 2023

    @kevinushey
    Collaborator

    @DavisVaughan or @krlmlr: would you be able to test with an upcoming daily build of RStudio and confirm if the issue is resolved?

  14. krlmlr commented on Aug 24, 2023

    @krlmlr
    ContributorAuthor

    Sure, happy to test! Will this require a duckdb version with "dataptr or null"?

  15. kevinushey commented on Aug 24, 2023

    @kevinushey
    Collaborator

    It shouldn't, the only downside is that duckdb data.frames will always disable in the Environment pane as having ?? obs. of <n> columns.

  16. krlmlr commented on Aug 25, 2023

    @krlmlr
    ContributorAuthor

    Draft implementation here. How would I test it?

    duckdb/duckdb@main...krlmlr:duckdb:f-dataptr-or-null

  17. DavisVaughan commented on Aug 25, 2023

    @DavisVaughan

    Rather than return R_NilValue; in RownamesDataptrOrNull() I think you want to literally return the C NULL pointer


    For testing:

  18. DavisVaughan commented on Aug 25, 2023

    @DavisVaughan

    @kevinushey I see the current expected behavior with CRAN duckdb / duckplyr in the latest daily, thanks!

    Screen.Recording.2023-08-25.at.9.48.58.AM.mov
  19. kevinushey commented on Aug 25, 2023

    @kevinushey
    Collaborator

    Awesome, thank you!

  20. krlmlr commented on Aug 31, 2023

    @krlmlr
    ContributorAuthor

    Tested with the nightly, works like a charm. I love how the preview shows the query plan.

    With a variant of duckdb/duckdb@main...krlmlr:duckdb:f-dataptr-or-null, the "??" also disappears when I hit the "refresh" button". The "??" remains if I merely click on the "table" icon or on the list item. I wonder if these actions should also trigger a refresh of the clicked item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions