Type Coercion Sample

This sample illustrates how certain numeric data types can "coerce"; that is, how same-named field values with certain compatible types can all appear to be carried by a new field with a type compatible with all original values. It consists of a Studio project containing a single application file (Coercions.sbapp).

The application illustrates some of the basic types that are coercible:

  • int and double coerce to double

  • int and long coerce to long

As the sample application shows, coercion also takes place when the fields are lists-of each type, or are individual fields within a field of type tuple.

Rearrange the arcs and try connecting the unconnected input streams to explore which types do not coerce: double will not coerce to long (loss of data from long type) no types coerce to string, timestamp or blobs

For details on type coercion, see Data Type Coercion and Conversion in the StreamBase Reference documentation.

This Sample's Files

The Type Coercion sample consists of the files shown in the following table.

File Description
Coercions.sbapp An EventFlow application illustrates which data types are compatible and in what ways.
Coercions.sblayout The file describing how the application is laid out in StreamBase Studio.

Explore the Type Coercion Application in StreamBase Studio

In the EventFlow editor, you can explore which data types can be coerced together and which may not. Follow these steps to explore this sample in the EventFlow editor:

  1. In the Package Explorer, double-click to open the Coercions.sbapp application. Make sure the application is the currently active tab in the EventFlow Editor.

    Notice that there are two groups of input streams, union operators, and output streams, called Lists and Tuples. The only difference between them is their schemas.

  2. In the Lists group, hover the mouse pointer over the arc exiting the intmeetslong Union operator (bottom right).

    Notice that its schema is list(long). This is because it is coercing its inputs, one of which is list(int) and the other of which is list(long), to data type long.

  3. Click on the arc exiting the input stream ListOfDouble near its arrowhead.

    Notice that its schema is list(double).

  4. Drag the arc away from the intmeetsdouble Union operator and instead connect it to the intmeetslong Union operator.

    A new input port on the intmeetslong Union operator appears to receive the arc.

  5. Again, hover the mouse pointer over the arc exiting the intmeetslong Union operator.

    Notice that the schema of that arc has changed from list(long) to list(double) as the result of type coercion.

  6. Try connecting the input streams ListOfTimestamp and ListOfString to either Union operator in its group. What happens? Hover over the Union operator to see.

    If you want to undo any change you have made to the diagram, click EditUndo or type Ctrl+Z.

  7. Repeat any of the above steps on the components in the Tuples group. Observe how it behave similarly to the Lists group.

  8. If the application has no typecheck errrors, you can click the Run button to send lists and tuples to it. (You may be asked to save the application first, which you should do.)

    The SB Test/Debug perspective opens and the application starts.

  9. In the Manual Input view, select ListOfInts.

  10. Click the green + button to the right of mylist to add a list element.

  11. Enter an integer in the mylist(0) field

  12. Optionally repeat steps 9 and 10 to add more list elements and enter their values.

  13. Click Send Data.

    In the application output view, observe the values in the OutListOfLong and OutListOfDouble streams and notice where type coercion has occurred.

  14. Select a different input stream and repeat steps 10–13 to observe when coercion takes place.

  15. When done, press F9 or click the Stop Running Application button.

Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

  • From the top menu, click FileLoad StreamBase Sample.

  • Select this application from the Applications list.

  • Click OK.

StreamBase Studio creates a project for each sample.

Sample Location

When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace, which is normally part of your home directory, with full access rights.

Important

Load this sample in StreamBase Studio, and thereafter use the Studio workspace copy of the sample to run and test it, even when running from the command prompt.

Using the workspace copy of the sample avoids the permission problems that can occur when trying to work with the initially installed location of the sample. The default workspace location for this sample is:

studio-workspace/sample_coercions

See Default Installation Directories for the location of studio-workspace on your system.

In the default TIBCO StreamBase installation, this sample's files are initially installed in:

streambase-install-dir/sample/coercions

See Default Installation Directories for the default location of studio-workspace on your system.