new()

Read(373) Label: new,

Here’s how to use new() function.

A. new( x i : F i ,… )

Description:

Generate a new table sequence/record sequence, whose field values are computed from the field values of a table sequence/record sequence.

Syntax:

A.new(xi:Fi,…)

Note:

The function generates a new table sequence/record sequence with the same length as table sequence/record sequence A, the field names and values of which are respectively Fi and xi. When parameter xi is #i, it represents the ith field and the original field name will be used.

Parameters:

Fi

Field name. If omitting it, then use the identifier parsed from the xi.

xi

Expression whose result is the field value. If omitted, field values will be null and :Fi must not be omitted at the same time. The sign # is used to represent a field with a sequence number.

A

Table sequence/record sequence.

Options:

@m

Use parallel algorithm to handle data-intensive or computation-intensive tasks; no definite order for the records in the result set.

@i

Won’t generate a record if the result of expression xi is null.

Return value:

The new table sequence.

Example:

Ø  Generate from an individual table sequence

 

A

 

1

=demo.query("select EID,NAME,DEPT,BIRTHDAY from EMPLOYEE")

2

=A1.new(EID:EmployeeID,NAME, #3:dept)

Generate a new table sequence directly. If the field names are the same as those of A1, Fi can be omitted.

 

3

=A1.new(NAME,age(BIRTHDAY):AGE)

Generate the new table sequence by computing new field values.

4

=A1.new@m(NAME,age(BIRTHDAY):AGE)

Use @m option to increase performance of big data handling.

5

=file("D:\\txt_files\\data1.txt").import@t()

Below is the file data1.txt:

6

=A5.new@i(CLASS,STUDENTID,SUBJECT,SCORE:score)

If the SCORE value is null, the corresponding record won’t be generated

Ø  Generate from multiple table sequences of the same order

 

A

 

1

=create(Name,Chinese).record(["Jack",99,"Lucy",90])

2

=create(Name,Math).record(["Jack",89,"Lucy",96])

3

=A1.new(Name:Name,Chinese:Chinese,A2(#).Math:Math)

Use A2(#) to get the record from A2 in the same position

 
 

Related functions:

cs.new()

ch .new()

Description:

Return the channel with newly-computed field values for its records.

Syntax:

ch.new(xi:Fi,…)

Note:

The function computes expression xi over each of the records in channel ch, names the new fields Fi, and returns the original ch consisting of Fi fields. This is an attached computation.

Parameters:

ch

Channel

xi

An expression, whose values are used as new field values. It is treated as null if omitted; in that case, parameter : Fi can’t be omitted. The sign # is used to represent a field with a sequence number

Fi

Field name in the given channel; use the identifiers parsed from expression xi if it is omitted

Options:

@i

Won’t generate the corresponding record when the result of computing expression xi is null

Return value:

  The original channel with new field values

Example:

 

A

 

1

=demo.cursor("select * from SCORES")

Create a cursor

2

=file("D:\\txt_files\\data1.txt").import@t()

Below is data1.txt:

3

=channel()

Create a channel

4

=channel()

Create a channel

5

=A3.new(CLASS,#2:ID,SCORE+5:newScores)

Change the structure of A2’s channel into CLASS, ID and newScores, among which the newScores values are computed from the original SCORE values according to an expression

6

=A3.fetch()

Fetch and store the existing data in the channel

7

=A4.new@i(CLASS,STUDENTID,SUBJECT,SCORE:score)

Won’t generate the corresponding record if the result of computing a SCORE value is null

8

=A4.fetch()

 

9

=A1.push(A3)

Be ready to push data in A1’s cursor into A3’s channel, but the action needs to wait

10

=A2.push(A4)

Push data in A2’s sequence into A4’s channel

11

=A1.fetch()

Data in A1’s cursor is pushed into the channel and operations are performed as the fetch() operation is performed over A1

12

=A3.result()

13

=A4.result()

cs .new()

Description:

Return a cursor with newly-computed field values for its records.

Syntax:

cs.new(xi:Fi,…)

Note:

The function computes expression xi against each record in cursor cs, names the newly-computed Fi fields, and returns the original cursor containing Fi fields.

Parameters:

cs

Cursor

xi

An expression, whose values are uses as the new field values. It is treated as null if omitted; in that case, : Fi can’t be omitted. The sign # is used to represent a field with a sequence number

Fi

Field name of cs; use the identifiers parsed from expression xi if it is omitted

Options:

@i

Won’t generate a record if the result of expression xi is null

Return value:

  The original cursor with new field values

Example:

 

A

 

1

=connect("demo").cursor("select * from SCORES where SCORES<60")

 

2

=A1.new(#2:ID, CLASS, SCORE+5:newScores)

Generate a new cursor composed of ID, CLASS, and newScores by performing expression evaluation on the existing SCORE field

3

=A2.fetch()

4

=file("D:\\txt_files\\1.txt").cursor@t()

Below is the file data1.txt:

5

=A4.new@i(CLASS,STUDENTID,SUBJECT,SCORE:score)

If the SCORE value is null, the corresponding record won’t be generated

6

=A5.fetch()

T.new(A/cs,x:C,… ;wi,... )

Description:

Get values of specified field(s) according to key values in a table sequence, record sequence or a cursor and return result as a table sequence or a cursor.

Syntax:

T.new(A/cs,x:C,…;wi,…)

Note:

 The function retrieves values of field(s) x,... from composite table T according to key values in table sequence/record sequence A or in cursor cs. The table sequence, record sequence and cursor should be ordered by the key.

When the relationship between T and A/cs is that of the main table and the subtable, the function supports an aggregate operation. When they switch over positions in the relationship, the aggregate operation is disabled.

Parameters:

T

A composite table

A/cs

A table sequence/cursor

x

Field values

C

Column alias; can be absent

wi

Filtering condition; separate multiple conditions, which should all be met, by comma(s)

Options:

@r

When A/cs is the main table and T is the subtable, return values that match the former’s key; in this case same records as those in T will be fetched. By default return values that match T’s key and aggregate is allowed.

Return value:

A table sequence/cursor

Example:

 

A

 

1

=file("D:\\emp10.ctx")

 

2

=A1.create()

Open a composite table

3

=demo.query("select EID from EMPLOYEE order by EID").keys(EID)

Return a table sequence where EID is the primary key and that is sorted by the key:

4

=A2.new(A3,NAME,GENDER)

Retrieve non-key fields from A2’s composite table accroding to the key values of A3’s table sequence

 

5

=demo.query("select * from EMPLOYEE order by EID").keys(EID).cursor()

 

6

=A2.new(A5,NAME:Name,GENDER:Gender).fetch()

Retrieve non-key fields from A2’s composite table accroding to the key values of A5’s cursor

7

=file("D:/employees.ctx").create()

Open a composite table. It’s data is as follows:

8

=demo.query("select EID from employee where EID<10 ").keys(EID)

Return a table sequence

9

=A7.new(A8,EID,Gender,Dept;Gender=="F",Dept=="Sales")

Retrieve Gender field and Dept field from A7’s composite table by the key EID; filtering condition is Gender=="F" & Dept=="Sales"

10

=file("D:/orderitem.ctx").create().cursor().fetch()

Return result:

11

=file("D:/orders.ctx").create()

Part of order.ctx is as follows:

12

=A11.new(A10,ORDERID,(sum(PRICE*NUMBER*(1-DISCOUNT))):AMOUNT)

Get field values from the composite table by keys and perform aggregate. Here’s the result:

13

=file("SCORES.ctx").create()

Open a composite table as follows:

14

=demo.query("select STUDENTID from SCORES WHERE STUDENTID =3").keys(STUDENTID)

Return a table sequence

15

=A13.new(A14,CLASS,STUDENTID,SUBJECT,SCORE)

By default return a table sequence by matching key values according to A13

16

=A13.new@r(A14,CLASS,STUDENTID,SUBJECT,SCORE)

There are duplicate records when matching values according to A14

T.new(A/cs,x:C,… ;wi,... )

Description:

Retrieve non-key fields from a cluster composite table according to key values of a table sequence/record sequence or a cursor and return a table sequence or a cluster multcursor.

Syntax:

T.new(A/cs,x:C,…;wi,…)

Note:

The function retrieves non-key fields from cluster composite table T according to key values of table sequence/record sequence A or cursor cs; parameter A/cs is ordered by their key.

Parameters:

T

A composite table

A/cs

A table sequence/cursor

x

Field values

C

Column alias

wi

Filtering condition; separate multiple conditions, which should be met at the same time, by comma(s)

Return value:

A table sequence/cursor

Example:

 

A

 

1

=file("emp10.ctx",["192.168.0.118:8281"])

 

2

=A1.create()

Open a composite table

3

=demo.query("select  EID from EMPLOYEE")

 

4

=A2.new(A3,NAME,GENDER)

Retrieve non-key fields from A2’s cluster composite table accroding to the key values of A3’s table sequence

5

=demo.query("select  EID from EMPLOYEE").cursor()

 

6

=A2.new(A5,NAME:NAME,GENDER:GENDER).fetch()

Retrieve non-key fields from A2’s cluster composite table accroding to the key values of A5’s cursor