Skip to content
Open
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
21 changes: 14 additions & 7 deletions datastore/datastore/storagebackend/postgresql/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func getTimeFilter(tspec common.TemporalSpec) string {
return fmt.Sprintf("(%s)", strings.Join(timeExprs, " AND "))
}

// createObsQueryVals creates from polygon, circle, filter, and tspec values used for querying
// observations.
// createObsQueryVals creates from polygon, circle, camslRange, filter, and tspec values used for
// querying observations.
//
// Values to be used for query placeholders are appended to phVals.
//
Expand All @@ -49,13 +49,13 @@ func getTimeFilter(tspec common.TemporalSpec) string {
// - nil,
// otherwise (..., ..., ..., ..., error).
func createObsQueryVals(
polygon *datastore.Polygon, circle *datastore.Circle, filter map[string]*datastore.Strings,
tspec common.TemporalSpec, phVals *[]interface{}) (
polygon *datastore.Polygon, circle *datastore.Circle, camslRange string,
filter map[string]*datastore.Strings, tspec common.TemporalSpec, phVals *[]interface{}) (
string, string, string, string, error) {

timeFilter := getTimeFilter(tspec)

geoFilter, err := getGeoFilter(polygon, circle, phVals)
geoFilter, err := getGeoFilter(polygon, circle, camslRange, phVals)
if err != nil {
return "", "", "", "", fmt.Errorf("getGeoFilter() failed: %v", err)
}
Expand All @@ -70,8 +70,15 @@ func createObsQueryVals(
}
}

int64MdataFilter := getInt64MdataFilter(filter, phVals)
stringMdataFilter := getStringMdataFilter(filter, phVals)
int64MdataFilter, err := getInt64MdataFilter(filter, phVals)
if err != nil {
return "", "", "", "", fmt.Errorf("getInt64MdataFilter() failed: %v", err)
}

stringMdataFilter, err := getStringMdataFilter(filter, phVals)
if err != nil {
return "", "", "", "", fmt.Errorf("getStringMdataFilter() failed: %v", err)
}

// --- END filters for reflectable metadata (of type int64 or string) -------------

Expand Down
7 changes: 3 additions & 4 deletions datastore/datastore/storagebackend/postgresql/getlocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ func getLocs(

// get values needed for query
phVals := []interface{}{} // placeholder values
timeFilter, geoFilter, int64MdataFilter, stringMdataFilter,
err := createObsQueryVals(
request.GetSpatialPolygon(), request.GetSpatialCircle(), request.GetFilter(), tspec,
&phVals)
timeFilter, geoFilter, int64MdataFilter, stringMdataFilter, err := createObsQueryVals(
request.GetSpatialPolygon(), request.GetSpatialCircle(), request.GetCamslRange(),
request.GetFilter(), tspec, &phVals)
if err != nil {
return nil, fmt.Errorf("createLocsQueryVals() failed: %v", err)
}
Expand Down
72 changes: 52 additions & 20 deletions datastore/datastore/storagebackend/postgresql/getobservations.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ type filterInfo struct {

// TODO: add filter info for non-reflectable types

// getPolygonFilter derives the expression used in a WHERE clause for selecting only
// points inside a polygon.
// getPolygonFilter derives the expression used in a WHERE clause for selecting only points inside
// a polygon.
//
// Values to be used for query placeholders are appended to phVals.
//
Expand Down Expand Up @@ -253,8 +253,8 @@ func getPolygonFilter(polygon *datastore.Polygon, phVals *[]interface{}) (string
return whereExpr, nil
}

// getCircleFilter derives the expression used in a WHERE clause for selecting only
// points inside a circle.
// getCircleFilter derives the expression used in a WHERE clause for selecting only points inside
// a circle.
//
// Values to be used for query placeholders are appended to phVals.
//
Expand Down Expand Up @@ -291,14 +291,34 @@ func getCircleFilter(circle *datastore.Circle, phVals *[]interface{}) (string, e
return whereExpr, nil
}

// getGeoFilter derives from polygon and circle the expression used in a WHERE clause for keeping
// observations inside both of these areas (i.e. in their intersection).
// getCamslRangeFilter derives the expression used in a WHERE clause for selecting only points
// within a camsl range.
//
// Values to be used for query placeholders are appended to phVals.
//
// Returns (expression, nil) upon success, otherwise (..., error).
func getCamslRangeFilter(camslRange string, phVals *[]interface{}) (string, error) {

whereExpr := []string{}

addWhereCondMatchAnyPatternForInt64("camsl", []string{camslRange}, &whereExpr, phVals, false)

if len(whereExpr) == 0 {
return "TRUE", nil
}
// assert len(whereExpr) == 1
return whereExpr[0], nil
}

// getGeoFilter derives from polygon, circle, and camslRange the expression used in a WHERE clause
// for keeping observations inside these areas/ranges (i.e. in their intersection).
//
// Values to be used for query placeholders are appended to phVals.
//
// Returns (expression, nil) upon success, otherwise (..., error).
func getGeoFilter(
polygon *datastore.Polygon, circle *datastore.Circle, phVals *[]interface{}) (string, error) {
polygon *datastore.Polygon, circle *datastore.Circle,
camslRange string, phVals *[]interface{}) (string, error) {

var err error

Expand All @@ -312,28 +332,35 @@ func getGeoFilter(
return "", fmt.Errorf("getCircleFilter() failed: %v", err)
}

return fmt.Sprintf("(%s) AND (%s)", polygonExpr, circleExpr), nil
camslRangeExpr, err := getCamslRangeFilter(camslRange, phVals)
if err != nil {
return "", fmt.Errorf("getCamslRangeFilter() failed: %v", err)
}

return fmt.Sprintf("(%s) AND (%s) AND (%s)", polygonExpr, circleExpr, camslRangeExpr), nil
}

// scanObsRow scans all columns from the current result row in rows and converts to an ObsMetadata
// object.
// Returns (ObsMetadata object, time series ID, nil) upon success, otherwise (..., ..., error).
func scanObsRow(rows *sql.Rows) (*datastore.ObsMetadata, int64, error) {
var (
tsID int64
obsTimeInstant0 time.Time
pubTime0 sql.NullTime
value sql.NullString
point postgis.PointS
tsID int64
obsTimeInstant time.Time
pubTime sql.NullTime
value sql.NullString
point postgis.PointS
camsl sql.NullInt32
)

// initialize colValPtrs with non-reflectable metadata
colValPtrs := []interface{}{
&tsID,
&obsTimeInstant0,
&pubTime0,
&obsTimeInstant,
&pubTime,
&value,
&point,
&camsl,
}

// extend colValPtrs with reflectable metadata of type int64
Expand Down Expand Up @@ -362,12 +389,15 @@ func scanObsRow(rows *sql.Rows) (*datastore.ObsMetadata, int64, error) {
},
},
Obstime: &datastore.ObsMetadata_ObstimeInstant{
ObstimeInstant: timestamppb.New(obsTimeInstant0),
ObstimeInstant: timestamppb.New(obsTimeInstant),
},
Value: value.String,
}
if pubTime0.Valid {
obsMdata.Pubtime = timestamppb.New(pubTime0.Time)
if pubTime.Valid {
obsMdata.Pubtime = timestamppb.New(pubTime.Time)
}
if camsl.Valid {
obsMdata.Camsl = &camsl.Int32
}

var err error
Expand Down Expand Up @@ -444,8 +474,8 @@ func getObs(
// get values needed for query
phVals := []interface{}{} // placeholder values
timeFilter, geoFilter, int64MdataFilter, stringMdataFilter, err := createObsQueryVals(
request.GetSpatialPolygon(), request.GetSpatialCircle(), request.GetFilter(), tspec,
&phVals)
request.GetSpatialPolygon(), request.GetSpatialCircle(), request.GetCamslRange(),
request.GetFilter(), tspec, &phVals)
if err != nil {
return nil, fmt.Errorf("createObsQueryVals() failed: %v", err)
}
Expand All @@ -471,6 +501,7 @@ func getObs(
%s,
point,
%s,
%s,
%s
FROM observation
JOIN time_series on observation.ts_id = time_series.id
Expand All @@ -481,6 +512,7 @@ func getObs(
distinctSpec,
convertSelectCol(incFields, "pubtime", "observation."),
convertSelectCol(incFields, "value", "observation."),
convertSelectCol(incFields, "camsl", "observation."),
strings.Join(convOI64MC, ","),
strings.Join(convOSMC, ","),
timeFilter,
Expand Down
1 change: 1 addition & 0 deletions datastore/datastore/storagebackend/postgresql/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func init() { // automatically called once on program startup (on first import o
supIncRespFields.Set(strings.TrimPrefix(f, "observation."))
}
supIncRespFields.Set("geo_point")
supIncRespFields.Set("camsl")
supIncRespFields.Set("obstime_instant")
supIncRespFields.Set("pubtime")
supIncRespFields.Set("value")
Expand Down
Loading
Loading