diff --git a/.licenserc.yaml b/.licenserc.yaml index 4244c22a90..d9f95d1fc0 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -37,6 +37,8 @@ header: - 'tsan_suppressions' - 'docs/.markdownlintignore' - 'fe/fe-core/src/test/resources/data/net_snmp_normal' + - 'fe/fe-core/src/main/antlr4/org/apache/doris/nereids/JavaLexer.g4' + - 'fe/fe-core/src/main/antlr4/org/apache/doris/nereids/JavaParser.g4' - 'be/src/common/signal_handler.h' - 'be/src/olap/lru_cache.cpp' - 'be/src/olap/lru_cache.h' diff --git a/LICENSE.txt b/LICENSE.txt index 775386b0ff..8ca9617539 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -608,4 +608,70 @@ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +-------------------------------------------------------------------------------- + +fe/fe-core/src/main/antlr4/org/apache/doris/nereids/JavaLexer.g4: BSD licence + +[The "BSD licence"] + Copyright (c) 2013 Terence Parr, Sam Harwell + Copyright (c) 2017 Ivan Kochurkin (upgrade to Java 8) + Copyright (c) 2021 Michał Lorek (upgrade to Java 11) + Copyright (c) 2022 Michał Lorek (upgrade to Java 17) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +-------------------------------------------------------------------------------- + +fe/fe-core/src/main/antlr4/org/apache/doris/nereids/JavaParser.g4: BSD licence + + [The "BSD licence"] + Copyright (c) 2013 Terence Parr, Sam Harwell + Copyright (c) 2017 Ivan Kochurkin (upgrade to Java 8) + Copyright (c) 2021 Michał Lorek (upgrade to Java 11) + Copyright (c) 2022 Michał Lorek (upgrade to Java 17) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/fe/fe-core/pom.xml b/fe/fe-core/pom.xml index 6ebb3b0b29..0382b6f4aa 100644 --- a/fe/fe-core/pom.xml +++ b/fe/fe-core/pom.xml @@ -643,6 +643,18 @@ under the License. ${antlr4.version} + + javax.annotation + javax.annotation-api + 1.3.2 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + palo-fe @@ -787,6 +799,65 @@ under the License. + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + + + compile-describable-processor + generate-sources + + compile + + + none + + org/apache/doris/nereids/pattern/generator/PatternDescribable.java + org/apache/doris/nereids/pattern/generator/PatternDescribableProcessor.java + + + + + + + generate-patterns + generate-sources + + compile + + + only + + -AoperatorPath=${basedir}/src/main/java/org/apache/doris/nereids + + + org/apache/doris/nereids/pattern/generator/PatternDescribableProcessPoint.java + + + org.apache.doris.nereids.pattern.generator.PatternDescribableProcessor + + + + + + + default-compile + compile + + compile + + + + org/apache/doris/nereids/pattern/generator/** + + + + + org.codehaus.mojo diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/JavaLexer.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/JavaLexer.g4 new file mode 100644 index 0000000000..2cdc0e1a2c --- /dev/null +++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/JavaLexer.g4 @@ -0,0 +1,241 @@ +/* + [The "BSD licence"] + Copyright (c) 2013 Terence Parr, Sam Harwell + Copyright (c) 2017 Ivan Kochurkin (upgrade to Java 8) + Copyright (c) 2021 Michał Lorek (upgrade to Java 11) + Copyright (c) 2022 Michał Lorek (upgrade to Java 17) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +lexer grammar JavaLexer; + +// Keywords + +ABSTRACT: 'abstract'; +ASSERT: 'assert'; +BOOLEAN: 'boolean'; +BREAK: 'break'; +BYTE: 'byte'; +CASE: 'case'; +CATCH: 'catch'; +CHAR: 'char'; +CLASS: 'class'; +CONST: 'const'; +CONTINUE: 'continue'; +DEFAULT: 'default'; +DO: 'do'; +DOUBLE: 'double'; +ELSE: 'else'; +ENUM: 'enum'; +EXTENDS: 'extends'; +FINAL: 'final'; +FINALLY: 'finally'; +FLOAT: 'float'; +FOR: 'for'; +IF: 'if'; +GOTO: 'goto'; +IMPLEMENTS: 'implements'; +IMPORT: 'import'; +INSTANCEOF: 'instanceof'; +INT: 'int'; +INTERFACE: 'interface'; +LONG: 'long'; +NATIVE: 'native'; +NEW: 'new'; +PACKAGE: 'package'; +PRIVATE: 'private'; +PROTECTED: 'protected'; +PUBLIC: 'public'; +RETURN: 'return'; +SHORT: 'short'; +STATIC: 'static'; +STRICTFP: 'strictfp'; +SUPER: 'super'; +SWITCH: 'switch'; +SYNCHRONIZED: 'synchronized'; +THIS: 'this'; +THROW: 'throw'; +THROWS: 'throws'; +TRANSIENT: 'transient'; +TRY: 'try'; +VOID: 'void'; +VOLATILE: 'volatile'; +WHILE: 'while'; + +// Module related keywords +MODULE: 'module'; +OPEN: 'open'; +REQUIRES: 'requires'; +EXPORTS: 'exports'; +OPENS: 'opens'; +TO: 'to'; +USES: 'uses'; +PROVIDES: 'provides'; +WITH: 'with'; +TRANSITIVE: 'transitive'; + +// Local Variable Type Inference +VAR: 'var'; // reserved type name + +// Switch Expressions +YIELD: 'yield'; + +// Records +RECORD: 'record'; + +// Sealed Classes +SEALED: 'sealed'; +PERMITS: 'permits'; +NON_SEALED: 'non-sealed'; + +// Literals + +DECIMAL_LITERAL: ('0' | [1-9] (Digits? | '_'+ Digits)) [lL]?; +HEX_LITERAL: '0' [xX] [0-9a-fA-F] ([0-9a-fA-F_]* [0-9a-fA-F])? [lL]?; +OCT_LITERAL: '0' '_'* [0-7] ([0-7_]* [0-7])? [lL]?; +BINARY_LITERAL: '0' [bB] [01] ([01_]* [01])? [lL]?; + +FLOAT_LITERAL: (Digits '.' Digits? | '.' Digits) ExponentPart? [fFdD]? + | Digits (ExponentPart [fFdD]? | [fFdD]) + ; + +HEX_FLOAT_LITERAL: '0' [xX] (HexDigits '.'? | HexDigits? '.' HexDigits) [pP] [+-]? Digits [fFdD]?; + +BOOL_LITERAL: 'true' + | 'false' + ; + +CHAR_LITERAL: '\'' (~['\\\r\n] | EscapeSequence) '\''; + +STRING_LITERAL: '"' (~["\\\r\n] | EscapeSequence)* '"'; + +TEXT_BLOCK: '"""' [ \t]* [\r\n] (. | EscapeSequence)*? '"""'; + +NULL_LITERAL: 'null'; + +// Separators + +LPAREN: '('; +RPAREN: ')'; +LBRACE: '{'; +RBRACE: '}'; +LBRACK: '['; +RBRACK: ']'; +SEMI: ';'; +COMMA: ','; +DOT: '.'; + +// Operators + +ASSIGN: '='; +GT: '>'; +LT: '<'; +BANG: '!'; +TILDE: '~'; +QUESTION: '?'; +COLON: ':'; +EQUAL: '=='; +LE: '<='; +GE: '>='; +NOTEQUAL: '!='; +AND: '&&'; +OR: '||'; +INC: '++'; +DEC: '--'; +ADD: '+'; +SUB: '-'; +MUL: '*'; +DIV: '/'; +BITAND: '&'; +BITOR: '|'; +CARET: '^'; +MOD: '%'; + +ADD_ASSIGN: '+='; +SUB_ASSIGN: '-='; +MUL_ASSIGN: '*='; +DIV_ASSIGN: '/='; +AND_ASSIGN: '&='; +OR_ASSIGN: '|='; +XOR_ASSIGN: '^='; +MOD_ASSIGN: '%='; +LSHIFT_ASSIGN: '<<='; +RSHIFT_ASSIGN: '>>='; +URSHIFT_ASSIGN: '>>>='; + +// Java 8 tokens + +ARROW: '->'; +COLONCOLON: '::'; + +// Additional symbols not defined in the lexical specification + +AT: '@'; +ELLIPSIS: '...'; + +// Whitespace and comments + +WS: [ \t\r\n\u000C]+ -> channel(HIDDEN); +COMMENT: '/*' .*? '*/' -> channel(HIDDEN); +LINE_COMMENT: '//' ~[\r\n]* -> channel(HIDDEN); + +// Identifiers + +IDENTIFIER: Letter LetterOrDigit*; + +// Fragment rules + +fragment ExponentPart + : [eE] [+-]? Digits + ; + +fragment EscapeSequence + : '\\' [btnfr"'\\] + | '\\' ([0-3]? [0-7])? [0-7] + | '\\' 'u'+ HexDigit HexDigit HexDigit HexDigit + ; + +fragment HexDigits + : HexDigit ((HexDigit | '_')* HexDigit)? + ; + +fragment HexDigit + : [0-9a-fA-F] + ; + +fragment Digits + : [0-9] ([0-9_]* [0-9])? + ; + +fragment LetterOrDigit + : Letter + | [0-9] + ; + +fragment Letter + : [a-zA-Z$_] // these are the "java letters" below 0x7F + | ~[\u0000-\u007F\uD800-\uDBFF] // covers all characters above 0x7F which are not a surrogate + | [\uD800-\uDBFF] [\uDC00-\uDFFF] // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF + ; diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/JavaParser.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/JavaParser.g4 new file mode 100644 index 0000000000..afb8b3549b --- /dev/null +++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/JavaParser.g4 @@ -0,0 +1,762 @@ +/* + [The "BSD licence"] + Copyright (c) 2013 Terence Parr, Sam Harwell + Copyright (c) 2017 Ivan Kochurkin (upgrade to Java 8) + Copyright (c) 2021 Michał Lorek (upgrade to Java 11) + Copyright (c) 2022 Michał Lorek (upgrade to Java 17) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +parser grammar JavaParser; + +options { tokenVocab=JavaLexer; } + +compilationUnit + : packageDeclaration? importDeclaration* typeDeclaration* + | moduleDeclaration EOF + ; + +packageDeclaration + : annotation* PACKAGE qualifiedName ';' + ; + +importDeclaration + : IMPORT STATIC? qualifiedName importStar? ';' + ; + +importStar + : '.' '*' + ; + +typeDeclaration + : classOrInterfaceModifier* + (classDeclaration | enumDeclaration | interfaceDeclaration | annotationTypeDeclaration | recordDeclaration) + | ';' + ; + +modifier + : classOrInterfaceModifier + | NATIVE + | SYNCHRONIZED + | TRANSIENT + | VOLATILE + ; + +classOrInterfaceModifier + : annotation + | PUBLIC + | PROTECTED + | PRIVATE + | STATIC + | ABSTRACT + | FINAL // FINAL for class only -- does not apply to interfaces + | STRICTFP + | SEALED // Java17 + | NON_SEALED // Java17 + ; + +variableModifier + : FINAL + | annotation + ; + +classDeclaration + : CLASS identifier typeParameters? + (EXTENDS typeType)? + (IMPLEMENTS typeList)? + (PERMITS typeList)? // Java17 + classBody + ; + +typeParameters + : '<' typeParameter (',' typeParameter)* '>' + ; + +typeParameter + : annotation* identifier (EXTENDS annotation* typeBound)? + ; + +typeBound + : typeType ('&' typeType)* + ; + +enumDeclaration + : ENUM identifier (IMPLEMENTS typeList)? '{' enumConstants? ','? enumBodyDeclarations? '}' + ; + +enumConstants + : enumConstant (',' enumConstant)* + ; + +enumConstant + : annotation* identifier arguments? classBody? + ; + +enumBodyDeclarations + : ';' classBodyDeclaration* + ; + +interfaceDeclaration + : INTERFACE identifier typeParameters? (EXTENDS typeList)? (PERMITS typeList)? interfaceBody + ; + +classBody + : '{' classBodyDeclaration* '}' + ; + +interfaceBody + : '{' interfaceBodyDeclaration* '}' + ; + +classBodyDeclaration + : ';' + | STATIC? block + | modifier* memberDeclaration + ; + +memberDeclaration + : methodDeclaration + | genericMethodDeclaration + | fieldDeclaration + | constructorDeclaration + | genericConstructorDeclaration + | interfaceDeclaration + | annotationTypeDeclaration + | classDeclaration + | enumDeclaration + | recordDeclaration //Java17 + ; + +/* We use rule this even for void methods which cannot have [] after parameters. + This simplifies grammar and we can consider void to be a type, which + renders the [] matching as a context-sensitive issue or a semantic check + for invalid return type after parsing. + */ +methodDeclaration + : typeTypeOrVoid identifier formalParameters ('[' ']')* + (THROWS qualifiedNameList)? + methodBody + ; + +methodBody + : block + | ';' + ; + +typeTypeOrVoid + : typeType + | VOID + ; + +genericMethodDeclaration + : typeParameters methodDeclaration + ; + +genericConstructorDeclaration + : typeParameters constructorDeclaration + ; + +constructorDeclaration + : identifier formalParameters (THROWS qualifiedNameList)? constructorBody=block + ; + +fieldDeclaration + : typeType variableDeclarators ';' + ; + +interfaceBodyDeclaration + : modifier* interfaceMemberDeclaration + | ';' + ; + +interfaceMemberDeclaration + : constDeclaration + | interfaceMethodDeclaration + | genericInterfaceMethodDeclaration + | interfaceDeclaration + | annotationTypeDeclaration + | classDeclaration + | enumDeclaration + | recordDeclaration // Java17 + ; + +constDeclaration + : typeType constantDeclarator (',' constantDeclarator)* ';' + ; + +constantDeclarator + : identifier ('[' ']')* '=' variableInitializer + ; + +// Early versions of Java allows brackets after the method name, eg. +// public int[] return2DArray() [] { ... } +// is the same as +// public int[][] return2DArray() { ... } +interfaceMethodDeclaration + : interfaceMethodModifier* interfaceCommonBodyDeclaration + ; + +// Java8 +interfaceMethodModifier + : annotation + | PUBLIC + | ABSTRACT + | DEFAULT + | STATIC + | STRICTFP + ; + +genericInterfaceMethodDeclaration + : interfaceMethodModifier* typeParameters interfaceCommonBodyDeclaration + ; + +interfaceCommonBodyDeclaration + : annotation* typeTypeOrVoid identifier formalParameters ('[' ']')* (THROWS qualifiedNameList)? methodBody + ; + +variableDeclarators + : variableDeclarator (',' variableDeclarator)* + ; + +variableDeclarator + : variableDeclaratorId ('=' variableInitializer)? + ; + +variableDeclaratorId + : identifier arrayDeclarator* + ; + +arrayDeclarator + : '[' ']' + ; + +variableInitializer + : arrayInitializer + | expression + ; + +arrayInitializer + : '{' (variableInitializer (',' variableInitializer)* (',')? )? '}' + ; + +classOrInterfaceType + : identifierAndTypeArguments ('.' identifierAndTypeArguments)* + ; + +identifierAndTypeArguments + : identifier typeArguments? + ; + +typeArgument + : typeType + | annotation* '?' ((EXTENDS | SUPER) typeType)? + ; + +qualifiedNameList + : qualifiedName (',' qualifiedName)* + ; + +formalParameters + : '(' ( receiverParameter? + | receiverParameter (',' formalParameterList)? + | formalParameterList? + ) ')' + ; + +receiverParameter + : typeType (identifier '.')* THIS + ; + +formalParameterList + : formalParameter (',' formalParameter)* (',' lastFormalParameter)? + | lastFormalParameter + ; + +formalParameter + : variableModifier* typeType variableDeclaratorId + ; + +lastFormalParameter + : variableModifier* typeType annotation* '...' variableDeclaratorId + ; + +// local variable type inference +lambdaLVTIList + : lambdaLVTIParameter (',' lambdaLVTIParameter)* + ; + +lambdaLVTIParameter + : variableModifier* VAR identifier + ; + +qualifiedName + : identifier ('.' identifier)* + ; + +literal + : integerLiteral + | floatLiteral + | CHAR_LITERAL + | STRING_LITERAL + | BOOL_LITERAL + | NULL_LITERAL + | TEXT_BLOCK // Java17 + ; + +integerLiteral + : DECIMAL_LITERAL + | HEX_LITERAL + | OCT_LITERAL + | BINARY_LITERAL + ; + +floatLiteral + : FLOAT_LITERAL + | HEX_FLOAT_LITERAL + ; + +// ANNOTATIONS +altAnnotationQualifiedName + : (identifier DOT)* '@' identifier + ; + +annotation + : ('@' qualifiedName | altAnnotationQualifiedName) ('(' ( elementValuePairs | elementValue )? ')')? + ; + +elementValuePairs + : elementValuePair (',' elementValuePair)* + ; + +elementValuePair + : identifier '=' elementValue + ; + +elementValue + : expression + | annotation + | elementValueArrayInitializer + ; + +elementValueArrayInitializer + : '{' (elementValue (',' elementValue)*)? (',')? '}' + ; + +annotationTypeDeclaration + : '@' INTERFACE identifier annotationTypeBody + ; + +annotationTypeBody + : '{' (annotationTypeElementDeclaration)* '}' + ; + +annotationTypeElementDeclaration + : modifier* annotationTypeElementRest + | ';' // this is not allowed by the grammar, but apparently allowed by the actual compiler + ; + +annotationTypeElementRest + : typeType annotationMethodOrConstantRest ';' + | classDeclaration ';'? + | interfaceDeclaration ';'? + | enumDeclaration ';'? + | annotationTypeDeclaration ';'? + | recordDeclaration ';'? // Java17 + ; + +annotationMethodOrConstantRest + : annotationMethodRest + | annotationConstantRest + ; + +annotationMethodRest + : identifier '(' ')' defaultValue? + ; + +annotationConstantRest + : variableDeclarators + ; + +defaultValue + : DEFAULT elementValue + ; + +// MODULES - Java9 + +moduleDeclaration + : OPEN? MODULE qualifiedName moduleBody + ; + +moduleBody + : '{' moduleDirective* '}' + ; + +moduleDirective + : REQUIRES requiresModifier* qualifiedName ';' + | EXPORTS qualifiedName (TO qualifiedName)? ';' + | OPENS qualifiedName (TO qualifiedName)? ';' + | USES qualifiedName ';' + | PROVIDES qualifiedName WITH qualifiedName ';' + ; + +requiresModifier + : TRANSITIVE + | STATIC + ; + +// RECORDS - Java 17 + +recordDeclaration + : RECORD identifier typeParameters? recordHeader + (IMPLEMENTS typeList)? + recordBody + ; + +recordHeader + : '(' recordComponentList? ')' + ; + +recordComponentList + : recordComponent (',' recordComponent)* + ; + +recordComponent + : typeType identifier + ; + +recordBody + : '{' classBodyDeclaration* '}' + ; + +// STATEMENTS / BLOCKS + +block + : '{' blockStatement* '}' + ; + +blockStatement + : localVariableDeclaration ';' + | statement + | localTypeDeclaration + ; + +localVariableDeclaration + : variableModifier* (typeType variableDeclarators | VAR identifier '=' expression) + ; + +identifier + : IDENTIFIER + | MODULE + | OPEN + | REQUIRES + | EXPORTS + | OPENS + | TO + | USES + | PROVIDES + | WITH + | TRANSITIVE + | YIELD + | SEALED + | PERMITS + | RECORD + | VAR + ; + +localTypeDeclaration + : classOrInterfaceModifier* + (classDeclaration | interfaceDeclaration | recordDeclaration) + | ';' + ; + +statement + : blockLabel=block + | ASSERT expression (':' expression)? ';' + | IF parExpression statement (ELSE statement)? + | FOR '(' forControl ')' statement + | WHILE parExpression statement + | DO statement WHILE parExpression ';' + | TRY block (catchClause+ finallyBlock? | finallyBlock) + | TRY resourceSpecification block catchClause* finallyBlock? + | SWITCH parExpression '{' switchBlockStatementGroup* switchLabel* '}' + | SYNCHRONIZED parExpression block + | RETURN expression? ';' + | THROW expression ';' + | BREAK identifier? ';' + | CONTINUE identifier? ';' + | YIELD expression ';' // Java17 + | SEMI + | statementExpression=expression ';' + | switchExpression ';'? // Java17 + | identifierLabel=identifier ':' statement + ; + +catchClause + : CATCH '(' variableModifier* catchType identifier ')' block + ; + +catchType + : qualifiedName ('|' qualifiedName)* + ; + +finallyBlock + : FINALLY block + ; + +resourceSpecification + : '(' resources ';'? ')' + ; + +resources + : resource (';' resource)* + ; + +resource + : variableModifier* ( classOrInterfaceType variableDeclaratorId | VAR identifier ) '=' expression + | identifier + ; + +/** Matches cases then statements, both of which are mandatory. + * To handle empty cases at the end, we add switchLabel* to statement. + */ +switchBlockStatementGroup + : switchLabel+ blockStatement+ + ; + +switchLabel + : CASE (constantExpression=expression | enumConstantName=IDENTIFIER | typeType varName=identifier) ':' + | DEFAULT ':' + ; + +forControl + : enhancedForControl + | forInit? ';' expression? ';' forUpdate=expressionList? + ; + +forInit + : localVariableDeclaration + | expressionList + ; + +enhancedForControl + : variableModifier* (typeType | VAR) variableDeclaratorId ':' expression + ; + +// EXPRESSIONS + +parExpression + : '(' expression ')' + ; + +expressionList + : expression (',' expression)* + ; + +methodCall + : identifier '(' expressionList? ')' + | THIS '(' expressionList? ')' + | SUPER '(' expressionList? ')' + ; + +expression + : primary + | expression bop='.' + ( + identifier + | methodCall + | THIS + | NEW nonWildcardTypeArguments? innerCreator + | SUPER superSuffix + | explicitGenericInvocation + ) + | expression '[' expression ']' + | methodCall + | NEW creator + | '(' annotation* typeType ('&' typeType)* ')' expression + | expression postfix=('++' | '--') + | prefix=('+'|'-'|'++'|'--') expression + | prefix=('~'|'!') expression + | expression bop=('*'|'/'|'%') expression + | expression bop=('+'|'-') expression + | expression ('<' '<' | '>' '>' '>' | '>' '>') expression + | expression bop=('<=' | '>=' | '>' | '<') expression + | expression bop=INSTANCEOF (typeType | pattern) + | expression bop=('==' | '!=') expression + | expression bop='&' expression + | expression bop='^' expression + | expression bop='|' expression + | expression bop='&&' expression + | expression bop='||' expression + | expression bop='?' expression ':' expression + | expression + bop=('=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' | '>>=' | '>>>=' | '<<=' | '%=') + expression + | lambdaExpression // Java8 + | switchExpression // Java17 + + // Java 8 methodReference + | expression '::' typeArguments? identifier + | typeType '::' (typeArguments? identifier | NEW) + | classType '::' typeArguments? NEW + ; + +// Java17 +pattern + : variableModifier* typeType annotation* identifier + ; + +// Java8 +lambdaExpression + : lambdaParameters '->' lambdaBody + ; + +// Java8 +lambdaParameters + : identifier + | '(' formalParameterList? ')' + | '(' identifier (',' identifier)* ')' + | '(' lambdaLVTIList? ')' + ; + +// Java8 +lambdaBody + : expression + | block + ; + +primary + : '(' expression ')' + | THIS + | SUPER + | literal + | identifier + | typeTypeOrVoid '.' CLASS + | nonWildcardTypeArguments (explicitGenericInvocationSuffix | THIS arguments) + ; + +// Java17 +switchExpression + : SWITCH parExpression '{' switchLabeledRule* '}' + ; + +// Java17 +switchLabeledRule + : CASE (expressionList | NULL_LITERAL | guardedPattern) (ARROW | COLON) switchRuleOutcome + | DEFAULT (ARROW | COLON) switchRuleOutcome + ; + +// Java17 +guardedPattern + : '(' guardedPattern ')' + | variableModifier* typeType annotation* identifier ('&&' expression)* + | guardedPattern '&&' expression + ; + +// Java17 +switchRuleOutcome + : block + | blockStatement* + ; + +classType + : (classOrInterfaceType '.')? annotation* identifier typeArguments? + ; + +creator + : nonWildcardTypeArguments createdName classCreatorRest + | createdName (arrayCreatorRest | classCreatorRest) + ; + +createdName + : identifier typeArgumentsOrDiamond? ('.' identifier typeArgumentsOrDiamond?)* + | primitiveType + ; + +innerCreator + : identifier nonWildcardTypeArgumentsOrDiamond? classCreatorRest + ; + +arrayCreatorRest + : '[' (']' ('[' ']')* arrayInitializer | expression ']' ('[' expression ']')* ('[' ']')*) + ; + +classCreatorRest + : arguments classBody? + ; + +explicitGenericInvocation + : nonWildcardTypeArguments explicitGenericInvocationSuffix + ; + +typeArgumentsOrDiamond + : '<' '>' + | typeArguments + ; + +nonWildcardTypeArgumentsOrDiamond + : '<' '>' + | nonWildcardTypeArguments + ; + +nonWildcardTypeArguments + : '<' typeList '>' + ; + +typeList + : typeType (',' typeType)* + ; + +typeType + : annotation* (classOrInterfaceType | primitiveType) (annotation* '[' ']')* + ; + +primitiveType + : BOOLEAN + | CHAR + | BYTE + | SHORT + | INT + | LONG + | FLOAT + | DOUBLE + ; + +typeArguments + : '<' typeArgument (',' typeArgument)* '>' + ; + +superSuffix + : arguments + | '.' typeArguments? identifier arguments? + ; + +explicitGenericInvocationSuffix + : SUPER superSuffix + | identifier arguments + ; + +arguments + : '(' expressionList? ')' + ; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/OptimizerContext.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/OptimizerContext.java index 9956aacaef..d5ac07f27f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/OptimizerContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/OptimizerContext.java @@ -29,7 +29,7 @@ import org.apache.doris.nereids.trees.TreeNode; /** * Context used in memo. */ -public class OptimizerContext { +public class OptimizerContext> { private final Memo memo; private RuleSet ruleSet; private JobPool jobPool; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/PlanOperatorVisitor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/PlanOperatorVisitor.java index 063f92aae7..bd69b5a230 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/PlanOperatorVisitor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/PlanOperatorVisitor.java @@ -17,7 +17,6 @@ package org.apache.doris.nereids; -import org.apache.doris.nereids.operators.Operator; import org.apache.doris.nereids.operators.plans.physical.PhysicalAggregation; import org.apache.doris.nereids.operators.plans.physical.PhysicalFilter; import org.apache.doris.nereids.operators.plans.physical.PhysicalHashJoin; @@ -25,7 +24,9 @@ import org.apache.doris.nereids.operators.plans.physical.PhysicalOlapScan; import org.apache.doris.nereids.operators.plans.physical.PhysicalProject; import org.apache.doris.nereids.operators.plans.physical.PhysicalSort; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalBinaryPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalLeafPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalUnaryPlan; /** * Base class for the processing of logical and physical plan. @@ -36,35 +37,29 @@ import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan; @SuppressWarnings("rawtypes") public abstract class PlanOperatorVisitor { - public abstract R visit(Plan plan, C context); + public abstract R visit(Plan plan, C context); - public R visitPhysicalAggregationPlan(PhysicalPlan aggPlan, - C context) { + public R visitPhysicalAggregationPlan(PhysicalUnaryPlan aggPlan, C context) { return null; } - public R visitPhysicalOlapScanPlan(PhysicalPlan olapScanPlan, - C context) { + public R visitPhysicalOlapScanPlan(PhysicalLeafPlan olapScanPlan, C context) { return null; } - public R visitPhysicalSortPlan(PhysicalPlan sortPlan, - C context) { + public R visitPhysicalSortPlan(PhysicalUnaryPlan sortPlan, C context) { return null; } - public R visitPhysicalHashJoinPlan(PhysicalPlan hashJoinPlan, - C context) { + public R visitPhysicalHashJoinPlan(PhysicalBinaryPlan hashJoinPlan, C context) { return null; } - public R visitPhysicalProject(PhysicalPlan projectPlan, - C context) { + public R visitPhysicalProject(PhysicalUnaryPlan projectPlan, C context) { return null; } - public R visitPhysicalFilter(PhysicalPlan filterPlan, - C context) { + public R visitPhysicalFilter(PhysicalUnaryPlan filterPlan, C context) { return null; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java index 23272aaf7e..d7140292f4 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java @@ -29,8 +29,9 @@ import java.util.List; /** * Expression for unbound alias. */ -public class UnboundAlias extends NamedExpression> - implements UnaryExpression, CHILD_TYPE> { +public class UnboundAlias + extends NamedExpression + implements UnaryExpression { public UnboundAlias(CHILD_TYPE child) { super(NodeType.UNBOUND_ALIAS, child); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundRelation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundRelation.java index ca69806022..550894126d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundRelation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundRelation.java @@ -32,7 +32,7 @@ import java.util.List; /** * Represent a relation plan node that has not been bound. */ -public class UnboundRelation extends LogicalLeafOperator { +public class UnboundRelation extends LogicalLeafOperator { private final List nameParts; public UnboundRelation(List nameParts) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundSlot.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundSlot.java index c0a20a529f..5d1d4d7cbb 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundSlot.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundSlot.java @@ -28,7 +28,7 @@ import java.util.List; /** * Slot has not been bound. */ -public class UnboundSlot extends Slot { +public class UnboundSlot extends Slot { private final List nameParts; public UnboundSlot(List nameParts) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundStar.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundStar.java index ce4c9c1ca4..11b8a54d3d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundStar.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundStar.java @@ -29,7 +29,7 @@ import java.util.List; /** * Star expression. */ -public class UnboundStar extends NamedExpression implements LeafExpression { +public class UnboundStar extends NamedExpression implements LeafExpression { private final List target; public UnboundStar(List target) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/Job.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/Job.java index 28b3e01dcd..9ab88961cb 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/Job.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/Job.java @@ -30,7 +30,7 @@ import java.util.stream.Collectors; /** * Abstract class for all job using for analyze and optimize query plan in Nereids. */ -public abstract class Job { +public abstract class Job> { protected JobType type; protected PlannerContext context; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RewriteBottomUpJob.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RewriteBottomUpJob.java index fcada408a7..b82ca9f280 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RewriteBottomUpJob.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RewriteBottomUpJob.java @@ -35,7 +35,7 @@ import java.util.Objects; /** * Bottom up job for rewrite, use pattern match. */ -public class RewriteBottomUpJob extends Job { +public class RewriteBottomUpJob> extends Job { private final Group group; private final List> rules; private final boolean childrenOptimized; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RewriteTopDownJob.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RewriteTopDownJob.java index 9ea29d7bd2..64b348510d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RewriteTopDownJob.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RewriteTopDownJob.java @@ -34,7 +34,7 @@ import java.util.Objects; /** * Top down job for rewrite, use pattern match. */ -public class RewriteTopDownJob extends Job { +public class RewriteTopDownJob> extends Job { private final Group group; private final List> rules; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java index f2bf9aac37..1158e6550e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java @@ -33,7 +33,7 @@ import java.util.Map; * * @param should be {@link Plan} or {@link Expression} */ -public class Memo { +public class Memo> { private final List groups = Lists.newArrayList(); // we could not use Set, because Set has no get method. private final Map groupExpressions = Maps.newHashMap(); @@ -59,12 +59,11 @@ public class Memo { public GroupExpression copyIn(NODE_TYPE node, Group target, boolean rewrite) { Preconditions.checkArgument(!rewrite || target != null); List childrenGroups = Lists.newArrayList(); - for (Object object : node.children()) { - NODE_TYPE child = (NODE_TYPE) object; + for (NODE_TYPE child : node.children()) { childrenGroups.add(copyIn(child, null, rewrite).getParent()); } - if (node.getGroupExpression() != null && groupExpressions.containsKey(node.getGroupExpression())) { - return node.getGroupExpression(); + if (node.getGroupExpression().isPresent() && groupExpressions.containsKey(node.getGroupExpression().get())) { + return node.getGroupExpression().get(); } GroupExpression newGroupExpression = new GroupExpression(node.getOperator()); newGroupExpression.setChildren(childrenGroups); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/AbstractOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/AbstractOperator.java index 137317cbeb..6363fbcae4 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/AbstractOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/AbstractOperator.java @@ -25,7 +25,7 @@ import java.util.Objects; /** * Abstract class for all concrete operator. */ -public abstract class AbstractOperator> implements Operator { +public abstract class AbstractOperator implements Operator { protected final OperatorType type; protected final long limited; @@ -52,7 +52,7 @@ public abstract class AbstractOperator> impl * (PhysicalPlan) plan, context); * */ - public R accept(PlanOperatorVisitor visitor, Plan plan, C context) { + public R accept(PlanOperatorVisitor visitor, Plan plan, C context) { return null; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/Operator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/Operator.java index efecb3a201..b391ff24a5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/Operator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/Operator.java @@ -25,11 +25,11 @@ import org.apache.doris.nereids.trees.plans.Plan; /** * interface for all concrete operator. */ -public interface Operator> { +public interface Operator { OperatorType getType(); - NODE_TYPE toTreeNode(GroupExpression groupExpression); + > NODE_TYPE toTreeNode(GroupExpression groupExpression); - public R accept(PlanOperatorVisitor visitor, Plan plan, C context); + R accept(PlanOperatorVisitor visitor, Plan plan, C context); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/OperatorType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/OperatorType.java index f1a183521f..f2ddc06716 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/OperatorType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/OperatorType.java @@ -46,6 +46,7 @@ public enum OperatorType { PHYSICAL_EXCHANGE, // pattern + NORMAL_PATTERN, ANY, MULTI, FIXED, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/BinaryPlanOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/BinaryPlanOperator.java index aa46862efc..30fea7cbac 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/BinaryPlanOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/BinaryPlanOperator.java @@ -17,13 +17,8 @@ package org.apache.doris.nereids.operators.plans; -import org.apache.doris.nereids.trees.plans.Plan; - /** * interface for all concrete binary plan operator. */ -public interface BinaryPlanOperator< - TYPE extends BinaryPlanOperator, - LEFT_INPUT_TYPE extends Plan, - RIGHT_INPUT_TYPE extends Plan> extends PlanOperator { +public interface BinaryPlanOperator extends PlanOperator { } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/LeafPlanOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/LeafPlanOperator.java index 3fc89d8d26..9efe4c0386 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/LeafPlanOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/LeafPlanOperator.java @@ -20,5 +20,5 @@ package org.apache.doris.nereids.operators.plans; /** * interface for all concrete leaf plan operator. */ -public interface LeafPlanOperator> extends PlanOperator { +public interface LeafPlanOperator extends PlanOperator { } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/PlanOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/PlanOperator.java index 43adb3a4bf..b2b0342ebc 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/PlanOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/PlanOperator.java @@ -22,5 +22,5 @@ import org.apache.doris.nereids.operators.Operator; /** * interface for all concrete plan operator. */ -public interface PlanOperator> extends Operator { +public interface PlanOperator extends Operator { } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/UnaryPlanOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/UnaryPlanOperator.java index 8dfa3f19c4..321f7a6f78 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/UnaryPlanOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/UnaryPlanOperator.java @@ -17,12 +17,8 @@ package org.apache.doris.nereids.operators.plans; -import org.apache.doris.nereids.trees.plans.Plan; - /** * interface for all concrete unary plan operator. */ -public interface UnaryPlanOperator< - TYPE extends UnaryPlanOperator, - INPUT_TYPE extends Plan> extends PlanOperator { +public interface UnaryPlanOperator extends PlanOperator { } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalBinaryOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalBinaryOperator.java index 1b35a686d1..228ee08358 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalBinaryOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalBinaryOperator.java @@ -25,19 +25,15 @@ import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.expressions.Slot; import org.apache.doris.nereids.trees.plans.PlaceHolderPlan; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.logical.LogicalBinary; +import org.apache.doris.nereids.trees.plans.logical.LogicalBinaryPlan; import java.util.List; /** * Abstract class for all logical binary operator that have two inputs. */ -public abstract class LogicalBinaryOperator< - TYPE extends LogicalBinaryOperator, - LEFT_INPUT_TYPE extends Plan, - RIGHT_INPUT_TYPE extends Plan> - extends AbstractOperator - implements LogicalOperator, BinaryPlanOperator { +public abstract class LogicalBinaryOperator extends AbstractOperator + implements LogicalOperator, BinaryPlanOperator { public LogicalBinaryOperator(OperatorType type) { super(type); @@ -45,15 +41,15 @@ public abstract class LogicalBinaryOperator< @Override public final List computeOutput(Plan... inputs) { - return doComputeOutput((LEFT_INPUT_TYPE) inputs[0], (RIGHT_INPUT_TYPE) inputs[1]); + return doComputeOutput(inputs[0], inputs[1]); } - public abstract List doComputeOutput(LEFT_INPUT_TYPE left, RIGHT_INPUT_TYPE right); + public abstract List doComputeOutput(Plan left, Plan right); @Override - public LogicalBinary toTreeNode(GroupExpression groupExpression) { + public LogicalBinaryPlan toTreeNode(GroupExpression groupExpression) { LogicalProperties logicalProperties = groupExpression.getParent().getLogicalProperties(); - return new LogicalBinary(this, groupExpression, logicalProperties, + return new LogicalBinaryPlan(this, groupExpression, logicalProperties, new PlaceHolderPlan(), new PlaceHolderPlan()); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalFilter.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalFilter.java index 4d5ffd6862..c7d579898c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalFilter.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalFilter.java @@ -28,8 +28,7 @@ import java.util.Objects; /** * Logical filter plan operator. */ -public class LogicalFilter - extends LogicalUnaryOperator, INPUT_TYPE> { +public class LogicalFilter extends LogicalUnaryOperator { private final Expression predicates; @@ -44,7 +43,7 @@ public class LogicalFilter @Override - public List doComputeOutput(INPUT_TYPE input) { + public List doComputeOutput(Plan input) { return input.getOutput(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalJoin.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalJoin.java index 2ecdbafb4c..b1672627bd 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalJoin.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalJoin.java @@ -33,9 +33,7 @@ import java.util.Optional; /** * Logical join plan operator. */ -public class LogicalJoin - extends LogicalBinaryOperator, - LEFT_INPUT_TYPE, RIGHT_INPUT_TYPE> { +public class LogicalJoin extends LogicalBinaryOperator { private final JoinType joinType; private final Optional onClause; @@ -73,7 +71,7 @@ public class LogicalJoin doComputeOutput(LEFT_INPUT_TYPE leftInput, RIGHT_INPUT_TYPE rightInput) { + public List doComputeOutput(Plan leftInput, Plan rightInput) { switch (joinType) { case LEFT_SEMI_JOIN: return ImmutableList.copyOf(leftInput.getOutput()); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalLeafOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalLeafOperator.java index 9ce54fee3a..44c72f06e6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalLeafOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalLeafOperator.java @@ -23,16 +23,15 @@ import org.apache.doris.nereids.operators.OperatorType; import org.apache.doris.nereids.operators.plans.LeafPlanOperator; import org.apache.doris.nereids.trees.expressions.Slot; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.logical.LogicalLeaf; +import org.apache.doris.nereids.trees.plans.logical.LogicalLeafPlan; import java.util.List; /** * Abstract class for all logical operator that have no input. */ -public abstract class LogicalLeafOperator> - extends AbstractOperator - implements LogicalOperator, LeafPlanOperator { +public abstract class LogicalLeafOperator extends AbstractOperator + implements LogicalOperator, LeafPlanOperator { public LogicalLeafOperator(OperatorType type) { super(type); @@ -46,7 +45,7 @@ public abstract class LogicalLeafOperator public abstract List doComputeOutput(); @Override - public LogicalLeaf toTreeNode(GroupExpression groupExpression) { - return new LogicalLeaf(this, groupExpression, groupExpression.getParent().getLogicalProperties()); + public LogicalLeafPlan toTreeNode(GroupExpression groupExpression) { + return new LogicalLeafPlan(this, groupExpression, groupExpression.getParent().getLogicalProperties()); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalOperator.java index 4c22b7371d..0421f8f159 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalOperator.java @@ -26,6 +26,6 @@ import java.util.List; /** * interface for all concrete logical plan operator. */ -public interface LogicalOperator> extends PlanOperator { +public interface LogicalOperator extends PlanOperator { List computeOutput(Plan... inputs); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalProject.java index 9428a69121..5554542443 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalProject.java @@ -32,8 +32,7 @@ import java.util.Objects; /** * Logical project plan operator. */ -public class LogicalProject - extends LogicalUnaryOperator, INPUT_TYPE> { +public class LogicalProject extends LogicalUnaryOperator { private final List projects; @@ -57,7 +56,7 @@ public class LogicalProject } @Override - public List doComputeOutput(INPUT_TYPE input) { + public List doComputeOutput(Plan input) { // fixme: not throw a checked exception return projects.stream() .map(namedExpr -> { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalRelation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalRelation.java index 61048e994b..6f01817b82 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalRelation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalRelation.java @@ -31,7 +31,7 @@ import java.util.Objects; /** * Logical relation plan operator. */ -public class LogicalRelation extends LogicalLeafOperator { +public class LogicalRelation extends LogicalLeafOperator { private final Table table; private final List qualifier; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalUnaryOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalUnaryOperator.java index 5c2345a69d..b04f2ae8f3 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalUnaryOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/logical/LogicalUnaryOperator.java @@ -25,18 +25,15 @@ import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.expressions.Slot; import org.apache.doris.nereids.trees.plans.PlaceHolderPlan; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.logical.LogicalUnary; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnaryPlan; import java.util.List; /** * Abstract class for all logical operator that have one input. */ -public abstract class LogicalUnaryOperator< - TYPE extends LogicalUnaryOperator, - INPUT_TYPE extends Plan> - extends AbstractOperator - implements LogicalOperator, UnaryPlanOperator { +public abstract class LogicalUnaryOperator extends AbstractOperator + implements LogicalOperator, UnaryPlanOperator { public LogicalUnaryOperator(OperatorType type) { super(type); @@ -44,14 +41,14 @@ public abstract class LogicalUnaryOperator< @Override public final List computeOutput(Plan... inputs) { - return doComputeOutput((INPUT_TYPE) inputs[0]); + return doComputeOutput(inputs[0]); } - public abstract List doComputeOutput(INPUT_TYPE input); + public abstract List doComputeOutput(Plan input); @Override - public LogicalUnary toTreeNode(GroupExpression groupExpression) { + public LogicalUnaryPlan toTreeNode(GroupExpression groupExpression) { LogicalProperties logicalProperties = groupExpression.getParent().getLogicalProperties(); - return new LogicalUnary(this, groupExpression, logicalProperties, new PlaceHolderPlan()); + return new LogicalUnaryPlan(this, groupExpression, logicalProperties, new PlaceHolderPlan()); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalAggregation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalAggregation.java index feb72bd837..3240435917 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalAggregation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalAggregation.java @@ -22,14 +22,14 @@ import org.apache.doris.nereids.operators.OperatorType; import org.apache.doris.nereids.operators.plans.AggPhase; import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalUnaryPlan; import java.util.List; /** * Physical aggregation plan operator. */ -public class PhysicalAggregation extends PhysicalUnaryOperator { +public class PhysicalAggregation extends PhysicalUnaryOperator { private final List groupByExprList; @@ -80,8 +80,7 @@ public class PhysicalAggregation extends PhysicalUnaryOperator R accept(PlanOperatorVisitor visitor, Plan plan, C context) { - return visitor.visitPhysicalAggregationPlan( - (PhysicalPlan) plan, context); + public R accept(PlanOperatorVisitor visitor, Plan plan, C context) { + return visitor.visitPhysicalAggregationPlan((PhysicalUnaryPlan) plan, context); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalBinaryOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalBinaryOperator.java index 40c7774680..813cb3d445 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalBinaryOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalBinaryOperator.java @@ -25,19 +25,15 @@ import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.expressions.Slot; import org.apache.doris.nereids.trees.plans.PlaceHolderPlan; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.physical.PhysicalBinary; +import org.apache.doris.nereids.trees.plans.physical.PhysicalBinaryPlan; import java.util.List; /** * Abstract class for all physical operator that have two inputs. */ -public abstract class PhysicalBinaryOperator< - TYPE extends PhysicalBinaryOperator, - LEFT_INPUT_TYPE extends Plan, - RIGHT_INPUT_TYPE extends Plan> - extends AbstractOperator - implements PhysicalOperator, BinaryPlanOperator { +public abstract class PhysicalBinaryOperator extends AbstractOperator + implements PhysicalOperator, BinaryPlanOperator { public PhysicalBinaryOperator(OperatorType type) { super(type); @@ -45,18 +41,17 @@ public abstract class PhysicalBinaryOperator< @Override public final List computeOutputs(LogicalProperties logicalProperties, Plan... inputs) { - return doComputeOutput(logicalProperties, (LEFT_INPUT_TYPE) inputs[0], (RIGHT_INPUT_TYPE) inputs[1]); + return doComputeOutput(logicalProperties, inputs[0], inputs[1]); } - public List doComputeOutput(LogicalProperties logicalProperties, - LEFT_INPUT_TYPE left, RIGHT_INPUT_TYPE right) { + public List doComputeOutput(LogicalProperties logicalProperties, Plan left, Plan right) { return logicalProperties.getOutput(); } @Override - public PhysicalBinary toTreeNode(GroupExpression groupExpression) { + public PhysicalBinaryPlan toTreeNode(GroupExpression groupExpression) { LogicalProperties logicalProperties = groupExpression.getParent().getLogicalProperties(); - return new PhysicalBinary(this, groupExpression, logicalProperties, + return new PhysicalBinaryPlan(this, groupExpression, logicalProperties, new PlaceHolderPlan(), new PlaceHolderPlan()); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalBroadcastHashJoin.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalBroadcastHashJoin.java index cf03bf55f9..3ccbf70657 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalBroadcastHashJoin.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalBroadcastHashJoin.java @@ -28,9 +28,7 @@ import java.util.Optional; /** * Physical operator represents broadcast hash join. */ -public class PhysicalBroadcastHashJoin - extends PhysicalBinaryOperator, - LEFT_INPUT_TYPE, RIGHT_INPUT_TYPE> { +public class PhysicalBroadcastHashJoin extends PhysicalBinaryOperator { private final JoinType joinType; private final Optional onClause; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalFilter.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalFilter.java index f62b69e3ad..aa54b67caa 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalFilter.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalFilter.java @@ -21,15 +21,14 @@ import org.apache.doris.nereids.PlanOperatorVisitor; import org.apache.doris.nereids.operators.OperatorType; import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalUnaryPlan; import java.util.Objects; /** * Physical filter plan operator. */ -public class PhysicalFilter - extends PhysicalUnaryOperator, INPUT_TYPE> { +public class PhysicalFilter extends PhysicalUnaryOperator { private final Expression predicates; @@ -54,8 +53,7 @@ public class PhysicalFilter } @Override - public R accept(PlanOperatorVisitor visitor, Plan plan, C context) { - return visitor.visitPhysicalFilter((PhysicalPlan) plan, - context); + public R accept(PlanOperatorVisitor visitor, Plan plan, C context) { + return visitor.visitPhysicalFilter((PhysicalUnaryPlan) plan, context); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalHashJoin.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalHashJoin.java index 41785dbab8..94eb6f9147 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalHashJoin.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalHashJoin.java @@ -22,12 +22,12 @@ import org.apache.doris.nereids.operators.OperatorType; import org.apache.doris.nereids.operators.plans.JoinType; import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalBinaryPlan; /** * Physical hash join plan operator. */ -public class PhysicalHashJoin extends PhysicalBinaryOperator { +public class PhysicalHashJoin extends PhysicalBinaryOperator { private final JoinType joinType; @@ -54,9 +54,8 @@ public class PhysicalHashJoin extends PhysicalBinaryOperator R accept(PlanOperatorVisitor visitor, Plan plan, C context) { - return visitor.visitPhysicalHashJoinPlan( - (PhysicalPlan) plan, context); + public R accept(PlanOperatorVisitor visitor, Plan plan, C context) { + return visitor.visitPhysicalHashJoinPlan((PhysicalBinaryPlan) plan, context); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalLeafOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalLeafOperator.java index 1093e6086d..6cf64ede7c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalLeafOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalLeafOperator.java @@ -24,16 +24,15 @@ import org.apache.doris.nereids.operators.plans.LeafPlanOperator; import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.expressions.Slot; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.physical.PhysicalLeaf; +import org.apache.doris.nereids.trees.plans.physical.PhysicalLeafPlan; import java.util.List; /** * Abstract class for all physical operator that have no input. */ -public abstract class PhysicalLeafOperator> - extends AbstractOperator - implements PhysicalOperator, LeafPlanOperator { +public abstract class PhysicalLeafOperator extends AbstractOperator + implements PhysicalOperator, LeafPlanOperator { public PhysicalLeafOperator(OperatorType type) { super(type); @@ -49,7 +48,7 @@ public abstract class PhysicalLeafOperator { +public class PhysicalOlapScan extends PhysicalScan { private final long selectedIndexId; private final List selectedTabletId; private final List selectedPartitionId; @@ -80,9 +80,8 @@ public class PhysicalOlapScan extends PhysicalScan { } @Override - public R accept(PlanOperatorVisitor visitor, Plan plan, C context) { - return visitor.visitPhysicalOlapScanPlan( - (PhysicalPlan) plan, context); + public R accept(PlanOperatorVisitor visitor, Plan plan, C context) { + return visitor.visitPhysicalOlapScanPlan((PhysicalLeafPlan) plan, context); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalOperator.java index dd5aa79a9f..8414f6356d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalOperator.java @@ -27,7 +27,7 @@ import java.util.List; /** * interface for all concrete physical operator. */ -public interface PhysicalOperator> extends PlanOperator { +public interface PhysicalOperator extends PlanOperator { List computeOutputs(LogicalProperties logicalProperties, Plan... inputs); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalProject.java index d5046083bf..5de5fc8c01 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalProject.java @@ -21,7 +21,7 @@ import org.apache.doris.nereids.PlanOperatorVisitor; import org.apache.doris.nereids.operators.OperatorType; import org.apache.doris.nereids.trees.expressions.NamedExpression; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalUnaryPlan; import org.apache.commons.lang3.StringUtils; @@ -31,8 +31,7 @@ import java.util.Objects; /** * Physical project plan operator. */ -public class PhysicalProject - extends PhysicalUnaryOperator, INPUT_TYPE> { +public class PhysicalProject extends PhysicalUnaryOperator { private final List projects; @@ -51,8 +50,7 @@ public class PhysicalProject } @Override - public R accept(PlanOperatorVisitor visitor, Plan plan, C context) { - return visitor.visitPhysicalProject( - (PhysicalPlan) plan, context); + public R accept(PlanOperatorVisitor visitor, Plan plan, C context) { + return visitor.visitPhysicalProject(((PhysicalUnaryPlan) plan), context); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalScan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalScan.java index 10d434c7e2..63ffef9b0f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalScan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalScan.java @@ -25,7 +25,7 @@ import java.util.Objects; /** * Abstract class for all physical scan operator. */ -public abstract class PhysicalScan> extends PhysicalLeafOperator { +public abstract class PhysicalScan extends PhysicalLeafOperator { protected final List qualifier; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalSort.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalSort.java index 1dcb3d6540..dc1ab43b70 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalSort.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalSort.java @@ -21,14 +21,14 @@ import org.apache.doris.nereids.PlanOperatorVisitor; import org.apache.doris.nereids.operators.OperatorType; import org.apache.doris.nereids.properties.OrderKey; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalUnaryPlan; import java.util.List; /** * Physical sort plan operator. */ -public class PhysicalSort extends PhysicalUnaryOperator { +public class PhysicalSort extends PhysicalUnaryOperator { private final int offset; @@ -70,8 +70,7 @@ public class PhysicalSort extends PhysicalUnaryOperator R accept(PlanOperatorVisitor visitor, Plan plan, C context) { - return visitor.visitPhysicalSortPlan((PhysicalPlan) plan, - context); + public R accept(PlanOperatorVisitor visitor, Plan plan, C context) { + return visitor.visitPhysicalSortPlan((PhysicalUnaryPlan) plan, context); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalUnaryOperator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalUnaryOperator.java index 80f54d3ab6..daecac8191 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalUnaryOperator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/operators/plans/physical/PhysicalUnaryOperator.java @@ -25,18 +25,15 @@ import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.expressions.Slot; import org.apache.doris.nereids.trees.plans.PlaceHolderPlan; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.physical.PhysicalUnary; +import org.apache.doris.nereids.trees.plans.physical.PhysicalUnaryPlan; import java.util.List; /** * Abstract class for all physical operator that have one input. */ -public abstract class PhysicalUnaryOperator< - TYPE extends PhysicalUnaryOperator, - INPUT_TYPE extends Plan> - extends AbstractOperator - implements PhysicalOperator, UnaryPlanOperator { +public abstract class PhysicalUnaryOperator extends AbstractOperator + implements PhysicalOperator, UnaryPlanOperator { public PhysicalUnaryOperator(OperatorType type) { super(type); @@ -44,16 +41,16 @@ public abstract class PhysicalUnaryOperator< @Override public final List computeOutputs(LogicalProperties logicalProperties, Plan... inputs) { - return doComputeOutput(logicalProperties, (INPUT_TYPE) inputs[0]); + return doComputeOutput(logicalProperties, inputs[0]); } - public List doComputeOutput(LogicalProperties logicalProperties, INPUT_TYPE input) { + public List doComputeOutput(LogicalProperties logicalProperties, Plan input) { return logicalProperties.getOutput(); } @Override - public PhysicalUnary toTreeNode(GroupExpression groupExpression) { + public PhysicalUnaryPlan toTreeNode(GroupExpression groupExpression) { LogicalProperties logicalProperties = groupExpression.getParent().getLogicalProperties(); - return new PhysicalUnary(this, groupExpression, logicalProperties, new PlaceHolderPlan()); + return new PhysicalUnaryPlan(this, groupExpression, logicalProperties, new PlaceHolderPlan()); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 77c827433e..c70b8a4dc4 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -66,10 +66,10 @@ import org.apache.doris.nereids.trees.expressions.Literal; import org.apache.doris.nereids.trees.expressions.NamedExpression; import org.apache.doris.nereids.trees.expressions.Not; import org.apache.doris.nereids.trees.expressions.NullSafeEqual; -import org.apache.doris.nereids.trees.plans.logical.LogicalBinary; -import org.apache.doris.nereids.trees.plans.logical.LogicalLeaf; +import org.apache.doris.nereids.trees.plans.logical.LogicalBinaryPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalLeafPlan; import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; -import org.apache.doris.nereids.trees.plans.logical.LogicalUnary; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnaryPlan; import com.google.common.collect.Lists; import org.antlr.v4.runtime.ParserRuleContext; @@ -95,7 +95,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor { */ private final BiFunction withWhereClause = (WhereClauseContext ctx, LogicalPlan plan) - -> new LogicalUnary(new LogicalFilter(expression((ctx.booleanExpression()))), plan); + -> new LogicalUnaryPlan(new LogicalFilter(expression((ctx.booleanExpression()))), plan); protected T typedVisit(ParseTree ctx) { return (T) ctx.accept(this); @@ -213,7 +213,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor { LogicalPlan withProject; if (CollectionUtils.isNotEmpty(namedExpressions)) { - withProject = new LogicalUnary(new LogicalProject(namedExpressions), withFilter); + withProject = new LogicalUnaryPlan(new LogicalProject(namedExpressions), withFilter); } else { withProject = withFilter; } @@ -229,7 +229,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor { if (left == null) { left = right; } else { - left = new LogicalBinary(new LogicalJoin(JoinType.INNER_JOIN, null), left, right); + left = new LogicalBinaryPlan(new LogicalJoin(JoinType.INNER_JOIN, null), left, right); } left = withJoinRelations(left, relation); } @@ -269,7 +269,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor { condition = expression(joinCriteria.booleanExpression()); } - last = new LogicalBinary( + last = new LogicalBinaryPlan( new LogicalJoin(joinType, Optional.ofNullable(condition)), last, plan(join.relationPrimary()) ); @@ -285,7 +285,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor { List tableId = visitMultipartIdentifier(ctx.multipartIdentifier()); UnboundRelation relation = new UnboundRelation(tableId); // TODO: sample and time travel, alias, sub query - return new LogicalLeaf(relation); + return new LogicalLeafPlan(relation); } /** diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/GroupExpressionMatching.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/GroupExpressionMatching.java index 33a975f147..955665a18b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/GroupExpressionMatching.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/GroupExpressionMatching.java @@ -32,11 +32,11 @@ import java.util.Objects; * TODO: adapt ANY and MULTI * TODO: add ut */ -public class GroupExpressionMatching implements Iterable { - private final Pattern pattern; +public class GroupExpressionMatching> implements Iterable { + private final Pattern pattern; private final GroupExpression groupExpression; - public GroupExpressionMatching(Pattern pattern, GroupExpression groupExpression) { + public GroupExpressionMatching(Pattern pattern, GroupExpression groupExpression) { this.pattern = Objects.requireNonNull(pattern); this.groupExpression = Objects.requireNonNull(groupExpression); } @@ -49,7 +49,7 @@ public class GroupExpressionMatching implements Iter /** * Iterator to get all subtrees. */ - public static class GroupExpressionIterator implements Iterator { + public static class GroupExpressionIterator> implements Iterator { private final List results; private int resultIndex = 0; @@ -59,7 +59,8 @@ public class GroupExpressionMatching implements Iter * @param pattern pattern to match * @param groupExpression group expression to be matched */ - public GroupExpressionIterator(Pattern pattern, GroupExpression groupExpression) { + public GroupExpressionIterator(Pattern pattern, + GroupExpression groupExpression) { results = Lists.newArrayList(); if (!pattern.matchOperator(groupExpression.getOperator())) { @@ -74,7 +75,7 @@ public class GroupExpressionMatching implements Iter return; } - NODE_TYPE root = (NODE_TYPE) groupExpression.getOperator().toTreeNode(groupExpression); + NODE_TYPE root = groupExpression.getOperator().toTreeNode(groupExpression); List> childrenResults = Lists.newArrayListWithCapacity(groupExpression.arity()); for (int i = 0; i < groupExpression.arity(); ++i) { @@ -96,7 +97,7 @@ public class GroupExpressionMatching implements Iter for (int i = 0; i < childrenResults.size(); i++) { children.add(childrenResults.get(i).get(childrenResultsIndex[i])); } - NODE_TYPE result = (NODE_TYPE) root.newChildren(children); + NODE_TYPE result = root.newChildren(children); results.add(result); offset = 0; while (true) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/GroupMatching.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/GroupMatching.java index 9a37868957..4771300ad9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/GroupMatching.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/GroupMatching.java @@ -48,7 +48,7 @@ public class GroupMatching implements Iterable implements Iterator { + public static class GroupIterator> implements Iterator { private final Pattern pattern; private final List> iterator; private int iteratorIndex = 0; @@ -59,19 +59,19 @@ public class GroupMatching implements Iterable pattern, Group group) { this.pattern = pattern; this.iterator = Lists.newArrayList(); for (GroupExpression groupExpression : group.getLogicalExpressions()) { GroupExpressionMatching.GroupExpressionIterator groupExpressionIterator = - new GroupExpressionMatching(pattern, groupExpression).iterator(); + new GroupExpressionMatching(pattern, groupExpression).iterator(); if (groupExpressionIterator.hasNext()) { this.iterator.add(groupExpressionIterator); } } for (GroupExpression groupExpression : group.getPhysicalExpressions()) { GroupExpressionMatching.GroupExpressionIterator groupExpressionIterator = - new GroupExpressionMatching(pattern, groupExpression).iterator(); + new GroupExpressionMatching(pattern, groupExpression).iterator(); if (groupExpressionIterator.hasNext()) { this.iterator.add(groupExpressionIterator); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/MatchedAction.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/MatchedAction.java index 67877c6f87..dbc127fb32 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/MatchedAction.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/MatchedAction.java @@ -23,6 +23,10 @@ import org.apache.doris.nereids.trees.TreeNode; * Define an callback action when match a pattern, usually implement as a rule body. * e.g. exchange join children for JoinCommutative Rule */ -public interface MatchedAction { - OUTPUT_TYPE apply(MatchingContext ctx); +public interface MatchedAction< + INPUT_TYPE extends RULE_TYPE, + OUTPUT_TYPE extends RULE_TYPE, + RULE_TYPE extends TreeNode> { + + OUTPUT_TYPE apply(MatchingContext ctx); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/MatchingContext.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/MatchingContext.java index 460a982a70..c25c927831 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/MatchingContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/MatchingContext.java @@ -23,9 +23,9 @@ import org.apache.doris.nereids.trees.TreeNode; /** * Define a context when match a pattern pass through a MatchedAction. */ -public class MatchingContext { - public final T root; - public final Pattern pattern; +public class MatchingContext> { + public final TYPE root; + public final Pattern pattern; public final PlannerContext plannerContext; /** @@ -35,7 +35,7 @@ public class MatchingContext { * @param pattern the defined pattern * @param plannerContext the planner context */ - public MatchingContext(T root, Pattern pattern, PlannerContext plannerContext) { + public MatchingContext(TYPE root, Pattern pattern, PlannerContext plannerContext) { this.root = root; this.pattern = pattern; this.plannerContext = plannerContext; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/Pattern.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/Pattern.java index 15d9544a87..781e10aa97 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/Pattern.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/Pattern.java @@ -28,20 +28,21 @@ import com.google.common.collect.ImmutableList; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.function.Predicate; - /** * Pattern node used in pattern matching. */ -public class Pattern extends AbstractTreeNode> { +public class Pattern> + extends AbstractTreeNode> { public static final Pattern ANY = new Pattern(OperatorType.ANY); public static final Pattern MULTI = new Pattern(OperatorType.MULTI); public static final Pattern FIXED = new Pattern(OperatorType.FIXED); public static final Pattern MULTI_FIXED = new Pattern(OperatorType.MULTI_FIXED); - private final List> predicates; - private final OperatorType operatorType; + protected final List> predicates; + protected final OperatorType operatorType; /** * Constructor for Pattern. @@ -62,7 +63,7 @@ public class Pattern extends AbstractTreeNode> { * @param predicates custom matching predicate * @param children sub pattern */ - public Pattern(OperatorType operatorType, List> predicates, Pattern... children) { + public Pattern(OperatorType operatorType, List> predicates, Pattern... children) { super(NodeType.PATTERN, children); this.operatorType = operatorType; this.predicates = ImmutableList.copyOf(predicates); @@ -112,7 +113,7 @@ public class Pattern extends AbstractTreeNode> { * @param root wait to match * @return ture if current Pattern match Plan in params */ - public boolean matchRoot(T root) { + public boolean matchRoot(TYPE root) { if (root == null) { return false; } @@ -125,6 +126,10 @@ public class Pattern extends AbstractTreeNode> { return true; } + return doMatchRoot(root); + } + + protected boolean doMatchRoot(TYPE root) { return getOperatorType().equals(root.getOperator().getType()) && predicates.stream().allMatch(predicate -> predicate.test(root)); } @@ -135,9 +140,10 @@ public class Pattern extends AbstractTreeNode> { * @param root wait to match * @return ture if children Patterns match root's children in params */ - public boolean matchChildren(T root) { + public boolean matchChildren(TYPE root) { for (int i = 0; i < arity(); i++) { - if (!child(i).match(root.child(i))) { + Pattern child = child(i); + if (!child.match(root.child(i))) { return false; } } @@ -150,18 +156,22 @@ public class Pattern extends AbstractTreeNode> { * @param root wait to match * @return ture if current pattern and children patterns match root in params */ - public boolean match(T root) { + public boolean match(TYPE root) { return matchRoot(root) && matchChildren(root); } @Override - public Pattern newChildren(List children) { + public Pattern newChildren(List> children) { throw new RuntimeException(); } + public Pattern withPredicates(List> predicates) { + return new Pattern(operatorType, predicates, children.toArray(new Pattern[0])); + } + @Override - public GroupExpression getGroupExpression() { - throw new RuntimeException(); + public Optional getGroupExpression() { + return Optional.empty(); } @Override @@ -180,14 +190,4 @@ public class Pattern extends AbstractTreeNode> { public int hashCode() { return Objects.hash(operatorType); } - - @Override - public List children() { - return (List) children; - } - - @Override - public Pattern child(int index) { - return (Pattern) children.get(index); - } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternDescriptor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternDescriptor.java index d8bd2154a8..9a51a39a4d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternDescriptor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternDescriptor.java @@ -30,12 +30,12 @@ import java.util.function.Predicate; * Define a descriptor to wrap a pattern tree to define a pattern shape. * It can support pattern generic type to MatchedAction. */ -public class PatternDescriptor { - public final Pattern pattern; +public class PatternDescriptor> { + public final Pattern pattern; public final RulePromise defaultPromise; public final List> predicates = new ArrayList<>(); - public PatternDescriptor(Pattern pattern, RulePromise defaultPromise) { + public PatternDescriptor(Pattern pattern, RulePromise defaultPromise) { this.pattern = Objects.requireNonNull(pattern, "pattern can not be null"); this.defaultPromise = Objects.requireNonNull(defaultPromise, "defaultPromise can not be null"); } @@ -47,16 +47,12 @@ public class PatternDescriptor PatternMatcher then( Function matchedAction) { - return new PatternMatcher<>(patternWithPredicates(), defaultPromise, ctx -> matchedAction.apply(ctx.root)); + return new PatternMatcher<>( + pattern.withPredicates(predicates), defaultPromise, ctx -> matchedAction.apply(ctx.root)); } public PatternMatcher thenApply( - MatchedAction matchedAction) { - return new PatternMatcher<>(patternWithPredicates(), defaultPromise, matchedAction); - } - - public Pattern patternWithPredicates() { - Pattern[] children = pattern.children().toArray(new Pattern[0]); - return new Pattern<>(pattern.getOperatorType(), predicates, children); + MatchedAction matchedAction) { + return new PatternMatcher<>(pattern.withPredicates(predicates), defaultPromise, matchedAction); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternMatcher.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternMatcher.java index 0df03564ab..7ed680279c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternMatcher.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/PatternMatcher.java @@ -35,11 +35,11 @@ import java.util.Objects; public class PatternMatcher< INPUT_TYPE extends RULE_TYPE, OUTPUT_TYPE extends RULE_TYPE, - RULE_TYPE extends TreeNode> { + RULE_TYPE extends TreeNode> { - public final Pattern pattern; + public final Pattern pattern; public final RulePromise defaultRulePromise; - public final MatchedAction matchedAction; + public final MatchedAction matchedAction; /** * PatternMatcher wrap a pattern, defaultRulePromise and matchedAction. @@ -48,8 +48,8 @@ public class PatternMatcher< * @param defaultRulePromise defaultRulePromise * @param matchedAction matched callback function */ - public PatternMatcher(Pattern pattern, RulePromise defaultRulePromise, - MatchedAction matchedAction) { + public PatternMatcher(Pattern pattern, RulePromise defaultRulePromise, + MatchedAction matchedAction) { this.pattern = Objects.requireNonNull(pattern, "pattern can not be null"); this.defaultRulePromise = Objects.requireNonNull( defaultRulePromise, "defaultRulePromise can not be null"); @@ -71,7 +71,7 @@ public class PatternMatcher< return new Rule(ruleType, pattern, rulePromise) { @Override public List transform(RULE_TYPE originPlan, PlannerContext context) { - MatchingContext matchingContext = + MatchingContext matchingContext = new MatchingContext<>((INPUT_TYPE) originPlan, pattern, context); OUTPUT_TYPE replacePlan = matchedAction.apply(matchingContext); return ImmutableList.of(replacePlan == null ? originPlan : replacePlan); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/Patterns.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/Patterns.java index b938660bd6..e9891eb33f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/Patterns.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/Patterns.java @@ -17,26 +17,28 @@ package org.apache.doris.nereids.pattern; -import org.apache.doris.nereids.analyzer.UnboundRelation; -import org.apache.doris.nereids.operators.OperatorType; -import org.apache.doris.nereids.operators.plans.JoinType; -import org.apache.doris.nereids.operators.plans.logical.LogicalFilter; -import org.apache.doris.nereids.operators.plans.logical.LogicalJoin; -import org.apache.doris.nereids.operators.plans.logical.LogicalProject; -import org.apache.doris.nereids.operators.plans.logical.LogicalRelation; -import org.apache.doris.nereids.operators.plans.physical.PhysicalBroadcastHashJoin; -import org.apache.doris.nereids.operators.plans.physical.PhysicalFilter; -import org.apache.doris.nereids.operators.plans.physical.PhysicalOlapScan; -import org.apache.doris.nereids.operators.plans.physical.PhysicalProject; +import org.apache.doris.nereids.operators.plans.BinaryPlanOperator; +import org.apache.doris.nereids.operators.plans.LeafPlanOperator; +import org.apache.doris.nereids.operators.plans.UnaryPlanOperator; +import org.apache.doris.nereids.operators.plans.logical.LogicalBinaryOperator; +import org.apache.doris.nereids.operators.plans.logical.LogicalLeafOperator; +import org.apache.doris.nereids.operators.plans.logical.LogicalUnaryOperator; +import org.apache.doris.nereids.operators.plans.physical.PhysicalBinaryOperator; +import org.apache.doris.nereids.operators.plans.physical.PhysicalLeafOperator; +import org.apache.doris.nereids.operators.plans.physical.PhysicalScan; +import org.apache.doris.nereids.operators.plans.physical.PhysicalUnaryOperator; import org.apache.doris.nereids.rules.RulePromise; import org.apache.doris.nereids.trees.TreeNode; +import org.apache.doris.nereids.trees.plans.BinaryPlan; +import org.apache.doris.nereids.trees.plans.LeafPlan; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.logical.LogicalBinary; -import org.apache.doris.nereids.trees.plans.logical.LogicalLeaf; -import org.apache.doris.nereids.trees.plans.logical.LogicalUnary; -import org.apache.doris.nereids.trees.plans.physical.PhysicalBinary; -import org.apache.doris.nereids.trees.plans.physical.PhysicalLeaf; -import org.apache.doris.nereids.trees.plans.physical.PhysicalUnary; +import org.apache.doris.nereids.trees.plans.UnaryPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalBinaryPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalLeafPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnaryPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalBinaryPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalLeafPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalUnaryPlan; /** * An interface provided some PatternDescriptor. @@ -47,231 +49,171 @@ public interface Patterns { // need implement RulePromise defaultPromise(); - // logical pattern descriptors + /* special pattern descriptors */ - default PatternDescriptor any() { + default > PatternDescriptor any() { return new PatternDescriptor<>(Pattern.ANY, defaultPromise()); } - default PatternDescriptor multi() { + default > PatternDescriptor multi() { return new PatternDescriptor<>(Pattern.MULTI, defaultPromise()); } - default PatternDescriptor fixed() { + default > PatternDescriptor fixed() { return new PatternDescriptor<>(Pattern.FIXED, defaultPromise()); } + default > PatternDescriptor multiFixed() { + return new PatternDescriptor<>(Pattern.MULTI_FIXED, defaultPromise()); + } + + /* abstract plan operator patterns */ + /** - * create a unboundRelation pattern. + * create a leafPlan pattern. */ - default PatternDescriptor, Plan> unboundRelation() { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.LOGICAL_UNBOUND_RELATION), + default PatternDescriptor leafPlan() { + return new PatternDescriptor(new TypePattern(LeafPlanOperator.class), defaultPromise()); + } + + /** + * create a unaryPlan pattern. + */ + default PatternDescriptor, Plan> unaryPlan() { + return new PatternDescriptor(new TypePattern(UnaryPlanOperator.class, Pattern.FIXED), defaultPromise()); + } + + /** + * create a unaryPlan pattern. + */ + default PatternDescriptor, Plan> + unaryPlan(PatternDescriptor child) { + return new PatternDescriptor(new TypePattern(UnaryPlanOperator.class, child.pattern), defaultPromise()); + } + + /** + * create a binaryPlan pattern. + */ + default PatternDescriptor, Plan> binaryPlan() { + return new PatternDescriptor( + new TypePattern(BinaryPlanOperator.class, Pattern.FIXED, Pattern.FIXED), defaultPromise() ); } /** - * create a logicalFilter pattern. + * create a binaryPlan pattern. */ - default PatternDescriptor, Plan> logicalFilter() { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.LOGICAL_FILTER, new Pattern<>(OperatorType.FIXED)), + default + PatternDescriptor, Plan> binaryPlan( + PatternDescriptor leftChild, + PatternDescriptor rightChild) { + return new PatternDescriptor( + new TypePattern(BinaryPlanOperator.class, leftChild.pattern, rightChild.pattern), + defaultPromise() + ); + } + + /* abstract logical operator patterns */ + + /** + * create a logicalLeaf pattern. + */ + default PatternDescriptor, Plan> logicalLeaf() { + return new PatternDescriptor(new TypePattern(LogicalLeafPlan.class), defaultPromise()); + } + + /** + * create a logicalUnary pattern. + */ + default PatternDescriptor, Plan> logicalUnary() { + return new PatternDescriptor(new TypePattern(LogicalUnaryPlan.class, Pattern.FIXED), defaultPromise()); + } + + /** + * create a logicalUnary pattern. + */ + default PatternDescriptor, Plan> + logicalUnary(PatternDescriptor child) { + return new PatternDescriptor(new TypePattern(LogicalUnaryPlan.class, child.pattern), defaultPromise()); + } + + /** + * create a logicalBinary pattern. + */ + default PatternDescriptor, Plan> logicalBinary() { + return new PatternDescriptor( + new TypePattern(LogicalBinaryPlan.class, Pattern.FIXED, Pattern.FIXED), defaultPromise() ); } /** - * create a logicalFilter pattern with child pattern. + * create a logicalBinary pattern. */ - default PatternDescriptor, Plan> - logicalFilter(PatternDescriptor childPattern) { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.LOGICAL_FILTER, childPattern.pattern), + default + PatternDescriptor, Plan> + logicalBinary( + PatternDescriptor leftChild, + PatternDescriptor rightChild) { + return new PatternDescriptor( + new TypePattern(LogicalBinaryPlan.class, leftChild.pattern, rightChild.pattern), + defaultPromise() + ); + } + + /* abstract physical operator patterns */ + + /** + * create a physicalLeaf pattern. + */ + default PatternDescriptor, Plan> physicalLeaf() { + return new PatternDescriptor(new TypePattern(PhysicalLeafPlan.class), defaultPromise()); + } + + /** + * create a physicalUnary pattern. + */ + default PatternDescriptor, Plan> physicalUnary() { + return new PatternDescriptor(new TypePattern(PhysicalUnaryPlan.class, Pattern.FIXED), defaultPromise()); + } + + /** + * create a physicalUnary pattern. + */ + default PatternDescriptor, Plan> + physicalUnary(PatternDescriptor child) { + return new PatternDescriptor(new TypePattern(PhysicalUnaryPlan.class, child.pattern), defaultPromise()); + } + + /** + * create a physicalBinary pattern. + */ + default PatternDescriptor, Plan> physicalBinary() { + return new PatternDescriptor( + new TypePattern(PhysicalBinaryPlan.class, Pattern.FIXED, Pattern.FIXED), defaultPromise() ); } /** - * create a logicalProject pattern. + * create a physicalBinary pattern. */ - default PatternDescriptor, Plan> logicalProject() { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.LOGICAL_PROJECT, new Pattern<>(OperatorType.FIXED)), + default + PatternDescriptor, Plan> + physicalBinary( + PatternDescriptor leftChild, + PatternDescriptor rightChild) { + return new PatternDescriptor( + new TypePattern(PhysicalBinaryPlan.class, leftChild.pattern, rightChild.pattern), defaultPromise() ); } /** - * create a logicalProject pattern. + * create a physicalScan pattern. */ - default PatternDescriptor, Plan> - logicalProject(PatternDescriptor childPattern) { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.LOGICAL_PROJECT, childPattern.pattern), - defaultPromise() - ); - } - - /** - * create a logicalJoin pattern. - */ - default PatternDescriptor, Plan> logicalJoin() { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.LOGICAL_JOIN, - new Pattern<>(OperatorType.FIXED), new Pattern<>(OperatorType.FIXED)), - defaultPromise() - ); - } - - /** - * create a logicalJoin pattern with join type. - */ - default PatternDescriptor, Plan> logicalJoin(JoinType joinType) { - return new PatternDescriptor, Plan>( - new Pattern<>(OperatorType.LOGICAL_JOIN, - new Pattern<>(OperatorType.FIXED), new Pattern<>(OperatorType.FIXED)), - defaultPromise() - ).when(j -> j.operator.getJoinType() == joinType); - } - - /** - * create a logicalJoin pattern with joinType and children patterns. - */ - default - PatternDescriptor, Plan> logicalJoin( - JoinType joinType, PatternDescriptor leftChildPattern, - PatternDescriptor rightChildPattern) { - return new PatternDescriptor, Plan>( - new Pattern<>(OperatorType.LOGICAL_JOIN, leftChildPattern.pattern, rightChildPattern.pattern), - defaultPromise() - ).when(j -> j.operator.getJoinType() == joinType); - } - - /** - * create a logicalJoin pattern with children patterns. - */ - default - PatternDescriptor, Plan> logicalJoin( - PatternDescriptor leftChildPattern, PatternDescriptor rightChildPattern) { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.LOGICAL_JOIN, leftChildPattern.pattern, rightChildPattern.pattern), - defaultPromise() - ); - } - - /** - * create a logicalJoin pattern with joinType is inner. - */ - default PatternDescriptor, Plan> innerLogicalJoin() { - return new PatternDescriptor, Plan>( - new Pattern<>(OperatorType.LOGICAL_JOIN, - new Pattern<>(OperatorType.FIXED), new Pattern<>(OperatorType.FIXED)), - defaultPromise() - ).when(j -> j.operator.getJoinType() == JoinType.INNER_JOIN); - } - - /** - * create a logical join pattern with join type is inner and children patterns. - */ - default - PatternDescriptor, Plan> innerLogicalJoin( - PatternDescriptor leftChildPattern, PatternDescriptor rightChildPattern) { - return new PatternDescriptor, Plan>( - new Pattern<>(OperatorType.LOGICAL_JOIN, leftChildPattern.pattern, rightChildPattern.pattern), - defaultPromise() - ).when(j -> j.operator.getJoinType() == JoinType.INNER_JOIN); - } - - /** - * create a logicalRelation pattern. - */ - default PatternDescriptor, Plan> logicalRelation() { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.LOGICAL_BOUND_RELATION), - defaultPromise() - ); - } - - // physical pattern descriptors - - /** - * create a physicalFilter pattern. - */ - default PatternDescriptor, Plan> physicalFilter() { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.PHYSICAL_FILTER, new Pattern<>(OperatorType.FIXED)), - defaultPromise() - ); - } - - /** - * create a physicalFilter pattern with child pattern. - */ - default PatternDescriptor, Plan> - physicalFilter(PatternDescriptor childPattern) { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.PHYSICAL_FILTER, childPattern.pattern), - defaultPromise() - ); - } - - /** - * create a physicalProject pattern. - */ - default PatternDescriptor, Plan> physicalProject() { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.PHYSICAL_PROJECT, new Pattern<>(OperatorType.FIXED)), - defaultPromise() - ); - } - - /** - * create a physicalProject pattern with child pattern. - */ - default PatternDescriptor, Plan> - physicalProject(PatternDescriptor childPattern) { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.PHYSICAL_PROJECT, childPattern.pattern), - defaultPromise() - ); - } - - /** - * create a physicalBroadcastHashJoin pattern. - */ - default PatternDescriptor, Plan> - physicalBroadcastHashJoin() { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.PHYSICAL_BROADCAST_HASH_JOIN, - new Pattern<>(OperatorType.FIXED), new Pattern<>(OperatorType.FIXED)), - defaultPromise() - ); - } - - /** - * create a physicalBroadcastHashJoin pattern with children patterns. - */ - default - PatternDescriptor, Plan> - physicalBroadcastHashJoin(PatternDescriptor leftChildPattern, - PatternDescriptor rightChildPattern) { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.PHYSICAL_BROADCAST_HASH_JOIN, - leftChildPattern.pattern, - rightChildPattern.pattern - ), - defaultPromise() - ); - } - - /** - * create a physicalOlapScan pattern. - */ - default PatternDescriptor, Plan> physicalOlapScan() { - return new PatternDescriptor<>( - new Pattern<>(OperatorType.PHYSICAL_OLAP_SCAN), - defaultPromise() - ); + default PatternDescriptor, Plan> physicalScan() { + return new PatternDescriptor(new TypePattern(PhysicalScan.class), defaultPromise()); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/TypePattern.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/TypePattern.java new file mode 100644 index 0000000000..bbb93e65c9 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/TypePattern.java @@ -0,0 +1,57 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern; + +import org.apache.doris.nereids.operators.Operator; +import org.apache.doris.nereids.operators.OperatorType; +import org.apache.doris.nereids.trees.TreeNode; + +import java.util.List; +import java.util.Objects; +import java.util.function.Predicate; + +/** pattern that used to match class type. */ +public class TypePattern> + extends Pattern { + protected final Class type; + + public TypePattern(Class clazz, Pattern... children) { + super(OperatorType.NORMAL_PATTERN, children); + this.type = Objects.requireNonNull(clazz, "class can not be null"); + } + + public TypePattern(Class clazz, List> predicates, Pattern... children) { + super(OperatorType.NORMAL_PATTERN, predicates, children); + this.type = Objects.requireNonNull(clazz, "class can not be null"); + } + + @Override + protected boolean doMatchRoot(TYPE root) { + return type.isInstance(root) && predicates.stream().allMatch(predicate -> predicate.test(root)); + } + + @Override + public TypePattern withPredicates(List> predicates) { + return new TypePattern(type, predicates, children.toArray(new Pattern[0])); + } + + @Override + public boolean matchOperator(Operator operator) { + return type.isInstance(operator); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/JavaAstBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/JavaAstBuilder.java new file mode 100644 index 0000000000..069b2dd4cb --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/JavaAstBuilder.java @@ -0,0 +1,445 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +import org.apache.doris.nereids.JavaParser.ClassBodyDeclarationContext; +import org.apache.doris.nereids.JavaParser.ClassDeclarationContext; +import org.apache.doris.nereids.JavaParser.ClassOrInterfaceModifierContext; +import org.apache.doris.nereids.JavaParser.ClassOrInterfaceTypeContext; +import org.apache.doris.nereids.JavaParser.EnumConstantContext; +import org.apache.doris.nereids.JavaParser.EnumDeclarationContext; +import org.apache.doris.nereids.JavaParser.FieldDeclarationContext; +import org.apache.doris.nereids.JavaParser.FormalParameterListContext; +import org.apache.doris.nereids.JavaParser.IdentifierAndTypeArgumentsContext; +import org.apache.doris.nereids.JavaParser.IdentifierContext; +import org.apache.doris.nereids.JavaParser.ImportDeclarationContext; +import org.apache.doris.nereids.JavaParser.InterfaceDeclarationContext; +import org.apache.doris.nereids.JavaParser.MemberDeclarationContext; +import org.apache.doris.nereids.JavaParser.MethodDeclarationContext; +import org.apache.doris.nereids.JavaParser.PackageDeclarationContext; +import org.apache.doris.nereids.JavaParser.PrimitiveTypeContext; +import org.apache.doris.nereids.JavaParser.QualifiedNameContext; +import org.apache.doris.nereids.JavaParser.TypeArgumentContext; +import org.apache.doris.nereids.JavaParser.TypeArgumentsContext; +import org.apache.doris.nereids.JavaParser.TypeBoundContext; +import org.apache.doris.nereids.JavaParser.TypeDeclarationContext; +import org.apache.doris.nereids.JavaParser.TypeListContext; +import org.apache.doris.nereids.JavaParser.TypeParameterContext; +import org.apache.doris.nereids.JavaParser.TypeParametersContext; +import org.apache.doris.nereids.JavaParser.TypeTypeContext; +import org.apache.doris.nereids.JavaParser.TypeTypeOrVoidContext; +import org.apache.doris.nereids.JavaParser.VariableDeclaratorContext; +import org.apache.doris.nereids.JavaParser.VariableDeclaratorIdContext; +import org.apache.doris.nereids.JavaParser.VariableDeclaratorsContext; +import org.apache.doris.nereids.JavaParserBaseVisitor; +import org.apache.doris.nereids.pattern.generator.javaast.ClassDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.ClassOrInterfaceModifier; +import org.apache.doris.nereids.pattern.generator.javaast.ClassOrInterfaceType; +import org.apache.doris.nereids.pattern.generator.javaast.EnumConstant; +import org.apache.doris.nereids.pattern.generator.javaast.EnumDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.FieldDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.IdentifyTypeArgumentsPair; +import org.apache.doris.nereids.pattern.generator.javaast.ImportDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.InterfaceDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.JavaAstNode; +import org.apache.doris.nereids.pattern.generator.javaast.MethodDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.QualifiedName; +import org.apache.doris.nereids.pattern.generator.javaast.TypeArgument; +import org.apache.doris.nereids.pattern.generator.javaast.TypeArgument.ArgType; +import org.apache.doris.nereids.pattern.generator.javaast.TypeArguments; +import org.apache.doris.nereids.pattern.generator.javaast.TypeBound; +import org.apache.doris.nereids.pattern.generator.javaast.TypeDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.TypeParameter; +import org.apache.doris.nereids.pattern.generator.javaast.TypeParameters; +import org.apache.doris.nereids.pattern.generator.javaast.TypeType; +import org.apache.doris.nereids.pattern.generator.javaast.TypeTypeOrVoid; +import org.apache.doris.nereids.pattern.generator.javaast.VariableDeclarator; +import org.apache.doris.nereids.pattern.generator.javaast.VariableDeclaratorId; +import org.apache.doris.nereids.pattern.generator.javaast.VariableDeclarators; + +import org.antlr.v4.runtime.ParserRuleContext; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.Stack; + +/** + * Used to build a copy from antlr ast. + */ +public class JavaAstBuilder extends JavaParserBaseVisitor { + Optional packageName = Optional.empty(); + List importDeclarations = new ArrayList<>(); + List rootTypeDeclarations = new ArrayList<>(); + Stack> childrenStack = new Stack<>(); + + public List build(ParserRuleContext tree) { + visit(tree); + return rootTypeDeclarations; + } + + @Override + public JavaAstNode visitPackageDeclaration(PackageDeclarationContext ctx) { + packageName = Optional.of(visitQualifiedName(ctx.qualifiedName())); + return null; + } + + @Override + public ImportDeclaration visitImportDeclaration(ImportDeclarationContext ctx) { + ImportDeclaration importDeclaration = new ImportDeclaration(ctx.STATIC() != null, + visitQualifiedName(ctx.qualifiedName()), ctx.importStar() != null); + importDeclarations.add(importDeclaration); + return importDeclaration; + } + + @Override + public JavaAstNode visitTypeDeclaration(TypeDeclarationContext ctx) { + ClassOrInterfaceModifier modifier = mergeModifiers(ctx.classOrInterfaceModifier()); + if (ctx.classDeclaration() != null) { + return visitClassDeclaration(ctx.classDeclaration(), modifier); + } else if (ctx.interfaceDeclaration() != null) { + return visitInterfaceDeclaration(ctx.interfaceDeclaration(), modifier); + } else if (ctx.enumDeclaration() != null) { + return visitEnumDeclaration(ctx.enumDeclaration(), modifier); + } else { + return null; + } + } + + /** create enum declaration. */ + public EnumDeclaration visitEnumDeclaration(EnumDeclarationContext ctx, ClassOrInterfaceModifier modifier) { + List implementTypes = new ArrayList<>(); + if (ctx.IMPLEMENTS() != null) { + implementTypes = getTypes(ctx.typeList()); + } + + List enumConstants = new ArrayList<>(); + if (ctx.enumConstants() != null) { + enumConstants = visit(EnumConstant.class, ctx.enumConstants().enumConstant()); + } + + childrenStack.add(new ArrayList<>()); + if (ctx.enumBodyDeclarations() != null) { + // find inner class + ctx.enumBodyDeclarations().accept(this); + } + + String enumName = getText(ctx.identifier()); + EnumDeclaration enumDeclaration = new EnumDeclaration(packageName.orElse(null), + importDeclarations, modifier, enumName, implementTypes, enumConstants, childrenStack.pop()); + addTypeDeclaration(enumDeclaration); + + return enumDeclaration; + } + + @Override + public EnumConstant visitEnumConstant(EnumConstantContext ctx) { + return new EnumConstant(getText(ctx.identifier())); + } + + + /** create interface declaration. */ + public InterfaceDeclaration visitInterfaceDeclaration( + InterfaceDeclarationContext ctx, ClassOrInterfaceModifier modifier) { + TypeParameters typeParameters = null; + if (ctx.typeParameters() != null) { + typeParameters = visitTypeParameters(ctx.typeParameters()); + } + List extendsTypes = new ArrayList<>(); + if (ctx.EXTENDS() != null) { + extendsTypes = getTypes(ctx.typeList().get(0)); + } + + childrenStack.add(new ArrayList<>()); + // find inner class + ctx.interfaceBody().accept(this); + + String interfaceName = getText(ctx.identifier()); + InterfaceDeclaration interfaceDeclaration = new InterfaceDeclaration(packageName.orElse(null), + importDeclarations, modifier, interfaceName, typeParameters, extendsTypes, childrenStack.pop()); + addTypeDeclaration(interfaceDeclaration); + + return interfaceDeclaration; + } + + + /** create class declaration. */ + public ClassDeclaration visitClassDeclaration(ClassDeclarationContext ctx, ClassOrInterfaceModifier modifier) { + TypeParameters typeParameters = null; + if (ctx.typeParameters() != null) { + typeParameters = visitTypeParameters(ctx.typeParameters()); + } + TypeType extendsType = null; + if (ctx.EXTENDS() != null) { + extendsType = visitTypeType(ctx.typeType()); + } + List implementTypes = new ArrayList<>(); + if (ctx.IMPLEMENTS() != null) { + implementTypes = getTypes(ctx.typeList().get(0)); + } + + childrenStack.add(new ArrayList<>()); + List fieldDeclarations = new ArrayList<>(); + List methodDeclarations = new ArrayList<>(); + for (ClassBodyDeclarationContext classBodyCtx : ctx.classBody().classBodyDeclaration()) { + MemberDeclarationContext memberCtx = classBodyCtx.memberDeclaration(); + if (memberCtx != null) { + if (memberCtx.fieldDeclaration() != null) { + fieldDeclarations.add(visitFieldDeclaration(memberCtx.fieldDeclaration())); + continue; + } else if (memberCtx.methodDeclaration() != null) { + methodDeclarations.add(visitMethodDeclaration(memberCtx.methodDeclaration())); + } + } + // find inner class + memberCtx.accept(this); + } + + String className = getText(ctx.identifier()); + ClassDeclaration classDeclaration = new ClassDeclaration( + packageName.orElse(null), importDeclarations, + modifier, className, typeParameters, extendsType, implementTypes, + fieldDeclarations, methodDeclarations, childrenStack.pop()); + addTypeDeclaration(classDeclaration); + return classDeclaration; + } + + @Override + public ClassOrInterfaceModifier visitClassOrInterfaceModifier(ClassOrInterfaceModifierContext ctx) { + int mod = 0; + if (ctx.PUBLIC() != null) { + mod |= Modifier.PUBLIC; + } else if (ctx.PROTECTED() != null) { + mod |= Modifier.PROTECTED; + } else if (ctx.PRIVATE() != null) { + mod |= Modifier.PRIVATE; + } else if (ctx.STATIC() != null) { + mod |= Modifier.STATIC; + } else if (ctx.ABSTRACT() != null) { + mod |= Modifier.ABSTRACT; + } else if (ctx.ABSTRACT() != null) { + mod |= Modifier.FINAL; + } + return new ClassOrInterfaceModifier(mod); + } + + @Override + public FieldDeclaration visitFieldDeclaration(FieldDeclarationContext ctx) { + TypeType typeType = visitTypeType(ctx.typeType()); + VariableDeclarators variableDeclarators = visitVariableDeclarators(ctx.variableDeclarators()); + return new FieldDeclaration(typeType, variableDeclarators); + } + + @Override + public MethodDeclaration visitMethodDeclaration(MethodDeclarationContext ctx) { + TypeTypeOrVoid typeTypeOrVoid = visitTypeTypeOrVoid(ctx.typeTypeOrVoid()); + String identifier = getText(ctx.identifier()); + int paramNum = 0; + FormalParameterListContext paramListCtx = ctx.formalParameters().formalParameterList(); + if (paramListCtx != null && paramListCtx.formalParameter() != null) { + paramNum = paramListCtx.formalParameter().size() + 1; // + lastFormalParameter + } + return new MethodDeclaration(typeTypeOrVoid, identifier, paramNum); + } + + @Override + public TypeTypeOrVoid visitTypeTypeOrVoid(TypeTypeOrVoidContext ctx) { + TypeType typeType = null; + if (ctx.typeType() != null) { + typeType = visitTypeType(ctx.typeType()); + } + return new TypeTypeOrVoid(typeType, ctx.VOID() != null); + } + + @Override + public VariableDeclarators visitVariableDeclarators(VariableDeclaratorsContext ctx) { + List vars = visit(VariableDeclarator.class, ctx.variableDeclarator()); + return new VariableDeclarators(vars); + } + + @Override + public VariableDeclarator visitVariableDeclarator(VariableDeclaratorContext ctx) { + VariableDeclaratorId id = visitVariableDeclaratorId(ctx.variableDeclaratorId()); + return new VariableDeclarator(id); + } + + @Override + public VariableDeclaratorId visitVariableDeclaratorId(VariableDeclaratorIdContext ctx) { + String text = getText(ctx.identifier()); + int arrayDimension = ctx.arrayDeclarator().size(); + return new VariableDeclaratorId(text, arrayDimension); + } + + @Override + public ClassOrInterfaceType visitClassOrInterfaceType(ClassOrInterfaceTypeContext ctx) { + List pairs = new ArrayList<>(); + for (IdentifierAndTypeArgumentsContext identifierAndTypeArgument : ctx.identifierAndTypeArguments()) { + String identifier = getText(identifierAndTypeArgument.identifier()); + TypeArguments typeArguments = null; + if (identifierAndTypeArgument.typeArguments() != null) { + typeArguments = visitTypeArguments(identifierAndTypeArgument.typeArguments()); + } + pairs.add(new IdentifyTypeArgumentsPair(identifier, typeArguments)); + } + return new ClassOrInterfaceType(pairs); + } + + @Override + public TypeParameters visitTypeParameters(TypeParametersContext ctx) { + List typeParameters = new ArrayList<>(); + for (TypeParameterContext typeParameterContext : ctx.typeParameter()) { + typeParameters.add(visitTypeParameter(typeParameterContext)); + } + return new TypeParameters(typeParameters); + } + + @Override + public TypeParameter visitTypeParameter(TypeParameterContext ctx) { + String identifier = getText(ctx.identifier()); + TypeBound typeBound = null; + if (ctx.typeBound() != null) { + typeBound = visitTypeBound(ctx.typeBound()); + } + return new TypeParameter(identifier, typeBound); + } + + @Override + public TypeBound visitTypeBound(TypeBoundContext ctx) { + List typeParameters = new ArrayList<>(); + for (TypeTypeContext typeTypeContext : ctx.typeType()) { + typeParameters.add(visitTypeType(typeTypeContext)); + } + return new TypeBound(typeParameters); + } + + @Override + public TypeArguments visitTypeArguments(TypeArgumentsContext ctx) { + List typeArguments = new ArrayList<>(); + for (TypeArgumentContext typeArgumentContext : ctx.typeArgument()) { + typeArguments.add(visitTypeArgument(typeArgumentContext)); + } + return new TypeArguments(typeArguments); + } + + @Override + public TypeArgument visitTypeArgument(TypeArgumentContext ctx) { + TypeType typeType = null; + ArgType argType = ArgType.UNKNOWN; + if (ctx.typeType() != null) { + typeType = visitTypeType(ctx.typeType()); + if (ctx.EXTENDS() != null) { + argType = ArgType.EXTENDS; + } else if (ctx.SUPER() != null) { + argType = ArgType.SUPER; + } else { + argType = ArgType.NORMAL; + } + } + return new TypeArgument(argType, typeType); + } + + @Override + public TypeType visitTypeType(TypeTypeContext ctx) { + ClassOrInterfaceType classOrInterfaceType = null; + if (ctx.classOrInterfaceType() != null) { + classOrInterfaceType = visitClassOrInterfaceType(ctx.classOrInterfaceType()); + } + String primitiveType = null; + if (ctx.primitiveType() != null) { + primitiveType = getText(ctx.primitiveType()); + } + return new TypeType(classOrInterfaceType, primitiveType); + } + + @Override + public QualifiedName visitQualifiedName(QualifiedNameContext ctx) { + List identifiers = new ArrayList<>(); + for (IdentifierContext identifierContext : ctx.identifier()) { + identifiers.add(getText(identifierContext)); + } + return new QualifiedName(identifiers); + } + + + /** merge modifiers, e.g public + static + final. */ + public ClassOrInterfaceModifier mergeModifiers(List contexts) { + int mod = 0; + for (ClassOrInterfaceModifierContext context : contexts) { + ClassOrInterfaceModifier modifier = visitClassOrInterfaceModifier(context); + mod |= modifier.mod; + } + return new ClassOrInterfaceModifier(mod); + } + + + /** create a type list. */ + public List getTypes(TypeListContext typeListContext) { + List types = new ArrayList<>(); + for (TypeTypeContext typeTypeContext : typeListContext.typeType()) { + types.add(visitTypeType(typeTypeContext)); + } + return types; + } + + + /** create a List by class and contexts. */ + public List visit(Class clazz, List contexts) { + List list = new ArrayList<>(); + for (C ctx : contexts) { + list.add((T) ctx.accept(this)); + } + return list; + } + + public String getText(IdentifierContext ctx) { + return ctx.getText(); + } + + public String getText(PrimitiveTypeContext ctx) { + return ctx.getText(); + } + + private void addTypeDeclaration(TypeDeclaration typeDeclaration) { + if (!childrenStack.isEmpty()) { + childrenStack.peek().add(typeDeclaration); + } else { + rootTypeDeclarations.add(typeDeclaration); + } + } + + + /** get full qualified name, e.g. OuterClassName.InnerClassName. */ + public static String getFullQualifiedName(Stack outerClassStack, + Optional packageName, String name) { + if (!outerClassStack.isEmpty()) { + return outerClassStack.peek() + "." + name; + } + return TypeDeclaration.getFullQualifiedName(packageName, name); + } + + class OuterClass { + public final Optional packageName; + public final String name; + + public OuterClass(Optional packageName, String name) { + this.packageName = packageName; + this.name = name; + } + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/LogicalBinaryPatternGenerator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/LogicalBinaryPatternGenerator.java new file mode 100644 index 0000000000..52d847c41f --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/LogicalBinaryPatternGenerator.java @@ -0,0 +1,65 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +import org.apache.doris.nereids.pattern.generator.javaast.ClassDeclaration; + +import java.util.Set; +import java.util.TreeSet; + +/** used to generate pattern for LogicalBinaryOperator. */ +public class LogicalBinaryPatternGenerator extends PatternGenerator { + + public LogicalBinaryPatternGenerator(PatternGeneratorAnalyzer analyzer, + ClassDeclaration opType, Set parentClass) { + super(analyzer, opType, parentClass); + } + + @Override + public String genericType() { + return ", Plan>"; + } + + @Override + public String genericTypeWithChildren() { + return ", Plan>"; + } + + @Override + public Set getImports() { + Set imports = new TreeSet<>(); + imports.add(opType.getFullQualifiedName()); + imports.add("org.apache.doris.nereids.operators.OperatorType"); + imports.add("org.apache.doris.nereids.trees.plans.Plan"); + imports.add("org.apache.doris.nereids.trees.plans.logical.LogicalBinaryPlan"); + enumFieldPatternInfos.stream() + .map(info -> info.enumFullName) + .forEach(imports::add); + return imports; + } + + @Override + public boolean isLogical() { + return true; + } + + @Override + public int childrenNum() { + return 2; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/LogicalLeafPatternGenerator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/LogicalLeafPatternGenerator.java new file mode 100644 index 0000000000..f84acdb403 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/LogicalLeafPatternGenerator.java @@ -0,0 +1,82 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +import org.apache.doris.nereids.pattern.generator.javaast.ClassDeclaration; + +import java.util.Set; +import java.util.TreeSet; + +/** used to generate pattern for LogicalLeafOperator. */ +public class LogicalLeafPatternGenerator extends PatternGenerator { + + public LogicalLeafPatternGenerator(PatternGeneratorAnalyzer analyzer, + ClassDeclaration opType, Set parentClass) { + super(analyzer, opType, parentClass); + } + + @Override + public String generate() { + String opClassName = opType.name; + String methodName = getPatternMethodName(); + + String patternParam = ", Plan>"; + + generateTypePattern(methodName, opClassName, patternParam, "", false); + + for (EnumFieldPatternInfo info : enumFieldPatternInfos) { + String predicate = ".when(p -> p.operator." + info.enumInstanceGetter + "() == " + + info.enumType + "." + info.enumInstance + ")"; + generateTypePattern(info.patternName, opClassName, patternParam, predicate, false); + } + + return generatePatterns(); + } + + @Override + public String genericType() { + return ", Plan>"; + } + + @Override + public String genericTypeWithChildren() { + throw new IllegalStateException("Can not get children generic type by LogicalLeafPlan"); + } + + @Override + public Set getImports() { + Set imports = new TreeSet<>(); + imports.add(opType.getFullQualifiedName()); + imports.add("org.apache.doris.nereids.trees.plans.Plan"); + imports.add("org.apache.doris.nereids.trees.plans.logical.LogicalLeafPlan"); + enumFieldPatternInfos.stream() + .map(info -> info.enumFullName) + .forEach(imports::add); + return imports; + } + + @Override + public boolean isLogical() { + return true; + } + + @Override + public int childrenNum() { + return 0; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/LogicalUnaryPatternGenerator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/LogicalUnaryPatternGenerator.java new file mode 100644 index 0000000000..c86b24456f --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/LogicalUnaryPatternGenerator.java @@ -0,0 +1,65 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +import org.apache.doris.nereids.pattern.generator.javaast.ClassDeclaration; + +import java.util.Set; +import java.util.TreeSet; + +/** used to generate pattern for LogicalUnaryOperator. */ +public class LogicalUnaryPatternGenerator extends PatternGenerator { + + public LogicalUnaryPatternGenerator(PatternGeneratorAnalyzer analyzer, + ClassDeclaration opType, Set parentClass) { + super(analyzer, opType, parentClass); + } + + @Override + public String genericType() { + return ", Plan>"; + } + + @Override + public String genericTypeWithChildren() { + return ", Plan>"; + } + + @Override + public Set getImports() { + Set imports = new TreeSet<>(); + imports.add(opType.getFullQualifiedName()); + imports.add("org.apache.doris.nereids.operators.OperatorType"); + imports.add("org.apache.doris.nereids.trees.plans.Plan"); + imports.add("org.apache.doris.nereids.trees.plans.logical.LogicalUnaryPlan"); + enumFieldPatternInfos.stream() + .map(info -> info.enumFullName) + .forEach(imports::add); + return imports; + } + + @Override + public boolean isLogical() { + return true; + } + + @Override + public int childrenNum() { + return 1; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternDescribable.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternDescribable.java new file mode 100644 index 0000000000..256278ffde --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternDescribable.java @@ -0,0 +1,29 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** used to trigger PatternDescribableProcessor. */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.SOURCE) +public @interface PatternDescribable { +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternDescribableProcessPoint.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternDescribableProcessPoint.java new file mode 100644 index 0000000000..1e93a8103c --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternDescribableProcessPoint.java @@ -0,0 +1,23 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +/** just for process entry point. */ +@PatternDescribable +public class PatternDescribableProcessPoint { +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternDescribableProcessor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternDescribableProcessor.java new file mode 100644 index 0000000000..295d3da2af --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternDescribableProcessor.java @@ -0,0 +1,157 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +import org.apache.doris.nereids.JavaLexer; +import org.apache.doris.nereids.JavaParser; +import org.apache.doris.nereids.pattern.generator.javaast.TypeDeclaration; + +import com.google.common.base.Throwables; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.DefaultErrorStrategy; +import org.antlr.v4.runtime.InputMismatchException; +import org.antlr.v4.runtime.Parser; +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.RecognitionException; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.atn.PredictionMode; +import org.antlr.v4.runtime.misc.ParseCancellationException; +import org.apache.commons.io.FileUtils; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.ProcessingEnvironment; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.TypeElement; +import javax.tools.Diagnostic.Kind; +import javax.tools.StandardLocation; + +/** + * annotation processor for generate GeneratedPattern.java. + */ +@SupportedSourceVersion(SourceVersion.RELEASE_8) +@SupportedAnnotationTypes("org.apache.doris.nereids.pattern.generator.PatternDescribable") +public class PatternDescribableProcessor extends AbstractProcessor { + private List operatorPaths; + + @Override + public synchronized void init(ProcessingEnvironment processingEnv) { + super.init(processingEnv); + this.operatorPaths = Arrays.stream(processingEnv.getOptions().get("operatorPath").split(",")) + .map(path -> path.trim()) + .filter(path -> !path.isEmpty()) + .collect(Collectors.toSet()) + .stream() + .map(path -> new File(path)) + .collect(Collectors.toList()); + } + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + if (annotations.isEmpty()) { + return false; + } + try { + List operatorFiles = findJavaFiles(operatorPaths); + PatternGeneratorAnalyzer patternGeneratorAnalyzer = new PatternGeneratorAnalyzer(); + for (File file : operatorFiles) { + List asts = parseJavaFile(file); + patternGeneratorAnalyzer.addAsts(asts); + } + String generatePatternCode = patternGeneratorAnalyzer.generatePatterns(); + File generatePatternFile = new File(processingEnv.getFiler() + .getResource(StandardLocation.SOURCE_OUTPUT, "org.apache.doris.nereids.pattern", + "GeneratedPatterns.java").toUri()); + if (generatePatternFile.exists()) { + generatePatternFile.delete(); + } + if (!generatePatternFile.getParentFile().exists()) { + generatePatternFile.getParentFile().mkdirs(); + } + + // bypass create file for processingEnv.getFiler(), compile GeneratePatterns in next compile term + try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(generatePatternFile))) { + bufferedWriter.write(generatePatternCode); + } + } catch (Throwable t) { + String exceptionMsg = Throwables.getStackTraceAsString(t); + processingEnv.getMessager().printMessage(Kind.ERROR, + "Analyze and generate patterns failed:\n" + exceptionMsg); + } + return false; + } + + private List findJavaFiles(List dirs) { + List files = new ArrayList<>(); + for (File dir : dirs) { + files.addAll(FileUtils.listFiles(dir, new String[] {"java"}, true)); + } + return files; + } + + private List parseJavaFile(File javaFile) throws IOException { + String javaCodeString = FileUtils.readFileToString(javaFile, StandardCharsets.UTF_8); + JavaLexer lexer = new JavaLexer(CharStreams.fromString(javaCodeString)); + + CommonTokenStream tokenStream = new CommonTokenStream(lexer); + JavaParser parser = new JavaParser(tokenStream); + parser.setErrorHandler(new DefaultErrorStrategy() { + @Override + public Token recoverInline(Parser recognizer) throws RecognitionException { + if (nextTokensContext == null) { + throw new InputMismatchException(recognizer); + } else { + throw new InputMismatchException(recognizer, nextTokensState, nextTokensContext); + } + } + }); + + // parser.addParseListener(PostProcessor) + // parser.removeErrorListeners() + // parser.addErrorListener(ParseErrorListener) + + ParserRuleContext tree; + try { + // first, try parsing with potentially faster SLL mode + parser.getInterpreter().setPredictionMode(PredictionMode.SLL); + tree = parser.compilationUnit(); + } catch (ParseCancellationException ex) { + // if we fail, parse with LL mode + tokenStream.seek(0); // rewind input stream + parser.reset(); + + parser.getInterpreter().setPredictionMode(PredictionMode.LL); + tree = parser.compilationUnit(); + } + + return new JavaAstBuilder().build(tree); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternGenerator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternGenerator.java new file mode 100644 index 0000000000..1048fdf6e6 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternGenerator.java @@ -0,0 +1,325 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +import org.apache.doris.nereids.pattern.generator.javaast.ClassDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.EnumConstant; +import org.apache.doris.nereids.pattern.generator.javaast.EnumDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.FieldDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.MethodDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.VariableDeclarator; + +import com.google.common.base.Joiner; +import org.apache.commons.lang.math.IntRange; +import org.apache.commons.lang3.StringUtils; + +import java.util.AbstractMap.SimpleEntry; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** used to generate pattern by operator. */ +public abstract class PatternGenerator { + protected final PatternGeneratorAnalyzer analyzer; + protected final ClassDeclaration opType; + protected final Set parentClass; + protected final List enumFieldPatternInfos; + protected final List generatePatterns = new ArrayList<>(); + + /** constructor. */ + public PatternGenerator(PatternGeneratorAnalyzer analyzer, ClassDeclaration opType, Set parentClass) { + this.analyzer = analyzer; + this.opType = opType; + this.parentClass = parentClass; + this.enumFieldPatternInfos = getEnumFieldPatternInfos(); + } + + public abstract String genericType(); + + public abstract String genericTypeWithChildren(); + + public abstract Set getImports(); + + public abstract boolean isLogical(); + + public abstract int childrenNum(); + + public String getPatternMethodName() { + return opType.name.substring(0, 1).toLowerCase(Locale.ENGLISH) + opType.name.substring(1); + } + + /** generate code by generators and analyzer. */ + public static String generateCode(List generators, PatternGeneratorAnalyzer analyzer) { + String generateCode + = "// Licensed to the Apache Software Foundation (ASF) under one\n" + + "// or more contributor license agreements. See the NOTICE file\n" + + "// distributed with this work for additional information\n" + + "// regarding copyright ownership. The ASF licenses this file\n" + + "// to you under the Apache License, Version 2.0 (the\n" + + "// \"License\"); you may not use this file except in compliance\n" + + "// with the License. You may obtain a copy of the License at\n" + + "//\n" + + "// http://www.apache.org/licenses/LICENSE-2.0\n" + + "//\n" + + "// Unless required by applicable law or agreed to in writing,\n" + + "// software distributed under the License is distributed on an\n" + + "// \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n" + + "// KIND, either express or implied. See the License for the\n" + + "// specific language governing permissions and limitations\n" + + "// under the License.\n" + + "\n" + + "package org.apache.doris.nereids.pattern;\n" + + "\n" + + generateImports(generators) + + "\n"; + + generateCode += "public interface GeneratedPatterns extends Patterns {\n"; + generateCode += generators.stream() + .map(generator -> { + String patternMethods = generator.generate(); + // add indent + return Arrays.stream(patternMethods.split("\n")) + .map(line -> " " + line + "\n") + .collect(Collectors.joining("")); + }).collect(Collectors.joining("\n")); + return generateCode + "}\n"; + } + + protected List getEnumFieldPatternInfos() { + List enumFieldInfos = new ArrayList<>(); + for (Entry pair : findEnumFieldType()) { + FieldDeclaration fieldDecl = pair.getKey(); + EnumDeclaration enumDecl = pair.getValue(); + + Set enumClassNameParts = splitCase(enumDecl.name) + .stream() + .map(part -> part.toLowerCase(Locale.ENGLISH)) + .collect(Collectors.toSet()); + + for (VariableDeclarator varDecl : fieldDecl.variableDeclarators.variableDeclarators) { + String enumFieldName = varDecl.variableDeclaratorId.identifier; + Optional getter = findGetter(enumDecl.name, enumFieldName); + if (getter.isPresent()) { + for (EnumConstant constant : enumDecl.constants) { + String enumInstance = constant.identifier; + String enumPatternName = getEnumPatternName(enumInstance, enumClassNameParts) + opType.name; + + enumFieldInfos.add(new EnumFieldPatternInfo(enumPatternName, + enumDecl.getFullQualifiedName(), enumDecl.name, enumInstance, getter.get())); + } + } + } + + } + return enumFieldInfos; + } + + protected Optional findGetter(String type, String name) { + String getterName = "get" + name.substring(0, 1).toUpperCase(Locale.ENGLISH) + name.substring(1); + for (MethodDeclaration methodDecl : opType.methodDeclarations) { + if (methodDecl.typeTypeOrVoid.isVoid) { + continue; + } + if (methodDecl.typeTypeOrVoid.typeType.isPresent() + && methodDecl.typeTypeOrVoid.typeType.get().toString().equals(type)) { + if (methodDecl.identifier.equals(getterName) && methodDecl.paramNum == 0) { + return Optional.of(getterName); + } + } + } + return Optional.empty(); + } + + protected String getEnumPatternName(String enumInstance, Set enumClassNameParts) { + String[] instanceNameParts = enumInstance.split("_+"); + List newParts = new ArrayList<>(); + + boolean isFirst = true; + for (int i = 0; i < instanceNameParts.length; i++) { + String part = instanceNameParts[i].toLowerCase(Locale.ENGLISH); + // skip instanceNameParts, e.g. INNER_JOIN has two part: [inner and Join]. + // because 'Join' is the part of the 'JoinType' enum className, so skip 'Join' and return 'inner' + if (part.isEmpty() || enumClassNameParts.contains(part)) { + continue; + } + if (!isFirst) { + newParts.add(part.substring(0, 1).toUpperCase(Locale.ENGLISH) + part.substring(1)); + } else { + newParts.add(part.substring(0, 1).toLowerCase(Locale.ENGLISH) + part.substring(1)); + } + isFirst = false; + } + + return Joiner.on("").join(newParts); + } + + protected List> findEnumFieldType() { + return opType.fieldDeclarations + .stream() + .map(f -> new SimpleEntry<>(f, analyzer.getType(opType, f.type))) + .filter(pair -> pair.getValue().isPresent() && pair.getValue().get() instanceof EnumDeclaration) + .map(pair -> new SimpleEntry<>(pair.getKey(), (EnumDeclaration) (pair.getValue().get()))) + .collect(Collectors.toList()); + } + + // e.g. split PhysicalBroadcastHashJoin to [Physical, Broadcast, Hash, Join] + // e.g. split JoinType to [Join, Type] + protected List splitCase(String name) { + Pattern pattern = Pattern.compile("([A-Z]+[^A-Z]*)"); + Matcher matcher = pattern.matcher(name); + List parts = new ArrayList<>(); + while (matcher.find()) { + parts.add(matcher.group(0)); + } + return parts; + } + + /** create generator by operator's type. */ + public static Optional create(PatternGeneratorAnalyzer analyzer, + ClassDeclaration opType, Set parentClass) { + if (parentClass.contains("org.apache.doris.nereids.operators.plans.logical.LogicalLeafOperator")) { + return Optional.of(new LogicalLeafPatternGenerator(analyzer, opType, parentClass)); + } else if (parentClass.contains("org.apache.doris.nereids.operators.plans.logical.LogicalUnaryOperator")) { + return Optional.of(new LogicalUnaryPatternGenerator(analyzer, opType, parentClass)); + } else if (parentClass.contains("org.apache.doris.nereids.operators.plans.logical.LogicalBinaryOperator")) { + return Optional.of(new LogicalBinaryPatternGenerator(analyzer, opType, parentClass)); + } else if (parentClass.contains("org.apache.doris.nereids.operators.plans.physical.PhysicalLeafOperator")) { + return Optional.of(new PhysicalLeafPatternGenerator(analyzer, opType, parentClass)); + } else if (parentClass.contains("org.apache.doris.nereids.operators.plans.physical.PhysicalUnaryOperator")) { + return Optional.of(new PhysicalUnaryPatternGenerator(analyzer, opType, parentClass)); + } else if (parentClass.contains("org.apache.doris.nereids.operators.plans.physical.PhysicalBinaryOperator")) { + return Optional.of(new PhysicalBinaryPatternGenerator(analyzer, opType, parentClass)); + } else { + return Optional.empty(); + } + } + + private static String generateImports(List generators) { + Set imports = new HashSet<>(); + for (PatternGenerator generator : generators) { + imports.addAll(generator.getImports()); + } + List sortedImports = new ArrayList<>(imports); + sortedImports.sort(Comparator.naturalOrder()); + + return sortedImports.stream() + .map(it -> "import " + it + ";\n") + .collect(Collectors.joining("")); + } + + /** generate some pattern method code. */ + public String generate() { + String opClassName = opType.name; + String methodName = getPatternMethodName(); + + generateTypePattern(methodName, opClassName, genericType(), "", false); + if (childrenNum() > 0) { + generateTypePattern(methodName, opClassName, genericTypeWithChildren(), "", true); + } + + for (EnumFieldPatternInfo info : enumFieldPatternInfos) { + String predicate = ".when(p -> p.operator." + info.enumInstanceGetter + "() == " + + info.enumType + "." + info.enumInstance + ")"; + generateTypePattern(info.patternName, opClassName, genericType(), predicate, false); + if (childrenNum() > 0) { + generateTypePattern(info.patternName, opClassName, genericTypeWithChildren(), predicate, true); + } + } + return generatePatterns(); + } + + /** generate a pattern method code. */ + public String generateTypePattern(String patterName, String className, + String genericParam, String predicate, boolean specifyChildren) { + + int childrenNum = childrenNum(); + + if (specifyChildren) { + String methodGeneric = Arrays.stream(new IntRange(1, childrenNum).toArray()) + .mapToObj(i -> "C" + i + " extends Plan") + .collect(Collectors.joining(", ", "<", ">")); + + String methodParam = Arrays.stream(new IntRange(1, childrenNum).toArray()) + .mapToObj(i -> "PatternDescriptor child" + i) + .collect(Collectors.joining(", ")); + + String childrenPattern = Arrays.stream(new IntRange(1, childrenNum).toArray()) + .mapToObj(i -> "child" + i + ".pattern") + .collect(Collectors.joining(", ")); + if (childrenNum > 0) { + childrenPattern = ", " + childrenPattern; + } + + String pattern = "default " + methodGeneric + "\n" + + "PatternDescriptor" + genericParam + "\n" + + " " + patterName + "(" + methodParam + ") {\n" + + " return new PatternDescriptor" + genericParam + "(\n" + + " new TypePattern(" + className + ".class" + childrenPattern + "),\n" + + " defaultPromise()\n" + + " )" + predicate + ";\n" + + "}\n"; + generatePatterns.add(pattern); + return pattern; + } else { + String childrenPattern = StringUtils.repeat("Pattern.FIXED", ", ", childrenNum); + if (childrenNum > 0) { + childrenPattern = ", " + childrenPattern; + } + + String pattern = "default PatternDescriptor" + genericParam + " " + patterName + "() {\n" + + " return new PatternDescriptor" + genericParam + "(\n" + + " new TypePattern(" + className + ".class" + childrenPattern + "),\n" + + " defaultPromise()\n" + + " )" + predicate + ";\n" + + "}\n"; + generatePatterns.add(pattern); + return pattern; + } + } + + public String generatePatterns() { + return generatePatterns.stream().collect(Collectors.joining("\n")); + } + + static class EnumFieldPatternInfo { + public final String patternName; + public final String enumFullName; + public final String enumType; + public final String enumInstance; + public final String enumInstanceGetter; + + public EnumFieldPatternInfo(String patternName, String enumFullName, String enumType, + String enumInstance, String enumInstanceGetter) { + this.patternName = patternName; + this.enumFullName = enumFullName; + this.enumType = enumType; + this.enumInstance = enumInstance; + this.enumInstanceGetter = enumInstanceGetter; + } + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternGeneratorAnalyzer.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternGeneratorAnalyzer.java new file mode 100644 index 0000000000..ebc8b7a572 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PatternGeneratorAnalyzer.java @@ -0,0 +1,226 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +import org.apache.doris.nereids.pattern.generator.javaast.ClassDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.ClassOrInterfaceType; +import org.apache.doris.nereids.pattern.generator.javaast.EnumDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.IdentifyTypeArgumentsPair; +import org.apache.doris.nereids.pattern.generator.javaast.ImportDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.InterfaceDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.QualifiedName; +import org.apache.doris.nereids.pattern.generator.javaast.TypeDeclaration; +import org.apache.doris.nereids.pattern.generator.javaast.TypeType; + +import com.google.common.base.Joiner; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.IdentityHashMap; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * used to analyze operator class extends hierarchy and then generated pattern builder methods. + */ +public class PatternGeneratorAnalyzer { + private final Map name2Ast = new LinkedHashMap<>(); + private final IdentityHashMap ast2Name = new IdentityHashMap<>(); + private final IdentityHashMap> ast2Import = new IdentityHashMap<>(); + private final IdentityHashMap> parentClassMap = new IdentityHashMap<>(); + + /** add java AST. */ + public void addAsts(List typeDeclarations) { + for (TypeDeclaration typeDeclaration : typeDeclarations) { + addAst(Optional.empty(), typeDeclaration); + } + } + + /** generate pattern methods. */ + public String generatePatterns() { + analyzeImport(); + analyzeParentClass(); + return doGenerate(); + } + + Optional getType(TypeDeclaration typeDeclaration, TypeType type) { + String typeName = analyzeClass(new LinkedHashSet<>(), typeDeclaration, type); + if (typeName != null) { + TypeDeclaration ast = name2Ast.get(typeName); + return Optional.ofNullable(ast); + } + + return Optional.empty(); + } + + private String doGenerate() { + Map> planOpClassMap = parentClassMap.entrySet().stream() + .filter(kv -> kv.getValue().contains("org.apache.doris.nereids.operators.plans.PlanOperator")) + .filter(kv -> !Modifier.isAbstract(kv.getKey().modifiers.mod) + && kv.getKey() instanceof ClassDeclaration) + .collect(Collectors.toMap(kv -> (ClassDeclaration) kv.getKey(), kv -> kv.getValue())); + + List generators = planOpClassMap.entrySet() + .stream() + .map(kv -> PatternGenerator.create(this, kv.getKey(), kv.getValue())) + .filter(generator -> generator.isPresent()) + .map(Optional::get) + .sorted((g1, g2) -> { + // logical first + if (g1.isLogical() != g2.isLogical()) { + return g1.isLogical() ? -1 : 1; + } + // leaf first + if (g1.childrenNum() != g2.childrenNum()) { + return g1.childrenNum() - g2.childrenNum(); + } + // string dict sort + return g1.opType.name.compareTo(g2.opType.name); + }) + .collect(Collectors.toList()); + + return PatternGenerator.generateCode(generators, this); + } + + private void analyzeImport() { + for (TypeDeclaration typeDeclaration : name2Ast.values()) { + Map imports = new LinkedHashMap<>(); + for (ImportDeclaration importDeclaration : typeDeclaration.imports) { + QualifiedName name = importDeclaration.name; + if (!importDeclaration.isStatic && !importDeclaration.importAll + && name.suffix().isPresent()) { + String importName = Joiner.on(".").join(name.identifiers); + imports.put(name.suffix().get(), importName); + } + } + ast2Import.put(typeDeclaration, imports); + } + } + + private void analyzeParentClass() { + for (TypeDeclaration typeDeclaration : name2Ast.values()) { + analyzeParentClass(new LinkedHashSet<>(), typeDeclaration); + } + } + + private void analyzeParentClass(Set parentClasses, TypeDeclaration typeDeclaration) { + Set currentParentClasses = new LinkedHashSet<>(); + if (typeDeclaration instanceof InterfaceDeclaration) { + for (TypeType extendsType : ((InterfaceDeclaration) typeDeclaration).extendsTypes) { + analyzeClass(currentParentClasses, typeDeclaration, extendsType); + } + } else if (typeDeclaration instanceof EnumDeclaration) { + for (TypeType implementType : ((EnumDeclaration) typeDeclaration).implementTypes) { + analyzeClass(currentParentClasses, typeDeclaration, implementType); + } + } else if (typeDeclaration instanceof ClassDeclaration) { + ClassDeclaration classDeclaration = (ClassDeclaration) typeDeclaration; + if (classDeclaration.extendsType.isPresent()) { + analyzeClass(currentParentClasses, typeDeclaration, classDeclaration.extendsType.get()); + } + if (!classDeclaration.implementTypes.isEmpty()) { + for (TypeType implementType : classDeclaration.implementTypes) { + analyzeClass(currentParentClasses, typeDeclaration, implementType); + } + } + } + parentClassMap.put(typeDeclaration, currentParentClasses); + parentClasses.addAll(currentParentClasses); + } + + String analyzeClass(Set parentClasses, TypeDeclaration typeDeclaration, TypeType type) { + if (type.classOrInterfaceType.isPresent()) { + List identifiers = new ArrayList<>(); + ClassOrInterfaceType classOrInterfaceType = type.classOrInterfaceType.get(); + for (IdentifyTypeArgumentsPair identifyTypeArgument : classOrInterfaceType.identifyTypeArguments) { + identifiers.add(identifyTypeArgument.identifier); + } + String className = Joiner.on(".").join(identifiers); + + if (analyzeIfExist(parentClasses, className)) { + parentClasses.add(className); + return className; + } + Optional importName = findFullImportName(typeDeclaration, className); + if (importName.isPresent() && analyzeIfExist(parentClasses, importName.get())) { + parentClasses.add(importName.get()); + return importName.get(); + } + if (typeDeclaration.packageName.isPresent()) { + String currentPackageClass = Joiner.on(".") + .join(typeDeclaration.packageName.get().identifiers) + "." + className; + if (analyzeIfExist(parentClasses, currentPackageClass)) { + parentClasses.add(currentPackageClass); + return currentPackageClass; + } + } + parentClasses.add(className); + return className; + } else if (type.primitiveType.isPresent()) { + parentClasses.add(type.primitiveType.get()); + return type.primitiveType.get(); + } else { + return null; + } + } + + private boolean analyzeIfExist(Set parentClasses, String name) { + if (name2Ast.get(name) != null) { + analyzeParentClass(parentClasses, name2Ast.get(name)); + return true; + } else { + return false; + } + } + + private Optional findFullImportName(TypeDeclaration typeDeclaration, String name) { + Map name2FullName = ast2Import.get(typeDeclaration); + if (name2FullName != null && name2FullName.get(name) != null) { + return Optional.of(name2FullName.get(name)); + } + return Optional.empty(); + } + + private void addAst(Optional outerClassName, TypeDeclaration typeDeclaration) { + String nameWithOuterClass; + if (!outerClassName.isPresent()) { + nameWithOuterClass = typeDeclaration.name; + String fullQualifiedName = typeDeclaration.getFullQualifiedName(); + name2Ast.put(fullQualifiedName, typeDeclaration); + ast2Name.put(typeDeclaration, fullQualifiedName); + } else if (typeDeclaration.packageName.isPresent()) { + nameWithOuterClass = outerClassName.get() + "." + typeDeclaration.name; + String fullName = typeDeclaration.packageName.get() + "." + nameWithOuterClass; + name2Ast.put(fullName, typeDeclaration); + ast2Name.put(typeDeclaration, fullName); + } else { + nameWithOuterClass = outerClassName.get() + "." + typeDeclaration.name; + name2Ast.put(nameWithOuterClass, typeDeclaration); + ast2Name.put(typeDeclaration, nameWithOuterClass); + } + + for (TypeDeclaration child : typeDeclaration.children) { + addAst(Optional.of(nameWithOuterClass), child); + } + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PhysicalBinaryPatternGenerator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PhysicalBinaryPatternGenerator.java new file mode 100644 index 0000000000..a4a1a653c9 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PhysicalBinaryPatternGenerator.java @@ -0,0 +1,66 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +import org.apache.doris.nereids.pattern.generator.javaast.ClassDeclaration; + +import java.util.Set; +import java.util.TreeSet; + + +/** used to generate pattern for PhysicalBinaryOperator. */ +public class PhysicalBinaryPatternGenerator extends PatternGenerator { + + public PhysicalBinaryPatternGenerator(PatternGeneratorAnalyzer analyzer, + ClassDeclaration opType, Set parentClass) { + super(analyzer, opType, parentClass); + } + + @Override + public String genericType() { + return ", Plan>"; + } + + @Override + public String genericTypeWithChildren() { + return ", Plan>"; + } + + @Override + public Set getImports() { + Set imports = new TreeSet<>(); + imports.add(opType.getFullQualifiedName()); + imports.add("org.apache.doris.nereids.operators.OperatorType"); + imports.add("org.apache.doris.nereids.trees.plans.Plan"); + imports.add("org.apache.doris.nereids.trees.plans.physical.PhysicalBinaryPlan"); + enumFieldPatternInfos.stream() + .map(info -> info.enumFullName) + .forEach(imports::add); + return imports; + } + + @Override + public boolean isLogical() { + return false; + } + + @Override + public int childrenNum() { + return 2; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PhysicalLeafPatternGenerator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PhysicalLeafPatternGenerator.java new file mode 100644 index 0000000000..9ba34c3cd4 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PhysicalLeafPatternGenerator.java @@ -0,0 +1,64 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +import org.apache.doris.nereids.pattern.generator.javaast.ClassDeclaration; + +import java.util.Set; +import java.util.TreeSet; + +/** used to generate pattern for PhysicalLeafOperator. */ +public class PhysicalLeafPatternGenerator extends PatternGenerator { + + public PhysicalLeafPatternGenerator(PatternGeneratorAnalyzer analyzer, + ClassDeclaration opType, Set parentClass) { + super(analyzer, opType, parentClass); + } + + @Override + public String genericType() { + return ", Plan>"; + } + + @Override + public String genericTypeWithChildren() { + throw new IllegalStateException("Can not get children generic type by PhysicalLeafPlan"); + } + + @Override + public Set getImports() { + Set imports = new TreeSet<>(); + imports.add(opType.getFullQualifiedName()); + imports.add("org.apache.doris.nereids.trees.plans.Plan"); + imports.add("org.apache.doris.nereids.trees.plans.physical.PhysicalLeafPlan"); + enumFieldPatternInfos.stream() + .map(info -> info.enumFullName) + .forEach(imports::add); + return imports; + } + + @Override + public boolean isLogical() { + return false; + } + + @Override + public int childrenNum() { + return 0; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PhysicalUnaryPatternGenerator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PhysicalUnaryPatternGenerator.java new file mode 100644 index 0000000000..942006cb97 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/PhysicalUnaryPatternGenerator.java @@ -0,0 +1,65 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator; + +import org.apache.doris.nereids.pattern.generator.javaast.ClassDeclaration; + +import java.util.Set; +import java.util.TreeSet; + +/** used to generate pattern for PhysicalUnaryOperator. */ +public class PhysicalUnaryPatternGenerator extends PatternGenerator { + + public PhysicalUnaryPatternGenerator(PatternGeneratorAnalyzer analyzer, + ClassDeclaration opType, Set parentClass) { + super(analyzer, opType, parentClass); + } + + @Override + public String genericType() { + return ", Plan>"; + } + + @Override + public String genericTypeWithChildren() { + return ", Plan>"; + } + + @Override + public Set getImports() { + Set imports = new TreeSet<>(); + imports.add(opType.getFullQualifiedName()); + imports.add("org.apache.doris.nereids.operators.OperatorType"); + imports.add("org.apache.doris.nereids.trees.plans.Plan"); + imports.add("org.apache.doris.nereids.trees.plans.physical.PhysicalUnaryPlan"); + enumFieldPatternInfos.stream() + .map(info -> info.enumFullName) + .forEach(imports::add); + return imports; + } + + @Override + public boolean isLogical() { + return false; + } + + @Override + public int childrenNum() { + return 1; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ClassDeclaration.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ClassDeclaration.java new file mode 100644 index 0000000000..2f1bfa1333 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ClassDeclaration.java @@ -0,0 +1,88 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +import java.util.List; +import java.util.Optional; + + +/** java's class declaration. */ +public class ClassDeclaration extends TypeDeclaration { + public final Optional extendsType; + public final Optional typeParameters; + public final List implementTypes; + public final List fieldDeclarations; + public final List methodDeclarations; + + /** constructor. */ + public ClassDeclaration(QualifiedName packageName, List imports, + ClassOrInterfaceModifier modifier, String name, TypeParameters typeParameters, + TypeType extendsType, List implementTypes, + List fieldDeclarations, List methodDeclarations, + List children) { + super(packageName, imports, modifier, name, children); + this.typeParameters = Optional.ofNullable(typeParameters); + this.extendsType = Optional.ofNullable(extendsType); + this.implementTypes = ImmutableList.copyOf(implementTypes); + this.fieldDeclarations = ImmutableList.copyOf(fieldDeclarations); + this.methodDeclarations = ImmutableList.copyOf(methodDeclarations); + } + + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + if (packageName.isPresent()) { + buffer.append("package ").append(packageName.get()).append(";\n\n"); + } + + if (!imports.isEmpty()) { + for (ImportDeclaration importDeclaration : imports) { + buffer.append(importDeclaration).append("\n"); + } + buffer.append("\n"); + } + + String mod = modifiers.toString(); + if (!mod.isEmpty()) { + mod += " "; + } + buffer.append(mod).append("class ").append(name); + if (typeParameters.isPresent()) { + buffer.append(typeParameters.get()); + } + buffer.append(" "); + if (extendsType.isPresent()) { + buffer.append("extends ").append(extendsType.get()).append(" "); + } + if (!implementTypes.isEmpty()) { + buffer.append("implements ").append(Joiner.on(", ").join(implementTypes)).append(" "); + } + buffer.append("{\n"); + for (FieldDeclaration fieldDeclaration : fieldDeclarations) { + buffer.append(" ").append(fieldDeclaration).append("\n"); + } + for (MethodDeclaration methodDeclaration : methodDeclarations) { + buffer.append(" ").append(methodDeclaration).append("\n"); + } + buffer.append("}\n"); + return buffer.toString(); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ClassOrInterfaceModifier.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ClassOrInterfaceModifier.java new file mode 100644 index 0000000000..44fe5034b6 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ClassOrInterfaceModifier.java @@ -0,0 +1,57 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import com.google.common.base.Joiner; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; + + +/** java's class or interface's modifiers. */ +public class ClassOrInterfaceModifier implements JavaAstNode { + public final int mod; + + public ClassOrInterfaceModifier(int mod) { + this.mod = mod; + } + + @Override + public String toString() { + List modifiers = new ArrayList<>(3); + if (Modifier.isPublic(mod)) { + modifiers.add("public"); + } else if (Modifier.isProtected(mod)) { + modifiers.add("protected"); + } else if (Modifier.isPrivate(mod)) { + modifiers.add("private"); + } + + if (Modifier.isStatic(mod)) { + modifiers.add("static"); + } + + if (Modifier.isAbstract(mod)) { + modifiers.add("abstract"); + } else if (Modifier.isFinal(mod)) { + modifiers.add("final"); + } + return Joiner.on(" ").join(modifiers); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ClassOrInterfaceType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ClassOrInterfaceType.java new file mode 100644 index 0000000000..182c61645a --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ClassOrInterfaceType.java @@ -0,0 +1,38 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +import java.util.List; + + +/** java class or interface's type. */ +public class ClassOrInterfaceType implements JavaAstNode { + public final List identifyTypeArguments; + + public ClassOrInterfaceType(List identifyTypeArguments) { + this.identifyTypeArguments = ImmutableList.copyOf(identifyTypeArguments); + } + + @Override + public String toString() { + return Joiner.on(".").join(identifyTypeArguments); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/EnumConstant.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/EnumConstant.java new file mode 100644 index 0000000000..37867f4a25 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/EnumConstant.java @@ -0,0 +1,33 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + + +/** java enum instance. */ +public class EnumConstant implements JavaAstNode { + public final String identifier; + + public EnumConstant(String identifier) { + this.identifier = identifier; + } + + @Override + public String toString() { + return identifier; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/EnumDeclaration.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/EnumDeclaration.java new file mode 100644 index 0000000000..8a813e36db --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/EnumDeclaration.java @@ -0,0 +1,67 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +import java.util.List; + +/** java's enum declaration. */ +public class EnumDeclaration extends TypeDeclaration { + public final List implementTypes; + public final List constants; + + /** constructor. */ + public EnumDeclaration(QualifiedName packageName, List imports, + ClassOrInterfaceModifier modifier, String name, List implementTypes, + List constants, List children) { + super(packageName, imports, modifier, name, children); + this.implementTypes = ImmutableList.copyOf(implementTypes); + this.constants = ImmutableList.copyOf(constants); + } + + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + if (packageName.isPresent()) { + buffer.append("package ").append(packageName.get()).append(";\n\n"); + } + + if (!imports.isEmpty()) { + for (ImportDeclaration importDeclaration : imports) { + buffer.append(importDeclaration).append("\n"); + } + buffer.append("\n"); + } + String mod = modifiers.toString(); + if (!mod.isEmpty()) { + mod += " "; + } + buffer.append(mod).append("enum ").append(name).append(" "); + if (!implementTypes.isEmpty()) { + buffer.append("implements ").append(Joiner.on(", ").join(implementTypes)).append(" "); + } + buffer.append("{\n"); + if (!constants.isEmpty()) { + buffer.append(" ").append(Joiner.on(", ").join(constants)).append(";\n"); + } + buffer.append("}\n"); + return buffer.toString(); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/FieldDeclaration.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/FieldDeclaration.java new file mode 100644 index 0000000000..8650808296 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/FieldDeclaration.java @@ -0,0 +1,34 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +/** java class field declaration. */ +public class FieldDeclaration extends MemberDeclaration { + public final TypeType type; + public final VariableDeclarators variableDeclarators; + + public FieldDeclaration(TypeType type, VariableDeclarators variableDeclarators) { + this.type = type; + this.variableDeclarators = variableDeclarators; + } + + @Override + public String toString() { + return type + " " + variableDeclarators + ";"; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/IdentifyTypeArgumentsPair.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/IdentifyTypeArgumentsPair.java new file mode 100644 index 0000000000..5bde5b6893 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/IdentifyTypeArgumentsPair.java @@ -0,0 +1,41 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import java.util.Optional; + + +/** java's identify type and type arguments. */ +public class IdentifyTypeArgumentsPair implements JavaAstNode { + public final String identifier; + public final Optional typeArguments; + + public IdentifyTypeArgumentsPair(String identifier, TypeArguments typeArguments) { + this.identifier = identifier; + this.typeArguments = Optional.ofNullable(typeArguments); + } + + @Override + public String toString() { + if (typeArguments.isPresent()) { + return identifier + typeArguments.get(); + } else { + return identifier; + } + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ImportDeclaration.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ImportDeclaration.java new file mode 100644 index 0000000000..7672d8826f --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/ImportDeclaration.java @@ -0,0 +1,38 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + + +/** java import declaration. */ +public class ImportDeclaration implements JavaAstNode { + public final boolean isStatic; + public final QualifiedName name; + public final boolean importAll; + + /** import declaration constructor. */ + public ImportDeclaration(boolean isStatic, QualifiedName name, boolean importAll) { + this.isStatic = isStatic; + this.name = name; + this.importAll = importAll; + } + + @Override + public String toString() { + return "import " + (isStatic ? "static " : "") + name + (importAll ? ".*" : "") + ";"; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/InterfaceDeclaration.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/InterfaceDeclaration.java new file mode 100644 index 0000000000..d547d30aa1 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/InterfaceDeclaration.java @@ -0,0 +1,68 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +import java.util.List; +import java.util.Optional; + +/** java's interface declaration. */ +public class InterfaceDeclaration extends TypeDeclaration { + public final Optional typeParameters; + public final List extendsTypes; + + /** constructor. */ + public InterfaceDeclaration(QualifiedName packageName, List imports, + ClassOrInterfaceModifier modifier, String name, TypeParameters typeParameters, + List extendsTypes, List children) { + super(packageName, imports, modifier, name, children); + this.typeParameters = Optional.ofNullable(typeParameters); + this.extendsTypes = ImmutableList.copyOf(extendsTypes); + } + + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + if (packageName.isPresent()) { + buffer.append("package ").append(packageName.get()).append(";\n\n"); + } + + if (!imports.isEmpty()) { + for (ImportDeclaration importDeclaration : imports) { + buffer.append(importDeclaration).append("\n"); + } + buffer.append("\n"); + } + String mod = modifiers.toString(); + if (!mod.isEmpty()) { + mod += " "; + } + buffer.append(mod).append("interface ").append(name); + if (typeParameters.isPresent()) { + buffer.append(typeParameters.get()); + } + buffer.append(" "); + if (!extendsTypes.isEmpty()) { + buffer.append("extends ").append(Joiner.on(", ").join(extendsTypes)).append(" "); + } + buffer.append("{}"); + return buffer.toString(); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/JavaAstNode.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/JavaAstNode.java new file mode 100644 index 0000000000..ef76f0ee0f --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/JavaAstNode.java @@ -0,0 +1,23 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + + +/** java ast base interface. */ +public interface JavaAstNode { +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/MemberDeclaration.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/MemberDeclaration.java new file mode 100644 index 0000000000..13ef05eb1b --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/MemberDeclaration.java @@ -0,0 +1,21 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +/** java's member declaration. */ +public class MemberDeclaration implements JavaAstNode {} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/MethodDeclaration.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/MethodDeclaration.java new file mode 100644 index 0000000000..f3882fd9d3 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/MethodDeclaration.java @@ -0,0 +1,37 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +/** java's method declaration. */ +public class MethodDeclaration extends MemberDeclaration { + public final TypeTypeOrVoid typeTypeOrVoid; + public final String identifier; + public final int paramNum; + + /** constructor. */ + public MethodDeclaration(TypeTypeOrVoid typeTypeOrVoid, String identifier, int paramNum) { + this.typeTypeOrVoid = typeTypeOrVoid; + this.identifier = identifier; + this.paramNum = paramNum; + } + + @Override + public String toString() { + return typeTypeOrVoid.toString() + " " + identifier + "();"; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/QualifiedName.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/QualifiedName.java new file mode 100644 index 0000000000..40671a8c5e --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/QualifiedName.java @@ -0,0 +1,52 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +import java.util.List; +import java.util.Optional; + + +/** java's qualified name. */ +public class QualifiedName implements JavaAstNode { + public final List identifiers; + + public QualifiedName(List identifiers) { + this.identifiers = ImmutableList.copyOf(identifiers); + } + + public boolean suffixIs(String name) { + return !identifiers.isEmpty() && identifiers.get(identifiers.size() - 1).equals(name); + } + + /** get suffix name. */ + public Optional suffix() { + if (identifiers.isEmpty()) { + return Optional.empty(); + } else { + return Optional.of(identifiers.get(identifiers.size() - 1)); + } + } + + @Override + public String toString() { + return Joiner.on(".").join(identifiers); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeArgument.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeArgument.java new file mode 100644 index 0000000000..2a9983dfb7 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeArgument.java @@ -0,0 +1,53 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import java.util.Optional; + + +/** java generic type argument. */ +public class TypeArgument implements JavaAstNode { + /** generic type. */ + public enum ArgType { + NORMAL, EXTENDS, SUPER, UNKNOWN + } + + private final ArgType argType; + private final Optional typeType; + + public TypeArgument(ArgType argType, TypeType typeType) { + this.argType = argType; + this.typeType = Optional.ofNullable(typeType); + } + + @Override + public String toString() { + switch (argType) { + case NORMAL: + return typeType.get().toString(); + case EXTENDS: + return "? extends " + typeType.get(); + case SUPER: + return "? super " + typeType.get(); + case UNKNOWN: + return "?"; + default: + throw new UnsupportedOperationException("Unknown argument type: " + argType); + } + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeArguments.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeArguments.java new file mode 100644 index 0000000000..fc30706ba1 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeArguments.java @@ -0,0 +1,38 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +import java.util.List; + + +/** java's type arguments. */ +public class TypeArguments implements JavaAstNode { + public final List typeArguments; + + public TypeArguments(List typeArguments) { + this.typeArguments = ImmutableList.copyOf(typeArguments); + } + + @Override + public String toString() { + return "<" + Joiner.on(", ").join(typeArguments) + ">"; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeBound.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeBound.java new file mode 100644 index 0000000000..57b5f5ff51 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeBound.java @@ -0,0 +1,38 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +import java.util.List; + + +/** java's type bound. */ +public class TypeBound implements JavaAstNode { + public final List types; + + public TypeBound(List types) { + this.types = ImmutableList.copyOf(types); + } + + @Override + public String toString() { + return Joiner.on(" & ").join(types); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeDeclaration.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeDeclaration.java new file mode 100644 index 0000000000..5692e95aa2 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeDeclaration.java @@ -0,0 +1,56 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +import java.util.List; +import java.util.Optional; + + +/** java's type declaration. */ +public abstract class TypeDeclaration implements JavaAstNode { + public final Optional packageName; + public final List imports; + public final ClassOrInterfaceModifier modifiers; + public final String name; + public final List children; + + /** type declaration's constructor. */ + public TypeDeclaration(QualifiedName packageName, List imports, + ClassOrInterfaceModifier modifiers, String name, List children) { + this.packageName = Optional.ofNullable(packageName); + this.imports = ImmutableList.copyOf(imports); + this.modifiers = modifiers; + this.name = name; + this.children = ImmutableList.copyOf(children); + } + + public String getFullQualifiedName() { + return getFullQualifiedName(packageName, name); + } + + /** function to concat package name and type name. */ + public static String getFullQualifiedName(Optional packageName, String name) { + if (packageName.isPresent()) { + return Joiner.on(".").join(packageName.get().identifiers) + "." + name; + } + return name; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeParameter.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeParameter.java new file mode 100644 index 0000000000..3a482bdd8c --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeParameter.java @@ -0,0 +1,41 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import java.util.Optional; + + +/** java's type. */ +public class TypeParameter implements JavaAstNode { + public final String identifier; + public final Optional typeBound; + + public TypeParameter(String identifier, TypeBound typeBound) { + this.identifier = identifier; + this.typeBound = Optional.ofNullable(typeBound); + } + + @Override + public String toString() { + if (typeBound.isPresent()) { + return identifier + " extends " + typeBound.get(); + } else { + return identifier; + } + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeParameters.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeParameters.java new file mode 100644 index 0000000000..ca21ee55af --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeParameters.java @@ -0,0 +1,38 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +import java.util.List; + + +/** java's types. */ +public class TypeParameters implements JavaAstNode { + public final List typeParameters; + + public TypeParameters(List typeParameters) { + this.typeParameters = ImmutableList.copyOf(typeParameters); + } + + @Override + public String toString() { + return "<" + Joiner.on(", ").join(typeParameters) + ">"; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeType.java new file mode 100644 index 0000000000..82937c7898 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeType.java @@ -0,0 +1,40 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import java.util.Optional; + +/** java's type. */ +public class TypeType implements JavaAstNode { + public final Optional classOrInterfaceType; + public final Optional primitiveType; + + public TypeType(ClassOrInterfaceType classOrInterfaceType, String primitiveType) { + this.classOrInterfaceType = Optional.ofNullable(classOrInterfaceType); + this.primitiveType = Optional.ofNullable(primitiveType); + } + + @Override + public String toString() { + if (primitiveType.isPresent()) { + return primitiveType.get(); + } else { + return classOrInterfaceType.get().toString(); + } + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeTypeOrVoid.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeTypeOrVoid.java new file mode 100644 index 0000000000..85ec17146e --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/TypeTypeOrVoid.java @@ -0,0 +1,37 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import java.util.Optional; + + +/** java's type or void. */ +public class TypeTypeOrVoid implements JavaAstNode { + public final Optional typeType; + public final boolean isVoid; + + public TypeTypeOrVoid(TypeType typeType, boolean isVoid) { + this.typeType = Optional.ofNullable(typeType); + this.isVoid = isVoid; + } + + @Override + public String toString() { + return isVoid ? "void" : typeType.get().toString(); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/VariableDeclarator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/VariableDeclarator.java new file mode 100644 index 0000000000..54315dafe7 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/VariableDeclarator.java @@ -0,0 +1,33 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + + +/** java variable's declarator. */ +public class VariableDeclarator implements JavaAstNode { + public final VariableDeclaratorId variableDeclaratorId; + + public VariableDeclarator(VariableDeclaratorId variableDeclaratorId) { + this.variableDeclaratorId = variableDeclaratorId; + } + + @Override + public String toString() { + return variableDeclaratorId.toString(); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/VariableDeclaratorId.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/VariableDeclaratorId.java new file mode 100644 index 0000000000..24d3aee445 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/VariableDeclaratorId.java @@ -0,0 +1,37 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import org.apache.commons.lang3.StringUtils; + + +/** java's variable. */ +public class VariableDeclaratorId implements JavaAstNode { + public final String identifier; + public final int arrayDimension; + + public VariableDeclaratorId(String identifier, int arrayDimension) { + this.identifier = identifier; + this.arrayDimension = arrayDimension; + } + + @Override + public String toString() { + return identifier + StringUtils.repeat("[]", arrayDimension); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/VariableDeclarators.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/VariableDeclarators.java new file mode 100644 index 0000000000..7aca79e223 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/generator/javaast/VariableDeclarators.java @@ -0,0 +1,38 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.pattern.generator.javaast; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +import java.util.List; + + +/** java's variable declarators. */ +public class VariableDeclarators implements JavaAstNode { + public final List variableDeclarators; + + public VariableDeclarators(List variableDeclarators) { + this.variableDeclarators = ImmutableList.copyOf(variableDeclarators); + } + + @Override + public String toString() { + return Joiner.on(", ").join(variableDeclarators); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/OneRuleFactory.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/OneRuleFactory.java index 5f774a49c9..941d08ea94 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/OneRuleFactory.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/OneRuleFactory.java @@ -26,7 +26,7 @@ import java.util.List; /** * abstract class for all rule factories build one rule. */ -public abstract class OneRuleFactory implements RuleFactory { +public abstract class OneRuleFactory> implements RuleFactory { @Override public final List> buildRules() { return ImmutableList.of(build()); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/Rule.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/Rule.java index f618b67999..024bebfc3c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/Rule.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/Rule.java @@ -28,9 +28,9 @@ import java.util.List; /** * Abstract class for all rules. */ -public abstract class Rule { +public abstract class Rule> { private final RuleType ruleType; - private final Pattern pattern; + private final Pattern pattern; private final RulePromise rulePromise; /** @@ -40,7 +40,7 @@ public abstract class Rule { * @param pattern target pattern of rule * @param rulePromise rule promise */ - public Rule(RuleType ruleType, Pattern pattern, RulePromise rulePromise) { + public Rule(RuleType ruleType, Pattern pattern, RulePromise rulePromise) { this.ruleType = ruleType; this.pattern = pattern; this.rulePromise = rulePromise; @@ -54,7 +54,7 @@ public abstract class Rule { return rulePromise; } - public Pattern getPattern() { + public Pattern getPattern() { return pattern; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleFactory.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleFactory.java index dd1e991b59..faadbe87de 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleFactory.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleFactory.java @@ -17,7 +17,7 @@ package org.apache.doris.nereids.rules; -import org.apache.doris.nereids.pattern.Patterns; +import org.apache.doris.nereids.pattern.GeneratedPatterns; import org.apache.doris.nereids.trees.TreeNode; import java.util.List; @@ -25,7 +25,7 @@ import java.util.List; /** * interface for all rule factories for build some rules. */ -public interface RuleFactory extends Patterns { +public interface RuleFactory> extends GeneratedPatterns { // need implement List> buildRules(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleSet.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleSet.java index 33439b5094..13dfed0722 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleSet.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleSet.java @@ -62,7 +62,7 @@ public class RuleSet { return new RuleFactories(); } - private static class RuleFactories { + private static class RuleFactories> { final Builder> rules = ImmutableList.builder(); public RuleFactories add(RuleFactory ruleFactory) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchange.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchange.java index 775423a76f..57be65087c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchange.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinExchange.java @@ -22,7 +22,7 @@ import org.apache.doris.nereids.rules.Rule; import org.apache.doris.nereids.rules.RuleType; import org.apache.doris.nereids.rules.exploration.OneExplorationRuleFactory; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.logical.LogicalBinary; +import org.apache.doris.nereids.trees.plans.logical.LogicalBinaryPlan; /** @@ -39,8 +39,8 @@ public class JoinExchange extends OneExplorationRuleFactory { @Override public Rule build() { return innerLogicalJoin(innerLogicalJoin(), innerLogicalJoin()).then(topJoin -> { - LogicalBinary leftJoin = topJoin.left(); - LogicalBinary rightJoin = topJoin.right(); + LogicalBinaryPlan leftJoin = topJoin.left(); + LogicalBinaryPlan rightJoin = topJoin.right(); Plan a = leftJoin.left(); Plan b = leftJoin.right(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinLAsscom.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinLAsscom.java index 1e30c64aea..399b3d0d02 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinLAsscom.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/join/JoinLAsscom.java @@ -22,7 +22,7 @@ import org.apache.doris.nereids.rules.Rule; import org.apache.doris.nereids.rules.RuleType; import org.apache.doris.nereids.rules.exploration.OneExplorationRuleFactory; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.logical.LogicalBinary; +import org.apache.doris.nereids.trees.plans.logical.LogicalBinaryPlan; /** * Rule for change inner join left associative to right. @@ -38,7 +38,7 @@ public class JoinLAsscom extends OneExplorationRuleFactory { @Override public Rule build() { return innerLogicalJoin(innerLogicalJoin(), any()).then(topJoin -> { - LogicalBinary bottomJoin = topJoin.left(); + LogicalBinaryPlan bottomJoin = topJoin.left(); Plan a = bottomJoin.left(); Plan b = bottomJoin.right(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/AbstractTreeNode.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/AbstractTreeNode.java index e2b38eff5f..16cbaf9f44 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/AbstractTreeNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/AbstractTreeNode.java @@ -24,6 +24,7 @@ import org.apache.doris.nereids.operators.Operator; import com.google.common.collect.ImmutableList; import java.util.List; +import java.util.Optional; /** * Abstract class for plan node in Nereids, include plan node and expression. @@ -31,17 +32,17 @@ import java.util.List; * @param either {@link org.apache.doris.nereids.trees.plans.Plan} * or {@link org.apache.doris.nereids.trees.expressions.Expression} */ -public abstract class AbstractTreeNode> +public abstract class AbstractTreeNode> implements TreeNode { protected final NodeType type; - protected final List children; + protected final List children; // TODO: Maybe we should use a GroupPlan to avoid TreeNode hold the GroupExpression. // https://github.com/apache/incubator-doris/pull/9807#discussion_r884829067 - protected final GroupExpression groupExpression; + protected final Optional groupExpression; - public AbstractTreeNode(NodeType type, TreeNode... children) { + public AbstractTreeNode(NodeType type, NODE_TYPE... children) { this(type, null, children); } @@ -52,10 +53,10 @@ public abstract class AbstractTreeNode getGroupExpression() { return groupExpression; } @Override - public NODE_TYPE newChildren(List children) { - throw new RuntimeException(); + public NODE_TYPE child(int index) { + return children.get(index); + } + + @Override + public List children() { + return children; } @Override @@ -78,16 +84,6 @@ public abstract class AbstractTreeNode List children() { - return (List) children; - } - - @Override - public CHILD_TYPE child(int index) { - return (CHILD_TYPE) children.get(index); - } - public int arity() { return children.size(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/BinaryNode.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/BinaryNode.java index 920a3cc8fd..c182fdc2f6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/BinaryNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/BinaryNode.java @@ -21,17 +21,17 @@ package org.apache.doris.nereids.trees; * interface for all tree node that have two children. */ public interface BinaryNode< - NODE_TYPE extends BinaryNode, + NODE_TYPE extends TreeNode, LEFT_CHILD_TYPE extends TreeNode, RIGHT_CHILD_TYPE extends TreeNode> extends TreeNode { default LEFT_CHILD_TYPE left() { - return child(0); + return (LEFT_CHILD_TYPE) child(0); } default RIGHT_CHILD_TYPE right() { - return child(1); + return (RIGHT_CHILD_TYPE) child(1); } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/LeafNode.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/LeafNode.java index f2bfa75b5f..a40dffe14f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/LeafNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/LeafNode.java @@ -20,7 +20,7 @@ package org.apache.doris.nereids.trees; /** * Abstract class for all tree node that have no child. */ -public interface LeafNode> extends TreeNode { +public interface LeafNode> extends TreeNode { @Override default int arity() { return 0; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/TreeNode.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/TreeNode.java index 075b155516..12a8597782 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/TreeNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/TreeNode.java @@ -21,6 +21,7 @@ import org.apache.doris.nereids.memo.GroupExpression; import org.apache.doris.nereids.operators.Operator; import java.util.List; +import java.util.Optional; /** * interface for all node in Nereids, include plan node and expression. @@ -32,15 +33,16 @@ public interface TreeNode> { Operator getOperator(); - GroupExpression getGroupExpression(); + // cache GroupExpression for fast exit from Memo.copyIn. + Optional getGroupExpression(); NodeType getType(); - List children(); + List children(); - CHILD_TYPE child(int index); + NODE_TYPE child(int index); int arity(); - NODE_TYPE newChildren(List children); + NODE_TYPE newChildren(List children); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/UnaryNode.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/UnaryNode.java index 1aed540e60..b173cfdc8c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/UnaryNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/UnaryNode.java @@ -20,13 +20,11 @@ package org.apache.doris.nereids.trees; /** * interface for all tree node that have one child. */ -public interface UnaryNode< - NODE_TYPE extends UnaryNode, - CHILD_TYPE extends TreeNode> +public interface UnaryNode, CHILD_TYPE extends TreeNode> extends TreeNode { default CHILD_TYPE child() { - return child(0); + return (CHILD_TYPE) child(0); } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java index a9b46ae80c..425c3b0bc9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java @@ -27,8 +27,8 @@ import java.util.List; /** * Expression for alias, such as col1 as c1. */ -public class Alias extends NamedExpression> - implements UnaryExpression, CHILD_TYPE> { +public class Alias extends NamedExpression + implements UnaryExpression { private final ExprId exprId; private final String name; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BinaryExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BinaryExpression.java index b031de9d0f..d29a182f4c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BinaryExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BinaryExpression.java @@ -23,19 +23,18 @@ import org.apache.doris.nereids.trees.BinaryNode; * Interface for all expression that have two children. */ public interface BinaryExpression< - EXPR_TYPE extends BinaryExpression, LEFT_CHILD_TYPE extends Expression, RIGHT_CHILD_TYPE extends Expression> - extends BinaryNode { + extends BinaryNode { @Override default LEFT_CHILD_TYPE left() { - return child(0); + return (LEFT_CHILD_TYPE) child(0); } @Override default RIGHT_CHILD_TYPE right() { - return child(1); + return (RIGHT_CHILD_TYPE) child(1); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ComparisonPredicate.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ComparisonPredicate.java index 88fc03c2f9..6f5ae18fe2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ComparisonPredicate.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ComparisonPredicate.java @@ -30,8 +30,7 @@ import java.util.Objects; * Such as: "=", "<", "<=", ">", ">=", "<=>" */ public class ComparisonPredicate - extends Expression> implements - BinaryExpression, LEFT_CHILD_TYPE, RIGHT_CHILD_TYPE> { + extends Expression implements BinaryExpression { /** * Constructor of ComparisonPredicate. diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/EqualTo.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/EqualTo.java index 3f66da9106..4f561e525f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/EqualTo.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/EqualTo.java @@ -19,7 +19,6 @@ package org.apache.doris.nereids.trees.expressions; import org.apache.doris.nereids.exceptions.UnboundException; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import com.google.common.base.Preconditions; @@ -46,8 +45,8 @@ public class EqualTo children) { + public EqualTo newChildren(List children) { Preconditions.checkArgument(children.size() == 2); - return new EqualTo((Expression) children.get(0), (Expression) children.get(1)); + return new EqualTo<>(children.get(0), children.get(1)); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java index b52c58b023..7a275e4b2b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java @@ -21,7 +21,6 @@ import org.apache.doris.nereids.exceptions.UnboundException; import org.apache.doris.nereids.rules.expression.rewrite.ExpressionVisitor; import org.apache.doris.nereids.trees.AbstractTreeNode; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import org.apache.doris.nereids.types.DataType; import java.util.List; @@ -29,8 +28,7 @@ import java.util.List; /** * Abstract class for all Expression in Nereids. */ -public abstract class Expression> - extends AbstractTreeNode { +public abstract class Expression extends AbstractTreeNode { public Expression(NodeType type, Expression... children) { super(type, children); @@ -63,7 +61,7 @@ public abstract class Expression> } @Override - public EXPR_TYPE newChildren(List children) { + public Expression newChildren(List children) { throw new RuntimeException(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/FunctionCallExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/FunctionCallExpression.java index 88e4bc6ba5..fd172cd429 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/FunctionCallExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/FunctionCallExpression.java @@ -28,13 +28,13 @@ import java.util.stream.Collectors; /** * Temp definition of FunctionCallExpression. */ -public class FunctionCallExpression extends Expression { +public class FunctionCallExpression extends Expression { - private FunctionName functionName; + private final FunctionName functionName; - private List params; + private final List params; - private Function fn; + private final Function fn; /** * Constructor of FunctionCallExpression. diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/GreaterThan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/GreaterThan.java index 12497ff94e..7e0e291366 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/GreaterThan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/GreaterThan.java @@ -19,7 +19,6 @@ package org.apache.doris.nereids.trees.expressions; import org.apache.doris.nereids.exceptions.UnboundException; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import com.google.common.base.Preconditions; @@ -51,8 +50,8 @@ public class GreaterThan children) { + public GreaterThan newChildren(List children) { Preconditions.checkArgument(children.size() == 2); - return new GreaterThan((Expression) children.get(0), (Expression) children.get(1)); + return new GreaterThan<>(children.get(0), children.get(1)); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/GreaterThanEqual.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/GreaterThanEqual.java index e20499f6c2..2ca42bbb26 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/GreaterThanEqual.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/GreaterThanEqual.java @@ -19,7 +19,6 @@ package org.apache.doris.nereids.trees.expressions; import org.apache.doris.nereids.exceptions.UnboundException; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import com.google.common.base.Preconditions; @@ -51,8 +50,8 @@ public class GreaterThanEqual children) { + public GreaterThanEqual newChildren(List children) { Preconditions.checkArgument(children.size() == 2); - return new GreaterThanEqual((Expression) children.get(0), (Expression) children.get(1)); + return new GreaterThanEqual<>(children.get(0), children.get(1)); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LeafExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LeafExpression.java index 2c8aeb55c4..50fd0373f3 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LeafExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LeafExpression.java @@ -22,5 +22,5 @@ import org.apache.doris.nereids.trees.LeafNode; /** * Interface for all expression that have no child. */ -public interface LeafExpression> extends LeafNode { +public interface LeafExpression extends LeafNode { } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LessThan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LessThan.java index 3e97d42102..3cdaa4abaf 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LessThan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LessThan.java @@ -19,7 +19,6 @@ package org.apache.doris.nereids.trees.expressions; import org.apache.doris.nereids.exceptions.UnboundException; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import com.google.common.base.Preconditions; @@ -51,8 +50,8 @@ public class LessThan children) { + public LessThan newChildren(List children) { Preconditions.checkArgument(children.size() == 2); - return new LessThan((Expression) children.get(0), (Expression) children.get(1)); + return new LessThan<>(children.get(0), children.get(1)); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LessThanEqual.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LessThanEqual.java index 2a759a4973..f38dc8a8c1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LessThanEqual.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/LessThanEqual.java @@ -19,7 +19,6 @@ package org.apache.doris.nereids.trees.expressions; import org.apache.doris.nereids.exceptions.UnboundException; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import com.google.common.base.Preconditions; @@ -51,8 +50,8 @@ public class LessThanEqual children) { + public LessThanEqual newChildren(List children) { Preconditions.checkArgument(children.size() == 2); - return new LessThanEqual((Expression) children.get(0), (Expression) children.get(1)); + return new LessThanEqual<>(children.get(0), children.get(1)); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Literal.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Literal.java index 244d523fb6..66872018f6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Literal.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Literal.java @@ -33,7 +33,7 @@ import java.util.Objects; /** * All data type literal expression in Nereids. */ -public class Literal extends Expression implements LeafExpression { +public class Literal extends Expression implements LeafExpression { private final DataType dataType; private final Object value; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/NamedExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/NamedExpression.java index c3f796a066..8df2861bd9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/NamedExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/NamedExpression.java @@ -27,7 +27,7 @@ import java.util.List; /** * Expression in Nereids that having name. */ -public abstract class NamedExpression> extends Expression { +public abstract class NamedExpression extends Expression { public NamedExpression(NodeType type, Expression... children) { super(type, children); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Not.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Not.java index ac512a9242..a7a38e56ea 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Not.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Not.java @@ -20,7 +20,6 @@ package org.apache.doris.nereids.trees.expressions; import org.apache.doris.nereids.exceptions.UnboundException; import org.apache.doris.nereids.rules.expression.rewrite.ExpressionVisitor; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import com.google.common.base.Preconditions; @@ -30,8 +29,9 @@ import java.util.Objects; /** * Not expression: not a. */ -public class Not extends Expression> - implements UnaryExpression, CHILD_TYPE> { +public class Not extends Expression + implements UnaryExpression { + public Not(CHILD_TYPE child) { super(NodeType.NOT, child); } @@ -47,9 +47,9 @@ public class Not extends Expression children) { + public Not newChildren(List children) { Preconditions.checkArgument(children.size() == 1); - return new Not((Expression) children.get(0)); + return new Not<>(children.get(0)); } @Override @@ -60,7 +60,7 @@ public class Not extends Expression other = (Not) o; return Objects.equals(child(), other.child()); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Slot.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Slot.java index 3143b807f0..c235dd19e7 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Slot.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Slot.java @@ -22,8 +22,7 @@ import org.apache.doris.nereids.trees.NodeType; /** * Abstract class for all slot in expression. */ -public abstract class Slot> extends NamedExpression - implements LeafExpression { +public abstract class Slot extends NamedExpression implements LeafExpression { private int id; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java index acecad51ad..dd64620688 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java @@ -29,7 +29,7 @@ import java.util.Objects; /** * Reference to slot in expression. */ -public class SlotReference extends Slot { +public class SlotReference extends Slot { private final ExprId exprId; private final String name; private final List qualifier; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/UnaryExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/UnaryExpression.java index ace6c53ba3..e84751147b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/UnaryExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/UnaryExpression.java @@ -22,11 +22,11 @@ import org.apache.doris.nereids.trees.UnaryNode; /** * Abstract class for all expression that have one child. */ -public interface UnaryExpression, - CHILD_TYPE extends Expression> extends UnaryNode { +public interface UnaryExpression + extends UnaryNode { @Override default CHILD_TYPE child() { - return child(0); + return (CHILD_TYPE) child(0); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/AbstractPlan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/AbstractPlan.java index 581cd5bab0..ff6cd109cc 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/AbstractPlan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/AbstractPlan.java @@ -30,14 +30,9 @@ import java.util.Objects; /** * Abstract class for all concrete plan node. - * - * @param either {@link org.apache.doris.nereids.trees.plans.logical.LogicalPlan} - * or {@link org.apache.doris.nereids.trees.plans.physical.PhysicalPlan} */ -public abstract class AbstractPlan< - PLAN_TYPE extends AbstractPlan, - OP_TYPE extends PlanOperator> - extends AbstractTreeNode implements Plan { +public abstract class AbstractPlan + extends AbstractTreeNode implements Plan { public final OP_TYPE operator; @@ -56,16 +51,6 @@ public abstract class AbstractPlan< return operator; } - @Override - public List children() { - return (List) children; - } - - @Override - public Plan child(int index) { - return (Plan) children.get(index); - } - /** * Get tree like string describing query plan. * diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/BinaryPlan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/BinaryPlan.java index 849f4f3e07..41e3c48201 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/BinaryPlan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/BinaryPlan.java @@ -20,31 +20,12 @@ package org.apache.doris.nereids.trees.plans; import org.apache.doris.nereids.operators.plans.BinaryPlanOperator; import org.apache.doris.nereids.trees.BinaryNode; -import java.util.List; - /** * interface for all plan that have two children. */ -public interface BinaryPlan< - PLAN_TYPE extends BinaryPlan, - OP_TYPE extends BinaryPlanOperator, - LEFT_CHILD_TYPE extends Plan, - RIGHT_CHILD_TYPE extends Plan> - extends Plan, BinaryNode { +public interface BinaryPlan + extends Plan, BinaryNode { @Override - List children(); - - @Override - Plan child(int index); - - @Override - default LEFT_CHILD_TYPE left() { - return BinaryNode.super.left(); - } - - @Override - default RIGHT_CHILD_TYPE right() { - return BinaryNode.super.right(); - } + BinaryPlanOperator getOperator(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/LeafPlan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/LeafPlan.java index 6e869ea65b..74df8c503c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/LeafPlan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/LeafPlan.java @@ -20,21 +20,13 @@ package org.apache.doris.nereids.trees.plans; import org.apache.doris.nereids.operators.plans.LeafPlanOperator; import org.apache.doris.nereids.trees.LeafNode; -import java.util.List; - /** * Abstract class for all plan node that have no child. */ -public interface LeafPlan< - PLAN_TYPE extends LeafPlan, - OP_TYPE extends LeafPlanOperator> - extends Plan, LeafNode { +public interface LeafPlan extends Plan, LeafNode { @Override - List children(); - - @Override - Plan child(int index); + LeafPlanOperator getOperator(); @Override default int arity() { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PhysicalPlanTranslator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PhysicalPlanTranslator.java index 3c4384b671..ca39c91cfa 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PhysicalPlanTranslator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PhysicalPlanTranslator.java @@ -26,8 +26,6 @@ import org.apache.doris.analysis.TupleDescriptor; import org.apache.doris.catalog.OlapTable; import org.apache.doris.catalog.Table; import org.apache.doris.nereids.PlanOperatorVisitor; -import org.apache.doris.nereids.operators.AbstractOperator; -import org.apache.doris.nereids.operators.Operator; import org.apache.doris.nereids.operators.plans.JoinType; import org.apache.doris.nereids.operators.plans.physical.PhysicalAggregation; import org.apache.doris.nereids.operators.plans.physical.PhysicalFilter; @@ -41,7 +39,10 @@ import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.expressions.ExpressionConverter; import org.apache.doris.nereids.trees.expressions.Slot; import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.plans.physical.PhysicalBinaryPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalLeafPlan; import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalUnaryPlan; import org.apache.doris.nereids.util.Utils; import org.apache.doris.planner.AggregationNode; import org.apache.doris.planner.CrossJoinNode; @@ -66,14 +67,13 @@ import java.util.stream.Collectors; @SuppressWarnings("rawtypes") public class PhysicalPlanTranslator extends PlanOperatorVisitor { - public void translatePlan(PhysicalPlan physicalPlan, - PlanContext context) { + public void translatePlan(PhysicalPlan physicalPlan, PlanContext context) { visit(physicalPlan, context); } @Override - public PlanFragment visit(Plan plan, PlanContext context) { - PhysicalOperator operator = (PhysicalOperator) plan.getOperator(); + public PlanFragment visit(Plan plan, PlanContext context) { + PhysicalOperator operator = (PhysicalOperator) plan.getOperator(); return operator.accept(this, plan, context); } @@ -84,15 +84,14 @@ public class PhysicalPlanTranslator extends PlanOperatorVisitor aggPlan, PlanContext context) { + PhysicalUnaryPlan aggPlan, PlanContext context) { - PlanFragment inputPlanFragment = visit( - (PhysicalPlan) aggPlan.child(0), context); + PlanFragment inputPlanFragment = visit(aggPlan.child(0), context); AggregationNode aggregationNode = null; List slotList = aggPlan.getOutput(); TupleDescriptor outputTupleDesc = generateTupleDesc(slotList, context, null); - PhysicalAggregation physicalAggregation = (PhysicalAggregation) aggPlan.getOperator(); + PhysicalAggregation physicalAggregation = aggPlan.getOperator(); AggregateInfo.AggPhase phase = physicalAggregation.getAggPhase().toExec(); List groupByExpressionList = physicalAggregation.getGroupByExprList(); @@ -136,7 +135,7 @@ public class PhysicalPlanTranslator extends PlanOperatorVisitor olapScanPlan, PlanContext context) { + PhysicalLeafPlan olapScanPlan, PlanContext context) { // Create OlapScanNode List slotList = olapScanPlan.getOutput(); PhysicalOlapScan physicalOlapScan = olapScanPlan.getOperator(); @@ -150,10 +149,9 @@ public class PhysicalPlanTranslator extends PlanOperatorVisitor sortPlan, + public PlanFragment visitPhysicalSortPlan(PhysicalUnaryPlan sortPlan, PlanContext context) { - PlanFragment childFragment = visit( - (PhysicalPlan) sortPlan.child(0), context); + PlanFragment childFragment = visit(sortPlan.child(0), context); PhysicalSort physicalSort = sortPlan.getOperator(); if (!childFragment.isPartitioned()) { return childFragment; @@ -203,11 +201,9 @@ public class PhysicalPlanTranslator extends PlanOperatorVisitor hashJoinPlan, PlanContext context) { - PlanFragment leftFragment = visit( - (PhysicalPlan) hashJoinPlan.child(0), context); - PlanFragment rightFragment = visit( - (PhysicalPlan) hashJoinPlan.child(0), context); + PhysicalBinaryPlan hashJoinPlan, PlanContext context) { + PlanFragment leftFragment = visit(hashJoinPlan.child(0), context); + PlanFragment rightFragment = visit(hashJoinPlan.child(0), context); PhysicalHashJoin physicalHashJoin = hashJoinPlan.getOperator(); Expression predicateExpr = physicalHashJoin.getPredicate(); List eqExprList = Utils.getEqConjuncts(hashJoinPlan.child(0).getOutput(), @@ -261,15 +257,14 @@ public class PhysicalPlanTranslator extends PlanOperatorVisitor projectPlan, PlanContext context) { - return visit((PhysicalPlan) projectPlan.child(0), context); + PhysicalUnaryPlan projectPlan, PlanContext context) { + return visit(projectPlan.child(0), context); } @Override - public PlanFragment visitPhysicalFilter(PhysicalPlan filterPlan, - PlanContext context) { - PlanFragment inputFragment = visit( - (PhysicalPlan) filterPlan.child(0), context); + public PlanFragment visitPhysicalFilter( + PhysicalUnaryPlan filterPlan, PlanContext context) { + PlanFragment inputFragment = visit(filterPlan.child(0), context); PlanNode planNode = inputFragment.getPlanRoot(); PhysicalFilter filter = filterPlan.getOperator(); Expression expression = filter.getPredicates(); @@ -316,7 +311,7 @@ public class PhysicalPlanTranslator extends PlanOperatorVisitor getGroupExpression() { + return Optional.empty(); } @Override @@ -44,12 +44,12 @@ public class PlaceHolderPlan implements LeafPlan { } @Override - public TreeNode newChildren(List children) { + public Plan newChildren(List children) { throw new RuntimeException(); } @Override - public PlanOperator getOperator() { + public LeafPlanOperator getOperator() { throw new RuntimeException(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plan.java index 77bf7ac625..ff7b26d617 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plan.java @@ -27,11 +27,9 @@ import java.util.List; /** * Abstract class for all plan node. */ -public interface Plan< - PLAN_TYPE extends Plan, - OP_TYPE extends PlanOperator> extends TreeNode { +public interface Plan extends TreeNode { - OP_TYPE getOperator(); + PlanOperator getOperator(); LogicalProperties getLogicalProperties(); @@ -39,10 +37,4 @@ public interface Plan< String treeString(); - @Override - List children(); - - @Override - Plan child(int index); - } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plans.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plans.java index 487d32a662..73447f2ae3 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plans.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plans.java @@ -24,12 +24,12 @@ import org.apache.doris.nereids.operators.plans.physical.PhysicalBinaryOperator; import org.apache.doris.nereids.operators.plans.physical.PhysicalLeafOperator; import org.apache.doris.nereids.operators.plans.physical.PhysicalUnaryOperator; import org.apache.doris.nereids.properties.LogicalProperties; -import org.apache.doris.nereids.trees.plans.logical.LogicalBinary; -import org.apache.doris.nereids.trees.plans.logical.LogicalLeaf; -import org.apache.doris.nereids.trees.plans.logical.LogicalUnary; -import org.apache.doris.nereids.trees.plans.physical.PhysicalBinary; -import org.apache.doris.nereids.trees.plans.physical.PhysicalLeaf; -import org.apache.doris.nereids.trees.plans.physical.PhysicalUnary; +import org.apache.doris.nereids.trees.plans.logical.LogicalBinaryPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalLeafPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnaryPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalBinaryPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalLeafPlan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalUnaryPlan; /** * An interface provided some builder of Plan. @@ -37,35 +37,35 @@ import org.apache.doris.nereids.trees.plans.physical.PhysicalUnary; * You can simply use the override plan function to build a plan by the operator type. */ public interface Plans { - default LogicalLeaf plan(OP_TYPE op) { - return new LogicalLeaf(op); + default LogicalLeafPlan plan(OP_TYPE op) { + return new LogicalLeafPlan(op); } - default LogicalUnary + default LogicalUnaryPlan plan(OP_TYPE op, CHILD_TYPE child) { - return new LogicalUnary(op, child); + return new LogicalUnaryPlan(op, child); } default - LogicalBinary + LogicalBinaryPlan plan(OP_TYPE op, LEFT_CHILD_TYPE leftChild, RIGHT_CHILD_TYPE rightChild) { - return new LogicalBinary(op, leftChild, rightChild); + return new LogicalBinaryPlan(op, leftChild, rightChild); } - default PhysicalLeaf + default PhysicalLeafPlan plan(OP_TYPE op, LogicalProperties logicalProperties) { - return new PhysicalLeaf(op, logicalProperties); + return new PhysicalLeafPlan(op, logicalProperties); } - default PhysicalUnary + default PhysicalUnaryPlan plan(OP_TYPE op, LogicalProperties logicalProperties, CHILD_TYPE child) { - return new PhysicalUnary(op, logicalProperties, child); + return new PhysicalUnaryPlan(op, logicalProperties, child); } default - PhysicalBinary + PhysicalBinaryPlan plan(OP_TYPE op, LogicalProperties logicalProperties, LEFT_CHILD_TYPE leftChild, RIGHT_CHILD_TYPE rightChild) { - return new PhysicalBinary(op, logicalProperties, leftChild, rightChild); + return new PhysicalBinaryPlan(op, logicalProperties, leftChild, rightChild); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/UnaryPlan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/UnaryPlan.java index 82e221b249..af03827ae0 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/UnaryPlan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/UnaryPlan.java @@ -20,25 +20,12 @@ package org.apache.doris.nereids.trees.plans; import org.apache.doris.nereids.operators.plans.UnaryPlanOperator; import org.apache.doris.nereids.trees.UnaryNode; -import java.util.List; - /** * interface for all plan node that have one child. */ -public interface UnaryPlan< - PLAN_TYPE extends UnaryPlan, - OP_TYPE extends UnaryPlanOperator, - CHILD_TYPE extends Plan> - extends Plan, UnaryNode { +public interface UnaryPlan + extends Plan, UnaryNode { @Override - List children(); - - @Override - Plan child(int index); - - @Override - default int arity() { - return 1; - } + UnaryPlanOperator getOperator(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/AbstractLogicalPlan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/AbstractLogicalPlan.java index 698b6f1208..780dcbb59c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/AbstractLogicalPlan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/AbstractLogicalPlan.java @@ -31,11 +31,8 @@ import java.util.List; /** * Abstract class for all concrete logical plan. */ -public abstract class AbstractLogicalPlan< - PLAN_TYPE extends AbstractLogicalPlan, - OP_TYPE extends LogicalOperator> - extends AbstractPlan - implements LogicalPlan { +public abstract class AbstractLogicalPlan + extends AbstractPlan implements LogicalPlan { protected final LogicalProperties logicalProperties; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalBinary.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalBinaryPlan.java similarity index 70% rename from fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalBinary.java rename to fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalBinaryPlan.java index d5417b57e6..8d860b9dd7 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalBinary.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalBinaryPlan.java @@ -21,7 +21,6 @@ import org.apache.doris.nereids.memo.GroupExpression; import org.apache.doris.nereids.operators.plans.logical.LogicalBinaryOperator; import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import org.apache.doris.nereids.trees.plans.BinaryPlan; import org.apache.doris.nereids.trees.plans.Plan; @@ -32,27 +31,26 @@ import java.util.List; /** * Abstract class for all logical plan that have two children. */ -public class LogicalBinary< +public class LogicalBinaryPlan< OP_TYPE extends LogicalBinaryOperator, LEFT_CHILD_TYPE extends Plan, RIGHT_CHILD_TYPE extends Plan> - extends AbstractLogicalPlan, OP_TYPE> - implements BinaryPlan, - OP_TYPE, LEFT_CHILD_TYPE, RIGHT_CHILD_TYPE> { + extends AbstractLogicalPlan + implements BinaryPlan { - public LogicalBinary(OP_TYPE operator, LEFT_CHILD_TYPE leftChild, RIGHT_CHILD_TYPE rightChild) { + public LogicalBinaryPlan(OP_TYPE operator, LEFT_CHILD_TYPE leftChild, RIGHT_CHILD_TYPE rightChild) { super(NodeType.LOGICAL, operator, leftChild, rightChild); } - public LogicalBinary(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties, + public LogicalBinaryPlan(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties, LEFT_CHILD_TYPE leftChild, RIGHT_CHILD_TYPE rightChild) { super(NodeType.LOGICAL, operator, groupExpression, logicalProperties, leftChild, rightChild); } @Override - public LogicalBinary newChildren(List children) { + public LogicalBinaryPlan newChildren(List children) { Preconditions.checkArgument(children.size() == 2); - return new LogicalBinary(operator, groupExpression, logicalProperties, - (Plan) children.get(0), (Plan) children.get(1)); + return new LogicalBinaryPlan(operator, groupExpression.orElse(null), + logicalProperties, children.get(0), children.get(1)); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalLeaf.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalLeafPlan.java similarity index 73% rename from fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalLeaf.java rename to fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalLeafPlan.java index bbc82d4633..012424125e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalLeaf.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalLeafPlan.java @@ -21,8 +21,8 @@ import org.apache.doris.nereids.memo.GroupExpression; import org.apache.doris.nereids.operators.plans.logical.LogicalLeafOperator; import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import org.apache.doris.nereids.trees.plans.LeafPlan; +import org.apache.doris.nereids.trees.plans.Plan; import com.google.common.base.Preconditions; @@ -31,21 +31,21 @@ import java.util.List; /** * Abstract class for all logical plan that have no child. */ -public class LogicalLeaf - extends AbstractLogicalPlan, OP_TYPE> - implements LeafPlan, OP_TYPE> { +public class LogicalLeafPlan + extends AbstractLogicalPlan + implements LeafPlan { - public LogicalLeaf(OP_TYPE operator) { + public LogicalLeafPlan(OP_TYPE operator) { super(NodeType.LOGICAL, operator); } - public LogicalLeaf(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties) { + public LogicalLeafPlan(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties) { super(NodeType.LOGICAL, operator, groupExpression, logicalProperties); } @Override - public LogicalLeaf newChildren(List children) { + public LogicalLeafPlan newChildren(List children) { Preconditions.checkArgument(children.size() == 0); - return new LogicalLeaf(operator, groupExpression, logicalProperties); + return new LogicalLeafPlan(operator, groupExpression.orElse(null), logicalProperties); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPlan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPlan.java index 1c66e122fc..3cb4341a6e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPlan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalPlan.java @@ -26,16 +26,13 @@ import java.util.function.BiFunction; /** * Abstract class for all logical plan in Nereids. */ -public interface LogicalPlan< - PLAN_TYPE extends LogicalPlan, - OP_TYPE extends LogicalOperator> - extends Plan { +public interface LogicalPlan extends Plan { @Override - List children(); + LogicalOperator getOperator(); @Override - Plan child(int index); + LogicalPlan newChildren(List children); /** * Map a [[LogicalPlan]] to another [[LogicalPlan]] if the passed context exists using the diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalUnary.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalUnaryPlan.java similarity index 71% rename from fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalUnary.java rename to fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalUnaryPlan.java index c0de9a22fd..b44db32de2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalUnary.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalUnaryPlan.java @@ -21,7 +21,6 @@ import org.apache.doris.nereids.memo.GroupExpression; import org.apache.doris.nereids.operators.plans.logical.LogicalUnaryOperator; import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import org.apache.doris.nereids.trees.plans.Plan; import org.apache.doris.nereids.trees.plans.UnaryPlan; @@ -32,22 +31,22 @@ import java.util.List; /** * Abstract class for all logical plan that have one child. */ -public class LogicalUnary - extends AbstractLogicalPlan, OP_TYPE> - implements UnaryPlan, OP_TYPE, CHILD_TYPE> { +public class LogicalUnaryPlan + extends AbstractLogicalPlan + implements UnaryPlan { - public LogicalUnary(OP_TYPE operator, CHILD_TYPE child) { + public LogicalUnaryPlan(OP_TYPE operator, CHILD_TYPE child) { super(NodeType.LOGICAL, operator, child); } - public LogicalUnary(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties, + public LogicalUnaryPlan(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties, CHILD_TYPE child) { super(NodeType.LOGICAL, operator, groupExpression, logicalProperties, child); } @Override - public LogicalUnary newChildren(List children) { + public LogicalUnaryPlan newChildren(List children) { Preconditions.checkArgument(children.size() == 1); - return new LogicalUnary(operator, groupExpression, logicalProperties, (Plan) children.get(0)); + return new LogicalUnaryPlan(operator, groupExpression.orElse(null), logicalProperties, children.get(0)); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/AbstractPhysicalPlan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/AbstractPhysicalPlan.java index 81362b557c..ddd0228250 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/AbstractPhysicalPlan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/AbstractPhysicalPlan.java @@ -32,11 +32,9 @@ import java.util.Objects; /** * Abstract class for all concrete physical plan. */ -public abstract class AbstractPhysicalPlan< - PLAN_TYPE extends AbstractPhysicalPlan, - OP_TYPE extends PhysicalOperator> - extends AbstractPlan - implements PhysicalPlan { +public abstract class AbstractPhysicalPlan + extends AbstractPlan + implements PhysicalPlan { protected final LogicalProperties logicalProperties; protected final PhysicalProperties physicalProperties; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalBinary.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalBinaryPlan.java similarity index 71% rename from fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalBinary.java rename to fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalBinaryPlan.java index 7b7883d61a..9b95552216 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalBinary.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalBinaryPlan.java @@ -21,7 +21,6 @@ import org.apache.doris.nereids.memo.GroupExpression; import org.apache.doris.nereids.operators.plans.physical.PhysicalBinaryOperator; import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import org.apache.doris.nereids.trees.plans.BinaryPlan; import org.apache.doris.nereids.trees.plans.Plan; @@ -32,29 +31,27 @@ import java.util.List; /** * Abstract class for all physical plan that have two children. */ -public class PhysicalBinary< +public class PhysicalBinaryPlan< OP_TYPE extends PhysicalBinaryOperator, LEFT_CHILD_TYPE extends Plan, RIGHT_CHILD_TYPE extends Plan> - extends AbstractPhysicalPlan, OP_TYPE> - implements BinaryPlan< - PhysicalBinary, - OP_TYPE, LEFT_CHILD_TYPE, RIGHT_CHILD_TYPE> { + extends AbstractPhysicalPlan + implements BinaryPlan { - public PhysicalBinary(OP_TYPE operator, LogicalProperties logicalProperties, + public PhysicalBinaryPlan(OP_TYPE operator, LogicalProperties logicalProperties, LEFT_CHILD_TYPE leftChild, RIGHT_CHILD_TYPE rightChild) { super(NodeType.PHYSICAL, operator, logicalProperties, leftChild, rightChild); } - public PhysicalBinary(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties, + public PhysicalBinaryPlan(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties, LEFT_CHILD_TYPE leftChild, RIGHT_CHILD_TYPE rightChild) { super(NodeType.PHYSICAL, operator, groupExpression, logicalProperties, leftChild, rightChild); } @Override - public PhysicalBinary newChildren(List children) { + public PhysicalBinaryPlan newChildren(List children) { Preconditions.checkArgument(children.size() == 2); - return new PhysicalBinary(operator, groupExpression, logicalProperties, - (Plan) children.get(0), (Plan) children.get(1)); + return new PhysicalBinaryPlan(operator, groupExpression.orElse(null), logicalProperties, + children.get(0), children.get(1)); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalLeaf.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalLeafPlan.java similarity index 72% rename from fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalLeaf.java rename to fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalLeafPlan.java index abdc193baf..0408641b79 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalLeaf.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalLeafPlan.java @@ -21,8 +21,8 @@ import org.apache.doris.nereids.memo.GroupExpression; import org.apache.doris.nereids.operators.plans.physical.PhysicalLeafOperator; import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import org.apache.doris.nereids.trees.plans.LeafPlan; +import org.apache.doris.nereids.trees.plans.Plan; import com.google.common.base.Preconditions; @@ -31,21 +31,21 @@ import java.util.List; /** * Abstract class for all physical plan that have no child. */ -public class PhysicalLeaf - extends AbstractPhysicalPlan, OP_TYPE> - implements LeafPlan, OP_TYPE> { +public class PhysicalLeafPlan + extends AbstractPhysicalPlan + implements LeafPlan { - public PhysicalLeaf(OP_TYPE operator, LogicalProperties logicalProperties) { + public PhysicalLeafPlan(OP_TYPE operator, LogicalProperties logicalProperties) { super(NodeType.PHYSICAL, operator, logicalProperties); } - public PhysicalLeaf(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties) { + public PhysicalLeafPlan(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties) { super(NodeType.PHYSICAL, operator, groupExpression, logicalProperties); } @Override - public PhysicalLeaf newChildren(List children) { + public PhysicalLeafPlan newChildren(List children) { Preconditions.checkArgument(children.size() == 0); - return new PhysicalLeaf(operator, groupExpression, logicalProperties); + return new PhysicalLeafPlan(operator, groupExpression.orElse(null), logicalProperties); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalPlan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalPlan.java index 9301c294e6..e0b1ff34b8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalPlan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalPlan.java @@ -20,20 +20,10 @@ package org.apache.doris.nereids.trees.plans.physical; import org.apache.doris.nereids.operators.plans.physical.PhysicalOperator; import org.apache.doris.nereids.trees.plans.Plan; -import java.util.List; - /** * interface for all physical plan. */ -public interface PhysicalPlan< - PLAN_TYPE extends PhysicalPlan, - OP_TYPE extends PhysicalOperator> - extends Plan { - +public interface PhysicalPlan extends Plan { @Override - List children(); - - @Override - Plan child(int index); - + PhysicalOperator getOperator(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalUnary.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalUnaryPlan.java similarity index 69% rename from fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalUnary.java rename to fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalUnaryPlan.java index a289627536..caadb0c177 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalUnary.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalUnaryPlan.java @@ -21,7 +21,6 @@ import org.apache.doris.nereids.memo.GroupExpression; import org.apache.doris.nereids.operators.plans.physical.PhysicalUnaryOperator; import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.NodeType; -import org.apache.doris.nereids.trees.TreeNode; import org.apache.doris.nereids.trees.plans.Plan; import org.apache.doris.nereids.trees.plans.UnaryPlan; @@ -32,23 +31,23 @@ import java.util.List; /** * Abstract class for all physical plan that have one child. */ -public class PhysicalUnary - extends AbstractPhysicalPlan, OP_TYPE> - implements UnaryPlan, OP_TYPE, CHILD_TYPE> { +public class PhysicalUnaryPlan + extends AbstractPhysicalPlan + implements UnaryPlan { - public PhysicalUnary(OP_TYPE operator, LogicalProperties logicalProperties, CHILD_TYPE child) { + public PhysicalUnaryPlan(OP_TYPE operator, LogicalProperties logicalProperties, CHILD_TYPE child) { super(NodeType.PHYSICAL, operator, logicalProperties, child); } - public PhysicalUnary(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties, + public PhysicalUnaryPlan(OP_TYPE operator, GroupExpression groupExpression, LogicalProperties logicalProperties, CHILD_TYPE child) { super(NodeType.PHYSICAL, operator, groupExpression, logicalProperties, child); } @Override - public PhysicalUnary newChildren(List children) { + public PhysicalUnaryPlan newChildren(List children) { Preconditions.checkArgument(children.size() == 1); - return new PhysicalUnary(operator, groupExpression, logicalProperties, (Plan) children.get(0)); + return new PhysicalUnaryPlan(operator, groupExpression.orElse(null), + logicalProperties, (Plan) children.get(0)); } }