본문으로 건너뛰기
cccskills
무료GitHub에 공개

clickhouse-rust-view-column-order

Debug ClickHouse deserialization errors in Rust caused by column ORDER mismatch when using SELECT alias.* on a VIEW that adds computed columns. Use when: (1) Some query variants (e.g. sort=trending) return 500 but others (sort=recent) succeed, (2) Queries use SELECT view_alias.*, extra_cols FROM view JOIN ..., (3) The Rust Row struct has the right number and types of fields but deserialization fails anyway (wrong type error, not "not enough data"), (4) The VIEW was recently extended with a computed column (e.g. trending_score).

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

설치 방법 보기

포함된 파일(1)

  • SKILL.md6.1 KB

SKILL.md(원문)

설치하기 전에 에이전트에게 전달되는 지시 내용을 확인할 수 있습니다.

ClickHouse Rust: View Computed Column Ordering

Problem

When using SELECT alias.* , extra_col FROM my_view alias LEFT JOIN ..., ClickHouse expands alias.* to include ALL columns defined in the view — including any computed columns added by the view itself (e.g. SELECT *, expr AS trending_score FROM base). Those computed columns appear INSIDE the alias.* expansion, BEFORE any columns appended after it in the outer query.

The clickhouse Rust crate (#[derive(Row)]) deserializes positionally — field N in the Rust struct receives column N from the query result. If the struct field order doesn't match the actual SQL column order, the wrong bytes land in the wrong fields, causing type errors at deserialization even though the column count is correct.

Symptoms

  • HTTP 500 on one sort variant (e.g. sort=trending, sort=popular) but not others (e.g. sort=recent) that query a different view or table
  • Error like: "cannot decode Float64 from String" or similar type mismatch
  • Column COUNT is correct (no "not enough data" error)
  • Bug appears after adding a computed column to an existing view

Root Cause Explained

Suppose trending_videos is defined as:

CREATE VIEW trending_videos AS
SELECT
    *,                                          -- all base columns
    (views * 0.5 + likes * 2.0) AS trending_score  -- computed column APPENDED HERE
FROM video_stats;

An outer query then does:

SELECT
    tv.*,                   -- expands to: base_cols..., trending_score
    text_track_ref,         -- subtitle columns come AFTER
    text_track_content
FROM trending_videos tv
LEFT JOIN subtitle_subquery USING (id);

The actual SQL result column order is:

[...base_cols, trending_score, text_track_ref, text_track_content]

But if the Rust struct was written with subtitle fields before trending_score:

pub struct TrendingVideo {
    // ...base fields...
    pub text_track_ref: String,      // position N   <- WRONG: gets trending_score bytes
    pub text_track_content: String,  // position N+1 <- WRONG: gets text_track_ref bytes
    pub trending_score: f64,         // position N+2 <- WRONG: gets text_track_content bytes
}

ClickHouse sends a Float64 where Rust expects a String → deserialization error → 500.

Why Only Some Queries Fail

The sort=recent variant queries video_stats directly with vs.* — that view has no extra computed columns, so subtitle columns land at the same relative position the struct expects. Only the trending_videos view adds trending_score inside vs.*, shifting everything after it.

Debugging Steps

  1. Identify which query variants fail vs. succeed. Failing ones likely use a different view or have a different SELECT * source.

  2. Expand the failing SELECT alias.* — run the inner view query directly:

    curl -u 'user:pass' 'https://clickhouse-host:8443' \
      --data-binary "SELECT * FROM trending_videos LIMIT 0 FORMAT TabSeparated"
    

    Or use DESCRIBE TABLE trending_videos to see declared column order.

  3. List actual column order of the full outer query:

    curl ... --data-binary \
      "SELECT tv.*, '' as text_track_ref, '' as text_track_content
       FROM trending_videos tv LIMIT 0 FORMAT TabSeparatedWithNames"
    
  4. Compare with Rust struct field order line by line — they must match exactly.

  5. Find the misplaced field — look for computed columns added to the view that appear inside alias.* but after columns that appear in the outer SELECT.

Fix

Reorder the Rust struct fields to match the actual SQL column order — computed VIEW columns belong before any columns appended in the outer SELECT:

pub struct TrendingVideo {
    // ...base fields in same order as base table...

    // trending_score comes from tv.* (view-computed), so it appears
    // BEFORE the subtitle columns we append in the outer query
    pub trending_score: f64,

    // Subtitle columns are appended after tv.* in the outer SELECT
    pub text_track_ref: String,
    pub text_track_content: String,
}

Do NOT change the SQL query or the view — just align the struct field order.

Key Rule

When a ClickHouse VIEW adds a computed column via SELECT *, expr AS col FROM base, that column becomes part of the view's column list. Any SELECT alias.* in an outer query will emit it in the view's declared order — BEFORE any extra columns appended after alias.* in the outer SELECT. The Rust #[derive(Row)] struct must reflect this exact order.

Prevention

  • When adding a computed column to a ClickHouse view, immediately check ALL Rust structs that query that view with SELECT alias.* and update field ordering.
  • Add an integration test that queries the affected endpoint; CI will catch future ordering drift before staging.
  • Consider using SELECT col1, col2, ..., trending_score, text_track_ref, text_track_content (explicit column list instead of *) in the outer query to make ordering explicit and immune to view schema changes.

Related Skills

  • clickhouse-rust-type-mismatches — covers "not enough data" (column COUNT mismatch), FixedString encoding, Option vs String nullability issues. This skill covers column ORDER mismatch (column count is correct, types match, but positional order is wrong).

References

리뷰

아직 리뷰가 없습니다. 사용해 본 소감을 남겨 주세요.

댓글

로그인 없이 쓸 수 있습니다. 사용 후기, 질문, 작동하지 않은 환경 등을 편하게 남겨 주세요.

아직 댓글이 없습니다. 첫 댓글을 남겨 보세요.

보내면 바로 공개됩니다. 이메일 주소나 전화번호 같은 개인 정보는 쓰지 마세요. URL은 2개까지입니다.

0 / 1,000자

이 양식은 reCAPTCHA로 보호되며 Google 개인정보처리방침 및 서비스 약관이 적용됩니다.

같은 저장소의 스킬

Fix ArgoCD ExternalSecret deployment failing with "namespace X is not permitted in project Y". Use when: (1) ExternalSecret shows OutOfSync in ArgoCD but won't sync, (2) ArgoCD application status shows "namespace X is not permitted in project 'infrastructure'", (3) ExternalSecret targets a namespace managed by a different ArgoCD project, (4) Using apps-of-apps pattern with separate infrastructure and application projects.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

divinevideo/divine-mobile2662026년 10월 20일 업데이트

Art direction for any content — reads text, PDF, Word, HTML, PPT, then proposes 2-3 creative directions with photography style, mood, and visual language. After selection, generates AI image prompts and visual briefs section-by-section. Use when the user shares content and needs visual direction, image sourcing, or creative direction for any material.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

divinevideo/divine-mobile2662026년 10월 20일 업데이트

Fix "Null check operator used on a null value" errors when an object is set to null during an async await. Use when: (1) Object reference is nullified while awaiting, (2) Code accesses object with ! after await returns, (3) Cancel/dispose operations run concurrently with async operations on same object. Solution: capture local reference before await.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

divinevideo/divine-mobile2662026년 10월 20일 업데이트

Add custom metadata headers (x-amz-meta-*) to AWS v4 signed requests for GCS S3-compatible API. Use when: (1) Adding custom metadata to GCS uploads via S3 API, (2) Getting signature mismatch errors after adding new headers, (3) x-amz-meta-* headers being ignored or causing 403 errors. Custom headers MUST be included in canonical headers and signed headers list.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

divinevideo/divine-mobile2662026년 10월 20일 업데이트

Fix password/secret authentication failures caused by trailing newlines when creating Google Cloud secrets (or similar) with bash here-strings. Use when: (1) Password authentication fails with correct password, (2) Secret created with `<<< "value"` syntax, (3) Error like "password authentication failed" or "invalid token" despite correct value. Bash here-strings (`<<<`) add a trailing newline that corrupts secrets.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

divinevideo/divine-mobile2662026년 10월 20일 업데이트

Fix silent video/media processing failures caused by URL extraction code that filters on file extensions (.mp4, .webm, .webp). Use when: (1) Media moderation, transcoding, or analysis silently skips files from Blossom or content-addressed storage servers, (2) URL extraction from Nostr event tags (imeta, r tags) drops URLs without recognized extensions, (3) CDN fallback URLs append .mp4 but the actual server uses extensionless content-addressed paths like /{sha256}. Common in Nostr video events (kind 34236) where different clients use different URL formats.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

divinevideo/divine-mobile2662026년 10월 20일 업데이트

divinevideo의 스킬 모두 보기

이 스킬의 문제 신고