data_conversion.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package engine
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  5. "github.com/seaweedfs/seaweedfs/weed/query/sqltypes"
  6. )
  7. // formatAggregationResult formats an aggregation result into a SQL value
  8. func (e *SQLEngine) formatAggregationResult(spec AggregationSpec, result AggregationResult) sqltypes.Value {
  9. switch spec.Function {
  10. case "COUNT":
  11. return sqltypes.NewInt64(result.Count)
  12. case "SUM":
  13. return sqltypes.NewFloat64(result.Sum)
  14. case "AVG":
  15. return sqltypes.NewFloat64(result.Sum) // Sum contains the average for AVG
  16. case "MIN":
  17. if result.Min != nil {
  18. return e.convertRawValueToSQL(result.Min)
  19. }
  20. return sqltypes.NULL
  21. case "MAX":
  22. if result.Max != nil {
  23. return e.convertRawValueToSQL(result.Max)
  24. }
  25. return sqltypes.NULL
  26. }
  27. return sqltypes.NULL
  28. }
  29. // convertRawValueToSQL converts a raw Go value to a SQL value
  30. func (e *SQLEngine) convertRawValueToSQL(value interface{}) sqltypes.Value {
  31. switch v := value.(type) {
  32. case int32:
  33. return sqltypes.NewInt32(v)
  34. case int64:
  35. return sqltypes.NewInt64(v)
  36. case float32:
  37. return sqltypes.NewFloat32(v)
  38. case float64:
  39. return sqltypes.NewFloat64(v)
  40. case string:
  41. return sqltypes.NewVarChar(v)
  42. case bool:
  43. if v {
  44. return sqltypes.NewVarChar("1")
  45. }
  46. return sqltypes.NewVarChar("0")
  47. }
  48. return sqltypes.NULL
  49. }
  50. // extractRawValue extracts the raw Go value from a schema_pb.Value
  51. func (e *SQLEngine) extractRawValue(value *schema_pb.Value) interface{} {
  52. switch v := value.Kind.(type) {
  53. case *schema_pb.Value_Int32Value:
  54. return v.Int32Value
  55. case *schema_pb.Value_Int64Value:
  56. return v.Int64Value
  57. case *schema_pb.Value_FloatValue:
  58. return v.FloatValue
  59. case *schema_pb.Value_DoubleValue:
  60. return v.DoubleValue
  61. case *schema_pb.Value_StringValue:
  62. return v.StringValue
  63. case *schema_pb.Value_BoolValue:
  64. return v.BoolValue
  65. case *schema_pb.Value_BytesValue:
  66. return string(v.BytesValue) // Convert bytes to string for comparison
  67. }
  68. return nil
  69. }
  70. // compareValues compares two schema_pb.Value objects
  71. func (e *SQLEngine) compareValues(value1 *schema_pb.Value, value2 *schema_pb.Value) int {
  72. if value2 == nil {
  73. return 1 // value1 > nil
  74. }
  75. raw1 := e.extractRawValue(value1)
  76. raw2 := e.extractRawValue(value2)
  77. if raw1 == nil {
  78. return -1
  79. }
  80. if raw2 == nil {
  81. return 1
  82. }
  83. // Simple comparison - in a full implementation this would handle type coercion
  84. switch v1 := raw1.(type) {
  85. case int32:
  86. if v2, ok := raw2.(int32); ok {
  87. if v1 < v2 {
  88. return -1
  89. } else if v1 > v2 {
  90. return 1
  91. }
  92. return 0
  93. }
  94. case int64:
  95. if v2, ok := raw2.(int64); ok {
  96. if v1 < v2 {
  97. return -1
  98. } else if v1 > v2 {
  99. return 1
  100. }
  101. return 0
  102. }
  103. case float32:
  104. if v2, ok := raw2.(float32); ok {
  105. if v1 < v2 {
  106. return -1
  107. } else if v1 > v2 {
  108. return 1
  109. }
  110. return 0
  111. }
  112. case float64:
  113. if v2, ok := raw2.(float64); ok {
  114. if v1 < v2 {
  115. return -1
  116. } else if v1 > v2 {
  117. return 1
  118. }
  119. return 0
  120. }
  121. case string:
  122. if v2, ok := raw2.(string); ok {
  123. if v1 < v2 {
  124. return -1
  125. } else if v1 > v2 {
  126. return 1
  127. }
  128. return 0
  129. }
  130. case bool:
  131. if v2, ok := raw2.(bool); ok {
  132. if v1 == v2 {
  133. return 0
  134. } else if v1 && !v2 {
  135. return 1
  136. }
  137. return -1
  138. }
  139. }
  140. return 0
  141. }
  142. // convertRawValueToSchemaValue converts raw Go values back to schema_pb.Value for comparison
  143. func (e *SQLEngine) convertRawValueToSchemaValue(rawValue interface{}) *schema_pb.Value {
  144. switch v := rawValue.(type) {
  145. case int32:
  146. return &schema_pb.Value{Kind: &schema_pb.Value_Int32Value{Int32Value: v}}
  147. case int64:
  148. return &schema_pb.Value{Kind: &schema_pb.Value_Int64Value{Int64Value: v}}
  149. case float32:
  150. return &schema_pb.Value{Kind: &schema_pb.Value_FloatValue{FloatValue: v}}
  151. case float64:
  152. return &schema_pb.Value{Kind: &schema_pb.Value_DoubleValue{DoubleValue: v}}
  153. case string:
  154. return &schema_pb.Value{Kind: &schema_pb.Value_StringValue{StringValue: v}}
  155. case bool:
  156. return &schema_pb.Value{Kind: &schema_pb.Value_BoolValue{BoolValue: v}}
  157. case []byte:
  158. return &schema_pb.Value{Kind: &schema_pb.Value_BytesValue{BytesValue: v}}
  159. default:
  160. // Convert other types to string as fallback
  161. return &schema_pb.Value{Kind: &schema_pb.Value_StringValue{StringValue: fmt.Sprintf("%v", v)}}
  162. }
  163. }
  164. // convertJSONValueToSchemaValue converts JSON values to schema_pb.Value
  165. func (e *SQLEngine) convertJSONValueToSchemaValue(jsonValue interface{}) *schema_pb.Value {
  166. switch v := jsonValue.(type) {
  167. case string:
  168. return &schema_pb.Value{Kind: &schema_pb.Value_StringValue{StringValue: v}}
  169. case float64:
  170. // JSON numbers are always float64, try to detect if it's actually an integer
  171. if v == float64(int64(v)) {
  172. return &schema_pb.Value{Kind: &schema_pb.Value_Int64Value{Int64Value: int64(v)}}
  173. }
  174. return &schema_pb.Value{Kind: &schema_pb.Value_DoubleValue{DoubleValue: v}}
  175. case bool:
  176. return &schema_pb.Value{Kind: &schema_pb.Value_BoolValue{BoolValue: v}}
  177. case nil:
  178. return nil
  179. default:
  180. // Convert other types to string
  181. return &schema_pb.Value{Kind: &schema_pb.Value_StringValue{StringValue: fmt.Sprintf("%v", v)}}
  182. }
  183. }
  184. // Helper functions for aggregation processing
  185. // isNullValue checks if a schema_pb.Value is null or empty
  186. func (e *SQLEngine) isNullValue(value *schema_pb.Value) bool {
  187. return value == nil || value.Kind == nil
  188. }
  189. // convertToNumber converts a schema_pb.Value to a float64 for numeric operations
  190. func (e *SQLEngine) convertToNumber(value *schema_pb.Value) *float64 {
  191. switch v := value.Kind.(type) {
  192. case *schema_pb.Value_Int32Value:
  193. result := float64(v.Int32Value)
  194. return &result
  195. case *schema_pb.Value_Int64Value:
  196. result := float64(v.Int64Value)
  197. return &result
  198. case *schema_pb.Value_FloatValue:
  199. result := float64(v.FloatValue)
  200. return &result
  201. case *schema_pb.Value_DoubleValue:
  202. return &v.DoubleValue
  203. }
  204. return nil
  205. }