Java Code Examples for org.apache.calcite.sql.SqlLiteral#intValue()

The following examples show how to use org.apache.calcite.sql.SqlLiteral#intValue() . 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 6 votes vote down vote up
public SqlNode visit(SqlLiteral literal) {
	// Ordinal markers, e.g. 'select a, b from t order by 2'.
	// Only recognize them if they are the whole expression,
	// and if the dialect permits.
	if (literal == root && getConformance().isSortByOrdinal()) {
		switch (literal.getTypeName()) {
			case DECIMAL:
			case DOUBLE:
				final int intValue = literal.intValue(false);
				if (intValue >= 0) {
					if (intValue < 1 || intValue > aliasList.size()) {
						throw newValidationError(
							literal, RESOURCE.orderByOrdinalOutOfRange());
					}

					// SQL ordinals are 1-based, but Sort's are 0-based
					int ordinal = intValue - 1;
					return nthSelectItem(ordinal, literal.getParserPosition());
				}
				break;
		}
	}

	return super.visit(literal);
}
 
Example 2
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
public SqlNode visit(SqlLiteral literal) {
	// Ordinal markers, e.g. 'select a, b from t order by 2'.
	// Only recognize them if they are the whole expression,
	// and if the dialect permits.
	if (literal == root && getConformance().isSortByOrdinal()) {
		switch (literal.getTypeName()) {
			case DECIMAL:
			case DOUBLE:
				final int intValue = literal.intValue(false);
				if (intValue >= 0) {
					if (intValue < 1 || intValue > aliasList.size()) {
						throw newValidationError(
							literal, RESOURCE.orderByOrdinalOutOfRange());
					}

					// SQL ordinals are 1-based, but Sort's are 0-based
					int ordinal = intValue - 1;
					return nthSelectItem(ordinal, literal.getParserPosition());
				}
				break;
		}
	}

	return super.visit(literal);
}
 
Example 3
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public SqlNode visit(SqlLiteral literal) {
	if (havingExpr || !validator.getConformance().isGroupByOrdinal()) {
		return super.visit(literal);
	}
	boolean isOrdinalLiteral = literal == root;
	switch (root.getKind()) {
		case GROUPING_SETS:
		case ROLLUP:
		case CUBE:
			if (root instanceof SqlBasicCall) {
				List<SqlNode> operandList = ((SqlBasicCall) root).getOperandList();
				for (SqlNode node : operandList) {
					if (node.equals(literal)) {
						isOrdinalLiteral = true;
						break;
					}
				}
			}
			break;
	}
	if (isOrdinalLiteral) {
		switch (literal.getTypeName()) {
			case DECIMAL:
			case DOUBLE:
				final int intValue = literal.intValue(false);
				if (intValue >= 0) {
					if (intValue < 1 || intValue > select.getSelectList().size()) {
						throw validator.newValidationError(literal,
							RESOURCE.orderByOrdinalOutOfRange());
					}

					// SQL ordinals are 1-based, but Sort's are 0-based
					int ordinal = intValue - 1;
					return SqlUtil.stripAs(select.getSelectList().get(ordinal));
				}
				break;
		}
	}

	return super.visit(literal);
}
 
Example 4
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
public SqlNode visit(SqlLiteral literal) {
	if (havingExpr || !validator.getConformance().isGroupByOrdinal()) {
		return super.visit(literal);
	}
	boolean isOrdinalLiteral = literal == root;
	switch (root.getKind()) {
		case GROUPING_SETS:
		case ROLLUP:
		case CUBE:
			if (root instanceof SqlBasicCall) {
				List<SqlNode> operandList = ((SqlBasicCall) root).getOperandList();
				for (SqlNode node : operandList) {
					if (node.equals(literal)) {
						isOrdinalLiteral = true;
						break;
					}
				}
			}
			break;
	}
	if (isOrdinalLiteral) {
		switch (literal.getTypeName()) {
			case DECIMAL:
			case DOUBLE:
				final int intValue = literal.intValue(false);
				if (intValue >= 0) {
					if (intValue < 1 || intValue > select.getSelectList().size()) {
						throw validator.newValidationError(literal,
							RESOURCE.orderByOrdinalOutOfRange());
					}

					// SQL ordinals are 1-based, but Sort's are 0-based
					int ordinal = intValue - 1;
					return SqlUtil.stripAs(select.getSelectList().get(ordinal));
				}
				break;
		}
	}

	return super.visit(literal);
}