Java Code Examples for org.apache.calcite.sql.SqlIntervalQualifier#evaluateIntervalLiteral()

The following examples show how to use org.apache.calcite.sql.SqlIntervalQualifier#evaluateIntervalLiteral() . 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: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
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:
			// default is to do nothing
	}
}
 
Example 2
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
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:
			// default is to do nothing
	}
}