Skip to content

Commit

Permalink
remove relationshp, update model (#1186)
Browse files Browse the repository at this point in the history
  • Loading branch information
hellohanchen authored and Aaron Klish committed Feb 17, 2020
1 parent 65fd06f commit 95b1a67
Show file tree
Hide file tree
Showing 32 changed files with 326 additions and 885 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void populateEntityDictionary(EntityDictionary dictionary) {
for (Table table : queryEngine.getMetaDataStore().getMetaData(Table.class)) {
for (TimeDimension timeDim : table.getColumns(TimeDimension.class)) {
dictionary.addArgumentToAttribute(
dictionary.getEntityClass(table.getName()),
dictionary.getEntityClass(table.getId()),
timeDim.getName(),
new ArgumentType("grain", String.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.yahoo.elide.core.DataStore;
import com.yahoo.elide.core.DataStoreTransaction;
import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.core.exceptions.InvalidPredicateException;
import com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore;
import com.yahoo.elide.datastores.aggregation.metadata.models.Table;
import com.yahoo.elide.datastores.aggregation.query.Query;
Expand Down Expand Up @@ -67,7 +68,6 @@ public abstract class QueryEngine {
@Getter
private final EntityDictionary metadataDictionary;

@Getter
private final Map<String, Table> tables;

/**
Expand All @@ -81,7 +81,7 @@ public QueryEngine(MetaDataStore metaDataStore) {
this.metadataDictionary = metaDataStore.getDictionary();
populateMetaData(metaDataStore);
this.tables = metaDataStore.getMetaData(Table.class).stream()
.collect(Collectors.toMap(Table::getName, Functions.identity()));
.collect(Collectors.toMap(Table::getId, Functions.identity()));
}

/**
Expand All @@ -99,6 +99,15 @@ public QueryEngine(MetaDataStore metaDataStore) {
* @param metaDataStore metadata store to populate
*/
private void populateMetaData(MetaDataStore metaDataStore) {
metaDataStore.getModelsToBind()
.forEach(model -> {
if (!metadataDictionary.isJPAEntity(model)
&& !metadataDictionary.getRelationships(model).isEmpty()) {
throw new InvalidPredicateException(
"Non-JPA entities " + model.getSimpleName() + " is not allowed to have relationship.");
}
});

metaDataStore.getModelsToBind().stream()
.map(model -> constructTable(model, metadataDictionary))
.forEach(metaDataStore::addTable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void validateHavingClause(FilterExpression havingClause) {
Class<?> cls = last.getType();
String fieldName = last.getFieldName();

Class<?> tableClass = dictionary.getEntityClass(queriedTable.getName());
Class<?> tableClass = dictionary.getEntityClass(queriedTable.getId());

if (cls != tableClass) {
throw new InvalidOperationException(
Expand Down Expand Up @@ -128,17 +128,6 @@ public void validateSorting() {
private void validateSortingPath(Path path, Set<String> allFields) {
List<Path.PathElement> pathElements = path.getPathElements();

// TODO: add support for double nested sorting
if (pathElements.size() > 2) {
throw new UnsupportedOperationException(
"Currently sorting on double nested fields is not supported");
}

if (metrics.isEmpty() && pathElements.size() > 1) {
throw new UnsupportedOperationException(
"Query with no metric can't sort on nested field.");
}

Path.PathElement currentElement = pathElements.get(0);
String currentField = currentElement.getFieldName();
Class<?> currentClass = currentElement.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.yahoo.elide.datastores.aggregation.annotation.Join;
import com.yahoo.elide.datastores.aggregation.annotation.MetricAggregation;
import com.yahoo.elide.datastores.aggregation.metadata.models.Column;
import com.yahoo.elide.datastores.aggregation.metadata.models.DataType;
import com.yahoo.elide.datastores.aggregation.metadata.models.FunctionArgument;
import com.yahoo.elide.datastores.aggregation.metadata.models.Metric;
import com.yahoo.elide.datastores.aggregation.metadata.models.MetricFunction;
Expand Down Expand Up @@ -83,7 +82,6 @@ public void addTable(Table table) {
*/
private void addColumn(Column column) {
addMetaData(column);
addDataType(column.getDataType());

if (column instanceof TimeDimension) {
((TimeDimension) column).getSupportedGrains().forEach(this::addTimeDimensionGrain);
Expand All @@ -102,15 +100,6 @@ private void addMetricFunction(MetricFunction metricFunction) {
metricFunction.getArguments().forEach(this::addFunctionArgument);
}

/**
* Add a datatype metadata object.
*
* @param dataType datatype metadata
*/
private void addDataType(DataType dataType) {
addMetaData(dataType);
}

/**
* Add a function argument metadata object.
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,43 @@
*/
package com.yahoo.elide.datastores.aggregation.metadata.enums;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
* Actual value type of a data type.
*/
public enum ValueType {
DATE,
NUMBER,
TIME,
INTEGER,
DECIMAL,
MONEY,
TEXT,
COORDINATE,
BOOLEAN,
RELATIONSHIP,
ID
ID;

private static final Map<Class<?>, ValueType> SCALAR_TYPES = new HashMap<Class<?>, ValueType>() {{
put(short.class, INTEGER);
put(Short.class, INTEGER);
put(int.class, INTEGER);
put(Integer.class, INTEGER);
put(long.class, INTEGER);
put(Long.class, INTEGER);
put(BigDecimal.class, DECIMAL);
put(float.class, DECIMAL);
put(Float.class, DECIMAL);
put(double.class, DECIMAL);
put(Double.class, DECIMAL);
put(boolean.class, BOOLEAN);
put(Boolean.class, BOOLEAN);
put(char.class, TEXT);
put(String.class, TEXT);
}};

public static ValueType getScalarType(Class<?> fieldClass) {
return SCALAR_TYPES.get(fieldClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
package com.yahoo.elide.datastores.aggregation.metadata.models;

import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.ToOne;
import com.yahoo.elide.core.EntityDictionary;
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.Date;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

/**
* Column is the super class of a field in a table, it can be either dimension or metric.
Expand All @@ -34,21 +34,25 @@ public abstract class Column {

private String longName;

private String tableName;

private String description;

private String category;

@ManyToOne
private DataType dataType;
@ToOne
@ToString.Exclude
@EqualsAndHashCode.Exclude
private Table table;

private ValueType valueType;

@ToString.Exclude
private Set<String> columnTags;

protected Column(Class<?> tableClass, String fieldName, EntityDictionary dictionary) {
this.tableName = dictionary.getJsonAliasFor(tableClass);
this.id = tableName + "." + fieldName;
protected Column(Table table, String fieldName, EntityDictionary dictionary) {
this.table = table;
Class<?> tableClass = dictionary.getEntityClass(table.getId());

this.id = table.getId() + "." + fieldName;
this.name = fieldName;
this.columnTags = new HashSet<>();

Expand All @@ -58,29 +62,25 @@ protected Column(Class<?> tableClass, String fieldName, EntityDictionary diction
this.description = meta.description();
}

dataType = getDataType(tableClass, fieldName, dictionary);
if (dataType == null) {
valueType = getValueType(tableClass, fieldName, dictionary);
if (valueType == null) {
throw new IllegalArgumentException("Unknown data type for " + this.id);
}
}

public static DataType getDataType(Class<?> tableClass, String fieldName, EntityDictionary dictionary) {
String tableName = dictionary.getJsonAliasFor(tableClass);
DataType dataType;
public static ValueType getValueType(Class<?> tableClass, String fieldName, EntityDictionary dictionary) {
if (dictionary.isRelation(tableClass, fieldName)) {
Class<?> relationshipClass = dictionary.getParameterizedType(tableClass, fieldName);
dataType = new RelationshipType(dictionary.getJsonAliasFor(relationshipClass));
return ValueType.RELATIONSHIP;
} else {
Class<?> fieldClass = dictionary.getType(tableClass, fieldName);

if (fieldName.equals(dictionary.getIdFieldName(tableClass))) {
dataType = new DataType(tableName + "." + fieldName, ValueType.ID);
return ValueType.ID;
} else if (Date.class.isAssignableFrom(fieldClass)) {
dataType = new DataType(fieldClass.getSimpleName().toLowerCase(Locale.ENGLISH), ValueType.DATE);
return ValueType.TIME;
} else {
dataType = DataType.getScalarType(fieldClass);
return ValueType.getScalarType(fieldClass);
}
}
return dataType;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Include(type = "dimension")
@Data
public class Dimension extends Column {
public Dimension(Class<?> tableClass, String fieldName, EntityDictionary dictionary) {
super(tableClass, fieldName, dictionary);
public Dimension(Table table, String fieldName, EntityDictionary dictionary) {
super(table, fieldName, dictionary);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
package com.yahoo.elide.datastores.aggregation.metadata.models;

import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType;

import lombok.Data;
import lombok.ToString;

import javax.persistence.Id;
import javax.persistence.ManyToOne;

/**
* Arguments that can be provided into a metric function.
Expand All @@ -27,13 +27,15 @@ public class FunctionArgument {

private String description;

@ManyToOne
private DataType dataType;
private ValueType type;

private String subType;

public FunctionArgument(String functionName, FunctionArgument argument) {
this.id = functionName + "." + argument.getName();
this.name = argument.getName();
this.description = argument.getDescription();
this.dataType = argument.getDataType();
this.type = argument.getType();
this.subType = argument.getSubType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public class Metric extends Column {
@ToString.Exclude
private MetricFunction metricFunction;

public Metric(Class<?> tableClass, String fieldName, EntityDictionary dictionary) {
super(tableClass, fieldName, dictionary);
public Metric(Table table, String fieldName, EntityDictionary dictionary) {
super(table, fieldName, dictionary);
Class<?> tableClass = dictionary.getEntityClass(table.getId());

MetricAggregation metric = dictionary.getAttributeOrRelationAnnotation(
tableClass,
Expand Down
Loading

0 comments on commit 95b1a67

Please sign in to comment.