use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project beam by apache.
the class CEPUtils method getRegexFromPattern.
/**
* Recursively construct a regular expression from a {@code RexNode}.
*/
public static String getRegexFromPattern(RexNode call) {
if (call.getClass() == RexLiteral.class) {
return ((RexLiteral) call).getValueAs(String.class);
} else {
RexCall opr = (RexCall) call;
SqlOperator operator = opr.getOperator();
List<RexNode> operands = opr.getOperands();
if (operator.getKind() == SqlKind.PATTERN_QUANTIFIER) {
String p = ((RexLiteral) operands.get(0)).getValueAs(String.class);
int start = ((RexLiteral) operands.get(1)).getValueAs(Integer.class);
int end = ((RexLiteral) operands.get(2)).getValueAs(Integer.class);
boolean isReluctant = ((RexLiteral) operands.get(3)).getValueAs(Boolean.class);
Quantifier quantifier = getQuantifier(start, end, isReluctant);
return p + quantifier.toString();
}
return getRegexFromPattern(opr.getOperands().get(0)) + getRegexFromPattern(opr.getOperands().get(1));
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project beam by apache.
the class ExpressionConverter method retrieveRexNode.
/**
* Extract expressions from a project scan node.
*/
public List<RexNode> retrieveRexNode(ResolvedProjectScan node, List<RelDataTypeField> fieldList) {
List<RexNode> ret = new ArrayList<>();
for (ResolvedColumn column : node.getColumnList()) {
int index = -1;
if ((index = indexOfResolvedColumnInExprList(node.getExprList(), column)) != -1) {
ResolvedComputedColumn computedColumn = node.getExprList().get(index);
int windowFieldIndex = -1;
if (computedColumn.getExpr().nodeKind() == RESOLVED_FUNCTION_CALL) {
String functionName = ((ResolvedFunctionCall) computedColumn.getExpr()).getFunction().getName();
if (WINDOW_START_END_FUNCTION_SET.contains(functionName)) {
ResolvedAggregateScan resolvedAggregateScan = (ResolvedAggregateScan) node.getInputScan();
windowFieldIndex = indexOfWindowField(resolvedAggregateScan.getGroupByList(), resolvedAggregateScan.getColumnList(), WINDOW_START_END_TO_WINDOW_MAP.get(functionName));
}
}
ret.add(convertRexNodeFromComputedColumnWithFieldList(computedColumn, node.getInputScan().getColumnList(), fieldList, windowFieldIndex));
} else {
// ResolvedColumn is not a expression, which means it has to be an input column reference.
index = indexOfProjectionColumnRef(column.getId(), node.getInputScan().getColumnList());
if (index < 0 || index >= node.getInputScan().getColumnList().size()) {
throw new IllegalStateException(String.format("Cannot find %s in fieldList %s", column, fieldList));
}
ret.add(rexBuilder().makeInputRef(fieldList.get(index).getType(), index));
}
}
return ret;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project apex-malhar by apache.
the class ExpressionCompiler method getExpression.
/**
* Create quasi-Java expression from given {@link RexNode}
*
* @param node Expression in the form of {@link RexNode}
* @param inputRowType Input Data type to expression in the form of {@link RelDataType}
* @param outputRowType Output data type of expression in the form of {@link RelDataType}
*
* @return Returns quasi-Java expression
*/
public String getExpression(RexNode node, RelDataType inputRowType, RelDataType outputRowType) {
final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
programBuilder.addProject(node, null);
final RexProgram program = programBuilder.getProgram();
final BlockBuilder builder = new BlockBuilder();
final JavaTypeFactory javaTypeFactory = (JavaTypeFactory) rexBuilder.getTypeFactory();
final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.<Expression, PhysType>of(Expressions.variable(Object[].class, "inputValues"), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
final Function1<String, RexToLixTranslator.InputGetter> correlates = new Function1<String, RexToLixTranslator.InputGetter>() {
public RexToLixTranslator.InputGetter apply(String a0) {
throw new UnsupportedOperationException();
}
};
final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder, PhysTypeImpl.of(javaTypeFactory, outputRowType, JavaRowFormat.ARRAY, false), null, inputGetter, correlates);
for (int i = 0; i < list.size(); i++) {
Statement statement = Expressions.statement(list.get(i));
builder.add(statement);
}
return finalizeExpression(builder.toBlock(), inputRowType);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project beam by apache.
the class ZetaSqlUnnest method deriveUncollectRowType.
/**
* Returns the row type returned by applying the 'UNNEST' operation to a relational expression.
*
* <p>Each column in the relational expression must be a multiset of structs or an array. The
* return type is the type of that column, plus an ORDINALITY column if {@code withOrdinality}.
*/
public static RelDataType deriveUncollectRowType(RelNode rel, boolean withOrdinality) {
RelDataType inputType = rel.getRowType();
assert inputType.isStruct() : inputType + " is not a struct";
final List<RelDataTypeField> fields = inputType.getFieldList();
final RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory();
final RelDataTypeFactory.Builder builder = typeFactory.builder();
if (fields.size() == 1 && fields.get(0).getType().getSqlTypeName() == SqlTypeName.ANY) {
// and Any type.
return builder.add(fields.get(0).getName(), SqlTypeName.ANY).nullable(true).build();
}
for (RelDataTypeField field : fields) {
if (field.getType() instanceof MapSqlType) {
builder.add(SqlUnnestOperator.MAP_KEY_COLUMN_NAME, Preconditions.checkArgumentNotNull(field.getType().getKeyType(), "Encountered MAP type with null key type in field %s", field));
builder.add(SqlUnnestOperator.MAP_VALUE_COLUMN_NAME, Preconditions.checkArgumentNotNull(field.getType().getValueType(), "Encountered MAP type with null value type in field %s", field));
} else {
assert field.getType() instanceof ArraySqlType;
RelDataType ret = Preconditions.checkArgumentNotNull(field.getType().getComponentType(), "Encountered ARRAY type with null component type in field %s", field);
// Only difference than Uncollect.java: treats record type and scalar type equally
builder.add(SqlUtil.deriveAliasFromOrdinal(field.getIndex()), ret);
}
}
if (withOrdinality) {
builder.add(SqlUnnestOperator.ORDINALITY_COLUMN_NAME, SqlTypeName.INTEGER);
}
return builder.build();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project beam by apache.
the class CalcRelSplitter method createProgramForLevel.
/**
* Creates a program containing the expressions for a given level.
*
* <p>The expression list of the program will consist of all entries in the expression list <code>
* allExprs[i]</code> for which the corresponding level ordinal <code>exprLevels[i]</code> is
* equal to <code>level</code>. Expressions are mapped according to <code>inputExprOrdinals</code>
* .
*
* @param level Level ordinal
* @param levelCount Number of levels
* @param inputRowType Input row type
* @param allExprs Array of all expressions
* @param exprLevels Array of the level ordinal of each expression
* @param inputExprOrdinals Ordinals in the expression list of input expressions. Input expression
* <code>i</code> will be found at position <code>inputExprOrdinals[i]</code>.
* @param projectExprOrdinals Ordinals of the expressions to be output this level.
* @param conditionExprOrdinal Ordinal of the expression to form the condition for this level, or
* -1 if there is no condition.
* @param outputRowType Output row type
* @return Relational expression
*/
private RexProgram createProgramForLevel(int level, int levelCount, RelDataType inputRowType, RexNode[] allExprs, int[] exprLevels, int[] inputExprOrdinals, final int[] projectExprOrdinals, int conditionExprOrdinal, @Nullable RelDataType outputRowType) {
// Build a list of expressions to form the calc.
List<RexNode> exprs = new ArrayList<>();
// exprInverseOrdinals describes where an expression in allExprs comes
// from -- from an input, from a calculated expression, or -1 if not
// available at this level.
int[] exprInverseOrdinals = new int[allExprs.length];
Arrays.fill(exprInverseOrdinals, -1);
int j = 0;
// and are used here.
for (int i = 0; i < inputExprOrdinals.length; i++) {
final int inputExprOrdinal = inputExprOrdinals[i];
exprs.add(new RexInputRef(i, allExprs[inputExprOrdinal].getType()));
exprInverseOrdinals[inputExprOrdinal] = j;
++j;
}
// Next populate the computed expressions.
final RexShuttle shuttle = new InputToCommonExprConverter(exprInverseOrdinals, exprLevels, level, inputExprOrdinals, allExprs);
for (int i = 0; i < allExprs.length; i++) {
if (exprLevels[i] == level || exprLevels[i] == -1 && level == (levelCount - 1) && allExprs[i] instanceof RexLiteral) {
RexNode expr = allExprs[i];
final RexNode translatedExpr = expr.accept(shuttle);
exprs.add(translatedExpr);
assert exprInverseOrdinals[i] == -1;
exprInverseOrdinals[i] = j;
++j;
}
}
// Form the projection and condition list. Project and condition
// ordinals are offsets into allExprs, so we need to map them into
// exprs.
final List<RexLocalRef> projectRefs = new ArrayList<>(projectExprOrdinals.length);
final List<String> fieldNames = new ArrayList<>(projectExprOrdinals.length);
for (int i = 0; i < projectExprOrdinals.length; i++) {
final int projectExprOrdinal = projectExprOrdinals[i];
final int index = exprInverseOrdinals[projectExprOrdinal];
assert index >= 0;
RexNode expr = allExprs[projectExprOrdinal];
projectRefs.add(new RexLocalRef(index, expr.getType()));
// Inherit meaningful field name if possible.
fieldNames.add(deriveFieldName(expr, i));
}
RexLocalRef conditionRef;
if (conditionExprOrdinal >= 0) {
final int index = exprInverseOrdinals[conditionExprOrdinal];
conditionRef = new RexLocalRef(index, allExprs[conditionExprOrdinal].getType());
} else {
conditionRef = null;
}
if (outputRowType == null) {
outputRowType = RexUtil.createStructType(typeFactory, projectRefs, fieldNames, null);
}
final RexProgram program = new RexProgram(inputRowType, exprs, projectRefs, conditionRef, outputRowType);
// call operands), since literals should be inlined.
return program;
}
Aggregations