-
Notifications
You must be signed in to change notification settings - Fork 655
File upload method #1746
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
Open
cl-bvl
wants to merge
18
commits into
ClickHouse:main
Choose a base branch
from
clickadu-com:file_upload_method
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
File upload method #1746
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e37c842
Add InsertFile method
cl-bvl 7011fb9
Add InsertFile method
cl-bvl e02c708
Merge branch 'file_upload_method' of github.com:clickadu-com/clickhou…
cl-bvl f018dd3
Merge branch 'file_upload_method' of github.com:clickadu-com/clickhou…
cl-bvl 68df08c
Merge branch 'file_upload_method' of github.com:clickadu-com/clickhou…
cl-bvl d5d8a07
Lint
cl-bvl c2a673b
Update Readme
cl-bvl 9b7d8cf
Small fix
cl-bvl 09fc370
Merge branch 'main' into file_upload_method
cl-bvl eeabc1e
Move test files
cl-bvl 88b34dc
Merge branch 'file_upload_method' of github.com:clickadu-com/clickhou…
cl-bvl ae3158f
io.Reader instead of file
cl-bvl eeff072
Merge branch 'main' into file_upload_method
cl-bvl 90abf25
Merge branch 'main' into file_upload_method
cl-bvl dba1d37
Fix tests
cl-bvl c3cfe06
Merge branch 'file_upload_method' of github.com:clickadu-com/clickhou…
cl-bvl 56b872f
Merge
cl-bvl b1266ee
Merge branch 'ClickHouse:main' into file_upload_method
cl-bvl 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
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 |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package clickhouse | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| var contentEncodingExtensions = map[string][]string{ | ||
| "gzip": {".gz", ".gzip"}, | ||
| "br": {".br", ".brotli"}, | ||
| "deflate": {".deflate"}, | ||
| "xz": {".xz"}, | ||
| "zstd": {".zst", ".zstd"}, | ||
| "lz4": {".lz", ".lz4"}, | ||
| "bz2": {".bz2"}, | ||
| "snappy": {".snappy"}, | ||
| } | ||
|
|
||
| // uploadFile streams a local file directly to ClickHouse over HTTP as the request body. | ||
| // | ||
| // The file is sent "as-is" without any decompression or recompression on the client side. | ||
| // This method is intended for INSERT ... FORMAT <fmt> queries where the payload is already | ||
| // prepared and optionally compressed (e.g. TSV.zst). | ||
| // | ||
| // Compression handling: | ||
| // - If contentEncoding is explicitly provided, it is set as HTTP "Content-Encoding". | ||
| // - If contentEncoding is empty, the encoding is auto-detected from the file extension | ||
| // (e.g. ".zst" → "zstd", ".gz" → "gzip"). | ||
| // - The driver does NOT attempt to decode, encode, or transform the stream. | ||
| // | ||
| // Parameters: | ||
| // - ctx: request context (cancellation, deadlines). | ||
| // - filePath: path to the file to upload; the file is streamed and not buffered in memory. | ||
| // - query: ClickHouse INSERT query (typically "INSERT INTO <table> FORMAT <format>"). | ||
| // | ||
| // Limitations: | ||
| // - External tables are not supported for file uploads. | ||
| // - This method is available only for the HTTP transport. | ||
| // | ||
| // Typical usage: | ||
| // err := conn.uploadFile(ctx, "data.tsv.zst", "text/tab-separated-values", "zstd", "INSERT INTO db.table FORMAT TSV") | ||
| // | ||
| // On success, the file contents are fully consumed and the request body is discarded. | ||
| func (h *httpConnect) uploadFile(ctx context.Context, reader io.Reader, query string) error { | ||
| options := queryOptions(ctx) | ||
| options.settings["query"] = query | ||
|
|
||
| if len(options.external) > 0 { | ||
| return fmt.Errorf("external tables are not supported for file upload") | ||
| } | ||
| if options.fileContentType == "" { | ||
| options.fileContentType = contentTypeFromFormat(parseFormatFromSQL(query)) | ||
| if options.fileContentType == "" { | ||
| return fmt.Errorf("unknown file Content-Type") | ||
| } | ||
| } | ||
|
|
||
| headers := map[string]string{"Content-Type": options.fileContentType} | ||
| if options.fileEncoding != "" { | ||
| headers["Content-Encoding"] = options.fileEncoding | ||
| } | ||
|
|
||
| switch h.compression { | ||
| case CompressionZSTD, CompressionLZ4: | ||
| options.settings["compress"] = "1" | ||
| case CompressionGZIP, CompressionDeflate, CompressionBrotli: | ||
| // request encoding | ||
| headers["Accept-Encoding"] = h.compression.String() | ||
| } | ||
|
|
||
| req, err := h.createRequest(ctx, h.url.String(), reader, &options, headers) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| res, err := h.executeRequest(req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer discardAndClose(res.Body) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
|
|
||
|
|
||
| func parseFormatFromSQL(query string) string { | ||
| var re = regexp.MustCompile(`(?i)\bformat\b\s*([A-Za-z0-9_]+)`) | ||
| m := re.FindStringSubmatch(query) | ||
| if len(m) > 1 { | ||
| return m[1] | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func contentTypeFromFormat(format string) string { | ||
| formats := map[string][]string{ | ||
| "text/tab-separated-values": { | ||
| "TabSeparated", "TSV", | ||
| "TabSeparatedRaw", "TSVRaw", "Raw", | ||
| "TabSeparatedWithNames", "TSVWithNames", "RawWithNames", | ||
| "TabSeparatedWithNamesAndTypes", "TSVWithNamesAndTypes", "RawWithNamesAndTypes", | ||
| "TabSeparatedRawWithNames", "TSVRawWithNames", "RawWithNames", | ||
| "TabSeparatedRawWithNamesAndTypes", "TSVRawWithNamesAndNames", "RawWithNamesAndNames", | ||
| }, | ||
| "text/csv": {"CSV", "CSVWithNames", "CSVWithNamesAndTypes"}, | ||
| "application/json": { | ||
| "JSON", "JSONAsString", "JSONAsObject", "JSONStrings", "JSONColumns", "JSONColumnsWithMetadata", "JSONObjectEachRow", | ||
| "JSONEachRow", "PrettyJSONEachRow", "JSONEachRowWithProgress", "JSONStringsEachRow", "JSONStringsEachRowWithProgress", | ||
| "JSONCompact", "JSONCompactStrings", "JSONCompactColumns", "JSONCompactEachRow", "JSONCompactEachRowWithNames", | ||
| "JSONCompactEachRowWithNamesAndTypes", "JSONCompactEachRowWithProgress", "JSONCompactStringsEachRow", | ||
| "JSONCompactStringsEachRowWithNames", "JSONCompactStringsEachRowWithNamesAndTypes", "JSONCompactStringsEachRowWithProgress", | ||
| }, | ||
| } | ||
|
|
||
| for contentType, fmts := range formats { | ||
| for _, fmt := range fmts { | ||
| if strings.ToLower(fmt) == strings.ToLower(format) { | ||
| return contentType | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return "application/octet-stream" | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package clickhouse | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| ) | ||
|
|
||
| func (c *connect) uploadFile(ctx context.Context, reader io.Reader, query string) error { | ||
| return fmt.Errorf("UploadFile is not implemented for Native connector") | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.