You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ze...@apache.org on 2025/04/18 12:03:39 UTC

(arrow-go) branch main updated: feat(parquet/compress): add MarshalText and UnmarshalText (#357)

This is an automated email from the ASF dual-hosted git repository.

zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 4e6db6f  feat(parquet/compress): add MarshalText and UnmarshalText (#357)
4e6db6f is described below

commit 4e6db6f219d659ccf159f0d23fa84a08c84e37f3
Author: Jeroen Demeyer <je...@gmail.com>
AuthorDate: Fri Apr 18 14:03:34 2025 +0200

    feat(parquet/compress): add MarshalText and UnmarshalText (#357)
    
    ### Rationale for this change
    
    Right now, there is no public API to convert a string like `"ZSTD"` to a
    compression codec. There is only
    https://pkg.go.dev/github.com/apache/arrow-go/v18@v18.2.0/parquet/internal/gen-go/parquet#CompressionCodecFromString
    but that can't be used since it's in an `internal` package.
    
    Adding `UnmarshalText` can be used for applications taking such a string
    as configuration option. Adding `MarshalText` isn't strictly needed
    since it overlaps with `String()` but makes sense for symmetry.
    
    ### What changes are included in this PR?
    
    Add `UnmarshalText` and `MarshalText` as thin wrappers around the same
    methods from the internal
    https://pkg.go.dev/github.com/apache/arrow-go/v18@v18.2.0/parquet/internal/gen-go/parquet#CompressionCodec
    type.
    
    ### Are these changes tested?
    
    Yes
    
    ### Are there any user-facing changes?
    
    Minor new API for `Compression` type
---
 parquet/compress/compress.go      |  8 ++++++++
 parquet/compress/compress_test.go | 41 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/parquet/compress/compress.go b/parquet/compress/compress.go
index 72768e0..dfa0745 100644
--- a/parquet/compress/compress.go
+++ b/parquet/compress/compress.go
@@ -33,6 +33,14 @@ func (c Compression) String() string {
 	return parquet.CompressionCodec(c).String()
 }
 
+func (c Compression) MarshalText() ([]byte, error) {
+	return parquet.CompressionCodec(c).MarshalText()
+}
+
+func (c *Compression) UnmarshalText(text []byte) error {
+	return (*parquet.CompressionCodec)(c).UnmarshalText(text)
+}
+
 // DefaultCompressionLevel will use flate.DefaultCompression since many of the compression libraries
 // use that to denote "use the default".
 const DefaultCompressionLevel = flate.DefaultCompression
diff --git a/parquet/compress/compress_test.go b/parquet/compress/compress_test.go
index e6c135d..00f1376 100644
--- a/parquet/compress/compress_test.go
+++ b/parquet/compress/compress_test.go
@@ -138,3 +138,44 @@ func TestCompressReaderWriter(t *testing.T) {
 		})
 	}
 }
+
+var marshalTests = []struct {
+	text  string
+	codec compress.Compression
+}{
+	{"UNCOMPRESSED", compress.Codecs.Uncompressed},
+	{"SNAPPY", compress.Codecs.Snappy},
+	{"GZIP", compress.Codecs.Gzip},
+	{"LZO", compress.Codecs.Lzo},
+	{"BROTLI", compress.Codecs.Brotli},
+	{"LZ4", compress.Codecs.Lz4},
+	{"ZSTD", compress.Codecs.Zstd},
+	{"LZ4_RAW", compress.Codecs.Lz4Raw},
+}
+
+func TestMarshalText(t *testing.T) {
+	for _, tt := range marshalTests {
+		t.Run(tt.text, func(t *testing.T) {
+			data, err := tt.codec.MarshalText()
+			assert.NoError(t, err)
+			assert.Equal(t, tt.text, string(data))
+		})
+	}
+}
+
+func TestUnmarshalText(t *testing.T) {
+	for _, tt := range marshalTests {
+		t.Run(tt.text, func(t *testing.T) {
+			var compression compress.Compression
+			err := compression.UnmarshalText([]byte(tt.text))
+			assert.NoError(t, err)
+			assert.Equal(t, tt.codec, compression)
+		})
+	}
+}
+
+func TestUnmarshalTextError(t *testing.T) {
+	var compression compress.Compression
+	err := compression.UnmarshalText([]byte("NO SUCH CODEC"))
+	assert.EqualError(t, err, "not a valid CompressionCodec string")
+}