CREATE WINDOW Statement

Defines a named window that can be referenced and used by one or more downstream operators or, if an output window, can be used to publish results.

Syntax

CREATE INPUT WINDOW name schema_clause
primary_key_clause
[store_clause]
[keep_clause]
[autogenerate_clause]
[GUARANTEE DELIVERY];

CREATE [ LOCAL | OUTPUT ] WINDOW name schema_clause
{ PRIMARY KEY (column1, column2, ...) | PRIMARY KEY DEDUCED } 
[store_clause]
[aging_clause]
[keep_clause]
[local-declare-block]
[GUARANTEE DELIVERY]
[partition_clause]
as_clause
;

Components

name

A name for the window being created.

schema_clause

Required for input windows, but optional for local and output windows. When the schema clause is not specified for local and output windows, it is automatically deduced by the compiler.

primary_key_clause

Set primary key.

store_clause

(Optional) Specifies the physical mechanism used to store the state of the records. If no clause is specified, project or module defaults apply.

autogenerate_clause

(Optional) Specify that the server will automatically generate values for one or more columns of datatype long. This can be used to generate a primary key for events that lack a natural key. Valid only for input windows. See AUTOGENERATE Clause for more information.

keep_clause

(Optional) Specifies the retention policy for the window. When not specified, the window uses the KEEP ALL retention policy as a default.

aging_clause

(Optional) Specifies the data aging policy. Used only with output or local windows.

local-declare-block

(Optional) Allows variable and function declarations that can be accessed in expressions in the query. You cannot define a local-declare-block on an input stream.

partition_clause

(Optional) Creates a specified number of parallel instances of the window. The partition type specifies how to partition the input for the window. See the PARTITION BY Clause for more information.

GUARANTEE DELIVERY (Optional) Enables guaranteed delivery for this window. Any window with guaranteed delivery must also be assigned to a log store.

as_clause

Introduces a query to a statement.

Usage

The SCHEMA and PRIMARY KEY clauses are mandatory for an input window. The SCHEMA clause is optional for derived windows. If a SCHEMA is not defined the compiler implicitly determines it based on the projection list. For derived windows, the primary key may be either deduced or explicitly specified. There are a few exceptions to these rules, which is noted in the appropriate context.

The CREATE WINDOW statement can also include a STORE clause to determine how records are stored, and a KEEP clause to determine how many records are stored and for how long. The window can be of type input, output, or local. Local and output windows can include an AGING clause that specifies the data aging policy.

Use GUARANTEE DELIVERY to enable guaranteed delivery only for a window stored in a log store; guaranteed delivery is not available to windows stored in memory stores. Do not use GUARANTEE DELIVERY inside a module because you cannot directly attach adapters to elements in modules.

When you enable guaranteed delivery on a window that has registered guaranteed delivery subscribers, the window stores a copy of every event it produces in its log store until all the registered guaranteed delivery subscribers acknowledge receiving the events.

Note: The window stores copies of events only if there are registered guaranteed delivery subscribers.

Because copies of events are kept in the same log store the window is assigned to, the log store for a guaranteed delivery window must be significantly larger than the log store for a similar window without guaranteed delivery. Ensure that the log store for every guaranteed delivery window is large enough to accommodate the required events. If the log store runs out of room, the project server shuts down.

Examples

This example creates a local window containing only position records received in the last ten minutes. It also uses the AGES clause to flag records that have not updated in the last 5 seconds by setting the value in the AgeColumn.
CREATE WINDOW TradesAge
PRIMARY KEY DEDUCED
KEEP 10 MINUTES
AGES EVERY 5 SECONDS SET AgeColumn 5 TIMES
AS
SELECT Trades.*, 0 AgeColumn FROM Trades;
This example creates a local window containing only position records received in the last ten minutes. Inclusion of the local declare block reports how many records have been processed (including updates and deletes).
CREATE WINDOW TradesAge
PRIMARY KEY DEDUCED
KEEP 10 MINUTES
AGES EVERY 5 SECONDS SET AgeColumn 5 TIMES
DECLARE
    long counter := 0;

    long getRecordCount() {
        return ++counter;
    }
END
AS
SELECT Trades.*, getRecordCount() RecordCount, 0 AgeColumn FROM Trades;
The following statement creates a window that maintains only the last 1000 rows while also getting updates on the age of the rows. The TradeId value is automatically generated beginning at 0.
CREATE INPUT WINDOW FreshTrades
SCHEMA (TradeId long, Symbol string, Shares integer, Price money(4), Age integer)
PRIMARY KEY (TradeId)
KEEP 1000 ROWS
AGES EVERY 5 MINUTES SET Age 100 TIMES
AUTOGENERATE (TradeId);
This example creates an input window and an output window with guaranteed delivery enabled and a log store to hold copies of events. If you subscribe to these windows in guaranteed delivery mode, you receive all the event data the windows produce even if events occur when the subscribed entity (a project or external application, for example) is not running or not actively listening.
CREATE LOG STORE Store1 PROPERTIES fileName='store';

CREATE INPUT WINDOW In1 
SCHEMA (Key1 integer, Col1 string, Col2 float)
PRIMARY KEY (Key1)
STORE Store1
GUARANTEE DELIVERY;

CREATE OUTPUT WINDOW Out1
PRIMARY KEY (Key1)
STORE Store1
GUARANTEE DELIVERY
AS SELECT * FROM In1;