use of com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType in project elide by yahoo.
the class TableType method getArgumentDefinitions.
private static ArgumentDefinition[] getArgumentDefinitions(List<Argument> arguments) {
int numArguments = arguments == null ? 0 : arguments.size();
ArgumentDefinition[] definitions = new ArgumentDefinition[numArguments];
for (int idx = 0; idx < numArguments; idx++) {
Argument argument = arguments.get(idx);
definitions[idx] = new ArgumentDefinition() {
@Override
public String name() {
return argument.getName();
}
@Override
public String description() {
return argument.getDescription();
}
@Override
public ValueType type() {
return ValueType.valueOf(argument.getType().toUpperCase(Locale.ROOT));
}
@Override
public TableSource tableSource() {
return buildTableSource(argument.getTableSource());
}
@Override
public String[] values() {
return argument.getValues().toArray(new String[0]);
}
@Override
public String defaultValue() {
Object value = argument.getDefaultValue();
return value == null ? null : value.toString();
}
@Override
public Class<? extends Annotation> annotationType() {
return ArgumentDefinition.class;
}
};
}
return definitions;
}
use of com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType in project elide by yahoo.
the class SQLMetricProjection method nest.
@Override
public Pair<ColumnProjection, Set<ColumnProjection>> nest(Queryable source, MetaDataStore metaDataStore, boolean joinInOuter) {
SQLDialect dialect = source.getConnectionDetails().getDialect();
String sql = toSQL(source, metaDataStore);
SqlParser sqlParser = SqlParser.create(sql, CalciteUtils.constructParserConfig(dialect));
SqlNode node;
try {
node = sqlParser.parseExpression();
} catch (SqlParseException e) {
throw new IllegalStateException(e);
}
CalciteInnerAggregationExtractor innerExtractor = new CalciteInnerAggregationExtractor(dialect);
List<List<String>> innerAggExpressions = node.accept(innerExtractor);
List<List<String>> innerAggLabels = innerAggExpressions.stream().map(list -> list.stream().map((expression) -> getAggregationLabel(dialect.getCalciteDialect(), expression)).collect(Collectors.toList())).collect(Collectors.toList());
Set<ColumnProjection> innerAggProjections = new LinkedHashSet<>();
Iterator<String> labelIt = innerAggLabels.stream().flatMap(List::stream).iterator();
Iterator<String> expressionIt = innerAggExpressions.stream().flatMap(List::stream).iterator();
while (labelIt.hasNext() && expressionIt.hasNext()) {
String innerAggExpression = expressionIt.next();
String innerAggLabel = labelIt.next();
innerAggProjections.add(SQLMetricProjection.builder().projected(true).name(innerAggLabel).alias(innerAggLabel).expression(innerAggExpression).columnType(columnType).valueType(valueType).arguments(arguments).build());
}
CalciteOuterAggregationExtractor outerExtractor = new CalciteOuterAggregationExtractor(dialect, innerAggLabels);
SqlNode transformedParseTree = node.accept(outerExtractor);
String outerAggExpression = transformedParseTree.toSqlString(dialect.getCalciteDialect()).getSql();
// replace INNER_AGG_... with {{$INNER_AGG...}}
outerAggExpression = outerAggExpression.replaceAll(dialect.getBeginQuote() + "?(" + getAggregationLabelPrefix(dialect.getCalciteDialect()) + "\\w+)" + dialect.getEndQuote() + "?", "{{\\$" + "$1" + "}}");
String columnId = source.isRoot() ? getName() : getAlias();
boolean inProjection = source.getColumnProjection(columnId, arguments, true) != null;
ColumnProjection outerProjection = SQLMetricProjection.builder().projected(inProjection).expression(outerAggExpression).name(name).alias(alias).valueType(valueType).columnType(columnType).arguments(arguments).build();
return Pair.of(outerProjection, innerAggProjections);
}
use of com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType in project elide by yahoo.
the class EntityHydrator method coerceObjectToEntity.
/**
* Coerces results from a {@link Query} into an Object.
*
* @param result a fieldName-value map
* @param counter Monotonically increasing number to generate IDs.
* @return A hydrated entity object.
*/
protected Object coerceObjectToEntity(Map<String, Object> result, MutableInt counter) {
Table table = getBaseTable(query);
Type<?> entityClass = entityDictionary.getEntityClass(table.getName(), table.getVersion());
// Construct the object.
Object entityInstance;
try {
entityInstance = entityClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
result.forEach((fieldName, value) -> {
ColumnProjection columnProjection = query.getColumnProjection(fieldName);
Column column = table.getColumn(Column.class, columnProjection.getName());
Type<?> fieldType = getType(entityClass, columnProjection);
Attribute attribute = projectionToAttribute(columnProjection, fieldType);
ValueType valueType = column.getValueType();
if (entityInstance instanceof ParameterizedModel) {
// This is an ENUM_TEXT or ENUM_ORDINAL type.
if (// Java enums can be coerced directly via CoerceUtil - so skip them.
!fieldType.isEnum() && valueType == ValueType.TEXT && column.getValues() != null && !column.getValues().isEmpty()) {
value = convertToEnumValue(value, column.getValues());
}
((ParameterizedModel) entityInstance).addAttributeValue(attribute, CoerceUtil.coerce(value, fieldType));
} else {
getEntityDictionary().setValue(entityInstance, fieldName, value);
}
});
// Set the ID (it must be coerced from an integer)
getEntityDictionary().setValue(entityInstance, getEntityDictionary().getIdFieldName(entityClass), counter.getAndIncrement());
return entityInstance;
}
use of com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType in project elide by yahoo.
the class Table method constructColumns.
/**
* Construct all columns of this table.
*
* @param cls table class
* @param dictionary dictionary contains the table class
* @return all resolved column metadata
*/
private Set<Column> constructColumns(Type<?> cls, EntityDictionary dictionary) {
EntityBinding binding = dictionary.getEntityBinding(cls);
Set<Column> columns = new HashSet<>();
// TODO - refactor entity binding to have classes for attributes & relationships.
binding.fieldsToValues.forEach((name, field) -> {
if (EntityBinding.isIdField(field)) {
return;
}
ValueType valueType = getValueType(cls, name, dictionary);
if (valueType == ValueType.UNKNOWN) {
return;
}
if (isMetricField(dictionary, cls, name)) {
columns.add(constructMetric(name, dictionary));
} else if (dictionary.attributeOrRelationAnnotationExists(cls, name, Temporal.class)) {
columns.add(constructTimeDimension(name, dictionary));
} else {
columns.add(constructDimension(name, dictionary));
}
});
// add id field if exists
if (dictionary.getIdFieldName(cls) != null) {
String idFieldName = dictionary.getIdFieldName(cls);
if (AggregationDataStore.isAggregationStoreModel(cls)) {
columns.add(constructMetric(idFieldName, dictionary));
} else {
columns.add(constructDimension(idFieldName, dictionary));
}
}
return columns;
}
Aggregations