Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions execution/engine/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ func (e *ExecutionEngine) Execute(ctx context.Context, operation *graphql.Reques
return report
}
if costCalculator != nil {
costCalculator.ValidateSliceArguments(e.config.plannerConfig, execContext.resolveContext.Variables, &report)
costCalculator.ValidateSliceArguments(execContext.resolveContext.Variables, &report)
if report.HasErrors() {
return report
}
}
operation.ComputeEstimatedCost(costCalculator, e.config.plannerConfig, execContext.resolveContext.Variables)
operation.ComputeEstimatedCost(costCalculator, execContext.resolveContext.Variables)

if execContext.resolveContext.TracingOptions.Enable && !execContext.resolveContext.TracingOptions.ExcludePlannerStats {
planningTime := resolve.GetDurationNanoSinceTraceStart(execContext.resolveContext.Context()) - tracePlanStart
Expand All @@ -240,7 +240,7 @@ func (e *ExecutionEngine) Execute(ctx context.Context, operation *graphql.Reques
return err
}
if resp != nil {
operation.ComputeActualCost(costCalculator, e.config.plannerConfig, execContext.resolveContext.ActualListSizes)
operation.ComputeActualCost(costCalculator, execContext.resolveContext.Variables, execContext.resolveContext.ActualListSizes)
}
return nil
case *plan.SubscriptionResponsePlan:
Expand Down
782 changes: 709 additions & 73 deletions execution/engine/execution_engine_cost_test.go

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions execution/engine/execution_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,20 @@ func runExecutionTest(testCase ExecutionEngineTestCase, withError bool, expected
assert.Equal(t, testCase.expectedResponse, actualResponse)
}

if testCase.expectedEstimatedCost != 0 {
if testCase.expectedEstimatedCost != nil {
gotCost := operation.EstimatedCost()
require.Equal(t, testCase.expectedEstimatedCost, gotCost)
require.Equal(t, *testCase.expectedEstimatedCost, gotCost)
}

if testCase.expectedActualCost != 0 {
if testCase.expectedActualCost != nil {
gotActualCost := operation.ActualCost()
require.Equal(t, testCase.expectedActualCost, gotActualCost)
require.Equal(t, *testCase.expectedActualCost, gotActualCost)
}
}
}

func intPtr(v int) *int { return &v }

func runWithAndCompareError(testCase ExecutionEngineTestCase, expectedErrorMessage string, options ...executionTestOptions) func(t *testing.T) {
return runExecutionTest(testCase, true, expectedErrorMessage, options...)
}
Expand Down Expand Up @@ -303,8 +305,8 @@ type ExecutionEngineTestCase struct {
expectedResponse string
expectedJSONResponse string
expectedFixture string
expectedEstimatedCost int
expectedActualCost int
expectedEstimatedCost *int
expectedActualCost *int
}

type _executionTestOptions struct {
Expand Down Expand Up @@ -4924,7 +4926,7 @@ func TestExecutionEngine_Execute(t *testing.T) {
// Children total = 7 + 3 = 10
// (is it possible to improve accuracy here by using the largest fragment instead of the sum?)
// Total = (5 + 10) * 3 = 45
expectedEstimatedCost: 45,
expectedEstimatedCost: intPtr(45),
},
computeCosts(),
))
Expand Down
12 changes: 6 additions & 6 deletions execution/graphql/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ func (r *Request) OperationType() (OperationType, error) {
return OperationTypeUnknown, nil
}

func (r *Request) ComputeEstimatedCost(calc *plan.CostCalculator, config plan.Configuration, variables *astjson.Value) {
func (r *Request) ComputeEstimatedCost(calc *plan.CostCalculator, variables *astjson.Value) {
if calc != nil {
r.estimatedCost = calc.EstimateCost(config, variables)
r.estimatedCost = calc.EstimateCost(variables)
// Debugging of cost trees. Uncomment to debug.
// fmt.Println(calc.DebugPrint(config, variables, nil))
// fmt.Println(calc.DebugPrint(variables, nil))
} else {
r.estimatedCost = 0
}
Expand All @@ -210,11 +210,11 @@ func (r *Request) EstimatedCost() int {
return r.estimatedCost
}

func (r *Request) ComputeActualCost(calc *plan.CostCalculator, config plan.Configuration, actualListSizes map[string]int) {
func (r *Request) ComputeActualCost(calc *plan.CostCalculator, variables *astjson.Value, actualListSizes map[string]int) {
if calc != nil {
r.actualCost = calc.ActualCost(config, actualListSizes)
r.actualCost = calc.ActualCost(variables, actualListSizes)
// Debugging of cost trees. Uncomment to debug.
// fmt.Println(calc.DebugPrint(config, nil, actualListSizes))
// fmt.Println(calc.DebugPrint(variables, actualListSizes))
} else {
r.actualCost = 0
}
Comment thread
ysmolski marked this conversation as resolved.
Expand Down
24 changes: 15 additions & 9 deletions v2/pkg/ast/ast_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,20 @@ func (d *Document) typesAreCompatible(left int, right int, ignoreNullability boo
}
}

// ResolveTypeNameBytes unwraps the ref type until it finds an underlying base type name.
// Example: [[String!]] -> String
func (d *Document) ResolveTypeNameBytes(ref int) ByteSlice {
resolvedTypeRef := d.ResolveUnderlyingType(ref)
return d.TypeNameBytes(resolvedTypeRef)
}

// ResolveTypeNameString unwraps the ref type until it finds an underlying base type name.
// Example: [[String!]] -> String
func (d *Document) ResolveTypeNameString(ref int) string {
return unsafebytes.BytesToString(d.ResolveTypeNameBytes(ref))
}

// ResolveUnderlyingType unwraps the ref type until it finds the named type.
// ResolveUnderlyingType unwraps the ref type until it finds an underlying base type.
func (d *Document) ResolveUnderlyingType(ref int) (typeRef int) {
typeRef = ref
graphqlType := d.Types[ref]
Expand All @@ -289,7 +293,10 @@ func (d *Document) ResolveUnderlyingType(ref int) (typeRef int) {
return
}

// ResolveListOrNameType unwraps the ref type until it finds the named or list type.
// ResolveListOrNameType unwraps the ref type until it finds an underlying base type or a list type.
// Examples:
// - [[String]!]! -> [[String]!]
// - String! -> String
func (d *Document) ResolveListOrNameType(ref int) (typeRef int) {
typeRef = ref
graphqlType := d.Types[ref]
Expand All @@ -300,13 +307,12 @@ func (d *Document) ResolveListOrNameType(ref int) (typeRef int) {
return
}

// ResolveNestedListOrListType returns the underlying type of a list.
// In contrast to ResolveListOrNameType, this function does not unwrap a non-null type.
// e.g.:
// * [[String]] -> [String]
// * [[String]!] -> [String]!
// * [String!]! -> String!
// * [String]! -> String
// ResolveNestedListOrListType unwraps the ref type from a nullable or non-nullable list.
// Examples:
// - [[String]] -> [String]
// - [[String]!] -> [String]!
// - [String!]! -> String!
// - [String]! -> String
func (d *Document) ResolveNestedListOrListType(ref int) int {
if !d.TypeIsList(ref) {
return InvalidRef
Expand Down
Loading
Loading