use of org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException in project ballerina by ballerina-lang.
the class EventHolderPasser method parse.
public static EventHolder parse(AbstractDefinition tableDefinition, StreamEventPool tableStreamEventPool, SiddhiAppContext siddhiAppContext) {
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);
}
// indexes.
Annotation indexAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_INDEX, tableDefinition.getAnnotations());
if (indexAnnotation != null) {
if (indexAnnotation.getElements().size() == 0) {
throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_INDEX + " annotation contains " + indexAnnotation.getElements().size() + " element");
}
for (Element element : indexAnnotation.getElements()) {
Integer previousValue = indexMetaData.put(element.getValue().trim(), tableDefinition.getAttributePosition(element.getValue().trim()));
if (previousValue != null) {
throw new SiddhiAppCreationException("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;
}
}
}
return new IndexEventHolder(tableStreamEventPool, eventConverter, primaryKeyReferenceHolders, isNumeric, indexMetaData, tableDefinition, siddhiAppContext);
} else {
return new ListEventHolder(tableStreamEventPool, eventConverter);
}
}
use of org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException in project ballerina by ballerina-lang.
the class ExpressionParser method parseVariable.
/**
* Parse and validate the given Siddhi variable and return a VariableExpressionExecutor.
*
* @param variable Variable to be parsed
* @param metaEvent Meta event used to collect execution info of stream associated with query
* @param currentState Current State Number
* @param executorList List to hold VariableExpressionExecutors to update after query parsing @return
* VariableExpressionExecutor representing given variable
*/
private static ExpressionExecutor parseVariable(Variable variable, MetaComplexEvent metaEvent, int currentState, List<VariableExpressionExecutor> executorList, int defaultStreamEventIndex) {
String attributeName = variable.getAttributeName();
int[] eventPosition = new int[2];
if (variable.getStreamIndex() != null) {
if (variable.getStreamIndex() <= LAST) {
eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex() + 1;
} else {
eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex();
}
} else {
eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = defaultStreamEventIndex;
}
eventPosition[STREAM_EVENT_CHAIN_INDEX] = UNKNOWN_STATE;
if (metaEvent instanceof MetaStreamEvent) {
MetaStreamEvent metaStreamEvent = (MetaStreamEvent) metaEvent;
AbstractDefinition abstractDefinition;
Attribute.Type type;
if (currentState == HAVING_STATE) {
abstractDefinition = metaStreamEvent.getOutputStreamDefinition();
type = abstractDefinition.getAttributeType(attributeName);
eventPosition[STREAM_EVENT_CHAIN_INDEX] = HAVING_STATE;
} else {
abstractDefinition = metaStreamEvent.getLastInputDefinition();
type = abstractDefinition.getAttributeType(attributeName);
((MetaStreamEvent) metaEvent).addData(new Attribute(attributeName, type));
}
VariableExpressionExecutor variableExpressionExecutor = new VariableExpressionExecutor(new Attribute(attributeName, type), eventPosition[STREAM_EVENT_CHAIN_INDEX], eventPosition[STREAM_EVENT_INDEX_IN_CHAIN]);
if (((MetaStreamEvent) metaEvent).getEventType() != MetaStreamEvent.EventType.DEFAULT) {
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_TYPE_INDEX] = OUTPUT_DATA_INDEX;
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_INDEX_IN_TYPE] = abstractDefinition.getAttributePosition(variableExpressionExecutor.getAttribute().getName());
}
if (executorList != null) {
executorList.add(variableExpressionExecutor);
}
return variableExpressionExecutor;
} else {
MetaStateEvent metaStateEvent = (MetaStateEvent) metaEvent;
Attribute.Type type = null;
AbstractDefinition definition = null;
String firstInput = null;
if (variable.getStreamId() == null) {
MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
if (currentState == HAVING_STATE) {
definition = metaStateEvent.getOutputStreamDefinition();
try {
type = definition.getAttributeType(attributeName);
eventPosition[STREAM_EVENT_CHAIN_INDEX] = HAVING_STATE;
} catch (AttributeNotExistException e) {
currentState = UNKNOWN_STATE;
}
}
if (currentState == UNKNOWN_STATE) {
for (int i = 0; i < metaStreamEvents.length; i++) {
MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
definition = metaStreamEvent.getLastInputDefinition();
if (type == null) {
try {
type = definition.getAttributeType(attributeName);
firstInput = "Input Stream: " + definition.getId() + " with " + "reference: " + metaStreamEvent.getInputReferenceId();
eventPosition[STREAM_EVENT_CHAIN_INDEX] = i;
} catch (AttributeNotExistException e) {
// do nothing
}
} else {
try {
definition.getAttributeType(attributeName);
throw new SiddhiAppValidationException(firstInput + " and Input Stream: " + definition.getId() + " with " + "reference: " + metaStreamEvent.getInputReferenceId() + " contains attribute " + "with same" + " name '" + attributeName + "'");
} catch (AttributeNotExistException e) {
// do nothing as its expected
}
}
}
} else if (currentState >= 0) {
MetaStreamEvent metaStreamEvent = metaStreamEvents[currentState];
definition = metaStreamEvent.getLastInputDefinition();
try {
type = definition.getAttributeType(attributeName);
eventPosition[STREAM_EVENT_CHAIN_INDEX] = currentState;
} catch (AttributeNotExistException e) {
throw new SiddhiAppValidationException(e.getMessageWithOutContext() + " Input Stream: " + definition.getId() + " with reference: " + metaStreamEvent.getInputReferenceId(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex());
}
}
} else {
MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
for (int i = 0, metaStreamEventsLength = metaStreamEvents.length; i < metaStreamEventsLength; i++) {
MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
definition = metaStreamEvent.getLastInputDefinition();
if (metaStreamEvent.getInputReferenceId() == null) {
if (definition.getId().equals(variable.getStreamId())) {
type = definition.getAttributeType(attributeName);
eventPosition[STREAM_EVENT_CHAIN_INDEX] = i;
break;
}
} else {
if (metaStreamEvent.getInputReferenceId().equals(variable.getStreamId())) {
type = definition.getAttributeType(attributeName);
eventPosition[STREAM_EVENT_CHAIN_INDEX] = i;
if (currentState > -1 && metaStreamEvents[currentState].getInputReferenceId() != null && variable.getStreamIndex() != null && variable.getStreamIndex() <= LAST) {
if (variable.getStreamId().equals(metaStreamEvents[currentState].getInputReferenceId())) {
eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex();
}
}
break;
}
}
}
}
if (eventPosition[STREAM_EVENT_CHAIN_INDEX] == UNKNOWN_STATE) {
throw new SiddhiAppValidationException("Stream with reference : " + variable.getStreamId() + " not found");
}
VariableExpressionExecutor variableExpressionExecutor = new VariableExpressionExecutor(new Attribute(attributeName, type), eventPosition[STREAM_EVENT_CHAIN_INDEX], eventPosition[STREAM_EVENT_INDEX_IN_CHAIN]);
if (eventPosition[STREAM_EVENT_CHAIN_INDEX] != HAVING_STATE) {
MetaStreamEvent metaStreamEvent = ((MetaStateEvent) metaEvent).getMetaStreamEvent(eventPosition[STREAM_EVENT_CHAIN_INDEX]);
if (metaStreamEvent.getEventType() != MetaStreamEvent.EventType.DEFAULT) {
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_TYPE_INDEX] = OUTPUT_DATA_INDEX;
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_INDEX_IN_TYPE] = metaStreamEvent.getLastInputDefinition().getAttributePosition(variableExpressionExecutor.getAttribute().getName());
for (Attribute attribute : metaStreamEvent.getLastInputDefinition().getAttributeList()) {
metaStreamEvent.addOutputData(new Attribute(attribute.getName(), attribute.getType()));
}
}
metaStreamEvent.addData(new Attribute(attributeName, type));
}
if (executorList != null) {
executorList.add(variableExpressionExecutor);
}
return variableExpressionExecutor;
}
}
use of org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException in project ballerina by ballerina-lang.
the class OutputParser method constructOutputCallback.
public static OutputCallback constructOutputCallback(final OutputStream outStream, StreamDefinition outputStreamDefinition, Map<String, Table> tableMap, Map<String, Window> eventWindowMap, SiddhiAppContext siddhiAppContext, boolean convertToStreamEvent, String queryName) {
String id = outStream.getId();
Table table = null;
Window window = null;
if (id != null) {
table = tableMap.get(id);
window = eventWindowMap.get(id);
}
StreamEventPool streamEventPool = null;
StreamEventConverter streamEventConverter = null;
MetaStreamEvent tableMetaStreamEvent = null;
if (table != null) {
tableMetaStreamEvent = new MetaStreamEvent();
tableMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
TableDefinition matchingTableDefinition = TableDefinition.id("");
for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
tableMetaStreamEvent.addOutputData(attribute);
matchingTableDefinition.attribute(attribute.getName(), attribute.getType());
}
matchingTableDefinition.setQueryContextStartIndex(outStream.getQueryContextStartIndex());
matchingTableDefinition.setQueryContextEndIndex(outStream.getQueryContextEndIndex());
tableMetaStreamEvent.addInputDefinition(matchingTableDefinition);
streamEventPool = new StreamEventPool(tableMetaStreamEvent, 10);
streamEventConverter = new ZeroStreamEventConverter();
}
// Construct CallBack
if (outStream instanceof InsertIntoStream) {
if (window != null) {
return new InsertIntoWindowCallback(window, outputStreamDefinition, queryName);
} else if (table != null) {
DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
return new InsertIntoTableCallback(table, outputStreamDefinition, convertToStreamEvent, streamEventPool, streamEventConverter, queryName);
} else {
return new InsertIntoStreamCallback(outputStreamDefinition, queryName);
}
} else if (outStream instanceof DeleteStream || outStream instanceof UpdateStream || outStream instanceof UpdateOrInsertStream) {
if (table != null) {
if (outStream instanceof UpdateStream) {
if (((UpdateStream) outStream).getUpdateSet() == null) {
TableDefinition tableDefinition = table.getTableDefinition();
for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
if (!tableDefinition.getAttributeList().contains(attribute)) {
throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
}
}
}
}
if (outStream instanceof UpdateOrInsertStream) {
TableDefinition tableDefinition = table.getTableDefinition();
for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
if (!tableDefinition.getAttributeList().contains(attribute)) {
throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
}
}
}
if (outStream instanceof DeleteStream) {
try {
MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
CompiledCondition compiledCondition = table.compileCondition((((DeleteStream) outStream).getOnDeleteExpression()), matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
StateEventPool stateEventPool = new StateEventPool(matchingMetaInfoHolder.getMetaStateEvent(), 10);
return new DeleteTableCallback(table, compiledCondition, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventPool, streamEventPool, streamEventConverter, queryName);
} catch (SiddhiAppValidationException e) {
throw new SiddhiAppCreationException("Cannot create delete for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
}
} else if (outStream instanceof UpdateStream) {
try {
MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
CompiledCondition compiledCondition = table.compileCondition((((UpdateStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
UpdateSet updateSet = ((UpdateStream) outStream).getUpdateSet();
if (updateSet == null) {
updateSet = new UpdateSet();
for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
}
}
CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
StateEventPool stateEventPool = new StateEventPool(matchingMetaInfoHolder.getMetaStateEvent(), 10);
return new UpdateTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventPool, streamEventPool, streamEventConverter, queryName);
} catch (SiddhiAppValidationException e) {
throw new SiddhiAppCreationException("Cannot create update for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext);
}
} else {
DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
try {
MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
CompiledCondition compiledCondition = table.compileCondition((((UpdateOrInsertStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
UpdateSet updateSet = ((UpdateOrInsertStream) outStream).getUpdateSet();
if (updateSet == null) {
updateSet = new UpdateSet();
for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
}
}
CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
StateEventPool stateEventPool = new StateEventPool(matchingMetaInfoHolder.getMetaStateEvent(), 10);
return new UpdateOrInsertTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventPool, streamEventPool, streamEventConverter, queryName);
} catch (SiddhiAppValidationException e) {
throw new SiddhiAppCreationException("Cannot create update or insert into for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext);
}
}
} else {
throw new SiddhiAppCreationException("Event table with id :" + id + " does not exist", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
}
} else {
throw new SiddhiAppCreationException(outStream.getClass().getName() + " not supported", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
}
}
use of org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException in project ballerina by ballerina-lang.
the class SingleClientDistributedSink method initTransport.
@Override
public void initTransport(OptionHolder sinkOptionHolder, List<OptionHolder> destinationOptionHolders, Annotation sinkAnnotation, ConfigReader sinkConfigReader, SiddhiAppContext siddhiAppContext) {
final String transportType = sinkOptionHolder.validateAndGetStaticValue(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
Extension sinkExtension = DefinitionParserHelper.constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, transportType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK);
Set<String> allDynamicOptionKeys = findAllDynamicOptions(destinationOptionHolders);
destinationOptionHolders.forEach(optionHolder -> {
optionHolder.merge(sinkOptionHolder);
allDynamicOptionKeys.forEach(optionKey -> {
String optionValue = optionHolder.getOrCreateOption(optionKey, null).getValue();
if (optionValue == null || optionValue.isEmpty()) {
throw new SiddhiAppValidationException("Destination properties can only contain " + "non-empty static values.");
}
Option sinkOption = sinkOptionHolder.getOrAddStaticOption(optionKey, optionValue);
sinkOption.addVariableValue(optionValue);
destinationCount++;
});
});
this.sink = (Sink) SiddhiClassLoader.loadExtensionImplementation(sinkExtension, SinkExecutorExtensionHolder.getInstance(siddhiAppContext));
this.sink.initOnlyTransport(streamDefinition, sinkOptionHolder, sinkConfigReader, siddhiAppContext);
}
use of org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException in project ballerina by ballerina-lang.
the class CoalesceFunctionExecutor method init.
@Override
public void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, SiddhiAppContext siddhiAppContext) {
if (attributeExpressionExecutors.length == 0) {
throw new SiddhiAppValidationException("Coalesce must have at least one parameter");
}
Attribute.Type type = attributeExpressionExecutors[0].getReturnType();
for (ExpressionExecutor expressionExecutor : attributeExpressionExecutors) {
if (type != expressionExecutor.getReturnType()) {
throw new SiddhiAppValidationException("Coalesce cannot have parameters with different type");
}
}
returnType = type;
}
Aggregations