We value your privacy

This site uses cookies to improve your browsing experience, analyze site traffic, and show personalized content. See our Privacy Policy.

  1. MooseStack
  2. Moose OLAP
  3. Alias Columns

Alias Columns

ALIAS columns define virtual computed values that are calculated at query time, not stored on disk. They behave like SQL views at the column level — the expression is re-evaluated every time the column is read.

When to use

  • Derive values without storing them (zero disk cost)
  • Expose computed fields in SELECT queries without materializing data
  • Prototype computed columns before committing to MATERIALIZED
Computed at Query Time

ALIAS columns are not physically stored. They are calculated on the fly when queried, so they add no insert overhead or disk usage, but do add query-time CPU cost.

Basic Usage

AliasExample.ts
import { OlapTable, Key, DateTime, ClickHouseAlias, UInt64 } from "@514labs/moose-lib"; interface UserEvents {  id: Key<string>;  timestamp: DateTime;  userId: string;   // Virtual date column — computed when queried  eventDate: Date & ClickHouseAlias<"toDate(timestamp)">;   // Virtual hash — no storage cost  userHash: UInt64 & ClickHouseAlias<"cityHash64(userId)">;} export const UserEventsTable = new OlapTable<UserEvents>("UserEvents", {  orderByFields: ["id"],});

ALIAS vs MATERIALIZED

ALIASMATERIALIZED
Stored on diskNoYes
Computed whenQuery timeInsert time
Disk costNoneSame as a regular column
Query costExpression re-evaluated per queryZero (pre-computed)
Best forLightweight derivations, prototypingExpensive computations, indexed lookups

Use ALIAS when storage matters more than query speed. Use MATERIALIZED when the expression is expensive and queried often.

Common Patterns

Human-Readable Formatting:

  • formatReadableSize(size_bytes) — Display 4678899 as 4.46 MiB
  • formatReadableQuantity(count) — Display 1234567 as 1.23 million
  • formatReadableTimeDelta(elapsed_seconds) — Display 3661 as 1 hour and 1 second

Date/Time Derivations:

  • toDate(timestamp) — Extract date without storing it
  • toHour(timestamp) — Derive hour on the fly
  • toStartOfMonth(timestamp) — Monthly bucketing

String Transformations:

  • lower(email) — Case-normalized view
  • concat(first_name, ' ', last_name) — Computed full name
  • extractURLParameter(url, 'utm_source') — Parse URL parameters on read
ALIAS vs MATERIALIZED

If you find yourself querying an ALIAS column frequently or using it in WHERE/ORDER BY clauses, consider promoting it to a MATERIALIZED column so the value is pre-computed and stored.

Important Notes

Column Names in Expressions

Use the exact field names from your data model. Moose preserves your naming convention (camelCase in TypeScript, snake_case in Python) in ClickHouse columns.

Restrictions:

  • Cannot combine ALIAS with DEFAULT or MATERIALIZED (mutually exclusive)
  • Cannot be primary keys (ALIAS columns are virtual — they don't exist in storage)
  • Cannot INSERT values into ALIAS columns
  • Excluded from SELECT * by default — you must name them explicitly, or set asterisk_include_alias_columns=1

Schema Changes:

  • Add: ALTER TABLE ADD COLUMN ... ALIAS expr
  • Modify: ALTER TABLE MODIFY COLUMN ... ALIAS new_expr
  • Remove: ALTER TABLE MODIFY COLUMN ... REMOVE ALIAS

Syncing from Remote

When using moose init --from-remote, ALIAS column definitions are automatically preserved:

moose init my-app --from-remote --language typescript
# Generated models include ClickHouseAlias annotations

Related

  • Materialized Columns — Pre-compute and store values at insert time
  • Data Types — All supported column types
  • Schema Optimization — Optimize storage
  • TTL (Time-to-Live) — Auto-expire data
  • ClickHouse Docs — Detailed reference

On this page

When to useBasic UsageALIAS vs MATERIALIZEDCommon PatternsImportant NotesSyncing from RemoteRelated
Edit this page
FiveonefourFiveonefour
Fiveonefour Docs
MooseStackHostingTemplatesGuides
Release Notes
Source531
  • Overview
Build a New App
  • 5 Minute Quickstart
  • Browse Templates
  • Existing ClickHouse
Add to Existing App
  • Next.js
  • Fastify
Fundamentals
  • Moose Runtime
  • MooseDev MCP
  • Language Server
  • Data Modeling
Moose Modules
  • Moose OLAP
    • Data Modeling
    • Tables
    • Views
    • Materialized Views
    • Materialized Columns
    • Alias Columns
    • External Data & Introspection
    • External Tables
    • Introspecting Tables
    • Data Access
    • Inserting Data
    • Reading Data
    • Performance & Optimization
    • Schema Optimization
    • Secondary & Data-skipping Indexes
    • Projections
    • TTL (Time-to-Live)
    • Schema Versioning
  • Moose Streaming
  • Moose Workflows
  • Moose APIs & Web Apps
Deployment & Lifecycle
  • Moose Dev
  • Moose Migrate
  • Moose Deploy
Reference
  • API Reference
  • Query Layer
  • Data Types
  • Table Engines
  • CLI
  • Configuration
  • Observability Metrics
  • Help
  • Release Notes
Contribution
  • Documentation
  • Framework
AliasExample.ts
import { OlapTable, Key, DateTime, ClickHouseAlias, UInt64 } from "@514labs/moose-lib"; interface UserEvents {  id: Key<string>;  timestamp: DateTime;  userId: string;   // Virtual date column — computed when queried  eventDate: Date & ClickHouseAlias<"toDate(timestamp)">;   // Virtual hash — no storage cost  userHash: UInt64 & ClickHouseAlias<"cityHash64(userId)">;} export const UserEventsTable = new OlapTable<UserEvents>("UserEvents", {  orderByFields: ["id"],});