Repository: lf-edge/ekuiper
Pull request #3095 (2024-08-13 09:29:19)

chore: merge v2alpha changes

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/lf-edge/ekuiper/pull/3095?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 68.16%. Comparing base [(`5306d22`)](https://app.codecov.io/gh/lf-edge/ekuiper/commit/5306d225f436022a145d48758cdd9aabdab9987a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge) to head [(`c617f8c`)](https://app.codecov.io/gh/lf-edge/ekuiper/commit/c617f8c75af068a7dad4acd20fd44e77054f7b1a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge).
> Report is 4 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master    #3095   +/-   ##
=======================================
  Coverage   68.16%   68.16%           
=======================================
  Files         391      391           
  Lines       37035    37014   -21     
=======================================
- Hits        25242    25229   -13     
+ Misses       9546     9537    -9     
- Partials     2247     2248    +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/lf-edge/ekuiper/pull/3095?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge).


Patch:
diff --git a/internal/converter/delimited/converter.go b/internal/converter/delimited/converter.go
index 65730d46..054cb654 100644
--- a/internal/converter/delimited/converter.go
+++ b/internal/converter/delimited/converter.go
@@ -79,7 +79,8 @@ func (c *Converter) Encode(ctx api.StreamContext, d any) (b []byte, err error) {
 			if i > 0 {
 				sb.WriteString(c.Delimiter)
 			}
-			fmt.Fprintf(sb, "%v", m[v])
+			p, _ := cast.ToString(m[v], cast.CONVERT_ALL)
+			sb.WriteString(p)
 		}
 		return sb.Bytes(), nil
 	case []map[string]any:
@@ -106,7 +107,8 @@ func (c *Converter) Encode(ctx api.StreamContext, d any) (b []byte, err error) {
 				if j > 0 {
 					sb.WriteString(c.Delimiter)
 				}
-				fmt.Fprintf(sb, "%v", mm[v])
+				p, _ := cast.ToString(mm[v], cast.CONVERT_ALL)
+				sb.WriteString(p)
 			}
 		}
 		return sb.Bytes(), nil
diff --git a/internal/xsql/row.go b/internal/xsql/row.go
index 6b3adfcf..4d2a0f19 100644
--- a/internal/xsql/row.go
+++ b/internal/xsql/row.go
@@ -202,7 +202,7 @@ func (d *AffiliateRow) Pick(cols [][]string) [][]string {
 	if len(cols) > 0 {
 		newAliasMap := make(map[string]interface{})
 		newCalCols := make(map[string]interface{})
-		var newCols [][]string
+		newCols := make([][]string, 0, len(cols))
 		for _, a := range cols {
 			if a[1] == "" || a[1] == string(ast.DefaultStream) {
 				if v, ok := d.AliasMap[a[0]]; ok {
diff --git a/pkg/cast/cast.go b/pkg/cast/cast.go
index 2a378aa4..da038523 100644
--- a/pkg/cast/cast.go
+++ b/pkg/cast/cast.go
@@ -1,4 +1,4 @@
-// Copyright 2021-2023 EMQ Technologies Co., Ltd.
+// Copyright 2021-2024 EMQ Technologies Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -93,6 +93,8 @@ func ToString(input interface{}, sn Strictness) (string, error) {
 				return s.String(), nil
 			case error:
 				return s.Error(), nil
+			default:
+				return ToStringAlways(input), nil
 			}
 		}
 	}


Test patch:
diff --git a/internal/converter/delimited/converter_test.go b/internal/converter/delimited/converter_test.go
index 387fccce..ad3ab012 100644
--- a/internal/converter/delimited/converter_test.go
+++ b/internal/converter/delimited/converter_test.go
@@ -37,10 +37,10 @@ func TestEncode(t *testing.T) {
 		{
 			name: "normal",
 			m: map[string]interface{}{
-				"id":   12,
+				"id":   1670170500.0,
 				"name": "test",
 			},
-			r: []byte(`12:test`),
+			r: []byte(`1670170500:test`),
 		},
 		{
 			name: "embedded",
@@ -130,11 +130,11 @@ func TestEncodeWithHeader(t *testing.T) {
 					"name": "test",
 				},
 				{
-					"id":   14,
+					"id":   1670170500.0,
 					"name": "test2",
 				},
 			},
-			r: []byte("id:name\n12:test\n14:test2"),
+			r: []byte("id:name\n12:test\n1670170500:test2"),
 		},
 	}
 	ctx := mockContext.NewMockContext("test", "op1")
diff --git a/pkg/cast/cast_test.go b/pkg/cast/cast_test.go
index 8dea6aa0..fe01b6a7 100644
--- a/pkg/cast/cast_test.go
+++ b/pkg/cast/cast_test.go
@@ -1,4 +1,4 @@
-// Copyright 2021-2023 EMQ Technologies Co., Ltd.
+// Copyright 2021-2024 EMQ Technologies Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import (
 	"fmt"
 	"net"
 	"reflect"
+	"strconv"
 	"testing"
 	"time"
 
@@ -972,3 +973,22 @@ func TestMapToStructDuration(t *testing.T) {
 		})
 	}
 }
+
+func BenchmarkToString(b *testing.B) {
+	f := 3.14
+	b.Run("all", func(b *testing.B) {
+		for i := 0; i < b.N; i++ {
+			_ = ToStringAlways(f)
+		}
+	})
+	b.Run("cast", func(b *testing.B) {
+		for i := 0; i < b.N; i++ {
+			_, _ = ToString(f, CONVERT_ALL)
+		}
+	})
+	b.Run("cast", func(b *testing.B) {
+		for i := 0; i < b.N; i++ {
+			_ = strconv.FormatFloat(f, 'f', -1, 64)
+		}
+	})
+}