use of com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression in project streamline by hortonworks.
the class GroovyExpressionTest method testNested.
@Test
public void testNested() throws Exception {
Condition condition = new Condition();
condition.setExpression(new BinaryExpression(Operator.LESS_THAN, new ArrayFieldExpression(new MapFieldExpression(getVariable("map"), "foo"), 100), getVariable("b")));
GroovyExpression expr = new GroovyExpression(condition);
Assert.assertEquals("map['foo'][100] < b", expr.asString());
}
use of com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression in project streamline by hortonworks.
the class GroovyExpressionTest method testArray.
@Test
public void testArray() throws Exception {
Condition condition = new Condition();
condition.setExpression(new BinaryExpression(Operator.LESS_THAN, new ArrayFieldExpression(getVariable("arr"), 100), getVariable("b")));
GroovyExpression expr = new GroovyExpression(condition);
Assert.assertEquals("arr[100] < b", expr.asString());
}
use of com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression in project streamline by hortonworks.
the class ExpressionGenerator method visitSqlSpecialOperator.
private Expression visitSqlSpecialOperator(SqlSpecialOperator specialOperator, List<SqlNode> operands) {
if (specialOperator.getName().equalsIgnoreCase("ITEM")) {
Expression left = operands.get(0).accept(this);
SqlNode right = operands.get(1);
if (right instanceof SqlNumericLiteral) {
SqlNumericLiteral index = (SqlNumericLiteral) right;
if (!index.isInteger()) {
throw new IllegalArgumentException("Invalid array index " + index);
}
return new ArrayFieldExpression(left, Integer.parseInt(index.toValue()));
} else if (right instanceof SqlCharStringLiteral) {
String key = ((SqlCharStringLiteral) right).toValue();
return new MapFieldExpression(left, key);
} else {
throw new IllegalArgumentException("Item right operand '" + right + "' must be numeric or character type");
}
} else if (specialOperator.getName().equalsIgnoreCase("AS")) {
Expression left = operands.get(0).accept(this);
String alias = operands.get(1).toString();
return new AsExpression(left, alias);
} else {
throw new UnsupportedOperationException("Operator " + specialOperator + " not implemented");
}
}
use of com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression in project streamline by hortonworks.
the class SqlNestedExprScriptTest method testEvaluateNestedMapList.
// @Test
public void testEvaluateNestedMapList() throws Exception {
Condition condition = new Condition();
Expression y_a_0 = new ArrayFieldExpression(new MapFieldExpression(new FieldExpression(Schema.Field.of("y", Schema.Type.NESTED)), "a"), 0);
condition.setExpression(new BinaryExpression(Operator.LESS_THAN, y_a_0, new Literal("100")));
sqlScript = new SqlScript(new StormSqlExpression(condition), new SqlEngine(), new SqlScript.CorrelatedValuesToStreamlineEventConverter(Collections.singletonList("y")));
List<Integer> nestedList = new ArrayList<>();
nestedList.add(500);
nestedList.add(1);
Map<String, Object> nestedMap = new HashMap<>();
nestedMap.put("a", nestedList);
Map<String, Object> kv = new HashMap<>();
kv.put("x", 10);
kv.put("y", nestedMap);
StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(kv).dataSourceId("1").build();
Collection<StreamlineEvent> result = sqlScript.evaluate(event);
Assert.assertTrue(result.isEmpty());
}
Aggregations