|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/posthog/duckgres/server/iceberg" |
| 9 | + "github.com/posthog/duckgres/server/sqlcore" |
| 10 | +) |
| 11 | + |
| 12 | +type dropSchemaCascadeTarget struct { |
| 13 | + Catalog string |
| 14 | + Schema string |
| 15 | +} |
| 16 | + |
| 17 | +func parseDropSchemaCascadeTarget(query string) (dropSchemaCascadeTarget, bool) { |
| 18 | + s := strings.TrimSpace(sqlcore.StripLeadingComments(query)) |
| 19 | + s = strings.TrimSpace(strings.TrimSuffix(s, ";")) |
| 20 | + |
| 21 | + pos, ok := consumeSQLKeyword(s, 0, "DROP") |
| 22 | + if !ok { |
| 23 | + return dropSchemaCascadeTarget{}, false |
| 24 | + } |
| 25 | + pos, ok = consumeSQLKeyword(s, pos, "SCHEMA") |
| 26 | + if !ok { |
| 27 | + return dropSchemaCascadeTarget{}, false |
| 28 | + } |
| 29 | + |
| 30 | + target := dropSchemaCascadeTarget{} |
| 31 | + if next, ok := consumeSQLKeyword(s, pos, "IF"); ok { |
| 32 | + next, ok = consumeSQLKeyword(s, next, "EXISTS") |
| 33 | + if !ok { |
| 34 | + return dropSchemaCascadeTarget{}, false |
| 35 | + } |
| 36 | + pos = next |
| 37 | + } |
| 38 | + |
| 39 | + targetSQL, ok := trimTrailingSQLKeyword(s[pos:], "CASCADE") |
| 40 | + if !ok { |
| 41 | + return dropSchemaCascadeTarget{}, false |
| 42 | + } |
| 43 | + parts, ok := sqlcore.ParseQualifiedIdentifier(targetSQL) |
| 44 | + if !ok || len(parts) == 0 || len(parts) > 2 { |
| 45 | + return dropSchemaCascadeTarget{}, false |
| 46 | + } |
| 47 | + if len(parts) == 1 { |
| 48 | + target.Schema = parts[0] |
| 49 | + } else { |
| 50 | + target.Catalog = parts[0] |
| 51 | + target.Schema = parts[1] |
| 52 | + } |
| 53 | + if target.Schema == "" || target.Catalog == "" && len(parts) == 2 { |
| 54 | + return dropSchemaCascadeTarget{}, false |
| 55 | + } |
| 56 | + return target, true |
| 57 | +} |
| 58 | + |
| 59 | +func consumeSQLKeyword(s string, pos int, keyword string) (int, bool) { |
| 60 | + pos = skipSQLSpace(s, pos) |
| 61 | + end := pos + len(keyword) |
| 62 | + if end > len(s) || !strings.EqualFold(s[pos:end], keyword) { |
| 63 | + return pos, false |
| 64 | + } |
| 65 | + if end < len(s) && isSQLIdentChar(s[end]) { |
| 66 | + return pos, false |
| 67 | + } |
| 68 | + return end, true |
| 69 | +} |
| 70 | + |
| 71 | +func trimTrailingSQLKeyword(s string, keyword string) (string, bool) { |
| 72 | + s = strings.TrimSpace(s) |
| 73 | + end := len(s) - len(keyword) |
| 74 | + if end < 0 || !strings.EqualFold(s[end:], keyword) { |
| 75 | + return "", false |
| 76 | + } |
| 77 | + if end > 0 && !isSQLSpace(s[end-1]) { |
| 78 | + return "", false |
| 79 | + } |
| 80 | + target := strings.TrimSpace(s[:end]) |
| 81 | + return target, target != "" |
| 82 | +} |
| 83 | + |
| 84 | +func skipSQLSpace(s string, pos int) int { |
| 85 | + for pos < len(s) && isSQLSpace(s[pos]) { |
| 86 | + pos++ |
| 87 | + } |
| 88 | + return pos |
| 89 | +} |
| 90 | + |
| 91 | +func isSQLSpace(ch byte) bool { |
| 92 | + switch ch { |
| 93 | + case ' ', '\t', '\n', '\r', '\f': |
| 94 | + return true |
| 95 | + default: |
| 96 | + return false |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func isSQLIdentChar(ch byte) bool { |
| 101 | + return ch == '_' || ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z' |
| 102 | +} |
| 103 | + |
| 104 | +func isIcebergDropSchemaCascadeUnsupported(err error) bool { |
| 105 | + if err == nil { |
| 106 | + return false |
| 107 | + } |
| 108 | + return strings.Contains(err.Error(), "DROP SCHEMA <schema_name> CASCADE is not supported for Iceberg schemas currently") |
| 109 | +} |
| 110 | + |
| 111 | +func (c *clientConn) dropIcebergSchemaCascade(ctx context.Context, query string) (ExecResult, error) { |
| 112 | + target, ok := parseDropSchemaCascadeTarget(query) |
| 113 | + if !ok { |
| 114 | + return nil, fmt.Errorf("not a DROP SCHEMA CASCADE statement") |
| 115 | + } |
| 116 | + |
| 117 | + catalog := target.Catalog |
| 118 | + if catalog == "" { |
| 119 | + defaultCatalog, err := c.currentSearchPathCatalog(ctx) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + catalog = defaultCatalog |
| 124 | + } |
| 125 | + if !strings.EqualFold(catalog, iceberg.CatalogName) { |
| 126 | + return nil, fmt.Errorf("DROP SCHEMA CASCADE fallback only supports iceberg catalog, got %q", catalog) |
| 127 | + } |
| 128 | + |
| 129 | + rows, err := c.executor.QueryContext(ctx, ` |
| 130 | + SELECT table_name |
| 131 | + FROM information_schema.tables |
| 132 | + WHERE table_catalog = 'iceberg' |
| 133 | + AND table_schema = ? |
| 134 | + ORDER BY table_name |
| 135 | + `, target.Schema) |
| 136 | + if err != nil { |
| 137 | + return nil, fmt.Errorf("list iceberg schema tables: %w", err) |
| 138 | + } |
| 139 | + defer func() { _ = rows.Close() }() |
| 140 | + |
| 141 | + var tables []string |
| 142 | + for rows.Next() { |
| 143 | + var table string |
| 144 | + if err := rows.Scan(&table); err != nil { |
| 145 | + return nil, fmt.Errorf("scan iceberg schema table: %w", err) |
| 146 | + } |
| 147 | + tables = append(tables, table) |
| 148 | + } |
| 149 | + if err := rows.Err(); err != nil { |
| 150 | + return nil, fmt.Errorf("list iceberg schema tables: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + for _, table := range tables { |
| 154 | + dropTable := fmt.Sprintf("DROP TABLE IF EXISTS %s.%s.%s", |
| 155 | + sqlcore.QuoteIdentifier(iceberg.CatalogName), |
| 156 | + sqlcore.QuoteIdentifier(target.Schema), |
| 157 | + sqlcore.QuoteIdentifier(table), |
| 158 | + ) |
| 159 | + if _, err := c.executor.ExecContext(ctx, dropTable); err != nil { |
| 160 | + return nil, fmt.Errorf("drop iceberg table %s.%s: %w", target.Schema, table, err) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + dropSchema := fmt.Sprintf("DROP SCHEMA IF EXISTS %s.%s", |
| 165 | + sqlcore.QuoteIdentifier(iceberg.CatalogName), |
| 166 | + sqlcore.QuoteIdentifier(target.Schema), |
| 167 | + ) |
| 168 | + return c.executor.ExecContext(ctx, dropSchema) |
| 169 | +} |
| 170 | + |
| 171 | +func (c *clientConn) currentSearchPathCatalog(ctx context.Context) (string, error) { |
| 172 | + rows, err := c.executor.QueryContext(ctx, ` |
| 173 | + SELECT value |
| 174 | + FROM duckdb_settings() |
| 175 | + WHERE name = 'search_path' |
| 176 | + `) |
| 177 | + if err != nil { |
| 178 | + return "", fmt.Errorf("read search_path: %w", err) |
| 179 | + } |
| 180 | + defer func() { _ = rows.Close() }() |
| 181 | + if !rows.Next() { |
| 182 | + return "", rows.Err() |
| 183 | + } |
| 184 | + var searchPath string |
| 185 | + if err := rows.Scan(&searchPath); err != nil { |
| 186 | + return "", fmt.Errorf("scan search_path: %w", err) |
| 187 | + } |
| 188 | + if err := rows.Err(); err != nil { |
| 189 | + return "", err |
| 190 | + } |
| 191 | + return sqlcore.CatalogFromSearchPath(searchPath), nil |
| 192 | +} |
0 commit comments