lidavidm commented on code in PR #1508:
URL: https://github.com/apache/arrow-go/pull/1508#discussion_r4544752863
##########
arrow/csv/transformer.go:
##########
@@ -304,3 +296,38 @@ func (w *Writer) transformColToStringArr(typ
arrow.DataType, col arrow.Array, st
}
return res, nil
}
+
+// encodeListItems formats list items as {item1,item2,...}, quoting items with
+// CSV rules when they are empty or contain characters that would be ambiguous
+// inside the list, so that the reader can parse them back. An unquoted empty
+// item would be lost: {} is an empty list. Quoting braces keeps the items
+// intact for readers that strip every leading and trailing brace.
+//
+// A quoted item is never read back as null, so null items are written as the
+// null value unquoted, and non-null items equal to it are quoted. With an
+// empty null value, a list whose only item is null is written as {}.
+func encodeListItems(values []string, list arrow.Array, nullValue string)
string {
+ var b strings.Builder
+ b.WriteByte('{')
+ for i, v := range values {
+ if i > 0 {
+ b.WriteByte(',')
+ }
+ if list.IsNull(i) || (!listItemNeedsQuotes(v) && v !=
nullValue) {
+ b.WriteString(v)
+ continue
+ }
+ b.WriteByte('"')
+ b.WriteString(strings.ReplaceAll(v, `"`, `""`))
+ b.WriteByte('"')
+ }
+ b.WriteByte('}')
+ return b.String()
+}
+
+func listItemNeedsQuotes(v string) bool {
+ if v == "" || strings.ContainsAny(v, ",\"\r\n{}") {
Review Comment:
Does this need to account for the user being able to override the comma
character?
##########
arrow/csv/transformer.go:
##########
@@ -304,3 +296,38 @@ func (w *Writer) transformColToStringArr(typ
arrow.DataType, col arrow.Array, st
}
return res, nil
}
+
+// encodeListItems formats list items as {item1,item2,...}, quoting items with
+// CSV rules when they are empty or contain characters that would be ambiguous
+// inside the list, so that the reader can parse them back. An unquoted empty
+// item would be lost: {} is an empty list. Quoting braces keeps the items
+// intact for readers that strip every leading and trailing brace.
+//
+// A quoted item is never read back as null, so null items are written as the
+// null value unquoted, and non-null items equal to it are quoted. With an
+// empty null value, a list whose only item is null is written as {}.
+func encodeListItems(values []string, list arrow.Array, nullValue string)
string {
+ var b strings.Builder
+ b.WriteByte('{')
+ for i, v := range values {
+ if i > 0 {
+ b.WriteByte(',')
+ }
+ if list.IsNull(i) || (!listItemNeedsQuotes(v) && v !=
nullValue) {
+ b.WriteString(v)
+ continue
+ }
+ b.WriteByte('"')
+ b.WriteString(strings.ReplaceAll(v, `"`, `""`))
+ b.WriteByte('"')
+ }
+ b.WriteByte('}')
+ return b.String()
+}
+
+func listItemNeedsQuotes(v string) bool {
+ if v == "" || strings.ContainsAny(v, ",\"\r\n{}") {
Review Comment:
I guess even before everything was hardcoded, though
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]