@GENERATED Directive

Directive @generated generates a field from existing fields/columns of the table or from SQL expressions.

Directive @generated takes optional argument path or sql, with an value that's used to calculate the JSON field value. The path value is a SQL/JSON path expression. The sql value is a SQL expression or query. Note that, when using the GraphQL table function, the only supported argument is the sql. The path argument is available only for creating duality-views.

Note:

  • Generated fields augment the documents produced by both duality views and GraphQL table functions. These fields are computed rather than directly mapped to underlying columns, and are always read-only.
  • A generated field does not have a column name. It can be referenced only by an alias.

The following example finds the race month using a sql expression:

SELECT JSON_SERIALIZE(data PRETTY) AS data FROM GRAPHQL('
    race {
        id: race_id
        name
        month @generated(sql: "regexp_substr(race_date, ''[^-]+'', 1, 2)")
    }
');
Which produces the following output containing the race month:
DATA                                                                           
--------------------------------------------------------------------------------
{                                                                              
  "id" : 201,                                                                  
  "name" : "Bahrain Grand Prix",                                               
  "month" : "MAR"                                                              
}                                                                              
                                                                                 
{                                                                              
  "id" : 202,                                                                  
  "name" : "Saudi Arabian Grand Prix",                                         
  "month" : "MAR"                                                              
}                                                                              
                                                                                 
{                                                                              
  "id" : 203,                                                                  
  "name" : "Australian Grand Prix",                                            
  "month" : "MAR"                                                              
}                                                                              
                                                                                 
{                                                                              
  "id" : 204,                                                                  
  "name" : "Japanese Grand Prix",                                              
  "month" : "APR"                                                              
} 
.......................
.......................
24 rows selected.