new()

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

Parameter:

xi

Field value

Fi

Field name

Option:

@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:

Perform computation on a sequence to generate a new table sequence.

Syntax:

A.new(xi:Fi,…)

Note:

The function computes expression xi on each member of sequence A and generate a new table sequence having same number of records as A and using xi as field values and Fi as field names.

Parameter:

Fi

Field names of the result table sequence; use xi when the parameter is absent; use the original field names when xi is #i

xi

An expression whose results are field values; if omitted, field values will be nulls a, and when is absent, :Fi must not be omitted

A

A sequence

Option:

@m

Use parallel processing to speed up computation

@i

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

@o

When parameter A is a pure table sequence, directly reference an old column if it is unmodified instead of generating a new column; sequence A will also be updated when the result table sequence is updated

Return value:

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 a pure table sequence

 

A

 

1

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

Return a pure table sequence

2

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

With @o option, directly referece the the unmodified old columns instead of generating new ones

3

=A2(1).NAME="aaa"

Modifying column values results in the modification of source table; below is A2’s result after execution:

And A1’s result is as follows:

 

Ø  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 function:

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.

Parameter:

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 ordinal number

Fi

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

Option:

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

Parameter:

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 ordinal number

Fi

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

Option:

@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:K,x:C,…;wi,... )

Description:

Get specified field(s) from a composite table according to correspondence between its key values and the fields in a table sequence or a cursor, and return result as a table sequence or a cursor.

Syntax:

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

Note:

The composite table T and table sequence A or cursor cs have a relationship of one-to-many. The function matches T’s key/dimension field with fields of A/cs (begin the correspondence from the first field) and returns T’s records according to the correspondence. It is already known that A/cs’s first one or two fields have same order as T’s key value field. With a table sequence, the function returns a table sequence, and with a cursor, it returns a cursor or a multicursor.

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

Parameter:

T

A composite table

A/cs

A table sequence/cursor

K

Field names; when there are K… parameters after A/cs, correspond them with theses specified fields

x

Field values

C

Column alias; can be absent

wi

Filtering condition; retrieve the whole set when this parameter is absent; separate multiple conditions by comma(s) and their relationships are AND; besides regular filtering expressions, you can also use the following five types of syntax in a filtering condition, where K is a non-key field in entity table T:

1K=w

w is expression Ti.find(K) or Ti.pfind(K), where Ti is a table sequence. When value of w is null or false, the corresponding record in the entity table will be filtered away; when w is expression Ti.find(K) and the to-be-selected fields C,... contain K, Ti’s referencing field will be assigned to K; when w is expression Ti.pfind(K) and the to-be-selected fields C,... contain K, ordinal numbers of K values in Ti will be assigned to K.

2(K1=w1,Kn=wn,w)

wi is expression Ti.find(K) or Ti.pfind(K), where Ti is a table sequence. When value of wi is null or false, the corresponding record in the entity table will be filtered away; when wi is expression Ti.find(K) and the to-be-selected fields C,... contain K1,...Ki, T1,...Ti’s referencing field will be assigned to K1,...Ki correspondingly; when w is expression Ti.pfind(K) and the to-be-selected fields C,... contain K, ordinal numbers of K values in Ti will be assigned to K. w is an expression that returns a Boolean value; each of the multiple Ki=wi and w has a relationship of AND; you can reference Ki in w.

3K:Ti

Ti is a table sequence. Compare Ki value in the entity table with key values of Ti and discard records whose Ki value does not match; when the to-be-selected fields C,... contain K, Ti’s referencing field will be assigned to K.

4K:Ti:null

Filter away all records that satisfy K:Ti.

Option:

@r

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

Return value:

A table sequence or a cursor

Example:

 

A

 

1

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

Open a composite table

2

=demo.query("select SALARY,NAME,EID from EMPLOYEE order by EID")

Return a table sequence ordered by EID in ascending order

3

=A1.new(A2:EID,NAME,GENDER)

Retrieve fields of the composite table according to the correspondence between the table sequence’s EID and the composite table’s primary key

4

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

 

 

5

=A1.new(A4,NAME:EMPName,GENDER:EMPGender).fetch()

Retrieve the other fields of the composite table by matching the cursor’s first field with the composite table’s primary key

6

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

 

7

=A1.new(A6,EID,GENDER,SALARY;GENDER=="F",SALARY>2000)

Retrieve fields where Gender is "F" and SALARY is greater than 2000 from the composite table by matching the table sequence’s first field with the composite table’s primary key

8

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

Below is content of composite table:

 

9

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

 

10

=A8.new(A9,ID,(sum(AMOUNT)):TOTAL)

Perform aggregation on field values in the subtable

11

=A8.new@r(A9,ID,AMOUNT,PRICE)

With @r option, align records of primary table A8 by A9, and copy the former’s records:

 

Use special types of filtering conditions:

 

A

 

1

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

Below is content of the cursor:

2

=file("students.ctx")

 

3

=A2.create@y(#ID,NAME,GENDER,AGE)

Create a composite table

4

=A3.append@i(A1)

Append cursor A1’s data to the composite table’s base table

5

=demo.query("select top 12 STUDENTID,SUBJECT,SCORE  from SCORES")

Return a table sequence

6

=A3.new(A5:STUDENTID,ID,NAME,AGE)

Match the composite table’s key values to table sequence’s fields and retrieve corresponding composite table fields and return a table sequence

7

=create(NAME,Num).record(["Emily",12,"Lauren",45]).keys(NAME)

Generate a table sequence using NAME as the key

8

=A3.new(A5:STUDENTID,ID,NAME,AGE;NAME=A7.find(NAME))

Use K=w filtering mode; in this case w is Ti.find(K) and entity table records making NAME=A7.find(NAME) get null or false are discarded; NAME is the selected field, to which table sequence A7’s referencing field is assigned

9

=A3.new(A5:STUDENTID,ID,NAME;NAME=A7.pfind(NAME))

Use K=w filtering mode; in this case w is Ti.pfind(K) and entity table records making NAME=A7.find(NAME) get null or false are discarded; NAME is the selected field, to which its ordinal numbers in table sequence A7 are assigned

10

=A3.new(A5:STUDENTID,ID,NAME;NAME:A7)

Use K:Ti filtering mode; compare the entity table’s NAME values with the table sequence’s key values and discard entity table records that cannot match

11

=A3.new(A5:STUDENTID,ID,GENDER;NAME:A7)

This is a case where K isn’t selected; NAME isn’t the selected field, so only filtering is performed

12

=A3.new(A5:STUDENTID,ID,NAME;NAME:A7:null)

Use K:Ti:null filtering mode; compare the entity table’s NAME values with the table sequence’s key values and discard entity table records that can match

13

=create(Age,Chinese_zodiac_sign).record([14,"tiger",15,"ox",16,"rat"]).keys(Age)

Return a table sequene using AGE as the key

14

=A3.new(A5:STUDENTID,ID,NAME,AGE;(AGE=A13.find(AGE),NAME=A7.find(NAME),AGE!=null&&NAME!=null))

Use (K1=w1,Kn=wn,w) filtring mode; return records that meet all conditions

 

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.

Parameter:

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 in-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 in-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