next up previous contents
Next: Attribute Composition Derivation Up: Derived Attributes Previous: Derived Attributes

Arithmetic and Aggregate Function Derivations

In this section we define derivation rules involving arithmetic expressions and aggregate functions. An arithmetic derivation rule for a derived attribute associated with object or protocol class consists of operators (+, -, *, /), constants, and other numeric attributes of . Attributes involved in an arithmetic expression must be simple or component attributes that are numeric, single-valued, and that are not associated with derivation rules.

An aggregate function derivation rule associated with object or protocol class consists of aggregate functions min, max, sum, or avg applied on a numeric attribute of , or aggregate function count applied on an attribute of . Attributes involved in an aggregate function derivation rule must be simple or component attributes that are multi-valued, and that are either not associated with derivation rules, or associated with composition path expressions.

<arithmetic expression> ::= <constant>
                          | <attribute name>
                          | ( <arithmetic expression> )
                          | <arithmetic expression> <op> <arithmetic expression>
                          ;
<op> ::=  + | - | * | / 
       ;
<aggregate expression> ::= <aggregate function> of <attribute name> 
                         ;
<aggregate function> ::= count | sum | avg | max | min
                       ;

The following object class definition contains an example of a derived attribute defined using an arithmetic expression:

OBJECT CLASS ENTRY
   DESCRIPTION: "an entry is part of a sequence from begin_pos to end_pos"
   ID: entry_id
   ATTRIBUTE entry_id: [1,1] INTEGER
   ATTRIBUTE from_sequence: [0,1] SEQUENCE
   ATTRIBUTE (begin_pos, end_pos): [0,1] (INTEGER, INTEGER)
   ATTRIBUTE length
                   DERIVATION: = end_pos - begin_pos + 1
                   DESCRIPTION: "length from beginning position to end position"

The following object class definition contains two examples of derived attributes involving aggregate function expressions:

OBJECT CLASS SPONSOR
   DESCRIPTION: "sponsor of a project"
   ID: sponsor_id
   ATTRIBUTE sponsor_id: [1,1] INTEGER
   ATTRIBUTE sponsor_name: [0,1] CHAR (80) 
   ATTRIBUTE sponsor_type: [0,1] SPONSOR_TYPE
   ATTRIBUTE address:  [0,1] VARCHAR (255)
   ATTRIBUTE office_phone:  [0,1] CHAR (12)
   ATTRIBUTE (account, project, amount): set-of [0,] (ACCOUNT, PROJECT, MONEY) 
   ATTRIBUTE total_amount
                       DERIVATION: sum of amount
                       DESCRIPTION: "total amount of money"
   ATTRIBUTE no_of_projects
                       DERIVATION: count of project

An arithmetic expression or aggregate function derived attribute is considered as single-valued, and therefore cannot be defined with an ORDER BY clause.