Browse Source

fixes

pull/7185/head
chrislu 1 month ago
parent
commit
bdce5439d8
  1. 5
      SQL_FEATURE_PLAN.md
  2. 3
      weed/command/db.go
  3. 47
      weed/command/sql.go
  4. 20
      weed/command/sql_test.go

5
SQL_FEATURE_PLAN.md

@ -303,7 +303,10 @@ SQL Query Flow:
## Success Metrics ## Success Metrics
* **Feature Completeness:** Support for all specified DDL/DML operations * **Feature Completeness:** Support for all specified DDL/DML operations
* **Performance:** Query latency < 100ms for simple selects, < 1s for complex joins
* **Performance:**
* **Simple SELECT queries**: < 100ms latency for single-table queries with up to 3 WHERE predicates on 100K records
* **Complex queries**: < 1s latency for queries involving aggregations (COUNT, SUM, MAX, MIN) on 1M records
* **Time-range queries**: < 500ms for timestamp-based filtering on 500K records within 24-hour windows
* **Scalability:** Handle topics with millions of messages efficiently * **Scalability:** Handle topics with millions of messages efficiently
* **Reliability:** 99.9% success rate for valid SQL operations * **Reliability:** 99.9% success rate for valid SQL operations
* **Usability:** Intuitive SQL interface matching standard database expectations * **Usability:** Intuitive SQL interface matching standard database expectations

3
weed/command/db.go

@ -10,7 +10,6 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/server/postgres" "github.com/seaweedfs/seaweedfs/weed/server/postgres"
"github.com/seaweedfs/seaweedfs/weed/util" "github.com/seaweedfs/seaweedfs/weed/util"
) )
@ -363,7 +362,7 @@ func validatePortNumber(port int) error {
return fmt.Errorf("port number must be between 1 and 65535, got %d", port) return fmt.Errorf("port number must be between 1 and 65535, got %d", port)
} }
if port < 1024 { if port < 1024 {
glog.Warningf("port number %d may require root privileges", port)
fmt.Fprintf(os.Stderr, "Warning: port number %d may require root privileges\n", port)
} }
return nil return nil
} }

47
weed/command/sql.go

@ -22,7 +22,7 @@ import (
func splitSQLStatements(query string) []string { func splitSQLStatements(query string) []string {
var statements []string var statements []string
var current strings.Builder var current strings.Builder
query = strings.TrimSpace(query) query = strings.TrimSpace(query)
if query == "" { if query == "" {
return []string{} return []string{}
@ -30,57 +30,50 @@ func splitSQLStatements(query string) []string {
runes := []rune(query) runes := []rune(query)
i := 0 i := 0
for i < len(runes) { for i < len(runes) {
char := runes[i] char := runes[i]
// Handle single-line comments (-- comment) // Handle single-line comments (-- comment)
if char == '-' && i+1 < len(runes) && runes[i+1] == '-' { if char == '-' && i+1 < len(runes) && runes[i+1] == '-' {
// Include the entire comment in the current statement
// Skip the entire comment without including it in any statement
for i < len(runes) && runes[i] != '\n' && runes[i] != '\r' { for i < len(runes) && runes[i] != '\n' && runes[i] != '\r' {
current.WriteRune(runes[i])
i++ i++
} }
// Include the newline if present
// Skip the newline if present
if i < len(runes) { if i < len(runes) {
current.WriteRune(runes[i])
i++ i++
} }
continue continue
} }
// Handle multi-line comments (/* comment */)
// Handle multi-line comments (/* comment */)
if char == '/' && i+1 < len(runes) && runes[i+1] == '*' { if char == '/' && i+1 < len(runes) && runes[i+1] == '*' {
current.WriteRune(char) // Include the /*
// Skip the /* opening
i++
i++ i++
if i < len(runes) {
current.WriteRune(runes[i])
i++
}
// Skip to end of comment or end of input
// Skip to end of comment or end of input without including content
for i < len(runes) { for i < len(runes) {
current.WriteRune(runes[i])
if runes[i] == '*' && i+1 < len(runes) && runes[i+1] == '/' { if runes[i] == '*' && i+1 < len(runes) && runes[i+1] == '/' {
i++
current.WriteRune(runes[i]) // Include the */
i++
i++ // Skip the *
i++ // Skip the /
break break
} }
i++ i++
} }
continue continue
} }
// Handle single-quoted strings // Handle single-quoted strings
if char == '\'' { if char == '\'' {
current.WriteRune(char) current.WriteRune(char)
i++ i++
for i < len(runes) { for i < len(runes) {
char = runes[i] char = runes[i]
current.WriteRune(char) current.WriteRune(char)
if char == '\'' { if char == '\'' {
// Check if it's an escaped quote // Check if it's an escaped quote
if i+1 < len(runes) && runes[i+1] == '\'' { if i+1 < len(runes) && runes[i+1] == '\'' {
@ -97,16 +90,16 @@ func splitSQLStatements(query string) []string {
i++ i++
continue continue
} }
// Handle double-quoted identifiers // Handle double-quoted identifiers
if char == '"' { if char == '"' {
current.WriteRune(char) current.WriteRune(char)
i++ i++
for i < len(runes) { for i < len(runes) {
char = runes[i] char = runes[i]
current.WriteRune(char) current.WriteRune(char)
if char == '"' { if char == '"' {
// Check if it's an escaped quote // Check if it's an escaped quote
if i+1 < len(runes) && runes[i+1] == '"' { if i+1 < len(runes) && runes[i+1] == '"' {
@ -123,7 +116,7 @@ func splitSQLStatements(query string) []string {
i++ i++
continue continue
} }
// Handle semicolon (statement separator) // Handle semicolon (statement separator)
if char == ';' { if char == ';' {
stmt := strings.TrimSpace(current.String()) stmt := strings.TrimSpace(current.String())
@ -134,7 +127,7 @@ func splitSQLStatements(query string) []string {
} else { } else {
current.WriteRune(char) current.WriteRune(char)
} }
i++ i++
} }

20
weed/command/sql_test.go

@ -44,33 +44,31 @@ func TestSplitSQLStatements(t *testing.T) {
{ {
name: "Single line comment", name: "Single line comment",
input: "SELECT * FROM users; -- This is a comment\nSELECT * FROM orders;", input: "SELECT * FROM users; -- This is a comment\nSELECT * FROM orders;",
expected: []string{"SELECT * FROM users", "-- This is a comment\nSELECT * FROM orders"},
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
}, },
{ {
name: "Single line comment with semicolon", name: "Single line comment with semicolon",
input: "SELECT * FROM users; -- Comment with; semicolon\nSELECT * FROM orders;", input: "SELECT * FROM users; -- Comment with; semicolon\nSELECT * FROM orders;",
expected: []string{"SELECT * FROM users", "-- Comment with; semicolon\nSELECT * FROM orders"},
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
}, },
{ {
name: "Multi-line comment", name: "Multi-line comment",
input: "SELECT * FROM users; /* Multi-line\ncomment */ SELECT * FROM orders;", input: "SELECT * FROM users; /* Multi-line\ncomment */ SELECT * FROM orders;",
expected: []string{"SELECT * FROM users", "/* Multi-line\ncomment */ SELECT * FROM orders"},
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
}, },
{ {
name: "Multi-line comment with semicolon", name: "Multi-line comment with semicolon",
input: "SELECT * FROM users; /* Comment with; semicolon */ SELECT * FROM orders;", input: "SELECT * FROM users; /* Comment with; semicolon */ SELECT * FROM orders;",
expected: []string{"SELECT * FROM users", "/* Comment with; semicolon */ SELECT * FROM orders"},
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
}, },
{ {
name: "Complex mixed case",
input: `SELECT 'test;string', "quoted;id" FROM users; -- Comment; here
name: "Complex mixed case",
input: `SELECT 'test;string', "quoted;id" FROM users; -- Comment; here
/* Another; comment */ /* Another; comment */
INSERT INTO users VALUES ('name''s value', "id""field");`, INSERT INTO users VALUES ('name''s value', "id""field");`,
expected: []string{ expected: []string{
`SELECT 'test;string', "quoted;id" FROM users`, `SELECT 'test;string', "quoted;id" FROM users`,
`-- Comment; here
/* Another; comment */
INSERT INTO users VALUES ('name''s value', "id""field")`,
`INSERT INTO users VALUES ('name''s value', "id""field")`,
}, },
}, },
{ {
@ -119,7 +117,7 @@ func TestSplitSQLStatements_EdgeCases(t *testing.T) {
{ {
name: "Nested comments are not supported but handled gracefully", name: "Nested comments are not supported but handled gracefully",
input: "SELECT * FROM users; /* Outer /* inner */ comment */ SELECT * FROM orders;", input: "SELECT * FROM users; /* Outer /* inner */ comment */ SELECT * FROM orders;",
expected: []string{"SELECT * FROM users", "/* Outer /* inner */ comment */ SELECT * FROM orders"},
expected: []string{"SELECT * FROM users", "comment */ SELECT * FROM orders"},
}, },
{ {
name: "Unterminated string (malformed SQL)", name: "Unterminated string (malformed SQL)",
@ -129,7 +127,7 @@ func TestSplitSQLStatements_EdgeCases(t *testing.T) {
{ {
name: "Unterminated comment (malformed SQL)", name: "Unterminated comment (malformed SQL)",
input: "SELECT * FROM users; /* unterminated comment", input: "SELECT * FROM users; /* unterminated comment",
expected: []string{"SELECT * FROM users", "/* unterminated comment"},
expected: []string{"SELECT * FROM users"},
}, },
{ {
name: "Multiple semicolons in quotes", name: "Multiple semicolons in quotes",

Loading…
Cancel
Save