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
660 changes: 330 additions & 330 deletions internal/server/spanner/golden/query/get_observations_contained_in.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
m.subject_id IN ('dc/g/UN'))<-[e:Edge
WHERE
e.predicate = 'specializationOf']-{1,10}(n:Node)
RETURN
RETURN DISTINCT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how these sql goldens show the changes clearly.

m.subject_id,
n.subject_id AS value
NEXT MATCH (n)
WHERE
n.subject_id = value
RETURN
subject_id,
'specializationOf+' AS predicate,
'' AS provenance,
n.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
m.subject_id IN ('dc/g/Farm_FarmInventoryStatus'))<-[e:Edge
WHERE
e.predicate = 'specializationOf']-{1,10}(n:Node)
RETURN
RETURN DISTINCT
m.subject_id,
n.subject_id AS value
NEXT MATCH (n)
WHERE
n.subject_id = value
RETURN
subject_id,
'specializationOf+' AS predicate,
'' AS provenance,
n.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
m.subject_id IN ('dc/g/Person_Gender'))-[e:Edge
WHERE
e.predicate = 'specializationOf']->{1,10}(n:Node)
RETURN
RETURN DISTINCT
m.subject_id,
n.subject_id AS value
NEXT MATCH (n)
WHERE
n.subject_id = value
RETURN
subject_id,
'specializationOf+' AS predicate,
'' AS provenance,
n.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
m.subject_id IN ('dc/g/UN'))<-[e:Edge
WHERE
e.predicate = 'specializationOf']-{1,10}(n:Node)
RETURN
RETURN DISTINCT
m.subject_id,
n.subject_id AS value
NEXT MATCH (n)
WHERE
n.subject_id = value
RETURN
subject_id,
'specializationOf+' AS predicate,
'' AS provenance,
n.value,
Expand Down
33 changes: 31 additions & 2 deletions internal/server/spanner/golden/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"path"
"runtime"
"sort"
"testing"

"github.com/datacommonsorg/mixer/internal/server/spanner"
Expand Down Expand Up @@ -101,7 +102,12 @@ func TestGetObservations(t *testing.T) {
goldenFile := c.golden + ".json"

runQueryGoldenTest(t, goldenFile, func(ctx context.Context) (interface{}, error) {
return client.GetObservations(ctx, c.variables, c.entities)
actual, err := client.GetObservations(ctx, c.variables, c.entities)
if err != nil {
return nil, err
}
sortObservations(actual)
return actual, nil
})
}
}
Expand All @@ -118,7 +124,12 @@ func TestGetObservationsContainedInPlace(t *testing.T) {
goldenFile := c.golden + ".json"

runQueryGoldenTest(t, goldenFile, func(ctx context.Context) (interface{}, error) {
return client.GetObservationsContainedInPlace(ctx, c.variables, c.containedInPlace)
actual, err := client.GetObservationsContainedInPlace(ctx, c.variables, c.containedInPlace)
if err != nil {
return nil, err
}
sortObservations(actual)
return actual, nil
})
}
}
Expand Down Expand Up @@ -222,3 +233,21 @@ func simplifyNodes(results map[string][]*spanner.Edge) map[string][]*spanner.Edg
}
return filtered
}

// sortObservations sorts Observations by variable, entity, facet (primary key) to ensure deterministic order in tests.
// The final Observation responses will be sorted later based on facet rank.
func sortObservations(results []*spanner.Observation) {
sort.Slice(results, func(i, j int) bool {
a, b := results[i], results[j]

if a.VariableMeasured != b.VariableMeasured {
return a.VariableMeasured < b.VariableMeasured
}

if a.ObservationAbout != b.ObservationAbout {
return a.ObservationAbout < b.ObservationAbout
}

return a.FacetId < b.FacetId
})
}
6 changes: 1 addition & 5 deletions internal/server/spanner/query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ func GetNodeEdgesByIDQuery(ids []string, arc *v2.Arc, offset int32) *spanner.Sta
switch arc.Decorator {
case CHAIN:
prefix = statements.chainedEdgePrefix
if len(arc.Filter) > 0 {
returnEdges = statements.returnFilterChainedEdges
} else {
returnEdges = statements.returnChainedEdges
}
returnEdges = statements.returnChainedEdges
default:
prefix = statements.edgePrefix
if len(arc.Filter) > 0 {
Expand Down
28 changes: 7 additions & 21 deletions internal/server/spanner/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ var statements = struct {
returnChainedEdges string
// Subquery to return Edges with filters.
returnFilterEdges string
// Subquery to return Edges for arcs with chaining and filters.
returnFilterChainedEdges string
// Subquery to apply page offset.
applyOffset string
// Subquery to apply page limit.
Expand Down Expand Up @@ -135,8 +133,14 @@ var statements = struct {
value,
provenance`,
returnChainedEdges: `
RETURN
RETURN DISTINCT
m.subject_id,
n.subject_id AS value
NEXT MATCH (n)
WHERE
n.subject_id = value
RETURN
subject_id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: Consider making indentations of the returned fields consistent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks! (sorry I think it's showing up weird in github, but it should be consistent)

@result_predicate AS predicate,
'' AS provenance,
n.value,
Expand Down Expand Up @@ -168,24 +172,6 @@ var statements = struct {
predicate,
value,
provenance`,
returnFilterChainedEdges: `
RETURN
m.subject_id,
n.subject_id AS value
NEXT MATCH (n)
WHERE
n.subject_id = value
RETURN
subject_id,
@result_predicate AS predicate,
'' AS provenance,
n.value,
n.bytes,
n.name,
n.types
ORDER BY
subject_id,
value`,
applyOffset: `
OFFSET %d`,
applyLimit: fmt.Sprintf(`
Expand Down