Skip to content

Commit 2651b80

Browse files
EDsCODEclaude
andcommitted
Support custom NULL string in COPY FROM STDIN
Adds support for the NULL option in COPY commands (e.g., NULL 'custom-value'). Previously, only the default PostgreSQL null representation (\N) and empty strings were treated as NULL values. Now the custom NULL string specified in the COPY command is also recognized. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 2507a92 commit 2651b80

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

server/conn.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ var (
612612
copyWithCSVRegex = regexp.MustCompile(`(?i)\bCSV\b`)
613613
copyWithHeaderRegex = regexp.MustCompile(`(?i)\bHEADER\b`)
614614
copyDelimiterRegex = regexp.MustCompile(`(?i)\bDELIMITER\s+['"](.)['"]\b`)
615+
copyNullRegex = regexp.MustCompile(`(?i)\bNULL\s+'([^']*)'`)
615616
)
616617

617618
// handleCopy handles COPY TO STDOUT and COPY FROM STDIN commands
@@ -768,6 +769,12 @@ func (c *clientConn) handleCopyIn(query, upperQuery string) error {
768769
}
769770
hasHeader := copyWithCSVRegex.MatchString(upperQuery) && copyWithHeaderRegex.MatchString(upperQuery)
770771

772+
// Parse NULL string option (e.g., NULL 'custom-null-value')
773+
nullString := "\\N" // Default PostgreSQL null representation
774+
if m := copyNullRegex.FindStringSubmatch(query); len(m) > 1 {
775+
nullString = m[1]
776+
}
777+
771778
// Get column count for the table
772779
colQuery := fmt.Sprintf("SELECT * FROM %s LIMIT 0", tableName)
773780
testRows, err := c.db.Query(colQuery)
@@ -828,7 +835,7 @@ func (c *clientConn) handleCopyIn(query, upperQuery string) error {
828835
args := make([]interface{}, len(values))
829836
for i, v := range values {
830837
placeholders[i] = "?"
831-
if v == "\\N" || v == "" {
838+
if v == nullString || v == "\\N" || v == "" {
832839
args[i] = nil
833840
} else {
834841
args[i] = v

0 commit comments

Comments
 (0)