CDS Views
CDS Views
Table of Contents
Table of Contents 2
OVERVIEW 4
Architecture overview 4
Types of views 7
Basic 7
Interface 7
Consumption 7
Data categories 8
Facts 8
Dimensions 8
AGGREGATION LEVEL 8
Cube 8
Data class 9
Master data 9
Transactional data 9
Metadata 10
CDS Releases 11
Session variables 15
Syntax basics 16
CASE Expressions 18
Associations 20
2
Path expressions 21
Parameter 30
CDS Extractors 32
Star Schema 33
3
OVERVIEW
Before dive deep into CDS, let's, first, define what is a VIEW?
● A view is a virtual table, which does not have any physical existence
● It is created by combining the data of one or more tables
● A view does not occupy any storage space
Architecture overview
4
activation process – exactly one SQL view and the corresponding CDS entity
in the ABAP Dictionary.
5
Motivation behind Core Data Services
6
● Extensibility: We can extend SAP-defined CDS views with fields that will
be automatically added to the CDS view along with its usage hierarchy.
○ On model level through extensions.
○ On meta-model level through annotations.
CDS entities and their metadata are extensible and optimally integrated into
the ABAP Data Dictionary and the ABAP language.
Types of views
Basic
These are used to bring data directly from the physical tables in the
database.
Interface
This type of view is used to link basic views together through associations
(like SQL joins between views). They are then used by the consumption views.
Consumption
These views allow you to create a specific query from a “composite” view. It
can be consumed for reporting virtual data model. It gives information to the
Analytic engine on how to interpret a CDS entity, it can be either (dimension,
fact, cube).
7
Data categories
Facts
Hold the values we can measure and are extracted based on the filters. They generally
represent transactional data.
Dimensions
These are the master data like date, customer, and person responsible. They are generally
the attributes of the master data.
AGGREGATION LEVEL
The view using data category as aggregation level indicates that CDS view is
of type projection. In this type of view associations are not allowed and
Cube
When we connect all the FACT and DIMENSIONS, we come upon CUBES
which represent the factual data. They are used to combine data from
8
Data class
Master data
It’s the core data that is absolutely essential for running operations within a
business enterprise or unit. It is data about key business entities that
provides context for business transactions and operations.
Transactional data
It’s the data that is generated by various applications while running or
supporting everyday business processes.
Transaction data are business documents which are created using master
The two data types are best exemplified in the following sentence:
9
Within this, the buyer and the product are comprised of master data, as they
are at the heart of the transaction; without them, the transaction process
would not exist. The secondary data generated as a result of this interaction
falls under transactional data (such as the amount, the date, the quantity
purchased, the invoice number, or tax identifiers).
Metadata
“A set of data that describes and gives information about other data.”
Metadata helps us understand the structure, nature, and context of the data.
● Title
● Description
● Tags or categories
● Date of creation
● Who created it
● Source or origin
● Count and description of variables or attributes (e.g. rows*columns count for a
data table)
10
Here are some common metadata fields in a spreadsheet:
File name, Tab name, Column names, Number of rows, Number of columns,
Comments.
CDS Releases
11
12
13
Data types in CDS ABAP and casting
In the CDS entities you can refer to the data elements of the Data Dictionary
or directly to the ABAP data types. The data types must always fit exactly,
otherwise there will be error messages when activating the CDS ABAP object.
The SQL function CAST is used to convert the data types:
The ABAP data types are specified with abap.<datatype>, for example
● abap.int4
● abap.char(<length>)
● abap.dec(<length>, <decimal places>)
Depending on the data type, the <length> and possibly also the number of
<decimal places> must be specified in parentheses. If the data type of the
<expression> is technically identical to the specified data element, you can
specify the addition PRESERVING TYPE.
For CDS views, three different objects are created in the object catalog (table
TADIR):
● DDL file - The source code file , which is created and edited with the
ADT in Eclipse. Only this object is in the object list of transport requests
(object type DDLS).
14
● CDS entity - The CDS object that represents all the properties of the
view, including annotations, associations, and permission checks. This
object is used in ABAP programs.
● SQL database view - A database view that is also displayed as a view
in DDic, but should not be used from an ABAP perspective.
The DDL file and the CDS entity usually have the same technical name. The
SQL database view must have a different name than the CDS entity, since
both are in the same namespace.
With Release 7.55, a new object type has been added to the classic CDS View,
the CDS View Entity, which has no SQL database view. It is recommended by
SAP to use these objects if you are on the appropriate system level.
Session variables
There are a handful of session variables that can be accessed in CDS ABAP.
$session.<variable_name>
So far there are the following variable names, which largely correspond to
components of the SY or SYST structure in ABAP:
15
● USER_DATE - Current date of the user, equivalent to SY-DATLO
Syntax basics
Strings, as literals, are set in quotation marks in the source code. Numeric
literals without decimal separator are of type ABAP.INT1 up to ABAP.INT8,
depending on the value. Numeric literals with the dot as decimal separator
are of the data type ABAP.FLTP.
Comments can either start at the end of the line with a double slash // or be
placed as block comments between /* and */.
In CDS ABAP the arithmetic operators for addition (+), subtraction (-) and
multiplication (*) work as you would expect. However, the division differs
from this. Basically, a whitespace is required before and after the division
operator (/).
16
● For operator expressions with ABAP.FLTP both operands must have
this data type. This is especially disturbing since literals with decimal
places are of the type ABAP.FLTP. The only thing that helps here is a
CAST.
● Division with the division operator (/) is allowed in classic CDS views only
for floating point data types. Therefore, use the SQL function DIVISION(
<numerator>, <denumerator>, <decimal places> ), which rounds
automatically.
● For calculations that result in a decimal number, a check is made to
ensure that the result is convenient for the ABAP.DEC data type. This
means a maximum of 31 digits, 14 of which are after the decimal point.
If this is not the case, the operands must be reduced beforehand per
CAST.
SAP has provided standard CDS views for each SAP module
Note: If you choose to use a standard CDS view to meet your business
requirements, it is a best practice to duplicate it and make your
customizations in the copy. The standard views may undergo updates and
overwrite your code.
17
CASE Expressions
A CASE expression in CDS ABAP always returns exactly one value, depending
on the conditions. The simple CASE expression compares an expression to
several other expressions for equality:
18
If no condition was evaluated to TRUE, either the value from the ELSE clause
or NULLis returned.
The syntax described here is largely identical for CDS Views and CDS View
Entities. There are two important differences in view entities:
19
Associations
The names of the associations always start with an underscore. They are
included in the field list and thus they are available to the users of the CDS
View.
20
The field list consists of expressions. These are mostly field names of the
sources. However, there may be other elements in it as well. The following
are possible:
The expressions can be nested within each other. For example, a parameter
can be used as a comparison value in a CASE expression.
The individual fields are separated by commas. If a field name is not referred
to, an alias name assigned with AS is mandatory for the field. The keyword
KEY marks the key value.
Path expressions
Path expressions can be used to include fields from associated sources in the
field list of the CDS view or query on the CDS entity. The path expressions can
also go over several levels. It is also possible to add attributes to the
evaluation of the associations, for example to filter or to define the join type:
21
In ABAP SQL
The path expressions can be used in ABAP SQL. However, the syntax is
slightly different. Before associations, and as separator between them, there
is always a backslash (\). The component is addressed (as usual in ABAP) with
a hyphen (-):
In the database there is the pseudo-value NULL, which signalizes the absence
of a value. This is translated into an initial value in ABAP. This can lead to
unexpected situations when aggregating, since two groups are formed - one
for NULL and one for initial values in CDS ABAP - but both are initial in ABAP.
Replace NULL
You can catch NULL values with the SQL function COALESCE(<value1>,
<value2>), because then this function returns the 2nd value.
22
A normal comparison with NULL always results in the logical value
UNKNOWN. Thus, such a comparison is never valid in a WHERE condition.
That is why the predicate IS (NOT) NULL is needed.
Be careful when using blanks at the beginning or at the end of strings (STR).
These are partially removed. A look at the documentation is essential here!
23
CDS Table Functions
CDS Table Functions are views programmed in SQLScript. They are based on
an AMDP function encapsulated by a CDS ABAP object.
24
The CDS table function defines the signature:
25
Numeric SQL functions
26
Date and time calculation
The functions in the CDS ABAP are the same as those available in the
OpenSQL in ABAP. The data types play a major role in the SQL functions,
which is why their abbreviation is always prefixed, e.g.:
● DATS is the popular ABAP data type, for the DB only CHAR(8)
● DATN is the SQL date format on HANA
● TIMS is the ABAP data type for time
● TIMN is the SQL time format on HANA
● TSTMP is the short timestamp, 15-digit decimal number
● TSTMPL is the long timestamp with 7 DP digits.
● UTCL corresponds to the ABAP data type UTCLONG, internally 8 bytes
The names of the CDS ABAP SQL functions start with the data type.
Conversions
● DATS_TO_DATN
● DATS_FROM_DATN
● TIMS_TO_TIMN
● TIMS_FROM_TIMN
● DATS_TIMS_TO_TSTMP
● TSTMP_TO_DATS
● TSTMP_TO_DST
● TSTMP_TO_TIMS
● TSTMPL_TO_UTCL
27
● TSTMPL_FROM_UTCL
Validity
● TSTMP_IS_VALID
● TIMS_IS_VALID
● DATS_IS_VALID
Addition
● DATN_ADD_DAYS
● DATN_ADD_MONTHS
● DATS_ADD_DAYS
● DATS_ADD_MONTHS
● TSTMP_ADD_SECONDS
● UTCL_ADD_SECONDS
Differences
● UTCL_SECONDS_BETWEEN
● TSTMP_SECONDS_BETWEEN
● DATS_DAYS_BETWEEN
28
● DATN_DAYS_BETWEEN
Current time
● UTCL_CURRENT
● TSTMP_CURRENT_UTCTIMESTAMP
● ABAP_SYSTEM_TIMEZONE
● ABAP_USER_TIMEZONE
CDS Cube Views are the basis of key figure reports. You can associate
additional CDS dimension views to enrich the attributes and texts of features.
29
CDS Dimension Views
The CDS Dimension Views provide attributes for a feature. These attributes
can be time-dependent.
30
Parameter
Parameters can be defined for a CDS ABAP View, which are then used as an
expression, e.g. for calculation, as a comparison value or as a function
parameter.
31
CDS Query Views
The source for a CDS Query View is always a CDS InfoProvider View. The CDS
ABAP Annotation @Analytics.query: true turns a view into a CDS Query View.
32
Filter with prompt
CDS Extractors
33
Delta extraction with timestamp field
As with the generic delta, a delta can be formed using a timestamp field.
Optionally, a safety interval can also be specified.
Star Schema
34
The structure of the analytical model resembles a star. The cube or fact view
In the next example, we will implement a simple analytical model that lets us
35
A CDS view is defined for existing database tables and views, or for other CDS
views in ABAP Dictionary, using the ABAP CDS statement DEFINE VIEW. A CDS
view serves to define the structure of an SQL view.
When activating a CDS view, the following objects are created in ABAP
Dictionary:
Note: SQL views and CDS entities are part of one and the same namespace.
Therefore, you must assign different names for an SQL view and the entity.
36
37