Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support mask distSQL parse to AST #384

Merged
merged 6 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions shardingsphere-operator/pkg/distsql/antlr4/mask/Alphabet.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.
*/

lexer grammar Alphabet;

FOR_GENERATOR: 'DO NOT MATCH ANY THING, JUST FOR GENERATOR';

fragment A: [Aa];
fragment B: [Bb];
fragment C: [Cc];
fragment D: [Dd];
fragment E: [Ee];
fragment F: [Ff];
fragment G: [Gg];
fragment H: [Hh];
fragment I: [Ii];
fragment J: [Jj];
fragment K: [Kk];
fragment L: [Ll];
fragment M: [Mm];
fragment N: [Nn];
fragment O: [Oo];
fragment P: [Pp];
fragment Q: [Qq];
fragment R: [Rr];
fragment S: [Ss];
fragment T: [Tt];
fragment U: [Uu];
fragment V: [Vv];
fragment W: [Ww];
fragment X: [Xx];
fragment Y: [Yy];
fragment Z: [Zz];
fragment UL_: '_';
64 changes: 64 additions & 0 deletions shardingsphere-operator/pkg/distsql/antlr4/mask/BaseRule.g4
Original file line number Diff line number Diff line change
@@ -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.
*/

grammar BaseRule;

import Symbol, Keyword, Literals;

literal
: STRING_ | (MINUS_)? INT_ | TRUE | FALSE
;

algorithmDefinition
: TYPE LP_ NAME EQ_ algorithmTypeName (COMMA_ propertiesDefinition)? RP_
;

algorithmTypeName
: STRING_ | buildInMaskAlgorithmType
;

buildInMaskAlgorithmType
: MD5
| KEEP_FIRST_N_LAST_M
| KEEP_FROM_X_TO_Y
| MASK_FIRST_N_LAST_M
| MASK_FROM_X_TO_Y
| MASK_BEFORE_SPECIAL_CHARS
| MASK_AFTER_SPECIAL_CHARS
| PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE
| MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE
| LANDLINE_NUMBER_RANDOM_REPLACE
| TELEPHONE_RANDOM_REPLACE
| UNIFIED_CREDIT_CODE_RANDOM_REPLACE
| GENERIC_TABLE_RANDOM_REPLACE
;

propertiesDefinition
: PROPERTIES LP_ properties? RP_
;

properties
: property (COMMA_ property)*
;

property
: key=STRING_ EQ_ value=literal
;

ruleName
: IDENTIFIER_
;
160 changes: 160 additions & 0 deletions shardingsphere-operator/pkg/distsql/antlr4/mask/Keyword.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* 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.
*/

lexer grammar Keyword;

import Alphabet;

WS
: [ \t\r\n] + ->skip
;

TRUE
: T R U E
;

FALSE
: F A L S E
;

CREATE
: C R E A T E
;

ALTER
: A L T E R
;

DROP
: D R O P
;

SHOW
: S H O W
;

RULE
: R U L E
;

FROM
: F R O M
;

MASK
: M A S K
;

TYPE
: T Y P E
;

NAME
: N A M E
;

PROPERTIES
: P R O P E R T I E S
;

COLUMN
: C O L U M N
;

RULES
: R U L E S
;

TABLE
: T A B L E
;

COLUMNS
: C O L U M N S
;

IF
: I F
;

EXISTS
: E X I S T S
;

COUNT
: C O U N T
;

NOT
: N O T
;

MD5
: M D [5]
;

KEEP_FIRST_N_LAST_M
: K E E P UL_ F I R S T UL_ N UL_ L A S T UL_ M
;

KEEP_FROM_X_TO_Y
: K E E P UL_ F R O M UL_ X UL_ T O UL_ Y
;

MASK_FIRST_N_LAST_M
: M A S K UL_ F I R S T UL_ N UL_ L A S T UL_ M
;

MASK_FROM_X_TO_Y
: M A S K UL_ F R O M UL_ X UL_ T O UL_ Y
;

MASK_BEFORE_SPECIAL_CHARS
: M A S K UL_ B E F O R E UL_ S P E C I A L UL_ C H A R S
;

MASK_AFTER_SPECIAL_CHARS
: M A S K UL_ A F T E R UL_ S P E C I A L UL_ C H A R S
;

PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE
: P E R S O N A L UL_ I D E N T I T Y UL_ N U M B E R UL_ R A N D O M UL_ R E P L A C E
;

MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE
: M I L I T A R Y UL_ I D E N T I T Y UL_ N U M B E R UL_ R A N D O M UL_ R E P L A C E
;

LANDLINE_NUMBER_RANDOM_REPLACE
: L A N D L I N E UL_ N U M B E R UL_ R A N D O M UL_ R E P L A C E
;

TELEPHONE_RANDOM_REPLACE
: T E L E P H O N E UL_ R A N D O M UL_ R E P L A C E
;

UNIFIED_CREDIT_CODE_RANDOM_REPLACE
: U N I F I E D UL_ C R E D I T UL_ C O D E UL_ R A N D O M UL_ R E P L A C E
;

GENERIC_TABLE_RANDOM_REPLACE
: G E N E R I C UL_ T A B L E UL_ R A N D O M UL_ R E P L A C E
;

ADDRESS_RANDOM_REPLACE
: A D D R E S S UL_ R A N D O M UL_ R E P L A C E
;
50 changes: 50 additions & 0 deletions shardingsphere-operator/pkg/distsql/antlr4/mask/Literals.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.
*/

lexer grammar Literals;

import Alphabet, Symbol;

IDENTIFIER_
: [A-Za-z_$0-9]*?[A-Za-z_$]+?[A-Za-z_$0-9]*
| BQ_ ~'`'+ BQ_
;

STRING_
: (DQ_ ('\\'. | '""' | ~('"' | '\\'))* DQ_)
| (SQ_ ('\\'. | '\'\'' | ~('\'' | '\\'))* SQ_)
;

INT_
: [0-9]+
;

HEX_
: [0-9a-fA-F]
;

NUMBER_
: INT_? DOT_? INT_ (E (PLUS_ | MINUS_)? INT_)?
;

HEXDIGIT_
: '0x' HEX_+ | 'X' SQ_ HEX_+ SQ_
;

BITNUM_
: '0b' ('0' | '1')+ | B SQ_ ('0' | '1')+ SQ_
;
52 changes: 52 additions & 0 deletions shardingsphere-operator/pkg/distsql/antlr4/mask/RDLStatement.g4
Original file line number Diff line number Diff line change
@@ -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.
*/

grammar RDLStatement;

import BaseRule;

createMaskRule
: CREATE MASK TABLE? RULE ifNotExists? maskRuleDefinition (COMMA_ maskRuleDefinition)*
;

alterMaskRule
: ALTER MASK TABLE? RULE maskRuleDefinition (COMMA_ maskRuleDefinition)*
;

dropMaskRule
: DROP MASK TABLE? RULE ifExists? ruleName (COMMA_ ruleName)*
;

maskRuleDefinition
: ruleName LP_ COLUMNS LP_ columnDefinition (COMMA_ columnDefinition)* RP_ RP_
;

columnDefinition
: LP_ NAME EQ_ columnName COMMA_ algorithmDefinition RP_
;

columnName
: IDENTIFIER_
;

ifExists
: IF EXISTS
;

ifNotExists
: IF NOT EXISTS
;
Loading