Search in sources :

Example 16 with SqlLiteral

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()));
}
Also used : NlsString(org.apache.calcite.util.NlsString) NlsString(org.apache.calcite.util.NlsString) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Example 17 with SqlLiteral

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);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 18 with SqlLiteral

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()));
}
Also used : ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) ExplicitOperatorBinding(org.apache.calcite.sql.ExplicitOperatorBinding) ComparableOperandTypeChecker(org.apache.calcite.sql.type.ComparableOperandTypeChecker) HazelcastCallBinding(com.hazelcast.jet.sql.impl.validate.HazelcastCallBinding) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 19 with SqlLiteral

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());
}
Also used : SqlLiteral(org.apache.calcite.sql.SqlLiteral) Literal(com.hazelcast.jet.sql.impl.validate.literal.Literal) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 20 with SqlLiteral

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);
}
Also used : SqlExplain(org.apache.calcite.sql.SqlExplain) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Aggregations

SqlLiteral (org.apache.calcite.sql.SqlLiteral)42 SqlNode (org.apache.calcite.sql.SqlNode)19 RelDataType (org.apache.calcite.rel.type.RelDataType)12 SqlCall (org.apache.calcite.sql.SqlCall)8 SqlNodeList (org.apache.calcite.sql.SqlNodeList)7 RexNode (org.apache.calcite.rex.RexNode)6 ArrayList (java.util.ArrayList)5 TimeUnitRange (org.apache.calcite.avatica.util.TimeUnitRange)5 NlsString (org.apache.calcite.util.NlsString)5 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)4 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)4 SqlMatchRecognize (org.apache.calcite.sql.SqlMatchRecognize)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)3 RexLiteral (org.apache.calcite.rex.RexLiteral)3 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)3 BitString (org.apache.calcite.util.BitString)3 HashMap (java.util.HashMap)2