use of org.apache.calcite.sql.SqlLiteral in project hazelcast by hazelcast.
the class HazelcastDynamicTableFunction method extractMapStringValue.
private static String extractMapStringValue(String functionName, HazelcastTableFunctionParameter parameter, SqlNode node, HazelcastSqlValidator validator) {
if (node.getKind() == SqlKind.DYNAMIC_PARAM) {
Object value = validator.getArgumentAt(((SqlDynamicParam) node).getIndex());
if (value instanceof String) {
return (String) value;
}
}
if (SqlUtil.isLiteral(node)) {
SqlLiteral literal = (SqlLiteral) node;
Object value = literal.getValue();
if (value instanceof NlsString) {
return ((NlsString) value).getValue();
}
}
throw QueryException.error("All values in the MAP constructor of the call to function " + functionName + ", argument #" + parameter.ordinal() + " (" + parameter.name() + ") must be VARCHAR literals. " + "Actual argument is: " + (SqlUtil.isLiteral(node) ? ((SqlLiteral) node).getTypeName() : node.getKind()));
}
use of org.apache.calcite.sql.SqlLiteral in project hazelcast by hazelcast.
the class HazelcastTrimFunction method getOperandsForSignatureError.
@Override
public Collection<SqlNode> getOperandsForSignatureError(SqlCall call) {
SqlNode fromOperand = call.operand(1);
SqlNode targetOperand = call.operand(2);
SqlTypeName literalType = LiteralUtils.literalTypeName(fromOperand);
if (literalType == SqlTypeName.VARCHAR && " ".equals(((SqlLiteral) fromOperand).getValueAs(String.class))) {
// Default value for the FROM operand, report only target operand.
return Collections.singletonList(targetOperand);
}
// Non-default FROM, report both target and FROM operands.
return Arrays.asList(fromOperand, targetOperand);
}
use of org.apache.calcite.sql.SqlLiteral in project hazelcast by hazelcast.
the class HazelcastInOperator method deriveType.
@Override
public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
final List<SqlNode> operands = call.getOperandList();
assert operands.size() == 2;
final SqlNode left = operands.get(0);
final SqlNode right = operands.get(1);
final RelDataTypeFactory typeFactory = validator.getTypeFactory();
RelDataType leftType = validator.deriveType(scope, left);
RelDataType rightType;
// Derive type for RHS.
if (right instanceof SqlNodeList) {
// Handle the 'IN (expr, ...)' form.
List<RelDataType> rightTypeList = new ArrayList<>();
SqlNodeList nodeList = (SqlNodeList) right;
for (SqlNode node : nodeList) {
if (node instanceof SqlLiteral) {
SqlLiteral lit = (SqlLiteral) node;
// We are not supporting raw NULL literals within IN right-hand side list.
if (lit.getValue() == null) {
throw validator.newValidationError(right, HZRESOURCE.noRawNullsAllowed());
}
}
RelDataType nodeType = validator.deriveType(scope, node);
rightTypeList.add(nodeType);
}
rightType = typeFactory.leastRestrictive(rightTypeList);
// Same rules as the VALUES operator (per SQL:2003 Part 2 Section 8.4, <in predicate>).
if (null == rightType && validator.config().typeCoercionEnabled()) {
// Do implicit type cast if it is allowed to.
rightType = validator.getTypeCoercion().getWiderTypeFor(rightTypeList, false);
}
if (null == rightType) {
throw validator.newValidationError(right, RESOURCE.incompatibleTypesInList());
}
// Record the RHS type for use by SqlToRelConverter.
validator.setValidatedNodeType(nodeList, rightType);
} else {
// We do not support sub-querying for IN operator.
throw validator.newValidationError(call, HZRESOURCE.noSubQueryAllowed());
}
HazelcastCallBinding hazelcastCallBinding = prepareBinding(new SqlCallBinding(validator, scope, call));
// Coerce type first.
if (hazelcastCallBinding.isTypeCoercionEnabled()) {
boolean coerced = hazelcastCallBinding.getValidator().getTypeCoercion().inOperationCoercion(hazelcastCallBinding);
if (coerced) {
// Update the node data type if we coerced any type.
leftType = validator.deriveType(scope, call.operand(0));
rightType = validator.deriveType(scope, call.operand(1));
}
}
// Now check that the left expression is compatible with the
// type of the list. Same strategy as the '=' operator.
// Normalize the types on both sides to be row types
// for the purposes of compatibility-checking.
RelDataType leftRowType = SqlTypeUtil.promoteToRowType(typeFactory, leftType, null);
RelDataType rightRowType = SqlTypeUtil.promoteToRowType(typeFactory, rightType, null);
final ComparableOperandTypeChecker checker = (ComparableOperandTypeChecker) OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED;
if (!checker.checkOperandTypes(new ExplicitOperatorBinding(hazelcastCallBinding, ImmutableList.of(leftRowType, rightRowType)), hazelcastCallBinding)) {
throw validator.newValidationError(call, RESOURCE.incompatibleValueType(SqlStdOperatorTable.IN.getName()));
}
return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BOOLEAN), anyNullable(leftRowType.getFieldList()) || anyNullable(rightRowType.getFieldList()));
}
use of org.apache.calcite.sql.SqlLiteral in project hazelcast by hazelcast.
the class LiteralTypeResolutionTest method checkLiteral.
private void checkLiteral(SqlLiteral literal, Object expectedValue, Object expectedStringValue, SqlTypeName expectedTypeName, boolean expectedNullable) {
String expectedStringValue0 = expectedStringValue != null ? expectedStringValue.toString() : null;
Literal literal0 = LiteralUtils.literal(literal);
assertNotNull(literal0);
assertEquals(expectedValue, literal0.getValue());
assertEquals(expectedStringValue0, literal0.getStringValue());
assertEquals(expectedTypeName, literal0.getTypeName());
assertEquals(expectedTypeName, literal0.getType(HazelcastTypeFactory.INSTANCE).getSqlTypeName());
assertEquals(expectedNullable, literal0.getType(HazelcastTypeFactory.INSTANCE).isNullable());
assertEquals(expectedTypeName, LiteralUtils.literalTypeName(literal));
RelDataType type = LiteralUtils.literalType(literal, HazelcastTypeFactory.INSTANCE);
assertNotNull(type);
assertEquals(expectedTypeName, type.getSqlTypeName());
assertEquals(expectedNullable, type.isNullable());
}
use of org.apache.calcite.sql.SqlLiteral in project drill by axbaretto.
the class ExplainHandler method rewrite.
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
SqlExplain node = unwrap(sqlNode, SqlExplain.class);
SqlLiteral op = node.operand(2);
SqlExplain.Depth depth = (SqlExplain.Depth) op.getValue();
if (node.getDetailLevel() != null) {
level = node.getDetailLevel();
}
switch(depth) {
case LOGICAL:
mode = ResultMode.LOGICAL;
break;
case PHYSICAL:
mode = ResultMode.PHYSICAL;
break;
default:
throw new UnsupportedOperationException("Unknown depth " + depth);
}
return node.operand(0);
}
Aggregations