Quantcast
Channel: SCN : All Content - SAP HANA Developer Center
Viewing all articles
Browse latest Browse all 6745

Introduction and Usage of SAP HANA CE functions

$
0
0

We have a Chinese version(http://scn.sap.com/community/chinese/hana/blog/2014/02/18/sap-hana-ce-function%E7%9A%84%E4%BB%8B%E7%BB%8D%E5%8F%8A%E4%BD%BF%E7%94%A8) of this document.

CE functions are short for Calculation Engine Plan Operators. Like the format of functions, they contain input parameters and output parameters, and encapsulate the operation of data transformation. CE functions are usually used in the definition of procedures with SQLScript. And the operations can be operated in the calculation engine directly. In general, each CE function can be present by a SELECT query sentence. So the operation can be implemented by SQL sentences alternatively.

Here is an example of using CE functions in the definition of a SQLScript procedure.

createprocedure test1(out pro_cat_join pro_cat)
language sqlscript as
begin
 category_tab1 = CE_COLUMN_TABLE("FURNITURE"."CATEGORY");
 category_tab2 = CE_PROJECTION(:category_tab1,["CATEGORYID","NAME" AS "CATEGORYNAME"]);
 product_tab = CE_COLUMN_TABLE("FURNITURE"."PRODUCT");
 pro_cat_join= CE_JOIN(:category_tab2,:product_tab,["CATEGORYID"],["PRODUCTID","CATEGORYID","CATEGORYNAME"]);
end;

The Introduction of  Common CE Functions

Data Source Access Operators: bind the physical tables or column tables to table variables.

1.CE_COLUMN_TABLE

     This function uses a physical column table or a list of column of it as data source to bind it to a table variable. It provides access to a physical table.

     For example: Choose the columns NAME,COLOR,PRICE of table PRODUCT and bind it to the table variable product1.


               product1 = CE_COLUMN_TABLE("PRODUCT",["NAME","COLOR","PRICE"]);

     Equivalent SQL sentence:

 

product1 = select"NAME","COLOR","PRICE"from"PRODUCT"     ;

2.CE_JOIN_VIEW

     This function provides access to an Attribute View by choosing the join view or an optional list of attributes to bind it to a table variable.

     For example: Choose the attributes PRODUCT_NAME,CATEGORY_NAME from Attribute View ATTR_PRODUCT, and bind it to the table variable product2.


               product2 = CE_JOIN_VIEW("ATTR_PRODUCT",["PROCUCT_NAME","CATEGORY_NAME"]);

     Equivalent SQL sentence:

 

              product2 = select"PROCUCT_NAME","CATEGORY_NAME"from"ATTR_PRODUCT";

3.CE_OLAP_VIEW

     This function uses an OLAP view, namely Analytical View, as data source. Then some key figures and dimension are chosen to group the OLAP view and compute the aggregation values. The result will be bound to a table variable.

     For example: use Analytical View as data source, group by CATEGORY_NAME and compute the SUM of AMOUNT. Then bind the result to table variable olap_view.

  

              olap_view = CE_OLAP_VIEW("ANAL_SALE", ["CATEGORY_NAME", SUM("AMOUNT")]);

     Equivalent SQL sentence:

 

olap_view = select CATEGORY_NAME, SUM(AMOUNT) FROM"ANAL_SALE"GROUPBY CATEGORY_NAME;

4.CE_CALC_VIEW

     This function provides access to an Calculation View by choosing the view or an optional list of attributes to bind it to a table variable.

     For example: Choose the attributes QUANTITY,AMOUNT from Calculation View, and bind it to the table variable cal.


               cal = CE_CALC_VIEW("CALC_SALE",["QUANTITY","AMOUNT"]);


     Equivalent SQL sentence:

 

cal = select"QUANTITY","AMOUNT"from"CALC_SALE"     ;

Relational Operators: execute relational operations on calculation engine directly

5.CE_JOIN

     This function is get the inner join result of two tables on a list of join attributes. For the join attributes, only one will contains and they must own the same column name.  if not, you need to rename them in advance. Like the usage of above functions, you can choose a list of columns of the inner join result to be returned.

     For example: inner join table variables :product and :category on the columns ”NAME”, contain column "PRODUCTID","PROJECT_NAME","CATEGORYID","NAME" and  bind the result to table variable product3.


              product3 = CE_JOIN(:project,:category,["CATEGORYID"],["PRODUCTID","PROJECT_NAME","CATEGORYID","NAME"]);

 

     Equivalent SQL sentence:

                                                                       

product3= select P."PRODUCTID",P."PROJECT_NAME",P."CATEGORYID",C."NAME"from :project as P innerjoin :category  as C on P."CATEGORYID"=C."CATEGORYID";

 

6.CE_LEFT_OUTER_JOIN

     This function realizes the function of left outer join. And the syntax is the same as CE_JOIN() function.

 

7.CE_RIGHT_OUTER_JOIN

     This function realizes the function of right outer join. And the syntax is the same as CE_JOIN() function.

 

8.CE_PROJECTION

     This function can restrict the columns of a table variable. And you can also rename columns, calculate expression or apply filters.

     For example: Restrict the columns "PRODUCTID","PRICE","NAME" from table variable :product.Specify a filter that "PRICE" is greater than 50 and rename the column ”NAME” to ”CATEGORYNAME”.

 

              product4 = CE_PROJECTION(:product,["PRODUCTID","PRICE","NAME"AS"CATEGORYNAME"],'"PRICE">50');

     Equivalent SQL sentence:

product4 = select"PRODUCTID","PRICE","NAME"AS"CATEGORYNAME"from :product where"PRICE">50;

9.CE_CALC

     This function is generally used with other CE functions. The usage of it is to compute the value of expressions and bind to a new column. It contains two parameters: the expression and the returned data type. And some functions can be used in the expression, such as conversion functions, string functions, mathematical functions, date functions and so on.

     For example: choose the columns CID,CNAME,OID,SALES of table variable:product, and add the result of  sales multiplies by :vat_rate as a new column with the name “SALES_VAT”.

 

with_tax = CE_PROJECTION(:product, ["CID", "CNAME", "OID", "SALES",CE_CALC('"SALES" * :vat_rate',decimal(10,2)) AS"SALES_VAT"]);

     Equivalent SQL sentence:


with_tax2 = SELECT cid, cname, oid, sales,sales * :vat_rate as sales_vat FROM :product;

10.CE_AGGREGATION

     This function can be used to group the input table variable and compute the aggregation value, such as COUNT SUM MIN MAX.

     For example: Group the table variable :product by the column “YEAR”, and compute the count value of each group.


              product5 = CE_AGGREGATION (:product,[COUNT ("PRODUCT_ID") AS"CNT"], ["YEAR"]);

              Equivalent SQL sentence:

 

product5 = SELECTCOUNT ("PRODUCT_ID") AS cnt, yearFROM :product GROUPBYyear;

11.CE_UNION_ALL

     This function computes the union of two tables which have the same table schema and duplicate rows will be preserved.

     For example:  compute the union of two table variables :product1 and :product2 with duplicate rows containing.


              pro_union_all = CE_UNION_ALL (:product1, :product2);

              Equivalent SQL sentence:

 

pro_union_all = SELECT * FROM :product1 UNIONALLSELECT * FROM :product2;

Special Operators:

12.CE_VERTICAL_UNION

     This function can realize the function of vertical union.  It combines the two table variables, which have the same data volume, to a new table variable. And you can also rename the columns of the result.

     For example: combine the columns "ID", "FIRSTNAME" of table variable :firstname and the column " LASTNAME " of table variable : lastname to a new table variable.

 

out = CE_VERTICAL_UNION(:firstname, ["ID", "FIRSTNAME"AS"GIVENNAME"], :lastname, ["LASTNAME"AS"FAMILYNAME"]);


Particular Usage

  1. Add Serial Number

     When using CE_CALC() function, we can use the rownum() function to return a new column containing the serial number. So use this we can add serial number to a table variable.

     For example:

              product2 = CE_PROJECTION(:product1, ["NAME","COLOR","PRICE",CE_CALC('rownum()',integer) as"ROWNUM"]);

     result:

1.png

 

2.Fetch top n records

     According the previous example, we can use rownum() function to get the serial number and fetch top n records.

     For example: fetch top 5 records of table variable :product1.

 

product3 = CE_PROJECTION(:product1, ["NAME","COLOR","PRICE",CE_CALC('rownum()',integer) as"ROWNUM"],'"ROWNUM"<5');


     result:

2.png

Notices

Because CE functions are executed directly on calculation engine, it may influence the execution of the procedure, in some cases, is more efficient. We should not mix the CE functions with SQL queries to avoid the transformation between different engines, which may influence the performance.

Constriants

By now, some SQL queries can’t be realized by a CE function. For instance, the operation of UNION, there only exist a CE function CE_UNION_ALL() which is equivalent to the operation of UNION ALL. And there isn’t a corresponding CE function of the operation FULL OUTER JOIN. But we can use other CE functions or other methods to implement the operations we want.

 

     About the detailed usage of CE functions, please refer to SAP HANA SQLScript Reference

  http://help.sap.com/hana/SAP_HANA_SQL_Script_Reference_en.pdf


Viewing all articles
Browse latest Browse all 6745

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>