private TableOnMergeActionIns setupInsert(
      TableMetadata tableMetadata,
      InternalEventRouter internalEventRouter,
      int selectClauseNumber,
      OnTriggerMergeActionInsert desc,
      EventType triggeringEventType,
      String triggeringStreamName,
      StatementContext statementContext)
      throws ExprValidationException {

    // Compile insert-into info
    String streamName =
        desc.getOptionalStreamName() != null
            ? desc.getOptionalStreamName()
            : tableMetadata.getTableName();
    InsertIntoDesc insertIntoDesc = InsertIntoDesc.fromColumns(streamName, desc.getColumns());
    EventType insertIntoTargetType =
        streamName.equals(tableMetadata.getTableName())
            ? tableMetadata.getInternalEventType()
            : null;

    // rewrite any wildcards to use "stream.wildcard"
    if (triggeringStreamName == null) {
      triggeringStreamName = UuidGenerator.generate();
    }
    List<SelectClauseElementCompiled> selectNoWildcard =
        NamedWindowOnMergeHelper.compileSelectNoWildcard(
            triggeringStreamName, desc.getSelectClauseCompiled());

    // Set up event types for select-clause evaluation: The first type does not contain anything as
    // its the named window row which is not present for insert
    EventType dummyTypeNoProperties =
        new MapEventType(
            EventTypeMetadata.createAnonymous("merge_named_window_insert"),
            "merge_named_window_insert",
            0,
            null,
            Collections.<String, Object>emptyMap(),
            null,
            null,
            null);
    EventType[] eventTypes = new EventType[] {dummyTypeNoProperties, triggeringEventType};
    String[] streamNames = new String[] {UuidGenerator.generate(), triggeringStreamName};
    StreamTypeService streamTypeService =
        new StreamTypeServiceImpl(
            eventTypes, streamNames, new boolean[1], statementContext.getEngineURI(), false);

    // Get select expr processor
    SelectExprEventTypeRegistry selectExprEventTypeRegistry =
        new SelectExprEventTypeRegistry(
            statementContext.getStatementName(), statementContext.getStatementEventTypeRef());
    ExprEvaluatorContextStatement exprEvaluatorContext =
        new ExprEvaluatorContextStatement(statementContext, false);
    SelectExprProcessor insertHelper =
        SelectExprProcessorFactory.getProcessor(
            Collections.singleton(selectClauseNumber),
            selectNoWildcard.toArray(new SelectClauseElementCompiled[selectNoWildcard.size()]),
            false,
            insertIntoDesc,
            insertIntoTargetType,
            null,
            streamTypeService,
            statementContext.getEventAdapterService(),
            statementContext.getStatementResultService(),
            statementContext.getValueAddEventService(),
            selectExprEventTypeRegistry,
            statementContext.getMethodResolutionService(),
            exprEvaluatorContext,
            statementContext.getVariableService(),
            statementContext.getTableService(),
            statementContext.getTimeProvider(),
            statementContext.getEngineURI(),
            statementContext.getStatementId(),
            statementContext.getStatementName(),
            statementContext.getAnnotations(),
            statementContext.getContextDescriptor(),
            statementContext.getConfigSnapshot(),
            null,
            statementContext.getNamedWindowService(),
            null);
    ExprEvaluator filterEval =
        desc.getOptionalWhereClause() == null
            ? null
            : desc.getOptionalWhereClause().getExprEvaluator();

    InternalEventRouter routerToUser =
        streamName.equals(tableMetadata.getTableName()) ? null : internalEventRouter;
    boolean audit = AuditEnum.INSERT.getAudit(statementContext.getAnnotations()) != null;
    return new TableOnMergeActionIns(
        filterEval,
        insertHelper,
        routerToUser,
        statementContext.getEpStatementHandle(),
        statementContext.getInternalEventEngineRouteDest(),
        audit,
        tableMetadata.getRowFactory());
  }
 public static NamedWindowOnExprFactory make(
     EventType namedWindowEventType,
     String namedWindowName,
     String namedWindowAlias,
     OnTriggerDesc onTriggerDesc,
     EventType filterEventType,
     String filterStreamName,
     boolean addToFront,
     InternalEventRouter internalEventRouter,
     EventType outputEventType,
     StatementContext statementContext,
     StatementMetricHandle createNamedWindowMetricsHandle,
     boolean isDistinct)
     throws ExprValidationException {
   if (onTriggerDesc.getOnTriggerType() == OnTriggerType.ON_DELETE) {
     return new NamedWindowOnDeleteViewFactory(
         namedWindowEventType, statementContext.getStatementResultService());
   } else if (onTriggerDesc.getOnTriggerType() == OnTriggerType.ON_SELECT) {
     EventBeanReader eventBeanReader = null;
     if (isDistinct) {
       if (outputEventType instanceof EventTypeSPI) {
         eventBeanReader = ((EventTypeSPI) outputEventType).getReader();
       }
       if (eventBeanReader == null) {
         eventBeanReader = new EventBeanReaderDefaultImpl(outputEventType);
       }
     }
     OnTriggerWindowDesc windowDesc = (OnTriggerWindowDesc) onTriggerDesc;
     return new NamedWindowOnSelectViewFactory(
         namedWindowEventType,
         internalEventRouter,
         addToFront,
         statementContext.getEpStatementHandle(),
         eventBeanReader,
         isDistinct,
         outputEventType,
         statementContext.getStatementResultService(),
         statementContext.getInternalEventEngineRouteDest(),
         windowDesc.isDeleteAndSelect());
   } else if (onTriggerDesc.getOnTriggerType() == OnTriggerType.ON_UPDATE) {
     OnTriggerWindowUpdateDesc updateDesc = (OnTriggerWindowUpdateDesc) onTriggerDesc;
     NamedWindowUpdateHelper updateHelper =
         NamedWindowUpdateHelper.make(
             namedWindowName,
             (EventTypeSPI) namedWindowEventType,
             updateDesc.getAssignments(),
             namedWindowAlias);
     return new NamedWindowOnUpdateViewFactory(
         namedWindowEventType, statementContext.getStatementResultService(), updateHelper);
   } else if (onTriggerDesc.getOnTriggerType() == OnTriggerType.ON_MERGE) {
     OnTriggerMergeDesc onMergeTriggerDesc = (OnTriggerMergeDesc) onTriggerDesc;
     NamedWindowOnMergeHelper onMergeHelper =
         new NamedWindowOnMergeHelper(
             statementContext,
             onMergeTriggerDesc,
             filterEventType,
             filterStreamName,
             internalEventRouter,
             namedWindowName,
             (EventTypeSPI) namedWindowEventType);
     return new NamedWindowOnMergeViewFactory(
         namedWindowEventType,
         onMergeHelper,
         statementContext.getStatementResultService(),
         createNamedWindowMetricsHandle,
         statementContext.getMetricReportingService());
   } else {
     throw new IllegalStateException("Unknown trigger type " + onTriggerDesc.getOnTriggerType());
   }
 }