Skip to content

Commit 7e0c943

Browse files
committed
Add PostgreSQL historical Cast And operators about JSON
1 parent 8d6ef3b commit 7e0c943

18 files changed

Lines changed: 3831 additions & 3388 deletions

MiniSqlParser/Enums/ExpOperator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@ public enum ExpOperator
1313
,RightBitShift
1414
,BitAnd
1515
,BitOr
16+
,GetJsonObj // ->
17+
,GetJsonObjAsText // ->>
18+
,GetJsonPath // #>
19+
,GetJsonPathAsText // #>>
20+
,DelJsonObj // #-
1621
}
1722
}

MiniSqlParser/Enums/PredicateOperator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ public enum PredicateOperator
1111
, Equal2
1212
, NotEqual
1313
, NotEqual2
14+
, ContainsJsonValueL // @>
15+
, ContainsJsonValueR // <@
16+
, ExistsJsonValue1 // ?
17+
, ExistsJsonValue2 // ?|
18+
, ExistsJsonValue3 // ?&
1419
}
1520
}

MiniSqlParser/Exprs/CastExpr.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,27 @@ namespace MiniSqlParser
33
{
44
public class CastExpr : Expr
55
{
6-
public CastExpr(Expr operand, Identifier typeName) {
7-
this.Comments = new Comments(5);
6+
public CastExpr(Expr operand
7+
, Identifier typeName
8+
, bool isPostgreSqlHistoricalCast=false) {
9+
if(isPostgreSqlHistoricalCast){
10+
this.Comments = new Comments(2);
11+
}else{
12+
this.Comments = new Comments(5);
13+
}
814
this.Operand = operand;
915
this.TypeName = typeName;
16+
this.IsPostgreSqlHistoricalCast = isPostgreSqlHistoricalCast;
1017
}
1118

12-
internal CastExpr(Expr operand, Identifier typeName, Comments comments) {
19+
internal CastExpr(Expr operand
20+
, Identifier typeName
21+
, bool isPostgreSqlHistoricalCast
22+
, Comments comments) {
1323
this.Comments = comments;
1424
this.Operand = operand;
1525
this.TypeName = typeName;
26+
this.IsPostgreSqlHistoricalCast = isPostgreSqlHistoricalCast;
1627
}
1728

1829
private Expr _operand;
@@ -28,6 +39,8 @@ public Expr Operand {
2839

2940
public Identifier TypeName { get; set; }
3041

42+
public bool IsPostgreSqlHistoricalCast { get; private set; }
43+
3144
protected override void AcceptImp(IVisitor visitor) {
3245
visitor.VisitBefore(this);
3346
this.Operand.Accept(visitor);

MiniSqlParser/Parser/MakeASTListener_Expr.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ public override void ExitBinaryOpExpr(MiniSqlParserParser.BinaryOpExprContext co
218218
op = ExpOperator.BitAnd;
219219
} else if(opType == MiniSqlParserLexer.PIPE) {
220220
op = ExpOperator.BitOr;
221+
} else if(opType == MiniSqlParserLexer.ARROW){
222+
op = ExpOperator.GetJsonObj;
223+
} else if(opType == MiniSqlParserLexer.ARROW2) {
224+
op = ExpOperator.GetJsonObjAsText;
225+
} else if(opType == MiniSqlParserLexer.S_GT) {
226+
op = ExpOperator.GetJsonPath;
227+
} else if(opType == MiniSqlParserLexer.S_GT2) {
228+
op = ExpOperator.GetJsonPathAsText;
229+
} else if(opType == MiniSqlParserLexer.S_MINUS) {
230+
op = ExpOperator.DelJsonObj;
221231
} else {
222232
throw new InvalidEnumArgumentException("Undefined ExpOperator is used"
223233
, (int)opType
@@ -308,6 +318,16 @@ public override void ExitSubQueryExpr(MiniSqlParserParser.SubQueryExprContext co
308318
_stack.Push(node);
309319
}
310320

321+
public override void ExitPostgreSqlCastExpr(MiniSqlParserParser.PostgreSqlCastExprContext context) {
322+
var comments = this.GetComments(context);
323+
var typeNameComment = this.GetComments(context.type_name()).Last;
324+
comments.Insert(1, typeNameComment);
325+
var typeName = context.type_name().GetText();
326+
var operand = (Expr)_stack.Pop();
327+
var node = new CastExpr(operand, typeName, true, comments);
328+
_stack.Push(node);
329+
}
330+
311331
public override void ExitBitwiseNotExpr(MiniSqlParserParser.BitwiseNotExprContext context) {
312332
var comments = this.GetComments(context);
313333
var operand = (Expr)_stack.Pop();
@@ -379,8 +399,7 @@ public override void ExitCastExpr(MiniSqlParserParser.CastExprContext context) {
379399
comments.Insert(3, typeNameComment);
380400
var typeName = context.type_name().GetText();
381401
var operand = (Expr)_stack.Pop();
382-
var name = context.K_CAST().GetText();
383-
var node = new CastExpr(operand, typeName, comments);
402+
var node = new CastExpr(operand, typeName, false, comments);
384403
_stack.Push(node);
385404
}
386405

MiniSqlParser/Parser/MakeASTListener_Predicate.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public override void ExitBinaryOpPredicate(MiniSqlParserParser.BinaryOpPredicate
2727
op = PredicateOperator.Equal2;
2828
} else if(opType == MiniSqlParserLexer.NOT_EQ1) {
2929
op = PredicateOperator.NotEqual2;
30+
} else if(opType == MiniSqlParserLexer.AT_LT) {
31+
op = PredicateOperator.ContainsJsonValueL;
32+
} else if(opType == MiniSqlParserLexer.AT_GT) {
33+
op = PredicateOperator.ContainsJsonValueR;
34+
} else if(opType == MiniSqlParserLexer.QRY_PIPE) {
35+
op = PredicateOperator.ExistsJsonValue2;
36+
} else if(opType == MiniSqlParserLexer.QRY_AMP) {
37+
op = PredicateOperator.ExistsJsonValue3;
3038
} else {
3139
throw new CannotBuildASTException("Undifined PredicateOperator is used");
3240
}

MiniSqlParser/Parser/MiniSqlParser.g4

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,10 @@ predicate
437437
| PLACEHOLDER2 # PhPredicate
438438
| expr op=( '<' | '<=' | '>' | '>=' ) expr # BinaryOpPredicate
439439
| expr op=( '=' | '==' | '!=' | '<>' ) expr # BinaryOpPredicate
440-
| expr K_NOT? op=( K_LIKE | K_ILIKE | K_GLOB | K_MATCH | K_REGEXP )
440+
| expr {IsPostgreSql}? op=( '<@' | '@>' | '?|' | '?&' )
441+
expr # BinaryOpPredicate
442+
| expr K_NOT?
443+
op=( K_LIKE | K_ILIKE | K_GLOB | K_MATCH | K_REGEXP )
441444
expr ( K_ESCAPE expr )? # LikePredicate
442445
| expr K_IS K_NOT? K_NULL # IsNullPredicate
443446
| expr K_IS K_NOT? expr # IsPredicate
@@ -473,14 +476,20 @@ expr
473476
| literal_value # LiteralExpr
474477
| PLACEHOLDER1 # PhExpr
475478
| PLACEHOLDER2 # PhExpr
476-
| column_name ( {IsOracle}? OUTER_JOIN )? # ColumnExpr
479+
| column_name ( {IsOracle}? OUTER_JOIN )? # ColumnExpr
477480
| '(' query ')' # SubQueryExpr
481+
| expr {IsPostgreSql}? '::' type_name # PostgreSqlCastExpr
478482
| '~' expr # BitwiseNotExpr
479483
| expr {IsOracle || IsSQLite || IsPostgreSql}?
480484
op='||' expr # BinaryOpExpr
481485
| expr op=( '*' | '/' | '%' ) expr # BinaryOpExpr
482486
| expr op=( '+' | '-' ) expr # BinaryOpExpr
483-
| expr op=( '<<' | '>>' | '&' | '|' ) expr # BinaryOpExpr
487+
| expr op=( '<<' | '>>' | '&' | '|' ) expr # BinaryOpExpr
488+
| expr {IsPostgreSql}?
489+
/* '?' expr is recogify with PLACEHOLDER2. */
490+
/* so, currently ignore this expr. */
491+
op=( '->' | '->>' | '#>' | '#>>' | '#-' )
492+
expr # BinaryOpExpr
484493
| substring_function # SubstrFuncExpr
485494
| extract_function # ExtractFuncExpr
486495
| aggregate_function1 # AggregateFuncExpr
@@ -702,6 +711,7 @@ identifiable_keyword
702711
| K_SECOND
703712
/* | K_SELECT */
704713
/* | K_SET */
714+
| K_SIMILAR
705715
| K_SKIP
706716
| K_SOME
707717
| K_SUM
@@ -792,6 +802,15 @@ EQ : '==';
792802
NOT_EQ1 : '!=';
793803
NOT_EQ2 : '<>';
794804
OUTER_JOIN : '(+)';
805+
ARROW : '->';
806+
ARROW2 : '->>';
807+
S_GT : '#>';
808+
S_GT2 : '#>>';
809+
S_MINUS : '#-';
810+
AT_GT : '@>';
811+
AT_LT : '<@';
812+
QRY_PIPE : '?|';
813+
QRY_AMP : '?&';
795814

796815

797816
UINTEGER_LITERAL
@@ -985,6 +1004,7 @@ K_ROWS : R O W S;
9851004
K_SECOND : S E C O N D;
9861005
K_SELECT : S E L E C T;
9871006
K_SET : S E T;
1007+
K_SIMILAR : S I M I L A R;
9881008
K_SKIP : S K I P;
9891009
K_SOME : S O M E;
9901010
K_SUM : S U M;

MiniSqlParser/Parser/MiniSqlParser.interp

Lines changed: 23 additions & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)