Skip to content

Commit 12ac145

Browse files
KSDaemonigorlukanin
authored andcommitted
perf(schema-compiler): Support native YAML transpilation (#9948)
* Prepare transpileYaml in native + schema compiler * fix test fix/sync error message * fix test
1 parent 4ec4117 commit 12ac145

File tree

7 files changed

+41
-14
lines changed

7 files changed

+41
-14
lines changed

packages/cubejs-backend-native/js/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,16 @@ export const transpileJs = async (transpileRequests: TransformConfig[]): Promise
517517
throw new Error('TranspileJs native implementation not found!');
518518
};
519519

520+
export const transpileYaml = async (transpileRequests: TransformConfig[]): Promise<TransformResponse[]> => {
521+
const native = loadNative();
522+
523+
if (native.transpileYaml) {
524+
return native.transpileYaml(transpileRequests);
525+
}
526+
527+
throw new Error('TranspileYaml native implementation not found!');
528+
};
529+
520530
export interface PyConfiguration {
521531
repositoryFactory?: (ctx: unknown) => Promise<unknown>,
522532
logger?: (msg: string, params: Record<string, any>) => void,

packages/cubejs-schema-compiler/src/compiler/DataSchemaCompiler.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import workerpool from 'workerpool';
1313
import { LRUCache } from 'lru-cache';
1414

1515
import { FileContent, getEnv, isNativeSupported, SchemaFileRepository } from '@cubejs-backend/shared';
16-
import { NativeInstance, PythonCtx, transpileJs } from '@cubejs-backend/native';
16+
import { NativeInstance, PythonCtx, transpileJs, transpileYaml } from '@cubejs-backend/native';
1717
import { UserError } from './UserError';
1818
import { ErrorReporter, ErrorReporterOptions, SyntaxErrorInterface } from './ErrorReporter';
1919
import { CONTEXT_SYMBOLS, CubeDefinition, CubeSymbols } from './CubeSymbols';
@@ -707,7 +707,7 @@ export class DataSchemaCompiler {
707707
private async transpileYamlFile(
708708
file: FileContent,
709709
errorsReport: ErrorReporter,
710-
{ cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage }: TranspileOptions
710+
{ cubeNames, cubeSymbols, compilerId }: TranspileOptions
711711
): Promise<(FileContent | undefined)> {
712712
const cacheKey = crypto.createHash('md5').update(JSON.stringify(file.content)).digest('hex');
713713

@@ -717,9 +717,24 @@ export class DataSchemaCompiler {
717717
return { ...file, content };
718718
}
719719

720-
/* if (getEnv('transpilationNative')) {
720+
if (getEnv('transpilationNative')) {
721+
const reqData = {
722+
fileName: file.fileName,
723+
fileContent: file.content,
724+
transpilers: [],
725+
compilerId: compilerId || '',
726+
};
727+
728+
errorsReport.inFile(file);
729+
const res = await transpileYaml([reqData]);
730+
errorsReport.addErrors(res[0].errors);
731+
errorsReport.addWarnings(res[0].warnings as unknown as SyntaxErrorInterface[]);
732+
errorsReport.exitFile();
733+
734+
this.compiledYamlCache.set(cacheKey, res[0].code);
721735

722-
} else */ if (getEnv('transpilationWorkerThreads')) {
736+
return { ...file, content: res[0].code };
737+
} else if (getEnv('transpilationWorkerThreads')) {
723738
const data = {
724739
fileName: file.fileName,
725740
content: file.content,

packages/cubejs-schema-compiler/src/compiler/YamlCompiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export class YamlCompiler {
277277
const pythonParser = new PythonParser(codeString);
278278
return pythonParser.transpileToJs();
279279
} catch (e: any) {
280-
errorsReport.error(`Can't parse python expression. Most likely this type of syntax isn't supported yet: ${e.message || e}`);
280+
errorsReport.error(`Failed to parse Python expression. Most likely this type of syntax isn't supported yet: ${e.message || e}`);
281281
}
282282

283283
return t.nullLiteral();

packages/cubejs-schema-compiler/test/unit/fixtures/orders_big.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ cubes:
6262
allowAll: true
6363
- role: admin
6464
conditions:
65-
- if: "{ !security_context.isBlocked }"
65+
- if: "{ security_context.isNotBlocked }"
6666
rowLevel:
6767
filters:
6868
- member: status

packages/cubejs-schema-compiler/test/unit/fixtures/orders_incorrect_acl.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ cubes:
6262
allowAll: true
6363
- role: admin
6464
conditions:
65-
- if: "{ !security_context.isBlocked }"
65+
- if: "{ security_context.isNotBlocked }"
6666
rowLevel:
6767
filters:
6868
- member: "{CUBE}.order_users.name"

packages/cubejs-schema-compiler/test/unit/fixtures/orders_nonexist_acl.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ cubes:
6262
allowAll: true
6363
- role: admin
6464
conditions:
65-
- if: "{ !security_context.isBlocked }"
65+
- if: "{ security_context.isNotBlocked }"
6666
rowLevel:
6767
filters:
6868
- member: "{CUBE}.other.path.created_at"

packages/cubejs-schema-compiler/test/unit/yaml-schema.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe('Yaml Schema Testing', () => {
119119
const { compiler } = prepareYamlCompiler(
120120
`cubes:
121121
- name: Products
122-
sql: select { "string"+123 } from tbl
122+
sql: select { "string"+123 } as a1, { 123abc } as a2 from tbl
123123
dimensions:
124124
`
125125
);
@@ -129,7 +129,7 @@ describe('Yaml Schema Testing', () => {
129129

130130
throw new Error('compile must return an error');
131131
} catch (e: any) {
132-
expect(e.message).toContain('Can\'t parse python expression');
132+
expect(e.message).toContain('Failed to parse Python expression');
133133
}
134134
});
135135

@@ -150,7 +150,7 @@ describe('Yaml Schema Testing', () => {
150150

151151
throw new Error('compile must return an error');
152152
} catch (e: any) {
153-
expect(e.message).toContain('name isn\'t defined for dimension: ');
153+
expect(e.message).toContain('name isn\'t defined for dimension');
154154
}
155155
});
156156

@@ -223,7 +223,9 @@ describe('Yaml Schema Testing', () => {
223223

224224
throw new Error('compile must return an error');
225225
} catch (e: any) {
226-
expect(e.message).toContain('Users cube: "title" must be a string');
226+
expect(e.message).toMatch(
227+
/Users cube: "title" (must be a string|is not allowed to be empty)/
228+
);
227229
}
228230
});
229231

@@ -479,7 +481,7 @@ describe('Yaml Schema Testing', () => {
479481
await compiler.compile();
480482
throw new Error('compile must return an error');
481483
} catch (e: any) {
482-
expect(e.message).toContain('dimension.granularitys must be defined as array');
484+
expect(e.message).toContain('must be defined as array');
483485
}
484486
});
485487

@@ -544,7 +546,7 @@ describe('Yaml Schema Testing', () => {
544546
accessPolicy:
545547
- role: admin
546548
conditions:
547-
- if: "{ !security_context.isBlocked }"
549+
- if: "{ security_context.isNotBlocked }"
548550
rowLevel:
549551
filters:
550552
- member: status

0 commit comments

Comments
 (0)