Java Code Examples for org.apache.calcite.linq4j.tree.Primitive#is()

The following examples show how to use org.apache.calcite.linq4j.tree.Primitive#is() . 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: EnumerableTableScan.java    From calcite with Apache License 2.0 6 votes vote down vote up
private JavaRowFormat format() {
  int fieldCount = getRowType().getFieldCount();
  if (fieldCount == 0) {
    return JavaRowFormat.LIST;
  }
  if (Object[].class.isAssignableFrom(elementType)) {
    return fieldCount == 1 ? JavaRowFormat.SCALAR : JavaRowFormat.ARRAY;
  }
  if (Row.class.isAssignableFrom(elementType)) {
    return JavaRowFormat.ROW;
  }
  if (fieldCount == 1 && (Object.class == elementType
        || Primitive.is(elementType)
        || Number.class.isAssignableFrom(elementType)
        || String.class == elementType)) {
    return JavaRowFormat.SCALAR;
  }
  return JavaRowFormat.CUSTOM;
}
 
Example 2
Source File: JaninoRelMetadataProvider.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns e.g. ", ignoreNulls". */
private static StringBuilder safeArgList(StringBuilder buff, Method method) {
  for (Ord<Class<?>> t : Ord.zip(method.getParameterTypes())) {
    if (Primitive.is(t.e) || RexNode.class.isAssignableFrom(t.e)) {
      buff.append(", a").append(t.i);
    } else {
      buff.append(", ") .append(NullSentinel.class.getName())
          .append(".mask(a").append(t.i).append(")");
    }
  }
  return buff;
}
 
Example 3
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
static Expression optimize2(Expression operand, Expression expression) {
  if (Primitive.is(operand.getType())) {
    // Primitive values cannot be null
    return optimize(expression);
  } else {
    return optimize(
        Expressions.condition(
            Expressions.equal(operand, NULL_EXPR),
            NULL_EXPR,
            expression));
  }
}
 
Example 4
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
static Expression getDefaultValue(Type type) {
  if (Primitive.is(type)) {
    Primitive p = Primitive.of(type);
    return Expressions.constant(p.defaultValue, type);
  }
  return Expressions.constant(null, type);
}
 
Example 5
Source File: EnumUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static Expression fromInternal(Expression operand,
    Type fromType, Type targetType) {
  if (operand == ConstantUntypedNull.INSTANCE) {
    return operand;
  }
  if (!(operand.getType() instanceof Class)) {
    return operand;
  }
  if (Types.isAssignableFrom(targetType, fromType)) {
    return operand;
  }
  if (targetType == java.sql.Date.class) {
    // E.g. from "int" or "Integer" to "java.sql.Date",
    // generate "SqlFunctions.internalToDate".
    if (isA(fromType, Primitive.INT)) {
      return Expressions.call(BuiltInMethod.INTERNAL_TO_DATE.method, operand);
    }
  } else if (targetType == java.sql.Time.class) {
    // E.g. from "int" or "Integer" to "java.sql.Time",
    // generate "SqlFunctions.internalToTime".
    if (isA(fromType, Primitive.INT)) {
      return Expressions.call(BuiltInMethod.INTERNAL_TO_TIME.method, operand);
    }
  } else if (targetType == java.sql.Timestamp.class) {
    // E.g. from "long" or "Long" to "java.sql.Timestamp",
    // generate "SqlFunctions.internalToTimestamp".
    if (isA(fromType, Primitive.LONG)) {
      return Expressions.call(BuiltInMethod.INTERNAL_TO_TIMESTAMP.method, operand);
    }
  }
  if (Primitive.is(operand.type)
      && Primitive.isBox(targetType)) {
    // E.g. operand is "int", target is "Long", generate "(long) operand".
    return Expressions.convert_(operand,
        Primitive.ofBox(targetType).primitiveClass);
  }
  return operand;
}
 
Example 6
Source File: EnumerableWindow.java    From calcite with Apache License 2.0 4 votes vote down vote up
private void declareAndResetState(final JavaTypeFactory typeFactory,
    BlockBuilder builder, final Result result, int windowIdx,
    List<AggImpState> aggs, PhysType outputPhysType,
    List<Expression> outputRow) {
  for (final AggImpState agg : aggs) {
    agg.context =
        new WinAggContext() {
          public SqlAggFunction aggregation() {
            return agg.call.getAggregation();
          }

          public RelDataType returnRelType() {
            return agg.call.type;
          }

          public Type returnType() {
            return EnumUtils.javaClass(typeFactory, returnRelType());
          }

          public List<? extends Type> parameterTypes() {
            return EnumUtils.fieldTypes(typeFactory,
                parameterRelTypes());
          }

          public List<? extends RelDataType> parameterRelTypes() {
            return EnumUtils.fieldRowTypes(result.physType.getRowType(),
                constants, agg.call.getArgList());
          }

          public List<ImmutableBitSet> groupSets() {
            throw new UnsupportedOperationException();
          }

          public List<Integer> keyOrdinals() {
            throw new UnsupportedOperationException();
          }

          public List<? extends RelDataType> keyRelTypes() {
            throw new UnsupportedOperationException();
          }

          public List<? extends Type> keyTypes() {
            throw new UnsupportedOperationException();
          }
        };
    String aggName = "a" + agg.aggIdx;
    if (CalciteSystemProperty.DEBUG.value()) {
      aggName = Util.toJavaId(agg.call.getAggregation().getName(), 0)
          .substring("ID$0$".length()) + aggName;
    }
    List<Type> state = agg.implementor.getStateType(agg.context);
    final List<Expression> decls = new ArrayList<>(state.size());
    for (int i = 0; i < state.size(); i++) {
      Type type = state.get(i);
      ParameterExpression pe =
          Expressions.parameter(type,
              builder.newName(aggName
                  + "s" + i + "w" + windowIdx));
      builder.add(Expressions.declare(0, pe, null));
      decls.add(pe);
    }
    agg.state = decls;
    Type aggHolderType = agg.context.returnType();
    Type aggStorageType =
        outputPhysType.getJavaFieldType(outputRow.size());
    if (Primitive.is(aggHolderType) && !Primitive.is(aggStorageType)) {
      aggHolderType = Primitive.box(aggHolderType);
    }
    ParameterExpression aggRes = Expressions.parameter(0,
        aggHolderType,
        builder.newName(aggName + "w" + windowIdx));

    builder.add(
        Expressions.declare(0, aggRes,
            Expressions.constant(Primitive.is(aggRes.getType())
                ? Primitive.of(aggRes.getType()).defaultValue
                : null,
                aggRes.getType())));
    agg.result = aggRes;
    outputRow.add(aggRes);
    agg.implementor.implementReset(agg.context,
        new WinAggResetContextImpl(builder, agg.state,
            null, null, null, null, null, null));
  }
}