sqlglot icon indicating copy to clipboard operation
sqlglot copied to clipboard

lineage - optionally add functions as nodes

Open wernerdaehn opened this issue 2 months ago • 6 comments

Example: concat(col1, ' ', col2)

Lineage can serve multiple use cases. If you just need to know the source table/columns being used, the current works perfectly. But what is the business case for that? To answer questions like where is a table/column being used. Fine.

But another use case could be to derive the data type of the output column, derive the resulting primary key or create documentation from the source table comments.

Example: Source is the table called CUSTOMERwith the columns

  • CUSTOMER_ID int comment "The unique customer number"
  • FIRST_NAME nvarchar(30) comment "First name of a natural person"
  • NAME nvarchar(40) comment "Company name or last name of a natural person"

The select statement is

select cast(CUSTOMER_ID as varchar(10)) as customer_number , trim(concat(FIRST_NAME, ' ', NAME)) from CUSTOMER;

For 1:1 mappings the output data type and comment can be taken from the node.downstream[0]. With that I know what the table/column name of the source is, query the data dictionary and assign the same data type and column comment to this field also. Given that the source table's primary key is left intact by the cast() function, I know the primary key of the result set. The column comment can be retained. The mapping formula can include the function. The data type can be derived.

But Functions show up in the node.expression, but they no longer have an association with the node.downstream. If there is a single function like in above example, this can be done manually. But an expression concat(case when (trim(... is not so easy any longer.

Why not add an parameter to the lineage method functions_as_nodes=True and then downstream contains the tree of functions also?

Then I can calculate that a concat of nvarchar(30) + Literal(1) + nvarchar(40) = nvarchar(71). And the column comment shall be "trim(concatenation of (FIRST_NAME (First name of a natural person), NAME(Company name or last name of a natural person)))".

Makes sense?

wernerdaehn avatar Sep 16 '26 11:09 wernerdaehn

I’ve looked into this and I understand the issue. I think the cleanest approach is to make function nodes opt-in via a functions_as_nodes=True parameter, while keeping the current lineage tree unchanged by default. The function nodes would sit between the output column and their source expressions, so nested expressions such as TRIM(CONCAT(FIRST_NAME, ' ', NAME)) can be represented as a tree rather than only appearing in node.expression. Before I implement it, I’d like to confirm a few details with you: should the child nodes preserve the SQL argument order, should all exp.Func expressions be treated as function nodes (including things like AND/OR), and should functions with no source columns still produce nodes? If that matches what you have in mind, I’ll implement it and raise a PR.

Kunal8954 avatar Sep 16 '26 15:09 Kunal8954

I'm not sure we want to support this. Need to think about this a bit.

georgesittas avatar Sep 16 '26 15:09 georgesittas

  • should the child nodes preserve the SQL argument order? You mean trim(substring(COL)) vs substring(trim(COL))? I'd say yes, the order must be preserved just like in the expressions.
  • should all exp.Func expressions be treated as function nodes (including things like AND/OR)? That will be a deep tree but again, just like the expression tree is as well.
  • should functions with no source columns still produce nodes? Yes. Let's take the simple example of current_time(). If the business case is to derive the data type for this column, to calculate the mapping formula, then it would make sense. So yes, please.

I wonder what others would decide. My thoughts are, if we keep it similar to the expression tree, it should make the implementation easier. If we say yes to all your questions, the tree gets deeper but users can decide what to extract - they walk the tree and stop at places or skip levels. Hence making that configurable would be not worth it. First to complicated and second, no matter how we group things together, e.g. add-functions-put-no-conditions, some people will need a different kind of grouping. Hence full tree. In the expressions you have everything also.

wernerdaehn avatar Sep 16 '26 15:09 wernerdaehn

We can look at it from two directions:

parse_one() returns an expression tree in the format of the SQL statement select->projection->expression->subselect. It is a horizontal layered view of the full SQL.

lineage() returns an expression tree per column through the full depth colA -> substring -> cte column -> table column. It is a vertical view of a SQL statement.

The current lineage is above tree minus nodes you declared as not necessary, because it serves a single use case only. That would be my argument of why it makes sense.

But let's look at what I am doing actually. I am in MS Fabric. The source is a Lakehouse table (Delta table, Parquet files), the target is a Warehouse and the transformation happens via SQL statements.

with sales_order as (select a.order id, b.line_item, b.material, a.sold_to from order_header a join order_item b on (a.order_id = b.order_id)
select order_id as order_number, to_int(line_item) as line_number, material, case when sold_to is null then ship_to else sold_to end as buyer;

What is the primary key of the target table? You don't know because in the Lakehouse the concept of a pk does not exist. What is the column comment for the columns? If I know the full lineage, I can derive them automatically. What is the data type of each column? If I have the full lineage, I can derive it, e.g. line_number with the to_int() is an integer for sure.

So what I do/need is the full vertical breakdown of where the column comes from. Then I can go into a schema registry to find order_header and order_item table details and push the information through all nodes while walking the tree for each.

As of today, all I can resolve are 1:1 mappings plus set operations like union all (because these are exposed as Nodes). With the full tree I can resolve everything.

wernerdaehn avatar Sep 16 '26 15:09 wernerdaehn

Thanks, that clarifies it. I’ll keep the design aligned with the full expression tree and preserve argument order, including function nodes without source columns. I’ll hold off on implementation for now and wait for the discussion to settle.

Kunal8954 avatar Sep 16 '26 16:09 Kunal8954

But another use case could be to derive the data type of the output column, derive the resulting primary key or create documentation from the source table comments.

The data type of the output column can already be derived via annotate_types. The rest are are outside lineage's scope and should be implemented at the application site.

georgesittas avatar Sep 17 '26 09:09 georgesittas