new()

Read(712) 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.

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.

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 whose original name will be used.

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").cursor@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 A3’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 cursor 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

=A2.fetch()

 

14

=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) from a composite table 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, and their key should keep the same order as the existing key field(s) in the composite table.

primary tableWithout @r option, the function supports an aggregate operation. In this case, T should be the primary table and should be the subtable, on which an aggregation is performed.

 

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

Copy records of the primary table and return result set aligned by A/cs

@z

Match existing fields of A/cs with T’s dimension/key

Return value:

A table sequence/cursor

Example:

 

A

 

1

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

Open a composite table

2

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

Return a table sequence ordered by primary key EID in ascending order

 

3

=A1.new(A2,NAME,GENDER)

Retrieve other fields of the composite table according to table sequence A2’ key values

 

4

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

 

 

 

5

=A1.new(A4,NAME:Name,GENDER:Gender).fetch()

Retrieve other fields of the composite table according to cursor A4’ key values

6

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

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

7

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

Return a table sequence

8

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

Retrieve fields where Gender=="F" & Dept=="Sales" from A7’s composite table by the key EID

9

=file("D:/file/dw/sale.ctx").open()

Below is the composite table:

10

=demo.query("select OrderID as ID,Amount as amount from OrderDetail where OrderID<10251").keys(ID)

Return table sequence as follows:

11

=A9.new@r(A10,ID,(sum(AMOUNT)):TOTAL)

Match records of composite table A9 according to table sequence A10’s key values to get field values of the composite table and perform aggregation. Here’s the result:

12

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

Open a composite table whose key is EID

13

=demo.query("SELECT EMPLOYEEID,EVALUATION,BONUS FROM PERFORMANCE")

Return a table sequence

14

=A12.new@z(A13,EID,NAME,GENDER,EVALUATION)

Use @z option to match existing fields of A18 with the key of A17’s composite table

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 multicursor.

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 in-memory table A or cluster cursor cs; parameter A/cs is ordered by their keys.

Parameters:

T

A cluster table

A/cs

A cluster in-memory table /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 multicursor

Example:

 

A

 

1

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

 

2

=A1.open()

Open a cluster composite table

3

=file("emp11.ctx",["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",["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