Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(hive): fix 'use' statement in SQL Editor (#11426)
Azusain0e7c2c39c31c
diff --git a/backend/plugin/db/hive/hive.go b/backend/plugin/db/hive/hive.goindex 84aa3c5aa8..31f35c8301 100644--- a/backend/plugin/db/hive/hive.go+++ b/backend/plugin/db/hive/hive.go@@ -30,13 +30,15 @@ func init() {}type Driver struct {- config db.ConnectionConfig- ctx db.ConnectionContext- ConnPool *FixedConnPool+ config db.ConnectionConfig+ ctx db.ConnectionContext+ conn *gohive.Connection}var (- _ db.Driver = (*Driver)(nil)+ _ db.Driver = (*Driver)(nil)+ connPool *FixedConnPool+ numMaxConn = 5)func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) {@@ -61,32 +63,41 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionC// TODO(tommy): actually there are various kinds of authentication to choose among [SASL, KERBEROS, NOSASL, PLAIN SASL]// "NONE" refers to PLAIN SASL that doesn't need authentication.- pool, err := CreateHiveConnPool(5, ConnPoolConfig{- Host: config.Host,- Port: port,- Config: configuration,- }, PlainSASLConnFactory)+ if connPool == nil {+ pool, err := CreateHiveConnPool(numMaxConn, ConnPoolConfig{+ Host: config.Host,+ Port: port,+ Config: configuration,+ }, PlainSASLConnFactory)+ if err != nil {+ return nil, err+ }+ connPool = pool+ }++ newConn, err := connPool.Get(config.Database)if err != nil {- return nil, err+ return nil, errors.New("failed to get connection from pool")}- d.ConnPool = pool+ d.conn = newConnreturn d, nil}func (d *Driver) Close(_ context.Context) error {- err := d.ConnPool.Destroy()- if err != nil {- return errors.Errorf("faild to close connection")- }+ connPool.Put(d.conn)return nil}func (d *Driver) Ping(ctx context.Context) error {- if d.ConnPool == nil {+ if d.conn == nil {return errors.Errorf("no database connection established")}- if _, err := d.QueryConn(ctx, nil, "SELECT 1", &db.QueryContext{}); err != nil {+ cursor := d.conn.Cursor()+ defer cursor.Close()++ cursor.Exec(ctx, "SELECT 1")+ if cursor.Err != nil {return errors.Errorf("bad connection")}return nil@@ -104,21 +115,14 @@ func (*Driver) GetDB() *sql.DB {// Even in Hive's bucketed transaction table, all the statements are committed automatically by// the Hive server.func (d *Driver) Execute(ctx context.Context, statementsStr string, _ db.ExecuteOptions) (int64, error) {- if d.ConnPool == nil {+ if connPool == nil {return 0, errors.Errorf("no database connection established")}var affectedRows int64- conn, err := d.ConnPool.Get()- if err != nil {- return 0, err- }- cursor := conn.Cursor()- defer func() {- cursor.Close()- d.ConnPool.Put(conn)- }()+ cursor := d.conn.Cursor()+ defer cursor.Close()statements, err := base.SplitMultiSQL(storepb.Engine_HIVE, statementsStr)if err != nil {@@ -137,8 +141,8 @@ func (d *Driver) Execute(ctx context.Context, statementsStr string, _ db.Executereturn affectedRows, nil}-func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statementsStr string, queryCtx *db.QueryContext) ([]*v1pb.QueryResult, error) {- if d.ConnPool == nil {+func (*Driver) QueryConn(ctx context.Context, _ *sql.Conn, statementsStr string, queryCtx *db.QueryContext) ([]*v1pb.QueryResult, error) {+ if connPool == nil {return nil, errors.Errorf("no database connection established")}@@ -149,9 +153,15 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statementsStr strinreturn nil, errors.Wrapf(err, "failed to split statements")}- isAdminMode := false+ conn, err := connPool.Get("")+ if err != nil {+ return nil, errors.Wrapf(err, "failed to get connection from pool")+ }+if queryCtx != nil && !queryCtx.ReadOnly {- isAdminMode = true+ if err := SetRole(ctx, conn, "admin"); err != nil {+ return nil, err+ }}for _, statement := range statements {@@ -160,7 +170,7 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statementsStr strinstatementStr = fmt.Sprintf("%s LIMIT %d", statementStr, queryCtx.Limit)}- result, err := d.runSingleStatement(ctx, statementStr, isAdminMode)+ result, err := runSingleStatement(ctx, conn, statementStr)if err != nil {result.Error = err.Error()return nil, err@@ -173,10 +183,28 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statementsStr strin}// this func is used for admin mode in SQL Editor.-func (d *Driver) RunStatement(ctx context.Context, _ *sql.Conn, statement string) ([]*v1pb.QueryResult, error) {- results, err := d.QueryConn(ctx, nil, statement, &db.QueryContext{ReadOnly: false})+func (d *Driver) RunStatement(ctx context.Context, _ *sql.Conn, statementsStr string) ([]*v1pb.QueryResult, error) {+ var results []*v1pb.QueryResult++ statements, err := base.SplitMultiSQL(storepb.Engine_HIVE, statementsStr)if err != nil {- return nil, errors.Wrapf(err, "")+ return nil, errors.Wrapf(err, "failed to split statements")+ }++ if err := SetRole(ctx, d.conn, "admin"); err != nil {+ return nil, err+ }++ for _, statement := range statements {+ statementStr := strings.TrimRight(statement.Text, ";")++ result, err := runSingleStatement(ctx, d.conn, statementStr)+ if err != nil {+ result.Error = err.Error()+ return nil, err+ }++ results = append(results, result)}return results, nil}@@ -214,27 +242,14 @@ func parseValueType(value any, gohiveType string) (*v1pb.RowValue, error) {return &rowValue, nil}-func (d *Driver) runSingleStatement(ctx context.Context, statement string, readOnly bool) (*v1pb.QueryResult, error) {+func runSingleStatement(ctx context.Context, conn *gohive.Connection, statement string) (*v1pb.QueryResult, error) {var (result = v1pb.QueryResult{}startTime = time.Now())- conn, err := d.ConnPool.Get()- if err != nil {- return nil, errors.Wrapf(err, "failed to get connection from pool")- }cursor := conn.Cursor()- defer func() {- cursor.Close()- d.ConnPool.Put(conn)- }()-- if readOnly {- if err := SetRole(ctx, conn, "admin"); err != nil {- return nil, err- }- }+ defer cursor.Close()// run query.cursor.Execute(ctx, statement, false)diff --git a/backend/plugin/db/hive/hive_conn_pool.go b/backend/plugin/db/hive/hive_conn_pool.goindex a3c298c568..8b133daab0 100644--- a/backend/plugin/db/hive/hive_conn_pool.go+++ b/backend/plugin/db/hive/hive_conn_pool.go@@ -1,6 +1,8 @@package hiveimport (+ "context"+ "fmt""sync""github.com/beltran/gohive"@@ -8,16 +10,18 @@ import ()type ConnPool interface {- Get() (*gohive.Connection, error)+ Get(dbName string) (*gohive.Connection, error)Put(*gohive.Connection)Destroy() error}type FixedConnPool struct {- RWMutex sync.RWMutex- Connections chan *gohive.Connection- IsActivated bool- NumMaxConns int+ RWMutex sync.RWMutex+ Connections chan *gohive.Connection+ IsActivated bool+ NumMaxConns int+ PoolConfig ConnPoolConfig+ HiveConnFactory func(ConnPoolConfig) (*gohive.Connection, error)}var _ ConnPool = &FixedConnPool{}@@ -54,21 +58,45 @@ func CreateHiveConnPool(}return &FixedConnPool{- RWMutex: sync.RWMutex{},- Connections: conns,- IsActivated: true,- NumMaxConns: numMaxConn,+ RWMutex: sync.RWMutex{},+ Connections: conns,+ IsActivated: true,+ NumMaxConns: numMaxConn,+ HiveConnFactory: hiveConnFactory,+ PoolConfig: poolConfig,},nil}-func (pool *FixedConnPool) Get() (*gohive.Connection, error) {+func (pool *FixedConnPool) Get(dbName string) (*gohive.Connection, error) {pool.RWMutex.RLock()if !pool.IsActivated {return nil, errors.New("connection pool has been closed")}pool.RWMutex.RUnlock()- return <-pool.Connections, nil++ var conn *gohive.Connection++ select {+ case conn = <-pool.Connections:++ default:+ var err error+ conn, err = pool.HiveConnFactory(pool.PoolConfig)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to create new connection")+ }+ }++ if dbName != "" {+ cursor := conn.Cursor()+ cursor.Exec(context.Background(), fmt.Sprintf("use %s", dbName))+ if cursor.Err != nil {+ return nil, cursor.Err+ }+ }++ return conn, nil}func (pool *FixedConnPool) Put(conn *gohive.Connection) {@@ -78,7 +106,12 @@ func (pool *FixedConnPool) Put(conn *gohive.Connection) {return}pool.RWMutex.RUnlock()- pool.Connections <- conn+ select {+ case pool.Connections <- conn:+ return+ default:+ conn.Close()+ }}func (pool *FixedConnPool) Destroy() error {diff --git a/backend/plugin/db/hive/sync.go b/backend/plugin/db/hive/sync.goindex 208760f538..d668167566 100644--- a/backend/plugin/db/hive/sync.go+++ b/backend/plugin/db/hive/sync.go@@ -145,7 +145,7 @@ func (d *Driver) getTables(ctx context.Context, databaseName string) (continue}- tabInfo, err := d.getTableInfo(ctx, tabName, databaseName)+ tabInfo, err := getTableInfo(ctx, tabName, databaseName)if err != nil {return nil, nil, nil, nil, errors.Wrapf(err, "failed to describe table %s's type", tabName)}@@ -178,7 +178,7 @@ func (d *Driver) getTables(ctx context.Context, databaseName string) (var tableMetadata storepb.TableMetadata// indexes.- indexResults, err := d.getIndexesByTableName(ctx, tabName, databaseName)+ indexResults, err := getIndexesByTableName(ctx, tabName, databaseName)if err != nil {return nil, nil, nil, nil, errors.Wrapf(err, "failed to get index info from tab %s", tabName)}@@ -227,18 +227,20 @@ func (d *Driver) getRoles(ctx context.Context) ([]*storepb.InstanceRoleMetadata,return roleMetadata, nil}-func (d *Driver) getIndexesByTableName(ctx context.Context, tableName string, databaseName string) ([]*storepb.IndexMetadata, error) {+func getIndexesByTableName(ctx context.Context, tableName string, databaseName string) ([]*storepb.IndexMetadata, error) {var (indexMetadata []*storepb.IndexMetadata)- conn, err := d.ConnPool.Get()+ conn, err := connPool.Get("")if err != nil {return nil, err}cursor := conn.Cursor()- defer cursor.Close()- defer d.ConnPool.Put(conn)+ defer func() {+ cursor.Close()+ connPool.Put(conn)+ }()cursor.Execute(ctx, fmt.Sprintf("use %s", databaseName), false)if cursor.Err != nil {@@ -289,7 +291,7 @@ type TableInfo struct {comment string}-func (d *Driver) getTableInfo(ctx context.Context, tabName string, databaseName string) (+func getTableInfo(ctx context.Context, tabName string, databaseName string) (*TableInfo,error,) {@@ -302,14 +304,14 @@ func (d *Driver) getTableInfo(ctx context.Context, tabName string, databaseNamenumRows inthasReadColumns = false)- conn, err := d.ConnPool.Get()+ conn, err := connPool.Get("")if err != nil {return nil, err}cursor := conn.Cursor()defer func() {cursor.Close()- d.ConnPool.Put(conn)+ connPool.Put(conn)}()cursor.Exec(ctx, fmt.Sprintf("DESCRIBE FORMATTED `%s`.`%s`", databaseName, tabName))