> For the complete documentation index, see [llms.txt](https://docs.datapancake.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datapancake.com/core-concepts/attribute-metadata/attributes-in-code-generation.md).

# Attributes in Code Generation

How active polymorphic versions become SQL columns in Dynamic Tables, including transformation application and array table generation.

## Dynamic Table Column Generation

For each active polymorphic version with `INCLUDE_IN_CODE_GEN = TRUE`, DataPancake generates a column:

1. **Column name:** Uses `ALIAS_NAME` if provided, otherwise `POLYMORPHIC_ATTRIBUTE_NAME` → `CODE_GENERATED_COLUMN_NAME`
2. **Type casting:** Uses `DATA_PLATFORM_DATA_TYPE` with precision/scale; applies datetime formats if `USE_DATETIME_FORMAT = TRUE`
3. **Value extraction:** Extracts from source using `ATTRIBUTE_PATH`; handles nested structures and arrays
4. **Transformation:**
   * If `TRANSFORMATION_TYPE = 'No Transformation'`: Applies `NULL_VALUE_EXPRESSION` (if provided)
   * If `TRANSFORMATION_TYPE = 'SQL Expression'`: Applies `TRANSFORMATION_EXPRESSION` (replaces `{attribute_name}` placeholder)
5. **Inclusion:** Only if `INCLUDE_IN_CODE_GEN = TRUE` AND `PARENT_INCLUDE_IN_CODE_GEN = TRUE` AND `VERSION_STATUS = 'active'`

***

## Example: Column Generation

**Attribute:** `customer.email` with polymorphic version `email_str`

**Configuration:**

* `DATA_PLATFORM_DATA_TYPE = 'VARCHAR(255)'`
* `TRANSFORMATION_TYPE = 'No Transformation'`
* `NULL_VALUE_EXPRESSION = 'COALESCE({attribute_name}, ''unknown@example.com'')'`
* `ALIAS_NAME = 'customer_email'`

**Generated SQL:**

```sql
COALESCE(json_data:"customer"."email"::VARCHAR(255), 'unknown@example.com') AS customer_email
```

**With transformation:**

* `TRANSFORMATION_TYPE = 'SQL Expression'`
* `TRANSFORMATION_EXPRESSION = 'UPPER({attribute_name})'`

**Generated SQL:**

```sql
UPPER(json_data:"customer"."email"::VARCHAR(255)) AS customer_email
```

***

## Array Table Generation

**Object arrays (`ARRAY_TYPE = 'object'`):**

* Separate Dynamic Table created for array contents
* Named using `CREATE_OBJECT_NAME_ALIAS_NAME` or default `{root_table_name}_array_object`
* Foreign key columns link array table to parent (configured in Foreign Keys section)

**Primitive arrays (`ARRAY_TYPE = 'primitive'`):**

* May be stored as VARIANT or ARRAY types in root table
* Or flattened into separate table depending on configuration
* `ARRAY_PRIMITIVE_TYPE` determines element handling

***

## ITDCs (Immutable Typed Derived Columns)

DataPancake uses explicit type casting (`::DATA_TYPE`) in generated SQL to prevent technical debt from implicit conversions.

**Example:**

```sql
-- DataPancake (explicit casting)
json_data:"customer"."email"::VARCHAR(255) AS customer_email

-- vs. implicit (technical debt)
json_data:"customer"."email" AS customer_email
```

***

## Code Generation Process

1. **Collect active attributes:** Query `vw_datasource_active_attributes` where `INCLUDE_IN_CODE_GEN = TRUE` and `PARENT_INCLUDE_IN_CODE_GEN = TRUE`
2. **Generate column statements:** For each active version, generate SQL with type casting and transformations
3. **Generate array tables:** Separate Dynamic Tables for object arrays with foreign key relationships
4. **Generate root table:** Combine all root-level columns with table-level settings
5. **Generate views:** Semantic layer views with `SEMANTIC_LAYER_EXPRESSION` and security policies

## Metadata Tracking

DataPancake can generate:

* **Streams:** Track changes to Dynamic Tables (change data capture)
* **Tasks:** Scheduled metadata synchronization
* **Metadata tables:** Track insert/update timestamps for audit and compliance


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.datapancake.com/core-concepts/attribute-metadata/attributes-in-code-generation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
