MATERIALIZED columns compute and store values at INSERT time, making queries faster at the cost of disk space and insert overhead.
MATERIALIZED columns are automatically calculated at insert time and cannot be set by users.
import { OlapTable, Key, DateTime, ClickHouseMaterialized, ClickHouseCodec, UInt64 } from "@514labs/moose-lib"; interface UserEvents { id: Key<string>; timestamp: DateTime; userId: string; logBlob: Record<string, any> & ClickHouseCodec<"ZSTD(3)">; // Extract date for partitioning eventDate: Date & ClickHouseMaterialized<"toDate(timestamp)">; // Precompute hash for fast lookups userHash: UInt64 & ClickHouseMaterialized<"cityHash64(userId)">; // Parse JSON once at insert (expensive) combinationHash: UInt64[] & ClickHouseMaterialized<"arrayMap(kv -> cityHash64(kv.1, kv.2), JSONExtractKeysAndValuesRaw(toString(logBlob)))"> & ClickHouseCodec<"ZSTD(1)">;} export const UserEventsTable = new OlapTable<UserEvents>("UserEvents", { orderByFields: ["userHash", "eventDate"], partitionBy: "toYYYYMM(eventDate)"});Date/Time Extraction:
toDate(timestamp) - Extract date for partitioningtoHour(timestamp) - Extract hour for time-series analysistoStartOfMonth(timestamp) - Monthly aggregation keyHash Functions:
cityHash64(user_id) - Fast user lookupscityHash64(user_id, session_id) - Combined hash for deduplicationJSON Processing:
JSONExtractString(log_blob, 'level') - Extract specific fieldarrayMap(kv -> cityHash64(...), JSONExtractKeysAndValuesRaw(...)) - Hash all key-value pairsUse the exact field names from your data model. Moose preserves your naming convention (camelCase in TypeScript, snake_case in Python) in ClickHouse columns.
Restrictions:
Schema Changes:
ALTER TABLE ADD COLUMN ... MATERIALIZED exprALTER TABLE MODIFY COLUMN ... MATERIALIZED new_expr (preserves existing values)ALTER TABLE MODIFY COLUMN ... REMOVE MATERIALIZEDWhen using moose init --from-remote, MATERIALIZED column definitions are automatically preserved:
moose init my-app --from-remote --language typescript
# Generated models include ClickHouseMaterialized annotations