Skip to content

Commit 010627e

Browse files
Cali0707pierDipi
andauthored
CE SQL v1 (#641)
- add exception factory for cesql exceptions - extend EvaluationResult to be usable internally - expressions use results instead of a thrower interface - functions use results instead of a thrower interface - parser handles not equals correctly, does not eagerly evaluate when there may be an error - parser handles integer literals properly - updated test files to test v1 spec Signed-off-by: Calum Murray <[email protected]> Co-authored-by: Pierangelo Di Pilato <[email protected]>
1 parent a790482 commit 010627e

File tree

80 files changed

+839
-702
lines changed

Some content is hidden

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

80 files changed

+839
-702
lines changed

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@
169169
<link>https://docs.spring.io/spring-framework/docs/current/javadoc-api/</link>
170170
<link>https://jakarta.ee/specifications/platform/8/apidocs/</link>
171171
<link>https://kafka.apache.org/30/javadoc/</link>
172-
<link>https://qpid.apache.org/releases/qpid-proton-j-0.33.7/api/</link>
173172
<link>https://fasterxml.github.io/jackson-databind/javadoc/2.10/</link>
174173
</links>
175174
<source>8</source>

sql/src/main/antlr4/imports/CESQLLexer.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ FALSE: 'FALSE';
7070

7171
DQUOTED_STRING_LITERAL: DQUOTA_STRING;
7272
SQUOTED_STRING_LITERAL: SQUOTA_STRING;
73-
INTEGER_LITERAL: INT_DIGIT+;
73+
INTEGER_LITERAL: ('+' | '-')? INT_DIGIT+;
7474

7575
// Identifiers
7676

sql/src/main/java/io/cloudevents/sql/EvaluationContext.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,5 @@ public interface EvaluationContext {
1818
*/
1919
String expressionText();
2020

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);
21+
ExceptionFactory exceptionFactory();
3622
}

sql/src/main/java/io/cloudevents/sql/EvaluationException.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,27 @@ public enum ErrorKind {
2020
/**
2121
* An implicit or an explicit casting failed.
2222
*/
23-
INVALID_CAST,
23+
CAST,
2424
/**
2525
* An event attribute was addressed, but missing.
2626
*/
2727
MISSING_ATTRIBUTE,
2828
/**
2929
* Error happened while dispatching a function invocation. Reasons may be invalid function name or invalid arguments number.
3030
*/
31-
FUNCTION_DISPATCH,
31+
MISSING_FUNCTION,
3232
/**
3333
* Error happened while executing a function. This usually contains a non null cause.
3434
*/
35-
FUNCTION_EXECUTION,
35+
FUNCTION_EVALUATION,
3636
/**
3737
* Error happened while executing a math operation. Reason may be a division by zero.
3838
*/
39-
MATH
39+
MATH,
40+
/**
41+
* Any error that does not fall into the other error kinds
42+
*/
43+
GENERIC,
4044
}
4145

4246
private final ErrorKind errorKind;

sql/src/main/java/io/cloudevents/sql/EvaluationRuntime.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,6 @@
77
* The evaluation runtime takes care of the function resolution, casting and other core functionalities to execute an expression.
88
*/
99
public interface EvaluationRuntime {
10-
11-
/**
12-
* Check if the cast can be executed from {@code value} to the {@code target} type.
13-
*
14-
* @param value the value to cast
15-
* @param target the type cast target
16-
* @return false if the cast trigger an error, true otherwise.
17-
*/
18-
boolean canCast(Object value, Type target);
19-
20-
/**
21-
* Return the {@code value} casted to the {@code target} type.
22-
*
23-
* @param ctx the evaluation context
24-
* @param value the value to cast
25-
* @param target the type cast target
26-
* @return the casted value, if the cast succeeds, otherwise the default value of the target type
27-
*/
28-
Object cast(EvaluationContext ctx, Object value, Type target);
29-
30-
/**
31-
* Return the {@code value} casted to the {@code target} type. If fails, this is going to throw an exception without the evaluation context.
32-
*
33-
* @param value the value to cast
34-
* @param target the type cast target
35-
* @return the casted value, if the cast succeeds, otherwise the default value of the target type
36-
*/
37-
Object cast(Object value, Type target) throws EvaluationException;
38-
3910
/**
4011
* Resolve a {@link Function} starting from its name and the concrete number of arguments.
4112
*
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.cloudevents.sql;
2+
3+
import org.antlr.v4.runtime.RecognitionException;
4+
import org.antlr.v4.runtime.misc.Interval;
5+
import org.antlr.v4.runtime.tree.ParseTree;
6+
7+
public interface ExceptionFactory {
8+
EvaluationException.EvaluationExceptionFactory invalidCastTarget(Class<?> from, Class<?> to);
9+
10+
EvaluationException.EvaluationExceptionFactory castError(Class<?> from, Class<?> to, Throwable cause);
11+
12+
EvaluationException missingAttribute(Interval interval, String expression, String key);
13+
14+
EvaluationException cannotDispatchFunction(Interval interval, String expression, String functionName, Throwable cause);
15+
16+
EvaluationException.EvaluationExceptionFactory functionExecutionError(String functionName, Throwable cause);
17+
18+
EvaluationException divisionByZero(Interval interval, String expression, Integer dividend);
19+
20+
EvaluationException mathError(Interval interval, String expression, String errorMessage);
21+
22+
static ParseException cannotParseValue(ParseTree node, Type target, Throwable cause) {
23+
return new ParseException(
24+
ParseException.ErrorKind.PARSE_VALUE,
25+
node.getSourceInterval(),
26+
node.getText(),
27+
"Cannot parse to " + target.name() + ": " + cause.getMessage(),
28+
cause
29+
);
30+
}
31+
32+
static ParseException recognitionError(RecognitionException e, String msg) {
33+
return new ParseException(
34+
ParseException.ErrorKind.RECOGNITION,
35+
new Interval(e.getOffendingToken().getStartIndex(), e.getOffendingToken().getStopIndex()),
36+
e.getOffendingToken().getText(),
37+
"Cannot parse: " + msg,
38+
e
39+
);
40+
}
41+
42+
static ParseException cannotEvaluateConstantExpression(EvaluationException exception) {
43+
return new ParseException(
44+
ParseException.ErrorKind.CONSTANT_EXPRESSION_EVALUATION,
45+
exception.getExpressionInterval(),
46+
exception.getExpressionText(),
47+
"Cannot evaluate the constant expression: " + exception.getExpressionText(),
48+
exception
49+
);
50+
}
51+
}

sql/src/main/java/io/cloudevents/sql/Function.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.cloudevents.sql;
22

33
import io.cloudevents.CloudEvent;
4+
import io.cloudevents.sql.impl.runtime.EvaluationResult;
45

56
import java.util.List;
67

@@ -15,9 +16,9 @@ public interface Function extends FunctionSignature {
1516
* @param ctx the evaluation context
1617
* @param evaluationRuntime the evaluation runtime
1718
* @param event the expression input event
18-
* @param arguments the arguments passed to this function. Note: the arguments are already casted to the appropriate type declared in the signature
19+
* @param arguments the arguments passed to this function. Note: the arguments are already cast to the appropriate type declared in the signature
1920
* @return the return value of the function
2021
*/
21-
Object invoke(EvaluationContext ctx, EvaluationRuntime evaluationRuntime, CloudEvent event, List<Object> arguments);
22+
EvaluationResult invoke(EvaluationContext ctx, EvaluationRuntime evaluationRuntime, CloudEvent event, List<Object> arguments);
2223

2324
}

sql/src/main/java/io/cloudevents/sql/FunctionSignature.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public interface FunctionSignature {
1818
*/
1919
Type typeOfParameter(int i) throws IllegalArgumentException;
2020

21+
/**
22+
* @return function return type
23+
*/
24+
Type returnType();
25+
2126
/**
2227
* @return the arity, excluding the vararg parameter if {@code isVariadic() == true}
2328
*/

sql/src/main/java/io/cloudevents/sql/impl/ExceptionFactory.java

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)