function_helpers.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package engine
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  7. )
  8. // Helper function to convert schema_pb.Value to float64
  9. func (e *SQLEngine) valueToFloat64(value *schema_pb.Value) (float64, error) {
  10. switch v := value.Kind.(type) {
  11. case *schema_pb.Value_Int32Value:
  12. return float64(v.Int32Value), nil
  13. case *schema_pb.Value_Int64Value:
  14. return float64(v.Int64Value), nil
  15. case *schema_pb.Value_FloatValue:
  16. return float64(v.FloatValue), nil
  17. case *schema_pb.Value_DoubleValue:
  18. return v.DoubleValue, nil
  19. case *schema_pb.Value_StringValue:
  20. // Try to parse string as number
  21. if f, err := strconv.ParseFloat(v.StringValue, 64); err == nil {
  22. return f, nil
  23. }
  24. return 0, fmt.Errorf("cannot convert string '%s' to number", v.StringValue)
  25. case *schema_pb.Value_BoolValue:
  26. if v.BoolValue {
  27. return 1, nil
  28. }
  29. return 0, nil
  30. default:
  31. return 0, fmt.Errorf("cannot convert value type to number")
  32. }
  33. }
  34. // Helper function to check if a value is an integer type
  35. func (e *SQLEngine) isIntegerValue(value *schema_pb.Value) bool {
  36. switch value.Kind.(type) {
  37. case *schema_pb.Value_Int32Value, *schema_pb.Value_Int64Value:
  38. return true
  39. default:
  40. return false
  41. }
  42. }
  43. // Helper function to convert schema_pb.Value to string
  44. func (e *SQLEngine) valueToString(value *schema_pb.Value) (string, error) {
  45. switch v := value.Kind.(type) {
  46. case *schema_pb.Value_StringValue:
  47. return v.StringValue, nil
  48. case *schema_pb.Value_Int32Value:
  49. return strconv.FormatInt(int64(v.Int32Value), 10), nil
  50. case *schema_pb.Value_Int64Value:
  51. return strconv.FormatInt(v.Int64Value, 10), nil
  52. case *schema_pb.Value_FloatValue:
  53. return strconv.FormatFloat(float64(v.FloatValue), 'g', -1, 32), nil
  54. case *schema_pb.Value_DoubleValue:
  55. return strconv.FormatFloat(v.DoubleValue, 'g', -1, 64), nil
  56. case *schema_pb.Value_BoolValue:
  57. if v.BoolValue {
  58. return "true", nil
  59. }
  60. return "false", nil
  61. case *schema_pb.Value_BytesValue:
  62. return string(v.BytesValue), nil
  63. default:
  64. return "", fmt.Errorf("cannot convert value type to string")
  65. }
  66. }
  67. // Helper function to convert schema_pb.Value to int64
  68. func (e *SQLEngine) valueToInt64(value *schema_pb.Value) (int64, error) {
  69. switch v := value.Kind.(type) {
  70. case *schema_pb.Value_Int32Value:
  71. return int64(v.Int32Value), nil
  72. case *schema_pb.Value_Int64Value:
  73. return v.Int64Value, nil
  74. case *schema_pb.Value_FloatValue:
  75. return int64(v.FloatValue), nil
  76. case *schema_pb.Value_DoubleValue:
  77. return int64(v.DoubleValue), nil
  78. case *schema_pb.Value_StringValue:
  79. if i, err := strconv.ParseInt(v.StringValue, 10, 64); err == nil {
  80. return i, nil
  81. }
  82. return 0, fmt.Errorf("cannot convert string '%s' to integer", v.StringValue)
  83. default:
  84. return 0, fmt.Errorf("cannot convert value type to integer")
  85. }
  86. }
  87. // Helper function to convert schema_pb.Value to time.Time
  88. func (e *SQLEngine) valueToTime(value *schema_pb.Value) (time.Time, error) {
  89. switch v := value.Kind.(type) {
  90. case *schema_pb.Value_TimestampValue:
  91. if v.TimestampValue == nil {
  92. return time.Time{}, fmt.Errorf("null timestamp value")
  93. }
  94. return time.UnixMicro(v.TimestampValue.TimestampMicros), nil
  95. case *schema_pb.Value_StringValue:
  96. // Try to parse various date/time string formats
  97. dateFormats := []struct {
  98. format string
  99. useLocal bool
  100. }{
  101. {"2006-01-02 15:04:05", true}, // Local time assumed for non-timezone formats
  102. {"2006-01-02T15:04:05Z", false}, // UTC format
  103. {"2006-01-02T15:04:05", true}, // Local time assumed
  104. {"2006-01-02", true}, // Local time assumed for date only
  105. {"15:04:05", true}, // Local time assumed for time only
  106. }
  107. for _, formatSpec := range dateFormats {
  108. if t, err := time.Parse(formatSpec.format, v.StringValue); err == nil {
  109. if formatSpec.useLocal {
  110. // Convert to UTC for consistency if no timezone was specified
  111. return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.UTC), nil
  112. }
  113. return t, nil
  114. }
  115. }
  116. return time.Time{}, fmt.Errorf("unable to parse date/time string: %s", v.StringValue)
  117. case *schema_pb.Value_Int64Value:
  118. // Assume Unix timestamp (seconds)
  119. return time.Unix(v.Int64Value, 0), nil
  120. default:
  121. return time.Time{}, fmt.Errorf("cannot convert value type to date/time")
  122. }
  123. }