Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(hive): support dump (#11319)

Azusainf05f81d55867
diff --git a/backend/plugin/db/hive/dump.go b/backend/plugin/db/hive/dump.go
index 18c5caba52..5933357560 100644
--- a/backend/plugin/db/hive/dump.go
+++ b/backend/plugin/db/hive/dump.go
@@ -12,29 +12,46 @@ import (
const (
schemaStmtFmt = "" +
"--\n" +
- "-- %s structure for `%s`\n" +
+ "-- %s structure for %s\n" +
"--\n" +
"%s;\n"
- // TODO(tommy): more rigorous grammar analysis is needed.
- mtViewDDLFormat = "CREATE MATERIALIZED VIEW %s\nAS\n%s\n"
)
type IndexDDLOptions struct {
+ databaseName string
indexName string
tableName string
indexType string
colNames []string
isWithDeferredRebuild bool
- idxProperties []string
+ idxProperties map[string]string
indexTableName string
rowFmt string
storedAs string
storedBy string
location string
- tableProperties []string
+ tableProperties map[string]string
comment string
}
+type MaterializedViewDDLOptions struct {
+ databaseName string
+ mtViewName string
+ disableRewrite bool
+ comment string
+ partitionedOn []string
+ clusteredOn []string
+ distributedOn []string
+ sortedOn []string
+ rowFmt string
+ storedAs string
+ storedBy string
+ serdProperties map[string]string
+ location string
+ tblProperties map[string]string
+ as string
+}
+
func (d *Driver) Dump(ctx context.Context, _ io.Writer, _ bool) (string, error) {
instanceMetadata, err := d.SyncInstance(ctx)
if err != nil {
@@ -59,11 +76,9 @@ func (d *Driver) Dump(ctx context.Context, _ io.Writer, _ bool) (string, error)
// dump indexes.
for _, index := range table.Indexes {
- var (
- tabNameWithDB = fmt.Sprintf("`%s`.`%s`", database.Name, table.Name)
- )
// TODO(tommy): get more index info from SyncInstance.
indexDDL, err := genIndexDDL(&IndexDDLOptions{
+ databaseName: database.Name,
indexName: index.Name,
tableName: table.Name,
indexType: index.Type,
@@ -82,7 +97,7 @@ func (d *Driver) Dump(ctx context.Context, _ io.Writer, _ bool) (string, error)
return "", errors.Wrapf(err, "failed to generate DDL for index %s", index.Name)
}
- _, _ = dumpStrBuilder.WriteString(fmt.Sprintf(schemaStmtFmt, "INDEX", tabNameWithDB, indexDDL))
+ _, _ = dumpStrBuilder.WriteString(fmt.Sprintf(schemaStmtFmt, "INDEX", fmt.Sprintf("`%s`", index.Name), indexDDL))
}
_, _ = dumpStrBuilder.WriteString(tabDDL)
}
@@ -105,10 +120,31 @@ func (d *Driver) Dump(ctx context.Context, _ io.Writer, _ bool) (string, error)
_, _ = dumpStrBuilder.WriteString(viewDDL)
}
+ // TODO(tommy): get more mt view info from SyncInstance.
// dump materialized views.
for _, mtView := range database.MaterializedViews {
- mtViewDDL := fmt.Sprintf(mtViewDDLFormat, mtView.Name, mtView.Definition)
- _, _ = dumpStrBuilder.WriteString(fmt.Sprintf(schemaStmtFmt, "MATERIALIZED VIEW", mtView.Name, mtViewDDL))
+ mtViewDDL, err := genMaterializedViewDDL(&MaterializedViewDDLOptions{
+ databaseName: database.Name,
+ mtViewName: mtView.Name,
+ disableRewrite: false,
+ comment: mtView.Comment,
+ partitionedOn: nil,
+ clusteredOn: nil,
+ distributedOn: nil,
+ sortedOn: nil,
+ rowFmt: "",
+ storedAs: "",
+ storedBy: "",
+ serdProperties: nil,
+ location: "",
+ tblProperties: nil,
+ as: mtView.Definition,
+ })
+ if err != nil {
+ return "", errors.Wrapf(err, "failed to generate DDL for materialized view %s", mtView.Name)
+ }
+
+ _, _ = dumpStrBuilder.WriteString(fmt.Sprintf(schemaStmtFmt, "MATERIALIZED VIEW", fmt.Sprintf("`%s`.`%s`", database.Name, mtView.Name), mtViewDDL))
}
}
@@ -122,10 +158,11 @@ func (*Driver) Restore(_ context.Context, _ io.Reader) error {
// This function shows DDLs for creating certain type of schema [VIEW, DATABASE, TABLE].
func (d *Driver) showCreateSchemaDDL(ctx context.Context, schemaType string, schemaName string, belongTo string) (string, error) {
+ schemaName = fmt.Sprintf("`%s`", schemaName)
// 'SHOW CREATE TABLE' can also be used for dumping views.
- queryStatement := fmt.Sprintf("SHOW CREATE %s `%s`", schemaType, schemaName)
+ queryStatement := fmt.Sprintf("SHOW CREATE %s %s", schemaType, schemaName)
if schemaType == "VIEW" {
- queryStatement = fmt.Sprintf("SHOW CREATE TABLE `%s`", schemaName)
+ queryStatement = fmt.Sprintf("SHOW CREATE TABLE %s", schemaName)
}
schemaDDLResults, err := d.QueryConn(ctx, nil, queryStatement, nil)
@@ -141,27 +178,22 @@ func (d *Driver) showCreateSchemaDDL(ctx context.Context, schemaType string, sch
// rename table to format: [DatabaseName].[TableName].
newSchemaName := schemaName
if belongTo != "" {
+ belongTo = fmt.Sprintf("`%s`", belongTo)
newSchemaName = fmt.Sprintf("%s.%s", belongTo, schemaName)
schemaDDL = strings.Replace(schemaDDL, schemaName, newSchemaName, 1)
}
- return fmt.Sprintf(schemaStmtFmt,
- schemaType, newSchemaName, schemaDDL), nil
+ return fmt.Sprintf(schemaStmtFmt, schemaType, newSchemaName, schemaDDL), nil
}
func genIndexDDL(opts *IndexDDLOptions) (string, error) {
var builder strings.Builder
// index name, table name.
- _, _ = builder.WriteString(fmt.Sprintf("CREATE INDEX `%s`\nON TABLE `%s` (\n", opts.indexName, opts.tableName))
+ _, _ = builder.WriteString(fmt.Sprintf("CREATE INDEX `%s`\nON TABLE %s ", opts.indexName, fmt.Sprintf("`%s`.`%s`", opts.databaseName, opts.tableName)))
// column names.
- for idx, colName := range opts.colNames {
- _, _ = builder.WriteString(fmt.Sprintf(" `%s`", colName))
- if idx != len(opts.colNames)-1 {
- _, _ = builder.WriteString(",\n")
- }
- }
+ formatColumn(&builder, opts.colNames)
// index type.
_, _ = builder.WriteString(fmt.Sprintf(")\nAS '%s'\n", opts.indexType))
@@ -172,15 +204,9 @@ func genIndexDDL(opts *IndexDDLOptions) (string, error) {
}
// index properties.
- if len(opts.idxProperties) != 0 {
+ if opts.idxProperties != nil {
_, _ = builder.WriteString("IDXPROPERTIES ")
- for idx, prop := range opts.idxProperties {
- _, _ = builder.WriteString(prop)
- if idx != len(opts.idxProperties)-1 {
- _, _ = builder.WriteString(", ")
- }
- }
- _, _ = builder.WriteRune('\n')
+ formatKVPair(&builder, opts.tableProperties)
}
// index table.
@@ -212,19 +238,109 @@ func genIndexDDL(opts *IndexDDLOptions) (string, error) {
// table properties.
if opts.tableProperties != nil {
_, _ = builder.WriteString("TBLPROPERTIES (")
- for idx, prop := range opts.tableProperties {
- _, _ = builder.WriteString(prop)
- if idx != len(opts.tableProperties)-1 {
- _, _ = builder.WriteString(", ")
- }
- }
- _, _ = builder.WriteString(")\n")
+ formatKVPair(&builder, opts.tableProperties)
}
// comment.
if opts.comment != "" {
- _, _ = builder.WriteString(fmt.Sprintf("COMMENT \"%s\"\n", opts.comment))
+ _, _ = builder.WriteString(fmt.Sprintf("COMMENT '%s'\n", opts.comment))
+ }
+
+ return builder.String(), nil
+}
+
+func genMaterializedViewDDL(opts *MaterializedViewDDLOptions) (string, error) {
+ var builder strings.Builder
+ viewNameWithDB := fmt.Sprintf("`%s`.`%s`", opts.databaseName, opts.mtViewName)
+
+ // mtView name.
+ _, _ = builder.WriteString(fmt.Sprintf("CREATE MATERIALIZED VIEW %s\n", viewNameWithDB))
+
+ // disable rewrite.
+ if opts.disableRewrite {
+ _, _ = builder.WriteString("DISABLE REWRITE\n")
+ }
+
+ // comment.
+ if opts.comment != "" {
+ _, _ = builder.WriteString(fmt.Sprintf("COMMENT '%s'\n", opts.comment))
+ }
+
+ // partitioned on.
+ if opts.partitionedOn != nil {
+ _, _ = builder.WriteString("PARTITIONED ON ")
+ formatColumn(&builder, opts.partitionedOn)
+ }
+
+ if opts.clusteredOn != nil && opts.distributedOn != nil {
+ return "", errors.New("keywords 'CLUSTERED ON' and 'DISTRIBUTED ON' cannot appear at the same time")
+ } else if opts.clusteredOn != nil {
+ // clusteredOn
+ _, _ = builder.WriteString("CLUSTERED ON ")
+ formatColumn(&builder, opts.clusteredOn)
+ } else if opts.distributedOn != nil && opts.sortedOn != nil {
+ // distributed on.
+ _, _ = builder.WriteString("DISTRIBUTED ON ")
+ formatColumn(&builder, opts.distributedOn)
+
+ // sorted on.
+ _, _ = builder.WriteString("SORTED ON ")
+ formatColumn(&builder, opts.sortedOn)
+ }
+
+ // stored as or stored by.
+ if opts.storedAs != "" && opts.storedBy != "" {
+ return "", errors.New("keywords 'STORED AS' and 'STORED BY' cannot appear at the same time")
+ } else if opts.storedAs != "" {
+ _, _ = builder.WriteString(fmt.Sprintf("STORED AS %s\n", opts.storedAs))
+ } else if opts.storedBy != "" {
+ _, _ = builder.WriteString(fmt.Sprintf("STORED BY '%s'\n", opts.storedBy))
+
+ // if with serde properties.
+ if opts.serdProperties != nil {
+ _, _ = builder.WriteString("WITH SERDEPROPERTIES ")
+ formatKVPair(&builder, opts.serdProperties)
+ }
+ }
+
+ // location.
+ if opts.location != "" {
+ _, _ = builder.WriteString(opts.location)
+ _, _ = builder.WriteRune('\n')
}
+ // table properties.
+ if opts.tblProperties != nil {
+ _, _ = builder.WriteString("TBLPROPERTIES ")
+ formatKVPair(&builder, opts.tblProperties)
+ }
+
+ // as.
+ _, _ = builder.WriteString(fmt.Sprintf("AS %s \n", opts.as))
+
return builder.String(), nil
}
+
+func formatColumn(builder *strings.Builder, columnNames []string) {
+ _, _ = builder.WriteString("(\n")
+ for idx, colName := range columnNames {
+ _, _ = builder.WriteString(fmt.Sprintf(" `%s`", colName))
+ if idx != len(columnNames)-1 {
+ _, _ = builder.WriteString(",\n")
+ }
+ }
+ _, _ = builder.WriteString(")\n")
+}
+
+func formatKVPair(builder *strings.Builder, kvMap map[string]string) {
+ _, _ = builder.WriteString("(\n")
+ index := 0
+ for key, value := range kvMap {
+ _, _ = builder.WriteString(fmt.Sprintf(" '%s' = '%s'", key, value))
+ if index != len(kvMap)-1 {
+ _, _ = builder.WriteString(",\n")
+ }
+ index++
+ }
+ _, _ = builder.WriteString(")\n")
+}
diff --git a/backend/plugin/db/hive/hive.go b/backend/plugin/db/hive/hive.go
index 19634ef876..a23594ef0c 100644
--- a/backend/plugin/db/hive/hive.go
+++ b/backend/plugin/db/hive/hive.go
@@ -21,6 +21,14 @@ import (
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
+func hiveDriverFunc(db.DriverConfig) db.Driver {
+ return &Driver{}
+}
+
+func init() {
+ db.Register(storepb.Engine_HIVE, hiveDriverFunc)
+}
+
type Driver struct {
config db.ConnectionConfig
ctx db.ConnectionContext
diff --git a/backend/plugin/db/hive/sync.go b/backend/plugin/db/hive/sync.go
index d2556135f6..5cd22efcb2 100644
--- a/backend/plugin/db/hive/sync.go
+++ b/backend/plugin/db/hive/sync.go
@@ -290,7 +290,11 @@ func (d *Driver) getTableInfo(ctx context.Context, tabName string) (
if rowMap["data_type"] == nil {
typeStr = ""
} else {
- typeStr = strings.ReplaceAll(rowMap["data_type"].(string), " ", "")
+ ok := false
+ typeStr, ok = rowMap["data_type"].(string)
+ if !ok {
+ return "", nil, 0, "", errors.New("type assertions fails")
+ }
}
if rowMap["comment"] == nil {
commentStr = ""
@@ -298,7 +302,7 @@ func (d *Driver) getTableInfo(ctx context.Context, tabName string) (
commentStr = strings.ReplaceAll(rowMap["comment"].(string), " ", "")
}
- // process table type
+ // process table type.
if strings.Contains(colName, "Table Type") {
tabType = strings.ReplaceAll(typeStr, " ", "")
} else if colName == "" {
diff --git a/backend/server/ultimate.go b/backend/server/ultimate.go
index 7d7eab7f0f..703475c7a0 100644
--- a/backend/server/ultimate.go
+++ b/backend/server/ultimate.go
@@ -6,6 +6,7 @@ import (
// Drivers.
_ "github.com/bytebase/bytebase/backend/plugin/db/clickhouse"
_ "github.com/bytebase/bytebase/backend/plugin/db/dm"
+ _ "github.com/bytebase/bytebase/backend/plugin/db/hive"
_ "github.com/bytebase/bytebase/backend/plugin/db/mssql"
_ "github.com/bytebase/bytebase/backend/plugin/db/oracle"
_ "github.com/bytebase/bytebase/backend/plugin/db/redis"