Skip to content

Commit

Permalink
create clone to prevent accidental modification of original argument …
Browse files Browse the repository at this point in the history
…list
  • Loading branch information
EpsilonPrime committed Jan 29, 2025
1 parent fd3495d commit 4d751cc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
17 changes: 17 additions & 0 deletions expr/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,23 @@ func (a *AggregateFunction) IntermediateType() (types.FuncDefArgType, error) {
return a.declaration.Intermediate()
}

func (a *AggregateFunction) Clone() *AggregateFunction {
newA := *a
newA.args = make([]types.FuncArg, len(a.args), len(a.args))

Check failure on line 816 in expr/functions.go

View workflow job for this annotation

GitHub Actions / Code Linting (ubuntu-latest)

S1019: should use make([]types.FuncArg, len(a.args)) instead (gosimple)
for i := 0; i < len(a.args); i++ {

Check failure on line 817 in expr/functions.go

View workflow job for this annotation

GitHub Actions / Code Linting (ubuntu-latest)

S1001: should use copy(to, from) instead of a loop (gosimple)
newA.args[i] = a.args[i]
}
newA.options = make([]*types.FunctionOption, len(a.options), len(a.options))

Check failure on line 820 in expr/functions.go

View workflow job for this annotation

GitHub Actions / Code Linting (ubuntu-latest)

S1019: should use make([]*types.FunctionOption, len(a.options)) instead (gosimple)
for i := 0; i < len(a.options); i++ {

Check failure on line 821 in expr/functions.go

View workflow job for this annotation

GitHub Actions / Code Linting (ubuntu-latest)

S1001: should use copy(to, from) instead of a loop (gosimple)
newA.options[i] = a.options[i]
}
newA.Sorts = make([]SortField, len(a.Sorts), len(a.Sorts))

Check failure on line 824 in expr/functions.go

View workflow job for this annotation

GitHub Actions / Code Linting (ubuntu-latest)

S1019: should use make([]SortField, len(a.Sorts)) instead (gosimple)
for i := 0; i < len(a.Sorts); i++ {

Check failure on line 825 in expr/functions.go

View workflow job for this annotation

GitHub Actions / Code Linting (ubuntu-latest)

S1001: should use copy(to, from) instead of a loop (gosimple)
newA.Sorts[i] = a.Sorts[i]
}
return &newA
}

// SetArg sets the specified argument to the provided value. The index is not checked for validity.
func (a *AggregateFunction) SetArg(i int, arg types.FuncArg) {
a.args[i] = arg
Expand Down
2 changes: 1 addition & 1 deletion plan/relations.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ func (ar *AggregateRel) rewriteAggregateFunc(rewriteFunc RewriteFunc, f *expr.Ag
if f == nil {
return f, nil
}
newF := f
newF := f.Clone()
argsAreEqual := true
for i := 0; i < f.NArgs(); i++ {
arg := f.Arg(i)
Expand Down
35 changes: 23 additions & 12 deletions plan/relations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ func TestRelations_Copy(t *testing.T) {
aggregateFnID, nil, types.AggInvocationAll,
types.AggPhaseInitialToResult, nil, createPrimitiveFloat(1.0))
require.NoError(t, err)
aggregateFnRevised, err := expr.NewAggregateFunc(extReg,
aggregateFnID, nil, types.AggInvocationAll,
types.AggPhaseInitialToResult, nil, createPrimitiveFloat(9.0))
require.NoError(t, err)

aggregateRel := &AggregateRel{input: createVirtualTableReadRel(1),
groupingExpressions: []expr.Expression{createPrimitiveFloat(1.0)}, groupingReferences: [][]uint32{{0}},
measures: []AggRelMeasure{{measure: aggregateFn, filter: expr.NewPrimitiveLiteral(false, false)}}}
groupingExpressions: []expr.Expression{createPrimitiveFloat(1.0)},
groupingReferences: [][]uint32{{0}},
measures: []AggRelMeasure{{measure: aggregateFn, filter: expr.NewPrimitiveLiteral(false, false)}}}
crossRel := &CrossRel{left: createVirtualTableReadRel(1), right: createVirtualTableReadRel(2)}
extensionLeafRel := &ExtensionLeafRel{}
extensionMultiRel := &ExtensionMultiRel{inputs: []Rel{createVirtualTableReadRel(1), createVirtualTableReadRel(2)}}
Expand Down Expand Up @@ -73,10 +78,13 @@ func TestRelations_Copy(t *testing.T) {
}
testCases := []relationTestCase{
{
name: "AggregateRel Copy with new inputs",
relation: aggregateRel,
newInputs: []Rel{createVirtualTableReadRel(6)},
expectedRel: &AggregateRel{input: createVirtualTableReadRel(6), groupingReferences: aggregateRel.groupingReferences, groupingExpressions: aggregateRel.groupingExpressions, measures: aggregateRel.measures},
name: "AggregateRel Copy with new inputs",
relation: aggregateRel,
newInputs: []Rel{createVirtualTableReadRel(6)},
expectedRel: &AggregateRel{input: createVirtualTableReadRel(6),
groupingReferences: aggregateRel.groupingReferences,
groupingExpressions: aggregateRel.groupingExpressions,
measures: aggregateRel.measures},
},
{
name: "AggregateRel Copy with same inputs and noOpRewrite",
Expand All @@ -86,13 +94,16 @@ func TestRelations_Copy(t *testing.T) {
expectedSameRel: true,
},
{
name: "AggregateRel Copy with new Inputs and noOpReWrite",
relation: aggregateRel,
newInputs: []Rel{createVirtualTableReadRel(7)},
expectedRel: &AggregateRel{input: createVirtualTableReadRel(7), groupingExpressions: aggregateRel.groupingExpressions, groupingReferences: aggregateRel.groupingReferences, measures: aggregateRel.measures},
name: "AggregateRel Copy with new Inputs and noOpReWrite",
relation: aggregateRel,
newInputs: []Rel{createVirtualTableReadRel(7)},
expectedRel: &AggregateRel{input: createVirtualTableReadRel(7),
groupingExpressions: aggregateRel.groupingExpressions,
groupingReferences: aggregateRel.groupingReferences,
measures: aggregateRel.measures},
},
{
name: "AggregateRel Copy with new Inputs and reWriteFunc",
name: "AggregateRel Copy with new Inputs and rewriteFunc",
relation: aggregateRel,
newInputs: []Rel{createVirtualTableReadRel(8)},
rewriteFunc: func(expression expr.Expression) (expr.Expression, error) {
Expand All @@ -107,7 +118,7 @@ func TestRelations_Copy(t *testing.T) {
expectedRel: &AggregateRel{input: createVirtualTableReadRel(8),
groupingExpressions: []expr.Expression{createPrimitiveFloat(9.0)},
groupingReferences: [][]uint32{{0}},
measures: []AggRelMeasure{{filter: expr.NewPrimitiveLiteral(true, false)}}},
measures: []AggRelMeasure{{measure: aggregateFnRevised, filter: expr.NewPrimitiveLiteral(true, false)}}},
},
{
name: "ExtensionLeafRel Copy with new inputs",
Expand Down

0 comments on commit 4d751cc

Please sign in to comment.