Calcite自定义SQL解析常用方法和配置
    config.fmpp
    配置和Parser.jj文件结合说明
    package,class,imports
    keywords
    nonReservedKeywords
    joinTypes
    statementParserMethods
    literalParserMethods
    dataTypeParserMethods
    alterStatementParserMethods
    createStatementParserMethods
    dropStatementParserMethods
    implementationFiles
    includeCompoundIdentifier
    includeBraces
    includeAdditionalDeclarations
    Parser.jj常用方法
    getPos()
    StringLiteral
    Identifier
    SimpleIdentifier
    CompoundIdentifier
    更多示例代码
    其他常见类
    自定义SQL
    自定义SQL语法
    定义解析结果类
    语法模板 parserImpls.ftl
    定义class 和 imports
    定义关键字 config.fmpp
    定义自定义解析 statementParserMethods
    测试类
    代码示例
    Github

    Calcite自定义SQL解析
    常用方法和配置
    config.fmpp
    calcite 模板配置

    data: {
    parser: {
    # Generated parser implementation package and class name.
    # 生成解析器实现类包和名称
    # 包名
    package: “com.github.quxiucheng.tutorial.parser.custom”,
    # 实体类名
    class: “ExtensionSqlParserImpl”,

    1. # List of import statements.<br /> # Example. "org.apache.calcite.sql.*", "java.util.List".<br /> # 导入处理的语句<br /> imports: [<br /> ]<br /> # List of new keywords. Example: "DATABASES", "TABLES". If the keyword is not a reserved<br /> # keyword add it to 'nonReservedKeywords' section.<br /> # 新关键字列表。示例:“DATABASES”、“TABLES”。如果关键字不是一个保留关键字,将其添加到“无保留关键字”部分。<br /> keywords: [<br /> ]<br /> <br /> # List of keywords from "keywords" section that are not reserved.<br /> # “keywords”部分中未保留的关键字列表。<br /> nonReservedKeywords: [<br /> "A"<br /> "ABSENT"<br /> "ABSOLUTE"<br /> "ACTION"<br /> "ADA"<br /> "ADD"<br /> "ADMIN"<br /> "AFTER"<br /> "ALWAYS"<br /> "APPLY"<br /> "ASC"<br /> "ASSERTION"<br /> "ASSIGNMENT"<br /> "ATTRIBUTE"<br /> "ATTRIBUTES"<br /> "BEFORE"<br /> "BERNOULLI"<br /> "BREADTH"<br /> "C"<br /> "CASCADE"<br /> "CATALOG"<br /> "CATALOG_NAME"<br /> "CENTURY"<br /> "CHAIN"<br /> "CHARACTER_SET_CATALOG"<br /> "CHARACTER_SET_NAME"<br /> "CHARACTER_SET_SCHEMA"<br /> "CHARACTERISTICS"<br /> "CHARACTERS"<br /> "CLASS_ORIGIN"<br /> "COBOL"<br /> "COLLATION"<br /> "COLLATION_CATALOG"<br /> "COLLATION_NAME"<br /> "COLLATION_SCHEMA"<br /> "COLUMN_NAME"<br /> "COMMAND_FUNCTION"<br /> "COMMAND_FUNCTION_CODE"<br /> "COMMITTED"<br /> "CONDITION_NUMBER"<br /> "CONDITIONAL"<br /> "CONNECTION"<br /> "CONNECTION_NAME"<br /> "CONSTRAINT_CATALOG"<br /> "CONSTRAINT_NAME"<br /> "CONSTRAINT_SCHEMA"<br /> "CONSTRAINTS"<br /> "CONSTRUCTOR"<br /> "CONTINUE"<br /> "CURSOR_NAME"<br /> "DATA"<br /> "DATABASE"<br /> "DATETIME_INTERVAL_CODE"<br /> "DATETIME_INTERVAL_PRECISION"<br /> "DECADE"<br /> "DEFAULTS"<br /> "DEFERRABLE"<br /> "DEFERRED"<br /> "DEFINED"<br /> "DEFINER"<br /> "DEGREE"<br /> "DEPTH"<br /> "DERIVED"<br /> "DESC"<br /> "DESCRIPTION"<br /> "DESCRIPTOR"<br /> "DIAGNOSTICS"<br /> "DISPATCH"<br /> "DOMAIN"<br /> "DOW"<br /> "DOY"<br /> "DYNAMIC_FUNCTION"<br /> "DYNAMIC_FUNCTION_CODE"<br /> "ENCODING"<br /> "EPOCH"<br /> "ERROR"<br /> "EXCEPTION"<br /> "EXCLUDE"<br /> "EXCLUDING"<br /> "FINAL"<br /> "FIRST"<br /> "FOLLOWING"<br /> "FORMAT"<br /> "FORTRAN"<br /> "FOUND"<br /> "FRAC_SECOND"<br /> "G"<br /> "GENERAL"<br /> "GENERATED"<br /> "GEOMETRY"<br /> "GO"<br /> "GOTO"<br /> "GRANTED"<br /> "HIERARCHY"<br /> "IMMEDIATE"<br /> "IMMEDIATELY"<br /> "IMPLEMENTATION"<br /> "INCLUDING"<br /> "INCREMENT"<br /> "INITIALLY"<br /> "INPUT"<br /> "INSTANCE"<br /> "INSTANTIABLE"<br /> "INVOKER"<br /> "ISODOW"<br /> "ISOYEAR"<br /> "ISOLATION"<br /> "JAVA"<br /> "JSON"<br /> "K"<br /> "KEY"<br /> "KEY_MEMBER"<br /> "KEY_TYPE"<br /> "LABEL"<br /> "LAST"<br /> "LENGTH"<br /> "LEVEL"<br /> "LIBRARY"<br /> "LOCATOR"<br /> "M"<br /> "MAP"<br /> "MATCHED"<br /> "MAXVALUE"<br /> "MICROSECOND"<br /> "MESSAGE_LENGTH"<br /> "MESSAGE_OCTET_LENGTH"<br /> "MESSAGE_TEXT"<br /> "MILLISECOND"<br /> "MILLENNIUM"<br /> "MINVALUE"<br /> "MORE_"<br /> "MUMPS"<br /> "NAME"<br /> "NAMES"<br /> "NANOSECOND"<br /> "NESTING"<br /> "NORMALIZED"<br /> "NULLABLE"<br /> "NULLS"<br /> "NUMBER"<br /> "OBJECT"<br /> "OCTETS"<br /> "OPTION"<br /> "OPTIONS"<br /> "ORDERING"<br /> "ORDINALITY"<br /> "OTHERS"<br /> "OUTPUT"<br /> "OVERRIDING"<br /> "PAD"<br /> "PARAMETER_MODE"<br /> "PARAMETER_NAME"<br /> "PARAMETER_ORDINAL_POSITION"<br /> "PARAMETER_SPECIFIC_CATALOG"<br /> "PARAMETER_SPECIFIC_NAME"<br /> "PARAMETER_SPECIFIC_SCHEMA"<br /> "PARTIAL"<br /> "PASCAL"<br /> "PASSING"<br /> "PASSTHROUGH"<br /> "PAST"<br /> "PATH"<br /> "PLACING"<br /> "PLAN"<br /> "PLI"<br /> "PRECEDING"<br /> "PRESERVE"<br /> "PRIOR"<br /> "PRIVILEGES"<br /> "PUBLIC"<br /> "QUARTER"<br /> "READ"<br /> "RELATIVE"<br /> "REPEATABLE"<br /> "REPLACE"<br /> "RESTART"<br /> "RESTRICT"<br /> "RETURNED_CARDINALITY"<br /> "RETURNED_LENGTH"<br /> "RETURNED_OCTET_LENGTH"<br /> "RETURNED_SQLSTATE"<br /> "RETURNING"<br /> "ROLE"<br /> "ROUTINE"<br /> "ROUTINE_CATALOG"<br /> "ROUTINE_NAME"<br /> "ROUTINE_SCHEMA"<br /> "ROW_COUNT"<br /> "SCALAR"<br /> "SCALE"<br /> "SCHEMA"<br /> "SCHEMA_NAME"<br /> "SCOPE_CATALOGS"<br /> "SCOPE_NAME"<br /> "SCOPE_SCHEMA"<br /> "SECTION"<br /> "SECURITY"<br /> "SELF"<br /> "SEQUENCE"<br /> "SERIALIZABLE"<br /> "SERVER"<br /> "SERVER_NAME"<br /> "SESSION"<br /> "SETS"<br /> "SIMPLE"<br /> "SIZE"<br /> "SOURCE"<br /> "SPACE"<br /> "SPECIFIC_NAME"<br /> "SQL_BIGINT"<br /> "SQL_BINARY"<br /> "SQL_BIT"<br /> "SQL_BLOB"<br /> "SQL_BOOLEAN"<br /> "SQL_CHAR"<br /> "SQL_CLOB"<br /> "SQL_DATE"<br /> "SQL_DECIMAL"<br /> "SQL_DOUBLE"<br /> "SQL_FLOAT"<br /> "SQL_INTEGER"<br /> "SQL_INTERVAL_DAY"<br /> "SQL_INTERVAL_DAY_TO_HOUR"<br /> "SQL_INTERVAL_DAY_TO_MINUTE"<br /> "SQL_INTERVAL_DAY_TO_SECOND"<br /> "SQL_INTERVAL_HOUR"<br /> "SQL_INTERVAL_HOUR_TO_MINUTE"<br /> "SQL_INTERVAL_HOUR_TO_SECOND"<br /> "SQL_INTERVAL_MINUTE"<br /> "SQL_INTERVAL_MINUTE_TO_SECOND"<br /> "SQL_INTERVAL_MONTH"<br /> "SQL_INTERVAL_SECOND"<br /> "SQL_INTERVAL_YEAR"<br /> "SQL_INTERVAL_YEAR_TO_MONTH"<br /> "SQL_LONGVARBINARY"<br /> "SQL_LONGVARNCHAR"<br /> "SQL_LONGVARCHAR"<br /> "SQL_NCHAR"<br /> "SQL_NCLOB"<br /> "SQL_NUMERIC"<br /> "SQL_NVARCHAR"<br /> "SQL_REAL"<br /> "SQL_SMALLINT"<br /> "SQL_TIME"<br /> "SQL_TIMESTAMP"<br /> "SQL_TINYINT"<br /> "SQL_TSI_DAY"<br /> "SQL_TSI_FRAC_SECOND"<br /> "SQL_TSI_HOUR"<br /> "SQL_TSI_MICROSECOND"<br /> "SQL_TSI_MINUTE"<br /> "SQL_TSI_MONTH"<br /> "SQL_TSI_QUARTER"<br /> "SQL_TSI_SECOND"<br /> "SQL_TSI_WEEK"<br /> "SQL_TSI_YEAR"<br /> "SQL_VARBINARY"<br /> "SQL_VARCHAR"<br /> "STATE"<br /> "STATEMENT"<br /> "STRUCTURE"<br /> "STYLE"<br /> "SUBCLASS_ORIGIN"<br /> "SUBSTITUTE"<br /> "TABLE_NAME"<br /> "TEMPORARY"<br /> "TIES"<br /> "TIMESTAMPADD"<br /> "TIMESTAMPDIFF"<br /> "TOP_LEVEL_COUNT"<br /> "TRANSACTION"<br /> "TRANSACTIONS_ACTIVE"<br /> "TRANSACTIONS_COMMITTED"<br /> "TRANSACTIONS_ROLLED_BACK"<br /> "TRANSFORM"<br /> "TRANSFORMS"<br /> "TRIGGER_CATALOG"<br /> "TRIGGER_NAME"<br /> "TRIGGER_SCHEMA"<br /> "TYPE"<br /> "UNBOUNDED"<br /> "UNCOMMITTED"<br /> "UNCONDITIONAL"<br /> "UNDER"<br /> "UNNAMED"<br /> "USAGE"<br /> "USER_DEFINED_TYPE_CATALOG"<br /> "USER_DEFINED_TYPE_CODE"<br /> "USER_DEFINED_TYPE_NAME"<br /> "USER_DEFINED_TYPE_SCHEMA"<br /> "UTF8"<br /> "UTF16"<br /> "UTF32"<br /> "VERSION"<br /> "VIEW"<br /> "WEEK"<br /> "WRAPPER"<br /> "WORK"<br /> "WRITE"<br /> "XML"<br /> "ZONE"<br /> ]<br /> <br /> # List of additional join types. Each is a method with no arguments.<br /> # Example: LeftSemiJoin()<br /> # 其他连接类型的列表。每个方法都是没有参数的方法。<br /> joinTypes: [<br /> ]<br /> <br /> # List of methods for parsing custom SQL statements.<br /> # Return type of method implementation should be 'SqlNode'.<br /> # Example: SqlShowDatabases(), SqlShowTables().<br /> # 用于解析自定义SQL语句的方法列表。<br /> statementParserMethods: [<br /> ]<br /> <br /> # List of methods for parsing custom literals.<br /> # Return type of method implementation should be "SqlNode".<br /> # Example: ParseJsonLiteral().<br /> # 解析自定义文本的方法列表<br /> literalParserMethods: [<br /> ]<br /> <br /> # List of methods for parsing custom data types.<br /> # Return type of method implementation should be "SqlIdentifier".<br /> # 用于解析自定义数据类型的方法列表。<br /> dataTypeParserMethods: [<br /> ]<br /> <br /> # List of methods for parsing extensions to "ALTER <scope>" calls.<br /> # Each must accept arguments "(SqlParserPos pos, String scope)".<br /> # Example: "SqlUploadJarNode"<br /> # 每个都必须接受参数 "(SqlParserPos pos, String scope)".<br /> # 解析扩展到“ALTER ”调用的方法。<br /> alterStatementParserMethods: [<br /> <br /> ]<br /> <br /> # List of methods for parsing extensions to "CREATE [OR REPLACE]" calls.<br /> # Each must accept arguments "(SqlParserPos pos, boolean replace)".<br /> # 解析扩展以"CREATE [OR REPLACE]"调用的方法列表。<br /> # 每个都必须接受参数 "(SqlParserPos pos, String scope)".<br /> createStatementParserMethods: [<br /> ]<br /> <br /> # List of methods for parsing extensions to "DROP" calls.<br /> # Each must accept arguments "(SqlParserPos pos)".<br /> # 解析扩展到“DROP”调用的方法列表。<br /> # 每个都必须接受参数 "(SqlParserPos pos)".<br /> dropStatementParserMethods: [<br /> ]<br /> <br /> # List of files in @includes directory that have parser method<br /> # implementations for parsing custom SQL statements, literals or types<br /> # given as part of "statementParserMethods", "literalParserMethods" or<br /> # "dataTypeParserMethods".<br /> # @includes目录中具有解析器方法的文件列表<br /> # 解析自定义SQL语句、文本或类型的实现<br /> # 作为“statementParserMethods”、“literalParserMethods”或“dataTypeParserMethods”的一部分给出。<br /> implementationFiles: [<br /> "parserImpls.ftl"<br /> ]<br /> <br /> includeCompoundIdentifier: true<br /> includeBraces: true<br /> includeAdditionalDeclarations: false<br /> <br /> }<br />}<br /># freemarker模板的位置<br />freemarkerLinks: {<br /> includes: includes/<br />}<br />1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br />194<br />195<br />196<br />197<br />198<br />199<br />200<br />201<br />202<br />203<br />204<br />205<br />206<br />207<br />208<br />209<br />210<br />211<br />212<br />213<br />214<br />215<br />216<br />217<br />218<br />219<br />220<br />221<br />222<br />223<br />224<br />225<br />226<br />227<br />228<br />229<br />230<br />231<br />232<br />233<br />234<br />235<br />236<br />237<br />238<br />239<br />240<br />241<br />242<br />243<br />244<br />245<br />246<br />247<br />248<br />249<br />250<br />251<br />252<br />253<br />254<br />255<br />256<br />257<br />258<br />259<br />260<br />261<br />262<br />263<br />264<br />265<br />266<br />267<br />268<br />269<br />270<br />271<br />272<br />273<br />274<br />275<br />276<br />277<br />278<br />279<br />280<br />281<br />282<br />283<br />284<br />285<br />286<br />287<br />288<br />289<br />290<br />291<br />292<br />293<br />294<br />295<br />296<br />297<br />298<br />299<br />300<br />301<br />302<br />303<br />304<br />305<br />306<br />307<br />308<br />309<br />310<br />311<br />312<br />313<br />314<br />315<br />316<br />317<br />318<br />319<br />320<br />321<br />322<br />323<br />324<br />325<br />326<br />327<br />328<br />329<br />330<br />331<br />332<br />333<br />334<br />335<br />336<br />337<br />338<br />339<br />340<br />341<br />342<br />343<br />344<br />345<br />346<br />347<br />348<br />349<br />350<br />351<br />352<br />353<br />354<br />355<br />356<br />357<br />358<br />359<br />360<br />361<br />362<br />363<br />364<br />365<br />366<br />367<br />368<br />369<br />370<br />371<br />372<br />373<br />374<br />375<br />376<br />377<br />378<br />379<br />380<br />381<br />382<br />383<br />384<br />385<br />386<br />387<br />388<br />389<br />390<br />391<br />392<br />393<br />394<br />395<br />396<br />配置和Parser.jj文件结合说明<br />package,class,imports<br />主要是负责导入包,设置编译package目录,编译的类名

    keywords
    定义关键字

    nonReservedKeywords
    keywords定义关键字中,保留的关键字

    joinTypes
    join类型

    statementParserMethods
    解析自定义SQL语句的方法列表,必须实现SqlNode

    literalParserMethods
    解析自定义文字的方法列表,必须实现SqlNode

    dataTypeParserMethods
    解析自定义数据类型的方法列表,必须实现SqlIdentifier

    alterStatementParserMethods
    解析自定义alter语句,必须有构造方法(SqlParserPos pos, String scope)

    createStatementParserMethods
    解析自定义create语句,必须有构造方法(SqlParserPos pos, boolean replace)

    dropStatementParserMethods
    解析自定义drop语句,必须有构造方法(SqlParserPos pos)

    implementationFiles
    模板文件

    includeCompoundIdentifier
    是否包含CompoundIdentifier解析

    includeBraces

    includeAdditionalDeclarations

    Parser.jj常用方法
    javacc教程

    getPos()
    获取当前token的配置,自定义解析的时候使用

    StringLiteral
    主要解析是语句中的string类型的字段

    select * from table where a=’string’
    1
    Identifier
    解析Identifier字段,返回string类型

    select SqlIdentifier from table;
    1
    SimpleIdentifier
    解析Identifier字段,返回Identifier

    select SqlIdentifier from table;
    1
    CompoundIdentifier
    解析Identifier字段,返回Identifier

    select Compound.SqlIdentifier from table;
    select SqlIdentifier from table;
    1
    2
    更多示例代码
    更多示例代码-JavaCC

    更多示例代码-Java代码

    其他常见类
    Calcite SQL解析

    自定义SQL
    自定义SQL语法
    create function function_name as class_name
    [method]
    [with] [(key=value)]
    1
    2
    3
    # 创建函数关键字
    create function
    # 函数名
    hr.custom_function
    # as关键字
    as
    # 类名称
    ‘com.github.quxiucheng.calcite.func.CustomFunction’
    # 可选 方法名称
    method ‘eval’
    # 可选 备注信息
    comment ‘comment’
    # 可选 附件变量
    property (‘a’=’b’,’c’=’1’)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    定义解析结果类
    SqlCreateFunction.java

    import org.apache.calcite.sql.SqlCall;
    import org.apache.calcite.sql.SqlKind;
    import org.apache.calcite.sql.SqlNode;
    import org.apache.calcite.sql.SqlNodeList;
    import org.apache.calcite.sql.SqlOperator;
    import org.apache.calcite.sql.SqlWriter;
    import org.apache.calcite.sql.parser.SqlParserPos;

    import java.util.List;

    public class SqlCreateFunction extends SqlCall {

    1. private SqlNode functionName;
    2. private String className;
    3. private SqlNodeList properties;
    4. private String methodName;
    5. private String comment;
    6. public SqlCreateFunction(SqlParserPos pos,<br /> SqlNode functionName, String className, String methodName, String comment,<br /> SqlNodeList properties) {
    7. super(pos);<br /> this.functionName = functionName;<br /> this.className = className;<br /> this.properties = properties;<br /> this.methodName = methodName;<br /> }
    8. public SqlNode getFunctionName() {<br /> return functionName;<br /> }
    9. public String getClassName() {<br /> return className;<br /> }
    10. public String getMethodName() {<br /> return methodName;<br /> }
    11. public SqlNodeList getProperties() {<br /> return properties;<br /> }
    12. public String getComment() {<br /> return comment;<br /> }
    13. @Override<br /> public SqlOperator getOperator() {<br /> return null;<br /> }
    14. @Override<br /> public List<SqlNode> getOperandList() {<br /> return null;<br /> }
    15. @Override<br /> public SqlKind getKind() {<br /> return SqlKind.OTHER_DDL;<br /> }
    16. @Override<br /> public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {<br /> writer.keyword("CREATE");<br /> writer.keyword("FUNCTION");<br /> functionName.unparse(writer, leftPrec, rightPrec);<br /> writer.keyword("AS");<br /> writer.print("'" + className + "'");<br /> if (methodName != null) {<br /> writer.newlineAndIndent();<br /> writer.keyword("METHOD");<br /> writer.print("'" + methodName + "'");<br /> }<br /> if (properties != null) {<br /> writer.newlineAndIndent();<br /> writer.keyword("PROPERTY");<br /> SqlWriter.Frame propertyFrame = writer.startList("(", ")");<br /> for (SqlNode property : properties) {<br /> writer.sep(",", false);<br /> writer.newlineAndIndent();<br /> writer.print(" ");<br /> property.unparse(writer, leftPrec, rightPrec);<br /> }<br /> writer.newlineAndIndent();<br /> writer.endList(propertyFrame);<br /> }<br /> if (comment != null) {<br /> writer.newlineAndIndent();<br /> writer.keyword("COMMENT");<br /> writer.print("'" + COMMENT + "'");<br /> }<br /> }<br />}<br />1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />解析key=value语句<br />SqlProperty.java

    import com.google.common.collect.ImmutableList;
    import org.apache.calcite.sql.SqlCall;
    import org.apache.calcite.sql.SqlIdentifier;
    import org.apache.calcite.sql.SqlKind;
    import org.apache.calcite.sql.SqlLiteral;
    import org.apache.calcite.sql.SqlNode;
    import org.apache.calcite.sql.SqlOperator;
    import org.apache.calcite.sql.SqlSpecialOperator;
    import org.apache.calcite.sql.SqlWriter;
    import org.apache.calcite.sql.parser.SqlParserPos;
    import org.apache.calcite.util.NlsString;

    import java.util.List;

    import static java.util.Objects.requireNonNull;

    public class SqlProperty extends SqlCall {

    1. /**<br /> * 定义特殊操作符<br /> */<br /> protected static final SqlOperator OPERATOR =<br /> new SqlSpecialOperator("Property", SqlKind.OTHER);
    2. private SqlNode key;
    3. private SqlNode value;
    4. public SqlProperty(SqlParserPos pos, SqlNode key, SqlNode value) {<br /> super(pos);<br /> this.key = requireNonNull(key, "Property key is missing");<br /> this.value = requireNonNull(value, "Property value is missing");<br /> }
    5. @Override<br /> public SqlOperator getOperator() {<br /> return OPERATOR;<br /> }
    6. @Override<br /> public List<SqlNode> getOperandList() {<br /> return ImmutableList.of(key, value);<br /> }
    7. @Override<br /> public SqlKind getKind() {<br /> return SqlKind.OTHER;<br /> }
    8. @Override<br /> public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {<br /> key.unparse(writer, leftPrec, rightPrec);<br /> writer.keyword("=");<br /> value.unparse(writer, leftPrec, rightPrec);<br /> }
    9. public SqlNode getKey() {<br /> return key;<br /> }
    10. public SqlNode getValue() {<br /> return value;<br /> }
    11. public String getKeyString() {<br /> return key.toString();<br /> }
    12. public String getValueString() {<br /> return ((NlsString) SqlLiteral.value(value)).getValue();<br /> }

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    语法模板 parserImpls.ftl
    // 创建函数
    SqlNode SqlCreateFunction() :
    {
    // 声明变量
    SqlParserPos createPos;
    SqlParserPos propertyPos;
    SqlNode functionName = null;
    String className = null;
    String methodName = null;
    String comment = null;
    SqlNodeList properties = null;
    }
    {
    // create 关键字

    {
    // 获取当前token的行列位置
    createPos = getPos();
    }
    // function 关键字

    // 函数名
    functionName = CompoundIdentifier()
    // as关键字

    // 类名
    { className = StringLiteralValue(); }
    // if语句
    [
    // method关键字

    {
    // 方法名称
    methodName = StringLiteralValue();
    }
    ]
    // if
    [
    // property 关键字,设置初始化变量

    {
    // 获取关键字位置
    propertyPos = getPos();
    SqlNode property;
    properties = new SqlNodeList(propertyPos);
    }

    [
    property = PropertyValue()
    {
    properties.add(property);
    }
    (

    {
    property = PropertyValue();
    properties.add(property);
    }
    )*
    ]

    ]
    // if
    [
    {
    // 备注
    comment = StringLiteralValue();
    }
    ]

    1. {<br /> return new SqlCreateFunction(createPos, functionName, className, methodName, comment, properties);<br /> }<br />}

    JAVACODE String StringLiteralValue() {
    SqlNode sqlNode = StringLiteral();
    return ((NlsString) SqlLiteral.value(sqlNode)).getValue();
    }

    /*
    解析SQL中的key=value形式的属性值
    */
    SqlNode PropertyValue() :
    {
    SqlNode key;
    SqlNode value;
    SqlParserPos pos;
    }
    {
    key = StringLiteral()
    { pos = getPos(); }
    value = StringLiteral()
    {
    return new SqlProperty(getPos(), key, value);
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    定义class 和 imports
    package: “com.github.quxiucheng.calcite.parser.tutorial”

    class: “CustomSqlParserImpl”,

    1. imports: [<br /> "com.github.quxiucheng.calcite.parser.tutorial.SqlCreateFunction"<br /> "com.github.quxiucheng.calcite.parser.tutorial.sample.model.*"<br /> ]<br />1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />定义关键字 config.fmpp<br /> keywords: [<br /> "PARAMS"<br /> "COMMENT"<br /> "PROPERTY" <br /> ]<br />1<br />2<br />3<br />4<br />5<br />定义自定义解析 statementParserMethods<br /> statementParserMethods: [<br /> "SqlCreateFunction()"<br /> ]<br />1<br />2<br />3<br />测试类

    import org.apache.calcite.config.Lex;
    import org.apache.calcite.sql.SqlNode;
    import org.apache.calcite.sql.dialect.OracleSqlDialect;
    import org.apache.calcite.sql.parser.SqlParseException;
    import org.apache.calcite.sql.parser.SqlParser;

    public class SqlCreateFunctionMain {
    public static void main(String[] args) throws SqlParseException {
    // 解析配置 - mysql设置
    SqlParser.Config mysqlConfig = SqlParser.configBuilder()
    // 定义解析工厂
    .setParserFactory(CustomSqlParserImpl.FACTORY)
    .setLex(Lex.MYSQL)
    .build();
    // 创建解析器
    SqlParser parser = SqlParser.create(“”, mysqlConfig);
    // Sql语句
    String sql = “create function “ +
    “hr.custom_function as ‘com.github.quxiucheng.calcite.func.CustomFunction’ “ +
    “method ‘eval’ “ +
    “property (‘a’=’b’,’c’=’1’) “;
    // 解析sql
    SqlNode sqlNode = parser.parseQuery(sql);
    // 还原某个方言的SQL
    System.out.println(sqlNode.toSqlString(OracleSqlDialect.DEFAULT));
    }
    }

    代码示例
    https://github.com/quxiucheng/apache-calcite-tutorial/tree/master/calcite-tutorial-2-parser/parser-4-calcite-custom-tutoria

    Calcite自定义SQL解析