Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change ReadFileExecute to handle reading from pipes #12128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rustyconover
Copy link

ReadFileExecute assumed that the size of the file could be determined then allocated a buffer and read the contents into that buffer. When using a pipe, the size of the input cannot be determined.

Change the function to dynamically allocate a buffer to read from pipes up to the maximum size of the string.

This change allows my new shellfs function which returns FileHandles that are pipes to function with read_text() and read_blob().

See the extension at:

https://github.com/rustyconover/duckdb-shellfs-extension

ReadFileExecute assumed that the size of the file could be determined
then allocated a buffer and read the contents into that buffer.  When
using a pipe, the size of the input cannot be determined.

Change the function to dynamically allocate a buffer to read from pipes
up to the maximum size of the string.
}
} while (read_bytes > 0);
content_string =
StringVector::AddStringOrBlob(file_content_vector, read_buffer.data(), total_bytes_read);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can do this incremental allocation directly on the StringVector, as that would save us from doing another allocation and copy of the data

But I'm not entirely sure that's feasible with the API right now

Copy link
Contributor

@Tishj Tishj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, the changes make sense to me

I did find this condition to resize the buffer a bit curious

buffer_size - total_bytes_read < initial_buffer_size

If I understand correctly, if the remaining size is under 4096 we will grow the buffer, instead of letting it fill entirely and then growing.

I guess this means our reads will be more efficient though, as we are not having small (< 4096) reads if we are close to the end of the buffer

On another note, as we are very close to a release, it might make more sense to push this to the feature branch, especially as no tests are/can be(?) added for this feature

@rustyconover
Copy link
Author

Thanks for the PR, the changes make sense to me

I did find this condition to resize the buffer a bit curious

buffer_size - total_bytes_read < initial_buffer_size

If I understand correctly, if the remaining size is under 4096 we will grow the buffer, instead of letting it fill entirely and then growing.

I guess this means our reads will be more efficient though, as we are not having small (< 4096) reads if we are close to the end of the buffer

Yes you're right, I just thought 4k was a reasonable size to ensure that the buffer can always read at least a page on Linux.

On another note, as we are very close to a release, it might make more sense to push this to the feature branch, especially as no tests are/can be(?) added for this feature

Tests could be added, but there isn't a filesystem implementation that returns pipes right now. If you wanted to bring the shellfs extension into the main tree it the tests for it could test these changes.

@Tishj
Copy link
Contributor

Tishj commented May 18, 2024

@rustyconover I believe reading from /dev/stdin uses pipes, see tools/shell/tests/test_read_from_stdin.py for a test with this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants