Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: add as of clause for SET and SELECT #1206

Merged
merged 11 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
_ Node = &FieldList{}
_ Node = &GroupByClause{}
_ Node = &HavingClause{}
_ Node = &AsOfClause{}
_ Node = &Join{}
_ Node = &Limit{}
_ Node = &OnCondition{}
Expand Down Expand Up @@ -262,6 +263,8 @@ type TableName struct {
IndexHints []*IndexHint
PartitionNames []model.CIStr
TableSample *TableSample
// AS OF is used to see the data as it was at a specific point in time.
AsOf *AsOfClause
}

// Restore implements Node interface.
Expand Down Expand Up @@ -306,6 +309,12 @@ func (n *TableName) Restore(ctx *format.RestoreCtx) error {
if err := n.restoreIndexHints(ctx); err != nil {
return err
}
if n.AsOf != nil {
ctx.WritePlain(" ")
if err := n.AsOf.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while splicing TableName.Asof")
}
}
if n.TableSample != nil {
ctx.WritePlain(" ")
if err := n.TableSample.Restore(ctx); err != nil {
Expand Down Expand Up @@ -502,6 +511,14 @@ func (n *TableSource) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord(" AS ")
ctx.WriteName(asName)
}

if tn.AsOf != nil {
ctx.WritePlain(" ")
if err := tn.AsOf.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore TableSource.AsOf")
}

}
if err := tn.restoreIndexHints(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore TableSource.Source.(*TableName).IndexHints")
}
Expand Down Expand Up @@ -3267,11 +3284,7 @@ func (m FulltextSearchModifier) WithQueryExpansion() bool {
return m&FulltextSearchModifierWithQueryExpansion == FulltextSearchModifierWithQueryExpansion
}

type TimestampBound struct {
Mode TimestampBoundMode
Timestamp ExprNode
}

// TODO: remove the TimestampBoundMode.
type TimestampBoundMode int

const (
Expand All @@ -3281,3 +3294,46 @@ const (
TimestampBoundReadTimestamp
TimestampBoundMinReadTimestamp
)

type TimestampReadModes int

const (
TimestampReadStrong TimestampReadModes = iota
TimestampReadBoundTimestamp
TimestampReadExactTimestamp
)

type TimestampBound struct {
Mode TimestampBoundMode
Timestamp ExprNode
}

type AsOfClause struct {
node
Mode TimestampReadModes
TsExpr ExprNode
}

// Restore implements Node interface.
func (n *AsOfClause) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("AS OF TIMESTAMP ")
if err := n.TsExpr.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore AsOfClause.Expr")
}
return nil
}

// Accept implements Node Accept interface.
func (n *AsOfClause) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*AsOfClause)
node, ok := n.TsExpr.Accept(v)
if !ok {
return n, false
}
n.TsExpr = node.(ExprNode)
return v.Leave(n)
}
22 changes: 22 additions & 0 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ func (s *Scanner) AppendError(err error) {
s.errs = append(s.errs, err)
}

func (s *Scanner) getNextToken() int {
r := s.r
tok, pos, lit := s.scan()
if tok == identifier {
tok = handleIdent(&yySymType{})
}
if tok == identifier {
if tok1 := s.isTokenIdentifier(lit, pos.Offset); tok1 != 0 {
tok = tok1
}
}
s.r = r
return tok
}

// Lex returns a token and store the token value in v.
// Scanner satisfies yyLexer interface.
// 0 and invalid are special token id this function would return:
Expand Down Expand Up @@ -165,6 +180,13 @@ func (s *Scanner) Lex(v *yySymType) int {
if tok == not && s.sqlMode.HasHighNotPrecedenceMode() {
return not2
}
if tok == as && s.getNextToken() == of {
_, pos, lit = s.scan()
v.ident = fmt.Sprintf("%s %s", v.ident, lit)
v.offset = pos.Offset
s.lastKeyword = asof
return asof
}

switch tok {
case intLit:
Expand Down
1 change: 1 addition & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ var tokenMap = map[string]int{
"NULLS": nulls,
"NUMERIC": numericType,
"NVARCHAR": nvarcharType,
"OF": of,
"OFF": off,
"OFFSET": offset,
"ON_DUPLICATE": onDuplicate,
Expand Down
Loading