You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoid touching the row names of a data frame if they are ALTREP #13540
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?
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.
@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:
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
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
@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
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.
Activity
DavisVaughan commentedon Aug 23, 2023
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.
DavisVaughan commentedon Aug 23, 2023
@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 requestingnrow()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:rstudio/src/cpp/session/modules/SessionEnvironment.R
Lines 531 to 536 in 9e5ccb2
(hopefully it is the only issue)
We should probably change this to do something like:
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
Screen.Recording.2023-08-23.at.10.34.00.AM.mov
kevinushey commentedon Aug 23, 2023
Hmm, but:
Should this have some other custom class here?
kevinushey commentedon Aug 23, 2023
Can we request the length of the vector without materializing it?
DavisVaughan commentedon Aug 23, 2023
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, yeakevinushey commentedon Aug 23, 2023
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
DavisVaughan commentedon Aug 23, 2023
@krlmlr do you think you can implement
Dataptr_or_nullALTREP methods for your ALTREP types?Should be fairly straightforward, something like "if this object has been materialized before, return the
DATAPTR()to it, otherwise returnNULLto 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
krlmlr commentedon Aug 24, 2023
I see what you mean, just using
ALTREP()to query ALTREP-ness won't suffice, I suppose. I'll put something together.kevinushey commentedon Aug 24, 2023
@DavisVaughan or @krlmlr: would you be able to test with an upcoming daily build of RStudio and confirm if the issue is resolved?
krlmlr commentedon Aug 24, 2023
Sure, happy to test! Will this require a duckdb version with "dataptr or null"?
kevinushey commentedon Aug 24, 2023
It shouldn't, the only downside is that duckdb data.frames will always disable in the Environment pane as having
?? obs. of <n> columns.krlmlr commentedon Aug 25, 2023
Draft implementation here. How would I test it?
duckdb/duckdb@main...krlmlr:duckdb:f-dataptr-or-null
DavisVaughan commentedon Aug 25, 2023
Rather than
return R_NilValue;inRownamesDataptrOrNull()I think you want to literally return the CNULLpointerFor testing:
??shown as the number of observationsdfmanually??should update to1(or however many rows you have)DavisVaughan commentedon Aug 25, 2023
@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
kevinushey commentedon Aug 25, 2023
Awesome, thank you!
krlmlr commentedon Aug 31, 2023
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.