Files
upstream-seaweedfs/weed/mq/schema/to_parquet_value_test.go
upstream-mirror 89f5be005d
Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled
snapshot of https://github.com/seaweedfs/seaweedfs.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 03:23:42 +05:00

667 lines
16 KiB
Go

package schema
import (
"math/big"
"testing"
"time"
"github.com/parquet-go/parquet-go"
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
)
func TestToParquetValue_BasicTypes(t *testing.T) {
tests := []struct {
name string
value *schema_pb.Value
expected parquet.Value
wantErr bool
}{
{
name: "BoolValue true",
value: &schema_pb.Value{
Kind: &schema_pb.Value_BoolValue{BoolValue: true},
},
expected: parquet.BooleanValue(true),
},
{
name: "Int32Value",
value: &schema_pb.Value{
Kind: &schema_pb.Value_Int32Value{Int32Value: 42},
},
expected: parquet.Int32Value(42),
},
{
name: "Int64Value",
value: &schema_pb.Value{
Kind: &schema_pb.Value_Int64Value{Int64Value: 12345678901234},
},
expected: parquet.Int64Value(12345678901234),
},
{
name: "FloatValue",
value: &schema_pb.Value{
Kind: &schema_pb.Value_FloatValue{FloatValue: 3.14159},
},
expected: parquet.FloatValue(3.14159),
},
{
name: "DoubleValue",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DoubleValue{DoubleValue: 2.718281828},
},
expected: parquet.DoubleValue(2.718281828),
},
{
name: "BytesValue",
value: &schema_pb.Value{
Kind: &schema_pb.Value_BytesValue{BytesValue: []byte("hello world")},
},
expected: parquet.ByteArrayValue([]byte("hello world")),
},
{
name: "BytesValue empty",
value: &schema_pb.Value{
Kind: &schema_pb.Value_BytesValue{BytesValue: []byte{}},
},
expected: parquet.ByteArrayValue([]byte{}),
},
{
name: "StringValue",
value: &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: "test string"},
},
expected: parquet.ByteArrayValue([]byte("test string")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := toParquetValue(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("toParquetValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !parquetValuesEqual(result, tt.expected) {
t.Errorf("toParquetValue() = %v, want %v", result, tt.expected)
}
})
}
}
func TestToParquetValue_TimestampValue(t *testing.T) {
tests := []struct {
name string
value *schema_pb.Value
expected parquet.Value
wantErr bool
}{
{
name: "Valid TimestampValue UTC",
value: &schema_pb.Value{
Kind: &schema_pb.Value_TimestampValue{
TimestampValue: &schema_pb.TimestampValue{
TimestampMicros: 1704067200000000, // 2024-01-01 00:00:00 UTC in microseconds
IsUtc: true,
},
},
},
expected: parquet.Int64Value(1704067200000000),
},
{
name: "Valid TimestampValue local",
value: &schema_pb.Value{
Kind: &schema_pb.Value_TimestampValue{
TimestampValue: &schema_pb.TimestampValue{
TimestampMicros: 1704067200000000,
IsUtc: false,
},
},
},
expected: parquet.Int64Value(1704067200000000),
},
{
name: "TimestampValue zero",
value: &schema_pb.Value{
Kind: &schema_pb.Value_TimestampValue{
TimestampValue: &schema_pb.TimestampValue{
TimestampMicros: 0,
IsUtc: true,
},
},
},
expected: parquet.Int64Value(0),
},
{
name: "TimestampValue negative (before epoch)",
value: &schema_pb.Value{
Kind: &schema_pb.Value_TimestampValue{
TimestampValue: &schema_pb.TimestampValue{
TimestampMicros: -1000000, // 1 second before epoch
IsUtc: true,
},
},
},
expected: parquet.Int64Value(-1000000),
},
{
name: "TimestampValue nil pointer",
value: &schema_pb.Value{
Kind: &schema_pb.Value_TimestampValue{
TimestampValue: nil,
},
},
expected: parquet.NullValue(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := toParquetValue(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("toParquetValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !parquetValuesEqual(result, tt.expected) {
t.Errorf("toParquetValue() = %v, want %v", result, tt.expected)
}
})
}
}
func TestToParquetValue_DateValue(t *testing.T) {
tests := []struct {
name string
value *schema_pb.Value
expected parquet.Value
wantErr bool
}{
{
name: "Valid DateValue (2024-01-01)",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DateValue{
DateValue: &schema_pb.DateValue{
DaysSinceEpoch: 19723, // 2024-01-01 = 19723 days since epoch
},
},
},
expected: parquet.Int32Value(19723),
},
{
name: "DateValue epoch (1970-01-01)",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DateValue{
DateValue: &schema_pb.DateValue{
DaysSinceEpoch: 0,
},
},
},
expected: parquet.Int32Value(0),
},
{
name: "DateValue before epoch",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DateValue{
DateValue: &schema_pb.DateValue{
DaysSinceEpoch: -365, // 1969-01-01
},
},
},
expected: parquet.Int32Value(-365),
},
{
name: "DateValue nil pointer",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DateValue{
DateValue: nil,
},
},
expected: parquet.NullValue(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := toParquetValue(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("toParquetValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !parquetValuesEqual(result, tt.expected) {
t.Errorf("toParquetValue() = %v, want %v", result, tt.expected)
}
})
}
}
func TestToParquetValue_DecimalValue(t *testing.T) {
tests := []struct {
name string
value *schema_pb.Value
expected parquet.Value
wantErr bool
}{
{
name: "Small Decimal (precision <= 9) - positive",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: &schema_pb.DecimalValue{
Value: encodeBigIntToBytes(big.NewInt(12345)), // 123.45 with scale 2
Precision: 5,
Scale: 2,
},
},
},
expected: createFixedLenByteArray(encodeBigIntToBytes(big.NewInt(12345))), // FixedLenByteArray conversion
},
{
name: "Small Decimal (precision <= 9) - negative",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: &schema_pb.DecimalValue{
Value: encodeBigIntToBytes(big.NewInt(-12345)),
Precision: 5,
Scale: 2,
},
},
},
expected: createFixedLenByteArray(encodeBigIntToBytes(big.NewInt(-12345))), // FixedLenByteArray conversion
},
{
name: "Medium Decimal (9 < precision <= 18)",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: &schema_pb.DecimalValue{
Value: encodeBigIntToBytes(big.NewInt(123456789012345)),
Precision: 15,
Scale: 2,
},
},
},
expected: createFixedLenByteArray(encodeBigIntToBytes(big.NewInt(123456789012345))), // FixedLenByteArray conversion
},
{
name: "Large Decimal (precision > 18)",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: &schema_pb.DecimalValue{
Value: []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, // Large number as bytes
Precision: 25,
Scale: 5,
},
},
},
expected: createFixedLenByteArray([]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}), // FixedLenByteArray conversion
},
{
name: "Decimal with zero precision",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: &schema_pb.DecimalValue{
Value: encodeBigIntToBytes(big.NewInt(0)),
Precision: 0,
Scale: 0,
},
},
},
expected: createFixedLenByteArray(encodeBigIntToBytes(big.NewInt(0))), // Zero as FixedLenByteArray
},
{
name: "Decimal nil pointer",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: nil,
},
},
expected: parquet.NullValue(),
},
{
name: "Decimal with nil Value bytes",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: &schema_pb.DecimalValue{
Value: nil, // This was the original panic cause
Precision: 5,
Scale: 2,
},
},
},
expected: parquet.NullValue(),
},
{
name: "Decimal with empty Value bytes",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: &schema_pb.DecimalValue{
Value: []byte{}, // Empty slice
Precision: 5,
Scale: 2,
},
},
},
expected: parquet.NullValue(), // Returns null for empty bytes
},
{
name: "Decimal out of int32 range (stored as binary)",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: &schema_pb.DecimalValue{
Value: encodeBigIntToBytes(big.NewInt(999999999999)), // Too large for int32
Precision: 5, // But precision says int32
Scale: 0,
},
},
},
expected: createFixedLenByteArray(encodeBigIntToBytes(big.NewInt(999999999999))), // FixedLenByteArray
},
{
name: "Decimal out of int64 range (stored as binary)",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: &schema_pb.DecimalValue{
Value: func() []byte {
// Create a number larger than int64 max
bigNum := new(big.Int)
bigNum.SetString("99999999999999999999999999999", 10)
return encodeBigIntToBytes(bigNum)
}(),
Precision: 15, // Says int64 but value is too large
Scale: 0,
},
},
},
expected: createFixedLenByteArray(func() []byte {
bigNum := new(big.Int)
bigNum.SetString("99999999999999999999999999999", 10)
return encodeBigIntToBytes(bigNum)
}()), // Large number as FixedLenByteArray (truncated to 16 bytes)
},
{
name: "Decimal extremely large value (should be rejected)",
value: &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: &schema_pb.DecimalValue{
Value: make([]byte, 100), // 100 bytes > 64 byte limit
Precision: 100,
Scale: 0,
},
},
},
expected: parquet.NullValue(),
wantErr: true, // Should return error instead of corrupting data
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := toParquetValue(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("toParquetValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !parquetValuesEqual(result, tt.expected) {
t.Errorf("toParquetValue() = %v, want %v", result, tt.expected)
}
})
}
}
func TestToParquetValue_TimeValue(t *testing.T) {
tests := []struct {
name string
value *schema_pb.Value
expected parquet.Value
wantErr bool
}{
{
name: "Valid TimeValue (12:34:56.789)",
value: &schema_pb.Value{
Kind: &schema_pb.Value_TimeValue{
TimeValue: &schema_pb.TimeValue{
TimeMicros: 45296789000, // 12:34:56.789 in microseconds since midnight
},
},
},
expected: parquet.Int64Value(45296789000),
},
{
name: "TimeValue midnight",
value: &schema_pb.Value{
Kind: &schema_pb.Value_TimeValue{
TimeValue: &schema_pb.TimeValue{
TimeMicros: 0,
},
},
},
expected: parquet.Int64Value(0),
},
{
name: "TimeValue end of day (23:59:59.999999)",
value: &schema_pb.Value{
Kind: &schema_pb.Value_TimeValue{
TimeValue: &schema_pb.TimeValue{
TimeMicros: 86399999999, // 23:59:59.999999
},
},
},
expected: parquet.Int64Value(86399999999),
},
{
name: "TimeValue nil pointer",
value: &schema_pb.Value{
Kind: &schema_pb.Value_TimeValue{
TimeValue: nil,
},
},
expected: parquet.NullValue(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := toParquetValue(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("toParquetValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !parquetValuesEqual(result, tt.expected) {
t.Errorf("toParquetValue() = %v, want %v", result, tt.expected)
}
})
}
}
func TestToParquetValue_EdgeCases(t *testing.T) {
tests := []struct {
name string
value *schema_pb.Value
expected parquet.Value
wantErr bool
}{
{
name: "Nil value",
value: &schema_pb.Value{
Kind: nil,
},
wantErr: true,
},
{
name: "Completely nil value",
value: nil,
wantErr: true,
},
{
name: "BytesValue with nil slice",
value: &schema_pb.Value{
Kind: &schema_pb.Value_BytesValue{BytesValue: nil},
},
expected: parquet.ByteArrayValue([]byte{}), // Should convert nil to empty slice
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := toParquetValue(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("toParquetValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !parquetValuesEqual(result, tt.expected) {
t.Errorf("toParquetValue() = %v, want %v", result, tt.expected)
}
})
}
}
// Helper function to encode a big.Int to bytes using two's complement representation
func encodeBigIntToBytes(n *big.Int) []byte {
if n.Sign() == 0 {
return []byte{0}
}
// For positive numbers, just use Bytes()
if n.Sign() > 0 {
return n.Bytes()
}
// For negative numbers, we need two's complement representation
bitLen := n.BitLen()
if bitLen%8 != 0 {
bitLen += 8 - (bitLen % 8) // Round up to byte boundary
}
byteLen := bitLen / 8
if byteLen == 0 {
byteLen = 1
}
// Calculate 2^(byteLen*8)
modulus := new(big.Int).Lsh(big.NewInt(1), uint(byteLen*8))
// Convert negative to positive representation: n + 2^(byteLen*8)
positive := new(big.Int).Add(n, modulus)
bytes := positive.Bytes()
// Pad with leading zeros if needed
if len(bytes) < byteLen {
padded := make([]byte, byteLen)
copy(padded[byteLen-len(bytes):], bytes)
return padded
}
return bytes
}
// Helper function to create a FixedLenByteArray(16) matching our conversion logic
func createFixedLenByteArray(inputBytes []byte) parquet.Value {
fixedBytes := make([]byte, 16)
if len(inputBytes) <= 16 {
// Right-align the value (big-endian) - same as our conversion logic
copy(fixedBytes[16-len(inputBytes):], inputBytes)
} else {
// Truncate if too large, taking the least significant bytes
copy(fixedBytes, inputBytes[len(inputBytes)-16:])
}
return parquet.FixedLenByteArrayValue(fixedBytes)
}
// Helper function to compare parquet values
func parquetValuesEqual(a, b parquet.Value) bool {
// Handle both being null
if a.IsNull() && b.IsNull() {
return true
}
if a.IsNull() != b.IsNull() {
return false
}
// Compare kind first
if a.Kind() != b.Kind() {
return false
}
// Compare based on type
switch a.Kind() {
case parquet.Boolean:
return a.Boolean() == b.Boolean()
case parquet.Int32:
return a.Int32() == b.Int32()
case parquet.Int64:
return a.Int64() == b.Int64()
case parquet.Float:
return a.Float() == b.Float()
case parquet.Double:
return a.Double() == b.Double()
case parquet.ByteArray:
aBytes := a.ByteArray()
bBytes := b.ByteArray()
if len(aBytes) != len(bBytes) {
return false
}
for i, v := range aBytes {
if v != bBytes[i] {
return false
}
}
return true
case parquet.FixedLenByteArray:
aBytes := a.ByteArray() // FixedLenByteArray also uses ByteArray() method
bBytes := b.ByteArray()
if len(aBytes) != len(bBytes) {
return false
}
for i, v := range aBytes {
if v != bBytes[i] {
return false
}
}
return true
default:
return false
}
}
// Benchmark tests
func BenchmarkToParquetValue_BasicTypes(b *testing.B) {
value := &schema_pb.Value{
Kind: &schema_pb.Value_Int64Value{Int64Value: 12345678901234},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = toParquetValue(value)
}
}
func BenchmarkToParquetValue_TimestampValue(b *testing.B) {
value := &schema_pb.Value{
Kind: &schema_pb.Value_TimestampValue{
TimestampValue: &schema_pb.TimestampValue{
TimestampMicros: time.Now().UnixMicro(),
IsUtc: true,
},
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = toParquetValue(value)
}
}
func BenchmarkToParquetValue_DecimalValue(b *testing.B) {
value := &schema_pb.Value{
Kind: &schema_pb.Value_DecimalValue{
DecimalValue: &schema_pb.DecimalValue{
Value: encodeBigIntToBytes(big.NewInt(123456789012345)),
Precision: 15,
Scale: 2,
},
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = toParquetValue(value)
}
}