ParseCopyFromOptions extracts options from a COPY FROM STDIN command
(query string)
| 66 | } |
| 67 | |
| 68 | // DuckDB sizes CSV buffers from MAX_LINE_SIZE. Keep enough headroom for large |
| 69 | // records without exhausting the memory available to a standard worker. |
| 70 | const copyMaxLineSizeBytes = 16 * 1024 * 1024 |
| 71 | |
| 72 | // ParseCopyFromOptions extracts options from a COPY FROM STDIN command |
| 73 | func ParseCopyFromOptions(query string) (*CopyFromOptions, error) { |
| 74 | upperQuery := strings.ToUpper(query) |
| 75 | |
| 76 | matches := copyFromStdinRegex.FindStringSubmatch(query) |
| 77 | if len(matches) < 2 { |
| 78 | return nil, fmt.Errorf("invalid COPY FROM STDIN syntax") |
| 79 | } |
| 80 | |
| 81 | opts := &CopyFromOptions{ |
| 82 | TableName: stripPublicSchema(matches[1]), |
| 83 | Delimiter: "\t", // Default PostgreSQL text format delimiter |
| 84 | NullString: "\\N", // Default PostgreSQL null representation |
| 85 | } |
| 86 | |
| 87 | // Extract column list if present |
| 88 | if len(matches) > 2 && matches[2] != "" { |
| 89 | opts.ColumnList = fmt.Sprintf("(%s)", matches[2]) |
| 90 | } |
| 91 | |
| 92 | // Detect binary format |
| 93 | if copyBinaryRegex.MatchString(upperQuery) { |
| 94 | opts.IsBinary = true |
| 95 | return opts, nil |
| 96 | } |
| 97 | |
| 98 | // Parse delimiter |
| 99 | if m := copyDelimiterRegex.FindStringSubmatch(query); len(m) > 1 { |
| 100 | opts.Delimiter = m[1] |
| 101 | } else if copyWithCSVRegex.MatchString(upperQuery) { |
| 102 | opts.Delimiter = "," |
| 103 | } |
| 104 | |
| 105 | // Parse header option (only valid with CSV) |
| 106 | opts.HasHeader = copyWithCSVRegex.MatchString(upperQuery) && copyWithHeaderRegex.MatchString(upperQuery) |
| 107 | |
| 108 | // Parse NULL string option |
| 109 | if m := copyNullRegex.FindStringSubmatch(query); len(m) > 1 { |
| 110 | opts.NullString = m[1] |
| 111 | } |
| 112 | |
| 113 | // Parse QUOTE option (default " for CSV) |
| 114 | if m := copyQuoteRegex.FindStringSubmatch(query); len(m) > 1 { |
| 115 | opts.Quote = m[1] |
| 116 | } else if copyWithCSVRegex.MatchString(upperQuery) { |
| 117 | opts.Quote = `"` // Default quote character for CSV |
| 118 | } |
| 119 | |
| 120 | // Parse ESCAPE option (default same as QUOTE) |
| 121 | if m := copyEscapeRegex.FindStringSubmatch(query); len(m) > 1 { |
| 122 | opts.Escape = m[1] |
| 123 | } |
| 124 |