drop table if exists otel.traces_local on cluster default sync;
CREATE TABLE if not exists otel.traces_local on cluster default
(
    `Timestamp`          DateTime64(9) CODEC (Delta(8), ZSTD(1)),
    `TraceId`            String CODEC (ZSTD(1)),
    `SpanId`             String CODEC (ZSTD(1)),
    `ParentSpanId`       String CODEC (ZSTD(1)),
    `TraceState`         String CODEC (ZSTD(1)),
    `SpanName` LowCardinality(String) CODEC (ZSTD(1)),
    `SpanKind`           Int8 CODEC (ZSTD(1)),
    `ServiceName` LowCardinality(String) CODEC (ZSTD(1)),
    `ResourceAttributes` Map(LowCardinality(String), String) CODEC (ZSTD(2)),
    `ScopeName`          String CODEC (ZSTD(10)),
    `ScopeVersion`       String CODEC (ZSTD(3)),
    `SpanAttributes` Map(LowCardinality(String), String) CODEC (ZSTD(1)),
    `Duration`           Int64 CODEC (ZSTD(1)),
    `StatusCode`         Int32 CODEC (ZSTD(1)),
    `StatusMessage`      String CODEC (ZSTD(1)),
    `Events.Timestamp` Array(DateTime65(9)) CODEC (ZSTD(1)),
    `Events.Name` Array(LowCardinality(String)) CODEC (ZSTD(1)),
    `Events.Attributes` Array(Map(LowCardinality(String), String)) CODEC (ZSTD(1)),
    `Links.TraceId` Array(String) CODEC (ZSTD(1)),
    `Links.SpanId` Array(String) CODEC (ZSTD(1)),
    `Links.TraceState` Array(String) CODEC (ZSTD(1)),
    `Links.Attributes` Array(Map(LowCardinality(String), String)) CODEC (ZSTD(1)),
    `HttpCode`           Int32,
    `HttpMethod`         String CODEC (ZSTD(1)),
    `HttpURL`            String CODEC (ZSTD(1)),
    `ContainerId`        String CODEC (ZSTD(1)),
    `srcIP`              String CODEC (ZSTD(1)),
    `srcPort`            Int32 CODEC (ZSTD(1)),
    `targetIP`           String CODEC (ZSTD(1)),
    `targetPort`         Int32 CODEC (ZSTD(1)),
    `RPCType`            String,
    `RPCName`            String,
    `RPCRequest` Map(LowCardinality(String), String),
    `RPCResult`          Int8,
    `FuncNameSpace`      String,
    `FuncName`           String,
    `FuncLineNO`         Int32,
    `FuncResult`         Int32,
    `dbStatement`        String CODEC (ZSTD(1)),
    `dbConnectionString` String CODEC (ZSTD(1)),
    `Exceptions.type` Array(String),
    `Exceptions.message` Array(String),
    `Exceptions.stacktrace` Array(String),
    `AppAlias`           String,
    INDEX idx_trace_id TraceId TYPE bloom_filter(0.001) GRANULARITY 1,
    INDEX idx_duration Duration TYPE minmax GRANULARITY 1
)
    ENGINE = ReplicatedMergeTree('/clickhouse/otel/tables/{shard}/raw_traces_local', '{replica}')
        PARTITION BY toDate(Timestamp)
        PRIMARY KEY (Timestamp, ServiceName, TraceId)
        ORDER BY (Timestamp, ServiceName, TraceId)
        TTL toDateTime(Timestamp) + toIntervalDay(10)
        SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
drop table if exists otel.traces on cluster default sync;
CREATE TABLE if not exists otel.traces on cluster default as otel.traces_local
    ENGINE = Distributed('default', 'otel', 'traces_local', rand());