aboutsummaryrefslogtreecommitdiff
path: root/weed/mq/kafka/schema/avro_decoder.go
blob: f40236a81812622ec4a0de6a4143d6fb625b4284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
package schema

import (
	"encoding/json"
	"fmt"
	"reflect"
	"time"

	"github.com/linkedin/goavro/v2"
	"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
)

// AvroDecoder handles Avro schema decoding and conversion to SeaweedMQ format
type AvroDecoder struct {
	codec *goavro.Codec
}

// NewAvroDecoder creates a new Avro decoder from a schema string
func NewAvroDecoder(schemaStr string) (*AvroDecoder, error) {
	codec, err := goavro.NewCodec(schemaStr)
	if err != nil {
		return nil, fmt.Errorf("failed to create Avro codec: %w", err)
	}

	return &AvroDecoder{
		codec: codec,
	}, nil
}

// Decode decodes Avro binary data to a Go map
func (ad *AvroDecoder) Decode(data []byte) (map[string]interface{}, error) {
	native, _, err := ad.codec.NativeFromBinary(data)
	if err != nil {
		return nil, fmt.Errorf("failed to decode Avro data: %w", err)
	}

	// Convert to map[string]interface{} for easier processing
	result, ok := native.(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("expected Avro record, got %T", native)
	}

	return result, nil
}

// DecodeToRecordValue decodes Avro data directly to SeaweedMQ RecordValue
func (ad *AvroDecoder) DecodeToRecordValue(data []byte) (*schema_pb.RecordValue, error) {
	nativeMap, err := ad.Decode(data)
	if err != nil {
		return nil, err
	}

	return MapToRecordValue(nativeMap), nil
}

// InferRecordType infers a SeaweedMQ RecordType from an Avro schema
func (ad *AvroDecoder) InferRecordType() (*schema_pb.RecordType, error) {
	schema := ad.codec.Schema()
	return avroSchemaToRecordType(schema)
}

// MapToRecordValue converts a Go map to SeaweedMQ RecordValue
func MapToRecordValue(m map[string]interface{}) *schema_pb.RecordValue {
	fields := make(map[string]*schema_pb.Value)

	for key, value := range m {
		fields[key] = goValueToSchemaValue(value)
	}

	return &schema_pb.RecordValue{
		Fields: fields,
	}
}

// goValueToSchemaValue converts a Go value to a SeaweedMQ Value
func goValueToSchemaValue(value interface{}) *schema_pb.Value {
	if value == nil {
		// For null values, use an empty string as default
		return &schema_pb.Value{
			Kind: &schema_pb.Value_StringValue{StringValue: ""},
		}
	}

	switch v := value.(type) {
	case bool:
		return &schema_pb.Value{
			Kind: &schema_pb.Value_BoolValue{BoolValue: v},
		}
	case int32:
		return &schema_pb.Value{
			Kind: &schema_pb.Value_Int32Value{Int32Value: v},
		}
	case int64:
		return &schema_pb.Value{
			Kind: &schema_pb.Value_Int64Value{Int64Value: v},
		}
	case int:
		return &schema_pb.Value{
			Kind: &schema_pb.Value_Int64Value{Int64Value: int64(v)},
		}
	case float32:
		return &schema_pb.Value{
			Kind: &schema_pb.Value_FloatValue{FloatValue: v},
		}
	case float64:
		return &schema_pb.Value{
			Kind: &schema_pb.Value_DoubleValue{DoubleValue: v},
		}
	case string:
		return &schema_pb.Value{
			Kind: &schema_pb.Value_StringValue{StringValue: v},
		}
	case []byte:
		return &schema_pb.Value{
			Kind: &schema_pb.Value_BytesValue{BytesValue: v},
		}
	case time.Time:
		return &schema_pb.Value{
			Kind: &schema_pb.Value_TimestampValue{
				TimestampValue: &schema_pb.TimestampValue{
					TimestampMicros: v.UnixMicro(),
					IsUtc:           true,
				},
			},
		}
	case []interface{}:
		// Handle arrays
		listValues := make([]*schema_pb.Value, len(v))
		for i, item := range v {
			listValues[i] = goValueToSchemaValue(item)
		}
		return &schema_pb.Value{
			Kind: &schema_pb.Value_ListValue{
				ListValue: &schema_pb.ListValue{
					Values: listValues,
				},
			},
		}
	case map[string]interface{}:
		// Check if this is an Avro union type (single key-value pair with type name as key)
		// Union types have keys that are typically Avro type names like "int", "string", etc.
		// Regular nested records would have meaningful field names like "inner", "name", etc.
		if len(v) == 1 {
			for unionType, unionValue := range v {
				// Handle common Avro union type patterns (only if key looks like a type name)
				switch unionType {
				case "int":
					if intVal, ok := unionValue.(int32); ok {
						// Store union as a record with the union type as field name
						// This preserves the union information for re-encoding
						return &schema_pb.Value{
							Kind: &schema_pb.Value_RecordValue{
								RecordValue: &schema_pb.RecordValue{
									Fields: map[string]*schema_pb.Value{
										"int": {
											Kind: &schema_pb.Value_Int32Value{Int32Value: intVal},
										},
									},
								},
							},
						}
					}
				case "long":
					if longVal, ok := unionValue.(int64); ok {
						return &schema_pb.Value{
							Kind: &schema_pb.Value_RecordValue{
								RecordValue: &schema_pb.RecordValue{
									Fields: map[string]*schema_pb.Value{
										"long": {
											Kind: &schema_pb.Value_Int64Value{Int64Value: longVal},
										},
									},
								},
							},
						}
					}
				case "float":
					if floatVal, ok := unionValue.(float32); ok {
						return &schema_pb.Value{
							Kind: &schema_pb.Value_RecordValue{
								RecordValue: &schema_pb.RecordValue{
									Fields: map[string]*schema_pb.Value{
										"float": {
											Kind: &schema_pb.Value_FloatValue{FloatValue: floatVal},
										},
									},
								},
							},
						}
					}
				case "double":
					if doubleVal, ok := unionValue.(float64); ok {
						return &schema_pb.Value{
							Kind: &schema_pb.Value_RecordValue{
								RecordValue: &schema_pb.RecordValue{
									Fields: map[string]*schema_pb.Value{
										"double": {
											Kind: &schema_pb.Value_DoubleValue{DoubleValue: doubleVal},
										},
									},
								},
							},
						}
					}
				case "string":
					if strVal, ok := unionValue.(string); ok {
						return &schema_pb.Value{
							Kind: &schema_pb.Value_RecordValue{
								RecordValue: &schema_pb.RecordValue{
									Fields: map[string]*schema_pb.Value{
										"string": {
											Kind: &schema_pb.Value_StringValue{StringValue: strVal},
										},
									},
								},
							},
						}
					}
				case "boolean":
					if boolVal, ok := unionValue.(bool); ok {
						return &schema_pb.Value{
							Kind: &schema_pb.Value_RecordValue{
								RecordValue: &schema_pb.RecordValue{
									Fields: map[string]*schema_pb.Value{
										"boolean": {
											Kind: &schema_pb.Value_BoolValue{BoolValue: boolVal},
										},
									},
								},
							},
						}
					}
				}
				// If it's not a recognized union type, fall through to treat as nested record
			}
		}

		// Handle nested records (both single-field and multi-field maps)
		fields := make(map[string]*schema_pb.Value)
		for key, val := range v {
			fields[key] = goValueToSchemaValue(val)
		}
		return &schema_pb.Value{
			Kind: &schema_pb.Value_RecordValue{
				RecordValue: &schema_pb.RecordValue{
					Fields: fields,
				},
			},
		}
	default:
		// Handle other types by converting to string
		return &schema_pb.Value{
			Kind: &schema_pb.Value_StringValue{
				StringValue: fmt.Sprintf("%v", v),
			},
		}
	}
}

// avroSchemaToRecordType converts an Avro schema to SeaweedMQ RecordType
func avroSchemaToRecordType(schemaStr string) (*schema_pb.RecordType, error) {
	// Validate the Avro schema by creating a codec (this ensures it's valid)
	_, err := goavro.NewCodec(schemaStr)
	if err != nil {
		return nil, fmt.Errorf("failed to parse Avro schema: %w", err)
	}

	// Parse the schema JSON to extract field definitions
	var avroSchema map[string]interface{}
	if err := json.Unmarshal([]byte(schemaStr), &avroSchema); err != nil {
		return nil, fmt.Errorf("failed to parse Avro schema JSON: %w", err)
	}

	// Extract fields from the Avro schema
	fields, err := extractAvroFields(avroSchema)
	if err != nil {
		return nil, fmt.Errorf("failed to extract Avro fields: %w", err)
	}

	return &schema_pb.RecordType{
		Fields: fields,
	}, nil
}

// extractAvroFields extracts field definitions from parsed Avro schema JSON
func extractAvroFields(avroSchema map[string]interface{}) ([]*schema_pb.Field, error) {
	// Check if this is a record type
	schemaType, ok := avroSchema["type"].(string)
	if !ok || schemaType != "record" {
		return nil, fmt.Errorf("expected record type, got %v", schemaType)
	}

	// Extract fields array
	fieldsInterface, ok := avroSchema["fields"]
	if !ok {
		return nil, fmt.Errorf("no fields found in Avro record schema")
	}

	fieldsArray, ok := fieldsInterface.([]interface{})
	if !ok {
		return nil, fmt.Errorf("fields must be an array")
	}

	// Convert each Avro field to SeaweedMQ field
	fields := make([]*schema_pb.Field, 0, len(fieldsArray))
	for i, fieldInterface := range fieldsArray {
		fieldMap, ok := fieldInterface.(map[string]interface{})
		if !ok {
			return nil, fmt.Errorf("field %d is not a valid object", i)
		}

		field, err := convertAvroFieldToSeaweedMQ(fieldMap, int32(i))
		if err != nil {
			return nil, fmt.Errorf("failed to convert field %d: %w", i, err)
		}

		fields = append(fields, field)
	}

	return fields, nil
}

// convertAvroFieldToSeaweedMQ converts a single Avro field to SeaweedMQ Field
func convertAvroFieldToSeaweedMQ(avroField map[string]interface{}, fieldIndex int32) (*schema_pb.Field, error) {
	// Extract field name
	name, ok := avroField["name"].(string)
	if !ok {
		return nil, fmt.Errorf("field name is required")
	}

	// Extract field type and check if it's an array
	fieldType, isRepeated, err := convertAvroTypeToSeaweedMQWithRepeated(avroField["type"])
	if err != nil {
		return nil, fmt.Errorf("failed to convert field type for %s: %w", name, err)
	}

	// Check if field has a default value (indicates it's optional)
	_, hasDefault := avroField["default"]
	isRequired := !hasDefault

	return &schema_pb.Field{
		Name:       name,
		FieldIndex: fieldIndex,
		Type:       fieldType,
		IsRequired: isRequired,
		IsRepeated: isRepeated,
	}, nil
}

// convertAvroTypeToSeaweedMQ converts Avro type to SeaweedMQ Type
func convertAvroTypeToSeaweedMQ(avroType interface{}) (*schema_pb.Type, error) {
	fieldType, _, err := convertAvroTypeToSeaweedMQWithRepeated(avroType)
	return fieldType, err
}

// convertAvroTypeToSeaweedMQWithRepeated converts Avro type to SeaweedMQ Type and returns if it's repeated
func convertAvroTypeToSeaweedMQWithRepeated(avroType interface{}) (*schema_pb.Type, bool, error) {
	switch t := avroType.(type) {
	case string:
		// Simple type
		fieldType, err := convertAvroSimpleType(t)
		return fieldType, false, err

	case map[string]interface{}:
		// Complex type (record, enum, array, map, fixed)
		return convertAvroComplexTypeWithRepeated(t)

	case []interface{}:
		// Union type
		fieldType, err := convertAvroUnionType(t)
		return fieldType, false, err

	default:
		return nil, false, fmt.Errorf("unsupported Avro type: %T", avroType)
	}
}

// convertAvroSimpleType converts simple Avro types to SeaweedMQ types
func convertAvroSimpleType(avroType string) (*schema_pb.Type, error) {
	switch avroType {
	case "null":
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_BYTES, // Use bytes for null
			},
		}, nil
	case "boolean":
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_BOOL,
			},
		}, nil
	case "int":
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_INT32,
			},
		}, nil
	case "long":
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_INT64,
			},
		}, nil
	case "float":
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_FLOAT,
			},
		}, nil
	case "double":
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_DOUBLE,
			},
		}, nil
	case "bytes":
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_BYTES,
			},
		}, nil
	case "string":
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_STRING,
			},
		}, nil
	default:
		return nil, fmt.Errorf("unsupported simple Avro type: %s", avroType)
	}
}

// convertAvroComplexType converts complex Avro types to SeaweedMQ types
func convertAvroComplexType(avroType map[string]interface{}) (*schema_pb.Type, error) {
	fieldType, _, err := convertAvroComplexTypeWithRepeated(avroType)
	return fieldType, err
}

// convertAvroComplexTypeWithRepeated converts complex Avro types to SeaweedMQ types and returns if it's repeated
func convertAvroComplexTypeWithRepeated(avroType map[string]interface{}) (*schema_pb.Type, bool, error) {
	typeStr, ok := avroType["type"].(string)
	if !ok {
		return nil, false, fmt.Errorf("complex type must have a type field")
	}

	// Handle logical types - they are based on underlying primitive types
	if _, hasLogicalType := avroType["logicalType"]; hasLogicalType {
		// For logical types, use the underlying primitive type
		return convertAvroSimpleTypeWithLogical(typeStr, avroType)
	}

	switch typeStr {
	case "record":
		// Nested record type
		fields, err := extractAvroFields(avroType)
		if err != nil {
			return nil, false, fmt.Errorf("failed to extract nested record fields: %w", err)
		}
		return &schema_pb.Type{
			Kind: &schema_pb.Type_RecordType{
				RecordType: &schema_pb.RecordType{
					Fields: fields,
				},
			},
		}, false, nil

	case "enum":
		// Enum type - treat as string for now
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_STRING,
			},
		}, false, nil

	case "array":
		// Array type
		itemsType, err := convertAvroTypeToSeaweedMQ(avroType["items"])
		if err != nil {
			return nil, false, fmt.Errorf("failed to convert array items type: %w", err)
		}
		// For arrays, we return the item type and set IsRepeated=true
		return itemsType, true, nil

	case "map":
		// Map type - treat as record with dynamic fields
		return &schema_pb.Type{
			Kind: &schema_pb.Type_RecordType{
				RecordType: &schema_pb.RecordType{
					Fields: []*schema_pb.Field{}, // Dynamic fields
				},
			},
		}, false, nil

	case "fixed":
		// Fixed-length bytes
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_BYTES,
			},
		}, false, nil

	default:
		return nil, false, fmt.Errorf("unsupported complex Avro type: %s", typeStr)
	}
}

// convertAvroSimpleTypeWithLogical handles logical types based on their underlying primitive types
func convertAvroSimpleTypeWithLogical(primitiveType string, avroType map[string]interface{}) (*schema_pb.Type, bool, error) {
	logicalType, _ := avroType["logicalType"].(string)

	// Map logical types to appropriate SeaweedMQ types
	switch logicalType {
	case "decimal":
		// Decimal logical type - use bytes for precision
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_BYTES,
			},
		}, false, nil
	case "uuid":
		// UUID logical type - use string
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_STRING,
			},
		}, false, nil
	case "date":
		// Date logical type (int) - use int32
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_INT32,
			},
		}, false, nil
	case "time-millis":
		// Time in milliseconds (int) - use int32
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_INT32,
			},
		}, false, nil
	case "time-micros":
		// Time in microseconds (long) - use int64
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_INT64,
			},
		}, false, nil
	case "timestamp-millis":
		// Timestamp in milliseconds (long) - use int64
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_INT64,
			},
		}, false, nil
	case "timestamp-micros":
		// Timestamp in microseconds (long) - use int64
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_INT64,
			},
		}, false, nil
	default:
		// For unknown logical types, fall back to the underlying primitive type
		fieldType, err := convertAvroSimpleType(primitiveType)
		return fieldType, false, err
	}
}

// convertAvroUnionType converts Avro union types to SeaweedMQ types
func convertAvroUnionType(unionTypes []interface{}) (*schema_pb.Type, error) {
	// For unions, we'll use the first non-null type
	// This is a simplification - in a full implementation, we might want to create a union type
	for _, unionType := range unionTypes {
		if typeStr, ok := unionType.(string); ok && typeStr == "null" {
			continue // Skip null types
		}

		// Use the first non-null type
		return convertAvroTypeToSeaweedMQ(unionType)
	}

	// If all types are null, return bytes type
	return &schema_pb.Type{
		Kind: &schema_pb.Type_ScalarType{
			ScalarType: schema_pb.ScalarType_BYTES,
		},
	}, nil
}

// InferRecordTypeFromMap infers a RecordType from a decoded map
// This is useful when we don't have the original Avro schema
func InferRecordTypeFromMap(m map[string]interface{}) *schema_pb.RecordType {
	fields := make([]*schema_pb.Field, 0, len(m))
	fieldIndex := int32(0)

	for key, value := range m {
		fieldType := inferTypeFromValue(value)

		field := &schema_pb.Field{
			Name:       key,
			FieldIndex: fieldIndex,
			Type:       fieldType,
			IsRequired: value != nil, // Non-nil values are considered required
			IsRepeated: false,
		}

		// Check if it's an array
		if reflect.TypeOf(value).Kind() == reflect.Slice {
			field.IsRepeated = true
		}

		fields = append(fields, field)
		fieldIndex++
	}

	return &schema_pb.RecordType{
		Fields: fields,
	}
}

// inferTypeFromValue infers a SeaweedMQ Type from a Go value
func inferTypeFromValue(value interface{}) *schema_pb.Type {
	if value == nil {
		// Default to string for null values
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_STRING,
			},
		}
	}

	switch v := value.(type) {
	case bool:
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_BOOL,
			},
		}
	case int32:
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_INT32,
			},
		}
	case int64, int:
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_INT64,
			},
		}
	case float32:
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_FLOAT,
			},
		}
	case float64:
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_DOUBLE,
			},
		}
	case string:
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_STRING,
			},
		}
	case []byte:
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_BYTES,
			},
		}
	case time.Time:
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_TIMESTAMP,
			},
		}
	case []interface{}:
		// Handle arrays - infer element type from first element
		var elementType *schema_pb.Type
		if len(v) > 0 {
			elementType = inferTypeFromValue(v[0])
		} else {
			// Default to string for empty arrays
			elementType = &schema_pb.Type{
				Kind: &schema_pb.Type_ScalarType{
					ScalarType: schema_pb.ScalarType_STRING,
				},
			}
		}

		return &schema_pb.Type{
			Kind: &schema_pb.Type_ListType{
				ListType: &schema_pb.ListType{
					ElementType: elementType,
				},
			},
		}
	case map[string]interface{}:
		// Handle nested records
		nestedRecordType := InferRecordTypeFromMap(v)
		return &schema_pb.Type{
			Kind: &schema_pb.Type_RecordType{
				RecordType: nestedRecordType,
			},
		}
	default:
		// Default to string for unknown types
		return &schema_pb.Type{
			Kind: &schema_pb.Type_ScalarType{
				ScalarType: schema_pb.ScalarType_STRING,
			},
		}
	}
}