Java Code Examples for org.apache.calcite.linq4j.tree.Expressions#condition()

The following examples show how to use org.apache.calcite.linq4j.tree.Expressions#condition() . 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: EnumerableMatch.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public Expression field(BlockBuilder list, int index,
    Type storageType) {
  if (this.index == null) {
    return generator.apply(this.row).field(list, index, storageType);
  }

  return Expressions.condition(
      Expressions.greaterThanOrEqual(this.index, Expressions.constant(0)),
      generator.apply(
          EnumUtils.convert(
              Expressions.call(this.passedRows,
                  BuiltInMethod.LIST_GET.method, this.index),
              physType.getJavaRowType()))
          .field(list, index, storageType),
      Expressions.constant(null));
}
 
Example 2
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void implementNotNullAdd(AggContext info,
    AggAddContext add) {
  final Expression accValue = add.accumulator().get(0);
  final Expression arg0 = add.arguments().get(0);
  final Expression arg1 = add.arguments().size() == 2
      ? add.arguments().get(1) : COMMA_EXPR;
  final Expression result = Expressions.condition(
      Expressions.equal(NULL_EXPR, accValue),
      arg0,
      Expressions.call(BuiltInMethod.STRING_CONCAT.method, accValue,
          Expressions.call(BuiltInMethod.STRING_CONCAT.method, arg1, arg0)));

  add.currentBlock().add(Expressions.statement(Expressions.assign(accValue, result)));
}
 
Example 3
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Expression implementResult(AggContext info,
    AggResultContext result) {
  WinAggResultContext winResult = (WinAggResultContext) result;

  return Expressions.condition(winResult.hasRows(),
      winResult.rowTranslator(
          winResult.computeIndex(Expressions.constant(0), seekType))
          .translate(winResult.rexArguments().get(0), info.returnType()),
      getDefaultValue(info.returnType()));
}
 
Example 4
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Expression implementRecurse(RexToLixTranslator translator,
    final List<Expression> argValueList) {
  if (argValueList.size() == 1) {
    return argValueList.get(0);
  } else {
    return Expressions.condition(
        translator.checkNotNull(argValueList.get(0)),
        argValueList.get(0),
        implementRecurse(translator, Util.skip(argValueList)));
  }
}
 
Example 5
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private ParameterExpression genValueStatement(
    final RexToLixTranslator translator,
    final RexCall call, final List<Expression> argValueList,
    final Expression condition) {
  List<Expression> optimizedArgValueList = argValueList;
  if (harmonize) {
    optimizedArgValueList =
        harmonize(optimizedArgValueList, translator, call);
  }
  optimizedArgValueList = unboxIfNecessary(optimizedArgValueList);

  final Expression callValue =
      implementSafe(translator, call, optimizedArgValueList);

  // In general, RexCall's type is correct for code generation
  // and thus we should ensure the consistency.
  // However, for some special cases (e.g., TableFunction),
  // the implementation's type is correct, we can't convert it.
  final SqlOperator op = call.getOperator();
  final Type returnType = translator.typeFactory.getJavaClass(call.getType());
  final boolean noConvert = (returnType == null)
          || (returnType == callValue.getType())
          || (op instanceof SqlUserDefinedTableMacro)
          || (op instanceof SqlUserDefinedTableFunction);
  final Expression convertedCallValue =
          noConvert
          ? callValue
          : EnumUtils.convert(callValue, returnType);

  final Expression valueExpression =
      Expressions.condition(condition,
          getIfTrue(convertedCallValue.getType(), argValueList),
          convertedCallValue);
  final ParameterExpression value =
      Expressions.parameter(convertedCallValue.getType(),
          translator.getBlockBuilder().newName(getVariableName() + "_value"));
  translator.getBlockBuilder().add(
      Expressions.declare(Modifier.FINAL, value, valueExpression));
  return value;
}
 
Example 6
Source File: StrictAggImplementor.java    From calcite with Apache License 2.0 5 votes vote down vote up
public final Expression implementResult(AggContext info,
    final AggResultContext result) {
  if (!needTrackEmptySet) {
    return EnumUtils.convert(
        implementNotNullResult(info, result), info.returnType());
  }
  String tmpName = result.accumulator().isEmpty()
      ? "ar"
      : (result.accumulator().get(0) + "$Res");
  ParameterExpression res = Expressions.parameter(0, info.returnType(),
      result.currentBlock().newName(tmpName));

  List<Expression> acc = result.accumulator();
  final BlockBuilder thenBlock = result.nestBlock();
  Expression nonNull = EnumUtils.convert(
      implementNotNullResult(info, result), info.returnType());
  result.exitBlock();
  thenBlock.add(Expressions.statement(Expressions.assign(res, nonNull)));
  BlockStatement thenBranch = thenBlock.toBlock();
  Expression seenNotNullRows =
      trackNullsPerRow
      ? acc.get(acc.size() - 1)
      : ((WinAggResultContext) result).hasRows();

  if (thenBranch.statements.size() == 1) {
    return Expressions.condition(seenNotNullRows,
        nonNull, RexImpTable.getDefaultValue(res.getType()));
  }
  result.currentBlock().add(Expressions.declare(0, res, null));
  result.currentBlock().add(
      Expressions.ifThenElse(seenNotNullRows,
          thenBranch,
          Expressions.statement(
              Expressions.assign(res,
                  RexImpTable.getDefaultValue(res.getType())))));
  return res;
}
 
Example 7
Source File: RexToLixTranslator.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public Result visitFieldAccess(RexFieldAccess fieldAccess) {
  final Pair<RexNode, Type> key = Pair.of(fieldAccess, currentStorageType);
  if (rexWithStorageTypeResultMap.containsKey(key)) {
    return rexWithStorageTypeResultMap.get(key);
  }
  final RexNode target = deref(fieldAccess.getReferenceExpr());
  int fieldIndex = fieldAccess.getField().getIndex();
  String fieldName = fieldAccess.getField().getName();
  switch (target.getKind()) {
  case CORREL_VARIABLE:
    if (correlates == null) {
      throw new RuntimeException("Cannot translate " + fieldAccess
          + " since correlate variables resolver is not defined");
    }
    final RexToLixTranslator.InputGetter getter =
        correlates.apply(((RexCorrelVariable) target).getName());
    final Expression input = getter.field(
        list, fieldIndex, currentStorageType);
    final Expression condition = checkNull(input);
    final ParameterExpression valueVariable =
        Expressions.parameter(input.getType(), list.newName("corInp_value"));
    list.add(Expressions.declare(Modifier.FINAL, valueVariable, input));
    final ParameterExpression isNullVariable =
        Expressions.parameter(Boolean.TYPE, list.newName("corInp_isNull"));
    final Expression isNullExpression = Expressions.condition(
        condition,
        RexImpTable.TRUE_EXPR,
        checkNull(valueVariable));
    list.add(Expressions.declare(Modifier.FINAL, isNullVariable, isNullExpression));
    final Result result1 = new Result(isNullVariable, valueVariable);
    rexWithStorageTypeResultMap.put(key, result1);
    return result1;
  default:
    RexNode rxIndex =
        builder.makeLiteral(fieldIndex, typeFactory.createType(int.class), true);
    RexNode rxName =
        builder.makeLiteral(fieldName, typeFactory.createType(String.class), true);
    RexCall accessCall = (RexCall) builder.makeCall(
        fieldAccess.getType(), SqlStdOperatorTable.STRUCT_ACCESS,
        ImmutableList.of(target, rxIndex, rxName));
    final Result result2 = accessCall.accept(this);
    rexWithStorageTypeResultMap.put(key, result2);
    return result2;
  }
}
 
Example 8
Source File: EnumUtils.java    From calcite with Apache License 2.0 4 votes vote down vote up
static Expression joinSelector(JoinRelType joinType, PhysType physType,
    List<PhysType> inputPhysTypes) {
  // A parameter for each input.
  final List<ParameterExpression> parameters = new ArrayList<>();

  // Generate all fields.
  final List<Expression> expressions = new ArrayList<>();
  final int outputFieldCount = physType.getRowType().getFieldCount();
  for (Ord<PhysType> ord : Ord.zip(inputPhysTypes)) {
    final PhysType inputPhysType =
        ord.e.makeNullable(joinType.generatesNullsOn(ord.i));
    // If input item is just a primitive, we do not generate specialized
    // primitive apply override since it won't be called anyway
    // Function<T> always operates on boxed arguments
    final ParameterExpression parameter =
        Expressions.parameter(Primitive.box(inputPhysType.getJavaRowType()),
            EnumUtils.LEFT_RIGHT.get(ord.i));
    parameters.add(parameter);
    if (expressions.size() == outputFieldCount) {
      // For instance, if semi-join needs to return just the left inputs
      break;
    }
    final int fieldCount = inputPhysType.getRowType().getFieldCount();
    for (int i = 0; i < fieldCount; i++) {
      Expression expression =
          inputPhysType.fieldReference(parameter, i,
              physType.getJavaFieldType(expressions.size()));
      if (joinType.generatesNullsOn(ord.i)) {
        expression =
            Expressions.condition(
                Expressions.equal(parameter, Expressions.constant(null)),
                Expressions.constant(null),
                expression);
      }
      expressions.add(expression);
    }
  }
  return Expressions.lambda(
      Function2.class,
      physType.record(expressions),
      parameters);
}