aboutsummaryrefslogtreecommitdiff
path: root/weed/query/engine/sql_types.go
blob: b679e89bd1f36e9c50a5b9d45e7aaa7c67d28638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package engine

import (
	"fmt"
	"strings"

	"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
)

// convertSQLTypeToMQ converts SQL column types to MQ schema field types
// Assumptions:
// 1. Standard SQL types map to MQ scalar types
// 2. Unsupported types result in errors
// 3. Default sizes are used for variable-length types
func (e *SQLEngine) convertSQLTypeToMQ(sqlType TypeRef) (*schema_pb.Type, error) {
	typeName := strings.ToUpper(sqlType.Type)

	switch typeName {
	case "BOOLEAN", "BOOL":
		return &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_BOOL}}, nil

	case "TINYINT", "SMALLINT", "INT", "INTEGER", "MEDIUMINT":
		return &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_INT32}}, nil

	case "BIGINT":
		return &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_INT64}}, nil

	case "FLOAT", "REAL":
		return &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_FLOAT}}, nil

	case "DOUBLE", "DOUBLE PRECISION":
		return &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_DOUBLE}}, nil

	case "CHAR", "VARCHAR", "TEXT", "LONGTEXT", "MEDIUMTEXT", "TINYTEXT":
		return &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_STRING}}, nil

	case "BINARY", "VARBINARY", "BLOB", "LONGBLOB", "MEDIUMBLOB", "TINYBLOB":
		return &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_BYTES}}, nil

	case "JSON":
		// JSON stored as string for now
		// TODO: Implement proper JSON type support
		return &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_STRING}}, nil

	case "TIMESTAMP", "DATETIME":
		// Store as BIGINT (Unix timestamp in nanoseconds)
		return &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_INT64}}, nil

	default:
		return nil, fmt.Errorf("unsupported SQL type: %s", typeName)
	}
}

// convertMQTypeToSQL converts MQ schema field types back to SQL column types
// This is the reverse of convertSQLTypeToMQ for display purposes
func (e *SQLEngine) convertMQTypeToSQL(fieldType *schema_pb.Type) string {
	switch t := fieldType.Kind.(type) {
	case *schema_pb.Type_ScalarType:
		switch t.ScalarType {
		case schema_pb.ScalarType_BOOL:
			return "BOOLEAN"
		case schema_pb.ScalarType_INT32:
			return "INT"
		case schema_pb.ScalarType_INT64:
			return "BIGINT"
		case schema_pb.ScalarType_FLOAT:
			return "FLOAT"
		case schema_pb.ScalarType_DOUBLE:
			return "DOUBLE"
		case schema_pb.ScalarType_BYTES:
			return "VARBINARY"
		case schema_pb.ScalarType_STRING:
			return "VARCHAR(255)"
		default:
			return "UNKNOWN"
		}
	case *schema_pb.Type_ListType:
		return "TEXT" // Lists serialized as JSON
	case *schema_pb.Type_RecordType:
		return "TEXT" // Nested records serialized as JSON
	default:
		return "UNKNOWN"
	}
}