Channel

Read(331) Label: channel, push, result, attach,

We always emphasize that data in a cursor can only be traversed once in a forward direction. Cursor-based operations discussed how to create a cursor and how to perform operations over a cursor or cursors. According to the cursor’s only-once-and-forward-direction feature, it is the cs.fetch() function that can really fetch data out from the last generated cursor. When the traversal is over, all the other cursors generated during the computation can’t fetch data any more.

At some occasions, you need to compute several different values at once during one data retrieval process. Then you can use channel, which is similar to cursor. esProc offers channel() function to create a channel and uses cs.push(ch) function to add the channel to cursor cs; or use ch2.push(ch) function to add it to another channel ch2. A newly-created channel only stores the computational actions to-be-performed until the fetch operation is actually performed over the cursor when the actions are performed and results recorded. The channel supports multiple operations through corresponding functions that are similar to what the cursor supports, including ch.select(), ch.new(), ch.derive(), ch.(), ch.news(), ch.run(), ch.switch(), ch.join(), ch.conj(), and ch.group(). A channel function can define many types of result set. ch.fetch() function defines returning all data from the channel; ch.id() function defines returning a result set where duplicate members are removed; ch.sortx() function defines returning a sorted result set; and ch.groupx() and ch.groups() functions define returning a grouped result set. Also, we can use ch.joinx() function to join data in a channel to a segmentable bin file. So we know that there are two kinds of channel functions. One is for attaching an operation to a channel, and the other is for getting the final result set. Data in a channel can be retrieved only when a function that returns a final result set is already attached to the channel. ch.result() function is used to get the defined result set from the channel. For example:

 

A

B

C

D

1

=file("Order_Wines.txt")

 

 

 

2

=A1.cursor@t()

 

 

 

3

=channel()

>A3.select(SalesID==1)

>A3.fetch()

>A2.push(A3)

4

=A2.select(month(Date)==8)

 

 

 

5

=channel()

>A4.push(A5)

>A5.select(SalesID==1)

>A5.fetch()

6

=A4.fetch()

=A3.result()

=A5.result()

 

As A6 performs the fetch operation to fetch data out from the cursor, the operations in the channel are completed. By defining the type of result set through channel, you can get multiple values at once during one traversal of cursor data. Here are results of A6, B6 and C6:

A3 and A5 create new channels. Both will select records whose SalesID is 1, yet results of B6 and C6 are different. That’s because A3’s channel is added to A2’s cursor from which all sales records are fetched; while A5’s channel is added to A4’s cursor that is bundled with an operation and from which only sales records in August are fetched.

According to the code in line 3 and line 5, after the push function is executed, a channel can still perform operatons effectively. But once you already define the type of result set to be returned using functions like ch.fetch() and ch.id(), the channel can’t be used to perform any operations.

A channel computes and pushes data forward according to attached operations step by step in a specified order. It only retains the result of the last function that returns the final result.

Besides ch.fetch() function that fetchs all data out, you can perform sorting, grouping and aggregation in a channel. For example:

 

A

B

C

D

1

=file("Order_Wines.txt")

 

 

 

2

=A1.cursor@t()

 

 

 

3

=channel()

>A3.select(SalesID==1)

>A2.push(A3)

 

4

=channel()

>A4.sortx(Amount)

>A3.push(A4)

>A3.groups(month(Date); count(~):Count)

5

=A2.skip ()

=A3.result()

=A4.result()

=C5.fetch()

A4 adds a channel to A3’s channel, so A4’s channel performs its operations within the result records of performing filtering according to the condition SalesID==1. A3 defines that the groups function be performed at last, so it is put behinde the execution of push funciton in C4. A channel function, like ch.sortx() and ch.groupx(), returns the defined result set as a cursor, whose data needs to be fetched using the fetch function, like what D5 does. Though A5 performs a skip operation over the cursor, the data will still flow through the channel to get the aggregagte result. Here are results of B5 and D5:

Besides ch.select() function and ch.new() function, you can use ch.attach(x) function to perform a bundled operation by setting expression x. In the expression, ~ is used to represent a value got in the previous computation in the channel. For example:

 

A

B

C

1

=file("Order_Wines.txt")

 

 

2

=A1.cursor@t()

 

 

3

=channel()

>A3.select(SalesID==1)

>A2.push(A3)

4

 

>A3.attach(~.(~.array()))

>A3.fetch()

5

=A2.skip ()

=A3.result()

 

B3 adds an operation to A3’s channel, that is, finding sales records whose SalesID is 1. B4 continues with another bundled operation to convert every record into a sequence. After execution, B5’s result is as follows: