Java Code Examples for org.apache.calcite.util.NlsString

The following examples show how to use org.apache.calcite.util.NlsString. These examples are extracted from open source projects. 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 Project: Bats   Source File: SqlUserDefinedTableMacro.java    License: Apache License 2.0 6 votes vote down vote up
private static Object coerce(Object o, RelDataType type) {
  if (o == null) {
    return null;
  }
  if (!(type instanceof RelDataTypeFactoryImpl.JavaType)) {
    return null;
  }
  final RelDataTypeFactoryImpl.JavaType javaType =
      (RelDataTypeFactoryImpl.JavaType) type;
  final Class<?> clazz = javaType.getJavaClass();
  //noinspection unchecked
  if (clazz.isAssignableFrom(o.getClass())) {
    return o;
  }
  if (clazz == String.class && o instanceof NlsString) {
    return ((NlsString) o).getValue();
  }
  return null;
}
 
Example 2
Source Project: Bats   Source File: SqlUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates the type of an {@link org.apache.calcite.util.NlsString}.
 *
 * <p>The type inherits the The NlsString's {@link Charset} and
 * {@link SqlCollation}, if they are set, otherwise it gets the system
 * defaults.
 *
 * @param typeFactory Type factory
 * @param str         String
 * @return Type, including collation and charset
 */
public static RelDataType createNlsStringType(
    RelDataTypeFactory typeFactory,
    NlsString str) {
  Charset charset = str.getCharset();
  if (null == charset) {
    charset = typeFactory.getDefaultCharset();
  }
  SqlCollation collation = str.getCollation();
  if (null == collation) {
    collation = SqlCollation.COERCIBLE;
  }
  RelDataType type =
      typeFactory.createSqlType(
          SqlTypeName.CHAR,
          str.getValue().length());
  type =
      typeFactory.createTypeWithCharsetAndCollation(
          type,
          charset,
          collation);
  return type;
}
 
Example 3
Source Project: Bats   Source File: RexBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a RexBuilder.
 *
 * @param typeFactory Type factory
 */
public RexBuilder(RelDataTypeFactory typeFactory) {
  this.typeFactory = typeFactory;
  this.booleanTrue =
      makeLiteral(
          Boolean.TRUE,
          typeFactory.createSqlType(SqlTypeName.BOOLEAN),
          SqlTypeName.BOOLEAN);
  this.booleanFalse =
      makeLiteral(
          Boolean.FALSE,
          typeFactory.createSqlType(SqlTypeName.BOOLEAN),
          SqlTypeName.BOOLEAN);
  this.charEmpty =
      makeLiteral(
          new NlsString("", null, null),
          typeFactory.createSqlType(SqlTypeName.CHAR, 0),
          SqlTypeName.CHAR);
  this.constantNull =
      makeLiteral(
          null,
          typeFactory.createSqlType(SqlTypeName.NULL),
          SqlTypeName.NULL);
}
 
Example 4
Source Project: kylin-on-parquet-v2   Source File: TupleFilterVisitor.java    License: 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 5
Source Project: dremio-oss   Source File: SqlDatePartOperator.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RexNode convertCall(SqlRexContext cx, SqlCall call) {
  final RexBuilder rexBuilder = cx.getRexBuilder();

  final SqlLiteral literal = (SqlLiteral) call.getOperandList().get(0);
  final String value = ((NlsString)literal.getValue()).getValue();
  TimeUnitRange range = VALID_PERIODS.get(value.toLowerCase());
  Preconditions.checkNotNull(range, "Unhandle range type: %s.", value);
  List<RexNode> exprs = new ArrayList<>();

  exprs.add(rexBuilder.makeFlag(range));
  exprs.add(cx.convertExpression(call.getOperandList().get(1)));

  RelDataTypeFactory typeFactory = cx.getTypeFactory();
  final RelDataType returnType
      = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BIGINT), exprs.get(1).getType().isNullable());
  return rexBuilder.makeCall(returnType, SqlStdOperatorTable.EXTRACT, exprs);
}
 
Example 6
Source Project: kylin   Source File: TupleFilterVisitor.java    License: 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 Project: calcite   Source File: RexBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a RexBuilder.
 *
 * @param typeFactory Type factory
 */
public RexBuilder(RelDataTypeFactory typeFactory) {
  this.typeFactory = typeFactory;
  this.booleanTrue =
      makeLiteral(
          Boolean.TRUE,
          typeFactory.createSqlType(SqlTypeName.BOOLEAN),
          SqlTypeName.BOOLEAN);
  this.booleanFalse =
      makeLiteral(
          Boolean.FALSE,
          typeFactory.createSqlType(SqlTypeName.BOOLEAN),
          SqlTypeName.BOOLEAN);
  this.charEmpty =
      makeLiteral(
          new NlsString("", null, null),
          typeFactory.createSqlType(SqlTypeName.CHAR, 0),
          SqlTypeName.CHAR);
  this.constantNull =
      makeLiteral(
          null,
          typeFactory.createSqlType(SqlTypeName.NULL),
          SqlTypeName.NULL);
}
 
Example 8
Source Project: calcite   Source File: SqlUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates the type of an {@link org.apache.calcite.util.NlsString}.
 *
 * <p>The type inherits the The NlsString's {@link Charset} and
 * {@link SqlCollation}, if they are set, otherwise it gets the system
 * defaults.
 *
 * @param typeFactory Type factory
 * @param str         String
 * @return Type, including collation and charset
 */
public static RelDataType createNlsStringType(
    RelDataTypeFactory typeFactory,
    NlsString str) {
  Charset charset = str.getCharset();
  if (null == charset) {
    charset = typeFactory.getDefaultCharset();
  }
  SqlCollation collation = str.getCollation();
  if (null == collation) {
    collation = SqlCollation.COERCIBLE;
  }
  RelDataType type =
      typeFactory.createSqlType(
          SqlTypeName.CHAR,
          str.getValue().length());
  type =
      typeFactory.createTypeWithCharsetAndCollation(
          type,
          charset,
          collation);
  return type;
}
 
Example 9
Source Project: calcite   Source File: RelBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/** Infers the alias of an expression.
 *
 * <p>If the expression was created by {@link #alias}, replaces the expression
 * in the project list.
 */
private String inferAlias(List<RexNode> exprList, RexNode expr, int i) {
  switch (expr.getKind()) {
  case INPUT_REF:
    final RexInputRef ref = (RexInputRef) expr;
    return stack.peek().fields.get(ref.getIndex()).getValue().getName();
  case CAST:
    return inferAlias(exprList, ((RexCall) expr).getOperands().get(0), -1);
  case AS:
    final RexCall call = (RexCall) expr;
    if (i >= 0) {
      exprList.set(i, call.getOperands().get(0));
    }
    return ((NlsString) ((RexLiteral) call.getOperands().get(1)).getValue())
        .getValue();
  default:
    return null;
  }
}
 
Example 10
Source Project: Bats   Source File: SqlCharStringLiteral.java    License: Apache License 2.0 5 votes vote down vote up
protected SqlAbstractStringLiteral concat1(List<SqlLiteral> literals) {
  return new SqlCharStringLiteral(
      NlsString.concat(
          Util.transform(literals,
              literal -> ((SqlCharStringLiteral) literal).getNlsString())),
      literals.get(0).getParserPosition());
}
 
Example 11
Source Project: Bats   Source File: SqlLiteral.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * For calc program builder - value may be different than {@link #unparse}
 * Typical values:
 *
 * <ul>
 * <li>Hello, world!</li>
 * <li>12.34</li>
 * <li>{null}</li>
 * <li>1969-04-29</li>
 * </ul>
 *
 * @return string representation of the value
 */
public String toValue() {
  if (value == null) {
    return null;
  }
  switch (typeName) {
  case CHAR:

    // We want 'It''s superman!', not _ISO-8859-1'It''s superman!'
    return ((NlsString) value).getValue();
  default:
    return value.toString();
  }
}
 
Example 12
Source Project: Bats   Source File: RexBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static Comparable zeroValue(RelDataType type) {
  switch (type.getSqlTypeName()) {
  case CHAR:
    return new NlsString(Spaces.of(type.getPrecision()), null, null);
  case VARCHAR:
    return new NlsString("", null, null);
  case BINARY:
    return new ByteString(new byte[type.getPrecision()]);
  case VARBINARY:
    return ByteString.EMPTY;
  case TINYINT:
  case SMALLINT:
  case INTEGER:
  case BIGINT:
  case DECIMAL:
  case FLOAT:
  case REAL:
  case DOUBLE:
    return BigDecimal.ZERO;
  case BOOLEAN:
    return false;
  case TIME:
  case DATE:
  case TIMESTAMP:
    return DateTimeUtils.ZERO_CALENDAR;
  case TIME_WITH_LOCAL_TIME_ZONE:
    return new TimeString(0, 0, 0);
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    return new TimestampString(0, 0, 0, 0, 0, 0);
  default:
    throw Util.unexpected(type.getSqlTypeName());
  }
}
 
Example 13
Source Project: Bats   Source File: RexBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/** Returns an {@link NlsString} with spaces to make it at least a given
 * length. */
private static NlsString padRight(NlsString s, int length) {
  if (s.getValue().length() >= length) {
    return s;
  }
  return s.copy(padRight(s.getValue(), length));
}
 
Example 14
Source Project: Bats   Source File: ShowSchemasHandler.java    License: Apache License 2.0 5 votes vote down vote up
/** Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.SCHEMATA ... */
@Override
public SqlNode rewrite(SqlNode sqlNode) throws ForemanSetupException {
  SqlShowSchemas node = unwrap(sqlNode, SqlShowSchemas.class);
  List<SqlNode> selectList = Collections.singletonList(new SqlIdentifier(SCHS_COL_SCHEMA_NAME, SqlParserPos.ZERO));

  SqlNode fromClause = new SqlIdentifier(Arrays.asList(IS_SCHEMA_NAME, InfoSchemaTableType.SCHEMATA.name()), SqlParserPos.ZERO);

  SqlNode where = null;
  SqlNode likePattern = node.getLikePattern();
  if (likePattern != null) {
    SqlNode column = new SqlIdentifier(SCHS_COL_SCHEMA_NAME, SqlParserPos.ZERO);
    // schema names are case insensitive, wrap column in lower function, pattern to lower case
    if (likePattern instanceof SqlCharStringLiteral) {
      NlsString conditionString = ((SqlCharStringLiteral) likePattern).getNlsString();
      likePattern = SqlCharStringLiteral.createCharString(
          conditionString.getValue().toLowerCase(),
          conditionString.getCharsetName(),
          likePattern.getParserPosition());
      column = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, column);
    }
    where = DrillParserUtil.createCondition(column, SqlStdOperatorTable.LIKE, likePattern);
  } else if (node.getWhereClause() != null) {
    where = node.getWhereClause();
  }

  return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO),
      fromClause, where, null, null, null, null, null, null);
}
 
Example 15
Source Project: Bats   Source File: SetOptionHandler.java    License: Apache License 2.0 5 votes vote down vote up
private static Object sqlLiteralToObject(final SqlLiteral literal) {
  final Object object = literal.getValue();
  final SqlTypeName typeName = literal.getTypeName();
  switch (typeName) {
  case DECIMAL: {
    final BigDecimal bigDecimal = (BigDecimal) object;
    if (bigDecimal.scale() == 0) {
      return bigDecimal.longValue();
    } else {
      return bigDecimal.doubleValue();
    }
  }

  case DOUBLE:
  case FLOAT:
    return ((BigDecimal) object).doubleValue();

  case SMALLINT:
  case TINYINT:
  case BIGINT:
  case INTEGER:
    return ((BigDecimal) object).longValue();

  case VARBINARY:
  case VARCHAR:
  case CHAR:
    return ((NlsString) object).getValue().toString();

  case BOOLEAN:
    return object;

  default:
    throw UserException.validationError()
      .message("Drill doesn't support assigning literals of type %s in SET statements.", typeName)
      .build(logger);
  }
}
 
Example 16
Source Project: calcite   Source File: RexLiteral.java    License: Apache License 2.0 5 votes vote down vote up
/** Returns whether a value is valid as a constant value, using the same
 * criteria as {@link #valueMatchesType}. */
public static boolean validConstant(Object o, Litmus litmus) {
  if (o == null
      || o instanceof BigDecimal
      || o instanceof NlsString
      || o instanceof ByteString
      || o instanceof Boolean) {
    return litmus.succeed();
  } else if (o instanceof List) {
    List list = (List) o;
    for (Object o1 : list) {
      if (!validConstant(o1, litmus)) {
        return litmus.fail("not a constant: {}", o1);
      }
    }
    return litmus.succeed();
  } else if (o instanceof Map) {
    @SuppressWarnings("unchecked") final Map<Object, Object> map = (Map) o;
    for (Map.Entry entry : map.entrySet()) {
      if (!validConstant(entry.getKey(), litmus)) {
        return litmus.fail("not a constant: {}", entry.getKey());
      }
      if (!validConstant(entry.getValue(), litmus)) {
        return litmus.fail("not a constant: {}", entry.getValue());
      }
    }
    return litmus.succeed();
  } else {
    return litmus.fail("not a constant: {}", o);
  }
}
 
Example 17
Source Project: marble   Source File: TypeConvertUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static String toString(Object value) {
  if (value == null) {
    return null;
  }
  if (value instanceof NlsString) {
    return ((NlsString) value).getValue();
  } else {
    return value.toString();
  }
}
 
Example 18
Source Project: calcite   Source File: RexBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static Comparable zeroValue(RelDataType type) {
  switch (type.getSqlTypeName()) {
  case CHAR:
    return new NlsString(Spaces.of(type.getPrecision()), null, null);
  case VARCHAR:
    return new NlsString("", null, null);
  case BINARY:
    return new ByteString(new byte[type.getPrecision()]);
  case VARBINARY:
    return ByteString.EMPTY;
  case TINYINT:
  case SMALLINT:
  case INTEGER:
  case BIGINT:
  case DECIMAL:
  case FLOAT:
  case REAL:
  case DOUBLE:
    return BigDecimal.ZERO;
  case BOOLEAN:
    return false;
  case TIME:
  case DATE:
  case TIMESTAMP:
    return DateTimeUtils.ZERO_CALENDAR;
  case TIME_WITH_LOCAL_TIME_ZONE:
    return new TimeString(0, 0, 0);
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    return new TimestampString(0, 0, 0, 0, 0, 0);
  default:
    throw Util.unexpected(type.getSqlTypeName());
  }
}
 
Example 19
Source Project: dremio-oss   Source File: SqlDatePartOperator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkSingleOperandType(SqlCallBinding callBinding, SqlNode node,
    int iFormalOperand, boolean throwOnFailure) {

  // check that the input is a literal.
  if(!super.checkSingleOperandType(callBinding, node, iFormalOperand, throwOnFailure)) {
    return false;
  }

  final RelDataType type = callBinding.getValidator().deriveType(callBinding.getScope(), node);
  final SqlTypeName typeName = type.getSqlTypeName();

  // Pass type checking for operators if it's of type 'ANY'.
  if (typeName.getFamily() == SqlTypeFamily.ANY) {
    return true;
  }

  if(!(typeName == SqlTypeName.CHAR || typeName == SqlTypeName.VARCHAR)) {
    if(throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }

  final SqlLiteral literal = (SqlLiteral) node;
  final String value = ((NlsString)literal.getValue()).getValue();
  if(validStrings.contains(value.toLowerCase())) {
    return true;
  }

  if(throwOnFailure) {
    throw callBinding.newValidationSignatureError();
    //throw new SqlValidatorException(String.format("DATE_PART function only accepts the following values for a date type: %s.", Joiner.on(", ").join(validStrings)), null);
  }

  return false;
}
 
Example 20
Source Project: dremio-oss   Source File: DataAdditionCmdHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to create map of key, value pairs, the value is a Java type object.
 * @param args
 * @return
 */
@VisibleForTesting
public void createStorageOptionsMap(final SqlNodeList args) {
  if (args == null || args.size() == 0) {
    return;
  }

  final ImmutableMap.Builder<String, Object> storageOptions = ImmutableMap.builder();
  for (SqlNode operand : args) {
    if (operand.getKind() != SqlKind.ARGUMENT_ASSIGNMENT) {
      throw UserException.unsupportedError()
        .message("Unsupported argument type. Only assignment arguments (param => value) are supported.")
        .build(logger);
    }
    final List<SqlNode> operandList = ((SqlCall) operand).getOperandList();

    final String name = ((SqlIdentifier) operandList.get(1)).getSimple();
    SqlNode literal = operandList.get(0);
    if (!(literal instanceof SqlLiteral)) {
      throw UserException.unsupportedError()
        .message("Only literals are accepted for storage option values")
        .build(logger);
    }

    Object value = ((SqlLiteral)literal).getValue();
    if (value instanceof NlsString) {
      value = ((NlsString)value).getValue();
    }
    storageOptions.put(name, value);
  }

  this.storageOptionsMap = storageOptions.build();
}
 
Example 21
Source Project: dremio-oss   Source File: AlterTableSetOptionHandler.java    License: Apache License 2.0 5 votes vote down vote up
private static AttributeValue createAttributeValue(final SqlLiteral literal) {
  final Object object = literal.getValue();
  final SqlTypeName typeName = literal.getTypeName();
  switch (typeName) {
    case DOUBLE:
    case FLOAT:
      return AttributeValue.of(((BigDecimal) object).doubleValue());

    case SMALLINT:
    case TINYINT:
    case BIGINT:
    case INTEGER:
      return AttributeValue.of(((BigDecimal) object).longValue());

    case VARBINARY:
    case VARCHAR:
    case CHAR:
      return AttributeValue.of(((NlsString) object).getValue());

    case BOOLEAN:
      return AttributeValue.of((Boolean) object);

    default:
      throw UserException.validationError()
          .message("Dremio doesn't support assigning literals of type %s in SET statements.", typeName)
          .buildSilently();
  }
}
 
Example 22
Source Project: dremio-oss   Source File: SetOptionHandler.java    License: Apache License 2.0 5 votes vote down vote up
private static OptionValue createOptionValue(final String name, final OptionValue.OptionType type,
                                             final SqlLiteral literal) {
  final Object object = literal.getValue();
  final SqlTypeName typeName = literal.getTypeName();
  switch (typeName) {
  case DECIMAL: {
    final BigDecimal bigDecimal = (BigDecimal) object;
    if (bigDecimal.scale() == 0) {
      return OptionValue.createLong(type, name, bigDecimal.longValue());
    } else {
      return OptionValue.createDouble(type, name, bigDecimal.doubleValue());
    }
  }

  case DOUBLE:
  case FLOAT:
    return OptionValue.createDouble(type, name, ((BigDecimal) object).doubleValue());

  case SMALLINT:
  case TINYINT:
  case BIGINT:
  case INTEGER:
    return OptionValue.createLong(type, name, ((BigDecimal) object).longValue());

  case VARBINARY:
  case VARCHAR:
  case CHAR:
    return OptionValue.createString(type, name, ((NlsString) object).getValue());

  case BOOLEAN:
    return OptionValue.createBoolean(type, name, (Boolean) object);

  default:
    throw UserException.validationError()
      .message("Dremio doesn't support assigning literals of type %s in SET statements.", typeName)
      .build(logger);
  }
}
 
Example 23
Source Project: calcite   Source File: SplunkPushDownRule.java    License: Apache License 2.0 5 votes vote down vote up
private String toString(boolean like, RexLiteral literal) {
  String value = null;
  SqlTypeName litSqlType = literal.getTypeName();
  if (SqlTypeName.NUMERIC_TYPES.contains(litSqlType)) {
    value = literal.getValue().toString();
  } else if (litSqlType == SqlTypeName.CHAR) {
    value = ((NlsString) literal.getValue()).getValue();
    if (like) {
      value = value.replace("%", "*");
    }
    value = searchEscape(value);
  }
  return value;
}
 
Example 24
Source Project: calcite   Source File: SqlLiteral.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * For calc program builder - value may be different than {@link #unparse}
 * Typical values:
 *
 * <ul>
 * <li>Hello, world!</li>
 * <li>12.34</li>
 * <li>{null}</li>
 * <li>1969-04-29</li>
 * </ul>
 *
 * @return string representation of the value
 */
public String toValue() {
  if (value == null) {
    return null;
  }
  switch (typeName) {
  case CHAR:

    // We want 'It''s superman!', not _ISO-8859-1'It''s superman!'
    return ((NlsString) value).getValue();
  default:
    return value.toString();
  }
}
 
Example 25
Source Project: Bats   Source File: RelMdSize.java    License: Apache License 2.0 4 votes vote down vote up
/** Estimates the average size (in bytes) of a value of a type.
 *
 * <p>Nulls count as 1 byte.
 */
public double typeValueSize(RelDataType type, Comparable value) {
  if (value == null) {
    return 1d;
  }
  switch (type.getSqlTypeName()) {
  case BOOLEAN:
  case TINYINT:
    return 1d;
  case SMALLINT:
    return 2d;
  case INTEGER:
  case FLOAT:
  case REAL:
  case DATE:
  case TIME:
  case TIME_WITH_LOCAL_TIME_ZONE:
  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
    return 4d;
  case BIGINT:
  case DOUBLE:
  case TIMESTAMP:
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
  case INTERVAL_DAY:
  case INTERVAL_DAY_HOUR:
  case INTERVAL_DAY_MINUTE:
  case INTERVAL_DAY_SECOND:
  case INTERVAL_HOUR:
  case INTERVAL_HOUR_MINUTE:
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_MINUTE:
  case INTERVAL_MINUTE_SECOND:
  case INTERVAL_SECOND:
    return 8d;
  case BINARY:
  case VARBINARY:
    return ((ByteString) value).length();
  case CHAR:
  case VARCHAR:
    return ((NlsString) value).getValue().length() * BYTES_PER_CHARACTER;
  default:
    return 32;
  }
}
 
Example 26
Source Project: Bats   Source File: SqlNodeToRexConverterImpl.java    License: Apache License 2.0 4 votes vote down vote up
public RexNode convertLiteral(
    SqlRexContext cx,
    SqlLiteral literal) {
  RexBuilder rexBuilder = cx.getRexBuilder();
  RelDataTypeFactory typeFactory = cx.getTypeFactory();
  SqlValidator validator = cx.getValidator();
  if (literal.getValue() == null) {
    // Since there is no eq. RexLiteral of SqlLiteral.Unknown we
    // treat it as a cast(null as boolean)
    RelDataType type;
    if (literal.getTypeName() == SqlTypeName.BOOLEAN) {
      type = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
      type = typeFactory.createTypeWithNullability(type, true);
    } else {
      type = validator.getValidatedNodeType(literal);
    }
    return rexBuilder.makeCast(
        type,
        rexBuilder.constantNull());
  }

  BitString bitString;
  SqlIntervalLiteral.IntervalValue intervalValue;
  long l;

  switch (literal.getTypeName()) {
  case DECIMAL:
    // exact number
    BigDecimal bd = literal.getValueAs(BigDecimal.class);
    return rexBuilder.makeExactLiteral(
        bd,
        literal.createSqlType(typeFactory));

  case DOUBLE:
    // approximate type
    // TODO:  preserve fixed-point precision and large integers
    return rexBuilder.makeApproxLiteral(literal.getValueAs(BigDecimal.class));

  case CHAR:
    return rexBuilder.makeCharLiteral(literal.getValueAs(NlsString.class));
  case BOOLEAN:
    return rexBuilder.makeLiteral(literal.getValueAs(Boolean.class));
  case BINARY:
    bitString = literal.getValueAs(BitString.class);
    Preconditions.checkArgument((bitString.getBitCount() % 8) == 0,
        "incomplete octet");

    // An even number of hexits (e.g. X'ABCD') makes whole number
    // of bytes.
    ByteString byteString = new ByteString(bitString.getAsByteArray());
    return rexBuilder.makeBinaryLiteral(byteString);
  case SYMBOL:
    return rexBuilder.makeFlag(literal.getValueAs(Enum.class));
  case TIMESTAMP:
    return rexBuilder.makeTimestampLiteral(
        literal.getValueAs(TimestampString.class),
        ((SqlTimestampLiteral) literal).getPrec());
  case TIME:
    return rexBuilder.makeTimeLiteral(
        literal.getValueAs(TimeString.class),
        ((SqlTimeLiteral) literal).getPrec());
  case DATE:
    return rexBuilder.makeDateLiteral(literal.getValueAs(DateString.class));

  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
  case INTERVAL_DAY:
  case INTERVAL_DAY_HOUR:
  case INTERVAL_DAY_MINUTE:
  case INTERVAL_DAY_SECOND:
  case INTERVAL_HOUR:
  case INTERVAL_HOUR_MINUTE:
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_MINUTE:
  case INTERVAL_MINUTE_SECOND:
  case INTERVAL_SECOND:
    SqlIntervalQualifier sqlIntervalQualifier =
        literal.getValueAs(SqlIntervalLiteral.IntervalValue.class)
            .getIntervalQualifier();
    return rexBuilder.makeIntervalLiteral(
        literal.getValueAs(BigDecimal.class),
        sqlIntervalQualifier);
  default:
    throw Util.unexpected(literal.getTypeName());
  }
}
 
Example 27
Source Project: Bats   Source File: SqlCharStringLiteral.java    License: Apache License 2.0 4 votes vote down vote up
protected SqlCharStringLiteral(NlsString val, SqlParserPos pos) {
  super(val, SqlTypeName.CHAR, pos);
}
 
Example 28
Source Project: Bats   Source File: SqlCharStringLiteral.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return the underlying NlsString
 */
public NlsString getNlsString() {
  return (NlsString) value;
}
 
Example 29
Source Project: Bats   Source File: SqlCharStringLiteral.java    License: Apache License 2.0 4 votes vote down vote up
@Override public SqlCharStringLiteral clone(SqlParserPos pos) {
  return new SqlCharStringLiteral((NlsString) value, pos);
}
 
Example 30
Source Project: Bats   Source File: SqlCallBinding.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override public String getStringLiteralOperand(int ordinal) {
  SqlNode node = call.operand(ordinal);
  final Object o = SqlLiteral.value(node);
  return o instanceof NlsString ? ((NlsString) o).getValue() : null;
}