Java Code Examples for org.apache.calcite.rex.RexLiteral#getValue()

The following examples show how to use org.apache.calcite.rex.RexLiteral#getValue() . 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: RexToTestCodeShuttle.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public String visitLiteral(RexLiteral literal) {
  RelDataType type = literal.getType();

  if (type.getSqlTypeName() == SqlTypeName.BOOLEAN) {
    if (literal.isNull()) {
      return "nullBool";
    }
    return literal.toString() + "Literal";
  }
  if (type.getSqlTypeName() == SqlTypeName.INTEGER) {
    if (literal.isNull()) {
      return "nullInt";
    }
    return "literal(" + literal.getValue() + ")";
  }
  if (type.getSqlTypeName() == SqlTypeName.VARCHAR) {
    if (literal.isNull()) {
      return "nullVarchar";
    }
  }
  return "/*" + literal.getTypeName().getName() + "*/" + literal.toString();
}
 
Example 2
Source File: TupleExpressionVisitor.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public TupleExpression visitLiteral(RexLiteral literal) {
    TupleExpression tuple;
    Object value = literal.getValue();
    if (value instanceof Number) {
        tuple = new NumberTupleExpression(value);
    } else {
        if (value == null) {
            tuple = new StringTupleExpression(null);
        } else if (value instanceof NlsString) {
            tuple = new StringTupleExpression(((NlsString) value).getValue());
        } else {
            tuple = new StringTupleExpression(value.toString());
        }
    }
    tuple.setDigest(literal.toString());
    return tuple;
}
 
Example 3
Source File: TupleFilterVisitor.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public TupleFilter visitLiteral(RexLiteral literal) {
    String strValue = null;
    Object literalValue = literal.getValue();
    if (literalValue instanceof NlsString) {
        strValue = ((NlsString) literalValue).getValue();
    } else if (literalValue instanceof GregorianCalendar) {
        GregorianCalendar g = (GregorianCalendar) literalValue;
        strValue = Long.toString(g.getTimeInMillis());
    } else if (literalValue instanceof TimeUnitRange) {
        // Extract(x from y) in where clause
        strValue = ((TimeUnitRange) literalValue).name();
    } else if (literalValue == null) {
        strValue = null;
    } else {
        strValue = literalValue.toString();
    }
    TupleFilter filter = new ConstantTupleFilter(strValue);
    return filter;
}
 
Example 4
Source File: ExtractOperatorConversion.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public String toDruidExpression(
    RexNode rexNode, RelDataType rowType, DruidQuery query) {

  final RexCall call = (RexCall) rexNode;
  final RexLiteral flag = (RexLiteral) call.getOperands().get(0);
  final TimeUnitRange calciteUnit = (TimeUnitRange) flag.getValue();
  final RexNode arg = call.getOperands().get(1);

  final String input = DruidExpressions.toDruidExpression(arg, rowType, query);
  if (input == null) {
    return null;
  }

  final String druidUnit = EXTRACT_UNIT_MAP.get(calciteUnit);
  if (druidUnit == null) {
    return null;
  }

  final TimeZone tz =
      arg.getType().getSqlTypeName() == SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE
          ? TimeZone.getTimeZone(query.getConnectionConfig().timeZone())
          : DateTimeUtils.UTC_ZONE;
  return DruidExpressions.applyTimeExtract(input, druidUnit, tz);
}
 
Example 5
Source File: TupleExpressionVisitor.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public TupleExpression visitLiteral(RexLiteral literal) {
    TupleExpression tuple;
    Object value = literal.getValue();

    DataType dataType = DataType.getType(OLAPTable.DATATYPE_MAPPING.get(literal.getType().getSqlTypeName()));
    if (value instanceof Number) {
        tuple = new ConstantTupleExpression(dataType, value);
    } else {
        if (value == null) {
            tuple = new ConstantTupleExpression(dataType, null);
        } else if (value instanceof NlsString) {
            tuple = new ConstantTupleExpression(dataType, ((NlsString) value).getValue());
        } else {
            tuple = new ConstantTupleExpression(dataType, value.toString());
        }
    }
    tuple.setDigest(literal.toString());
    return tuple;
}
 
Example 6
Source File: TupleFilterVisitor.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public TupleFilter visitLiteral(RexLiteral literal) {
    String strValue = null;
    Object literalValue = literal.getValue();
    if (literalValue instanceof NlsString) {
        strValue = ((NlsString) literalValue).getValue();
    } else if (literalValue instanceof GregorianCalendar) {
        GregorianCalendar g = (GregorianCalendar) literalValue;
        strValue = Long.toString(g.getTimeInMillis());
    } else if (literalValue instanceof TimeUnitRange) {
        // Extract(x from y) in where clause
        strValue = ((TimeUnitRange) literalValue).name();
    } else if (literalValue == null) {
        strValue = null;
    } else {
        strValue = literalValue.toString();
    }
    TupleFilter filter = new ConstantTupleFilter(strValue);
    return filter;
}
 
Example 7
Source File: ElasticsearchRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public String visitLiteral(RexLiteral literal) {
  if (literal.getValue() == null) {
    return "null";
  }
  return "\"literal\":\""
    + RexToLixTranslator.translateLiteral(literal, literal.getType(),
      typeFactory, RexImpTable.NullAs.NOT_POSSIBLE)
    + "\"";
}
 
Example 8
Source File: DateRangeRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
boolean isExtractCall(RexNode e) {
  switch (e.getKind()) {
  case EXTRACT:
    final RexCall call = (RexCall) e;
    final RexLiteral flag = (RexLiteral) call.operands.get(0);
    final TimeUnitRange timeUnit = (TimeUnitRange) flag.getValue();
    return timeUnit == this.timeUnit;
  default:
    return false;
  }
}
 
Example 9
Source File: MongoRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public String visitLiteral(RexLiteral literal) {
  if (literal.getValue() == null) {
    return "null";
  }
  return "{$literal: "
      + RexToLixTranslator.translateLiteral(literal, literal.getType(),
          typeFactory, RexImpTable.NullAs.NOT_POSSIBLE)
      + "}";
}
 
Example 10
Source File: CeilOperatorConversion.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override public String toDruidExpression(RexNode rexNode, RelDataType rowType,
    DruidQuery query) {
  final RexCall call = (RexCall) rexNode;
  final RexNode arg = call.getOperands().get(0);
  final String druidExpression = DruidExpressions.toDruidExpression(
      arg,
      rowType,
      query);
  if (druidExpression == null) {
    return null;
  } else if (call.getOperands().size() == 1) {
    // case CEIL(expr)
    return  DruidQuery.format("ceil(%s)", druidExpression);
  } else if (call.getOperands().size() == 2) {
    // CEIL(expr TO timeUnit)
    final RexLiteral flag = (RexLiteral) call.getOperands().get(1);
    final TimeUnitRange timeUnit = (TimeUnitRange) flag.getValue();
    final Granularity.Type type = DruidDateTimeUtils.toDruidGranularity(timeUnit);
    if (type == null) {
      // Unknown Granularity bail out
      return null;
    }
    String isoPeriodFormat = DruidDateTimeUtils.toISOPeriodFormat(type);
    if (isoPeriodFormat == null) {
      return null;
    }
    final TimeZone tz;
    if (arg.getType().getSqlTypeName() == SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE) {
      tz = TimeZone.getTimeZone(query.getConnectionConfig().timeZone());
    } else {
      tz = DateTimeUtils.UTC_ZONE;
    }
    return DruidExpressions.applyTimestampCeil(
        druidExpression, isoPeriodFormat, "", tz);
  } else {
    return null;
  }
}
 
Example 11
Source File: TimeExtractionFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the RexCall contains a valid FLOOR unit that we can
 * serialize to Druid.
 *
 * @param rexNode Extract expression
 *
 * @return true if the extract unit is valid
 */
public static boolean isValidTimeFloor(RexNode rexNode) {
  if (rexNode.getKind() != SqlKind.FLOOR) {
    return false;
  }
  final RexCall call = (RexCall) rexNode;
  if (call.operands.size() != 2) {
    return false;
  }
  final RexLiteral flag = (RexLiteral) call.operands.get(1);
  final TimeUnitRange timeUnit = (TimeUnitRange) flag.getValue();
  return timeUnit != null && VALID_TIME_FLOOR.contains(timeUnit);
}
 
Example 12
Source File: TimeExtractionFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the RexCall contains a valid extract unit that we can
 * serialize to Druid.
 *
 * @param rexNode Extract expression
 *
 * @return true if the extract unit is valid
 */

public static boolean isValidTimeExtract(RexNode rexNode) {
  final RexCall call = (RexCall) rexNode;
  if (call.getKind() != SqlKind.EXTRACT || call.getOperands().size() != 2) {
    return false;
  }
  final RexLiteral flag = (RexLiteral) call.operands.get(0);
  final TimeUnitRange timeUnit = (TimeUnitRange) flag.getValue();
  return timeUnit != null && VALID_TIME_EXTRACT.contains(timeUnit);
}
 
Example 13
Source File: DruidDateTimeUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Infers granularity from a time unit.
 * It supports {@code FLOOR(<time> TO <timeunit>)}
 * and {@code EXTRACT(<timeunit> FROM <time>)}.
 * Returns null if it cannot be inferred.
 *
 * @param node the Rex node
 * @return the granularity, or null if it cannot be inferred
 */
@Nullable
public static Granularity extractGranularity(RexNode node, String timeZone) {
  final int valueIndex;
  final int flagIndex;

  if (TimeExtractionFunction.isValidTimeExtract(node)) {
    flagIndex = 0;
    valueIndex = 1;
  } else if (TimeExtractionFunction.isValidTimeFloor(node)) {
    valueIndex = 0;
    flagIndex = 1;
  } else {
    // We can only infer granularity from floor and extract.
    return null;
  }
  final RexCall call = (RexCall) node;
  final RexNode value = call.operands.get(valueIndex);
  final RexLiteral flag = (RexLiteral) call.operands.get(flagIndex);
  final TimeUnitRange timeUnit = (TimeUnitRange) flag.getValue();

  final RelDataType valueType = value.getType();
  if (valueType.getSqlTypeName() == SqlTypeName.DATE
      || valueType.getSqlTypeName() == SqlTypeName.TIMESTAMP) {
    // We use 'UTC' for date/timestamp type as Druid needs timezone information
    return Granularities.createGranularity(timeUnit, "UTC");
  } else if (valueType.getSqlTypeName() == SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE) {
    return Granularities.createGranularity(timeUnit, timeZone);
  }
  // Type not recognized
  return null;
}
 
Example 14
Source File: DateRangeRules.java    From Bats with Apache License 2.0 5 votes vote down vote up
boolean isExtractCall(RexNode e) {
    switch (e.getKind()) {
    case EXTRACT:
        final RexCall call = (RexCall) e;
        final RexLiteral flag = (RexLiteral) call.getOperands().get(0);
        final TimeUnitRange timeUnit = (TimeUnitRange) flag.getValue();
        return timeUnit == this.timeUnit;
    default:
        return false;
    }
}
 
Example 15
Source File: MetadataUtils.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static SearchQuery toSplitsSearchQuery(List<FilterProperties> filters, Field field) {
  Preconditions.checkNotNull(field);

  final CompleteType ct = CompleteType.fromField(field);

  final FieldType fieldType = getFieldType(ct);
  final String columnKey = PartitionChunkConverter.buildColumnKey(fieldType, field.getName());
  final List<SearchQuery> filterQueries = Lists.newArrayList();

  for (FilterProperties filter: filters) {
    final RexLiteral literal = filter.getLiteral();
    SearchQuery matchingSplitsQuery = null;
    final RangeQueryInput rangeQueryInput;
    switch (ct.toMinorType()) {
      case BIGINT:

        rangeQueryInput = new RangeQueryInput(
          ((BigDecimal) literal.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP).longValue(), filter.getKind());
        matchingSplitsQuery = SearchQueryUtils.newRangeLong(columnKey, (Long) rangeQueryInput.min, (Long) rangeQueryInput.max, rangeQueryInput.includeMin, rangeQueryInput.includeMax);
        break;

      case TIME:
        rangeQueryInput = new RangeQueryInput((int) ((GregorianCalendar) literal.getValue()).getTimeInMillis(), filter.getKind());
        matchingSplitsQuery = SearchQueryUtils.newRangeInt(columnKey, (Integer) rangeQueryInput.min, (Integer) rangeQueryInput.max, rangeQueryInput.includeMin, rangeQueryInput.includeMax);
        break;

      case VARCHAR:
        if (literal.getValue() instanceof  NlsString) {
          rangeQueryInput = new RangeQueryInput(((NlsString) literal.getValue()).getValue(), filter.getKind());
        } else {
          rangeQueryInput = new RangeQueryInput((literal.getValue3().toString()), filter.getKind());
        }
        matchingSplitsQuery = SearchQueryUtils.newRangeTerm(columnKey, (String) rangeQueryInput.min, (String) rangeQueryInput.max, rangeQueryInput.includeMin, rangeQueryInput.includeMax);
        break;

      case FLOAT4:
        rangeQueryInput = new RangeQueryInput(((BigDecimal) literal.getValue()).floatValue(), filter.getKind());
        matchingSplitsQuery = SearchQueryUtils.newRangeFloat(columnKey, (Float) rangeQueryInput.min, (Float) rangeQueryInput.max, rangeQueryInput.includeMin, rangeQueryInput.includeMax);
        break;

      case FLOAT8:
        rangeQueryInput = new RangeQueryInput(((BigDecimal) literal.getValue()).doubleValue(), filter.getKind());
        matchingSplitsQuery = SearchQueryUtils.newRangeDouble(columnKey, (Double) rangeQueryInput.min, (Double) rangeQueryInput.max, rangeQueryInput.includeMin, rangeQueryInput.includeMax);
        break;

      case INT:
        rangeQueryInput = new RangeQueryInput(((BigDecimal) literal.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(), filter.getKind());
        matchingSplitsQuery = SearchQueryUtils.newRangeInt(columnKey, (Integer) rangeQueryInput.min, (Integer) rangeQueryInput.max, rangeQueryInput.includeMin, rangeQueryInput.includeMax);
        break;
      case DATE:
      case TIMESTAMP:
        rangeQueryInput = new RangeQueryInput(((GregorianCalendar) literal.getValue()).getTimeInMillis(), filter.getKind());
        matchingSplitsQuery = SearchQueryUtils.newRangeLong(columnKey, (Long) rangeQueryInput.min, (Long) rangeQueryInput.max, rangeQueryInput.includeMin, rangeQueryInput.includeMax);
        break;

      default:
        throw new UnsupportedOperationException("type not supported " + ct.toMinorType());
    }
    if (matchingSplitsQuery != null) {
      filterQueries.add(matchingSplitsQuery);
    }
  }

  return SearchQueryUtils.and(filterQueries);
}
 
Example 16
Source File: ElasticsearchFilter.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
private static Object literalValueWithDouble(RexLiteral literal){
    return literal.getValue();
}
 
Example 17
Source File: ElasticsearchFilter.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
private static Object literalValueWithDouble(RexLiteral literal){
    return literal.getValue();
}
 
Example 18
Source File: VisitorDataContext.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static Pair<Integer, ?> getValue(RexNode inputRef, RexNode literal) {
  inputRef = inputRef == null ? null : RexUtil.removeCast(inputRef);
  literal = literal == null ? null : RexUtil.removeCast(literal);

  if (inputRef instanceof RexInputRef
      && literal instanceof RexLiteral)  {
    final int index = ((RexInputRef) inputRef).getIndex();
    final RexLiteral rexLiteral = (RexLiteral) literal;
    final RelDataType type = inputRef.getType();

    if (type.getSqlTypeName() == null) {
      LOGGER.warn("{} returned null SqlTypeName", inputRef.toString());
      return null;
    }

    switch (type.getSqlTypeName()) {
    case INTEGER:
      return Pair.of(index, rexLiteral.getValueAs(Integer.class));
    case DOUBLE:
      return Pair.of(index, rexLiteral.getValueAs(Double.class));
    case REAL:
      return Pair.of(index, rexLiteral.getValueAs(Float.class));
    case BIGINT:
      return Pair.of(index, rexLiteral.getValueAs(Long.class));
    case SMALLINT:
      return Pair.of(index, rexLiteral.getValueAs(Short.class));
    case TINYINT:
      return Pair.of(index, rexLiteral.getValueAs(Byte.class));
    case DECIMAL:
      return Pair.of(index, rexLiteral.getValueAs(BigDecimal.class));
    case DATE:
    case TIME:
      return Pair.of(index, rexLiteral.getValueAs(Integer.class));
    case TIMESTAMP:
      return Pair.of(index, rexLiteral.getValueAs(Long.class));
    case CHAR:
      return Pair.of(index, rexLiteral.getValueAs(Character.class));
    case VARCHAR:
      return Pair.of(index, rexLiteral.getValueAs(String.class));
    default:
      // TODO: Support few more supported cases
      LOGGER.warn("{} for value of class {} is being handled in default way",
          type.getSqlTypeName(), rexLiteral.getValue().getClass());
      if (rexLiteral.getValue() instanceof NlsString) {
        return Pair.of(index, ((NlsString) rexLiteral.getValue()).getValue());
      } else {
        return Pair.of(index, rexLiteral.getValue());
      }
    }
  }

  // Unsupported Arguments
  return null;
}
 
Example 19
Source File: VisitorDataContext.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static Pair<Integer, ?> getValue(RexNode inputRef, RexNode literal) {
  inputRef = removeCast(inputRef);
  literal = removeCast(literal);

  if (inputRef instanceof RexInputRef
      && literal instanceof RexLiteral)  {
    final int index = ((RexInputRef) inputRef).getIndex();
    final RexLiteral rexLiteral = (RexLiteral) literal;
    final RelDataType type = inputRef.getType();

    if (type.getSqlTypeName() == null) {
      LOGGER.warn("{} returned null SqlTypeName", inputRef.toString());
      return null;
    }

    switch (type.getSqlTypeName()) {
    case INTEGER:
      return Pair.of(index, rexLiteral.getValueAs(Integer.class));
    case DOUBLE:
      return Pair.of(index, rexLiteral.getValueAs(Double.class));
    case REAL:
      return Pair.of(index, rexLiteral.getValueAs(Float.class));
    case BIGINT:
      return Pair.of(index, rexLiteral.getValueAs(Long.class));
    case SMALLINT:
      return Pair.of(index, rexLiteral.getValueAs(Short.class));
    case TINYINT:
      return Pair.of(index, rexLiteral.getValueAs(Byte.class));
    case DECIMAL:
      return Pair.of(index, rexLiteral.getValueAs(BigDecimal.class));
    case DATE:
    case TIME:
      return Pair.of(index, rexLiteral.getValueAs(Integer.class));
    case TIMESTAMP:
      return Pair.of(index, rexLiteral.getValueAs(Long.class));
    case CHAR:
      return Pair.of(index, rexLiteral.getValueAs(Character.class));
    case VARCHAR:
      return Pair.of(index, rexLiteral.getValueAs(String.class));
    default:
      // TODO: Support few more supported cases
      LOGGER.warn("{} for value of class {} is being handled in default way",
          type.getSqlTypeName(), rexLiteral.getValue().getClass());
      if (rexLiteral.getValue() instanceof NlsString) {
        return Pair.of(index, ((NlsString) rexLiteral.getValue()).getValue());
      } else {
        return Pair.of(index, rexLiteral.getValue());
      }
    }
  }

  // Unsupported Arguments
  return null;
}