Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.

Commit c8e31ee

Browse files
committed
Merge remote-tracking branch 'apache/master' into METRON-590
2 parents f8d014c + ec41e73 commit c8e31ee

File tree

35 files changed

+1679
-373
lines changed

35 files changed

+1679
-373
lines changed

metron-platform/metron-common/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Using parens such as: "foo" : "\<ok\>" requires escaping; "foo": "\'\<ok\>\'"
108108
| [ `SYSTEM_PROPERTY_GET`](#system_property_get)|
109109
| [ `TO_DOUBLE`](#to_double)|
110110
| [ `TO_EPOCH_TIMESTAMP`](#to_epoch_timestamp)|
111+
| [ `TO_FLOAT`](#to_float)|
111112
| [ `TO_INTEGER`](#to_integer)|
112113
| [ `TO_LONG`](#to_long)|
113114
| [ `TO_LOWER`](#to_lower)|
@@ -436,6 +437,12 @@ MAP_GET`
436437
* format - DateTime format as a String
437438
* timezone - Optional timezone in String format
438439
* Returns: Epoch timestamp
440+
441+
### `TO_FOAT`
442+
* Description: Transforms the first argument to a float
443+
* Input:
444+
* input - Object of string or numeric type
445+
* Returns: Float version of the first argument
439446

440447
### `TO_INTEGER`
441448
* Description: Transforms the first argument to an integer
@@ -643,7 +650,7 @@ JOIN, LENGTH, MAAS_GET_ENDPOINT, MAAS_MODEL_APPLY, MAP_EXISTS, MAP_GET, MONTH, P
643650
REGEXP_MATCH, SPLIT, STARTS_WITH, STATS_ADD, STATS_COUNT, STATS_GEOMETRIC_MEAN, STATS_INIT,
644651
STATS_KURTOSIS, STATS_MAX, STATS_MEAN, STATS_MERGE, STATS_MIN, STATS_PERCENTILE,
645652
STATS_POPULATION_VARIANCE, STATS_QUADRATIC_MEAN, STATS_SD, STATS_SKEWNESS, STATS_SUM,
646-
STATS_SUM_LOGS, STATS_SUM_SQUARES, STATS_VARIANCE, TO_DOUBLE, TO_EPOCH_TIMESTAMP,
653+
STATS_SUM_LOGS, STATS_SUM_SQUARES, STATS_VARIANCE, TO_DOUBLE, TO_EPOCH_TIMESTAMP, TO_FLOAT,
647654
TO_INTEGER, TO_LOWER, TO_STRING, TO_UPPER, TRIM, URL_TO_HOST, URL_TO_PATH, URL_TO_PORT,
648655
URL_TO_PROTOCOL, WEEK_OF_MONTH, WEEK_OF_YEAR, YEAR
649656
[Stellar]>>>

metron-platform/metron-common/src/main/antlr4/org/apache/metron/common/stellar/generated/Stellar.g4

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,22 @@ NIN : 'not in'
8888
;
8989
EXISTS : 'exists' | 'EXISTS';
9090
EXPONENT : ('e' | 'E') ( PLUS|MINUS )? ('0'..'9')+;
91-
INT_LITERAL : MINUS? '0'..'9'+ ;
92-
DOUBLE_LITERAL : MINUS? '0'..'9'+'.''0'..'9'+EXPONENT? ;
91+
INT_LITERAL :
92+
MINUS? '0'
93+
| MINUS? '1'..'9''0'..'9'*
94+
;
95+
DOUBLE_LITERAL :
96+
INT_LITERAL '.' '0'..'9'* EXPONENT? ('d'|'D')?
97+
| '.' '0'..'9'+ EXPONENT? ('d'|'D')?
98+
| INT_LITERAL EXPONENT ('d'|'D')?
99+
| INT_LITERAL EXPONENT? ('d'|'D')
100+
;
101+
FLOAT_LITERAL :
102+
INT_LITERAL'.''0'..'9'* EXPONENT? ('f'|'F')
103+
| MINUS? '.''0'..'9'+ EXPONENT? ('f'|'F')
104+
| INT_LITERAL EXPONENT? ('f'|'F')
105+
;
106+
LONG_LITERAL : INT_LITERAL ('l'|'L') ;
93107
IDENTIFIER : [a-zA-Z_][a-zA-Z_\.:0-9]* ;
94108
fragment SCHAR: ~['"\\\r\n];
95109
STRING_LITERAL : '"' SCHAR* '"'
@@ -168,6 +182,8 @@ functions : IDENTIFIER func_args #TransformationFunc
168182
arithmetic_operands : functions #NumericFunctions
169183
| DOUBLE_LITERAL #DoubleLiteral
170184
| INT_LITERAL #IntLiteral
185+
| LONG_LITERAL #LongLiteral
186+
| FLOAT_LITERAL #FloatLiteral
171187
| IDENTIFIER #Variable
172188
| LPAREN arithmetic_expr RPAREN #ParenArith
173189
| LPAREN conditional_expr RPAREN#condExpr

metron-platform/metron-common/src/main/java/Stellar.tokens

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ EXISTS=31
3232
EXPONENT=32
3333
INT_LITERAL=33
3434
DOUBLE_LITERAL=34
35-
IDENTIFIER=35
36-
STRING_LITERAL=36
37-
COMMENT=37
38-
WS=38
35+
FLOAT_LITERAL=35
36+
LONG_LITERAL=36
37+
IDENTIFIER=37
38+
STRING_LITERAL=38
39+
COMMENT=39
40+
WS=40
3941
','=1
4042
'=='=7
4143
'!='=8

metron-platform/metron-common/src/main/java/StellarLexer.tokens

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ EXISTS=31
3232
EXPONENT=32
3333
INT_LITERAL=33
3434
DOUBLE_LITERAL=34
35-
IDENTIFIER=35
36-
STRING_LITERAL=36
37-
COMMENT=37
38-
WS=38
35+
FLOAT_LITERAL=35
36+
LONG_LITERAL=36
37+
IDENTIFIER=37
38+
STRING_LITERAL=38
39+
COMMENT=39
40+
WS=40
3941
','=1
4042
'=='=7
4143
'!='=8

metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/BaseStellarProcessor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Licensed to the Apache Software Foundation (ASF) under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
@@ -24,9 +24,12 @@
2424

2525
import java.util.HashSet;
2626
import java.util.Set;
27+
import java.util.Stack;
28+
2729
import org.apache.metron.common.dsl.*;
2830
import org.apache.metron.common.dsl.functions.resolver.FunctionResolver;
2931
import org.apache.metron.common.stellar.evaluators.ArithmeticEvaluator;
32+
import org.apache.metron.common.stellar.evaluators.NumberLiteralEvaluator;
3033
import org.apache.metron.common.stellar.generated.StellarBaseListener;
3134
import org.apache.metron.common.stellar.generated.StellarLexer;
3235
import org.apache.metron.common.stellar.generated.StellarParser;
@@ -83,7 +86,10 @@ public T parse( String rule
8386
TokenStream tokens = new CommonTokenStream(lexer);
8487
StellarParser parser = new StellarParser(tokens);
8588

86-
StellarCompiler treeBuilder = new StellarCompiler(variableResolver, functionResolver, context, new ArithmeticEvaluator());
89+
StellarCompiler treeBuilder = new StellarCompiler(variableResolver, functionResolver, context, new Stack<>(),
90+
ArithmeticEvaluator.INSTANCE,
91+
NumberLiteralEvaluator.INSTANCE
92+
);
8793
parser.addParseListener(treeBuilder);
8894
parser.removeErrorListeners();
8995
parser.addErrorListener(new ErrorListener());

metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Licensed to the Apache Software Foundation (ASF) under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
@@ -29,6 +29,7 @@
2929
import org.apache.metron.common.dsl.Token;
3030
import org.apache.metron.common.dsl.VariableResolver;
3131
import org.apache.metron.common.stellar.evaluators.ArithmeticEvaluator;
32+
import org.apache.metron.common.stellar.evaluators.NumberLiteralEvaluator;
3233
import org.apache.metron.common.stellar.generated.StellarBaseListener;
3334
import org.apache.metron.common.stellar.generated.StellarParser;
3435
import org.apache.metron.common.utils.ConversionUtils;
@@ -46,18 +47,26 @@
4647

4748
public class StellarCompiler extends StellarBaseListener {
4849

49-
private Context context = null;
50-
private Stack<Token> tokenStack = new Stack<>();
51-
private FunctionResolver functionResolver;
52-
private VariableResolver variableResolver;
53-
private Throwable actualException = null;
50+
private final Context context;
51+
private final Stack<Token<?>> tokenStack;
52+
private final FunctionResolver functionResolver;
53+
private final VariableResolver variableResolver;
54+
private Throwable actualException;
5455
private final ArithmeticEvaluator arithmeticEvaluator;
55-
56-
public StellarCompiler(VariableResolver variableResolver, FunctionResolver functionResolver, Context context, ArithmeticEvaluator arithmeticEvaluator) {
56+
private final NumberLiteralEvaluator numberLiteralEvaluator;
57+
58+
public StellarCompiler(VariableResolver variableResolver,
59+
FunctionResolver functionResolver,
60+
Context context,
61+
Stack<Token<?>> tokenStack,
62+
ArithmeticEvaluator arithmeticEvaluator,
63+
NumberLiteralEvaluator numberLiteralEvaluator) {
5764
this.variableResolver = variableResolver;
5865
this.functionResolver = functionResolver;
5966
this.context = context;
67+
this.tokenStack = tokenStack;
6068
this.arithmeticEvaluator = arithmeticEvaluator;
69+
this.numberLiteralEvaluator = numberLiteralEvaluator;
6170
}
6271

6372
@Override
@@ -174,12 +183,22 @@ public void exitStringLiteral(StellarParser.StringLiteralContext ctx) {
174183

175184
@Override
176185
public void exitIntLiteral(StellarParser.IntLiteralContext ctx) {
177-
tokenStack.push(new Token<>(Integer.parseInt(ctx.getText()), Integer.class));
186+
tokenStack.push(numberLiteralEvaluator.evaluate(ctx));
178187
}
179188

180189
@Override
181190
public void exitDoubleLiteral(StellarParser.DoubleLiteralContext ctx) {
182-
tokenStack.push(new Token<>(Double.parseDouble(ctx.getText()), Double.class));
191+
tokenStack.push(numberLiteralEvaluator.evaluate(ctx));
192+
}
193+
194+
@Override
195+
public void exitFloatLiteral(StellarParser.FloatLiteralContext ctx) {
196+
tokenStack.push(numberLiteralEvaluator.evaluate(ctx));
197+
}
198+
199+
@Override
200+
public void exitLongLiteral(StellarParser.LongLiteralContext ctx) {
201+
tokenStack.push(numberLiteralEvaluator.evaluate(ctx));
183202
}
184203

185204
@Override

metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ArithmeticEvaluator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
import java.util.function.BiFunction;
2626

27-
public class ArithmeticEvaluator {
27+
public enum ArithmeticEvaluator {
28+
INSTANCE;
2829

2930
public Token<? extends Number> evaluate(BiFunction<Number, Number, Token<? extends Number>> function,
3031
Pair<Token<? extends Number>, Token<? extends Number>> p) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.metron.common.stellar.evaluators;
20+
21+
import org.apache.metron.common.dsl.Token;
22+
import org.apache.metron.common.stellar.generated.StellarParser;
23+
24+
public class DoubleLiteralEvaluator implements NumberEvaluator<StellarParser.DoubleLiteralContext> {
25+
@Override
26+
public Token<Double> evaluate(StellarParser.DoubleLiteralContext context) {
27+
if (context == null) {
28+
throw new IllegalArgumentException("Cannot evaluate a context that is null.");
29+
}
30+
31+
return new Token<>(Double.parseDouble(context.getText()), Double.class);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.metron.common.stellar.evaluators;
20+
21+
import org.apache.metron.common.dsl.Token;
22+
import org.apache.metron.common.stellar.generated.StellarParser;
23+
24+
public class FloatLiteralEvaluator implements NumberEvaluator<StellarParser.FloatLiteralContext> {
25+
@Override
26+
public Token<Float> evaluate(StellarParser.FloatLiteralContext context) {
27+
if (context == null) {
28+
throw new IllegalArgumentException("Cannot evaluate a context that is null.");
29+
}
30+
31+
return new Token<>(Float.parseFloat(context.getText()), Float.class);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.metron.common.stellar.evaluators;
20+
21+
import org.apache.metron.common.dsl.Token;
22+
import org.apache.metron.common.stellar.generated.StellarParser;
23+
24+
public class IntLiteralEvaluator implements NumberEvaluator<StellarParser.IntLiteralContext> {
25+
@Override
26+
public Token<Integer> evaluate(StellarParser.IntLiteralContext context) {
27+
if (context == null) {
28+
throw new IllegalArgumentException("Cannot evaluate a context that is null.");
29+
}
30+
31+
return new Token<>(Integer.parseInt(context.getText()), Integer.class);
32+
}
33+
}

0 commit comments

Comments
 (0)