new()

Read(406) Label: new,

Here’s how to use new() function.

new ()

Description:

Generate a record according to the specified field name and field value.

Syntax:

new(xi:Fi,…)

Note:

The function generates a record made up of one field whose name is Fi and value is xi. It works with @t option to return result as a table sequence.

Parameters:

xi

Field value

Fi

Field name

Options:

@t

Enable returning result as a table sequence

Return value:

A record or table sequence

Example:

 

A

 

1

=new(1:ID,"ZHS":name)

Generate a record

2

=new@t(1:ID,"ZHS":name)

Use @t option to generate a table sequence made up of one record

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 specified fields from a cluster composite table according to key values of a cluster table or a cluster cursor and return a cluster cursor.

Syntax:

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

Note:

The function retrieves specified fields x,... from cluster composite table T according to key values of cluster table A or cluster cursor cs; parameter A/cs is ordered by their keys.

Parameters:

T

A cluster table

A/cs

A clustertable /cluster 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 cluster cursor

Example:

 

A

 

1

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

 

2

=A1.open()

Open a cluster composite table

3

=file("emp11.ctx":[1],["192.168.0.119:8281"]).open().memory()

Open a cluster memory table

4

=A2.new(A3,NAME,GENDER).fetch()

Retrieve specified fields from A2’s cluster composite table accroding to the key values of A3’s memory table

5

=file("emp12.ctx":[2],["192.168.0.110:8281"]).open().cursor()

Return a cluster cursor

6

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

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

T.new( xi : Fi ,…)

Description:

Compute an expression on a specified field of a pseudo table, assign results to the field and return a new pseudo table.

Syntax:

T.new(xi:Fi,…)

Note:

The function computes expression xi on a specific field of each record of pseudo table T, names the modified field Fi, and returns a new pseudo table consisting of only the Fi field.

Parameters:

T

A pseudo table

xi

An expression whose values are assigned to a specific field; record the field values as null when the parameter is absent, and in this case, : Fi after it should be kept; when it is the number sign #, locate a value by the sequence number of its corresponding field

Fi

Field names in the result psudo table; use the identifiers parsed from expression xi when it is absent

Options:

@i

Won’t generate the corresponding record when parameter is present but its result of computing a record is null

Return value:

A pseudo table

Example:

 

A

 

1

=file("scores.ctx").open().pseudo()

Generate a pseudo table

2

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

Compute a specified expression on the SCORE field in A1’s pseudo table and return a new pseudo table made up of ID field, CLASS field and newScores field

3

=A2.import()

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

Description:

Retrieve specified fields from a pseudo table by matching its records with key values of another pseudo table, a table sequence, or a cursor.

Syntax:

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

Note:

The function retrieves x,... fields from pseudo table T by matching its records with the key values of another pseudo table T’, table sequence A, or cursor cs, which is ordered by their key, and returns a pseudo table.

When the relationship between T’/A/cs and pseudo table T is and is only that of the sub table and the main table, the function can harbor aggregate operations.

Parameters:

T

A pseudo table

T’/A/cs

A pseudo table/table sequence/cursor

x

Field values

C

Column alias; can be omitted

wi

Filtering condition separated by commas and where the relationship between them is AND

Options:

@r

When T is the main table and A/cs is the sub table but allowing to return the specified fields when the corresponding records match key values of the sub table, duplicate of T’s records will be generated; default action is returning data according to the main table T, when aggregate expressions are allowed on field x

Return value:

A pseudo table

Example:

 

A

 

1

=file("emp.ctx").open().pseudo()

Generate a pseudo table as follows:

2

=file("PERFORMANCE.ctx").open().pseudo()

Generate a pseudo table as follows:

3

=A1.new(A2,EID,NAME)

Retrieve EID field and NAME field from A1’s pseudo table by matching with key values of A2’s pseudo table

4

=A3.import()

Return a pseudo table as follows:

5

=T("PERFORMANCE.ctx").cursor()

Return a cursor as follows:

6

=A1.new(A5,EID,NAME,SALARY;SALARY>7000)

Retrieve EID field, NAME field and SALARY field from A1’s pseudo table that satisfies SALARY>7000

7

=A6.import()

Return a pseudo table as follows:

8

=file("scores.ctx").open().pseudo()

Generate a pseudo table as follows:

9

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

Return a table sequence

10

=A8.new(A9,CLASS,STUDENTID,SUBJECT,SCORE)

A8’s pseudo table is the main table, A9’s pseudo table’s the sub table, the function does the matching according to the main table and returns a pseudo table in the default way

11

=A10.import()

12

=A8.new@r(A9,CLASS,STUDENTID,SUBJECT,SCORE)

@r option enables to do the matching based on the sub table, which leads to generate duplicate records in the result pseudo table

13

=A12.import()