in zetasql-toolkit-core/src/main/java/com/google/zetasql/toolkit/tools/lineage/ParentColumnFinder.java [275:305]
public void visit(ResolvedWithRefScan withRefScan) {
// WithRefScans create new ResolvedColumns for each column in the WITH entry instead
// of referencing the WITH entry directly.
// Here we find the corresponding with entry which is in scope and register the original
// WITH entry columns a parents of their corresponding columns in the WithRefScan.
Optional<ResolvedWithEntry> maybeWithEntry =
findInScopeWithEntryByName(withRefScan.getWithQueryName());
if (!maybeWithEntry.isPresent()) {
// Should never happen, since the query would be invalid.
return;
}
ResolvedWithEntry withEntry = maybeWithEntry.get();
// Columns in the WITH entry and the WithRefScan map 1:1.
// Register each column in the WITH entry as a parent of its corresponding column in this
// WithRefScan
// If a column is a STRUCT, also register the 1:1 parent relationship between fields
for (int i = 0; i < withRefScan.getColumnList().size(); i++) {
ResolvedColumn withRefScanColumn = withRefScan.getColumnList().get(i);
ResolvedColumn matchingWithEntryColumn = withEntry.getWithSubquery().getColumnList().get(i);
List<ResolvedColumn> expandedRefScanColumn = expandColumn(withRefScanColumn);
List<ResolvedColumn> expandedMatchingWithEntryColumn = expandColumn(matchingWithEntryColumn);
for (int j = 0; j < expandedRefScanColumn.size(); j++) {
addParentToColumn(expandedRefScanColumn.get(j), expandedMatchingWithEntryColumn.get(j));
}
}
}