feat(spark): support materialized view for spark sql (#262)
* feat(spark): support materialized view for spark sql * fix(spark): code review update * fix(spark): update spark materilized view and zorder grammar * test(spark): add syntaxSuggestion test of materialized view --------- Co-authored-by: jialan <jialan@dtstack.com>
This commit is contained in:
		@ -46,4 +46,22 @@ SELECT id, n FROM tbl GROUP BY  ;
 | 
			
		||||
 | 
			
		||||
SELECT id, n FROM tbl ORDER BY name, i ;
 | 
			
		||||
 | 
			
		||||
SELECT id FROM tb1 GROUP BY ROLLUP(  );
 | 
			
		||||
SELECT id FROM tb1 GROUP BY ROLLUP(  );
 | 
			
		||||
 | 
			
		||||
CREATE MATERIALIZED VIEW db.mv;
 | 
			
		||||
 | 
			
		||||
DROP MATERIALIZED VIEW db.mv;
 | 
			
		||||
 | 
			
		||||
ALTER MATERIALIZED VIEW db.mv;
 | 
			
		||||
 | 
			
		||||
REFRESH MATERIALIZED VIEW db.mv;
 | 
			
		||||
 | 
			
		||||
SHOW CREATE MATERIALIZED VIEW db.mv;
 | 
			
		||||
 | 
			
		||||
SHOW MATERIALIZED VIEWS from db;
 | 
			
		||||
 | 
			
		||||
OPTIMIZE db.tb;
 | 
			
		||||
 | 
			
		||||
OPTIMIZE db.tb ZORDER BY  ;
 | 
			
		||||
 | 
			
		||||
OPTIMIZE db.tb ZORDER BY name, i;
 | 
			
		||||
@ -442,4 +442,157 @@ describe('Spark SQL Syntax Suggestion', () => {
 | 
			
		||||
        expect(suggestion).not.toBeUndefined();
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('Create materialized view', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 51,
 | 
			
		||||
            column: 31,
 | 
			
		||||
        };
 | 
			
		||||
        const syntaxes = parser.getSuggestionAtCaretPosition(
 | 
			
		||||
            commentOtherLine(syntaxSql, pos.lineNumber),
 | 
			
		||||
            pos
 | 
			
		||||
        )?.syntax;
 | 
			
		||||
        const suggestion = syntaxes?.find(
 | 
			
		||||
            (syn) => syn.syntaxContextType === SyntaxContextType.VIEW_CREATE
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        expect(suggestion).not.toBeUndefined();
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'mv']);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('Drop materialized view', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 53,
 | 
			
		||||
            column: 29,
 | 
			
		||||
        };
 | 
			
		||||
        const syntaxes = parser.getSuggestionAtCaretPosition(
 | 
			
		||||
            commentOtherLine(syntaxSql, pos.lineNumber),
 | 
			
		||||
            pos
 | 
			
		||||
        )?.syntax;
 | 
			
		||||
        const suggestion = syntaxes?.find(
 | 
			
		||||
            (syn) => syn.syntaxContextType === SyntaxContextType.VIEW
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        expect(suggestion).not.toBeUndefined();
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'mv']);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('Alter materialized view', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 55,
 | 
			
		||||
            column: 30,
 | 
			
		||||
        };
 | 
			
		||||
        const syntaxes = parser.getSuggestionAtCaretPosition(
 | 
			
		||||
            commentOtherLine(syntaxSql, pos.lineNumber),
 | 
			
		||||
            pos
 | 
			
		||||
        )?.syntax;
 | 
			
		||||
        const suggestion = syntaxes?.find(
 | 
			
		||||
            (syn) => syn.syntaxContextType === SyntaxContextType.VIEW
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        expect(suggestion).not.toBeUndefined();
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'mv']);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('Refresh materialized view', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 57,
 | 
			
		||||
            column: 32,
 | 
			
		||||
        };
 | 
			
		||||
        const syntaxes = parser.getSuggestionAtCaretPosition(
 | 
			
		||||
            commentOtherLine(syntaxSql, pos.lineNumber),
 | 
			
		||||
            pos
 | 
			
		||||
        )?.syntax;
 | 
			
		||||
        const suggestion = syntaxes?.find(
 | 
			
		||||
            (syn) => syn.syntaxContextType === SyntaxContextType.VIEW
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        expect(suggestion).not.toBeUndefined();
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'mv']);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('Show create materialized view', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 59,
 | 
			
		||||
            column: 36,
 | 
			
		||||
        };
 | 
			
		||||
        const syntaxes = parser.getSuggestionAtCaretPosition(
 | 
			
		||||
            commentOtherLine(syntaxSql, pos.lineNumber),
 | 
			
		||||
            pos
 | 
			
		||||
        )?.syntax;
 | 
			
		||||
        const suggestion = syntaxes?.find(
 | 
			
		||||
            (syn) => syn.syntaxContextType === SyntaxContextType.VIEW
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        expect(suggestion).not.toBeUndefined();
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'mv']);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('Show all materialized from database', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 61,
 | 
			
		||||
            column: 32,
 | 
			
		||||
        };
 | 
			
		||||
        const syntaxes = parser.getSuggestionAtCaretPosition(
 | 
			
		||||
            commentOtherLine(syntaxSql, pos.lineNumber),
 | 
			
		||||
            pos
 | 
			
		||||
        )?.syntax;
 | 
			
		||||
        const suggestion = syntaxes?.find(
 | 
			
		||||
            (syn) => syn.syntaxContextType === SyntaxContextType.DATABASE
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        expect(suggestion).not.toBeUndefined();
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db']);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('Optimize table', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 63,
 | 
			
		||||
            column: 15,
 | 
			
		||||
        };
 | 
			
		||||
        const syntaxes = parser.getSuggestionAtCaretPosition(
 | 
			
		||||
            commentOtherLine(syntaxSql, pos.lineNumber),
 | 
			
		||||
            pos
 | 
			
		||||
        )?.syntax;
 | 
			
		||||
        const suggestion = syntaxes?.find(
 | 
			
		||||
            (syn) => syn.syntaxContextType === SyntaxContextType.TABLE
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        expect(suggestion).not.toBeUndefined();
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'tb']);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('Optimize table zorder by empty', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 65,
 | 
			
		||||
            column: 26,
 | 
			
		||||
        };
 | 
			
		||||
        const syntaxes = parser.getSuggestionAtCaretPosition(
 | 
			
		||||
            commentOtherLine(syntaxSql, pos.lineNumber),
 | 
			
		||||
            pos
 | 
			
		||||
        )?.syntax;
 | 
			
		||||
        const suggestion = syntaxes?.find(
 | 
			
		||||
            (syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        expect(suggestion).not.toBeUndefined();
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('Optimize table zorder by columns', () => {
 | 
			
		||||
        const pos: CaretPosition = {
 | 
			
		||||
            lineNumber: 67,
 | 
			
		||||
            column: 33,
 | 
			
		||||
        };
 | 
			
		||||
        const syntaxes = parser.getSuggestionAtCaretPosition(
 | 
			
		||||
            commentOtherLine(syntaxSql, pos.lineNumber),
 | 
			
		||||
            pos
 | 
			
		||||
        )?.syntax;
 | 
			
		||||
        const suggestion = syntaxes?.find(
 | 
			
		||||
            (syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        expect(suggestion).not.toBeUndefined();
 | 
			
		||||
        expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['i']);
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
@ -19,7 +19,15 @@ describe('Spark SQL Token Suggestion', () => {
 | 
			
		||||
            pos
 | 
			
		||||
        )?.keywords;
 | 
			
		||||
 | 
			
		||||
        expect(suggestion).toEqual(['TABLE', 'INDEX', 'VIEW', 'DATABASE', 'NAMESPACE', 'SCHEMA']);
 | 
			
		||||
        expect(suggestion).toEqual([
 | 
			
		||||
            'TABLE',
 | 
			
		||||
            'INDEX',
 | 
			
		||||
            'VIEW',
 | 
			
		||||
            'MATERIALIZED',
 | 
			
		||||
            'DATABASE',
 | 
			
		||||
            'NAMESPACE',
 | 
			
		||||
            'SCHEMA',
 | 
			
		||||
        ]);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('After CREATE', () => {
 | 
			
		||||
@ -36,6 +44,7 @@ describe('Spark SQL Token Suggestion', () => {
 | 
			
		||||
            'TEMPORARY',
 | 
			
		||||
            'INDEX',
 | 
			
		||||
            'ROLE',
 | 
			
		||||
            'MATERIALIZED',
 | 
			
		||||
            'FUNCTION',
 | 
			
		||||
            'OR',
 | 
			
		||||
            'GLOBAL',
 | 
			
		||||
@ -102,6 +111,7 @@ describe('Spark SQL Token Suggestion', () => {
 | 
			
		||||
            'INDEX',
 | 
			
		||||
            'ROLE',
 | 
			
		||||
            'FUNCTION',
 | 
			
		||||
            'MATERIALIZED',
 | 
			
		||||
            'VIEW',
 | 
			
		||||
            'TABLE',
 | 
			
		||||
            'DATABASE',
 | 
			
		||||
@ -157,6 +167,7 @@ describe('Spark SQL Token Suggestion', () => {
 | 
			
		||||
            'PRINCIPALS',
 | 
			
		||||
            'ROLE',
 | 
			
		||||
            'GRANT',
 | 
			
		||||
            'MATERIALIZED',
 | 
			
		||||
            'CATALOGS',
 | 
			
		||||
            'FUNCTIONS',
 | 
			
		||||
            'ALL',
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user