Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 62 additions & 47 deletions opengin/core-api/pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ func (sg *SchemaGenerator) GenerateSchema(anyValue *anypb.Any) (*SchemaInfo, err
StorageType: storageinference.ScalarData,
TypeInfo: &typeinference.TypeInfo{},
}
if m == nil || m.GetKind() == nil {
schema.TypeInfo.Type = typeinference.NullType
schema.TypeInfo.IsNullable = true
return schema, nil
}
switch m.GetKind().(type) {
case *structpb.Value_StringValue:
schema.TypeInfo.Type = typeinference.StringType
Expand Down Expand Up @@ -340,6 +345,11 @@ func (sg *SchemaGenerator) GenerateSchema(anyValue *anypb.Any) (*SchemaInfo, err
StorageType: storageinference.ScalarData,
TypeInfo: &typeinference.TypeInfo{},
}
if value == nil || value.GetKind() == nil {
schema.TypeInfo.Type = typeinference.NullType
schema.TypeInfo.IsNullable = true
return schema, nil
}
switch value.GetKind().(type) {
case *structpb.Value_StringValue:
schema.TypeInfo.Type = typeinference.StringType
Expand Down Expand Up @@ -513,62 +523,62 @@ func inferColumnTypes(columnsList, rowsList *structpb.ListValue) (map[string]typ

// Scan rows until each column has a type from its first non-null value (or all-null → NullType below).
nPending := len(columnTypes)
rowsLoop:
for rowIndex, row := range rowsList.Values {
rowData := row.GetListValue()
if len(rowData.Values) != len(columnsList.Values) {
return nil, fmt.Errorf("row %d length (%d) does not match number of columns (%d)", rowIndex, len(rowData.Values), len(columnsList.Values))
rowsLoop:
for rowIndex, row := range rowsList.Values {
rowData := row.GetListValue()
if len(rowData.Values) != len(columnsList.Values) {
return nil, fmt.Errorf("row %d length (%d) does not match number of columns (%d)", rowIndex, len(rowData.Values), len(columnsList.Values))
}
for i, value := range rowData.Values {
colName := columnsList.Values[i].GetStringValue()
if columnTypes[colName].Type != typeinference.DataType(unknown) {
continue
}
for i, value := range rowData.Values {
colName := columnsList.Values[i].GetStringValue()
if columnTypes[colName].Type != typeinference.DataType(unknown) {
continue
}

if structpbScalarIsNull(value) {
continue
}
if structpbScalarIsNull(value) {
continue
}

matched := false
switch value.GetKind().(type) {
case *structpb.Value_NumberValue:
columnTypes[colName] = typeinference.TypeInfo{
Type: typeinference.NumericType,
IsNullable: true,
}
matched = true
case *structpb.Value_BoolValue:
columnTypes[colName] = typeinference.TypeInfo{
Type: typeinference.BoolType,
IsNullable: true,
}
matched = true
case *structpb.Value_StringValue:
str := value.GetStringValue()
var inferredType typeinference.DataType
if isDate, isDateTime := isDateOrDateTime(str); isDate {
if isDateTime {
inferredType = typeinference.DateTimeType
} else {
inferredType = typeinference.DateType
}
matched := false
switch value.GetKind().(type) {
case *structpb.Value_NumberValue:
columnTypes[colName] = typeinference.TypeInfo{
Type: typeinference.NumericType,
IsNullable: true,
}
matched = true
case *structpb.Value_BoolValue:
columnTypes[colName] = typeinference.TypeInfo{
Type: typeinference.BoolType,
IsNullable: true,
}
matched = true
case *structpb.Value_StringValue:
str := value.GetStringValue()
var inferredType typeinference.DataType
if isDate, isDateTime := isDateOrDateTime(str); isDate {
if isDateTime {
inferredType = typeinference.DateTimeType
} else {
inferredType = typeinference.StringType
inferredType = typeinference.DateType
}
columnTypes[colName] = typeinference.TypeInfo{
Type: inferredType,
IsNullable: true,
}
matched = true
} else {
inferredType = typeinference.StringType
}
if matched {
nPending--
if nPending == 0 {
break rowsLoop
}
columnTypes[colName] = typeinference.TypeInfo{
Type: inferredType,
IsNullable: true,
}
matched = true
}
if matched {
nPending--
if nPending == 0 {
break rowsLoop
}
}
}
}

// Any column still unknown = all nulls
for colName, info := range columnTypes {
Expand Down Expand Up @@ -2015,6 +2025,11 @@ func (sg *SchemaGenerator) handleScalarData(structValue *structpb.Struct, schema
// Find the first scalar field
var scalarField *structpb.Value
for _, field := range structValue.Fields {
if field == nil || field.GetKind() == nil || structpbScalarIsNull(field) {
scalarField = field
break
}

switch field.GetKind().(type) {
case *structpb.Value_StringValue, *structpb.Value_NumberValue, *structpb.Value_BoolValue, *structpb.Value_NullValue:
scalarField = field
Expand Down
19 changes: 19 additions & 0 deletions opengin/core-api/pkg/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/structpb"

"lk/datafoundation/core-api/pkg/storageinference"
"lk/datafoundation/core-api/pkg/typeinference"
)

Expand Down Expand Up @@ -773,3 +775,20 @@ func TestInferColumnTypesUnsetKindActsAsNull(t *testing.T) {
assert.Equal(t, typeinference.NumericType, colTypes["x"].Type)
assert.True(t, colTypes["x"].IsNullable)
}

func TestGenerateSchemaScalarNullWithUnsetKind(t *testing.T) {
structValue := &structpb.Struct{Fields: map[string]*structpb.Value{
"value": {
Kind: nil,
},
}}
anyValue, err := anypb.New(structValue)
assert.NoError(t, err)

generator := NewSchemaGenerator()
schema, err := generator.GenerateSchema(anyValue)
assert.NoError(t, err)
assert.Equal(t, storageinference.ScalarData, schema.StorageType)
assert.Equal(t, typeinference.NullType, schema.TypeInfo.Type)
assert.True(t, schema.TypeInfo.IsNullable)
}
3 changes: 3 additions & 0 deletions opengin/core-api/pkg/storageinference/inference.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ func isScalar(structValue *structpb.Struct) bool {
// Check if the struct has a single field with a scalar value
if len(structValue.Fields) == 1 {
for _, value := range structValue.Fields {
if value == nil || value.GetKind() == nil {
return true
}
switch value.GetKind().(type) {
case *structpb.Value_NumberValue, *structpb.Value_StringValue, *structpb.Value_BoolValue, *structpb.Value_NullValue:
return true
Expand Down
1 change: 1 addition & 0 deletions opengin/core-api/pkg/storageinference/inference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func TestDirectScalarEntity(t *testing.T) {
"float": `3.14159`,
"string": `"test"`,
"boolean": `true`,
"null": `null`,
}

inferrer := &StorageInferrer{}
Expand Down