Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
AndreaUS
Product and Topic Expert
Product and Topic Expert

Since ABAP release 7.88, SAP BTP ABAP Environment 2205, CDS view entities are feature complete. In this blog series, you find a complete list of new features, improvements, and differences of CDS view entities compared to CDS DDIC-based views.
Introduction and Overview
Part l: New features
Part ll: Improvements
Part lll: Differences

Part l: New features

CDS view entities offer the following new features:

  1. Typed literals
  2. Set operators EXCEPT and INTERSECT
  3. Calculated quantity with calculated unit reference
  4. Entity buffering using tuning objects
  5. Extensions with EXTEND VIEW ENTITY
  6. New functions:
    1. REPLACE_REGEX
    2. GET_NUMERIC_VALUE
    3. CURR_TO_DECFLOAT_AMOUNT
  7. $PROJECTION to reuse expressions from the SELECT list
  8. Refactoring of view stacks

1. Typed literals

Release info: Typed literals are available since ABAP release 7.83, SAP BTP ABAP Environment 2102.

CDS DDIC-based viewCDS view entity
    • Only untyped literals available.
    • Numeric literals can have data types INT1, INT2, INT4, INT8, FLTP.
    • Character literals have data type CHAR or NUMC.
    • Untyped + typed literals available.
    • Almost all data types supported.
    • More type-safe handling.

CDS DDIC-based views offer untyped character literals and untyped numeric literals of an integer type or of type FLTP. By contrast, CDS view entities offer typed literals that cover almost all data types. This reduces the need for casting and conversions. What’s more, typed literals explicitly define their data type, ensuring more type-safe handling. Type compatibility is checked in your IDE during design time.

A typed literal is a literal whose data type is defined by specifying an ABAP Dictionary type explicitly.
Syntaxdatatype'<literal>'
Exampleabap.decfloat34'123.456E789'
datatype specifies the required data type with the type namespace abap. as prefix, followed by the character-like representation of the value in single quotes ('). Almost all built-in ABAP Dictionary types are possible: INT1, INT2, INT4, INT8, DEC, DECFLOAT16, DF16_DEC, DF16_RAW, DECFLOAT34, DF34_DEC, DF34_RAW, FLTP, CHAR, SSTRING, STRING, RAW, RAWSTRING, DATN, DATS, TIMN, TIMS, UTCLONG, NUMC, CLNT, LANG, CURR, CUKY, QUAN, UNIT.

<literal> specifies the value in single quotes. The value must match the data type specified under datatype. Otherwise, a syntax check error occurs.

Here’s an example for a CDS view entity using date and timestamp typed literals:

@EndUserText.label: 'CDS view entity, typed literals'
define view entity DEMO_CDS_DATE_AND_TIME_TYP_LIT
  as select from demo_ddic_types
{
  key id,
      utcl_add_seconds(abap.utcl'2020-01-02 23:59:59,1234567',
                              200)             as add_seconds,
      datn_add_months(abap.datn'20200104', 13) as add_months,
      case when datn < abap.datn'20200101'
      then abap.char'<'
      else abap.char'>'
      end                                      as caseExpressionField
}
where
  utcl <> abap.utcl'2018-02-21 23:59:59.1234567'

For further details, see ABAP Keyword Documentation on SAP Help Portal: CDS DDL - CDS View Entity, Typed Literals

2. Set operators EXCEPT and INTERSECT

Release info: EXCEPT and INTERSECT are available since ABAP release 7.85, SAP BTP ABAP Environment 2108.
While CDS DDIC-based views offer only the UNION set operator, CDS view entities offer UNION, EXCEPT, and INTERSECT.
Set operators merge the result sets of multiple queries into a single result set. In CDS view entities, the following set operators are available:

  • EXCEPT returns all distinct rows of the first result set that are not part of the following result sets.
  • INTERSECT returns all distinct rows that are part of all result sets.
  • UNION merges the result sets of all queries.

The following image illustrates how each set operator merges several result sets:


Example for an EXCEPT view
The following CDS view entity combines the result sets of two SELECT statements using EXCEPT. It returns all flights that cost less than 2000, at the same time excluding all flights that cost between 600 and 900.

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS view entity, set operator EXCEPT'
@Metadata.ignorePropagatedAnnotations: true

define view entity DEMO_CDS_EXCEPT
  as select from sflight
{
  carrid,
  connid,
  @Semantics.amount.currencyCode: 'currency'
  price,
  currency
}
where
  price < 2000
except
select from sflight
{
  carrid,
  connid,
  price,
  currency
}
where
  price between 600 and 900

For further details, see ABAP Keyword Documentation on SAP Help Portal: CDS DDL - CDS View Entity, Set Operators.

3. Calculated quantity with calculated unit reference

Release info: Calculated quantities with calculated unit reference are available since ABAP release 7.84, SAP BTP ABAP Environment 2105.
In CDS view entities, a new type of quantity field is available: the calculated quantity with calculated unit reference.

Example

Arithmetic expressionResult
770 € (Amount) / 100m² (Quantity)7,70€/m² (Calculated Quantity)

The arithmetic expression 770€ / 100 m2 calculates cost per square meter. It divides an amount by a quantity. The result of this calculation is a calculated quantity that has calculated unit reference assigned. This calculated unit is not registered in the unit check table T006.

Calculated quantity:

  • Data type DECFLOAT34.
  • Reference: calculated unit.
  • Annotation: @Semantics.quantity.unitOfMeasure: 'calculatedUnit'.

Calculated unit:

  • Data type CHAR.
  • No entry in check table T006.
  • New domain + data element DD_CDS_CALCULATED_UNIT available for your convenience.
  • In analytical queries, the calculated unit is defined as virtual element.

Note: A CDS view entity selecting from another CDS view entity inherits the calculated quantity characteristic.
Example
In the following CDS view entity, the field rent_per_size divides the amount of the rent for an apartment by the apartment size. The result is the cost per square meter.

  • Field rent_decfloat34 has data type DECFLOAT34 and a currency key reference.
  • Field apartment_size has data type QUAN(10,2) and a unit key reference.
  • The calculated unit is defined by means of a concatenation of a currency code, a forward slash, and the apartment unit. The resulting calculated unit is EUR/MTK, MTK being the unit ID for square meter.
@AccessControl.authorizationCheck: #NOT_ALLOWED
@EndUserText.label: 'CDS view entity, calculated quantity'
define view entity DEMO_CDS_CALCULATED_QUANTITY
  as select from demo_rent
{
  key apartment_id                     as ApartmentId,
      apartment_size                   as ApartmentSize,
      apartment_unit                   as ApartmentUnit,
      currency                         as Currency,      

      // currency field and unit field in arith expression
      @Semantics.quantity.unitOfMeasure: 'calculatedUnit'
      rent_decfloat34 / apartment_size as rent_per_size,
      concat( concat(currency, '/' ), apartment_unit ) 
                                       as calculatedUnit
}

If data is inserted into the underlying database talbe demo_rent, the data preview might look as follows:

For further details, see ABAP Keyword Documentation on SAP Help Portal: ABAP CDS - Calculated Quantity with Calculated Unit.

4. Entity buffering using CDS tuning objects

Release info: Available since ABAP release 7.87, SAP BTP ABAP Environment 2202.
In CDS DDIC-based views, buffering is configured via annotations.

  • @AbapCatalog.buffering.status defines whether and how table buffering is allowed.
  • @AbapCatalog.buffering.type determines the buffering type.
  • @AbapCatalog.buffering.numberOfKeyFields determines the number of key fields covered if generic buffering is used.

In CDS view entities, these annotations are not supported. A new buffer handling has been implemented to provide more flexibility in buffering settings and to enable customers to change the buffering settings of SAP-delivered objects.
First of all, in a CDS view entity, buffering is prepared with the annotation @AbapCatalog.entityBuffer.definitionAllowed: true|false.
If buffering is allowed, the actual buffering type can be defined by a separate CDS tuning object, called CDS entity buffer, with the DDL statement: DEFINE VIEW ENTITY BUFFER ON cds_view_entity.
Syntax

DEFINE VIEW ENTITY BUFFER ON <entity_name>
    TYPE SINGLE|GENERIC|FULL
    LAYER CORE|LOCALIZATION|INDUSTRY|PARTNER|CUSTOMER
    [NUMBER OF KEY ELEMENTS <number>] // Needed for buffer type "GENERIC"

The CDS entity buffer relates the buffering type to one of the layers: core, localization, industry, partner, and customer. For each layer, one buffering type can be defined for a CDS view entity.
For details, see Buffering CDS View Entities | SAP Blogs: this blog post contrasts buffering of CDS DDIC-based views and CDS view entities and describes how to define a CDS view entity buffer.
For further details, see ABAP Keyword Documentation on SAP Help Portal: ABAP CDS - Table Buffering of CDS View Entities.

5. Extensions with EXTEND VIEW ENTITY

Release info: Available since ABAP release 7.78.
Extension to a CDS DDIC-based view:

@AbapCatalog.sqlViewAppendName: 'DEMO_CDS_EXTENS'
extend view demo_cds_original_view with demo_cds_view_extension 
  {
    spfli.distance,
    spfli.distid as unit,
    _assoc
  };

Extension to a CDS view entity:

extend view entity demo_cds_original_view_ve with
  {
    _assoc
  }

Differences:

  • No DDIC append view defined via @sqlViewAppendName.
  • No name after WITH.
  • The CDS view entity extension defines one single new association. A view extension to a CDS DDIC-based view requires at least one extension field.

Additional advantages of CDS view entity extensions:

  • The activation time of CDS view entity extensions is drastically reduced.
  • CDS view entity extensions support the new features, such as typed literals and calculated quantities.

Further details are described in the blog post ABAP Core Data Services: New syntax for extending CDS entities | SAP Blogs.

For further details, see ABAP Keyword Documentation on SAP Help Portal:
CDS DDL - EXTEND VIEW ENTITY.

6. New functions

CDS view entities offer new built-in functions.

REPLACE_REGEXPR

Release info: Available since ABAP release 7.81, SAP BTP ABAP Environment 2208.
The built-in function REPLACE_REGEXPR is available in CDS view entities and also in ABAP SQL. It replaces a character string within a text by another character string.

Syntax

REPLACE_REGEXPR( PCRE           => pcre,
                 VALUE          => arg1,
                 WITH           => arg2,
                 RESULT_LENGTH  => res[,
                 OCCURRENCE     => occ][,
                 CASE_SENSITIVE => case][,
                 SINGLE_LINE    => bool][,
                 MULTI_LINE     => bool][,
                 UNGREEDY       => bool])

Effect: A Perl Compatible Regular Expression (PCRE) pcre is replaced in a given string (specified after VALUE) with a character string specified after the keyword parameter WITH. The keyword parameter OCCURRENCE is optional and determines the number of occurrences of pcre to be replaced. By default, all occurrences are replaced. The search is case-sensitive by default, but this can be overridden using the parameter CASE_SENSITIVE. Single-line, multiline, and ungreedy regular expression matching can be set with the respective parameters, SINGLE_LINE, MULTI_LINE, and UNGREEDY.
Example

@AccessControl.authorizationCheck: #NOT_REQUIRED
define view entity demo_cds_sql_func_string_ve
  as select from demo_expressions
{
    replace_regexpr(   pcre => '\\Cg', 
                     value => char2, 
                     with => 'bb', 
                     result_length => 4  )     as r_replace_regexpr
}

For further details, see ABAP Keyword Documentation on SAP Help Portal: CDS DDL - CDS View Entity, String Functions.
GET_NUMERIC_VALUE
Release info: Available since ABAP release 7.83, SAP BTP ABAP Environment 2102.
GET_NUMERIC_VALUE returns the numeric value of a currency or quantity field without its currency or unit key. This function has been introduced to support the overhauled currency and quantity handling. If an operand position does not accept an amount or quantity field, due to stricter syntax checks, GET_NUMERIC_VALUE can be used to remove the reference to a unit or currency key.
Example

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS view entity, GET_NUMERIC_VALUE'
@Metadata.ignorePropagatedAnnotations: true
define view entity DEMO_CDS_GET_NUMERIC_VALUE
as select from sflight { key carrid, key connid, key fldate, @Semantics.amount.currencyCode: 'currency' price, currency, @Semantics.amount.currencyCode: 'currency' paymentsum, GET_NUMERIC_VALUE( paymentsum ) / GET_NUMERIC_VALUE( price ) as number_of_bookings }

For further details, see ABAP Keyword Documentation on SAP Help Portal: CDS DDL - CDS View Entity, Unit and Currency Conversion Functions, Variant 3.

CURR_TO_DECFLOAT_AMOUNT
Release info: Available since ABAP release 7.83, SAP BTP ABAP Environment 2102.
CURR_TO_DECFLOAT_AMOUNT converts a currency field of data type CURR into a currency field of data type DECFLOAT34. This function has been introduced to support the overhauled currency and quantity handling. It is required if you want to use a currency field in an expression, for example in an arithmetic expression, since CDS currency fields are generally not supported as operands of expressions.

Example

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS view entity, CURR_TO_DEC'
define view entity DEMO_CDS_CURR_TO_DEC
  as select from demo_ddic_types
{
  key id,
      cuky                              as currency,
      @Semantics.amount.currencyCode: 'currency'
      curr10_4                          as amount,
      @Semantics.amount.currencyCode: 'currency'
      CURR_TO_DECFLOAT_AMOUNT(curr10_4) as curr_conv
}

For further details, see ABAP Keyword Documentation on SAP Help Portal: CDS DDL - CDS View Entity, Unit and Currency Conversion Functions, Variant 4.

7. $PROJECTION to reuse expressions from the SELECT list

Release info: Available since ABAP release 7.84, SAP BTP ABAP Environment 2105.
CDS view entities support the syntax $PROJECTION.element to reuse an expression that was defined in the SELECT statement of the same CDS view entity. A reuse expression can be used in the SELECT list as part of an expression, or in the ON clause of a CDS association.
Example:
The following CDS view entity reuses a field, a literal, a case expression, a string function, and a cast expression in different operand positions of the SELECT statement.

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS view entity, expression reuse'
define view entity DEMO_CDS_EXPRESSION_REUSE
  as select from demo_expressions
{
      //field
  key id                                                   as field1,
      concat($projection.field1, $projection.case1)        as field_reuse,
      //literal
      abap.char'hallo'                                     as lit1,
      concat($projection.lit1, $projection.case_reuse)     as lit_reuse,
      //arithmetic expression
      abap.decfloat34'123.45E6'                            as arith,
      $projection.arith * $projection.builtIn_reuse        as arith_reuse,
      //cast expression
      cast(char1 as abap.dec(10))                          as cast1,
      coalesce($projection.cast1, $projection.arith_reuse) as cast_reuse,
      //built-in function
      abs(dec1)                                            as builtIn,
      cast($projection.builtIn as abap.int4)               as builtIn_reuse,
      //case distinction
      case char2
          when 'Anna' then 'X'
          when 'Lisa' then 'Y'
          else '-'
          end                                              as case1,
      left($projection.case1, 1)                           as case_reuse
}

To find a list of all expressions that can be reused, plus all operand positions that accept $PROJECTION.element, see the ABAP Keyword Documentation on SAP Help Portal: CDS DDL - CDS View Entity, $projection.

8. Refactoring of view stacks

Release info: Available since ABAP release 7.88, SAP BTP ABAP Environment 2205.
Refactoring of view stacks has become much faster and easier for CDS view entities. If a developer wants to rename a field or an association across a view stack, they can manually edit all affected DDL files, and then activate those DDL files together with the mass activation button. This improved refactoring works for fields and associations, also associations of type to-parent or composition.
Here’s an example:
Scenario: You want to rename a field that is used in multiple views that select from each other and build a view stack.

Issue when working with a CDS DDIC-based view: An activation error occurs when you try to mass-activate the changed objects, saying that field my_field is still used in another source.
Workaround to solve the issue:

  1. Add a dummy field of the required new name to the base object. Activate the base object.
  2. Replace the field name in consumer views.
  3. Delete the dummy field in base objects.

Solution available in CDS view entities: Renaming of fields across a view stack is possible via mass activation.
Conclusion: Fields and associations can be renamed consistently across the stack and the affected objects can be activated in one mass activation. This makes refactoring much faster and easier since the intermediate steps of the workaround are no longer necessary.

Further information

5 Comments
Labels in this area