Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit 3eb893b

Browse files
authored
Merge branch 'master' into fb-1909
2 parents ce9e2a3 + 41b8505 commit 3eb893b

15 files changed

+382
-354
lines changed

sql3/planner/expressionagg.go

Lines changed: 24 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,10 @@ func (n *countPlanExpression) NewBuffer() (types.AggregationBuffer, error) {
103103
return NewAggCountBuffer(n), nil
104104
}
105105

106-
func (n *countPlanExpression) AggType() types.AggregateFunctionType {
107-
return types.AGGREGATE_COUNT
108-
}
109-
110-
func (n *countPlanExpression) AggExpression() types.PlanExpression {
106+
func (n *countPlanExpression) FirstChildExpr() types.PlanExpression {
111107
return n.arg
112108
}
113109

114-
func (n *countPlanExpression) AggAdditionalExpr() []types.PlanExpression {
115-
return []types.PlanExpression{}
116-
}
117-
118110
func (n *countPlanExpression) Type() parser.ExprDataType {
119111
return n.returnDataType
120112
}
@@ -172,18 +164,10 @@ func (n *countDistinctPlanExpression) NewBuffer() (types.AggregationBuffer, erro
172164
return NewAggCountDistinctBuffer(n), nil
173165
}
174166

175-
func (n *countDistinctPlanExpression) AggType() types.AggregateFunctionType {
176-
return types.AGGREGATE_COUNT_DISTINCT
177-
}
178-
179-
func (n *countDistinctPlanExpression) AggExpression() types.PlanExpression {
167+
func (n *countDistinctPlanExpression) FirstChildExpr() types.PlanExpression {
180168
return n.arg
181169
}
182170

183-
func (n *countDistinctPlanExpression) AggAdditionalExpr() []types.PlanExpression {
184-
return []types.PlanExpression{}
185-
}
186-
187171
func (n *countDistinctPlanExpression) Type() parser.ExprDataType {
188172
return n.returnDataType
189173
}
@@ -320,29 +304,21 @@ func newSumPlanExpression(arg types.PlanExpression, returnDataType parser.ExprDa
320304
}
321305

322306
func (n *sumPlanExpression) Evaluate(currentRow []interface{}) (interface{}, error) {
323-
arg, ok := n.arg.(*qualifiedRefPlanExpression)
324-
if !ok {
325-
return nil, sql3.NewErrInternalf("unexpected aggregate function arg type '%T'", n.arg)
307+
arg, err := n.arg.Evaluate(currentRow)
308+
if err != nil {
309+
return nil, err
326310
}
327-
return currentRow[arg.columnIndex], nil
311+
return arg, nil
328312
}
329313

330314
func (n *sumPlanExpression) NewBuffer() (types.AggregationBuffer, error) {
331315
return NewAggSumBuffer(n), nil
332316
}
333317

334-
func (n *sumPlanExpression) AggType() types.AggregateFunctionType {
335-
return types.AGGREGATE_SUM
336-
}
337-
338-
func (n *sumPlanExpression) AggExpression() types.PlanExpression {
318+
func (n *sumPlanExpression) FirstChildExpr() types.PlanExpression {
339319
return n.arg
340320
}
341321

342-
func (n *sumPlanExpression) AggAdditionalExpr() []types.PlanExpression {
343-
return []types.PlanExpression{}
344-
}
345-
346322
func (n *sumPlanExpression) Type() parser.ExprDataType {
347323
return n.returnDataType
348324
}
@@ -426,7 +402,7 @@ func (a *aggregateAvg) Update(ctx context.Context, row types.Row) error {
426402

427403
a.sum = pql.AddDecimal(thisVal, aggVal)
428404

429-
case *parser.DataTypeInt:
405+
case *parser.DataTypeInt, *parser.DataTypeID:
430406
thisIVal, ok := v.(int64)
431407
if !ok {
432408
return sql3.NewErrInternalf("unexpected type conversion '%T'", v)
@@ -435,15 +411,6 @@ func (a *aggregateAvg) Update(ctx context.Context, row types.Row) error {
435411
thisVal := pql.FromInt64(thisIVal, returnType.Scale)
436412
a.sum = pql.AddDecimal(thisVal, aggVal)
437413

438-
case *parser.DataTypeID:
439-
thisIVal, ok := v.(uint64)
440-
if !ok {
441-
return sql3.NewErrInternalf("unexpected type conversion '%T'", v)
442-
}
443-
444-
thisVal := pql.FromInt64(int64(thisIVal), returnType.Scale)
445-
a.sum = pql.AddDecimal(thisVal, aggVal)
446-
447414
default:
448415
return sql3.NewErrInternalf("unhandled aggregate expression datatype '%T'", dataType)
449416
}
@@ -503,29 +470,21 @@ func newAvgPlanExpression(arg types.PlanExpression, returnDataType parser.ExprDa
503470
}
504471

505472
func (n *avgPlanExpression) Evaluate(currentRow []interface{}) (interface{}, error) {
506-
arg, ok := n.arg.(*qualifiedRefPlanExpression)
507-
if !ok {
508-
return nil, sql3.NewErrInternalf("unexpected aggregate function arg type '%T'", n.arg)
473+
arg, err := n.arg.Evaluate(currentRow)
474+
if err != nil {
475+
return nil, err
509476
}
510-
return currentRow[arg.columnIndex], nil
477+
return arg, nil
511478
}
512479

513480
func (n *avgPlanExpression) NewBuffer() (types.AggregationBuffer, error) {
514481
return NewAggAvgBuffer(n), nil
515482
}
516483

517-
func (n *avgPlanExpression) AggType() types.AggregateFunctionType {
518-
return types.AGGREGATE_AVG
519-
}
520-
521-
func (n *avgPlanExpression) AggExpression() types.PlanExpression {
484+
func (n *avgPlanExpression) FirstChildExpr() types.PlanExpression {
522485
return n.arg
523486
}
524487

525-
func (n *avgPlanExpression) AggAdditionalExpr() []types.PlanExpression {
526-
return []types.PlanExpression{}
527-
}
528-
529488
func (n *avgPlanExpression) Type() parser.ExprDataType {
530489
return n.returnDataType
531490
}
@@ -660,29 +619,21 @@ func newMinPlanExpression(arg types.PlanExpression, returnDataType parser.ExprDa
660619
}
661620

662621
func (n *minPlanExpression) Evaluate(currentRow []interface{}) (interface{}, error) {
663-
arg, ok := n.arg.(*qualifiedRefPlanExpression)
664-
if !ok {
665-
return nil, sql3.NewErrInternalf("unexpected aggregate function arg type '%T'", n.arg)
622+
arg, err := n.arg.Evaluate(currentRow)
623+
if err != nil {
624+
return nil, err
666625
}
667-
return currentRow[arg.columnIndex], nil
626+
return arg, nil
668627
}
669628

670629
func (n *minPlanExpression) NewBuffer() (types.AggregationBuffer, error) {
671630
return NewAggMinBuffer(n), nil
672631
}
673632

674-
func (n *minPlanExpression) AggType() types.AggregateFunctionType {
675-
return types.AGGREGATE_MIN
676-
}
677-
678-
func (n *minPlanExpression) AggExpression() types.PlanExpression {
633+
func (n *minPlanExpression) FirstChildExpr() types.PlanExpression {
679634
return n.arg
680635
}
681636

682-
func (n *minPlanExpression) AggAdditionalExpr() []types.PlanExpression {
683-
return []types.PlanExpression{}
684-
}
685-
686637
func (n *minPlanExpression) Type() parser.ExprDataType {
687638
return n.returnDataType
688639
}
@@ -818,29 +769,21 @@ func newMaxPlanExpression(arg types.PlanExpression, returnDataType parser.ExprDa
818769
}
819770

820771
func (n *maxPlanExpression) Evaluate(currentRow []interface{}) (interface{}, error) {
821-
arg, ok := n.arg.(*qualifiedRefPlanExpression)
822-
if !ok {
823-
return nil, sql3.NewErrInternalf("unexpected aggregate function arg type '%T'", n.arg)
772+
arg, err := n.arg.Evaluate(currentRow)
773+
if err != nil {
774+
return nil, err
824775
}
825-
return currentRow[arg.columnIndex], nil
776+
return arg, nil
826777
}
827778

828779
func (n *maxPlanExpression) NewBuffer() (types.AggregationBuffer, error) {
829780
return NewAggMaxBuffer(n), nil
830781
}
831782

832-
func (n *maxPlanExpression) AggType() types.AggregateFunctionType {
833-
return types.AGGREGATE_MAX
834-
}
835-
836-
func (n *maxPlanExpression) AggExpression() types.PlanExpression {
783+
func (n *maxPlanExpression) FirstChildExpr() types.PlanExpression {
837784
return n.arg
838785
}
839786

840-
func (n *maxPlanExpression) AggAdditionalExpr() []types.PlanExpression {
841-
return []types.PlanExpression{}
842-
}
843-
844787
func (n *maxPlanExpression) Type() parser.ExprDataType {
845788
return n.returnDataType
846789
}
@@ -900,20 +843,10 @@ func (n *percentilePlanExpression) NewBuffer() (types.AggregationBuffer, error)
900843
return NewAggCountBuffer(n), nil
901844
}
902845

903-
func (n *percentilePlanExpression) AggType() types.AggregateFunctionType {
904-
return types.AGGREGATE_PERCENTILE
905-
}
906-
907-
func (n *percentilePlanExpression) AggExpression() types.PlanExpression {
846+
func (n *percentilePlanExpression) FirstChildExpr() types.PlanExpression {
908847
return n.arg
909848
}
910849

911-
func (n *percentilePlanExpression) AggAdditionalExpr() []types.PlanExpression {
912-
return []types.PlanExpression{
913-
n.nthArg,
914-
}
915-
}
916-
917850
func (n *percentilePlanExpression) Type() parser.ExprDataType {
918851
return n.returnDataType
919852
}

sql3/planner/expressionanalyzercall.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,18 @@ func (p *ExecutionPlanner) analyzeCallExpression(ctx context.Context, call *pars
5656
return nil, sql3.NewErrCallParameterCountMismatch(call.Rparen.Line, call.Rparen.Column, call.Name.Name, 1, len(call.Args))
5757
}
5858

59-
//make sure it's a qualified ref
59+
// if it is a ref, we shouldn't do a sum on the _id
6060
ref, ok := call.Args[0].(*parser.QualifiedRef)
61-
if !ok {
62-
return nil, sql3.NewErrExpectedColumnReference(call.Args[0].Pos().Line, call.Args[0].Pos().Column)
63-
}
64-
65-
//can't do a sum on _id
66-
if strings.EqualFold(ref.Column.Name, "_id") {
61+
if ok && strings.EqualFold(ref.Column.Name, "_id") {
6762
return nil, sql3.NewErrIdColumnNotValidForAggregateFunction(call.Args[0].Pos().Line, call.Args[0].Pos().Column, call.Name.Name)
6863
}
6964

7065
//make sure the ref is sum-able
71-
if !(typeIsInteger(ref.DataType()) || typeIsDecimal(ref.DataType())) {
72-
return nil, sql3.NewErrIntOrDecimalExpressionExpected(ref.Table.NamePos.Line, ref.Table.NamePos.Column)
66+
if !(typeIsInteger(call.Args[0].DataType()) || typeIsDecimal(call.Args[0].DataType())) {
67+
return nil, sql3.NewErrIntOrDecimalExpressionExpected(call.Args[0].Pos().Line, call.Args[0].Pos().Column)
7368
}
7469

75-
call.ResultDataType = ref.DataType()
70+
call.ResultDataType = call.Args[0].DataType()
7671

7772
case "AVG":
7873
// can't do an avg on a *
@@ -85,19 +80,15 @@ func (p *ExecutionPlanner) analyzeCallExpression(ctx context.Context, call *pars
8580
return nil, sql3.NewErrCallParameterCountMismatch(call.Rparen.Line, call.Rparen.Column, call.Name.Name, 1, len(call.Args))
8681
}
8782

83+
// if it is a ref, we shouldn't do a avg on the _id
8884
ref, ok := call.Args[0].(*parser.QualifiedRef)
89-
if !ok {
90-
return nil, sql3.NewErrExpectedColumnReference(call.Args[0].Pos().Line, call.Args[0].Pos().Column)
91-
}
92-
93-
//can't do a avg on _id
94-
if strings.EqualFold(ref.Column.Name, "_id") {
85+
if ok && strings.EqualFold(ref.Column.Name, "_id") {
9586
return nil, sql3.NewErrIdColumnNotValidForAggregateFunction(call.Args[0].Pos().Line, call.Args[0].Pos().Column, call.Name.Name)
9687
}
9788

9889
//make sure the ref is avg-able
99-
if !(typeIsInteger(ref.DataType()) || typeIsDecimal(ref.DataType())) {
100-
return nil, sql3.NewErrIntOrDecimalExpressionExpected(ref.Table.NamePos.Line, ref.Table.NamePos.Column)
90+
if !(typeIsInteger(call.Args[0].DataType()) || typeIsDecimal(call.Args[0].DataType())) {
91+
return nil, sql3.NewErrIntOrDecimalExpressionExpected(call.Args[0].Pos().Line, call.Args[0].Pos().Column)
10192
}
10293

10394
call.ResultDataType = parser.NewDataTypeDecimal(4)
@@ -153,24 +144,19 @@ func (p *ExecutionPlanner) analyzeCallExpression(ctx context.Context, call *pars
153144
return nil, sql3.NewErrCallParameterCountMismatch(call.Rparen.Line, call.Rparen.Column, call.Name.Name, 1, len(call.Args))
154145
}
155146

156-
// first arg should be a qualified ref
147+
// if it is a ref, we shouldn't do a min/max on the _id
157148
ref, ok := call.Args[0].(*parser.QualifiedRef)
158-
if !ok {
159-
return nil, sql3.NewErrExpectedColumnReference(call.Args[0].Pos().Line, call.Args[0].Pos().Column)
160-
}
161-
162-
// can't do a min/max on _id
163-
if strings.EqualFold(ref.Column.Name, "_id") {
149+
if ok && strings.EqualFold(ref.Column.Name, "_id") {
164150
return nil, sql3.NewErrIdColumnNotValidForAggregateFunction(call.Args[0].Pos().Line, call.Args[0].Pos().Column, call.Name.Name)
165151
}
166152

167153
// make sure the ref is min/max-able
168-
if !(typeIsInteger(ref.DataType()) || typeIsDecimal(ref.DataType()) || typeIsTimestamp(ref.DataType()) || typeIsString(ref.DataType())) {
169-
return nil, sql3.NewErrIntOrDecimalOrTimestampOrStringExpressionExpected(ref.Table.NamePos.Line, ref.Table.NamePos.Column)
154+
if !(typeIsInteger(call.Args[0].DataType()) || typeIsDecimal(call.Args[0].DataType()) || typeIsTimestamp(call.Args[0].DataType()) || typeIsString(call.Args[0].DataType())) {
155+
return nil, sql3.NewErrIntOrDecimalOrTimestampOrStringExpressionExpected(call.Args[0].Pos().Line, call.Args[0].Pos().Column)
170156
}
171157

172158
// return the data type of the referenced column
173-
call.ResultDataType = ref.DataType()
159+
call.ResultDataType = call.Args[0].DataType()
174160

175161
case "SETCONTAINS":
176162
// two arguments
@@ -239,7 +225,6 @@ func (p *ExecutionPlanner) analyzeCallExpression(ctx context.Context, call *pars
239225

240226
case "DATEPART":
241227
return p.analyzeFunctionDatePart(call, scope)
242-
243228
case "SUBTABLE":
244229
return p.analyzeFunctionSubtable(call, scope)
245230
case "REVERSE":

sql3/planner/expressiontypes.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,16 @@ func typeIsDecimal(testType parser.ExprDataType) bool {
537537
}
538538
}
539539

540+
// returns true if the type is bit-sliced
541+
func typeIsBSI(testType parser.ExprDataType) bool {
542+
switch testType.(type) {
543+
case *parser.DataTypeInt, *parser.DataTypeDecimal, *parser.DataTypeTimestamp:
544+
return true
545+
default:
546+
return false
547+
}
548+
}
549+
540550
// returns true if the types can be compared
541551
func typesAreComparable(testTypeL parser.ExprDataType, testTypeR parser.ExprDataType) bool {
542552
switch testTypeL.(type) {

0 commit comments

Comments
 (0)