2024-04-22 15:42:18 +08:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/parquet-go/parquet-go"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
|
|
|
|
)
|
|
|
|
|
2024-04-25 14:28:20 +08:00
|
|
|
// ToRecordValue converts a parquet.Row to a schema_pb.RecordValue
|
|
|
|
// This does not work or did not test with nested structures.
|
|
|
|
// Using this may fail to convert the parquet.Row to schema_pb.RecordValue
|
2024-04-26 14:59:30 +08:00
|
|
|
func ToRecordValue(recordType *schema_pb.RecordType, parquetLevels *ParquetLevels, row parquet.Row) (*schema_pb.RecordValue, error) {
|
2024-04-22 15:42:18 +08:00
|
|
|
values := []parquet.Value(row)
|
2024-04-26 12:29:45 +08:00
|
|
|
recordValue, _, err := toRecordValue(recordType, parquetLevels, values, 0)
|
2024-04-22 15:42:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return recordValue.GetRecordValue(), nil
|
|
|
|
}
|
|
|
|
|
2024-04-26 12:29:45 +08:00
|
|
|
func ToValue(t *schema_pb.Type, levels *ParquetLevels, values []parquet.Value, valueIndex int) (value *schema_pb.Value, endValueIndex int, err error) {
|
2024-04-22 15:42:18 +08:00
|
|
|
switch t.Kind.(type) {
|
|
|
|
case *schema_pb.Type_ScalarType:
|
2024-04-26 12:29:45 +08:00
|
|
|
return toScalarValue(t.GetScalarType(), levels, values, valueIndex)
|
2024-04-22 15:42:18 +08:00
|
|
|
case *schema_pb.Type_ListType:
|
2024-04-26 12:29:45 +08:00
|
|
|
return toListValue(t.GetListType(), levels, values, valueIndex)
|
2024-04-22 15:42:18 +08:00
|
|
|
case *schema_pb.Type_RecordType:
|
2024-04-26 12:29:45 +08:00
|
|
|
return toRecordValue(t.GetRecordType(), levels, values, valueIndex)
|
2024-04-22 15:42:18 +08:00
|
|
|
}
|
2024-04-26 12:29:45 +08:00
|
|
|
return nil, valueIndex, fmt.Errorf("unsupported type: %v", t)
|
2024-04-22 15:42:18 +08:00
|
|
|
}
|
|
|
|
|
2024-04-26 12:29:45 +08:00
|
|
|
func toRecordValue(recordType *schema_pb.RecordType, levels *ParquetLevels, values []parquet.Value, valueIndex int) (*schema_pb.Value, int, error) {
|
2024-04-22 15:42:18 +08:00
|
|
|
recordValue := schema_pb.RecordValue{Fields: make(map[string]*schema_pb.Value)}
|
|
|
|
for _, field := range recordType.Fields {
|
2024-04-26 12:29:45 +08:00
|
|
|
fieldLevels := levels.levels[field.Name]
|
|
|
|
fieldValue, endValueIndex, err := ToValue(field.Type, fieldLevels, values, valueIndex)
|
2024-04-22 15:42:18 +08:00
|
|
|
if err != nil {
|
2024-04-26 12:29:45 +08:00
|
|
|
return nil, 0, err
|
2024-04-22 15:42:18 +08:00
|
|
|
}
|
2024-04-25 14:04:47 +08:00
|
|
|
valueIndex = endValueIndex
|
2024-04-22 15:42:18 +08:00
|
|
|
recordValue.Fields[field.Name] = fieldValue
|
|
|
|
}
|
2024-04-26 12:29:45 +08:00
|
|
|
return &schema_pb.Value{Kind: &schema_pb.Value_RecordValue{RecordValue: &recordValue}}, valueIndex, nil
|
2024-04-22 15:42:18 +08:00
|
|
|
}
|
|
|
|
|
2024-04-26 12:29:45 +08:00
|
|
|
func toListValue(listType *schema_pb.ListType, levels *ParquetLevels, values []parquet.Value, valueIndex int) (listValue *schema_pb.Value, endValueIndex int, err error) {
|
2024-04-22 15:42:18 +08:00
|
|
|
listValues := make([]*schema_pb.Value, 0)
|
|
|
|
var value *schema_pb.Value
|
2024-05-21 02:03:56 +08:00
|
|
|
for valueIndex < len(values) {
|
2024-04-26 12:29:45 +08:00
|
|
|
if values[valueIndex].Column() != levels.startColumnIndex {
|
2024-04-22 15:42:18 +08:00
|
|
|
break
|
|
|
|
}
|
2024-04-26 12:29:45 +08:00
|
|
|
value, valueIndex, err = ToValue(listType.ElementType, levels, values, valueIndex)
|
2024-04-25 14:04:47 +08:00
|
|
|
if err != nil {
|
2024-04-26 12:29:45 +08:00
|
|
|
return nil, valueIndex, err
|
2024-04-25 14:04:47 +08:00
|
|
|
}
|
2024-04-22 15:42:18 +08:00
|
|
|
listValues = append(listValues, value)
|
|
|
|
}
|
2024-04-26 12:29:45 +08:00
|
|
|
return &schema_pb.Value{Kind: &schema_pb.Value_ListValue{ListValue: &schema_pb.ListValue{Values: listValues}}}, valueIndex, nil
|
2024-04-22 15:42:18 +08:00
|
|
|
}
|
|
|
|
|
2024-04-26 12:29:45 +08:00
|
|
|
func toScalarValue(scalarType schema_pb.ScalarType, levels *ParquetLevels, values []parquet.Value, valueIndex int) (*schema_pb.Value, int, error) {
|
2024-04-25 14:04:47 +08:00
|
|
|
value := values[valueIndex]
|
2024-04-26 12:29:45 +08:00
|
|
|
if value.Column() != levels.startColumnIndex {
|
|
|
|
return nil, valueIndex, nil
|
2024-04-22 15:42:18 +08:00
|
|
|
}
|
|
|
|
switch scalarType {
|
2024-05-02 23:59:22 +08:00
|
|
|
case schema_pb.ScalarType_BOOL:
|
2024-05-21 02:03:56 +08:00
|
|
|
return &schema_pb.Value{Kind: &schema_pb.Value_BoolValue{BoolValue: value.Boolean()}}, valueIndex + 1, nil
|
2024-05-02 23:59:22 +08:00
|
|
|
case schema_pb.ScalarType_INT32:
|
2024-05-21 02:03:56 +08:00
|
|
|
return &schema_pb.Value{Kind: &schema_pb.Value_Int32Value{Int32Value: value.Int32()}}, valueIndex + 1, nil
|
2024-05-02 23:59:22 +08:00
|
|
|
case schema_pb.ScalarType_INT64:
|
2024-05-21 02:03:56 +08:00
|
|
|
return &schema_pb.Value{Kind: &schema_pb.Value_Int64Value{Int64Value: value.Int64()}}, valueIndex + 1, nil
|
2024-05-03 02:14:58 +08:00
|
|
|
case schema_pb.ScalarType_FLOAT:
|
2024-05-21 02:03:56 +08:00
|
|
|
return &schema_pb.Value{Kind: &schema_pb.Value_FloatValue{FloatValue: value.Float()}}, valueIndex + 1, nil
|
2024-05-03 02:14:58 +08:00
|
|
|
case schema_pb.ScalarType_DOUBLE:
|
2024-05-21 02:03:56 +08:00
|
|
|
return &schema_pb.Value{Kind: &schema_pb.Value_DoubleValue{DoubleValue: value.Double()}}, valueIndex + 1, nil
|
2024-04-22 15:42:18 +08:00
|
|
|
case schema_pb.ScalarType_BYTES:
|
2024-05-21 02:03:56 +08:00
|
|
|
return &schema_pb.Value{Kind: &schema_pb.Value_BytesValue{BytesValue: value.ByteArray()}}, valueIndex + 1, nil
|
2024-04-22 15:42:18 +08:00
|
|
|
case schema_pb.ScalarType_STRING:
|
2024-05-21 02:03:56 +08:00
|
|
|
return &schema_pb.Value{Kind: &schema_pb.Value_StringValue{StringValue: string(value.ByteArray())}}, valueIndex + 1, nil
|
2024-04-22 15:42:18 +08:00
|
|
|
}
|
2024-04-26 12:29:45 +08:00
|
|
|
return nil, valueIndex, fmt.Errorf("unsupported scalar type: %v", scalarType)
|
2024-04-22 15:42:18 +08:00
|
|
|
}
|