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

Column annotation #1017

Merged
merged 5 commits into from
Oct 16, 2019
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 @@ -14,14 +14,21 @@
*/
public abstract class AggregationDataStore implements DataStore {

private final QueryEngineFactory queryEngineFactory;
private QueryEngine queryEngine;

public AggregationDataStore(QueryEngine queryEngine) {
this.queryEngine = queryEngine;
public AggregationDataStore(QueryEngineFactory queryEngineFactory) {
this.queryEngineFactory = queryEngineFactory;
}

/**
* Populate an {@link EntityDictionary} and use this dictionary to construct a {@link QueryEngine}.
* @param dictionary the dictionary
*/
@Override
public abstract void populateEntityDictionary(EntityDictionary dictionary);
public void populateEntityDictionary(EntityDictionary dictionary) {
queryEngine = queryEngineFactory.buildQueryEngine(dictionary);
}

@Override
public DataStoreTransaction beginTransaction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import com.yahoo.elide.core.exceptions.InvalidOperationException;
import com.yahoo.elide.core.filter.expression.FilterExpression;
import com.yahoo.elide.datastores.aggregation.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.dimension.TimeDimension;
import com.yahoo.elide.datastores.aggregation.filter.visitor.FilterConstraints;
import com.yahoo.elide.datastores.aggregation.filter.visitor.SplitFilterExpressionVisitor;
import com.yahoo.elide.datastores.aggregation.metric.Aggregation;
import com.yahoo.elide.datastores.aggregation.metric.Metric;
import com.yahoo.elide.datastores.aggregation.schema.Schema;
import com.yahoo.elide.datastores.aggregation.schema.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.schema.dimension.TimeDimension;
import com.yahoo.elide.datastores.aggregation.schema.metric.Aggregation;
import com.yahoo.elide.datastores.aggregation.schema.metric.Metric;
import com.yahoo.elide.request.Attribute;
import com.yahoo.elide.request.EntityProjection;
import com.yahoo.elide.request.Relationship;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import com.yahoo.elide.core.filter.expression.FilterExpression;
import com.yahoo.elide.core.pagination.Pagination;
import com.yahoo.elide.core.sort.Sorting;
import com.yahoo.elide.datastores.aggregation.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.dimension.TimeDimension;
import com.yahoo.elide.datastores.aggregation.metric.Aggregation;
import com.yahoo.elide.datastores.aggregation.metric.Metric;

import com.yahoo.elide.datastores.aggregation.schema.Schema;
import com.yahoo.elide.datastores.aggregation.schema.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.schema.dimension.TimeDimension;
import com.yahoo.elide.datastores.aggregation.schema.metric.Aggregation;
import com.yahoo.elide.datastores.aggregation.schema.metric.Metric;

import lombok.Builder;
import lombok.Data;
import lombok.Singular;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2019, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.datastores.aggregation;

import com.yahoo.elide.core.EntityDictionary;

/**
* Interface that constructs {@link QueryEngine} based on given entityDictionary.
*/
public interface QueryEngineFactory {

public QueryEngine buildQueryEngine(EntityDictionary dictionary);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
package com.yahoo.elide.datastores.aggregation.annotation;

import com.yahoo.elide.datastores.aggregation.metric.Aggregation;
import com.yahoo.elide.datastores.aggregation.schema.metric.Aggregation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.datastores.aggregation.engine;
package com.yahoo.elide.datastores.aggregation.queryengines;

import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.datastores.aggregation.Query;
import com.yahoo.elide.datastores.aggregation.QueryEngine;
import com.yahoo.elide.datastores.aggregation.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.dimension.DimensionType;
import com.yahoo.elide.datastores.aggregation.metric.Metric;
import com.yahoo.elide.datastores.aggregation.schema.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.schema.dimension.DimensionType;
import com.yahoo.elide.datastores.aggregation.schema.metric.Metric;

import com.google.common.base.Preconditions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.datastores.aggregation.engine;
package com.yahoo.elide.datastores.aggregation.queryengines;

import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.utils.coerce.CoerceUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.datastores.aggregation.engine;
package com.yahoo.elide.datastores.aggregation.queryengines.sql;

import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.datastores.aggregation.Query;
import com.yahoo.elide.datastores.aggregation.queryengines.AbstractEntityHydrator;
import com.yahoo.elide.utils.coerce.CoerceUtil;
import lombok.AccessLevel;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE file in project root for terms.
*/

package com.yahoo.elide.datastores.aggregation.engine;
package com.yahoo.elide.datastores.aggregation.queryengines.sql;

import com.yahoo.elide.datastores.aggregation.Query;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE file in project root for terms.
*/

package com.yahoo.elide.datastores.aggregation.engine;
package com.yahoo.elide.datastores.aggregation.queryengines.sql;

import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.core.Path;
Expand All @@ -18,15 +18,15 @@
import com.yahoo.elide.core.sort.Sorting;
import com.yahoo.elide.datastores.aggregation.Query;
import com.yahoo.elide.datastores.aggregation.QueryEngine;
import com.yahoo.elide.datastores.aggregation.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.engine.annotation.FromSubquery;
import com.yahoo.elide.datastores.aggregation.engine.annotation.FromTable;
import com.yahoo.elide.datastores.aggregation.engine.annotation.JoinTo;
import com.yahoo.elide.datastores.aggregation.engine.schema.SQLDimension;
import com.yahoo.elide.datastores.aggregation.engine.schema.SQLSchema;
import com.yahoo.elide.datastores.aggregation.metric.Aggregation;
import com.yahoo.elide.datastores.aggregation.metric.Metric;
import com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery;
import com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable;
import com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.JoinTo;
import com.yahoo.elide.datastores.aggregation.queryengines.sql.schema.SQLDimension;
import com.yahoo.elide.datastores.aggregation.queryengines.sql.schema.SQLSchema;
import com.yahoo.elide.datastores.aggregation.schema.Schema;
import com.yahoo.elide.datastores.aggregation.schema.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.schema.metric.Aggregation;
import com.yahoo.elide.datastores.aggregation.schema.metric.Metric;
import com.yahoo.elide.utils.coerce.CoerceUtil;

import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -398,7 +398,7 @@ private SQLQuery toPageTotalSQL(SQLQuery sql) {
.map(SQLDimension::getColumnName)
.collect(Collectors.joining(","));

String projectionClause = String.format("SELECT COUNT(DISTINCT(%s))", groupByDimensions);
String projectionClause = String.format("COUNT(DISTINCT(%s))", groupByDimensions);

return SQLQuery.builder()
.clientQuery(sql.getClientQuery())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2019, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.datastores.aggregation.queryengines.sql;

import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.datastores.aggregation.QueryEngine;
import com.yahoo.elide.datastores.aggregation.QueryEngineFactory;

import javax.persistence.EntityManagerFactory;

/**
* Object that constructs {@link QueryEngine} based on given entityDictionary and entityManagerFactory.
*/
public class SQLQueryEngineFactory implements QueryEngineFactory {
private EntityManagerFactory emf;

public SQLQueryEngineFactory(EntityManagerFactory emf) {
this.emf = emf;
}

@Override
public QueryEngine buildQueryEngine(EntityDictionary dictionary) {
return new SQLQueryEngine(emf, dictionary);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.datastores.aggregation.engine.annotation;
package com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.datastores.aggregation.engine.annotation;
package com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.datastores.aggregation.engine.annotation;
package com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* See LICENSE file in project root for terms.
*/

package com.yahoo.elide.datastores.aggregation.engine.schema;
package com.yahoo.elide.datastores.aggregation.queryengines.sql.schema;

import com.yahoo.elide.core.Path;
import com.yahoo.elide.datastores.aggregation.annotation.CardinalitySize;
import com.yahoo.elide.datastores.aggregation.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.dimension.DimensionType;
import com.yahoo.elide.datastores.aggregation.schema.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.schema.dimension.DimensionType;

/**
* A dimension but supporting extra metadata needed to generate SQL.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2019, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/

package com.yahoo.elide.datastores.aggregation.queryengines.sql.schema;

import com.yahoo.elide.datastores.aggregation.annotation.Meta;
import com.yahoo.elide.datastores.aggregation.schema.Schema;
import com.yahoo.elide.datastores.aggregation.schema.metric.AggregatedMetric;
import com.yahoo.elide.datastores.aggregation.schema.metric.Aggregation;
import com.yahoo.elide.datastores.aggregation.schema.metric.Metric;

import java.util.List;

/**
* A metric with physical column name needed to generate SQL.
*/
public class SQLMetric extends AggregatedMetric {
private final String columnName;

/**
* Constructor.
*
* @param schema The schema this {@link Metric} belongs to
* @param metricField The entity field or relation that this {@link Metric} represents
* @param annotation Provides static meta data about this {@link Metric}
* @param fieldType The Java type for this entity field or relation
* @param aggregations A list of all supported aggregations on this {@link Metric}
* @param columnName Physical column name of this metric in SQL database.
*/
public SQLMetric(
Schema schema,
String metricField,
Meta annotation,
Class<?> fieldType,
List<Class<? extends Aggregation>> aggregations,
String columnName
) {
super(schema, metricField, annotation, fieldType, aggregations);
this.columnName = columnName;
}

@Override
protected String getMetricName() {
return schema.getAlias() + "." + columnName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@
* See LICENSE file in project root for terms.
*/

package com.yahoo.elide.datastores.aggregation.engine.schema;
package com.yahoo.elide.datastores.aggregation.queryengines.sql.schema;

import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.core.Path;
import com.yahoo.elide.core.filter.FilterPredicate;
import com.yahoo.elide.datastores.aggregation.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.dimension.TimeDimension;
import com.yahoo.elide.datastores.aggregation.engine.annotation.FromSubquery;
import com.yahoo.elide.datastores.aggregation.engine.annotation.FromTable;
import com.yahoo.elide.datastores.aggregation.engine.annotation.JoinTo;
import com.yahoo.elide.datastores.aggregation.annotation.Meta;
import com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery;
import com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable;
import com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.JoinTo;
import com.yahoo.elide.datastores.aggregation.schema.Schema;
import com.yahoo.elide.datastores.aggregation.schema.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.schema.dimension.TimeDimension;
import com.yahoo.elide.datastores.aggregation.schema.metric.Aggregation;
import com.yahoo.elide.datastores.aggregation.schema.metric.Metric;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.JoinColumn;

Expand Down Expand Up @@ -80,6 +85,32 @@ protected Dimension constructDimension(String dimensionField, Class<?> cls, Enti
return new SQLDimension(dim, getJoinColumn(path), getJoinTableAlias(path), path);
}

/**
* Constructs a new {@link SQLMetric} instance.
*
* @param metricField The entity field of the metric being constructed
* @param cls The entity that contains the metric being constructed
* @param entityDictionary The auxiliary object that offers binding info used to construct this {@link SQLMetric}
*
* @return a {@link SQLMetric}
*/
@Override
protected Metric constructMetric(String metricField, Class<?> cls, EntityDictionary entityDictionary) {
Meta metaData = entityDictionary.getAttributeOrRelationAnnotation(cls, Meta.class, metricField);
Class<?> fieldType = entityDictionary.getType(cls, metricField);

List<Class<? extends Aggregation>> aggregations = getAggregations(metricField, cls, entityDictionary);

return new SQLMetric(
this,
metricField,
metaData,
fieldType,
aggregations,
getColumnName(entityDictionary, cls, metricField)
);
}

/**
* Maps a logical entity attribute into a physical SQL column name.
* @param entityDictionary The dictionary for this elide instance.
Expand All @@ -90,6 +121,7 @@ protected Dimension constructDimension(String dimensionField, Class<?> cls, Enti
public static String getColumnName(EntityDictionary entityDictionary, Class<?> clazz, String fieldName) {
Column[] column = entityDictionary.getAttributeOrRelationAnnotations(clazz, Column.class, fieldName);

// this would only be valid for dimension columns
JoinColumn[] joinColumn = entityDictionary.getAttributeOrRelationAnnotations(clazz,
JoinColumn.class, fieldName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* See LICENSE file in project root for terms.
*/

package com.yahoo.elide.datastores.aggregation.engine.schema;
package com.yahoo.elide.datastores.aggregation.queryengines.sql.schema;

import com.yahoo.elide.core.Path;
import com.yahoo.elide.datastores.aggregation.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.dimension.TimeDimension;
import com.yahoo.elide.datastores.aggregation.schema.dimension.Dimension;
import com.yahoo.elide.datastores.aggregation.schema.dimension.TimeDimension;
import com.yahoo.elide.datastores.aggregation.time.TimeGrain;

import java.util.TimeZone;
Expand Down
Loading