How CDS Views support better performance and faster development

In this post, I will share with you the why is it important to know about CDS Views, its features and how to create it, etc. You can click on the links to go directly to a topic that interests you.


Why is CDS Views important?

CDS is an infrastructure layer for defining semantically rich data models, which are represented as CDS views. In a very basic way, CDS allows developers to define entity types (such as orders, business partners, or products) and the semantic relationships between them, which correspond to foreign key relationships in traditional entity relationship (ER) models. CDS is defined using a SQL-based data definition language (DDL) that is based on standard SQL with some additional concepts, such as associations, which define the relationships between CDS views, and annotations, which direct the domain-specific use of CDS artifacts. Another example is expressions, which can be used in scenarios in which certain CDS attributes are considered as measures to be aggregated.

With the availability of the SAP HANA platform there has been a paradigm shift in the way business applications are developed at SAP. The rule-of-thumb is: Do as much as you can in the database to get the best performance.

To take advantage of SAP HANA for application development, SAP introduced a new data modeling infrastructure known as core data services. With CDS, data models are defined and consumed on the database rather than on the application server. CDS also offers capabilities beyond the traditional data modeling tools, including support for conceptual modeling and relationship definitions, built-in functions, and extensions.

CDS provides the following enhancements in comparison to SQL.

  • Expressions used for calculations and queries in the data model
  • Associations on a conceptual level, replacing joins with simple path expressions in queries
  • Annotations to enrich the data models with additional (domain specific) metadata

CDS is supported natively in both ABAP and SAP HANA, the data models are expressed in data definition language (DDL) and are defined as CDS views, which can be used in ABAP programs via Open SQL statements to enable access to the database. SAP HANA-based CDS operates on SAP HANA, while ABAP-based CDS operates on most major database platforms as well as SAP HANA, and each has a different type of repository for development objects.


What are its features?
CDS serve as central definitions that can be used in many different domains, such as transactional and analytical applications, to interact with data in the database in a unified way. For example, in CDS, you can define views that aggregate and analyze data in a layered fashion, starting with basic views and then adding powerful views that combine the basic views. Another difference is the support for special operators such as UNION, which enables the combination of multiple select statements to return only one result set. You can use the DDL of ABAP CDS in Eclipse based ADT to create rather complex views that exceed the capabilities of the classical database views created in SE11 by far.

Following are some of the important features provided by CDS Views:

  1. Semantically rich data models
    CDS builds on entity relationship model and is declarative in nature. It is very close to conceptual thinking.
  2. Compatibility across any database platform
    CDS is generated into managed Open SQL views and is natively integrated into the SAP HANA layer. These views based on Open SQL are supported by all major database vendors.
  3. Efficiency
    CDS offers a variety of highly efficient built-in functions — such as SQL operators, aggregations, and expressions — for creating views.
  4. Support for annotations
    The CDS syntax supports domain-specific annotations that can be easily evaluated by other components, such as the UI, analytics, and OData services.
  5. Support for conceptual associations
    CDS helps you define associations that serve as relationships between different views. Path expressions can be used to navigate along relations. Introducing an abstraction of foreign key relationships and joins, associations make navigation between entities consumable.
  6. Extensibility
    SAP delivered CDS views can be extended with fields that will be automatically added to the CDS view along with its usage hierarchy.

ABAP-based CDS has been engineered with complete transparency to the underlying database, meaning that all major database vendors are supported in addition to SAP HANA, which remains the optimal database choice. ABAP-based CDS plays a substantial role in the foundation of SAP Business Suite 4 SAP HANA (SAP S/4HANA). A large set of CDS artifacts — several thousand CDS views — consisting of several hundred thousand lines of ABAP code, represent the underlying core data model of the SAP S/4HANA solution.


How to create a CDS view?

Lets create a basic CDS view for the table SCARR and expose it in a oData service that can then be consumed in Fiori 2.0 applications. You should have downloaded and installed the ABAP Development Tools extension for Eclipse already, as well as being within the ABAP perspective with desired package selected. The artifacts are modeled and edited using the DDL editor tool within the Eclipse-based ABAP development tools (ADT) for SAP NetWeaver (known as ABAP in Eclipse).

Every CDS view has its own CDS Source code. Two objects are created in ABAP Dictionary of Every CDS View:
1. CDS Database View: It is read-only classical database view in ABAP Dictionary.
2. CDS Entity: It is actual CDS view. It covers the CDS Database view and makes other attributes possible, such as authorization checks defined in CDS view.

1.Use Transaction code SE16 to check there is data in the table. This is the data that we can expose in oData service.

2. Create a project within the HANA Studio in ABAP perspective. CDS is not written in ABAP, but the object will exist in the ABAP repository. It is a combination of Open SQL and various annotations. Select the Package, right click on $TMP and select New > Other ABAP Repository Object > Core Data Services. Expand that node.

3. In the Core Data Services node, choose DDL Source. The CDS DDL contains language elements for CDS data definitions and language elements for CDS metadata extensions.

DCL is used for security. ABAP CDS enables access control based on a data control language (CDS DCL). Access control in ABAP CDS further restricts the data returned from a CDS entity in ABAP CDS. CDS access control is based on the following:
CDS roles defined using the CDS DCL statement DEFINE ROLE. Currently, a CDS role is mapped to each user implicitly. This is why they are also known as mapping roles.
Access rules defined in a CDS role CDS entities. Access rules can define conditions, but also provide free access. Access rules can be inherited from another in a CDS role.
Access conditions defined in an access rule for the elements of the CDS entities for CDS entities.
If a CDS role with access rules is defined for a CDS entity, the access conditions are evaluated implicitly each time an object is accessed using Open SQL or using an SADL query (unless access control is disabled using the value #NOT_ALLOWED for the annotation @AccessControl.authorizationCheck. If access control is enabled, only that data is read that meets the access conditions.

4. Create the view, ZX is reserved, maintain this so it is in its own namespace and does not interfere with pre-delivered views. Underscores are allowed. Add a description and name.

5. If the CDS view is going to be used in production, choose a transport request. Note: ABAP CDS View will create and deploy the corresponding database view on the target database automatically (requiring no additional steps for the developer or transport manager).

6. Choose ‘Define View’ for a template since we are just creating a Basic view. We can also create other views with joins, associations and parameters. See links section on how to do this. As for CDS Extend Views, it extends an existing CDS view cds_entity using a CDS view extension cds_view_extension in the CDS DDL. The extended CDS view must be specified under the name of its CDS entity. See blog with video here.

7. In the View definition, specify the table name SCARR (from step 1) in the code by changing the data_source_name which came from the template. Make other necessary changes as shown below. The outline on the left bottom shows the changes made via code.
Datasource Declarations usually comes before the first curly bracket. Within this section, the developer specifies:
i. The type of view (define or extend view)
ii. The CDS view name
iii. The source table or view (SCARR from step 1)
iv. Any parameters
v. Any joins/associations

8. Save and Activate the CDS View.

9. Test the CDS View.

10. The results are displayed.

11. There are many types of annotations. For example, change the end user text label annotation to ‘Airline’ as shown below. See click here for post on how to create ABAP CDS view and OData with SAP Annotations.
@VDMViewType can be Basic (private, end user never accesses them), Composite (combination of 2 or more views with associations) or Consumption (exposed to end user, accessible through analytic frontend or published to oData).
@AbapCatalog.sqlViewName is the only mandatory annotation for a non-extending CDS View source file.
@AbapControl.authorizationCheck specifies whether an authorization check should be performed for the current CDS view.
@Analytics.dataCategory: #DIMENSION means it can be used for analytics and it is of type Dimension table. (other choices are AGGREGATIONLEVEL, CUBE, FACT)

12. Primary Key. A CDS View has two types of keys:
i. For CDS Entities, KEY can be used to define key elements, This Key is the semantic key of the CDS View.
ii. The key of the CDS database view is determined implicitly, as in a classical view. This key is a technical key of the CDS View.

13. Save, Activate and Test as in steps 8-9-10. Now we can build a Consumption view so that the CDS view we just built can be used in Analytics or OData. Start just like before with step #2 above. Description field can be filled as ‘SCARR Query – Consumption View – Public View’. Choose Basic View for template.

14. Replace data_source_name with basic view created earlier. As a naming convention, basic view contains “I” (for Interface) and consumption view contains “C” (for Consumption).

15. Include @OData.publish: true in annotations section and make sure a key is defined when you include all fields in the view definition. Save, activate and check if it was successful.

16. Open the SAP GUI for the relevant ABAP project by starting the SAP GUI Launcher ABAP Development Tools (icon  in the toolbar). Within the embedded SAP GUI, you are able to access the complete functionality of the classic ABAP Workbench.

17. In the command field, enter the transaction code /n/IWFND/MAINT_SERVICE. (You may get a message saying user not logged in, hence the /n). The entry screen of the transaction displays in the target system all activated Gateway services in the Service Catalog and allows you to add new services. Copy the service name as shown in step #15 which is of the format<CDS_VIEW>_CDS.

18. Click the Add Service button in the toolbar. Enter the System Alias of your front-end server. Enter the Technical Service Name (<CDS_VIEW>_CDS) and click on Get Services. As a result, the service is displayed for selection.

19. Select the service created as a result of last procedure and then choose Add Selected Services or alternatively click the object link for further selection.

20. The Add Service dialog that appears, and it suggests the name Z_CDS for the Technical Service, and for the Technical Model.
The dialog that now appears informs you that the model metadata for the Gateway service is going to be created.
Specify the package for service activation (should be same as the package the CDS view was created in).

21. We should get a message in a dialog box that the model metadata for the Gateway service has been created successfully in the Gateway. We can check it by going to the CDS view as below.

22. Clicking on the link in the picture above, will launch a browser. It will ask for user id and password. Enter the values, and you should now be able to see that the service is being correctly exposed.

Metadata can be retrieved by changing the URL from “sap-ds-debug=true” to “$metadata”.

Data can be retrieved by changing the URL from “sap-ds-debug=true” to “<query name>”.


Notes

  • A CDS database view is created for each CDS view and this database view supports only transparent tables, which means that pooled tables and cluster tables cannot be accessed using CDS views.
  • For CDS views, CDS view enhancements are a separate way of making enhancements without making modifications.
  • When a CDS data definition is activated, it is created as metadata in CDS source code. CDS entities are not transported. Instead they are created after the transport of a CDS source code when this code is activated in the target system.
  • CDS with input parameter is supported only from 7.40 SP8 onwards. CDS was introduced in ABAP 7.4 0SP5.
  • SAP recommends to use associations instead of joins because they are closer to “conceptual Thinking”. Association is not a join, it is just metadata about possible join. Actual join is created when the association is used in a path expression.
  • work with the CDS entity, whose name is defined behind DEFINE VIEW. Only the entity carries the full capabilities of the CDS view, like semantical information, client handling, connection to authority checks, etc. You can use the CDS entity behind TYPE for declaring work areas and in Open SQL in ABAP programs.

Links
Differences between OLAP and OLTP
What is SQL-92
CDS in SAP S4 HANA
ABAP CDS Views
CDS Views with input parameters
Understanding Join Types
CDS Views with Joins
CDS Views with Associations 
How to create ABAP CDS view and OData with SAP Annotations
Standard DEMO Examples of ABAP CDS views
OData Documentation
CDS Wiki
SAP Executive Keynote: Björn Goerke, Barcelona 2015

 


Please feel free to reach out to me for any decisions that you might need guidance on with regards to SuccessFactors, S/4 HANA, SAP Leonardo. I am happy to pay it forward. This post would not have been possible without all the help from blogs in SAP community and other articles published by SAP. Please connect with me at https://www.linkedin.com/in/krismoturi so that you can get notified if topics like SuccessFactors and S/4 HANA interest you.