Skip to content

Enhance TableSourceCollector to include query and referenced columns #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/transformers/TableSourceCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TypeValue
} from "../models/ValueComponent";
import { CTECollector } from "./CTECollector";
import { SelectableColumnCollector } from "./SelectableColumnCollector";

/**
* A visitor that collects all table source names from a SQL query structure.
Expand Down Expand Up @@ -287,6 +288,15 @@ export class TableSourceCollector implements SqlComponentVisitor<void> {
// Check if this is a table managed by a CTE
if (!this.tableNameMap.has(identifier) && !this.isCTETable(source.table.name)) {
this.tableNameMap.set(identifier, true);

// Collect referenced columns using SelectableColumnCollector
const columnCollector = new SelectableColumnCollector();
const referencedColumns = columnCollector.collect(source);

// Add the query and referenced columns to the table source
source.query = source;
source.referencedColumns = referencedColumns.map(col => col.name);

this.tableSources.push(source);
}
}
Expand Down Expand Up @@ -458,4 +468,4 @@ export class TableSourceCollector implements SqlComponentVisitor<void> {
expr.input.accept(this);
expr.castType.accept(this);
}
}
}
50 changes: 49 additions & 1 deletion tests/transformers/TableSourceCollector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,52 @@ order by
// Assert
expect(tableNames.length).toBe(0);
});
});

test('includes the query the table belongs to in the return value', () => {
// Arrange
const sql = `SELECT * FROM users`;
const query = SelectQueryParser.parse(sql);
const collector = new TableSourceCollector();

// Act
collector.visit(query);
const tableSources = collector.getTableSources();

// Assert
expect(tableSources.length).toBe(1);
expect(tableSources[0].table.name).toBe('users');
expect(tableSources[0].query).toBeDefined();
});

test('includes the list of column names referenced from the table in the return value', () => {
// Arrange
const sql = `SELECT id, name FROM users`;
const query = SelectQueryParser.parse(sql);
const collector = new TableSourceCollector();

// Act
collector.visit(query);
const tableSources = collector.getTableSources();

// Assert
expect(tableSources.length).toBe(1);
expect(tableSources[0].table.name).toBe('users');
expect(tableSources[0].referencedColumns).toEqual(['id', 'name']);
});

test('handles wildcard columns correctly', () => {
// Arrange
const sql = `SELECT * FROM users`;
const query = SelectQueryParser.parse(sql);
const collector = new TableSourceCollector();

// Act
collector.visit(query);
const tableSources = collector.getTableSources();

// Assert
expect(tableSources.length).toBe(1);
expect(tableSources[0].table.name).toBe('users');
expect(tableSources[0].referencedColumns).toEqual([]);
});
});