Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit b5dfb07

Browse files
Improve test coverage for ast components in ast.go (#2355)
*Tests are added to extend coverage for statement, expression and source types and many of the ast helper functions *For those SQL language elements where ast exists but parsing is not implemented, test coverage is added to test only the ast correctness *Also, removed timestamp EPOCH related compiler code as they become unreachable after their ast equivalent were removed in a previous PR.
1 parent 52f9703 commit b5dfb07

File tree

4 files changed

+525
-46
lines changed

4 files changed

+525
-46
lines changed

‎sql3/parser/ast.go‎

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ func CloneStatement(stmt Statement) Statement {
181181
return stmt.Clone()
182182
case *InsertStatement:
183183
return stmt.Clone()
184+
case *BulkInsertStatement:
185+
return stmt.Clone()
184186
case *ReleaseStatement:
185187
return stmt.Clone()
186188
case *RollbackStatement:
@@ -305,6 +307,12 @@ func CloneExpr(expr Expr) Expr {
305307
return expr.Clone()
306308
case *Variable:
307309
return expr.Clone()
310+
case *SysVariable:
311+
return expr.Clone()
312+
case *DateLit:
313+
return expr.Clone()
314+
case *SetLiteralExpr:
315+
return expr.Clone()
308316
default:
309317
panic(fmt.Sprintf("invalid expr type: %T", expr))
310318
}
@@ -1237,10 +1245,8 @@ func (c *CacheTypeConstraint) String() string {
12371245
}
12381246

12391247
type TimeUnitConstraint struct {
1240-
TimeUnit Pos // position of TIMEUNIT keyword
1241-
Expr Expr // expression
1242-
Epoch Pos // position of TIMEUNIT keyword
1243-
EpochExpr Expr // expression
1248+
TimeUnit Pos // position of TIMEUNIT keyword
1249+
Expr Expr // expression
12441250
}
12451251

12461252
// Clone returns a deep copy of c.
@@ -1258,10 +1264,6 @@ func (c *TimeUnitConstraint) String() string {
12581264
var buf bytes.Buffer
12591265
buf.WriteString("TIMEUNIT ")
12601266
buf.WriteString(c.Expr.String())
1261-
if c.Epoch.IsValid() {
1262-
buf.WriteString(" EPOCH ")
1263-
buf.WriteString(c.EpochExpr.String())
1264-
}
12651267
return buf.String()
12661268
}
12671269

@@ -1690,8 +1692,10 @@ func IdentName(ident *Ident) string {
16901692
return ident.Name
16911693
}
16921694

1693-
// SysVariable represents built-in system variables that can be referenced in the sql for current date, current time and other potential pre-determinable values.
1694-
// In SQL these system provided data elements are referenced using keywords such as CURRENT_DATE & CURRENT_TIMESTAMP, etc.
1695+
// SysVariable represents built-in system variables that can be referenced in
1696+
// the sql for current date, current time and other potential system determinable
1697+
// values. In SQL these system provided data elements are referenced using
1698+
// keywords such as CURRENT_DATE & CURRENT_TIMESTAMP, etc.
16951699
type SysVariable struct {
16961700
NamePos Pos // variable position in sql
16971701
Token Token // parser token mapped to the variable's name/keyword
@@ -1711,11 +1715,11 @@ func (svar *SysVariable) Clone() *SysVariable {
17111715
return &other
17121716
}
17131717
func (svar *SysVariable) Name() string {
1714-
return tokens[svar.Token]
1718+
return svar.String()
17151719
}
17161720

17171721
func (svar *SysVariable) String() string {
1718-
return svar.Name()
1722+
return svar.Token.String()
17191723
}
17201724

17211725
func (svar *SysVariable) DataType() ExprDataType {
@@ -3115,8 +3119,6 @@ func (c *BulkInsertMapDefinition) String() string {
31153119
var buf bytes.Buffer
31163120
buf.WriteString(c.MapExpr.String())
31173121
buf.WriteString(" ")
3118-
buf.WriteString(c.Name.String())
3119-
buf.WriteString(" ")
31203122
buf.WriteString(c.Type.String())
31213123
return buf.String()
31223124
}
@@ -3202,35 +3204,65 @@ func (s *BulkInsertStatement) String() string {
32023204

32033205
buf.WriteString(" FROM ")
32043206
fmt.Fprintf(&buf, " %s", s.DataSource.String())
3205-
buf.WriteString(" WITH")
3207+
buf.WriteString(" WITH")
32063208

32073209
if s.Format != nil {
3208-
buf.WriteString("FORMAT ")
3210+
buf.WriteString("FORMAT ")
32093211
buf.WriteString(s.Format.String())
32103212
}
32113213

32123214
if s.Input != nil {
3213-
buf.WriteString("INPUT ")
3215+
buf.WriteString("INPUT ")
32143216
buf.WriteString(s.Input.String())
32153217
}
32163218

32173219
if s.HeaderRow != nil {
3218-
buf.WriteString("HEADER_ROW ")
3220+
buf.WriteString("HEADER_ROW ")
32193221
}
32203222

32213223
if s.BatchSize != nil {
3222-
buf.WriteString("BATCHSIZE ")
3224+
buf.WriteString("BATCHSIZE ")
32233225
buf.WriteString(s.BatchSize.String())
32243226
}
32253227

32263228
if s.RowsLimit != nil {
3227-
buf.WriteString("ROWSLIMIT ")
3229+
buf.WriteString("ROWSLIMIT ")
32283230
buf.WriteString(s.RowsLimit.String())
32293231
}
32303232

3233+
if s.AllowMissingValues != nil {
3234+
buf.WriteString(" ALLOW_MISSING_VALUES ")
3235+
}
32313236
return buf.String()
32323237
}
32333238

3239+
func (s *BulkInsertStatement) Clone() *BulkInsertStatement {
3240+
if s == nil {
3241+
return nil
3242+
}
3243+
other := *s
3244+
other.Table = s.Table.Clone()
3245+
other.Columns = cloneIdents(s.Columns)
3246+
other.TransformList = cloneExprs(s.TransformList)
3247+
other.DataSource = CloneExpr(s.DataSource)
3248+
other.BatchSize = CloneExpr(s.BatchSize)
3249+
other.RowsLimit = CloneExpr(s.RowsLimit)
3250+
other.Format = CloneExpr(s.Format)
3251+
other.Input = CloneExpr(s.Input)
3252+
other.HeaderRow = CloneExpr(s.HeaderRow)
3253+
other.AllowMissingValues = CloneExpr(s.AllowMissingValues)
3254+
other.MapList = cloneBulkInsertMap(s.MapList)
3255+
return &other
3256+
}
3257+
3258+
func cloneBulkInsertMap(s []*BulkInsertMapDefinition) []*BulkInsertMapDefinition {
3259+
other := make([]*BulkInsertMapDefinition, len(s))
3260+
for i := range s {
3261+
other[i] = s[i].Clone()
3262+
}
3263+
return other
3264+
}
3265+
32343266
type InsertStatement struct {
32353267
//WithClause *WithClause // clause containing CTEs
32363268

@@ -4139,6 +4171,7 @@ func (c *JoinClause) Clone() *JoinClause {
41394171
other.X = CloneSource(c.X)
41404172
other.Y = CloneSource(c.Y)
41414173
other.Constraint = CloneJoinConstraint(c.Constraint)
4174+
other.Operator = c.Operator.Clone()
41424175
return &other
41434176
}
41444177

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /