Skip to content

Commit

Permalink
Improve BQ query efficiency for stat var observation related SPARQL (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shifucun authored Jun 9, 2023
1 parent 2c3fd8d commit 1353e82
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
23 changes: 20 additions & 3 deletions internal/translator/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"google.golang.org/grpc/status"
)

const svProp = "variable_measured"

// Binding contains a query and mapping object which bind together.
type Binding struct {
Query *types.Query
Expand Down Expand Up @@ -812,8 +814,17 @@ func getSQL(

// Sort to get deterministic result.
sort.SliceStable(whereConstraints, func(i, j int) bool {
return strings.Compare(
whereConstraints[i].LHS.String(), whereConstraints[j].LHS.String()) < 0
l := whereConstraints[i].LHS
r := whereConstraints[j].LHS
// Put "variable_measured" constraints at the beginning to better use
// StatVarObservation's key.
if l.Name == svProp {
return true
}
if r.Name == svProp {
return false
}
return strings.Compare(l.String(), r.String()) < 0
})
for idx, c := range whereConstraints {
if idx == 0 {
Expand Down Expand Up @@ -904,7 +915,13 @@ func Translate(
queryOptions = &types.QueryOptions{}
}

sql, prov, err := getSQL(nodes, constraints, constNode, ProvInfo{queryProv, tableProv}, queryOptions)
sql, prov, err := getSQL(
nodes,
constraints,
constNode,
ProvInfo{queryProv, tableProv},
queryOptions,
)
if err != nil {
return nil, err
}
Expand Down
24 changes: 22 additions & 2 deletions internal/translator/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,8 +1234,28 @@ func TestStatVarObs(t *testing.T) {
"FROM `dc_v3.Place` AS _dc_v3_Place_1\n" +
"JOIN `dc_v3.StatVarObservation` AS _dc_v3_StatVarObservation_0\n" +
"ON _dc_v3_Place_1.id = _dc_v3_StatVarObservation_0.observation_about\n" +
"WHERE _dc_v3_Place_1.type = \"Country\"\n" +
"AND _dc_v3_StatVarObservation_0.variable_measured = \"Amount_EconomicActivity_GrossNationalIncome_PurchasingPowerParity_PerCapita\"\n",
"WHERE _dc_v3_StatVarObservation_0.variable_measured = \"Amount_EconomicActivity_GrossNationalIncome_PurchasingPowerParity_PerCapita\"\n" +
"AND _dc_v3_Place_1.type = \"Country\"\n",
},
{
"browser-observation",
`
SELECT ?dcid ?mmethod ?obsPeriod ?obsDate
WHERE {
?svObservation typeOf StatVarObservation .
?svObservation variableMeasured Count_Person .
?svObservation observationAbout country/USA .
?svObservation dcid ?dcid .
?svObservation measurementMethod ?mmethod .
?svObservation observationPeriod ?obsPeriod .
}
`,
"SELECT _dc_v3_StatVarObservation_0.id AS dcid,\n" +
"_dc_v3_StatVarObservation_0.measurement_method AS mmethod,\n" +
"_dc_v3_StatVarObservation_0.observation_period AS obsPeriod,\n\n" +
"FROM `dc_v3.StatVarObservation` AS _dc_v3_StatVarObservation_0\n" +
"WHERE _dc_v3_StatVarObservation_0.variable_measured = \"Count_Person\"\n" +
"AND _dc_v3_StatVarObservation_0.observation_about = \"country/USA\"\n",
},
} {
nodes, queries, _, err := sparql.ParseQuery(c.queryStr)
Expand Down

0 comments on commit 1353e82

Please sign in to comment.