Skip to content

Commit

Permalink
Support SHOW CREATE STREAM SYNTAX
Browse files Browse the repository at this point in the history
  • Loading branch information
spongedu committed Dec 1, 2018
1 parent be298b5 commit 3fd0062
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
80 changes: 79 additions & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func (e *ShowExec) fetchAll() error {
return e.fetchShowColumns()
case ast.ShowCreateTable:
return e.fetchShowCreateTable()
case ast.ShowCreateStream:
return e.fetchShowCreateStream()
case ast.ShowCreateDatabase:
return e.fetchShowCreateDatabase()
case ast.ShowDatabases:
Expand Down Expand Up @@ -536,7 +538,9 @@ func (e *ShowExec) fetchShowCreateTable() error {
if err != nil {
return errors.Trace(err)
}

if tb.Meta().IsStream {
return errors.Trace(errors.New(fmt.Sprintf("table %s is a stream table. use 'show create stream %s' instead", tb.Meta().Name.L, tb.Meta().Name.L)))
}
sqlMode := e.ctx.GetSessionVars().SQLMode

// TODO: let the result more like MySQL.
Expand Down Expand Up @@ -722,6 +726,80 @@ func (e *ShowExec) fetchShowCreateTable() error {
return nil
}

func (e *ShowExec) fetchShowCreateStream() error {
tb, err := e.getTable()
if err != nil {
return errors.Trace(err)
}
if !tb.Meta().IsStream {
return errors.Trace(errors.New(fmt.Sprintf("table %s is not a stream table. use 'show create talbe %s' instead", tb.Meta().Name.L, tb.Meta().Name.L)))
}

sqlMode := e.ctx.GetSessionVars().SQLMode

// TODO: let the result more like MySQL.
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("CREATE STREAM %s (\n", escape(tb.Meta().Name, sqlMode)))
for i, col := range tb.Cols() {
buf.WriteString(fmt.Sprintf(" %s %s", escape(col.Name, sqlMode), col.GetTypeDesc()))
if col.IsGenerated() {
// It's a generated column.
buf.WriteString(fmt.Sprintf(" GENERATED ALWAYS AS (%s)", col.GeneratedExprString))
if col.GeneratedStored {
buf.WriteString(" STORED")
} else {
buf.WriteString(" VIRTUAL")
}
}
if mysql.HasAutoIncrementFlag(col.Flag) {
buf.WriteString(" NOT NULL AUTO_INCREMENT")
} else {
if mysql.HasNotNullFlag(col.Flag) {
buf.WriteString(" NOT NULL")
}
if !mysql.HasNoDefaultValueFlag(col.Flag) {
defaultValue := col.GetDefaultValue()
switch defaultValue {
case nil:
if !mysql.HasNotNullFlag(col.Flag) {
if col.Tp == mysql.TypeTimestamp {
buf.WriteString(" NULL")
}
buf.WriteString(" DEFAULT NULL")
}
case "CURRENT_TIMESTAMP":
buf.WriteString(" DEFAULT CURRENT_TIMESTAMP")
default:
defaultValStr := fmt.Sprintf("%v", defaultValue)
if col.Tp == mysql.TypeBit {
defaultValBinaryLiteral := types.BinaryLiteral(defaultValStr)
buf.WriteString(fmt.Sprintf(" DEFAULT %s", defaultValBinaryLiteral.ToBitLiteralString(true)))
} else {
buf.WriteString(fmt.Sprintf(" DEFAULT '%s'", format.OutputFormat(defaultValStr)))
}
}
}
if mysql.HasOnUpdateNowFlag(col.Flag) {
buf.WriteString(" ON UPDATE CURRENT_TIMESTAMP")
}
}
if len(col.Comment) > 0 {
buf.WriteString(fmt.Sprintf(" COMMENT '%s'", format.OutputFormat(col.Comment)))
}
if i != len(tb.Cols())-1 {
buf.WriteString(",\n")
}
}
buf.WriteString(") WITH (\n")
for k, v := range tb.Meta().StreamProperties {
buf.WriteString(fmt.Sprintf("\t\t'%s'='%s'\n", k, v))
}
buf.WriteString("\t\t);")

e.appendRow([]interface{}{tb.Meta().Name.O, buf.String()})
return nil
}

// fetchShowCreateDatabase composes show create database result.
func (e *ShowExec) fetchShowCreateDatabase() error {
db, ok := e.is.SchemaByName(e.DBName)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)

replace github.com/pingcap/parser => github.com/spongedu/parser v0.0.0-20181102150703-69c772388fc2
replace github.com/pingcap/parser => github.com/spongedu/parser v0.0.0-20181102150703-28da3dcd8827
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ github.com/spongedu/parser v0.0.0-20181102150703-05d67ec39b67 h1:Yb88YfrMzIZWbXi
github.com/spongedu/parser v0.0.0-20181102150703-05d67ec39b67/go.mod h1:pt5ToBPRXvy7eNveA9VEwpNj5AQiVGE4GaPxBeAmeUo=
github.com/spongedu/parser v0.0.0-20181102150703-263a8d3a093d h1:ympimjDjKqSoc4Y1XUYmc1ZbyYfC99jNdOJIeySLLHI=
github.com/spongedu/parser v0.0.0-20181102150703-263a8d3a093d/go.mod h1:pt5ToBPRXvy7eNveA9VEwpNj5AQiVGE4GaPxBeAmeUo=
github.com/spongedu/parser v0.0.0-20181102150703-28da3dcd8827 h1:0PQpmdl+tqYB+TlfTSgMjXHaLlQ6PrLEifBuPykWJys=
github.com/spongedu/parser v0.0.0-20181102150703-28da3dcd8827/go.mod h1:pt5ToBPRXvy7eNveA9VEwpNj5AQiVGE4GaPxBeAmeUo=
github.com/spongedu/parser v0.0.0-20181102150703-2ae733bc2c81 h1:rs+sXF/OicOiX//QfVIVDfBvsB5MzcE0coyHDWTHvmw=
github.com/spongedu/parser v0.0.0-20181102150703-2ae733bc2c81/go.mod h1:pt5ToBPRXvy7eNveA9VEwpNj5AQiVGE4GaPxBeAmeUo=
github.com/spongedu/parser v0.0.0-20181102150703-36c270493d6d h1:qEeHZ/BVNH8zriuiZKsbq9GzaXdx6Vyw7EWIgLKTcuk=
Expand Down
4 changes: 3 additions & 1 deletion planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ func (b *PlanBuilder) buildShow(show *ast.ShowStmt) (Plan, error) {
if p.DBName == "" {
return nil, ErrNoDB
}
case ast.ShowCreateTable:
case ast.ShowCreateTable, ast.ShowCreateStream:
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.AllPrivMask, show.Table.Schema.L, show.Table.Name.L, "")
}
p.SetSchema(buildShowSchema(show))
Expand Down Expand Up @@ -1631,6 +1631,8 @@ func buildShowSchema(s *ast.ShowStmt) (schema *expression.Schema) {
mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLonglong}
case ast.ShowCreateTable:
names = []string{"Table", "Create Table"}
case ast.ShowCreateStream:
names = []string{"Table", "Create Stream"}
case ast.ShowCreateDatabase:
names = []string{"Database", "Create Database"}
case ast.ShowGrants:
Expand Down

0 comments on commit 3fd0062

Please sign in to comment.