Skip to content
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

[2/n] Update selection syntax to use DBT style traversal syntax (javascript) #27016

Merged
merged 5 commits into from
Jan 13, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AndExpressionContext,
AttributeExpressionContext,
CodeLocationAttributeExprContext,
DownTraversalContext,
DownTraversalExpressionContext,
FunctionCallExpressionContext,
FunctionNameContext,
Expand All @@ -19,8 +20,8 @@ import {
StartContext,
TagAttributeExprContext,
TraversalAllowedExpressionContext,
TraversalContext,
UpAndDownTraversalExpressionContext,
UpTraversalContext,
UpTraversalExpressionContext,
ValueContext,
} from './generated/AssetSelectionParser';
Expand All @@ -29,14 +30,12 @@ import {GraphTraverser} from '../app/GraphQueryImpl';
import {AssetGraphQueryItem} from '../asset-graph/useAssetGraphData';
import {buildRepoPathForHuman} from '../workspace/buildRepoAddress';

export function getTraversalDepth(ctx: TraversalContext): number {
if (ctx.STAR()) {
return Number.MAX_SAFE_INTEGER;
export function getTraversalDepth(ctx: UpTraversalContext | DownTraversalContext): number {
const digits = ctx.DIGITS();
if (digits) {
return parseInt(ctx.text);
}
if (ctx.PLUS()) {
return ctx.PLUS().length;
}
throw new Error('Invalid traversal');
return Number.MAX_SAFE_INTEGER;
}

export function getFunctionName(ctx: FunctionNameContext): string {
Expand Down Expand Up @@ -88,8 +87,8 @@ export class AntlrAssetSelectionVisitor

visitUpAndDownTraversalExpression(ctx: UpAndDownTraversalExpressionContext) {
const selection = this.visit(ctx.traversalAllowedExpr());
const up_depth: number = getTraversalDepth(ctx.traversal(0));
const down_depth: number = getTraversalDepth(ctx.traversal(1));
const up_depth: number = getTraversalDepth(ctx.upTraversal());
const down_depth: number = getTraversalDepth(ctx.downTraversal());
const selection_copy = new Set(selection);
for (const item of selection_copy) {
this.traverser.fetchUpstream(item, up_depth).forEach((i) => selection.add(i));
Expand All @@ -100,7 +99,7 @@ export class AntlrAssetSelectionVisitor

visitUpTraversalExpression(ctx: UpTraversalExpressionContext) {
const selection = this.visit(ctx.traversalAllowedExpr());
const traversal_depth: number = getTraversalDepth(ctx.traversal());
const traversal_depth: number = getTraversalDepth(ctx.upTraversal());
const selection_copy = new Set(selection);
for (const item of selection_copy) {
this.traverser.fetchUpstream(item, traversal_depth).forEach((i) => selection.add(i));
Expand All @@ -110,7 +109,7 @@ export class AntlrAssetSelectionVisitor

visitDownTraversalExpression(ctx: DownTraversalExpressionContext) {
const selection = this.visit(ctx.traversalAllowedExpr());
const traversal_depth: number = getTraversalDepth(ctx.traversal());
const traversal_depth: number = getTraversalDepth(ctx.downTraversal());
const selection_copy = new Set(selection);
for (const item of selection_copy) {
this.traverser.fetchDownstream(item, traversal_depth).forEach((i) => selection.add(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ function assertQueryResult(query: string, expectedNames: string[]) {
if (result instanceof Error) {
throw result;
}
expect(result.all.length).toBe(expectedNames.length);
expect(new Set(result.all.map((asset) => asset.name))).toEqual(new Set(expectedNames));
expect(result.all.length).toBe(expectedNames.length);
}

describe('parseAssetSelectionQuery', () => {
Expand Down Expand Up @@ -103,36 +103,36 @@ describe('parseAssetSelectionQuery', () => {
});

it('should parse upstream plus query', () => {
assertQueryResult('+key:A', ['A']);
assertQueryResult('+key:B', ['A', 'B']);
assertQueryResult('+key:C', ['B', 'B2', 'C']);
assertQueryResult('++key:C', ['A', 'B', 'B2', 'C']);
assertQueryResult('1+key:A', ['A']);
assertQueryResult('1+key:B', ['A', 'B']);
assertQueryResult('1+key:C', ['B', 'B2', 'C']);
assertQueryResult('2+key:C', ['A', 'B', 'B2', 'C']);
});

it('should parse downstream plus query', () => {
assertQueryResult('key:A+', ['A', 'B', 'B2']);
assertQueryResult('key:A++', ['A', 'B', 'B2', 'C']);
assertQueryResult('key:C+', ['C']);
assertQueryResult('key:B+', ['B', 'C']);
assertQueryResult('key:A+1', ['A', 'B', 'B2']);
assertQueryResult('key:A+2', ['A', 'B', 'B2', 'C']);
assertQueryResult('key:C+1', ['C']);
assertQueryResult('key:B+1', ['B', 'C']);
});

it('should parse upstream star query', () => {
assertQueryResult('*key:A', ['A']);
assertQueryResult('*key:B', ['A', 'B']);
assertQueryResult('*key:C', ['A', 'B', 'B2', 'C']);
assertQueryResult('+key:A', ['A']);
assertQueryResult('+key:B', ['A', 'B']);
assertQueryResult('+key:C', ['A', 'B', 'B2', 'C']);
});

it('should parse downstream star query', () => {
assertQueryResult('key:A*', ['A', 'B', 'B2', 'C']);
assertQueryResult('key:B*', ['B', 'C']);
assertQueryResult('key:C*', ['C']);
assertQueryResult('key:A+', ['A', 'B', 'B2', 'C']);
assertQueryResult('key:B+', ['B', 'C']);
assertQueryResult('key:C+', ['C']);
});

it('should parse up and down traversal queries', () => {
assertQueryResult('key:A* and *key:C', ['A', 'B', 'B2', 'C']);
assertQueryResult('*key:B*', ['A', 'B', 'C']);
assertQueryResult('key:A* and *key:C and *key:B*', ['A', 'B', 'C']);
assertQueryResult('key:A* and *key:B* and *key:C', ['A', 'B', 'C']);
assertQueryResult('key:A+ and +key:C', ['A', 'B', 'B2', 'C']);
assertQueryResult('+key:B+', ['A', 'B', 'C']);
assertQueryResult('key:A+ and +key:C and +key:B+', ['A', 'B', 'C']);
assertQueryResult('key:A+ and +key:B+ and +key:C', ['A', 'B', 'C']);
});

it('should parse sinks query', () => {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading