Skip to content

Commit

Permalink
sql: add support for optional frame exclusion
Browse files Browse the repository at this point in the history
Adds support for optional frame exclusion clause in the specification
of window frames.

Release note (sql change): CockroachDB now supports optinal frame
exclusion clause in the specification of window frames.

Release note: None
  • Loading branch information
yuzefovich committed Aug 5, 2019
1 parent 836c28d commit bbaaa51
Show file tree
Hide file tree
Showing 27 changed files with 1,817 additions and 290 deletions.
16 changes: 13 additions & 3 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ unreserved_keyword ::=
| 'ENCODING'
| 'ENUM'
| 'ESCAPE'
| 'EXCLUDE'
| 'EXECUTE'
| 'EXPERIMENTAL'
| 'EXPERIMENTAL_AUDIT'
Expand Down Expand Up @@ -703,6 +704,7 @@ unreserved_keyword ::=
| 'OPTION'
| 'OPTIONS'
| 'ORDINALITY'
| 'OTHERS'
| 'OVER'
| 'OWNED'
| 'PARENT'
Expand Down Expand Up @@ -789,6 +791,7 @@ unreserved_keyword ::=
| 'TEMPORARY'
| 'TESTING_RELOCATE'
| 'TEXT'
| 'TIES'
| 'TRACE'
| 'TRANSACTION'
| 'TRIGGER'
Expand Down Expand Up @@ -2287,9 +2290,9 @@ opt_partition_clause ::=
|

opt_frame_clause ::=
'RANGE' frame_extent
| 'ROWS' frame_extent
| 'GROUPS' frame_extent
'RANGE' frame_extent opt_frame_exclusion
| 'ROWS' frame_extent opt_frame_exclusion
| 'GROUPS' frame_extent opt_frame_exclusion
|

extract_list ::=
Expand Down Expand Up @@ -2331,6 +2334,13 @@ frame_extent ::=
frame_bound
| 'BETWEEN' frame_bound 'AND' frame_bound

opt_frame_exclusion ::=
'EXCLUDE' 'CURRENT' 'ROW'
| 'EXCLUDE' 'GROUP'
| 'EXCLUDE' 'TIES'
| 'EXCLUDE' 'NO' 'OTHERS'
|

extract_arg ::=
'identifier'
| 'YEAR'
Expand Down
37 changes: 36 additions & 1 deletion pkg/sql/distsqlpb/processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ func (spec *WindowerSpec_Frame_BoundType) initFromAST(bt tree.WindowFrameBoundTy
}
}

func (spec *WindowerSpec_Frame_Exclusion) initFromAST(e tree.WindowFrameExclusion) {
switch e {
case tree.NoExclusion:
*spec = WindowerSpec_Frame_NO_EXCLUSION
case tree.ExcludeCurrentRow:
*spec = WindowerSpec_Frame_EXCLUDE_CURRENT_ROW
case tree.ExcludeGroup:
*spec = WindowerSpec_Frame_EXCLUDE_GROUP
case tree.ExcludeTies:
*spec = WindowerSpec_Frame_EXCLUDE_TIES
default:
panic("unexpected WindowerFrameExclusion")
}
}

// If offset exprs are present, we evaluate them and save the encoded results
// in the spec.
func (spec *WindowerSpec_Frame_Bounds) initFromAST(
Expand Down Expand Up @@ -190,6 +205,7 @@ func isNegative(evalCtx *tree.EvalContext, offset tree.Datum) bool {
// offset expressions if present in the frame.
func (spec *WindowerSpec_Frame) InitFromAST(f *tree.WindowFrame, evalCtx *tree.EvalContext) error {
spec.Mode.initFromAST(f.Mode)
spec.Exclusion.initFromAST(f.Exclusion)
return spec.Bounds.initFromAST(f.Bounds, f.Mode, evalCtx)
}

Expand Down Expand Up @@ -223,6 +239,21 @@ func (spec WindowerSpec_Frame_BoundType) convertToAST() tree.WindowFrameBoundTyp
}
}

func (spec WindowerSpec_Frame_Exclusion) convertToAST() tree.WindowFrameExclusion {
switch spec {
case WindowerSpec_Frame_NO_EXCLUSION:
return tree.NoExclusion
case WindowerSpec_Frame_EXCLUDE_CURRENT_ROW:
return tree.ExcludeCurrentRow
case WindowerSpec_Frame_EXCLUDE_GROUP:
return tree.ExcludeGroup
case WindowerSpec_Frame_EXCLUDE_TIES:
return tree.ExcludeTies
default:
panic("unexpected WindowerSpec_Frame_Exclusion")
}
}

// convertToAST produces tree.WindowFrameBounds based on
// WindowerSpec_Frame_Bounds. Note that it might not be fully equivalent to
// original - if offsetExprs were present in original tree.WindowFrameBounds,
Expand All @@ -239,5 +270,9 @@ func (spec WindowerSpec_Frame_Bounds) convertToAST() tree.WindowFrameBounds {

// ConvertToAST produces a tree.WindowFrame given a WindoweSpec_Frame.
func (spec *WindowerSpec_Frame) ConvertToAST() *tree.WindowFrame {
return &tree.WindowFrame{Mode: spec.Mode.convertToAST(), Bounds: spec.Bounds.convertToAST()}
return &tree.WindowFrame{
Mode: spec.Mode.convertToAST(),
Bounds: spec.Bounds.convertToAST(),
Exclusion: spec.Exclusion.convertToAST(),
}
}
Loading

0 comments on commit bbaaa51

Please sign in to comment.