use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project flink by apache.
the class ExpressionConverterTest method testLiteral.
@Test
public void testLiteral() {
RexNode rex = converter.visit(valueLiteral((byte) 1));
Assert.assertEquals(1, (int) ((RexLiteral) rex).getValueAs(Integer.class));
Assert.assertEquals(SqlTypeName.TINYINT, rex.getType().getSqlTypeName());
rex = converter.visit(valueLiteral((short) 1));
Assert.assertEquals(1, (int) ((RexLiteral) rex).getValueAs(Integer.class));
Assert.assertEquals(SqlTypeName.SMALLINT, rex.getType().getSqlTypeName());
rex = converter.visit(valueLiteral(1));
Assert.assertEquals(1, (int) ((RexLiteral) rex).getValueAs(Integer.class));
Assert.assertEquals(SqlTypeName.INTEGER, rex.getType().getSqlTypeName());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project beam by apache.
the class RelConverter method createOneRow.
// This function creates a single dummy input row for queries that don't read from a table.
// For example: SELECT "hello"
// The code is copy-pasted from Calcite's LogicalValues.createOneRow() with a single line
// change: SqlTypeName.INTEGER replaced by SqlTypeName.BIGINT.
// Would like to call LogicalValues.createOneRow() directly, but it uses type SqlTypeName.INTEGER
// which corresponds to TypeKind.TYPE_INT32 in ZetaSQL, a type not supported in ZetaSQL
// PRODUCT_EXTERNAL mode. See
// https://github.com/google/zetasql/blob/c610a21ffdc110293c1c7bd255a2674ebc7ec7a8/java/com/google/zetasql/TypeFactory.java#L61
static LogicalValues createOneRow(RelOptCluster cluster) {
final RelDataType rowType = cluster.getTypeFactory().builder().add("ZERO", SqlTypeName.BIGINT).nullable(false).build();
final ImmutableList<ImmutableList<RexLiteral>> tuples = ImmutableList.of(ImmutableList.of(cluster.getRexBuilder().makeExactLiteral(BigDecimal.ZERO, rowType.getFieldList().get(0).getType())));
return LogicalValues.create(cluster, rowType, tuples);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project beam by apache.
the class SqlInOperatorRewriter method apply.
@Override
public RexNode apply(RexBuilder rexBuilder, List<RexNode> operands) {
Preconditions.checkArgument(operands.size() >= 2, "IN should have at least two arguments in function call.");
final RexNode arg = operands.get(0);
final List<RexNode> ranges = ImmutableList.copyOf(operands.subList(1, operands.size()));
// ZetaSQL has weird behavior for NULL...
for (RexNode node : ranges) {
if (node instanceof RexLiteral && ((RexLiteral) node).isNull()) {
throw new UnsupportedOperationException("IN NULL unsupported");
}
}
return rexBuilder.makeIn(arg, ranges);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project beam by apache.
the class CEPCall method of.
public static CEPCall of(RexCall operation) {
SqlOperator call = operation.getOperator();
CEPOperator myOp = CEPOperator.of(call);
ArrayList<CEPOperation> operandsList = new ArrayList<>();
for (RexNode i : operation.getOperands()) {
if (i.getClass() == RexCall.class) {
CEPCall callToAdd = CEPCall.of((RexCall) i);
operandsList.add(callToAdd);
} else if (i.getClass() == RexLiteral.class) {
RexLiteral lit = (RexLiteral) i;
CEPLiteral litToAdd = CEPLiteral.of(lit);
operandsList.add(litToAdd);
} else if (i.getClass() == RexPatternFieldRef.class) {
RexPatternFieldRef fieldRef = (RexPatternFieldRef) i;
CEPFieldRef fieldRefToAdd = CEPFieldRef.of(fieldRef);
operandsList.add(fieldRefToAdd);
} else {
throw new UnsupportedOperationException("RexNode not supported: " + i.getClass().getName());
}
}
return new CEPCall(myOp, operandsList);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project beam by apache.
the class CalcRelSplitter method execute.
// ~ Methods ----------------------------------------------------------------
public RelNode execute() {
// expressions to the left.
assert program.isValid(Litmus.THROW, null);
final List<RexNode> exprList = program.getExprList();
final RexNode[] exprs = exprList.toArray(new RexNode[0]);
assert !RexUtil.containComplexExprs(exprList);
// Figure out what level each expression belongs to.
int[] exprLevels = new int[exprs.length];
// The type of a level is given by
// relTypes[levelTypeOrdinals[level]].
int[] levelTypeOrdinals = new int[exprs.length];
int levelCount = chooseLevels(exprs, -1, exprLevels, levelTypeOrdinals);
// For each expression, figure out which is the highest level where it
// is used.
int[] exprMaxUsingLevelOrdinals = new HighestUsageFinder(exprs, exprLevels).getMaxUsingLevelOrdinals();
// If expressions are used as outputs, mark them as higher than that.
final List<RexLocalRef> projectRefList = program.getProjectList();
final RexLocalRef conditionRef = program.getCondition();
for (RexLocalRef projectRef : projectRefList) {
exprMaxUsingLevelOrdinals[projectRef.getIndex()] = levelCount;
}
if (conditionRef != null) {
exprMaxUsingLevelOrdinals[conditionRef.getIndex()] = levelCount;
}
// Print out what we've got.
if (RULE_LOGGER.isTraceEnabled()) {
traceLevelExpressions(exprs, exprLevels, levelTypeOrdinals, levelCount);
}
// Now build the calcs.
RelNode rel = child;
final int inputFieldCount = program.getInputRowType().getFieldCount();
int[] inputExprOrdinals = identityArray(inputFieldCount);
boolean doneCondition = false;
for (int level = 0; level < levelCount; level++) {
final int[] projectExprOrdinals;
final RelDataType outputRowType;
if (level == (levelCount - 1)) {
outputRowType = program.getOutputRowType();
projectExprOrdinals = new int[projectRefList.size()];
for (int i = 0; i < projectExprOrdinals.length; i++) {
projectExprOrdinals[i] = projectRefList.get(i).getIndex();
}
} else {
outputRowType = null;
// Project the expressions which are computed at this level or
// before, and will be used at later levels.
List<Integer> projectExprOrdinalList = new ArrayList<>();
for (int i = 0; i < exprs.length; i++) {
RexNode expr = exprs[i];
if (expr instanceof RexLiteral) {
// Don't project literals. They are always created in
// the level where they are used.
exprLevels[i] = -1;
continue;
}
if ((exprLevels[i] <= level) && (exprMaxUsingLevelOrdinals[i] > level)) {
projectExprOrdinalList.add(i);
}
}
projectExprOrdinals = Ints.toArray(projectExprOrdinalList);
}
final RelType relType = relTypes[levelTypeOrdinals[level]];
// Can we do the condition this level?
int conditionExprOrdinal = -1;
if ((conditionRef != null) && !doneCondition) {
conditionExprOrdinal = conditionRef.getIndex();
if ((exprLevels[conditionExprOrdinal] > level) || !relType.supportsCondition()) {
// stand down -- we're not ready to do the condition yet
conditionExprOrdinal = -1;
} else {
doneCondition = true;
}
}
RexProgram program1 = createProgramForLevel(level, levelCount, rel.getRowType(), exprs, exprLevels, inputExprOrdinals, projectExprOrdinals, conditionExprOrdinal, outputRowType);
rel = relType.makeRel(cluster, traits, relBuilder, rel, program1);
rel = handle(rel);
// The outputs of this level will be the inputs to the next level.
inputExprOrdinals = projectExprOrdinals;
}
Preconditions.checkArgument(doneCondition || (conditionRef == null), "unhandled condition");
return rel;
}
Aggregations