Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BigQuery query template to retrieve training data #182

Merged
merged 3 commits into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ public String createQuery(
throw new NoSuchElementException("features not found: " + featureIds);
}

assert featureInfos.size() > 0;
String tableId = getBqTableId(featureInfos.get(0));
Features features = new Features(featureIds, tableId);
String entityName = featureInfos.get(0).getEntity().getName();
Features features = new Features(featureIds, tableId, entityName);

String startDateStr = formatDateString(startDate);
String endDateStr = formatDateString(endDate);
Expand Down Expand Up @@ -117,9 +119,11 @@ static final class Features {
final List<String> columns;
final String tableId;

public Features(List<String> featureIds, String tableId) {
this.columns = featureIds.stream()
.map(f -> f.replace(".", "_"))
Features(List<String> featureIds, String tableId, String entityName) {
// columns represent the column name in BigQuery
// feature with id "myentity.myfeature" will be represented as column "myfeature" in BigQuery
columns = featureIds.stream()
.map(f -> f.replace(".", "_").replace(entityName + "_", ""))
davidheryanto marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toList());
this.tableId = tableId;
}
Expand Down
10 changes: 4 additions & 6 deletions core/src/main/resources/templates/bq_training.tmpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
SELECT
{{ feature_set.tableId }}.id,
{{ feature_set.tableId }}.event_timestamp
{% for feature in feature_set.columns -%}
,{{ feature }}
{%- endfor %}
id,
event_timestamp{%- if feature_set.columns | length > 0 %},{%- endif %}
{{ feature_set.columns | join(',') }}
FROM
{{ feature_set.tableId }}
`{{ feature_set.tableId }}`
WHERE event_timestamp >= TIMESTAMP("{{ start_date }}") AND event_timestamp <= TIMESTAMP(DATETIME_ADD("{{ end_date }}", INTERVAL 1 DAY))
{% if limit is not none -%}
LIMIT {{ limit }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public void shouldPassCorrectArgumentToTemplateEngine() {
Timestamps.fromSeconds(Instant.parse("2019-01-01T00:00:00.00Z").getEpochSecond());
int limit = 100;
String featureId = "myentity.feature1";
String expectedColumnName = "feature1";
String tableId = "project.dataset.myentity";

when(featureInfoRespository.findAllById(any(List.class)))
Expand Down Expand Up @@ -123,7 +124,7 @@ public void shouldPassCorrectArgumentToTemplateEngine() {

Features features = (Features) actualContext.get("feature_set");
assertThat(features.getColumns().size(), equalTo(1));
assertThat(features.getColumns().get(0), equalTo(featureId.replace(".", "_")));
assertThat(features.getColumns().get(0), equalTo(expectedColumnName));
assertThat(features.getTableId(), equalTo(tableId));
}

Expand Down
12 changes: 6 additions & 6 deletions core/src/test/resources/sql/expQuery1.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
SELECT
project.dataset.myentity.id,
project.dataset.myentity.event_timestamp ,
myentity_feature1,
myentity_feature2,
myentity_feature3
id,
event_timestamp,
feature1,
feature2,
feature3
FROM
project.dataset.myentity
`project.dataset.myentity`
WHERE
event_timestamp >= TIMESTAMP("2018-01-02")
AND event_timestamp <= TIMESTAMP(DATETIME_ADD("2018-01-30", INTERVAL 1 DAY)) LIMIT 100
8 changes: 4 additions & 4 deletions core/src/test/resources/sql/expQuery2.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
SELECT
project.dataset.myentity.id,
project.dataset.myentity.event_timestamp ,
myentity_feature1
id,
event_timestamp,
feature1
FROM
project.dataset.myentity
`project.dataset.myentity`
WHERE
event_timestamp >= TIMESTAMP("2018-01-02")
AND event_timestamp <= TIMESTAMP(DATETIME_ADD("2018-01-30", INTERVAL 1 DAY)) LIMIT 1000