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

fix: converter lose precision #3445

Merged
merged 6 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
34 changes: 23 additions & 11 deletions internal/converter/json/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ package json
import (
"encoding/json"
"fmt"
"strings"
"sync"

"github.com/lf-edge/ekuiper/contract/v2/api"
"github.com/valyala/fastjson"

"github.com/lf-edge/ekuiper/v2/internal/conf"
"github.com/lf-edge/ekuiper/v2/pkg/ast"
"github.com/lf-edge/ekuiper/v2/pkg/cast"
"github.com/lf-edge/ekuiper/v2/pkg/errorx"
Expand Down Expand Up @@ -57,11 +59,6 @@ func (f *FastJsonConverter) Decode(ctx api.StreamContext, b []byte) (m any, err
}()
f.RLock()
defer f.RUnlock()
if f.schema == nil {
var r any
err = json.Unmarshal(b, &r)
return r, err
}
return f.decodeWithSchema(b, f.schema)
}

Expand All @@ -85,7 +82,7 @@ func (f *FastJsonConverter) DecodeField(_ api.StreamContext, b []byte, field str
case fastjson.TypeString:
return vv.String(), nil
case fastjson.TypeNumber:
return vv.Float64()
return extractNumber(vv)
case fastjson.TypeTrue, fastjson.TypeFalse:
return vv.Bool()
}
Expand Down Expand Up @@ -340,11 +337,7 @@ func (f *FastJsonConverter) checkSchema(key, typ string, schema map[string]*ast.

func (f *FastJsonConverter) extractNumberValue(name string, v *fastjson.Value, field *ast.JsonStreamField) (interface{}, error) {
if field == nil {
f64, err := v.Float64()
if err != nil {
return nil, err
}
return f64, nil
return extractNumber(v)
}
switch {
case field.Type == "float", field.Type == "datetime":
Expand Down Expand Up @@ -454,3 +447,22 @@ func getType(t *ast.JsonStreamField) string {
return t.Type
}
}

func extractNumber(v *fastjson.Value) (any, error) {
if !isFloat64(v.String()) && !conf.IsTesting {
Yisaer marked this conversation as resolved.
Show resolved Hide resolved
i64, err := v.Int64()
if err != nil {
return nil, err
}
return i64, nil
}
f64, err := v.Float64()
if err != nil {
return nil, err
}
return f64, nil
}

func isFloat64(v string) bool {
return strings.Contains(v, ".")
}
19 changes: 19 additions & 0 deletions internal/converter/json/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/lf-edge/ekuiper/v2/internal/conf"
"github.com/lf-edge/ekuiper/v2/internal/topo/context"
"github.com/lf-edge/ekuiper/v2/pkg/ast"
mockContext "github.com/lf-edge/ekuiper/v2/pkg/mock/context"
Expand Down Expand Up @@ -767,3 +768,21 @@ func TestDecodeField(t *testing.T) {
})
}
}

func TestIssue3441(t *testing.T) {
conf.IsTesting = false
originSchema := map[string]*ast.JsonStreamField{
"id": nil,
}
f := NewFastJsonConverter(originSchema)
data := `{"id":1795292668348461056}`
ctx := mockContext.NewMockContext("test", "op1")
m, err := f.Decode(ctx, []byte(data))
require.NoError(t, err)
require.Equal(t, map[string]interface{}{"id": int64(1795292668348461056)}, m)

data = `{"id":17952926683484.44}`
m, err = f.Decode(ctx, []byte(data))
require.NoError(t, err)
require.Equal(t, map[string]interface{}{"id": 17952926683484.44}, m)
}
1 change: 1 addition & 0 deletions internal/io/memory/lookupsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func TestUpdateLookup(t *testing.T) {
}

func TestLookup(t *testing.T) {
conf.IsTesting = true
contextLogger := conf.Log.WithField("rule", "test2")
ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
ls := GetLookupSource().(api.LookupSource)
Expand Down
2 changes: 2 additions & 0 deletions internal/topo/node/decode_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/lf-edge/ekuiper/v2/internal/conf"
"github.com/lf-edge/ekuiper/v2/internal/pkg/def"
"github.com/lf-edge/ekuiper/v2/internal/xsql"
"github.com/lf-edge/ekuiper/v2/pkg/ast"
Expand Down Expand Up @@ -392,6 +393,7 @@ func TestPayloadDecodeWithSchema(t *testing.T) {
}

func TestPayloadBatchDecodeWithSchema(t *testing.T) {
conf.IsTesting = true
tests := []struct {
name string
input any
Expand Down
3 changes: 3 additions & 0 deletions internal/topo/node/lookup_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/lf-edge/ekuiper/contract/v2/api"
"github.com/stretchr/testify/assert"

"github.com/lf-edge/ekuiper/v2/internal/conf"
"github.com/lf-edge/ekuiper/v2/internal/pkg/def"
"github.com/lf-edge/ekuiper/v2/internal/topo/lookup"
"github.com/lf-edge/ekuiper/v2/internal/xsql"
Expand Down Expand Up @@ -341,6 +342,7 @@ func TestLookup(t *testing.T) {
}

func TestLookupInner(t *testing.T) {
conf.IsTesting = true
tests := []struct {
name string
input any
Expand Down Expand Up @@ -608,6 +610,7 @@ func TestLookupInner(t *testing.T) {
}

func TestLookupPayload(t *testing.T) {
conf.IsTesting = true
tests := []struct {
name string
input any
Expand Down
3 changes: 2 additions & 1 deletion internal/topo/node/window_inc_agg_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ func TestIncAggTumblingWindow(t *testing.T) {
errCh := make(chan error, 10)
ctx, cancel := mockContext.NewMockContext("1", "2").WithCancel()
op.Exec(ctx, errCh)
time.Sleep(10 * time.Millisecond)
waitExecute()
input <- &xsql.Tuple{Message: map[string]any{"a": int64(1)}}
waitExecute()
timex.Add(1100 * time.Millisecond)
got := <-output
wt, ok := got.(*xsql.WindowTuples)
Expand Down
Loading