org.apache.flink.table.expressions.Expression Java Examples

The following examples show how to use org.apache.flink.table.expressions.Expression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExistingField.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link Expression} that casts a {@link Long}, {@link Timestamp}, or
 * timestamp formatted {@link String} field (e.g., "2018-05-28 12:34:56.000")
 * into a rowtime attribute.
 */
@Override
public Expression getExpression(ResolvedFieldReference[] fieldAccesses) {
	ResolvedFieldReference fieldAccess = fieldAccesses[0];
	DataType type = fromLegacyInfoToDataType(fieldAccess.resultType());

	FieldReferenceExpression fieldReferenceExpr = new FieldReferenceExpression(
			fieldAccess.name(),
			type,
			0,
			fieldAccess.fieldIndex());

	switch (type.getLogicalType().getTypeRoot()) {
		case BIGINT:
		case TIMESTAMP_WITHOUT_TIME_ZONE:
			return fieldReferenceExpr;
		case VARCHAR:
			return unresolvedCall(
					CAST,
					fieldReferenceExpr,
					typeLiteral(TIMESTAMP(3).bridgedTo(Timestamp.class)));
		default:
			throw new RuntimeException("Unsupport type: " + type);
	}
}
 
Example #2
Source File: ExpressionConverter.java    From flink with Apache License 2.0 6 votes vote down vote up
private ConvertContext newFunctionContext() {
	return new ConvertContext() {
		@Override
		public RexNode toRexNode(Expression expr) {
			return expr.accept(ExpressionConverter.this);
		}

		@Override
		public RelBuilder getRelBuilder() {
			return relBuilder;
		}

		@Override
		public FlinkTypeFactory getTypeFactory() {
			return typeFactory;
		}

		@Override
		public DataTypeFactory getDataTypeFactory() {
			return dataTypeFactory;
		}
	};
}
 
Example #3
Source File: TableImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
private Table addColumnsOperation(boolean replaceIfExist, List<Expression> fields) {
	List<Expression> expressionsWithResolvedCalls = preprocessExpressions(fields);
	CategorizedExpressions extracted = OperationExpressionsUtils.extractAggregationsAndProperties(
		expressionsWithResolvedCalls
	);

	List<Expression> aggNames = extracted.getAggregations();

	if (!aggNames.isEmpty()) {
		throw new ValidationException(
			"The added field expression cannot be an aggregation, found: " + aggNames.get(0));
	}

	return createTable(operationTreeBuilder.addColumns(
		replaceIfExist, expressionsWithResolvedCalls, operationTree));
}
 
Example #4
Source File: ProjectionOperationFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
private Optional<String> extractNameFromGet(CallExpression call) {
	Expression child = call.getChildren().get(0);
	ValueLiteralExpression key = (ValueLiteralExpression) call.getChildren().get(1);

	final LogicalType keyType = key.getOutputDataType().getLogicalType();

	final String keySuffix;
	if (hasRoot(keyType, INTEGER)) {
		keySuffix = "$_" + key.getValueAs(Integer.class)
			.orElseThrow(() -> new TableException("Integer constant excepted."));
	} else {
		keySuffix = "$" + key.getValueAs(String.class)
			.orElseThrow(() -> new TableException("Integer constant excepted."));
	}
	return child.accept(this).map(p -> p + keySuffix);
}
 
Example #5
Source File: FieldInfoUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Reference input fields by name:
 * All fields in the schema definition are referenced by name
 * (and possibly renamed using an alias (as). In this mode, fields can be reordered and
 * projected out. Moreover, we can define proctime and rowtime attributes at arbitrary
 * positions using arbitrary names (except those that exist in the result schema). This mode
 * can be used for any input type, including POJOs.
 *
 * <p>Reference input fields by position:
 * In this mode, fields are simply renamed. Event-time attributes can
 * replace the field on their position in the input data (if it is of correct type) or be
 * appended at the end. Proctime attributes must be appended at the end. This mode can only be
 * used if the input type has a defined field order (tuple, case class, Row) and no of fields
 * references a field of the input type.
 */
private static boolean isReferenceByPosition(CompositeType<?> ct, Expression[] fields) {
	if (!(ct instanceof TupleTypeInfoBase)) {
		return false;
	}

	List<String> inputNames = Arrays.asList(ct.getFieldNames());

	// Use the by-position mode if no of the fields exists in the input.
	// This prevents confusing cases like ('f2, 'f0, 'myName) for a Tuple3 where fields are renamed
	// by position but the user might assume reordering instead of renaming.
	return Arrays.stream(fields).allMatch(f -> {
		if (f instanceof UnresolvedCallExpression &&
			((UnresolvedCallExpression) f).getFunctionDefinition() == BuiltInFunctionDefinitions.AS &&
			f.getChildren().get(0) instanceof UnresolvedReferenceExpression) {
			return false;
		}
		if (f instanceof UnresolvedReferenceExpression) {
			return !inputNames.contains(((UnresolvedReferenceExpression) f).getName());
		}

		return true;
	});
}
 
Example #6
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visit(CallExpression unresolvedCall) {
	final Expression[] newChildren = unresolvedCall.getChildren().stream().map(expr -> {
		RexNode convertedNode = expr.accept(this);
		return (Expression) new RexPlannerExpression(convertedNode);
	}).toArray(Expression[]::new);

	UnresolvedCallExpression newCall = unresolvedCall(unresolvedCall.getFunctionDefinition(), newChildren);
	return expressionBridge.bridge(newCall).toRexNode(relBuilder);
}
 
Example #7
Source File: OverWindowResolverRule.java    From flink with Apache License 2.0 5 votes vote down vote up
private Expression calculateOverWindowFollowing(LocalOverWindow referenceWindow) {
	return referenceWindow.getFollowing().orElseGet(() -> {
			WindowKind kind = referenceWindow.getPreceding().accept(OVER_WINDOW_KIND_EXTRACTOR);
			if (kind == WindowKind.ROW) {
				return unresolvedCall(BuiltInFunctionDefinitions.CURRENT_ROW);
			} else {
				return unresolvedCall(BuiltInFunctionDefinitions.CURRENT_RANGE);
			}
		}
	);
}
 
Example #8
Source File: ExpandColumnFunctionsRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public List<Expression> visit(UnresolvedCallExpression unresolvedCall) {

	List<Expression> result;

	final FunctionDefinition definition = unresolvedCall.getFunctionDefinition();
	if (definition == WITH_COLUMNS) {
		result = resolveArgsOfColumns(unresolvedCall.getChildren(), false);
	} else if (definition == WITHOUT_COLUMNS) {
		result = resolveArgsOfColumns(unresolvedCall.getChildren(), true);
	} else {
		List<Expression> args = unresolvedCall.getChildren()
			.stream()
			.flatMap(c -> c.accept(this).stream())
			.collect(Collectors.toList());
		result = Collections.singletonList(unresolvedCall.replaceArgs(args));

		// validate alias
		if (definition == AS) {
			for (int i = 1; i < args.size(); ++i) {
				if (!(args.get(i) instanceof ValueLiteralExpression)) {
					final String errorMessage = args.stream()
						.map(Object::toString)
						.collect(Collectors.joining(", "));
					throw new ValidationException(String.format("Invalid AS, parameters are: [%s].", errorMessage));
				}
			}
		}
	}

	return result;
}
 
Example #9
Source File: OperationTreeBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
public Expression resolveExpression(Expression expression, QueryOperation... tableOperation) {
	ExpressionResolver resolver = ExpressionResolver.resolverFor(
		config,
		tableReferenceLookup,
		functionCatalog,
		typeFactory,
		tableOperation).build();

	return resolveSingleExpression(expression, resolver);
}
 
Example #10
Source File: ParquetTableSource.java    From flink with Apache License 2.0 5 votes vote down vote up
@Nullable
private FilterPredicate greaterThan(Expression exp, Tuple2<Column, Comparable> columnPair) {
	Preconditions.checkArgument(exp instanceof GreaterThan, "exp has to be GreaterThan");
	if (columnPair.f0 instanceof IntColumn) {
		return FilterApi.gt((IntColumn) columnPair.f0, (Integer) columnPair.f1);
	} else if (columnPair.f0 instanceof LongColumn) {
		return FilterApi.gt((LongColumn) columnPair.f0, (Long) columnPair.f1);
	} else if (columnPair.f0 instanceof DoubleColumn) {
		return FilterApi.gt((DoubleColumn) columnPair.f0, (Double) columnPair.f1);
	} else if (columnPair.f0 instanceof FloatColumn) {
		return FilterApi.gt((FloatColumn) columnPair.f0, (Float) columnPair.f1);
	}

	return null;
}
 
Example #11
Source File: TableImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
private AggregatedTableImpl(
		TableImpl table,
		List<Expression> groupKeys,
		Expression aggregateFunction) {
	this.table = table;
	this.groupKeys = groupKeys;
	this.aggregateFunction = aggregateFunction;
}
 
Example #12
Source File: StarReferenceFlatteningRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public List<Expression> visit(UnresolvedReferenceExpression unresolvedReference) {
	if (unresolvedReference.getName().equals("*")) {
		return new ArrayList<>(resolutionContext.referenceLookup().getAllInputFields());
	} else {
		return singletonList(unresolvedReference);
	}
}
 
Example #13
Source File: IncrSumWithRetractAggFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Expression[] initialValuesExpressions() {
	return new Expression[] {
			/* sum = */ nullOf(getResultType()),
			/* count = */ literal(0L)
	};
}
 
Example #14
Source File: OperationExpressionsUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Expression visit(UnresolvedCallExpression unresolvedCall) {
	if (aggregates.get(unresolvedCall) != null) {
		return unresolvedRef(aggregates.get(unresolvedCall));
	} else if (properties.get(unresolvedCall) != null) {
		return unresolvedRef(properties.get(unresolvedCall));
	}

	final List<Expression> args = unresolvedCall.getChildren()
		.stream()
		.map(c -> c.accept(this))
		.collect(Collectors.toList());
	return unresolvedCall.replaceArgs(args);
}
 
Example #15
Source File: DeclarativeExpressionResolver.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected ResolvedExpression defaultMethod(Expression expression) {
	if (expression instanceof UnresolvedReferenceExpression) {
		UnresolvedReferenceExpression expr = (UnresolvedReferenceExpression) expression;
		String name = expr.getName();
		int localIndex = ArrayUtils.indexOf(function.aggBufferAttributes(), expr);
		if (localIndex == -1) {
			// We always use UnresolvedFieldReference to represent reference of input field.
			// In non-merge case, the input is operand of the aggregate function. But in merge
			// case, the input is aggregate buffers which sent by local aggregate.
			if (isMerge) {
				return toMergeInputExpr(name, ArrayUtils.indexOf(function.mergeOperands(), expr));
			} else {
				return toAccInputExpr(name, ArrayUtils.indexOf(function.operands(), expr));
			}
		} else {
			return toAggBufferExpr(name, localIndex);
		}
	} else if (expression instanceof UnresolvedCallExpression) {
		UnresolvedCallExpression unresolvedCall = (UnresolvedCallExpression) expression;
		return resolver.resolve(ApiExpressionUtils.unresolvedCall(
			unresolvedCall.getFunctionDefinition(),
			unresolvedCall.getChildren().stream()
				.map(c -> c.accept(DeclarativeExpressionResolver.this))
				.collect(Collectors.toList())));
	} else if (expression instanceof ResolvedExpression) {
		return (ResolvedExpression) expression;
	} else {
		return resolver.resolve(expression);
	}
}
 
Example #16
Source File: OverWindowPartitionedOrderedPreceding.java    From flink with Apache License 2.0 5 votes vote down vote up
OverWindowPartitionedOrderedPreceding(
	List<Expression> partitionBy,
	Expression orderBy,
	Expression preceding) {
	this.partitionBy = partitionBy;
	this.orderBy = orderBy;
	this.preceding = preceding;
}
 
Example #17
Source File: ExpandColumnFunctionsRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public List<Expression> apply(List<Expression> expression, ResolutionContext context) {
	ColumnFunctionsExpander columnFunctionsExpander =
		new ColumnFunctionsExpander(
			context.referenceLookup().getAllInputFields().stream()
				.map(p -> unresolvedRef(p.getName()))
				.collect(Collectors.toList())
		);
	return expression.stream()
		.flatMap(expr -> expr.accept(columnFunctionsExpander).stream())
		.collect(Collectors.toList());
}
 
Example #18
Source File: TableImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
private WindowGroupedTableImpl(
		TableImpl table,
		List<Expression> groupKeys,
		GroupWindow window) {
	this.table = table;
	this.groupKeys = groupKeys;
	this.window = window;
}
 
Example #19
Source File: AggregateOperationFactory.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Extract names for the aggregate or the table aggregate expression. For a table aggregate, it
 * may return multi output names when the composite return type is flattened. If the result type
 * is not a {@link CompositeType}, the result name should not conflict with the group names.
 */
private Stream<String> extractAggregateNames(Expression expression, List<String> groupNames) {
	if (isFunctionOfKind(expression, TABLE_AGGREGATE)) {
		final TableAggregateFunctionDefinition definition =
			(TableAggregateFunctionDefinition) ((CallExpression) expression).getFunctionDefinition();
		return Arrays.stream(FieldInfoUtils.getFieldNames(definition.getResultTypeInfo(), groupNames));
	} else {
		return Stream.of(extractName(expression).orElseGet(expression::toString));
	}
}
 
Example #20
Source File: ExpressionResolver.java    From flink with Apache License 2.0 5 votes vote down vote up
private Function<List<Expression>, List<Expression>> concatenateRules(List<ResolverRule> rules) {
	return rules.stream()
		.reduce(
			Function.identity(),
			(function, resolverRule) -> function.andThen(exprs -> resolverRule.apply(exprs,
				new ExpressionResolverContext())),
			Function::andThen
		);
}
 
Example #21
Source File: ListAggFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Expression[] initialValuesExpressions() {
	return new Expression[] {
			/* delimiter */ literal(",", DataTypes.STRING().notNull()),
			/* acc */ nullOf(DataTypes.STRING())
	};
}
 
Example #22
Source File: FieldInfoUtilsTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testByNameModeReorder() {
	FieldInfoUtils.TypeInfoSchema schema = FieldInfoUtils.getFieldsInfo(
		typeInfo,
		new Expression[]{$("f2"), $("f1"), $("f0")});

	Assert.assertArrayEquals(new String[]{"f2", "f1", "f0"}, schema.getFieldNames());
}
 
Example #23
Source File: ResolveCallByArgumentsRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected List<ResolvedExpression> defaultMethod(Expression expression) {
	if (expression instanceof ResolvedExpression) {
		return Collections.singletonList((ResolvedExpression) expression);
	}
	throw new TableException("Unexpected unresolved expression: " + expression);
}
 
Example #24
Source File: MaxAggFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Expression[] accumulateExpressions() {
	return new Expression[] {
			/* max = */
			ifThenElse(isNull(operand(0)), max,
					ifThenElse(isNull(max), operand(0),
							ifThenElse(greaterThan(operand(0), max), operand(0), max)))
	};
}
 
Example #25
Source File: SumWithRetractAggFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Expression[] mergeExpressions() {
	return new Expression[] {
			/* sum = */
			ifThenElse(isNull(mergeOperand(sum)), sum,
					ifThenElse(isNull(sum), mergeOperand(sum), plus(sum, mergeOperand(sum)))),
			/* count = */
			plus(count, mergeOperand(count))
	};
}
 
Example #26
Source File: FieldInfoUtilsTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testByPositionMode() {
	FieldInfoUtils.TypeInfoSchema schema = FieldInfoUtils.getFieldsInfo(
		rowTypeInfo,
		new Expression[]{$("aa"), $("bb"), $("cc")});

	Assert.assertArrayEquals(new String[]{"aa", "bb", "cc"}, schema.getFieldNames());
}
 
Example #27
Source File: ExpressionResolver.java    From flink with Apache License 2.0 5 votes vote down vote up
private Function<List<Expression>, List<Expression>> concatenateRules(List<ResolverRule> rules) {
	return rules.stream()
		.reduce(
			Function.identity(),
			(function, resolverRule) -> function.andThen(exprs -> resolverRule.apply(exprs,
				new ExpressionResolverContext())),
			Function::andThen
		);
}
 
Example #28
Source File: FieldInfoUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private FieldInfo createProctimeFieldInfo(Expression expression, @Nullable String alias) {
	UnresolvedReferenceExpression reference = getChildAsReference(expression);
	String originalName = reference.getName();
	validateProctimeDoesNotReplaceField(originalName);

	return createTimeAttributeField(reference, TimestampKind.PROCTIME, alias);
}
 
Example #29
Source File: OperationExpressionsUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
public List<Expression> getProjections() {
	return projections;
}
 
Example #30
Source File: IncrSumAggFunction.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public Expression[] retractExpressions() {
	throw new TableException("This function does not support retraction, Please choose SumWithRetractAggFunction.");
}