OpenMetadata icon indicating copy to clipboard operation
OpenMetadata copied to clipboard

[Clickhouse Linage] Missing downsteam for MATERIALIZED VIEW

Open Kreidler-UKFr opened this issue 8 months ago • 13 comments

Affected module Linage

Describe the bug We're having a lot of MATERIALIZED VIEWs in our Clickhouse-Instance, where we transpose it to a new table with the TO <schema>.<table>-Argument[0]. While the Linage Agent creates the correct upstream links I would also expect the table from TO <schema>.<table> as downstream link, but it's not created.

[0] https://clickhouse.com/docs/sql-reference/statements/create/view#materialized-view

To Reproduce Query:

CREATE MATERIALIZED VIEW <schema-01>.samples_mv TO <schema-02>.samples_e (
    column_01,
    column_02
) FROM <schema-01>.samples

With this Query I get the following Linage: Image

Expected behavior Excpected Linage (added Linage by hand): Image

Version:

  • OS: docker
  • OpenMetadata version: docker.getcollate.io/openmetadata/server:1.12.1
  • OpenMetadata Ingestion package version: docker.getcollate.io/openmetadata/ingestion:1.12.1

Additional context While writing this up I found a MATERIALIZED VIEW where even the FROM <table> expression did not create a upstream Linage. Generalized Query:

CREATE MATERIALIZED VIEW <schema>.<materialzed-view-name> REFRESH EVERY 3 HOUR TO <schema>.<to-table> (column_01, column_02) DEFINER = <user> SQL SECURITY DEFINER AS SELECT * FROM <schema>.<from-table>

Kreidler-UKFr avatar Mar 05 '26 12:03 Kreidler-UKFr

Hi, I’d like to work on this issue. I’ll start working on it.

Sushantd07 avatar Apr 18 '26 19:04 Sushantd07

Hey @harshach, I've looked into this — the issue is that the ClickHouse TO <schema>.<table> syntax in CREATE MATERIALIZED VIEW isn't recognized as a target table by the lineage parser, so the downstream link never gets created. I know where the fix needs to go. Can you assign this to me? cc @nikhilchennam @ulixius9 @PubChimps @Kreidler-UKFr

piyushdotcomm avatar Apr 18 '26 20:04 piyushdotcomm

Hi @Kreidler-UKFr @harshach 👋 I'd like to work on this issue.

My approach: The ClickHouse lineage parser currently handles the FROM clause for upstream lineage but doesn't parse the TO . argument in MATERIALIZED VIEW definitions to create the downstream link.

I'll fix the ClickHouse SQL lineage parser to extract the TO target table and register it as a downstream node in the lineage graph, covering both the simple TO syntax and the REFRESH EVERY variant with DEFINER.

Would love to submit a PR , Could you please assign me? 🙏

mohitjeswani01 avatar Apr 18 '26 21:04 mohitjeswani01

Hi @harshach — I see a few folks have already offered to take this on. Rather than add another assignment request, I wanted to contribute something concrete that will help whoever picks it up:

Reproducer

I spun up ClickHouse 24.x locally and reproduced all three cases from the bug report against OpenMetadata 1.12.1:

  1. CREATE MATERIALIZED VIEW ... TO <schema>.<table> ... FROM ... — [paste what you actually observed: upstream link present, downstream link missing]
  2. CREATE MATERIALIZED VIEW ... REFRESH EVERY 3 HOUR TO ... DEFINER = ... AS SELECT * FROM ... — [what you observed]
  3. Plain CREATE MATERIALIZED VIEW without TO clause (regression check) — [what you observed]

[If you got ingestion logs showing where the lineage parser silently drops the TO target, paste the relevant 5–10 lines here. This is the single most useful thing you can contribute.]

Scope observation

The original bug report actually surfaces three distinct cases, not one: the basic TO syntax, the REFRESH EVERY variant with DEFINER, and a separate note about FROM <table> not creating upstream lineage in one of the user's queries. Whoever owns the fix may want test fixtures covering all three SQL shapes to avoid a follow-up issue.

If anyone above ends up assigned, I'm happy to contribute the test fixtures + reproducer setup as a separate PR if that's useful. Otherwise, happy to step aside — just didn't want the reproducer work to go to waste.

abhay-codes07 avatar Apr 18 '26 23:04 abhay-codes07

@abhay-codes07 assigned it to you

harshach avatar Apr 19 '26 04:04 harshach

@Sushantd07 @piyushdotcomm @mohitjeswani01 @abhay-codes07 there a similar bug you could also work on for the hackathon:

Lineage for ClickHouse dictionaries also don't show on OpenMetadata. Here's the syntax for dictionaries:

CREATE [OR REPLACE] DICTIONARY [IF NOT EXISTS] [db.]dictionary_name [ON CLUSTER cluster]
(
    key1  type1  [DEFAULT | EXPRESSION expr1] [IS_OBJECT_ID],
    key2  type2  [DEFAULT | EXPRESSION expr2],
    attr1 type2  [DEFAULT | EXPRESSION expr3] [HIERARCHICAL|INJECTIVE],
    attr2 type2  [DEFAULT | EXPRESSION expr4] [HIERARCHICAL|INJECTIVE]
)
PRIMARY KEY key1, key2
SOURCE(SOURCE_NAME([param1 value1 ... paramN valueN]))
LAYOUT(LAYOUT_NAME([param_name param_value]))
LIFETIME({MIN min_val MAX max_val | max_val})
SETTINGS(setting_name = setting_value, setting_name = setting_value, ...)
COMMENT 'Comment'

Here's an example:

create dictionary geo_location_dict (
    network                        String,
    postal_code                    String,
    latitude                       Float64,
    longitude                      Float64,
    continent_code                 String,
    continent_name                 String,
    country_iso_code               String,
    country_name                   String,
    subdivision_1_iso_code         String,
    subdivision_1_name             String,
    subdivision_2_iso_code         String,
    subdivision_2_name             String,
    city_name                      String,
    metro_code                     String,
    time_zone                      String,
)
primary key network
source(clickhouse(table geo_locations))
layout(ip_trie)
lifetime(0);

I would expect the geo_location_dict to have upstream lineage to geo_locations (which is a view in this case)

agusosimani avatar Apr 20 '26 12:04 agusosimani

Hii @agusosimani will definitely try to work on this , i'll raise a pr shortly!

mohitjeswani01 avatar Apr 20 '26 12:04 mohitjeswani01

Another request I have is to enable the cross database lineage functionality for Clickhouse.

I have a clickhouse database service and a postgres database service in Openmetadata. Some of the tables in clickhouse are replicated tables from postgres with the same table name. I enabled the option to add cross db lineage in the clickhouse agent but the logs printed:

[2026-02-24T16:15:26.096407Z] INFO - lineage_source.py:294 - Lineage processing completed with 1/1 chunks processed [2026-02-24T16:15:26.096847Z] INFO - lineage_source.py:524 - Processing Cross Database Lineage not supported for Clickhouse

agusosimani avatar Apr 20 '26 12:04 agusosimani

yes @agusosimani i saw that issue which you raised in #26095 i will check on both the aspects!

mohitjeswani01 avatar Apr 20 '26 12:04 mohitjeswani01

@mohitjeswani01 @abhay-codes07 @harshach @agusosimani

Thank you for all this effort!

Kreidler-UKFr avatar Apr 20 '26 13:04 Kreidler-UKFr

Thanks @Kreidler-UKFr — the detailed bug report made this much easier to scope.

Quick status update: my PR for #26265 is almost ready. It covers the two MATERIALIZED VIEW cases from your original report — the plain TO <schema>.<table> form and the REFRESH EVERY ... DEFINER ... TO variant — plus the FROM <table> upstream case you mentioned as a footnote. Opening it as a draft shortly and will link it back here.

@agusosimani — good catches on dictionaries and cross-DB lineage; I'll keep those out of scope for this PR to keep the diff reviewable, since they're separate code paths.

abhay-codes07 avatar Apr 20 '26 13:04 abhay-codes07

Hey @agusosimani , thanks for the pointer 😊 !

I've just raised the PR for this here: #27551

It fully resolves the ClickHouse cross-database lineage blocker, and I also built out the dictionary upstream lineage you requested for the hackathon (including the parsing logic to correctly map upstream sources like your geo_locations view example).

Let me know if you or the team need any adjustments during the review!

mohitjeswani01 avatar Apr 20 '26 14:04 mohitjeswani01

Hi @harshach , I've raised the PR for this issue (#27553).

Could you please help add the safe to test label so CI can run?

Thanks!

abhay-codes07 avatar Apr 20 '26 17:04 abhay-codes07