use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.
the class TrimConverter method convert.
@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
checkArgumentNumber(call, 4);
List<Expression> children = call.getChildren();
ValueLiteralExpression removeLeadingExpr = (ValueLiteralExpression) children.get(0);
Boolean removeLeading = extractValue(removeLeadingExpr, Boolean.class);
ValueLiteralExpression removeTrailingExpr = (ValueLiteralExpression) children.get(1);
Boolean removeTrailing = extractValue(removeTrailingExpr, Boolean.class);
RexNode trimString = context.toRexNode(children.get(2));
RexNode str = context.toRexNode(children.get(3));
Enum trimMode;
if (removeLeading && removeTrailing) {
trimMode = SqlTrimFunction.Flag.BOTH;
} else if (removeLeading) {
trimMode = SqlTrimFunction.Flag.LEADING;
} else if (removeTrailing) {
trimMode = SqlTrimFunction.Flag.TRAILING;
} else {
throw new IllegalArgumentException("Unsupported trim mode.");
}
return context.getRelBuilder().call(FlinkSqlOperatorTable.TRIM, context.getRelBuilder().getRexBuilder().makeFlag(trimMode), trimString, str);
}
use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.
the class FilterUtils method getValue.
private static Comparable<?> getValue(Expression expr, Function<String, Comparable<?>> getter) {
if (expr instanceof ValueLiteralExpression) {
Optional<?> value = ((ValueLiteralExpression) expr).getValueAs(((ValueLiteralExpression) expr).getOutputDataType().getConversionClass());
return (Comparable<?>) value.orElse(null);
}
if (expr instanceof FieldReferenceExpression) {
return getter.apply(((FieldReferenceExpression) expr).getName());
}
if (expr instanceof CallExpression && expr.getChildren().size() == 1) {
Object child = getValue(expr.getChildren().get(0), getter);
FunctionDefinition functionDefinition = ((CallExpression) expr).getFunctionDefinition();
if (functionDefinition.equals(UPPER)) {
return child.toString().toUpperCase();
} else if (functionDefinition.equals(LOWER)) {
return child.toString().toLowerCase();
} else {
throw new UnsupportedOperationException(String.format("Unrecognized function definition: %s.", functionDefinition));
}
}
throw new UnsupportedOperationException(expr + " not supported!");
}
use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.
the class LogicalWindowJsonDeserializer method deserialize.
@Override
public LogicalWindow deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode jsonNode = jsonParser.readValueAsTree();
String kind = jsonNode.get(FIELD_NAME_KIND).asText().toUpperCase();
WindowReference alias = deserializationContext.readValue(jsonNode.get(FIELD_NAME_ALIAS).traverse(jsonParser.getCodec()), WindowReference.class);
FieldReferenceExpression timeField = deserializeFieldReferenceExpression(jsonNode.get(FIELD_NAME_TIME_FIELD), jsonParser, deserializationContext);
switch(kind) {
case KIND_TUMBLING:
boolean isTimeTumblingWindow = jsonNode.get(FIELD_NAME_IS_TIME_WINDOW).asBoolean();
if (isTimeTumblingWindow) {
Duration size = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SIZE), jsonParser.getCodec()), Duration.class);
return new TumblingGroupWindow(alias, timeField, new ValueLiteralExpression(size));
} else {
long size = jsonNode.get(FIELD_NAME_SIZE).asLong();
return new TumblingGroupWindow(alias, timeField, new ValueLiteralExpression(size));
}
case KIND_SLIDING:
boolean isTimeSlidingWindow = jsonNode.get(FIELD_NAME_IS_TIME_WINDOW).asBoolean();
if (isTimeSlidingWindow) {
Duration size = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SIZE), jsonParser.getCodec()), Duration.class);
Duration slide = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SLIDE), jsonParser.getCodec()), Duration.class);
return new SlidingGroupWindow(alias, timeField, new ValueLiteralExpression(size), new ValueLiteralExpression(slide));
} else {
long size = jsonNode.get(FIELD_NAME_SIZE).asLong();
long slide = jsonNode.get(FIELD_NAME_SLIDE).asLong();
return new SlidingGroupWindow(alias, timeField, new ValueLiteralExpression(size), new ValueLiteralExpression(slide));
}
case KIND_SESSION:
Duration gap = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_GAP), jsonParser.getCodec()), Duration.class);
return new SessionGroupWindow(alias, timeField, new ValueLiteralExpression(gap));
default:
throw new TableException("Unknown Logical Window:" + jsonNode);
}
}
use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.
the class LogicalWindowJsonSerializer method serialize.
@Override
public void serialize(LogicalWindow logicalWindow, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
gen.writeStartObject();
if (logicalWindow instanceof TumblingGroupWindow) {
TumblingGroupWindow window = (TumblingGroupWindow) logicalWindow;
gen.writeStringField(FIELD_NAME_KIND, KIND_TUMBLING);
serializerProvider.defaultSerializeField(FIELD_NAME_ALIAS, window.aliasAttribute(), gen);
FieldReferenceExpression timeField = logicalWindow.timeAttribute();
serializeFieldReferenceExpression(timeField, gen, serializerProvider);
ValueLiteralExpression size = window.size();
if (hasTimeIntervalType(size)) {
Duration duration = toDuration(size);
gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, true);
gen.writeObjectField(FIELD_NAME_SIZE, duration);
} else {
Long duration = toLong(size);
gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, false);
gen.writeNumberField(FIELD_NAME_SIZE, duration);
}
} else if (logicalWindow instanceof SlidingGroupWindow) {
SlidingGroupWindow window = (SlidingGroupWindow) logicalWindow;
gen.writeStringField(FIELD_NAME_KIND, KIND_SLIDING);
serializerProvider.defaultSerializeField(FIELD_NAME_ALIAS, window.aliasAttribute(), gen);
serializeFieldReferenceExpression(window.timeAttribute(), gen, serializerProvider);
ValueLiteralExpression size = window.size();
if (hasTimeIntervalType(size)) {
Duration duration = toDuration(size);
gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, true);
gen.writeObjectField(FIELD_NAME_SIZE, duration);
gen.writeObjectField(FIELD_NAME_SLIDE, toDuration(window.slide()));
} else {
Long duration = toLong(size);
gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, false);
gen.writeObjectField(FIELD_NAME_SIZE, duration);
gen.writeObjectField(FIELD_NAME_SLIDE, toLong(window.slide()));
}
} else if (logicalWindow instanceof SessionGroupWindow) {
gen.writeStringField(FIELD_NAME_KIND, KIND_SESSION);
SessionGroupWindow window = (SessionGroupWindow) logicalWindow;
serializerProvider.defaultSerializeField(FIELD_NAME_ALIAS, window.aliasAttribute(), gen);
serializeFieldReferenceExpression(window.timeAttribute(), gen, serializerProvider);
gen.writeObjectField(FIELD_NAME_GAP, toDuration(window.gap()));
} else {
throw new TableException("Unknown LogicalWindow: " + logicalWindow);
}
gen.writeEndObject();
}
use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.
the class OrcFileSystemFilterTest method testApplyPredicate.
@Test
@SuppressWarnings("unchecked")
public void testApplyPredicate() {
List<ResolvedExpression> args = new ArrayList<>();
// equal
FieldReferenceExpression fieldReferenceExpression = new FieldReferenceExpression("long1", DataTypes.BIGINT(), 0, 0);
ValueLiteralExpression valueLiteralExpression = new ValueLiteralExpression(10);
args.add(fieldReferenceExpression);
args.add(valueLiteralExpression);
CallExpression equalExpression = CallExpression.permanent(BuiltInFunctionDefinitions.EQUALS, args, DataTypes.BOOLEAN());
OrcFilters.Predicate predicate1 = OrcFilters.toOrcPredicate(equalExpression);
OrcFilters.Predicate predicate2 = new OrcFilters.Equals("long1", PredicateLeaf.Type.LONG, 10);
assertTrue(predicate1.toString().equals(predicate2.toString()));
// greater than
CallExpression greaterExpression = CallExpression.permanent(BuiltInFunctionDefinitions.GREATER_THAN, args, DataTypes.BOOLEAN());
OrcFilters.Predicate predicate3 = OrcFilters.toOrcPredicate(greaterExpression);
OrcFilters.Predicate predicate4 = new OrcFilters.Not(new OrcFilters.LessThanEquals("long1", PredicateLeaf.Type.LONG, 10));
assertTrue(predicate3.toString().equals(predicate4.toString()));
// less than
CallExpression lessExpression = CallExpression.permanent(BuiltInFunctionDefinitions.LESS_THAN, args, DataTypes.BOOLEAN());
OrcFilters.Predicate predicate5 = OrcFilters.toOrcPredicate(lessExpression);
OrcFilters.Predicate predicate6 = new OrcFilters.LessThan("long1", PredicateLeaf.Type.LONG, 10);
assertTrue(predicate5.toString().equals(predicate6.toString()));
}
Aggregations