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

The following examples show how to use org.apache.calcite.linq4j.tree.Primitive#of() . 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: ElasticsearchEnumerators.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static Object convert(Object o, Class clazz) {
  if (o == null) {
    return null;
  }
  Primitive primitive = Primitive.of(clazz);
  if (primitive != null) {
    clazz = primitive.boxClass;
  } else {
    primitive = Primitive.ofBox(clazz);
  }
  if (clazz.isInstance(o)) {
    return o;
  }
  if (o instanceof Date && primitive != null) {
    o = ((Date) o).getTime() / DateTimeUtils.MILLIS_PER_DAY;
  }
  if (o instanceof Number && primitive != null) {
    return primitive.number((Number) o);
  }
  return o;
}
 
Example 2
Source File: MongoEnumerator.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static Object convert(Object o, Class clazz) {
  if (o == null) {
    return null;
  }
  Primitive primitive = Primitive.of(clazz);
  if (primitive != null) {
    clazz = primitive.boxClass;
  } else {
    primitive = Primitive.ofBox(clazz);
  }
  if (clazz.isInstance(o)) {
    return o;
  }
  if (o instanceof Date && primitive != null) {
    o = ((Date) o).getTime() / DateTimeUtils.MILLIS_PER_DAY;
  }
  if (o instanceof Number && primitive != null) {
    return primitive.number((Number) o);
  }
  return o;
}
 
Example 3
Source File: RexImpTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Expression unboxExpression(final Expression argValue) {
  Primitive fromBox = Primitive.ofBox(argValue.getType());
  if (fromBox == null || fromBox == Primitive.VOID) {
    return argValue;
  }
  // Optimization: for "long x";
  // "Long.valueOf(x)" generates "x"
  if (argValue instanceof MethodCallExpression) {
    MethodCallExpression mce = (MethodCallExpression) argValue;
    if (mce.method.getName().equals("valueOf") && mce.expressions.size() == 1) {
      Expression originArg = mce.expressions.get(0);
      if (Primitive.of(originArg.type) == fromBox) {
        return originArg;
      }
    }
  }
  return NullAs.NOT_POSSIBLE.handle(argValue);
}
 
Example 4
Source File: EnumUtils.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Match an argument expression to method parameter type with best effort
 * @param argument Argument Expression
 * @param parameter Parameter type
 * @return Converted argument expression that matches the parameter type.
 *         Returns null if it is impossible to match.
 */
private static Expression matchMethodParameterType(
    Expression argument, Class parameter) {
  Type argumentType = argument.getType();
  if (Types.isAssignableFrom(parameter, argumentType)) {
    return argument;
  }
  // Object.class is not assignable from primitive types,
  // but the method with Object parameters can accept primitive types.
  // E.g., "array(Object... args)" in SqlFunctions
  if (parameter == Object.class
      && Primitive.of(argumentType) != null) {
    return argument;
  }
  // Convert argument with Object.class type to parameter explicitly
  if (argumentType == Object.class
      && Primitive.of(argumentType) == null) {
    return convert(argument, parameter);
  }
  // assignable types that can be accepted with explicit conversion
  if (parameter == BigDecimal.class
      && Primitive.ofBoxOr(argumentType) != null) {
    return convert(argument, parameter);
  }
  return null;
}
 
Example 5
Source File: GeodeUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static Object convert(Object o, Class clazz) {
  if (o == null) {
    return null;
  }
  Primitive primitive = Primitive.of(clazz);
  if (primitive != null) {
    clazz = primitive.boxClass;
  } else {
    primitive = Primitive.ofBox(clazz);
  }
  if (clazz == null) {
    return o.toString();
  }
  if (Map.class.isAssignableFrom(clazz)
          && o instanceof PdxInstance) {
    // This is in case of nested Objects!
    return Util.toString(
            ((PdxInstance) o).getFieldNames(), "PDX[", ",", "]");
  }
  if (clazz.isInstance(o)) {
    return o;
  }
  if (o instanceof Date && primitive != null) {
    o = ((Date) o).getTime() / DateTimeUtils.MILLIS_PER_DAY;
  }
  if (o instanceof Number && primitive != null) {
    return primitive.number((Number) o);
  }
  return o;
}
 
Example 6
Source File: ColumnLoader.java    From calcite with Apache License 2.0 5 votes vote down vote up
ArrayTable.Representation chooseRep(int ordinal) {
  Primitive primitive = Primitive.of(clazz);
  Primitive boxPrimitive = Primitive.ofBox(clazz);
  Primitive p = primitive != null ? primitive : boxPrimitive;
  if (!containsNull && p != null) {
    switch (p) {
    case FLOAT:
    case DOUBLE:
      return new ArrayTable.PrimitiveArray(ordinal, p, p);
    case OTHER:
    case VOID:
      throw new AssertionError("wtf?!");
    }
    if (canBeLong(min) && canBeLong(max)) {
      return chooseFixedRep(
          ordinal, p, toLong(min), toLong(max));
    }
  }

  // We don't want to use a dictionary if:
  // (a) there are so many values that an object pointer (with one
  //     indirection) has about as many bits as a code (with two
  //     indirections); or
  // (b) if there are very few copies of each value.
  // The condition kind of captures this, but needs to be tuned.
  final int codeCount = map.size() + (containsNull ? 1 : 0);
  final int codeBitCount = log2(nextPowerOf2(codeCount));
  if (codeBitCount < 10 && values.size() > 2000) {
    final ArrayTable.Representation representation =
        chooseFixedRep(-1, Primitive.INT, 0, codeCount - 1);
    return new ArrayTable.ObjectDictionary(ordinal, representation);
  }
  return new ArrayTable.ObjectArray(ordinal);
}
 
Example 7
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 8
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override protected void implementNotNullReset(AggContext info,
    AggResetContext reset) {
  Expression acc = reset.accumulator().get(0);
  Primitive p = Primitive.of(acc.getType());
  boolean isMin = MIN == info.aggregation();
  Object inf = p == null ? null : (isMin ? p.max : p.min);
  reset.currentBlock().add(
      Expressions.statement(
          Expressions.assign(acc,
              Expressions.constant(inf, acc.getType()))));
}
 
Example 9
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Expression maybeBox(Expression expression) {
  final Primitive primitive = Primitive.of(expression.getType());
  if (primitive != null) {
    expression = Expressions.box(expression, primitive);
  }
  return expression;
}
 
Example 10
Source File: EnumUtils.java    From calcite with Apache License 2.0 4 votes vote down vote up
private static boolean isA(Type fromType, Primitive primitive) {
  return Primitive.of(fromType) == primitive
      || Primitive.ofBox(fromType) == primitive;
}