Java Code Examples for org.apache.calcite.util.Util#unexpected()

The following examples show how to use org.apache.calcite.util.Util#unexpected() . 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: SqlLiteral.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the long value of this literal.
 *
 * @param exact Whether the value has to be exact. If true, and the literal
 *              is a fraction (e.g. 3.14), throws. If false, discards the
 *              fractional part of the value.
 * @return Long value of this literal
 */
public long longValue(boolean exact) {
  switch (typeName) {
  case DECIMAL:
  case DOUBLE:
    BigDecimal bd = (BigDecimal) value;
    if (exact) {
      try {
        return bd.longValueExact();
      } catch (ArithmeticException e) {
        throw SqlUtil.newContextException(getParserPosition(),
            RESOURCE.numberLiteralOutOfRange(bd.toString()));
      }
    } else {
      return bd.longValue();
    }
  default:
    throw Util.unexpected(typeName);
  }
}
 
Example 2
Source File: DateRangeRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Range<Calendar> floorRange(TimeUnitRange timeUnit, SqlKind comparison,
    Calendar c) {
  Calendar floor = floor(c, timeUnit);
  boolean boundary = floor.equals(c);
  switch (comparison) {
  case EQUALS:
    return Range.closedOpen(floor, boundary ? increment(floor, timeUnit) : floor);
  case LESS_THAN:
    return boundary ? Range.lessThan(floor) : Range.lessThan(increment(floor, timeUnit));
  case LESS_THAN_OR_EQUAL:
    return Range.lessThan(increment(floor, timeUnit));
  case GREATER_THAN:
    return Range.atLeast(increment(floor, timeUnit));
  case GREATER_THAN_OR_EQUAL:
    return boundary ? Range.atLeast(floor) : Range.atLeast(increment(floor, timeUnit));
  default:
    throw Util.unexpected(comparison);
  }
}
 
Example 3
Source File: SqlLiteral.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the integer value of this literal.
 *
 * @param exact Whether the value has to be exact. If true, and the literal
 *              is a fraction (e.g. 3.14), throws. If false, discards the
 *              fractional part of the value.
 * @return Integer value of this literal
 */
public int intValue(boolean exact) {
  switch (typeName) {
  case DECIMAL:
  case DOUBLE:
    BigDecimal bd = (BigDecimal) value;
    if (exact) {
      try {
        return bd.intValueExact();
      } catch (ArithmeticException e) {
        throw SqlUtil.newContextException(getParserPosition(),
            RESOURCE.numberLiteralOutOfRange(bd.toString()));
      }
    } else {
      return bd.intValue();
    }
  default:
    throw Util.unexpected(typeName);
  }
}
 
Example 4
Source File: RexImpTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static long getFactor(TimeUnit unit) {
  switch (unit) {
  case DAY:
    return 1L;
  case HOUR:
    return TimeUnit.DAY.multiplier.longValue();
  case MINUTE:
    return TimeUnit.HOUR.multiplier.longValue();
  case SECOND:
    return TimeUnit.MINUTE.multiplier.longValue();
  case MILLISECOND:
    return TimeUnit.SECOND.multiplier.longValue();
  case MONTH:
    return TimeUnit.YEAR.multiplier.longValue();
  case QUARTER:
    return TimeUnit.YEAR.multiplier.longValue();
  case YEAR:
  case DECADE:
  case CENTURY:
  case MILLENNIUM:
    return 1L;
  default:
    throw Util.unexpected(unit);
  }
}
 
Example 5
Source File: DateRangeRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Range<Calendar> ceilRange(TimeUnitRange timeUnit, SqlKind comparison,
    Calendar c) {
  final Calendar ceil = ceil(c, timeUnit);
  boolean boundary = ceil.equals(c);
  switch (comparison) {
  case EQUALS:
    return Range.openClosed(boundary ? decrement(ceil, timeUnit) : ceil, ceil);
  case LESS_THAN:
    return Range.atMost(decrement(ceil, timeUnit));
  case LESS_THAN_OR_EQUAL:
    return boundary ? Range.atMost(ceil) : Range.atMost(decrement(ceil, timeUnit));
  case GREATER_THAN:
    return boundary ? Range.greaterThan(ceil) : Range.greaterThan(decrement(ceil, timeUnit));
  case GREATER_THAN_OR_EQUAL:
    return Range.greaterThan(decrement(ceil, timeUnit));
  default:
    throw Util.unexpected(comparison);
  }
}
 
Example 6
Source File: AbstractNamespace.java    From calcite with Apache License 2.0 6 votes vote down vote up
public final void validate(RelDataType targetRowType) {
  switch (status) {
  case UNVALIDATED:
    try {
      status = SqlValidatorImpl.Status.IN_PROGRESS;
      Preconditions.checkArgument(rowType == null,
          "Namespace.rowType must be null before validate has been called");
      RelDataType type = validateImpl(targetRowType);
      Preconditions.checkArgument(type != null,
          "validateImpl() returned null");
      setType(type);
    } finally {
      status = SqlValidatorImpl.Status.VALID;
    }
    break;
  case IN_PROGRESS:
    throw new AssertionError("Cycle detected during type-checking");
  case VALID:
    break;
  default:
    throw Util.unexpected(status);
  }
}
 
Example 7
Source File: SqlLiteral.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the integer value of this literal.
 *
 * @param exact Whether the value has to be exact. If true, and the literal
 *              is a fraction (e.g. 3.14), throws. If false, discards the
 *              fractional part of the value.
 * @return Integer value of this literal
 */
public int intValue(boolean exact) {
  switch (typeName) {
  case DECIMAL:
  case DOUBLE:
    BigDecimal bd = (BigDecimal) value;
    if (exact) {
      try {
        return bd.intValueExact();
      } catch (ArithmeticException e) {
        throw SqlUtil.newContextException(getParserPosition(),
            RESOURCE.numberLiteralOutOfRange(bd.toString()));
      }
    } else {
      return bd.intValue();
    }
  default:
    throw Util.unexpected(typeName);
  }
}
 
Example 8
Source File: AbstractNamespace.java    From Bats with Apache License 2.0 6 votes vote down vote up
public final void validate(RelDataType targetRowType) {
  switch (status) {
  case UNVALIDATED:
    try {
      status = SqlValidatorImpl.Status.IN_PROGRESS;
      Preconditions.checkArgument(rowType == null,
          "Namespace.rowType must be null before validate has been called");
      RelDataType type = validateImpl(targetRowType);
      Preconditions.checkArgument(type != null,
          "validateImpl() returned null");
      setType(type);
    } finally {
      status = SqlValidatorImpl.Status.VALID;
    }
    break;
  case IN_PROGRESS:
    throw new AssertionError("Cycle detected during type-checking");
  case VALID:
    break;
  default:
    throw Util.unexpected(status);
  }
}
 
Example 9
Source File: DateRangeRules.java    From Bats with Apache License 2.0 6 votes vote down vote up
private Range<Calendar> ceilRange(TimeUnitRange timeUnit, SqlKind comparison, Calendar c) {
    final Calendar ceil = ceil(c, timeUnit);
    boolean boundary = ceil.equals(c);
    switch (comparison) {
    case EQUALS:
        return Range.openClosed(boundary ? decrement(ceil, timeUnit) : ceil, ceil);
    case LESS_THAN:
        return Range.atMost(decrement(ceil, timeUnit));
    case LESS_THAN_OR_EQUAL:
        return boundary ? Range.atMost(ceil) : Range.atMost(decrement(ceil, timeUnit));
    case GREATER_THAN:
        return boundary ? Range.greaterThan(ceil) : Range.greaterThan(decrement(ceil, timeUnit));
    case GREATER_THAN_OR_EQUAL:
        return Range.greaterThan(decrement(ceil, timeUnit));
    default:
        throw Util.unexpected(comparison);
    }
}
 
Example 10
Source File: DateRangeRules.java    From Bats with Apache License 2.0 6 votes vote down vote up
private Calendar timestampValue(RexLiteral timeLiteral) {
    switch (timeLiteral.getTypeName()) {
    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
        final TimeZone tz = TimeZone.getTimeZone(this.timeZone);
        return Util.calendar(
                SqlFunctions.timestampWithLocalTimeZoneToTimestamp(timeLiteral.getValueAs(Long.class), tz));
    case TIMESTAMP:
        return Util.calendar(timeLiteral.getValueAs(Long.class));
    case DATE:
        // Cast date to timestamp with local time zone
        final DateString d = timeLiteral.getValueAs(DateString.class);
        return Util.calendar(d.getMillisSinceEpoch());
    default:
        throw Util.unexpected(timeLiteral.getTypeName());
    }
}
 
Example 11
Source File: DateRangeRules.java    From Bats with Apache License 2.0 6 votes vote down vote up
private RexLiteral dateTimeLiteral(RexBuilder rexBuilder, Calendar calendar, RexNode operand) {
    final TimestampString ts;
    final int p;
    switch (operand.getType().getSqlTypeName()) {
    case TIMESTAMP:
        ts = TimestampString.fromCalendarFields(calendar);
        p = operand.getType().getPrecision();
        return rexBuilder.makeTimestampLiteral(ts, p);
    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
        ts = TimestampString.fromCalendarFields(calendar);
        final TimeZone tz = TimeZone.getTimeZone(this.timeZone);
        final TimestampString localTs = new TimestampWithTimeZoneString(ts, tz)
                .withTimeZone(DateTimeUtils.UTC_ZONE).getLocalTimestampString();
        p = operand.getType().getPrecision();
        return rexBuilder.makeTimestampWithLocalTimeZoneLiteral(localTs, p);
    case DATE:
        final DateString d = DateString.fromCalendarFields(calendar);
        return rexBuilder.makeDateLiteral(d);
    default:
        throw Util.unexpected(operand.getType().getSqlTypeName());
    }
}
 
Example 12
Source File: SqlLiteral.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void unparse(
    SqlWriter writer,
    int leftPrec,
    int rightPrec) {
  switch (typeName) {
  case BOOLEAN:
    writer.keyword(
        value == null ? "UNKNOWN" : (Boolean) value ? "TRUE" : "FALSE");
    break;
  case NULL:
    writer.keyword("NULL");
    break;
  case CHAR:
  case DECIMAL:
  case DOUBLE:
  case BINARY:
    // should be handled in subtype
    throw Util.unexpected(typeName);

  case SYMBOL:
    if (value instanceof Enum) {
      Enum enumVal = (Enum) value;
      writer.keyword(enumVal.toString());
    } else {
      writer.keyword(String.valueOf(value));
    }
    break;
  default:
    writer.literal(value.toString());
  }
}
 
Example 13
Source File: RexBuilder.java    From calcite with 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 14
Source File: RexBuilder.java    From Bats with 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 15
Source File: SqlLiteral.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void unparse(
    SqlWriter writer,
    int leftPrec,
    int rightPrec) {
  switch (typeName) {
  case BOOLEAN:
    writer.keyword(
        value == null ? "UNKNOWN" : (Boolean) value ? "TRUE" : "FALSE");
    break;
  case NULL:
    writer.keyword("NULL");
    break;
  case CHAR:
  case DECIMAL:
  case DOUBLE:
  case BINARY:
    // should be handled in subtype
    throw Util.unexpected(typeName);

  case SYMBOL:
    if (value instanceof Enum) {
      Enum enumVal = (Enum) value;
      writer.keyword(enumVal.toString());
    } else {
      writer.keyword(String.valueOf(value));
    }
    break;
  default:
    writer.literal(value.toString());
  }
}
 
Example 16
Source File: ExtendedSqlCollectionTypeNameSpec.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Create collection data type.
 *
 * @param elementType Type of the collection element
 * @param typeFactory Type factory
 * @return The collection data type, or throw exception if the collection
 *         type name does not belong to {@code SqlTypeName} enumerations
 */
private RelDataType createCollectionType(RelDataType elementType,
		RelDataTypeFactory typeFactory) {
	switch (collectionTypeName) {
	case MULTISET:
		return typeFactory.createMultisetType(elementType, -1);
	case ARRAY:
		return typeFactory.createArrayType(elementType, -1);

	default:
		throw Util.unexpected(collectionTypeName);
	}
}
 
Example 17
Source File: SqlCollectionTypeNameSpec.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Create collection data type.
 *
 * @param elementType Type of the collection element
 * @param typeFactory Type factory
 * @return The collection data type, or throw exception if the collection
 *         type name does not belong to {@code SqlTypeName} enumerations
 */
private RelDataType createCollectionType(RelDataType elementType,
    RelDataTypeFactory typeFactory) {
  switch (collectionTypeName) {
  case MULTISET:
    return typeFactory.createMultisetType(elementType, -1);
  case ARRAY:
    return typeFactory.createArrayType(elementType, -1);

  default:
    throw Util.unexpected(collectionTypeName);
  }
}
 
Example 18
Source File: SqlLiteral.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * @return whether value is appropriate for its type (we have rules about
 * these things)
 */
public static boolean valueMatchesType(
    Object value,
    SqlTypeName typeName) {
  switch (typeName) {
  case BOOLEAN:
    return (value == null) || (value instanceof Boolean);
  case NULL:
    return value == null;
  case DECIMAL:
  case DOUBLE:
    return value instanceof BigDecimal;
  case DATE:
    return value instanceof DateString;
  case TIME:
    return value instanceof TimeString;
  case TIMESTAMP:
    return value instanceof TimestampString;
  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:
    return value instanceof SqlIntervalLiteral.IntervalValue;
  case BINARY:
    return value instanceof BitString;
  case CHAR:
    return value instanceof NlsString;
  case SYMBOL:
    return (value instanceof Enum)
        || (value instanceof SqlSampleSpec);
  case MULTISET:
    return true;
  case INTEGER: // not allowed -- use Decimal
  case VARCHAR: // not allowed -- use Char
  case VARBINARY: // not allowed -- use Binary
  default:
    throw Util.unexpected(typeName);
  }
}
 
Example 19
Source File: RexLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * @return whether value is appropriate for its type (we have rules about
 * these things)
 */
public static boolean valueMatchesType(
    Comparable value,
    SqlTypeName typeName,
    boolean strict) {
  if (value == null) {
    return true;
  }
  switch (typeName) {
  case BOOLEAN:
    // Unlike SqlLiteral, we do not allow boolean null.
    return value instanceof Boolean;
  case NULL:
    return false; // value should have been null
  case INTEGER: // not allowed -- use Decimal
  case TINYINT:
  case SMALLINT:
    if (strict) {
      throw Util.unexpected(typeName);
    }
    // fall through
  case DECIMAL:
  case DOUBLE:
  case FLOAT:
  case REAL:
  case BIGINT:
    return value instanceof BigDecimal;
  case DATE:
    return value instanceof DateString;
  case TIME:
    return value instanceof TimeString;
  case TIME_WITH_LOCAL_TIME_ZONE:
    return value instanceof TimeString;
  case TIMESTAMP:
    return value instanceof TimestampString;
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    return value instanceof TimestampString;
  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:
    // The value of a DAY-TIME interval (whatever the start and end units,
    // even say HOUR TO MINUTE) is in milliseconds (perhaps fractional
    // milliseconds). The value of a YEAR-MONTH interval is in months.
    return value instanceof BigDecimal;
  case VARBINARY: // not allowed -- use Binary
    if (strict) {
      throw Util.unexpected(typeName);
    }
    // fall through
  case BINARY:
    return value instanceof ByteString;
  case VARCHAR: // not allowed -- use Char
    if (strict) {
      throw Util.unexpected(typeName);
    }
    // fall through
  case CHAR:
    // A SqlLiteral's charset and collation are optional; not so a
    // RexLiteral.
    return (value instanceof NlsString)
        && (((NlsString) value).getCharset() != null)
        && (((NlsString) value).getCollation() != null);
  case SYMBOL:
    return value instanceof Enum;
  case ROW:
  case MULTISET:
    return value instanceof List;
  case ANY:
    // Literal of type ANY is not legal. "CAST(2 AS ANY)" remains
    // an integer literal surrounded by a cast function.
    return false;
  default:
    throw Util.unexpected(typeName);
  }
}
 
Example 20
Source File: SqlLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * @return whether value is appropriate for its type (we have rules about
 * these things)
 */
public static boolean valueMatchesType(
    Object value,
    SqlTypeName typeName) {
  switch (typeName) {
  case BOOLEAN:
    return (value == null) || (value instanceof Boolean);
  case NULL:
    return value == null;
  case DECIMAL:
  case DOUBLE:
    return value instanceof BigDecimal;
  case DATE:
    return value instanceof DateString;
  case TIME:
    return value instanceof TimeString;
  case TIMESTAMP:
    return value instanceof TimestampString;
  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:
    return value instanceof SqlIntervalLiteral.IntervalValue;
  case BINARY:
    return value instanceof BitString;
  case CHAR:
    return value instanceof NlsString;
  case SYMBOL:
    return (value instanceof Enum)
        || (value instanceof SqlSampleSpec);
  case MULTISET:
    return true;
  case INTEGER: // not allowed -- use Decimal
  case VARCHAR: // not allowed -- use Char
  case VARBINARY: // not allowed -- use Binary
  default:
    throw Util.unexpected(typeName);
  }
}