-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from modelorona/clickhouse
add ClickHouse support
- Loading branch information
Showing
18 changed files
with
1,100 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package clickhouse | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/clidey/whodb/core/src/engine" | ||
) | ||
|
||
func (p *ClickHousePlugin) AddStorageUnit(config *engine.PluginConfig, schema string, storageUnit string, fields map[string]string) (bool, error) { | ||
conn, err := DB(config) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer conn.Close() | ||
|
||
// Extract engine settings from advanced configuration | ||
var engineSettings struct { | ||
engine string | ||
orderBy string | ||
partitionBy string | ||
settings map[string]string | ||
} | ||
|
||
engineSettings.engine = "MergeTree" // default engine | ||
engineSettings.orderBy = "tuple()" // default order | ||
engineSettings.settings = make(map[string]string) | ||
|
||
for _, record := range config.Credentials.Advanced { | ||
switch record.Key { | ||
case "Engine": | ||
engineSettings.engine = record.Value | ||
case "OrderBy": | ||
engineSettings.orderBy = record.Value | ||
case "PartitionBy": | ||
engineSettings.partitionBy = record.Value | ||
default: | ||
if strings.HasPrefix(record.Key, "Setting_") { | ||
key := strings.TrimPrefix(record.Key, "Setting_") | ||
engineSettings.settings[key] = record.Value | ||
} | ||
} | ||
} | ||
|
||
// Prepare columns | ||
var columns []string | ||
for field, fieldType := range fields { | ||
columns = append(columns, fmt.Sprintf("%s %s", field, fieldType)) | ||
} | ||
|
||
// Build the CREATE TABLE query | ||
query := fmt.Sprintf("CREATE TABLE %s.%s (\n\t%s\n) ENGINE = %s", | ||
schema, storageUnit, strings.Join(columns, ",\n\t"), engineSettings.engine) | ||
|
||
// Add ORDER BY clause | ||
if engineSettings.orderBy != "" { | ||
query += fmt.Sprintf("\nORDER BY %s", engineSettings.orderBy) | ||
} | ||
|
||
// Add PARTITION BY clause if specified | ||
if engineSettings.partitionBy != "" { | ||
query += fmt.Sprintf("\nPARTITION BY %s", engineSettings.partitionBy) | ||
} | ||
|
||
// Add engine settings if any | ||
if len(engineSettings.settings) > 0 { | ||
var settingsClauses []string | ||
for key, value := range engineSettings.settings { | ||
settingsClauses = append(settingsClauses, fmt.Sprintf("%s=%s", key, value)) | ||
} | ||
query += fmt.Sprintf("\nSETTINGS %s", strings.Join(settingsClauses, ", ")) | ||
} | ||
|
||
err = conn.Exec(context.Background(), query) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to create table: %w (query: %s)", err, query) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (p *ClickHousePlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values []engine.Record) (bool, error) { | ||
conn, err := DB(config) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer conn.Close() | ||
|
||
var columns []string | ||
var placeholders []string | ||
var args []interface{} | ||
|
||
for _, value := range values { | ||
columns = append(columns, value.Key) | ||
placeholders = append(placeholders, "?") | ||
args = append(args, value.Value) | ||
} | ||
|
||
query := fmt.Sprintf("INSERT INTO %s.%s (%s) VALUES (%s)", | ||
schema, storageUnit, strings.Join(columns, ", "), strings.Join(placeholders, ", ")) | ||
|
||
err = conn.Exec(context.Background(), query, args...) | ||
return err == nil, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package clickhouse | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/ClickHouse/clickhouse-go/v2/lib/driver" | ||
"strconv" | ||
|
||
"github.com/clidey/whodb/core/src/engine" | ||
) | ||
|
||
type ClickHousePlugin struct{} | ||
|
||
func (p *ClickHousePlugin) IsAvailable(config *engine.PluginConfig) bool { | ||
conn, err := DB(config) | ||
if err != nil { | ||
return false | ||
} | ||
defer conn.Close() | ||
return conn.Ping(context.Background()) == nil | ||
} | ||
|
||
func (p *ClickHousePlugin) GetDatabases(config *engine.PluginConfig) ([]string, error) { | ||
conn, err := DB(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer conn.Close() | ||
|
||
rows, err := conn.Query(context.Background(), "SHOW DATABASES") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
var databases []string | ||
for rows.Next() { | ||
var dbName string | ||
if err := rows.Scan(&dbName); err != nil { | ||
return nil, err | ||
} | ||
databases = append(databases, dbName) | ||
} | ||
|
||
return databases, nil | ||
} | ||
|
||
func (p *ClickHousePlugin) GetSchema(config *engine.PluginConfig) ([]string, error) { | ||
return []string{config.Credentials.Database}, nil | ||
} | ||
|
||
func (p *ClickHousePlugin) GetStorageUnits(config *engine.PluginConfig, schema string) ([]engine.StorageUnit, error) { | ||
conn, err := DB(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer conn.Close() | ||
|
||
query := fmt.Sprintf(` | ||
SELECT | ||
name, | ||
engine, | ||
total_rows, | ||
formatReadableSize(total_bytes) as total_size | ||
FROM system.tables | ||
WHERE database = '%s' | ||
`, schema) | ||
|
||
rows, err := conn.Query(context.Background(), query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
var storageUnits []engine.StorageUnit | ||
for rows.Next() { | ||
var name, tableType string | ||
var totalRows uint64 | ||
var totalSize string | ||
if err := rows.Scan(&name, &tableType, &totalRows, &totalSize); err != nil { | ||
return nil, err | ||
} | ||
|
||
attributes := []engine.Record{ | ||
{Key: "Table Type", Value: tableType}, | ||
{Key: "Total Size", Value: totalSize}, | ||
{Key: "Count", Value: strconv.FormatUint(totalRows, 10)}, | ||
} | ||
|
||
columns, err := getTableSchema(conn, schema, name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
attributes = append(attributes, columns...) | ||
|
||
storageUnits = append(storageUnits, engine.StorageUnit{ | ||
Name: name, | ||
Attributes: attributes, | ||
}) | ||
} | ||
|
||
return storageUnits, nil | ||
} | ||
|
||
func getTableSchema(conn driver.Conn, schema string, tableName string) ([]engine.Record, error) { | ||
query := fmt.Sprintf(` | ||
SELECT | ||
name, | ||
type | ||
FROM system.columns | ||
WHERE database = '%s' AND table = '%s' | ||
ORDER BY position | ||
`, schema, tableName) | ||
|
||
rows, err := conn.Query(context.Background(), query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
var result []engine.Record | ||
for rows.Next() { | ||
var name, dataType string | ||
if err := rows.Scan(&name, &dataType); err != nil { | ||
return nil, err | ||
} | ||
result = append(result, engine.Record{Key: name, Value: dataType}) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (p *ClickHousePlugin) Chat(config *engine.PluginConfig, schema string, model string, previousConversation string, query string) ([]*engine.ChatMessage, error) { | ||
// Implement chat functionality similar to MySQL implementation | ||
// You may need to adapt this based on ClickHouse specifics | ||
return nil, fmt.Errorf("chat functionality not implemented for ClickHouse") | ||
} | ||
|
||
func NewClickHousePlugin() *engine.Plugin { | ||
return &engine.Plugin{ | ||
Type: engine.DatabaseType_ClickHouse, | ||
PluginFunctions: &ClickHousePlugin{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package clickhouse | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ClickHouse/clickhouse-go/v2" | ||
"github.com/ClickHouse/clickhouse-go/v2/lib/driver" | ||
"github.com/clidey/whodb/core/src/common" | ||
"github.com/clidey/whodb/core/src/engine" | ||
) | ||
|
||
func DB(config *engine.PluginConfig) (driver.Conn, error) { | ||
port := common.GetRecordValueOrDefault(config.Credentials.Advanced, "Port", "9000") | ||
options := &clickhouse.Options{ | ||
Addr: []string{fmt.Sprintf("%s:%s", config.Credentials.Hostname, port)}, | ||
Auth: clickhouse.Auth{ | ||
Database: config.Credentials.Database, | ||
Username: config.Credentials.Username, | ||
Password: config.Credentials.Password, | ||
}, | ||
Settings: clickhouse.Settings{ | ||
"max_execution_time": 60, | ||
}, | ||
DialTimeout: time.Second * 30, | ||
MaxOpenConns: 5, | ||
MaxIdleConns: 5, | ||
ConnMaxLifetime: time.Hour, | ||
ConnOpenStrategy: clickhouse.ConnOpenInOrder, | ||
Compression: &clickhouse.Compression{ | ||
Method: clickhouse.CompressionLZ4, | ||
}, | ||
} | ||
|
||
return clickhouse.Open(options) | ||
} | ||
|
||
func getTableColumns(conn driver.Conn, schema, table string) ([]engine.Record, error) { | ||
query := fmt.Sprintf("DESCRIBE TABLE %s.%s", schema, table) | ||
rows, err := conn.Query(context.Background(), query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
var columns []engine.Record | ||
for rows.Next() { | ||
var name, typ, defaultType, defaultExpression string | ||
var comment *string | ||
if err := rows.Scan(&name, &typ, &defaultType, &defaultExpression, &comment); err != nil { | ||
return nil, err | ||
} | ||
columns = append(columns, engine.Record{Key: name, Value: typ}) | ||
} | ||
|
||
return columns, nil | ||
} |
Oops, something went wrong.