use of org.apache.calcite.util.BitString in project calcite by apache.
the class SqlValidatorImpl method validateLiteral.
public void validateLiteral(SqlLiteral literal) {
switch(literal.getTypeName()) {
case DECIMAL:
// Decimal and long have the same precision (as 64-bit integers), so
// the unscaled value of a decimal must fit into a long.
// REVIEW jvs 4-Aug-2004: This should probably be calling over to
// the available calculator implementations to see what they
// support. For now use ESP instead.
//
// jhyde 2006/12/21: I think the limits should be baked into the
// type system, not dependent on the calculator implementation.
BigDecimal bd = (BigDecimal) literal.getValue();
BigInteger unscaled = bd.unscaledValue();
long longValue = unscaled.longValue();
if (!BigInteger.valueOf(longValue).equals(unscaled)) {
// overflow
throw newValidationError(literal, RESOURCE.numberLiteralOutOfRange(bd.toString()));
}
break;
case DOUBLE:
validateLiteralAsDouble(literal);
break;
case BINARY:
final BitString bitString = (BitString) literal.getValue();
if ((bitString.getBitCount() % 8) != 0) {
throw newValidationError(literal, RESOURCE.binaryLiteralOdd());
}
break;
case DATE:
case TIME:
case TIMESTAMP:
Calendar calendar = literal.getValueAs(Calendar.class);
final int year = calendar.get(Calendar.YEAR);
final int era = calendar.get(Calendar.ERA);
if (year < 1 || era == GregorianCalendar.BC || year > 9999) {
throw newValidationError(literal, RESOURCE.dateLiteralOutOfRange(literal.toString()));
}
break;
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:
if (literal instanceof SqlIntervalLiteral) {
SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue();
SqlIntervalQualifier intervalQualifier = interval.getIntervalQualifier();
// ensure qualifier is good before attempting to validate literal
validateIntervalQualifier(intervalQualifier);
String intervalStr = interval.getIntervalLiteral();
// throws CalciteContextException if string is invalid
int[] values = intervalQualifier.evaluateIntervalLiteral(intervalStr, literal.getParserPosition(), typeFactory.getTypeSystem());
Util.discard(values);
}
break;
default:
}
}
use of org.apache.calcite.util.BitString in project flink by apache.
the class SqlValidatorImpl method validateLiteral.
public void validateLiteral(SqlLiteral literal) {
switch(literal.getTypeName()) {
case DECIMAL:
// Decimal and long have the same precision (as 64-bit integers), so
// the unscaled value of a decimal must fit into a long.
// REVIEW jvs 4-Aug-2004: This should probably be calling over to
// the available calculator implementations to see what they
// support. For now use ESP instead.
//
// jhyde 2006/12/21: I think the limits should be baked into the
// type system, not dependent on the calculator implementation.
BigDecimal bd = (BigDecimal) literal.getValue();
BigInteger unscaled = bd.unscaledValue();
long longValue = unscaled.longValue();
if (!BigInteger.valueOf(longValue).equals(unscaled)) {
// overflow
throw newValidationError(literal, RESOURCE.numberLiteralOutOfRange(bd.toString()));
}
break;
case DOUBLE:
validateLiteralAsDouble(literal);
break;
case BINARY:
final BitString bitString = (BitString) literal.getValue();
if ((bitString.getBitCount() % 8) != 0) {
throw newValidationError(literal, RESOURCE.binaryLiteralOdd());
}
break;
case DATE:
case TIME:
case TIMESTAMP:
Calendar calendar = literal.getValueAs(Calendar.class);
final int year = calendar.get(Calendar.YEAR);
final int era = calendar.get(Calendar.ERA);
if (year < 1 || era == GregorianCalendar.BC || year > 9999) {
throw newValidationError(literal, RESOURCE.dateLiteralOutOfRange(literal.toString()));
}
break;
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:
if (literal instanceof SqlIntervalLiteral) {
SqlIntervalLiteral.IntervalValue interval = literal.getValueAs(SqlIntervalLiteral.IntervalValue.class);
SqlIntervalQualifier intervalQualifier = interval.getIntervalQualifier();
// ensure qualifier is good before attempting to validate literal
validateIntervalQualifier(intervalQualifier);
String intervalStr = interval.getIntervalLiteral();
// throws CalciteContextException if string is invalid
int[] values = intervalQualifier.evaluateIntervalLiteral(intervalStr, literal.getParserPosition(), typeFactory.getTypeSystem());
Util.discard(values);
}
break;
default:
}
}
use of org.apache.calcite.util.BitString in project calcite by apache.
the class SqlLiteral method createSqlType.
public RelDataType createSqlType(RelDataTypeFactory typeFactory) {
BitString bitString;
switch(typeName) {
case NULL:
case BOOLEAN:
RelDataType ret = typeFactory.createSqlType(typeName);
ret = typeFactory.createTypeWithNullability(ret, null == value);
return ret;
case BINARY:
bitString = (BitString) value;
int bitCount = bitString.getBitCount();
return typeFactory.createSqlType(SqlTypeName.BINARY, bitCount / 8);
case CHAR:
NlsString string = (NlsString) value;
Charset charset = string.getCharset();
if (null == charset) {
charset = typeFactory.getDefaultCharset();
}
SqlCollation collation = string.getCollation();
if (null == collation) {
collation = SqlCollation.COERCIBLE;
}
RelDataType type = typeFactory.createSqlType(SqlTypeName.CHAR, string.getValue().length());
type = typeFactory.createTypeWithCharsetAndCollation(type, charset, collation);
return type;
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:
SqlIntervalLiteral.IntervalValue intervalValue = (SqlIntervalLiteral.IntervalValue) value;
return typeFactory.createSqlIntervalType(intervalValue.getIntervalQualifier());
case SYMBOL:
return typeFactory.createSqlType(SqlTypeName.SYMBOL);
// handled in derived class
case INTEGER:
// handled in derived class
case TIME:
// should never happen
case VARCHAR:
// should never happen
case VARBINARY:
default:
throw Util.needToImplement(toString() + ", operand=" + value);
}
}
use of org.apache.calcite.util.BitString in project calcite by apache.
the class SqlLiteralChainOperator method unparse.
public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
final SqlWriter.Frame frame = writer.startList("", "");
SqlCollation collation = null;
for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
SqlLiteral rand = (SqlLiteral) operand.e;
if (operand.i > 0) {
// SQL:2003 says there must be a newline between string
// fragments.
writer.newlineAndIndent();
}
if (rand instanceof SqlCharStringLiteral) {
NlsString nls = ((SqlCharStringLiteral) rand).getNlsString();
if (operand.i == 0) {
collation = nls.getCollation();
// print with prefix
writer.literal(nls.asSql(true, false));
} else {
// print without prefix
writer.literal(nls.asSql(false, false));
}
} else if (operand.i == 0) {
// print with prefix
rand.unparse(writer, leftPrec, rightPrec);
} else {
// print without prefix
if (rand.getTypeName() == SqlTypeName.BINARY) {
BitString bs = (BitString) rand.getValue();
writer.literal("'" + bs.toHexString() + "'");
} else {
writer.literal("'" + rand.toValue() + "'");
}
}
}
if (collation != null) {
collation.unparse(writer, 0, 0);
}
writer.endList(frame);
}
use of org.apache.calcite.util.BitString in project calcite by apache.
the class SqlNodeToRexConverterImpl method convertLiteral.
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:
// 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());
}
}
Aggregations