-
Notifications
You must be signed in to change notification settings - Fork 32
Copy to/from stdout/stdin with (format parquet) #121
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,6 +84,7 @@ jobs: | |
| postgresql-server-dev-${{ env.PG_MAJOR }} \ | ||
| postgresql-client-${{ env.PG_MAJOR }} \ | ||
| libpq-dev | ||
| echo "export PG_MAJOR=${{ env.PG_MAJOR }}" >> $GITHUB_ENV | ||
|
|
||
| - name: Install azure-cli | ||
| run: | | ||
|
|
@@ -94,7 +95,8 @@ jobs: | |
| - name: Install and configure pgrx | ||
| run: | | ||
| cargo install --locked [email protected] | ||
| cargo pgrx init --pg${{ env.PG_MAJOR }} /usr/lib/postgresql/${{ env.PG_MAJOR }}/bin/pg_config | ||
| cargo pgrx init --pg${{ env.PG_MAJOR }} /usr/lib/postgresql/${{ env.PG_MAJOR }}/bin/pg_config \ | ||
| --base-testing-port $PGRX_TEST_PG_BASE_PORT | ||
|
|
||
| - name: Install cargo-llvm-cov for coverage report | ||
| run: cargo install --locked [email protected] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| use std::{panic, sync::Arc}; | ||
| use std::{ffi::CStr, panic, sync::Arc}; | ||
|
|
||
| use arrow::datatypes::SchemaRef; | ||
| use object_store::{path::Path, ObjectStoreScheme}; | ||
|
|
@@ -13,7 +13,11 @@ | |
| }; | ||
| use pgrx::{ | ||
| ereport, | ||
| pg_sys::{get_role_oid, has_privs_of_role, superuser, AsPgCStr, GetUserId}, | ||
| ffi::c_char, | ||
| pg_sys::{ | ||
| get_role_oid, has_privs_of_role, palloc0, superuser, AsPgCStr, DataDir, FileClose, | ||
| FilePathName, GetUserId, InvalidOid, OpenTemporaryFile, TempTablespacePath, MAXPGPATH, | ||
| }, | ||
| }; | ||
| use url::Url; | ||
|
|
||
|
|
@@ -29,15 +33,48 @@ | |
| const PARQUET_OBJECT_STORE_WRITE_ROLE: &str = "parquet_object_store_write"; | ||
|
|
||
| // ParsedUriInfo is a struct that holds the parsed uri information. | ||
| #[derive(Debug, Clone)] | ||
| #[derive(Debug)] | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make no cloneable to make sure Drop runs once |
||
| pub(crate) struct ParsedUriInfo { | ||
| pub(crate) uri: Url, | ||
| pub(crate) bucket: Option<String>, | ||
| pub(crate) path: Path, | ||
| pub(crate) scheme: ObjectStoreScheme, | ||
| pub(crate) stdio_tmp_fd: Option<i32>, | ||
| } | ||
|
|
||
| impl ParsedUriInfo { | ||
| pub(crate) fn for_stdout() -> Self { | ||
| // open temp postgres file, which is removed after transaction ends | ||
| let tmp_path_fd = unsafe { OpenTemporaryFile(false) }; | ||
|
|
||
| let tmp_path = unsafe { | ||
| let data_dir = CStr::from_ptr(DataDir).to_str().expect("invalid base dir"); | ||
|
|
||
| let tmp_tblspace_path: *const c_char = palloc0(MAXPGPATH as _) as _; | ||
| TempTablespacePath(tmp_tblspace_path as _, InvalidOid); | ||
| let tmp_tblspace_path = CStr::from_ptr(tmp_tblspace_path) | ||
| .to_str() | ||
| .expect("invalid temp tablespace path"); | ||
|
|
||
| let tmp_file_path = FilePathName(tmp_path_fd); | ||
| let tmp_file_path = CStr::from_ptr(tmp_file_path) | ||
| .to_str() | ||
| .expect("invalid temp path"); | ||
|
|
||
| let tmp_path = std::path::Path::new(data_dir) | ||
| .join(tmp_tblspace_path) | ||
| .join(tmp_file_path); | ||
|
|
||
| tmp_path.to_str().expect("invalid tmp path").to_string() | ||
| }; | ||
|
|
||
| let mut parsed_uri = Self::try_from(tmp_path.as_str()).unwrap_or_else(|e| panic!("{}", e)); | ||
|
|
||
| parsed_uri.stdio_tmp_fd = Some(tmp_path_fd); | ||
|
|
||
| parsed_uri | ||
| } | ||
|
|
||
| fn try_parse_uri(uri: &str) -> Result<Url, String> { | ||
| if !uri.contains("://") { | ||
| // local file | ||
|
|
@@ -92,10 +129,20 @@ | |
| bucket, | ||
| path, | ||
| scheme, | ||
| stdio_tmp_fd: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for ParsedUriInfo { | ||
| fn drop(&mut self) { | ||
| if let Some(stdio_tmp_fd) = self.stdio_tmp_fd { | ||
| // close temp file, postgres api will remove it on close | ||
| unsafe { FileClose(stdio_tmp_fd) }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn uri_as_string(uri: &Url) -> String { | ||
| if uri.scheme() == "file" { | ||
| // removes file:// prefix from the local path uri | ||
|
|
@@ -109,7 +156,7 @@ | |
| uri.to_string() | ||
| } | ||
|
|
||
| pub(crate) fn parquet_schema_from_uri(uri_info: ParsedUriInfo) -> SchemaDescriptor { | ||
| pub(crate) fn parquet_schema_from_uri(uri_info: &ParsedUriInfo) -> SchemaDescriptor { | ||
| let parquet_reader = parquet_reader_from_uri(uri_info); | ||
|
|
||
| let arrow_schema = parquet_reader.schema(); | ||
|
|
@@ -119,19 +166,18 @@ | |
| .unwrap_or_else(|e| panic!("{}", e)) | ||
| } | ||
|
|
||
| pub(crate) fn parquet_metadata_from_uri(uri_info: ParsedUriInfo) -> Arc<ParquetMetaData> { | ||
| pub(crate) fn parquet_metadata_from_uri(uri_info: &ParsedUriInfo) -> Arc<ParquetMetaData> { | ||
| let uri = uri_info.uri.clone(); | ||
|
|
||
| let copy_from = true; | ||
| let (parquet_object_store, location) = get_or_create_object_store(uri_info.clone(), copy_from); | ||
| let (parquet_object_store, location) = get_or_create_object_store(uri_info, copy_from); | ||
|
|
||
| PG_BACKEND_TOKIO_RUNTIME.block_on(async { | ||
| let object_store_meta = parquet_object_store | ||
| .head(&location) | ||
| .await | ||
| .unwrap_or_else(|e| { | ||
| panic!( | ||
| "failed to get object store metadata for uri {}: {}", | ||
| uri_info.uri, e | ||
| ) | ||
| panic!("failed to get object store metadata for uri {}: {}", uri, e) | ||
| }); | ||
|
|
||
| let parquet_object_reader = | ||
|
|
@@ -149,20 +195,19 @@ | |
| pub(crate) const RECORD_BATCH_SIZE: i64 = 1024; | ||
|
|
||
| pub(crate) fn parquet_reader_from_uri( | ||
| uri_info: ParsedUriInfo, | ||
| uri_info: &ParsedUriInfo, | ||
| ) -> ParquetRecordBatchStream<ParquetObjectReader> { | ||
| let uri = uri_info.uri.clone(); | ||
|
|
||
| let copy_from = true; | ||
| let (parquet_object_store, location) = get_or_create_object_store(uri_info.clone(), copy_from); | ||
| let (parquet_object_store, location) = get_or_create_object_store(uri_info, copy_from); | ||
|
|
||
| PG_BACKEND_TOKIO_RUNTIME.block_on(async { | ||
| let object_store_meta = parquet_object_store | ||
| .head(&location) | ||
| .await | ||
| .unwrap_or_else(|e| { | ||
| panic!( | ||
| "failed to get object store metadata for uri {}: {}", | ||
| uri_info.uri, e | ||
| ) | ||
| panic!("failed to get object store metadata for uri {}: {}", uri, e) | ||
| }); | ||
|
|
||
| let parquet_object_reader = | ||
|
|
@@ -205,12 +250,12 @@ | |
| } | ||
|
|
||
| pub(crate) fn parquet_writer_from_uri( | ||
| uri_info: ParsedUriInfo, | ||
| uri_info: &ParsedUriInfo, | ||
| arrow_schema: SchemaRef, | ||
| writer_props: WriterProperties, | ||
| ) -> AsyncArrowWriter<ParquetObjectWriter> { | ||
| let copy_from = false; | ||
| let (parquet_object_store, location) = get_or_create_object_store(uri_info.clone(), copy_from); | ||
| let (parquet_object_store, location) = get_or_create_object_store(uri_info, copy_from); | ||
|
|
||
| let parquet_object_writer = ParquetObjectWriter::new(parquet_object_store, location); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| pub(crate) mod copy_from; | ||
| pub(crate) mod copy_from_stdin; | ||
| pub(crate) mod copy_to; | ||
| pub(crate) mod copy_to_dest_receiver; | ||
| pub(crate) mod copy_to_split_dest_receiver; | ||
| pub(crate) mod copy_to_stdout; | ||
| pub(crate) mod copy_utils; | ||
| pub(crate) mod hook; | ||
| pub(crate) mod pg_compat; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to make Drop fail safe.