Danilo Fragoso 6 ヶ月 前
コミット
94b84925a7
4 ファイル変更16 行追加28 行削除
  1. 12 0
      pkg/executor/executor.go
  2. 0 8
      pkg/httpserver/handler.go
  3. 4 17
      pkg/storage/kv.go
  4. 0 3
      pkg/storage/table.go

+ 12 - 0
pkg/executor/executor.go

@@ -2355,6 +2355,18 @@ func (e *Executor) evalAggregateExpr(expr parser.Expr, rows []storage.Row) (inte
 		if fn.Star {
 			return int64(len(rows)), nil
 		}
+		if fn.Distinct {
+			seen := make(map[interface{}]struct{})
+			for _, row := range rows {
+				if len(fn.Args) > 0 {
+					val, _ := e.evalExpr(fn.Args[0], row)
+					if val != nil {
+						seen[val] = struct{}{}
+					}
+				}
+			}
+			return int64(len(seen)), nil
+		}
 		count := int64(0)
 		for _, row := range rows {
 			if len(fn.Args) > 0 {

+ 0 - 8
pkg/httpserver/handler.go

@@ -4,7 +4,6 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
-	"log"
 	"net/http"
 	"strings"
 	"sync/atomic"
@@ -288,18 +287,13 @@ func (s *Server) handleSchemaTables(w http.ResponseWriter, r *http.Request) {
 	// Get database from X-Database header (trim whitespace)
 	dbName := strings.TrimSpace(r.Header.Get("X-Database"))
 
-	// Debug: Log the header value
-	log.Printf("[DEBUG] /schema/tables - X-Database header: %q", dbName)
-
 	_, schema, err := s.getExecutorForDatabase(dbName)
 	if err != nil {
 		writeError(w, http.StatusBadRequest, "DATABASE_ERROR", fmt.Sprintf("database not found: %s", dbName), nil)
 		return
 	}
 
-	// Debug: Log the actual database being used
 	actualDB := schema.GetDatabaseName()
-	log.Printf("[DEBUG] /schema/tables - Resolved to database: %q", actualDB)
 
 	tables, err := schema.ListTables()
 	if err != nil {
@@ -307,8 +301,6 @@ func (s *Server) handleSchemaTables(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	log.Printf("[DEBUG] /schema/tables - Found %d tables in database %q", len(tables), actualDB)
-
 	// Include the actual database name and requested name in the response for verification
 	resp := map[string]interface{}{
 		"database":           actualDB,

+ 4 - 17
pkg/storage/kv.go

@@ -53,7 +53,6 @@ func (c *KVClient) Write(key, value string) error {
 	defer c.mu.Unlock()
 
 	cmd := fmt.Sprintf("write %s|%s\r", key, value)
-	fmt.Printf("[DEBUG KV] Write command (len=%d): key=%q, value_len=%d\n", len(cmd), key, len(value))
 	if _, err := c.writer.WriteString(cmd); err != nil {
 		return fmt.Errorf("write command failed: %w", err)
 	}
@@ -66,7 +65,6 @@ func (c *KVClient) Write(key, value string) error {
 		return fmt.Errorf("read response failed: %w", err)
 	}
 
-	fmt.Printf("[DEBUG KV] Write response: %q\n", resp)
 	resp = strings.TrimSuffix(resp, "\r")
 	if resp != "success" {
 		return fmt.Errorf("write failed: %s", resp)
@@ -81,7 +79,6 @@ func (c *KVClient) Read(key string) (string, error) {
 	defer c.mu.Unlock()
 
 	cmd := fmt.Sprintf("read %s\r", key)
-	fmt.Printf("[DEBUG KV] Read command: %q\n", cmd)
 	if _, err := c.writer.WriteString(cmd); err != nil {
 		return "", fmt.Errorf("read command failed: %w", err)
 	}
@@ -94,7 +91,6 @@ func (c *KVClient) Read(key string) (string, error) {
 		return "", fmt.Errorf("read response failed: %w", err)
 	}
 
-	fmt.Printf("[DEBUG KV] Read raw response: %q\n", resp)
 	resp = strings.TrimSuffix(resp, "\r")
 	if resp == "error" {
 		return "", ErrKeyNotFound
@@ -135,7 +131,6 @@ func (c *KVClient) Reads(prefix string) ([]string, error) {
 	defer c.mu.Unlock()
 
 	cmd := fmt.Sprintf("reads %s\r", prefix)
-	fmt.Printf("[DEBUG KV] Reads command: %q\n", cmd)
 	if _, err := c.writer.WriteString(cmd); err != nil {
 		return nil, fmt.Errorf("reads command failed: %w", err)
 	}
@@ -145,29 +140,22 @@ func (c *KVClient) Reads(prefix string) ([]string, error) {
 
 	resp, err := c.reader.ReadString('\r')
 	if err != nil {
-		fmt.Printf("[DEBUG KV] Reads response error: %v\n", err)
 		return nil, fmt.Errorf("read response failed: %w", err)
 	}
 
-	fmt.Printf("[DEBUG KV] Reads raw response: %q (len=%d)\n", resp, len(resp))
 	resp = strings.TrimSuffix(resp, "\r")
 	if resp == "" {
-		fmt.Printf("[DEBUG KV] Reads: empty response, returning nil\n")
 		return nil, nil
 	}
 
 	values := strings.Split(resp, "\n")
-	fmt.Printf("[DEBUG KV] Reads: split into %d parts\n", len(values))
-	// Filter out empty strings
 	result := make([]string, 0, len(values))
-	for i, v := range values {
-		fmt.Printf("[DEBUG KV] Reads value[%d]: %q\n", i, v)
+	for _, v := range values {
 		if v != "" {
 			result = append(result, v)
 		}
 	}
 
-	fmt.Printf("[DEBUG KV] Reads: returning %d values\n", len(result))
 	return result, nil
 }
 
@@ -251,11 +239,10 @@ func (p *KVPool) Get() (*KVClient, error) {
 			}
 			return client, nil
 		}
-		// Create new connection if stale
-		return NewKVClient(p.addr)
-	default:
-		// Pool empty, create new connection
+		// Stale connection — replace with a fresh one
 		return NewKVClient(p.addr)
+	case <-time.After(30 * time.Second):
+		return nil, fmt.Errorf("kv pool timeout: no connection available after 30s")
 	}
 }
 

+ 0 - 3
pkg/storage/table.go

@@ -213,7 +213,6 @@ func (m *TableManager) Select(table string, filter func(Row) bool) ([]Row, error
 	err := m.pool.WithClient(func(c *KVClient) error {
 		var err error
 		values, err = c.Reads(prefix)
-		fmt.Printf("[DEBUG] Select: table=%s, database=%s, prefix=%s, values_count=%d\n", table, m.database, prefix, len(values))
 		return err
 	})
 	if err != nil {
@@ -224,7 +223,6 @@ func (m *TableManager) Select(table string, filter func(Row) bool) ([]Row, error
 	for _, data := range values {
 		var row Row
 		if err := json.Unmarshal([]byte(data), &row); err != nil {
-			fmt.Printf("[DEBUG] Select: failed to unmarshal row: %v\n", err)
 			continue // Skip invalid rows
 		}
 
@@ -233,7 +231,6 @@ func (m *TableManager) Select(table string, filter func(Row) bool) ([]Row, error
 		}
 	}
 
-	fmt.Printf("[DEBUG] Select: returning %d rows\n", len(rows))
 	return rows, nil
 }