use of io.siddhi.query.api.exception.SiddhiAppValidationException in project siddhi by wso2.
the class CoalesceFunctionExecutor method init.
@Override
public StateFactory init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, SiddhiQueryContext siddhiQueryContext) {
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;
return null;
}
use of io.siddhi.query.api.exception.SiddhiAppValidationException in project siddhi by wso2.
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
* @param siddhiQueryContext Siddhi Query Context
* @return VariableExpressionExecutor representing given variable
*/
private static ExpressionExecutor parseVariable(Variable variable, MetaComplexEvent metaEvent, int currentState, List<VariableExpressionExecutor> executorList, int defaultStreamEventIndex, SiddhiQueryContext siddhiQueryContext) {
String attributeName = variable.getAttributeName();
int[] eventPosition = new int[2];
if (variable.getStreamIndex() != null) {
if (variable.getStreamIndex() <= SiddhiConstants.LAST) {
eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex() + 1;
} else {
eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex();
}
} else {
eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN] = defaultStreamEventIndex;
}
eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = SiddhiConstants.UNKNOWN_STATE;
if (metaEvent instanceof MetaStreamEvent) {
MetaStreamEvent metaStreamEvent = (MetaStreamEvent) metaEvent;
AbstractDefinition abstractDefinition;
Attribute.Type type;
if (currentState == SiddhiConstants.HAVING_STATE) {
abstractDefinition = metaStreamEvent.getOutputStreamDefinition();
type = abstractDefinition.getAttributeType(attributeName);
eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = SiddhiConstants.HAVING_STATE;
} else {
abstractDefinition = metaStreamEvent.getLastInputDefinition();
type = abstractDefinition.getAttributeType(attributeName);
((MetaStreamEvent) metaEvent).addData(new Attribute(attributeName, type));
}
if (variable.getStreamId() != null && !variable.getStreamId().equals(abstractDefinition.getId())) {
throw new SiddhiAppCreationException("Id '" + variable.getStreamId() + "' not defined within the " + "current scope", variable, siddhiQueryContext.getSiddhiAppContext());
}
VariableExpressionExecutor variableExpressionExecutor = new VariableExpressionExecutor(new Attribute(attributeName, type), eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX], eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN]);
if (((MetaStreamEvent) metaEvent).getEventType() != MetaStreamEvent.EventType.DEFAULT) {
variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_ATTRIBUTE_TYPE_INDEX] = SiddhiConstants.OUTPUT_DATA_INDEX;
variableExpressionExecutor.getPosition()[SiddhiConstants.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;
boolean multiValue = false;
if (variable.getStreamId() == null) {
MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
if (currentState == SiddhiConstants.HAVING_STATE) {
definition = metaStateEvent.getOutputStreamDefinition();
try {
type = definition.getAttributeType(attributeName);
eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = SiddhiConstants.HAVING_STATE;
} catch (AttributeNotExistException e) {
currentState = SiddhiConstants.UNKNOWN_STATE;
}
}
if (currentState == SiddhiConstants.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[SiddhiConstants.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[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = currentState;
} catch (AttributeNotExistException e) {
ExpressionExecutor functionExecutor = constructEventExpressionExecutor(variable, currentState, siddhiQueryContext, eventPosition, metaStateEvent);
if (functionExecutor != null) {
return functionExecutor;
}
throw new SiddhiAppValidationException(e.getMessageWithOutContext() + " Input stream '" + definition.getId() + "' with reference '" + metaStreamEvent.getInputReferenceId() + "' for attribute '" + variable.getAttributeName() + "'", 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[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = i;
break;
}
} else {
if (metaStreamEvent.getInputReferenceId().equals(variable.getStreamId())) {
type = definition.getAttributeType(attributeName);
eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = i;
if (currentState > -1 && metaStreamEvents[currentState].getInputReferenceId() != null && variable.getStreamIndex() != null && variable.getStreamIndex() <= SiddhiConstants.LAST) {
if (variable.getStreamId().equals(metaStreamEvents[currentState].getInputReferenceId())) {
eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex();
}
} else if (currentState == SiddhiConstants.UNKNOWN_STATE && variable.getStreamIndex() == null) {
multiValue = metaStreamEvent.isMultiValue();
}
break;
}
}
}
}
if (eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] == SiddhiConstants.UNKNOWN_STATE) {
if (variable.getStreamId() == null) {
ExpressionExecutor functionExecutor = constructEventExpressionExecutor(variable, currentState, siddhiQueryContext, eventPosition, metaStateEvent);
if (functionExecutor != null) {
return functionExecutor;
}
throw new SiddhiAppValidationException("No matching stream reference found for attribute '" + variable.getAttributeName() + "'");
} else {
throw new SiddhiAppValidationException("Stream with reference '" + variable.getStreamId() + "' not found for attribute '" + variable.getAttributeName() + "'");
}
}
VariableExpressionExecutor variableExpressionExecutor = new VariableExpressionExecutor(new Attribute(attributeName, type), eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX], eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN]);
if (eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] != SiddhiConstants.HAVING_STATE) {
MetaStreamEvent metaStreamEvent = ((MetaStateEvent) metaEvent).getMetaStreamEvent(eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX]);
if (metaStreamEvent.getEventType() != MetaStreamEvent.EventType.DEFAULT) {
variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_ATTRIBUTE_TYPE_INDEX] = SiddhiConstants.OUTPUT_DATA_INDEX;
variableExpressionExecutor.getPosition()[SiddhiConstants.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);
}
if (multiValue) {
FunctionExecutor functionExecutor = new MultiValueVariableFunctionExecutor();
ExpressionExecutor[] innerExpressionExecutors = new ExpressionExecutor[] { variableExpressionExecutor };
functionExecutor.initExecutor(innerExpressionExecutors, ProcessingMode.BATCH, null, false, siddhiQueryContext);
return functionExecutor;
}
return variableExpressionExecutor;
}
}
use of io.siddhi.query.api.exception.SiddhiAppValidationException 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.exception.SiddhiAppValidationException in project siddhi by wso2.
the class PartitionedDistributionStrategy method init.
/**
* Initialize the Distribution strategy with the information it will require to make decisions.
*
* @param streamDefinition The stream attached to the sink this DistributionStrategy is used in
* @param transportOptionHolder Sink options of the sink which uses this DistributionStrategy
* @param destinationOptionHolders The list of options under @destination of the relevant sink.
* @param configReader This hold the {@link PartitionedDistributionStrategy} configuration reader.
*/
@Override
public void init(StreamDefinition streamDefinition, OptionHolder transportOptionHolder, OptionHolder distributionOptionHolder, List<OptionHolder> destinationOptionHolders, ConfigReader configReader) {
totalDestinationCount = destinationOptionHolders.size();
String partitionKey = distributionOptionHolder.validateAndGetStaticValue(SiddhiConstants.PARTITION_KEY_FIELD_KEY);
if (partitionKey == null || partitionKey.isEmpty()) {
throw new SiddhiAppValidationException("PartitionKey is required for partitioned distribution " + "strategy.");
}
try {
int partitionKeyFieldPosition = streamDefinition.getAttributePosition(partitionKey);
partitionOption = new Option(partitionKeyFieldPosition);
} catch (AttributeNotExistException e) {
throw new SiddhiAppValidationException("Could not find partition key attribute", e);
}
}
use of io.siddhi.query.api.exception.SiddhiAppValidationException in project siddhi by wso2.
the class TimeBatchWindowProcessor method init.
@Override
protected StateFactory init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, StreamEventClonerHolder streamEventClonerHolder, boolean outputExpectsExpiredEvents, boolean findToBeExecuted, SiddhiQueryContext siddhiQueryContext) {
this.outputExpectsExpiredEvents = outputExpectsExpiredEvents;
this.siddhiQueryContext = siddhiQueryContext;
if (attributeExpressionExecutors.length == 1) {
initTimeParameter(attributeExpressionExecutors[0]);
} else if (attributeExpressionExecutors.length == 2) {
initTimeParameter(attributeExpressionExecutors[0]);
if (!(attributeExpressionExecutors[1] instanceof ConstantExpressionExecutor)) {
throw new SiddhiAppValidationException("TimeBatch window's window.time (2nd) parameter should be " + "constant but found a dynamic attribute " + attributeExpressionExecutors[1].getClass().getCanonicalName());
}
// start time
if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.INT) {
isStartTimeEnabled = true;
startTime = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
} else if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.LONG) {
isStartTimeEnabled = true;
startTime = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
} else if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.BOOL) {
isStreamCurrentEvents = Boolean.valueOf(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
} else {
throw new SiddhiAppValidationException("TimeBatch window's 2nd parameter " + "should be 'start.time' which is int or long, or 'stream.current.event' which is bool " + " but found " + attributeExpressionExecutors[1].getReturnType());
}
} else if (attributeExpressionExecutors.length == 3) {
initTimeParameter(attributeExpressionExecutors[0]);
if (!(attributeExpressionExecutors[1] instanceof ConstantExpressionExecutor)) {
throw new SiddhiAppValidationException("TimeBatch window's window.time (2nd) parameter 'start.time' " + "should be a constant but found a dynamic attribute " + attributeExpressionExecutors[1].getClass().getCanonicalName());
}
// start time
isStartTimeEnabled = true;
if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.INT) {
startTime = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
} else if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.LONG) {
startTime = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
} else {
throw new SiddhiAppValidationException("TimeBatch window's 2nd parameter " + "should be 'start.time' which is int or long, " + " but found " + attributeExpressionExecutors[1].getReturnType());
}
if (!(attributeExpressionExecutors[2] instanceof ConstantExpressionExecutor)) {
throw new SiddhiAppValidationException("TimeBatch window's window.time (3rd) parameter " + "'stream.current.event' should be a constant but found a dynamic attribute " + attributeExpressionExecutors[2].getClass().getCanonicalName());
}
if (attributeExpressionExecutors[2].getReturnType() == Attribute.Type.BOOL) {
isStreamCurrentEvents = Boolean.valueOf(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[2]).getValue()));
} else {
throw new SiddhiAppValidationException("TimeBatch window's 3rd parameter " + "should be 'stream.current.event' which is bool " + " but found " + attributeExpressionExecutors[2].getReturnType());
}
} else {
throw new SiddhiAppValidationException("Time window should only have one or two parameters. " + "(<int|long|time> windowTime), but found " + attributeExpressionExecutors.length + " input " + "attributes");
}
return () -> new WindowState(streamEventClonerHolder, outputExpectsExpiredEvents, findToBeExecuted);
}
Aggregations