use of org.apache.flink.table.planner.calcite.FlinkTypeFactory in project flink by apache.
the class JsonValueConverter method convert.
@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
checkArgumentNumber(call, 7);
final List<RexNode> operands = new LinkedList<>();
operands.add(context.toRexNode(call.getChildren().get(0)));
operands.add(context.toRexNode(call.getChildren().get(1)));
operands.addAll(getBehaviorOperands(call, context, SqlJsonEmptyOrError.EMPTY));
operands.addAll(getBehaviorOperands(call, context, SqlJsonEmptyOrError.ERROR));
final FlinkTypeFactory typeFactory = unwrapTypeFactory(context.getRelBuilder());
final RelDataType returnRelType = typeFactory.createFieldTypeFromLogicalType(call.getOutputDataType().getLogicalType());
return context.getRelBuilder().getRexBuilder().makeCall(returnRelType, FlinkSqlOperatorTable.JSON_VALUE, operands);
}
use of org.apache.flink.table.planner.calcite.FlinkTypeFactory in project flink by apache.
the class CodeSplitTest method testJoinCondition.
@Test
public void testJoinCondition() {
int numFields = 200;
FlinkTypeFactory typeFactory = FlinkTypeFactory.INSTANCE();
RexBuilder builder = new RexBuilder(typeFactory);
RelDataType intType = typeFactory.createFieldTypeFromLogicalType(new IntType());
RexNode[] conditions = new RexNode[numFields];
for (int i = 0; i < numFields; i++) {
conditions[i] = builder.makeCall(SqlStdOperatorTable.LESS_THAN, new RexInputRef(i, intType), new RexInputRef(numFields + i, intType));
}
RexNode joinCondition = builder.makeCall(SqlStdOperatorTable.AND, conditions);
RowType rowType = getIntRowType(numFields);
GenericRowData rowData1 = new GenericRowData(numFields);
GenericRowData rowData2 = new GenericRowData(numFields);
Random random = new Random();
for (int i = 0; i < numFields; i++) {
rowData1.setField(i, 0);
rowData2.setField(i, 1);
}
boolean result = random.nextBoolean();
if (!result) {
rowData1.setField(random.nextInt(numFields), 1);
}
Consumer<TableConfig> consumer = tableConfig -> {
JoinCondition instance = JoinUtil.generateConditionFunction(tableConfig, joinCondition, rowType, rowType).newInstance(classLoader);
for (int i = 0; i < 100; i++) {
Assert.assertEquals(result, instance.apply(rowData1, rowData2));
}
};
runTest(consumer);
}
use of org.apache.flink.table.planner.calcite.FlinkTypeFactory in project flink by apache.
the class OverConvertRule method convert.
@Override
public Optional<RexNode> convert(CallExpression call, ConvertContext context) {
List<Expression> children = call.getChildren();
if (call.getFunctionDefinition() == BuiltInFunctionDefinitions.OVER) {
FlinkTypeFactory typeFactory = context.getTypeFactory();
Expression agg = children.get(0);
FunctionDefinition def = ((CallExpression) agg).getFunctionDefinition();
boolean isDistinct = BuiltInFunctionDefinitions.DISTINCT == def;
SqlAggFunction aggFunc = agg.accept(new SqlAggFunctionVisitor(context.getRelBuilder()));
RelDataType aggResultType = typeFactory.createFieldTypeFromLogicalType(fromDataTypeToLogicalType(((ResolvedExpression) agg).getOutputDataType()));
// assemble exprs by agg children
List<RexNode> aggExprs = agg.getChildren().stream().map(child -> {
if (isDistinct) {
return context.toRexNode(child.getChildren().get(0));
} else {
return context.toRexNode(child);
}
}).collect(Collectors.toList());
// assemble order by key
Expression orderKeyExpr = children.get(1);
Set<SqlKind> kinds = new HashSet<>();
RexNode collationRexNode = createCollation(context.toRexNode(orderKeyExpr), RelFieldCollation.Direction.ASCENDING, null, kinds);
ImmutableList<RexFieldCollation> orderKey = ImmutableList.of(new RexFieldCollation(collationRexNode, kinds));
// assemble partition by keys
List<RexNode> partitionKeys = children.subList(4, children.size()).stream().map(context::toRexNode).collect(Collectors.toList());
// assemble bounds
Expression preceding = children.get(2);
boolean isPhysical = fromDataTypeToLogicalType(((ResolvedExpression) preceding).getOutputDataType()).is(LogicalTypeRoot.BIGINT);
Expression following = children.get(3);
RexWindowBound lowerBound = createBound(context, preceding, SqlKind.PRECEDING);
RexWindowBound upperBound = createBound(context, following, SqlKind.FOLLOWING);
// build RexOver
return Optional.of(context.getRelBuilder().getRexBuilder().makeOver(aggResultType, aggFunc, aggExprs, partitionKeys, orderKey, lowerBound, upperBound, isPhysical, true, false, isDistinct));
}
return Optional.empty();
}
use of org.apache.flink.table.planner.calcite.FlinkTypeFactory in project flink by apache.
the class RelDataTypeJsonDeserializer method deserialize.
static RelDataType deserialize(JsonNode logicalTypeNode, SerdeContext serdeContext) {
final FlinkTypeFactory typeFactory = serdeContext.getTypeFactory();
final LogicalType logicalType = LogicalTypeJsonDeserializer.deserialize(logicalTypeNode, serdeContext);
return LogicalRelDataTypeConverter.toRelDataType(logicalType, typeFactory);
}
use of org.apache.flink.table.planner.calcite.FlinkTypeFactory in project flink by apache.
the class HiveTableSqlFunction method getRowType.
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory, List<Object> arguments) {
Preconditions.checkNotNull(operandTypeChecker.previousArgTypes);
FlinkTypeFactory factory = (FlinkTypeFactory) typeFactory;
Object[] argumentsArray = convertArguments(Arrays.stream(operandTypeChecker.previousArgTypes).map(factory::createFieldTypeFromLogicalType).collect(Collectors.toList()), arguments);
DataType resultType = fromLogicalTypeToDataType(FlinkTypeFactory.toLogicalType(invokeGetResultType(hiveUdtf, argumentsArray, operandTypeChecker.previousArgTypes, (FlinkTypeFactory) typeFactory)));
Tuple3<String[], int[], LogicalType[]> fieldInfo = UserDefinedFunctionUtils.getFieldInfo(resultType);
return buildRelDataType(typeFactory, fromDataTypeToLogicalType(resultType), fieldInfo._1(), fieldInfo._2());
}
Aggregations