Files
s3/weed/query/engine/data_conversion.go
zooqueen dd74d3e6d6 Replace AGPL MinIO fork with Apache-2.0 SeaweedFS fork; rebrand CLI weed->s3
hanzoai/s3 was an AGPL-3.0 MinIO fork whose network-copyleft blocks embedding
and reselling as a managed service. Replace it wholesale with a fork of
SeaweedFS (github.com/seaweedfs/seaweedfs, Apache-2.0, Go) which ships a
built-in S3 gateway + Filer.

- License: AGPL-3.0 -> Apache-2.0 (upstream LICENSE kept verbatim, incl.
  "Copyright 2025 Chris Lu"); NOTICE added crediting SeaweedFS. No AGPL/BUSL/
  SSPL/GPL or enterprise ("ee") files remain.
- Module: go.mod path set to github.com/hanzoai/s3 with a self-replace
  (replace github.com/seaweedfs/seaweedfs => ./) so the ~1800 internal Go files
  keep their upstream import paths with zero churn. Internal identifiers and
  import paths are intentionally NOT renamed.
- CLI rebrand: the binary is built as `s3` (was `weed`); root usage, per-command
  help/usage/flag strings, shell autocomplete program name, and the product name
  in CLI output now read "Hanzo S3" / `s3`. Users never see `weed`.
  (e.g. `s3 server`, `s3 filer`, `s3 master`, `s3 volume`, `s3 s3`, `s3 mount`.)
- Build: `make` (or `go build -o s3 ./weed`) builds the `s3` binary green;
  `cmd/weed-db` and `cmd/weed-sql` also build; `go vet ./weed/command/` clean.
- README/LLM.md rewritten for Hanzo S3; AGENTS.md/CLAUDE.md symlinks preserved.
- Dropped a 33MB prebuilt Mach-O `stress` binary upstream had committed; `.gitignore`
  updated to ignore the `s3` build output.
2026-06-18 23:41:25 -07:00

197 lines
4.9 KiB
Go

package engine
import (
"fmt"
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
"github.com/seaweedfs/seaweedfs/weed/query/sqltypes"
)
// formatAggregationResult formats an aggregation result into a SQL value
func (e *SQLEngine) formatAggregationResult(spec AggregationSpec, result AggregationResult) sqltypes.Value {
switch spec.Function {
case "COUNT":
return sqltypes.NewInt64(result.Count)
case "SUM":
return sqltypes.NewFloat64(result.Sum)
case "AVG":
return sqltypes.NewFloat64(result.Sum) // Sum contains the average for AVG
case "MIN":
if result.Min != nil {
return e.convertRawValueToSQL(result.Min)
}
return sqltypes.NULL
case "MAX":
if result.Max != nil {
return e.convertRawValueToSQL(result.Max)
}
return sqltypes.NULL
}
return sqltypes.NULL
}
// convertRawValueToSQL converts a raw Go value to a SQL value
func (e *SQLEngine) convertRawValueToSQL(value interface{}) sqltypes.Value {
switch v := value.(type) {
case int32:
return sqltypes.NewInt32(v)
case int64:
return sqltypes.NewInt64(v)
case float32:
return sqltypes.NewFloat32(v)
case float64:
return sqltypes.NewFloat64(v)
case string:
return sqltypes.NewVarChar(v)
case bool:
if v {
return sqltypes.NewVarChar("1")
}
return sqltypes.NewVarChar("0")
}
return sqltypes.NULL
}
// extractRawValue extracts the raw Go value from a schema_pb.Value
func (e *SQLEngine) extractRawValue(value *schema_pb.Value) interface{} {
switch v := value.Kind.(type) {
case *schema_pb.Value_Int32Value:
return v.Int32Value
case *schema_pb.Value_Int64Value:
return v.Int64Value
case *schema_pb.Value_FloatValue:
return v.FloatValue
case *schema_pb.Value_DoubleValue:
return v.DoubleValue
case *schema_pb.Value_StringValue:
return v.StringValue
case *schema_pb.Value_BoolValue:
return v.BoolValue
case *schema_pb.Value_BytesValue:
return string(v.BytesValue) // Convert bytes to string for comparison
}
return nil
}
// compareValues compares two schema_pb.Value objects
func (e *SQLEngine) compareValues(value1 *schema_pb.Value, value2 *schema_pb.Value) int {
if value2 == nil {
return 1 // value1 > nil
}
raw1 := e.extractRawValue(value1)
raw2 := e.extractRawValue(value2)
if raw1 == nil {
return -1
}
if raw2 == nil {
return 1
}
// Simple comparison - in a full implementation this would handle type coercion
switch v1 := raw1.(type) {
case int32:
if v2, ok := raw2.(int32); ok {
if v1 < v2 {
return -1
} else if v1 > v2 {
return 1
}
return 0
}
case int64:
if v2, ok := raw2.(int64); ok {
if v1 < v2 {
return -1
} else if v1 > v2 {
return 1
}
return 0
}
case float32:
if v2, ok := raw2.(float32); ok {
if v1 < v2 {
return -1
} else if v1 > v2 {
return 1
}
return 0
}
case float64:
if v2, ok := raw2.(float64); ok {
if v1 < v2 {
return -1
} else if v1 > v2 {
return 1
}
return 0
}
case string:
if v2, ok := raw2.(string); ok {
if v1 < v2 {
return -1
} else if v1 > v2 {
return 1
}
return 0
}
case bool:
if v2, ok := raw2.(bool); ok {
if v1 == v2 {
return 0
} else if v1 && !v2 {
return 1
}
return -1
}
}
return 0
}
// convertRawValueToSchemaValue converts raw Go values back to schema_pb.Value for comparison
func (e *SQLEngine) convertRawValueToSchemaValue(rawValue interface{}) *schema_pb.Value {
switch v := rawValue.(type) {
case int32:
return &schema_pb.Value{Kind: &schema_pb.Value_Int32Value{Int32Value: v}}
case int64:
return &schema_pb.Value{Kind: &schema_pb.Value_Int64Value{Int64Value: v}}
case float32:
return &schema_pb.Value{Kind: &schema_pb.Value_FloatValue{FloatValue: v}}
case float64:
return &schema_pb.Value{Kind: &schema_pb.Value_DoubleValue{DoubleValue: v}}
case string:
return &schema_pb.Value{Kind: &schema_pb.Value_StringValue{StringValue: v}}
case bool:
return &schema_pb.Value{Kind: &schema_pb.Value_BoolValue{BoolValue: v}}
case []byte:
return &schema_pb.Value{Kind: &schema_pb.Value_BytesValue{BytesValue: v}}
default:
// Convert other types to string as fallback
return &schema_pb.Value{Kind: &schema_pb.Value_StringValue{StringValue: fmt.Sprintf("%v", v)}}
}
}
// Helper functions for aggregation processing
// isNullValue checks if a schema_pb.Value is null or empty
func (e *SQLEngine) isNullValue(value *schema_pb.Value) bool {
return value == nil || value.Kind == nil
}
// convertToNumber converts a schema_pb.Value to a float64 for numeric operations
func (e *SQLEngine) convertToNumber(value *schema_pb.Value) *float64 {
switch v := value.Kind.(type) {
case *schema_pb.Value_Int32Value:
result := float64(v.Int32Value)
return &result
case *schema_pb.Value_Int64Value:
result := float64(v.Int64Value)
return &result
case *schema_pb.Value_FloatValue:
result := float64(v.FloatValue)
return &result
case *schema_pb.Value_DoubleValue:
return &v.DoubleValue
}
return nil
}