Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support scan Nebula subgraph results (vertex and edge) and slice of struct ptr #346

Merged
merged 9 commits into from
Jan 17, 2025
Merged
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
174 changes: 148 additions & 26 deletions result_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,19 @@ func (res ResultSet) Scan(v interface{}) error {
}

// Scan scans the rows into the given value.
func (res ResultSet) scanRow(row *nebula.Row, colNames []string, t reflect.Type) (reflect.Value, error) {
func (res ResultSet) scanRow(row *nebula.Row, colNames []string, rowType reflect.Type) (reflect.Value, error) {
rowVals := row.GetValues()

val := reflect.New(t).Elem()
var result reflect.Value
if rowType.Kind() == reflect.Ptr {
result = reflect.New(rowType.Elem())
} else {
result = reflect.New(rowType).Elem()
}
structVal := reflect.Indirect(result)

for fIdx := 0; fIdx < t.NumField(); fIdx++ {
f := t.Field(fIdx)
for fIdx := 0; fIdx < structVal.Type().NumField(); fIdx++ {
f := structVal.Type().Field(fIdx)
tag := f.Tag.Get("nebula")

if tag == "" {
Expand All @@ -356,31 +362,147 @@ func (res ResultSet) scanRow(row *nebula.Row, colNames []string, t reflect.Type)

rowVal := rowVals[cIdx]

switch f.Type.Kind() {
case reflect.Bool:
val.Field(fIdx).SetBool(rowVal.GetBVal())
case reflect.Int:
val.Field(fIdx).SetInt(rowVal.GetIVal())
case reflect.Int8:
val.Field(fIdx).SetInt(rowVal.GetIVal())
case reflect.Int16:
val.Field(fIdx).SetInt(rowVal.GetIVal())
case reflect.Int32:
val.Field(fIdx).SetInt(rowVal.GetIVal())
case reflect.Int64:
val.Field(fIdx).SetInt(rowVal.GetIVal())
case reflect.Float32:
val.Field(fIdx).SetFloat(rowVal.GetFVal())
case reflect.Float64:
val.Field(fIdx).SetFloat(rowVal.GetFVal())
case reflect.String:
val.Field(fIdx).SetString(string(rowVal.GetSVal()))
default:
return val, errors.New("scan: not support type")
if f.Type.Kind() == reflect.Slice {
list := rowVal.GetLVal()
err := scanListCol(list.Values, structVal.Field(fIdx), f.Type)
if err != nil {
return result, err
}
} else {
err := scanPrimitiveCol(rowVal, structVal.Field(fIdx), f.Type.Kind())
if err != nil {
return result, err
}
}
}

return result, nil
}

func scanListCol(vals []*nebula.Value, listVal reflect.Value, sliceType reflect.Type) error {
switch sliceType.Elem().Kind() {
case reflect.Struct:
var listCol = reflect.MakeSlice(sliceType, 0, len(vals))
for _, val := range vals {
ele := reflect.New(sliceType.Elem()).Elem()
err := scanStructField(val, ele, sliceType.Elem())
if err != nil {
return err
}
listCol = reflect.Append(listCol, ele)
}
listVal.Set(listCol)
case reflect.Ptr:
var listCol = reflect.MakeSlice(sliceType, 0, len(vals))
for _, val := range vals {
ele := reflect.New(sliceType.Elem().Elem())
err := scanStructField(val, reflect.Indirect(ele), sliceType.Elem().Elem())
if err != nil {
return err
}
listCol = reflect.Append(listCol, ele)
}
listVal.Set(listCol)
default:
return errors.New("scan: not support list type")
}

return val, nil
return nil
}

func scanStructField(val *nebula.Value, eleVal reflect.Value, eleType reflect.Type) error {
vertex := val.GetVVal()
if vertex != nil {
tags := vertex.GetTags()
vid := vertex.GetVid()

if len(tags) != 0 {
tag := tags[0]

props := tag.GetProps()
props["_vid"] = vid
tagName := tag.GetName()
props["_tag_name"] = &nebula.Value{SVal: tagName}

err := scanValFromProps(props, eleVal, eleType)
if err != nil {
return err
}
return nil
}
// no tags, continue
}

edge := val.GetEVal()
if edge != nil {
props := edge.GetProps()

src := edge.GetSrc()
dst := edge.GetDst()
name := edge.GetName()
props["_src"] = src
props["_dst"] = dst
props["_name"] = &nebula.Value{SVal: name}

err := scanValFromProps(props, eleVal, eleType)
if err != nil {
return err
}
return nil
}

return nil
}

func scanValFromProps(props map[string]*nebula.Value, val reflect.Value, tpe reflect.Type) error {
for fIdx := 0; fIdx < tpe.NumField(); fIdx++ {
f := tpe.Field(fIdx)
n := f.Tag.Get("nebula")
v, ok := props[n]
if !ok {
continue
}
err := scanPrimitiveCol(v, val.Field(fIdx), f.Type.Kind())
if err != nil {
return err
}
}

return nil
}

func scanPrimitiveCol(rowVal *nebula.Value, val reflect.Value, kind reflect.Kind) error {
w := ValueWrapper{value: rowVal}
if w.IsNull() || w.IsEmpty() {
// SetZero is introduced in go 1.20
// val.SetZero()
return nil
}

switch kind {
case reflect.Bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when rowVal is Null type, how to set value for reflect.Value?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to process these two types.
image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just skip to set the value if empty or null? I mean


func scanPrimitiveCol(rowVal *nebula.Value, val reflect.Value, kind reflect.Kind) error {
    if rowVal.IsSetNVal() {
        return nil
    }
    ...
}

But, is there a easy way to do IsEmpty on the type *nebula.Value?

Copy link
Collaborator Author

@haoxins haoxins Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I move the method GetType from ValueWrapper to *Value?

emm, we can't move the method GetType into *nebula.Value

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, what is the situation will output empty or null? I running these codes in my server for months already.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you means

func scanPrimitiveCol(rowVal *nebula.Value, val reflect.Value, kind reflect.Kind) error {
    w := ValueWrapper{value: rowVal}
    if w.IsNull() || w.IsEmpty() {
        val.SetZero()
        return nil
    }

    ......
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emm, will cause a bit performance cost but not a problem.
And, a bit ugly 😂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, tests passed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it's too slow to review it and too late to reply.

Please forgive me for not understanding why it is ugly~ I think that is the logic that must be processed, otherwise this method will not be able to handle null values.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please forgive me for not understanding why it is ugly~

Never mind, I found I don't need to copy the logic of IsNull and IsEmpty finally.
Just need to use ValueWrapper.

val.SetBool(rowVal.GetBVal())
case reflect.Int:
val.SetInt(rowVal.GetIVal())
case reflect.Int8:
val.SetInt(rowVal.GetIVal())
case reflect.Int16:
val.SetInt(rowVal.GetIVal())
case reflect.Int32:
val.SetInt(rowVal.GetIVal())
case reflect.Int64:
val.SetInt(rowVal.GetIVal())
case reflect.Float32:
val.SetFloat(rowVal.GetFVal())
case reflect.Float64:
val.SetFloat(rowVal.GetFVal())
case reflect.String:
val.SetString(string(rowVal.GetSVal()))
default:
return errors.New("scan: not support primitive type")
}

return nil
}

// Returns the number of total rows
Expand Down
Loading
Loading