use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.
the class AggregationParser method generateDatabaseQuery.
private static String generateDatabaseQuery(List<ExpressionExecutor> expressionExecutors, DBAggregationQueryConfigurationEntry dbAggregationQueryConfigurationEntry, StreamDefinition incomingOutputStreamDefinition, boolean isDistributed, String shardID, boolean isProcessingOnExternalTime, Table aggregationTable, Table parentAggregationTable, List<Variable> groupByVariableList, TimePeriod.Duration duration) {
DBAggregationSelectFunctionTemplate dbAggregationSelectFunctionTemplates = dbAggregationQueryConfigurationEntry.getRdbmsSelectFunctionTemplate();
DBAggregationSelectQueryTemplate dbAggregationSelectQueryTemplate = dbAggregationQueryConfigurationEntry.getRdbmsSelectQueryTemplate();
DBAggregationTimeConversionDurationMapping dbAggregationTimeConversionDurationMapping = dbAggregationQueryConfigurationEntry.getDbAggregationTimeConversionDurationMapping();
List<Attribute> attributeList = incomingOutputStreamDefinition.getAttributeList();
List<String> groupByColumnNames = new ArrayList<>();
StringJoiner outerSelectColumnJoiner = new StringJoiner(", ");
StringJoiner subSelectT1ColumnJoiner = new StringJoiner(", ", SQL_SELECT, " ");
StringJoiner subSelectT2ColumnJoiner = new StringJoiner(", ");
StringJoiner innerSelectT2ColumnJoiner = new StringJoiner(", ", SQL_SELECT, " ");
StringJoiner onConditionBuilder = new StringJoiner(SQL_AND);
StringJoiner subSelectT2OnConditionBuilder = new StringJoiner(SQL_AND);
StringJoiner groupByQueryBuilder = new StringJoiner(", ");
StringJoiner groupByT3QueryBuilder = new StringJoiner(", ");
StringJoiner finalSelectQuery = new StringJoiner(" ");
StringJoiner completeQuery = new StringJoiner(" ");
StringJoiner insertIntoColumns = new StringJoiner(", ");
StringBuilder filterQueryBuilder = new StringBuilder();
StringBuilder insertIntoQueryBuilder = new StringBuilder();
String innerFromClause = SQL_FROM + parentAggregationTable.getTableDefinition().getId();
String innerWhereFilterClause;
String groupByClause;
String innerT2WhereCondition;
StringJoiner innerT2Query = new StringJoiner(" ");
StringJoiner subQueryT1 = new StringJoiner(" ");
StringJoiner subQueryT2 = new StringJoiner(" ");
attributeList.stream().forEach(attribute -> insertIntoColumns.add(attribute.getName()));
int i = 0;
insertIntoQueryBuilder.append(dbAggregationSelectQueryTemplate.getRecordInsertQuery().replace(PLACEHOLDER_TABLE_NAME, aggregationTable.getTableDefinition().getId()).replace(PLACEHOLDER_COLUMNS, insertIntoColumns.toString()));
filterQueryBuilder.append(" (").append(AGG_START_TIMESTAMP_COL).append(" >= ?").append(" AND ").append(AGG_START_TIMESTAMP_COL).append(" < ? ").append(") ");
if (isDistributed) {
filterQueryBuilder.append(" AND ").append(AGG_SHARD_ID_COL).append(" = '").append(shardID).append("' ");
groupByQueryBuilder.add(AGG_SHARD_ID_COL);
groupByT3QueryBuilder.add(INNER_SELECT_QUERY_REF_T3 + "." + AGG_SHARD_ID_COL);
innerSelectT2ColumnJoiner.add(AGG_SHARD_ID_COL);
subSelectT2OnConditionBuilder.add(parentAggregationTable.getTableDefinition().getId() + "." + AGG_SHARD_ID_COL + EQUALS + INNER_SELECT_QUERY_REF_T3 + "." + AGG_SHARD_ID_COL);
if (isProcessingOnExternalTime) {
subSelectT1ColumnJoiner.add(AGG_SHARD_ID_COL);
}
}
if (isProcessingOnExternalTime) {
groupByVariableList.stream().forEach(variable -> {
groupByColumnNames.add(variable.getAttributeName());
groupByQueryBuilder.add(variable.getAttributeName());
groupByT3QueryBuilder.add(INNER_SELECT_QUERY_REF_T3 + "." + variable.getAttributeName());
onConditionBuilder.add(SUB_SELECT_QUERY_REF_T1 + "." + variable.getAttributeName() + EQUALS + SUB_SELECT_QUERY_REF_T2 + "." + variable.getAttributeName());
subSelectT2OnConditionBuilder.add(parentAggregationTable.getTableDefinition().getId() + "." + variable.getAttributeName() + EQUALS + INNER_SELECT_QUERY_REF_T3 + "." + variable.getAttributeName());
});
innerT2WhereCondition = INNER_SELECT_QUERY_REF_T3 + "." + groupByVariableList.get(0).getAttributeName() + SQL_NOT_NULL;
for (ExpressionExecutor expressionExecutor : expressionExecutors) {
if (expressionExecutor instanceof VariableExpressionExecutor) {
VariableExpressionExecutor variableExpressionExecutor = (VariableExpressionExecutor) expressionExecutor;
if (variableExpressionExecutor.getAttribute().getName().equals(AGG_START_TIMESTAMP_COL)) {
outerSelectColumnJoiner.add(" ? " + SQL_AS + variableExpressionExecutor.getAttribute().getName());
} else if (!variableExpressionExecutor.getAttribute().getName().equals(AGG_EXTERNAL_TIMESTAMP_COL)) {
if (groupByColumnNames.contains(variableExpressionExecutor.getAttribute().getName())) {
subSelectT2ColumnJoiner.add(INNER_SELECT_QUERY_REF_T3 + "." + variableExpressionExecutor.getAttribute().getName() + SQL_AS + variableExpressionExecutor.getAttribute().getName());
outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + variableExpressionExecutor.getAttribute().getName() + SQL_AS + attributeList.get(i).getName());
subSelectT1ColumnJoiner.add(variableExpressionExecutor.getAttribute().getName());
innerSelectT2ColumnJoiner.add(variableExpressionExecutor.getAttribute().getName());
} else {
subSelectT2ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMaxFunction().replace(PLACEHOLDER_COLUMN, variableExpressionExecutor.getAttribute().getName()) + SQL_AS + variableExpressionExecutor.getAttribute().getName());
outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T2 + "." + variableExpressionExecutor.getAttribute().getName() + SQL_AS + attributeList.get(i).getName());
}
}
} else if (expressionExecutor instanceof IncrementalAggregateBaseTimeFunctionExecutor) {
if (attributeList.get(i).getName().equals(AGG_EXTERNAL_TIMESTAMP_COL)) {
outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + AGG_EXTERNAL_TIMESTAMP_COL + SQL_AS + AGG_EXTERNAL_TIMESTAMP_COL);
subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getTimeConversionFunction().replace(PLACEHOLDER_COLUMN, AGG_EXTERNAL_TIMESTAMP_COL).replace(PLACEHOLDER_DURATION, dbAggregationTimeConversionDurationMapping.getDurationMapping(duration)) + SQL_AS + AGG_EXTERNAL_TIMESTAMP_COL);
subSelectT2ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getTimeConversionFunction().replace(PLACEHOLDER_COLUMN, AGG_EXTERNAL_TIMESTAMP_COL).replace(PLACEHOLDER_DURATION, dbAggregationTimeConversionDurationMapping.getDurationMapping(duration)) + SQL_AS + AGG_EXTERNAL_TIMESTAMP_COL);
onConditionBuilder.add(SUB_SELECT_QUERY_REF_T1 + "." + AGG_EXTERNAL_TIMESTAMP_COL + EQUALS + SUB_SELECT_QUERY_REF_T2 + "." + AGG_EXTERNAL_TIMESTAMP_COL);
} else {
outerSelectColumnJoiner.add(" ? " + SQL_AS + attributeList.get(i).getName());
}
} else if (expressionExecutor instanceof MaxAttributeAggregatorExecutor) {
if (attributeList.get(i).getName().equals(AGG_LAST_TIMESTAMP_COL)) {
innerSelectT2ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMaxFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()) + SQL_AS + attributeList.get(i).getName());
subSelectT2ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMaxFunction().replace(PLACEHOLDER_COLUMN, INNER_SELECT_QUERY_REF_T3 + "." + attributeList.get(i).getName()) + SQL_AS + attributeList.get(i).getName());
outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T2 + "." + attributeList.get(i).getName() + SQL_AS + attributeList.get(i).getName());
subSelectT2OnConditionBuilder.add(parentAggregationTable.getTableDefinition().getId() + "." + attributeList.get(i).getName() + EQUALS + INNER_SELECT_QUERY_REF_T3 + "." + attributeList.get(i).getName());
} else {
outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + attributeList.get(i).getName() + SQL_AS + attributeList.get(i).getName());
subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMaxFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()) + SQL_AS + attributeList.get(i).getName());
}
} else if (expressionExecutor instanceof MinAttributeAggregatorExecutor) {
outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + attributeList.get(i).getName() + SQL_AS + attributeList.get(i).getName());
subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMinFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()) + SQL_AS + attributeList.get(i).getName());
} else if (expressionExecutor instanceof ConstantExpressionExecutor) {
outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + attributeList.get(i).getName() + SQL_AS + attributeList.get(i).getName());
} else if (expressionExecutor instanceof SumAttributeAggregatorExecutor) {
outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + attributeList.get(i).getName() + SQL_AS + attributeList.get(i).getName());
subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getSumFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()) + SQL_AS + attributeList.get(i).getName());
}
i++;
}
groupByQueryBuilder.add(dbAggregationSelectFunctionTemplates.getTimeConversionFunction().replace(PLACEHOLDER_COLUMN, AGG_EXTERNAL_TIMESTAMP_COL).replace(PLACEHOLDER_DURATION, dbAggregationTimeConversionDurationMapping.getDurationMapping(duration)));
groupByT3QueryBuilder.add(dbAggregationSelectFunctionTemplates.getTimeConversionFunction().replace(PLACEHOLDER_COLUMN, AGG_EXTERNAL_TIMESTAMP_COL).replace(PLACEHOLDER_DURATION, dbAggregationTimeConversionDurationMapping.getDurationMapping(duration)));
groupByClause = dbAggregationSelectQueryTemplate.getGroupByClause().replace(PLACEHOLDER_COLUMNS, groupByQueryBuilder.toString());
innerWhereFilterClause = dbAggregationSelectQueryTemplate.getWhereClause().replace(PLACEHOLDER_CONDITION, filterQueryBuilder.toString());
innerT2Query.add(innerSelectT2ColumnJoiner.toString()).add(innerFromClause).add(innerWhereFilterClause).add(groupByClause);
subQueryT1.add(subSelectT1ColumnJoiner.toString()).add(innerFromClause).add(innerWhereFilterClause).add(groupByClause);
subQueryT2.add(dbAggregationSelectQueryTemplate.getSelectQueryWithInnerSelect().replace(PLACEHOLDER_SELECTORS, subSelectT2ColumnJoiner.toString()).replace(PLACEHOLDER_TABLE_NAME, parentAggregationTable.getTableDefinition().getId()).replace(PLACEHOLDER_INNER_QUERY_2, innerT2Query.toString()).replace(PLACEHOLDER_ON_CONDITION, subSelectT2OnConditionBuilder.toString()).replace(PLACEHOLDER_CONDITION, innerT2WhereCondition).replace(PLACEHOLDER_COLUMNS, groupByT3QueryBuilder.toString()));
finalSelectQuery.add(dbAggregationSelectQueryTemplate.getJoinClause().replace(PLACEHOLDER_SELECTORS, outerSelectColumnJoiner.toString()).replace(PLACEHOLDER_FROM_CONDITION, subQueryT1.toString()).replace(PLACEHOLDER_INNER_QUERY_1, subQueryT2.toString()).replace(PLACEHOLDER_CONDITION, onConditionBuilder.toString()));
completeQuery.add(insertIntoQueryBuilder.toString()).add(finalSelectQuery.toString());
} else {
for (ExpressionExecutor executor : expressionExecutors) {
if (executor instanceof VariableExpressionExecutor) {
VariableExpressionExecutor variableExpressionExecutor = (VariableExpressionExecutor) executor;
if (variableExpressionExecutor.getAttribute().getName().equals(AGG_START_TIMESTAMP_COL)) {
subSelectT1ColumnJoiner.add("? " + SQL_AS + variableExpressionExecutor.getAttribute().getName());
} else {
subSelectT1ColumnJoiner.add(variableExpressionExecutor.getAttribute().getName());
groupByQueryBuilder.add(variableExpressionExecutor.getAttribute().getName());
}
} else if (executor instanceof ConstantExpressionExecutor) {
if (((ConstantExpressionExecutor) executor).getValue() != null) {
subSelectT1ColumnJoiner.add("'" + ((ConstantExpressionExecutor) executor).getValue() + "' " + SQL_AS + attributeList.get(i).getName());
} else {
subSelectT1ColumnJoiner.add(attributeList.get(i).getName());
}
} else if (executor instanceof SumAttributeAggregatorExecutor) {
subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getSumFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()).concat(SQL_AS).concat(attributeList.get(i).getName()));
} else if (executor instanceof MinAttributeAggregatorExecutor) {
subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMinFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()).concat(SQL_AS).concat(attributeList.get(i).getName()));
} else if (executor instanceof MaxAttributeAggregatorExecutor) {
subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMaxFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()).concat(SQL_AS).concat(attributeList.get(i).getName()));
}
i++;
}
completeQuery.add(insertIntoQueryBuilder.toString()).add(subSelectT1ColumnJoiner.toString()).add(innerFromClause).add(SQL_WHERE + filterQueryBuilder).add(dbAggregationSelectQueryTemplate.getGroupByClause().replace(PLACEHOLDER_COLUMNS, groupByQueryBuilder.toString()));
}
return completeQuery.toString();
}
use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.
the class AggregationParser method populateIncomingAggregatorsAndExecutors.
private static void populateIncomingAggregatorsAndExecutors(AggregationDefinition aggregationDefinition, SiddhiQueryContext siddhiQueryContext, Map<String, Table> tableMap, List<VariableExpressionExecutor> incomingVariableExpressionExecutors, MetaStreamEvent incomingMetaStreamEvent, List<ExpressionExecutor> incomingExpressionExecutors, List<IncrementalAttributeAggregator> incrementalAttributeAggregators, List<Variable> groupByVariableList, List<Expression> outputExpressions, boolean isProcessingOnExternalTime, boolean isDistributed, String shardId) {
boolean isLatestEventAdded = false;
ExpressionExecutor timestampExecutor = getTimeStampExecutor(siddhiQueryContext, tableMap, incomingVariableExpressionExecutors, incomingMetaStreamEvent);
Attribute timestampAttribute = new Attribute(AGG_START_TIMESTAMP_COL, Attribute.Type.LONG);
incomingMetaStreamEvent.addOutputData(timestampAttribute);
incomingExpressionExecutors.add(timestampExecutor);
if (isDistributed) {
ExpressionExecutor nodeIdExpExecutor = new ConstantExpressionExecutor(shardId, Attribute.Type.STRING);
incomingExpressionExecutors.add(nodeIdExpExecutor);
incomingMetaStreamEvent.addOutputData(new Attribute(AGG_SHARD_ID_COL, Attribute.Type.STRING));
}
ExpressionExecutor externalTimestampExecutor = null;
if (isProcessingOnExternalTime) {
Expression externalTimestampExpression = aggregationDefinition.getAggregateAttribute();
externalTimestampExecutor = ExpressionParser.parseExpression(externalTimestampExpression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
if (externalTimestampExecutor.getReturnType() == Attribute.Type.STRING) {
Expression expression = AttributeFunction.function("incrementalAggregator", "timestampInMilliseconds", externalTimestampExpression);
externalTimestampExecutor = ExpressionParser.parseExpression(expression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
} else if (externalTimestampExecutor.getReturnType() != Attribute.Type.LONG) {
throw new SiddhiAppCreationException("Aggregation Definition '" + aggregationDefinition.getId() + "'s timestamp attribute expects " + "long or string, but found " + externalTimestampExecutor.getReturnType() + ". Hence, can't " + "create the siddhi app '" + siddhiQueryContext.getSiddhiAppContext().getName() + "'", externalTimestampExpression.getQueryContextStartIndex(), externalTimestampExpression.getQueryContextEndIndex());
}
Attribute externalTimestampAttribute = new Attribute(AGG_EXTERNAL_TIMESTAMP_COL, Attribute.Type.LONG);
incomingMetaStreamEvent.addOutputData(externalTimestampAttribute);
incomingExpressionExecutors.add(externalTimestampExecutor);
}
AbstractDefinition incomingLastInputStreamDefinition = incomingMetaStreamEvent.getLastInputDefinition();
for (Variable groupByVariable : groupByVariableList) {
incomingMetaStreamEvent.addOutputData(incomingLastInputStreamDefinition.getAttributeList().get(incomingLastInputStreamDefinition.getAttributePosition(groupByVariable.getAttributeName())));
incomingExpressionExecutors.add(ExpressionParser.parseExpression(groupByVariable, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext));
}
// Add AGG_TIMESTAMP to output as well
aggregationDefinition.getAttributeList().add(timestampAttribute);
// check and set whether the aggregation in happened on an external timestamp
if (isProcessingOnExternalTime) {
outputExpressions.add(Expression.variable(AGG_EXTERNAL_TIMESTAMP_COL));
} else {
outputExpressions.add(Expression.variable(AGG_START_TIMESTAMP_COL));
}
for (OutputAttribute outputAttribute : aggregationDefinition.getSelector().getSelectionList()) {
Expression expression = outputAttribute.getExpression();
// If the select contains the aggregation function expression type will be AttributeFunction
if (expression instanceof AttributeFunction) {
IncrementalAttributeAggregator incrementalAggregator = null;
try {
incrementalAggregator = (IncrementalAttributeAggregator) SiddhiClassLoader.loadExtensionImplementation(new AttributeFunction("incrementalAggregator", ((AttributeFunction) expression).getName(), ((AttributeFunction) expression).getParameters()), IncrementalAttributeAggregatorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
} catch (SiddhiAppCreationException ex) {
try {
SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, FunctionExecutorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
processAggregationSelectors(aggregationDefinition, siddhiQueryContext, tableMap, incomingVariableExpressionExecutors, incomingMetaStreamEvent, incomingExpressionExecutors, outputExpressions, outputAttribute, expression);
} catch (SiddhiAppCreationException e) {
throw new SiddhiAppCreationException("'" + ((AttributeFunction) expression).getName() + "' is neither a incremental attribute aggregator extension or a function" + " extension", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
}
}
if (incrementalAggregator != null) {
initIncrementalAttributeAggregator(incomingLastInputStreamDefinition, (AttributeFunction) expression, incrementalAggregator);
incrementalAttributeAggregators.add(incrementalAggregator);
aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), incrementalAggregator.getReturnType()));
outputExpressions.add(incrementalAggregator.aggregate());
}
} else {
if (expression instanceof Variable && groupByVariableList.contains(expression)) {
Attribute groupByAttribute = null;
for (Attribute attribute : incomingMetaStreamEvent.getOutputData()) {
if (attribute.getName().equals(((Variable) expression).getAttributeName())) {
groupByAttribute = attribute;
break;
}
}
if (groupByAttribute == null) {
throw new SiddhiAppCreationException("Expected GroupBy attribute '" + ((Variable) expression).getAttributeName() + "' not used in aggregation '" + siddhiQueryContext.getName() + "' processing.", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
}
aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), groupByAttribute.getType()));
outputExpressions.add(Expression.variable(groupByAttribute.getName()));
} else {
isLatestEventAdded = true;
processAggregationSelectors(aggregationDefinition, siddhiQueryContext, tableMap, incomingVariableExpressionExecutors, incomingMetaStreamEvent, incomingExpressionExecutors, outputExpressions, outputAttribute, expression);
}
}
}
if (isProcessingOnExternalTime && isLatestEventAdded) {
Attribute lastEventTimeStamp = new Attribute(AGG_LAST_TIMESTAMP_COL, Attribute.Type.LONG);
incomingMetaStreamEvent.addOutputData(lastEventTimeStamp);
incomingExpressionExecutors.add(externalTimestampExecutor);
}
}
use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.
the class EventHolderPasser method parse.
public static EventHolder parse(AbstractDefinition tableDefinition, StreamEventFactory tableStreamEventFactory, SiddhiAppContext siddhiAppContext, boolean isCacheTable) {
ZeroStreamEventConverter eventConverter = new ZeroStreamEventConverter();
PrimaryKeyReferenceHolder[] primaryKeyReferenceHolders = null;
Map<String, Integer> indexMetaData = new HashMap<String, Integer>();
// primaryKey.
Annotation primaryKeyAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_PRIMARY_KEY, tableDefinition.getAnnotations());
if (primaryKeyAnnotation != null) {
if (primaryKeyAnnotation.getElements().size() == 0) {
throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_PRIMARY_KEY + " annotation " + "contains " + primaryKeyAnnotation.getElements().size() + " element, at '" + tableDefinition.getId() + "'");
}
primaryKeyReferenceHolders = primaryKeyAnnotation.getElements().stream().map(element -> element.getValue().trim()).map(key -> new PrimaryKeyReferenceHolder(key, tableDefinition.getAttributePosition(key))).toArray(PrimaryKeyReferenceHolder[]::new);
}
for (Annotation indexAnnotation : AnnotationHelper.getAnnotations(SiddhiConstants.ANNOTATION_INDEX, tableDefinition.getAnnotations())) {
if (indexAnnotation.getElements().size() == 0) {
throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_INDEX + " annotation of " + "in-memory table should contain only one index element, but found " + indexAnnotation.getElements().size() + " element", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
} else if (indexAnnotation.getElements().size() > 1) {
throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_INDEX + " annotation of the " + "in-memory table should only contain one index element but found " + indexAnnotation.getElements().size() + " elements. To use multiple indexes, " + "define multiple '@index(<index key>)' annotations with one index element " + "per each index key", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
}
for (Element element : indexAnnotation.getElements()) {
Integer previousValue = indexMetaData.put(element.getValue().trim(), tableDefinition.getAttributePosition(element.getValue().trim()));
if (previousValue != null) {
throw new SiddhiAppValidationException("Multiple " + SiddhiConstants.ANNOTATION_INDEX + " " + "annotations defined with same attribute '" + element.getValue().trim() + "', at '" + tableDefinition.getId() + "'", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
}
}
}
// not support indexBy.
Annotation indexByAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_INDEX_BY, tableDefinition.getAnnotations());
if (indexByAnnotation != null) {
throw new OperationNotSupportedException(SiddhiConstants.ANNOTATION_INDEX_BY + " annotation is not " + "supported anymore, please use @PrimaryKey or @Index annotations instead," + " at '" + tableDefinition.getId() + "'");
}
if (primaryKeyReferenceHolders != null || indexMetaData.size() > 0) {
boolean isNumeric = false;
if (primaryKeyReferenceHolders != null) {
if (primaryKeyReferenceHolders.length == 1) {
Attribute.Type type = tableDefinition.getAttributeType(primaryKeyReferenceHolders[0].getPrimaryKeyAttribute());
if (type == Attribute.Type.DOUBLE || type == Attribute.Type.FLOAT || type == Attribute.Type.INT || type == Attribute.Type.LONG) {
isNumeric = true;
}
}
}
if (isCacheTable) {
return new IndexEventHolderForCache(tableStreamEventFactory, eventConverter, primaryKeyReferenceHolders, isNumeric, indexMetaData, tableDefinition, siddhiAppContext);
} else {
return new IndexEventHolder(tableStreamEventFactory, eventConverter, primaryKeyReferenceHolders, isNumeric, indexMetaData, tableDefinition, siddhiAppContext);
}
} else {
MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
for (Attribute attribute : tableDefinition.getAttributeList()) {
metaStreamEvent.addOutputData(attribute);
}
StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, tableStreamEventFactory);
return new ListEventHolder(tableStreamEventFactory, eventConverter, new StreamEventClonerHolder(streamEventCloner));
}
}
use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.
the class MatcherParser method constructMatchingMetaStateHolder.
public static MatchingMetaInfoHolder constructMatchingMetaStateHolder(MetaComplexEvent matchingMetaComplexEvent, int defaultStreamEventIndex, AbstractDefinition candsidateDefinition, int currentState) {
int storeEventIndex = 0;
MetaStreamEvent tableStreamEvent = new MetaStreamEvent();
tableStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
tableStreamEvent.addInputDefinition(candsidateDefinition);
for (Attribute attribute : candsidateDefinition.getAttributeList()) {
tableStreamEvent.addOutputData(attribute);
}
MetaStateEvent metaStateEvent = null;
if (matchingMetaComplexEvent instanceof MetaStreamEvent) {
metaStateEvent = new MetaStateEvent(2);
metaStateEvent.addEvent(((MetaStreamEvent) matchingMetaComplexEvent));
metaStateEvent.addEvent(tableStreamEvent);
storeEventIndex = 1;
defaultStreamEventIndex = 0;
if (currentState == SiddhiConstants.UNKNOWN_STATE) {
currentState = defaultStreamEventIndex;
}
} else {
MetaStreamEvent[] metaStreamEvents = ((MetaStateEvent) matchingMetaComplexEvent).getMetaStreamEvents();
// for join
for (; storeEventIndex < metaStreamEvents.length; storeEventIndex++) {
MetaStreamEvent metaStreamEvent = metaStreamEvents[storeEventIndex];
if (storeEventIndex != defaultStreamEventIndex && metaStreamEvent.getLastInputDefinition().equalsIgnoreAnnotations(candsidateDefinition)) {
metaStateEvent = ((MetaStateEvent) matchingMetaComplexEvent);
break;
}
}
if (metaStateEvent == null) {
metaStateEvent = new MetaStateEvent(metaStreamEvents.length + 1);
for (MetaStreamEvent metaStreamEvent : metaStreamEvents) {
metaStateEvent.addEvent(metaStreamEvent);
}
metaStateEvent.addEvent(tableStreamEvent);
storeEventIndex = metaStreamEvents.length;
}
}
return new MatchingMetaInfoHolder(metaStateEvent, defaultStreamEventIndex, storeEventIndex, metaStateEvent.getMetaStreamEvent(defaultStreamEventIndex).getLastInputDefinition(), candsidateDefinition, currentState);
}
use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.
the class ErrorHandlerUtils method constructErrorRecordString.
public static String constructErrorRecordString(ComplexEventChunk<StateEvent> eventChunk, boolean isObjectColumnPresent, TableDefinition tableDefinition, Exception e) {
JsonObject payloadJson = new JsonObject();
if (isObjectColumnPresent || e instanceof ConnectionUnavailableException) {
payloadJson.addProperty("isEditable", false);
} else {
payloadJson.addProperty("isEditable", true);
}
JsonArray attributes = new JsonArray();
JsonArray records = new JsonArray();
for (Attribute attribute : tableDefinition.getAttributeList()) {
JsonObject attributeJson = new JsonObject();
attributeJson.add("name", new JsonPrimitive(attribute.getName()));
attributeJson.add("type", new JsonPrimitive(String.valueOf(attribute.getType())));
attributes.add(attributeJson);
}
payloadJson.add("attributes", attributes);
while (eventChunk.hasNext()) {
StateEvent stateEvent = eventChunk.next();
for (StreamEvent streamEvent : stateEvent.getStreamEvents()) {
if (streamEvent != null) {
JsonArray record = new JsonArray();
for (Object item : streamEvent.getOutputData()) {
if (item == null) {
record.add(JsonNull.INSTANCE);
} else {
record.add(String.valueOf(item));
}
}
records.add(record);
}
}
}
payloadJson.add("records", records);
return payloadJson.toString();
}
Aggregations