Skip to content

Commit 2730ae4

Browse files
Expression language (#363)
* Configured the sql package Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Bootstrap implementation Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Literal done Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * More progress Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Progress Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Sync contract Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Fix type cohercion for event type system Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * In expression + sync grammar Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Implemented binary expressions Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Implemented Like expression Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Big refactor Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * More testing Fix math Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Implemented all the functions! Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Refactored logical expressions implementation More testing Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * More coverage and tests Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Fixed ConcatFunction and added ConcatWSFunction Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Fixed IN type casting Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Added ABS function Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Fix SUBSTRING implementation Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * More nits Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * WIP Javadoc-ing Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Fix division by 0 Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Bootstrapped TCK Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Added comparison operators to tck Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Added logical operators, case sensitivity and casting functions Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Copied all the tests to the tck Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Removed Java tests now covered by the TCK Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Added integer builtin test case Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Added fail fast evaluation mode Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * More changes More Javadoc Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Typo Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Fix bad javadoc Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Another CONCAT_WS test case Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Import yaml just for testing Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
1 parent 30fd676 commit 2730ae4

File tree

90 files changed

+4284
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+4284
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<module>http/restful-ws</module>
7777
<module>kafka</module>
7878
<module>spring</module>
79+
<module>sql</module>
7980
</modules>
8081

8182
<properties>

sql/pom.xml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>cloudevents-parent</artifactId>
7+
<groupId>io.cloudevents</groupId>
8+
<version>2.1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>cloudevents-sql</artifactId>
13+
14+
<properties>
15+
<module-name>io.cloudevents.sql</module-name>
16+
<antlr.version>4.9.2</antlr.version>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>io.cloudevents</groupId>
22+
<artifactId>cloudevents-api</artifactId>
23+
<version>${project.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.antlr</groupId>
27+
<artifactId>antlr4-runtime</artifactId>
28+
<version>${antlr.version}</version>
29+
</dependency>
30+
31+
<!-- Test deps -->
32+
<dependency>
33+
<groupId>org.junit.jupiter</groupId>
34+
<artifactId>junit-jupiter</artifactId>
35+
<version>${junit-jupiter.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.assertj</groupId>
40+
<artifactId>assertj-core</artifactId>
41+
<version>${assertj-core.version}</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>io.cloudevents</groupId>
46+
<artifactId>cloudevents-core</artifactId>
47+
<version>${project.version}</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.cloudevents</groupId>
52+
<artifactId>cloudevents-json-jackson</artifactId>
53+
<version>${project.version}</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.fasterxml.jackson.dataformat</groupId>
58+
<artifactId>jackson-dataformat-yaml</artifactId>
59+
<version>2.11.2</version>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>io.cloudevents</groupId>
64+
<artifactId>cloudevents-core</artifactId>
65+
<classifier>tests</classifier>
66+
<type>test-jar</type>
67+
<version>${project.version}</version>
68+
<scope>test</scope>
69+
</dependency>
70+
</dependencies>
71+
72+
<build>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.antlr</groupId>
76+
<artifactId>antlr4-maven-plugin</artifactId>
77+
<version>${antlr.version}</version>
78+
<executions>
79+
<execution>
80+
<goals>
81+
<goal>antlr4</goal>
82+
</goals>
83+
</execution>
84+
</executions>
85+
<configuration>
86+
<visitor>true</visitor>
87+
<listener>true</listener> <!-- TODO do we need the listener? -->
88+
</configuration>
89+
</plugin>
90+
</plugins>
91+
</build>
92+
93+
</project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
lexer grammar CESQLLexer;
2+
3+
// NOTE:
4+
// This grammar is case-sensitive, although CESQL keywords are case-insensitive.
5+
// In order to implement case-insensitivity, check out
6+
// https://github.com/antlr/antlr4/blob/master/doc/case-insensitive-lexing.md#custom-character-streams-approach
7+
8+
// Skip tab, carriage return and newlines
9+
10+
SPACE: [ \t\r\n]+ -> skip;
11+
12+
// Fragments for Literal primitives
13+
14+
fragment ID_LITERAL: [a-zA-Z0-9]+;
15+
fragment DQUOTA_STRING: '"' ( '\\'. | '""' | ~('"'| '\\') )* '"';
16+
fragment SQUOTA_STRING: '\'' ('\\'. | '\'\'' | ~('\'' | '\\'))* '\'';
17+
fragment INT_DIGIT: [0-9];
18+
fragment FN_LITERAL: [A-Z] [A-Z_]*;
19+
20+
// Constructors symbols
21+
22+
LR_BRACKET: '(';
23+
RR_BRACKET: ')';
24+
COMMA: ',';
25+
SINGLE_QUOTE_SYMB: '\'';
26+
DOUBLE_QUOTE_SYMB: '"';
27+
28+
fragment QUOTE_SYMB
29+
: SINGLE_QUOTE_SYMB | DOUBLE_QUOTE_SYMB
30+
;
31+
32+
// Operators
33+
// - Logic
34+
35+
AND: 'AND';
36+
OR: 'OR';
37+
XOR: 'XOR';
38+
NOT: 'NOT';
39+
40+
// - Arithmetics
41+
42+
STAR: '*';
43+
DIVIDE: '/';
44+
MODULE: '%';
45+
PLUS: '+';
46+
MINUS: '-';
47+
48+
// - Comparison
49+
50+
EQUAL: '=';
51+
NOT_EQUAL: '!=';
52+
GREATER: '>';
53+
GREATER_OR_EQUAL: '>=';
54+
LESS: '<';
55+
LESS_GREATER: '<>';
56+
LESS_OR_EQUAL: '<=';
57+
58+
// Like, exists, in
59+
60+
LIKE: 'LIKE';
61+
EXISTS: 'EXISTS';
62+
IN: 'IN';
63+
64+
// Booleans
65+
66+
TRUE: 'TRUE';
67+
FALSE: 'FALSE';
68+
69+
// Literals
70+
71+
DQUOTED_STRING_LITERAL: DQUOTA_STRING;
72+
SQUOTED_STRING_LITERAL: SQUOTA_STRING;
73+
INTEGER_LITERAL: INT_DIGIT+;
74+
75+
// Identifiers
76+
77+
IDENTIFIER: [a-zA-Z]+;
78+
IDENTIFIER_WITH_NUMBER: [a-zA-Z0-9]+;
79+
FUNCTION_IDENTIFIER_WITH_UNDERSCORE: [A-Z] [A-Z_]*;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
grammar CESQLParser;
2+
3+
import CESQLLexer;
4+
5+
// Entrypoint
6+
cesql: expression EOF;
7+
8+
// Structure of operations, function invocations and expression
9+
expression
10+
: functionIdentifier functionParameterList #functionInvocationExpression
11+
// unary operators are the highest priority
12+
| NOT expression #unaryLogicExpression
13+
| MINUS expression # unaryNumericExpression
14+
// LIKE, EXISTS and IN takes precedence over all the other binary operators
15+
| expression NOT? LIKE stringLiteral #likeExpression
16+
| EXISTS identifier #existsExpression
17+
| expression NOT? IN setExpression #inExpression
18+
// Numeric operations
19+
| expression (STAR | DIVIDE | MODULE) expression #binaryMultiplicativeExpression
20+
| expression (PLUS | MINUS) expression #binaryAdditiveExpression
21+
// Comparison operations
22+
| expression (EQUAL | NOT_EQUAL | LESS_GREATER | GREATER_OR_EQUAL | LESS_OR_EQUAL | LESS | GREATER) expression #binaryComparisonExpression
23+
// Logic operations
24+
|<assoc=right> expression (AND | OR | XOR) expression #binaryLogicExpression
25+
// Subexpressions and atoms
26+
| LR_BRACKET expression RR_BRACKET #subExpression
27+
| atom #atomExpression
28+
;
29+
30+
atom
31+
: booleanLiteral #booleanAtom
32+
| integerLiteral #integerAtom
33+
| stringLiteral #stringAtom
34+
| identifier #identifierAtom
35+
;
36+
37+
// Identifiers
38+
39+
identifier
40+
: (IDENTIFIER | IDENTIFIER_WITH_NUMBER)
41+
;
42+
functionIdentifier
43+
: (IDENTIFIER | FUNCTION_IDENTIFIER_WITH_UNDERSCORE)
44+
;
45+
46+
// Literals
47+
48+
booleanLiteral: (TRUE | FALSE);
49+
stringLiteral: (DQUOTED_STRING_LITERAL | SQUOTED_STRING_LITERAL);
50+
integerLiteral: INTEGER_LITERAL;
51+
52+
// Functions
53+
54+
functionParameterList
55+
: LR_BRACKET ( expression ( COMMA expression )* )? RR_BRACKET
56+
;
57+
58+
// Sets
59+
60+
setExpression
61+
: LR_BRACKET expression ( COMMA expression )* RR_BRACKET // Empty sets are not allowed
62+
;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.cloudevents.sql;
2+
3+
import org.antlr.v4.runtime.misc.Interval;
4+
5+
/**
6+
* This class wraps some elements of the evaluation context,
7+
* required to throw {@link EvaluationException} when an error occurs while evaluating a function.
8+
*/
9+
public interface EvaluationContext {
10+
11+
/**
12+
* @return the interval of the original expression string.
13+
*/
14+
Interval expressionInterval();
15+
16+
/**
17+
* @return the text of the original expression string.
18+
*/
19+
String expressionText();
20+
21+
/**
22+
* Append a new exception to the evaluation context.
23+
* This exception will be propagated back in the evaluation result.
24+
*
25+
* @param exception exception to append
26+
*/
27+
void appendException(EvaluationException exception);
28+
29+
/**
30+
* Append a new exception to the evaluation context.
31+
* This exception will be propagated back in the evaluation result.
32+
*
33+
* @param exceptionFactory exception factory, which will automatically include expression interval and text
34+
*/
35+
void appendException(EvaluationException.EvaluationExceptionFactory exceptionFactory);
36+
}

0 commit comments

Comments
 (0)