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

sourceColumn #1196

Merged
merged 3 commits into from
Feb 25, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ private void populateMetaData(MetaDataStore metaDataStore) {
metaDataStore.getModelsToBind().stream()
.map(model -> constructTable(model, metadataDictionary))
.forEach(metaDataStore::addTable);

metaDataStore.resolveSourceColumn();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.yahoo.elide.datastores.aggregation.metadata;

import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.core.Path;
import com.yahoo.elide.core.datastore.inmemory.HashMapDataStore;
import com.yahoo.elide.core.exceptions.DuplicateMappingException;
import com.yahoo.elide.datastores.aggregation.AggregationDataStore;
Expand Down Expand Up @@ -169,4 +170,24 @@ public static boolean isMetricField(EntityDictionary dictionary, Class<?> cls, S
public static boolean isTableJoin(Class<?> cls, String fieldName, EntityDictionary dictionary) {
return dictionary.getAttributeOrRelationAnnotation(cls, Join.class, fieldName) != null;
}

/**
* Resolve source columns for all Columns in all Tables.
*/
public void resolveSourceColumn() {
getMetaData(Table.class).forEach(table ->
table.getColumns().forEach(column -> {
Path sourcePath = column.getSourcePath(dictionary);
Path.PathElement source = sourcePath.lastElement().get();

Table sourceTable = (Table) dataStore.get(Table.class)
.get(dictionary.getJsonAliasFor(source.getType()));

Column sourceColumn = column instanceof Metric
? sourceTable.getMetric(source.getFieldName())
: sourceTable.getDimension(source.getFieldName());
column.setSourceColumn(sourceColumn);
})
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.ToOne;
import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.core.Path;
import com.yahoo.elide.datastores.aggregation.annotation.Meta;
import com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -45,6 +47,11 @@ public abstract class Column {

private ValueType valueType;

@ToOne
@ToString.Exclude
@EqualsAndHashCode.Exclude
private Column sourceColumn;

@ToString.Exclude
private Set<String> columnTags;

Expand Down Expand Up @@ -83,4 +90,17 @@ public static ValueType getValueType(Class<?> tableClass, String fieldName, Enti
}
}
}

/**
* Return a Path that navigate to the source of this column.
*
* @param metadataDictionary metadata dictionary
* @return Path to source column
*/
public Path getSourcePath(EntityDictionary metadataDictionary) {
Class<?> tableCls = metadataDictionary.getEntityClass(table.getId());
Class<?> columnCls = metadataDictionary.getParameterizedType(tableCls, getName());

return new Path(Collections.singletonList(new Path.PathElement(tableCls, columnCls, getName())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
package com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata;

import com.yahoo.elide.datastores.aggregation.core.JoinPath;
import com.yahoo.elide.datastores.aggregation.metadata.models.Table;

/**
* Column with physical SQL information like reference and join to path.
*/
public interface SQLColumn {
Table getTable();

String getName();

String getReference();

JoinPath getJoinPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine.getClassAlias;

import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.core.Path;
import com.yahoo.elide.datastores.aggregation.core.JoinPath;
import com.yahoo.elide.datastores.aggregation.metadata.models.Dimension;
import com.yahoo.elide.datastores.aggregation.metadata.models.Table;
Expand All @@ -26,6 +27,9 @@ public class SQLDimension extends Dimension implements SQLColumn {
@Getter
private final JoinPath joinPath;

@Getter
private EntityDictionary metadataDictionary;

public SQLDimension(Table table, String fieldName, EntityDictionary dictionary) {
super(table, fieldName, dictionary);
Class<?> tableClass = dictionary.getEntityClass(table.getId());
Expand All @@ -40,5 +44,12 @@ public SQLDimension(Table table, String fieldName, EntityDictionary dictionary)
this.reference = generateColumnReference(path, dictionary);
this.joinPath = path;
}

this.metadataDictionary = dictionary;
}

@Override
public Path getSourcePath(EntityDictionary metadataDictionary) {
return joinPath == null ? super.getSourcePath(metadataDictionary) : joinPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine.getClassAlias;

import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.core.Path;
import com.yahoo.elide.datastores.aggregation.core.JoinPath;
import com.yahoo.elide.datastores.aggregation.metadata.models.Table;
import com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension;
Expand All @@ -26,6 +27,9 @@ public class SQLTimeDimension extends TimeDimension implements SQLColumn {
@Getter
private final JoinPath joinPath;

@Getter
private EntityDictionary metadataDictionary;

public SQLTimeDimension(Table table, String fieldName, EntityDictionary dictionary) {
super(table, fieldName, dictionary);
Class<?> tableClass = dictionary.getEntityClass(table.getId());
Expand All @@ -40,5 +44,12 @@ public SQLTimeDimension(Table table, String fieldName, EntityDictionary dictiona
this.reference = generateColumnReference(path, dictionary);
this.joinPath = path;
}

this.metadataDictionary = dictionary;
}

@Override
public Path getSourcePath(EntityDictionary metadataDictionary) {
return joinPath == null ? super.getSourcePath(metadataDictionary) : joinPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class SQLUnitTest {
protected static Table playerStatsTable;
protected static EntityDictionary dictionary;
protected static RSQLFilterDialect filterParser;
protected static MetaDataStore metaDataStore = new MetaDataStore();
protected static MetaDataStore metaDataStore;

protected static final Country HONG_KONG = new Country();
protected static final Country USA = new Country();
Expand All @@ -49,6 +49,8 @@ public abstract class SQLUnitTest {
protected static QueryEngine engine;

public static void init() {
metaDataStore = new MetaDataStore();

emf = Persistence.createEntityManagerFactory("aggregationStore");
dictionary = new EntityDictionary(new HashMap<>());
dictionary.bindEntity(PlayerStatsWithView.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ public void metaDataTest() {
.statusCode(HttpStatus.SC_OK)
.body("data.attributes.name", equalTo("playerName"))
.body("data.attributes.valueType", equalTo("TEXT"))
.body("data.relationships.sourceColumn.data.id", equalTo("player.name"))
.body("data.relationships.table.data.id", equalTo("playerStats"));

given()
Expand All @@ -699,6 +700,7 @@ public void metaDataTest() {
.statusCode(HttpStatus.SC_OK)
.body("data.attributes.name", equalTo("lowScore"))
.body("data.attributes.valueType", equalTo("INTEGER"))
.body("data.relationships.sourceColumn.data.id", equalTo("playerStats.lowScore"))
.body("data.relationships.table.data.id", equalTo("playerStats"))
.body("data.relationships.metricFunction.data.id", equalTo("playerStats.lowScore[min]"))
.body("included.id", hasItem("playerStats.lowScore[min]"))
Expand All @@ -713,6 +715,7 @@ public void metaDataTest() {
.statusCode(HttpStatus.SC_OK)
.body("data.attributes.name", equalTo("recordedDate"))
.body("data.attributes.valueType", equalTo("TIME"))
.body("data.relationships.sourceColumn.data.id", equalTo("playerStats.recordedDate"))
.body("data.relationships.table.data.id", equalTo("playerStats"))
.body(
"data.relationships.supportedGrains.data.id",
Expand Down