Search in sources :

Example 46 with SqlCall

use of org.apache.calcite.sql.SqlCall in project flink by apache.

the class SqlValidatorImpl method getNamespace.

private SqlValidatorNamespace getNamespace(SqlNode node, SqlValidatorScope scope) {
    if (node instanceof SqlIdentifier && scope instanceof DelegatingScope) {
        final SqlIdentifier id = (SqlIdentifier) node;
        final DelegatingScope idScope = (DelegatingScope) ((DelegatingScope) scope).getParent();
        return getNamespace(id, idScope);
    } else if (node instanceof SqlCall) {
        // Handle extended identifiers.
        final SqlCall call = (SqlCall) node;
        switch(call.getOperator().getKind()) {
            case TABLE_REF:
                return getNamespace(call.operand(0), scope);
            case EXTEND:
                final SqlNode operand0 = call.getOperandList().get(0);
                final SqlIdentifier identifier = operand0.getKind() == SqlKind.TABLE_REF ? ((SqlCall) operand0).operand(0) : (SqlIdentifier) operand0;
                final DelegatingScope idScope = (DelegatingScope) scope;
                return getNamespace(identifier, idScope);
            case AS:
                final SqlNode nested = call.getOperandList().get(0);
                switch(nested.getKind()) {
                    case TABLE_REF:
                    case EXTEND:
                        return getNamespace(nested, scope);
                }
                break;
        }
    }
    return getNamespace(node);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 47 with SqlCall

use of org.apache.calcite.sql.SqlCall in project flink by apache.

the class SqlValidatorUtils method adjustTypeForMultisetConstructor.

/**
 * When the element element does not equal with the component type, making explicit casting.
 *
 * @param evenType derived type for element with even index
 * @param oddType derived type for element with odd index
 * @param sqlCallBinding description of call
 */
private static void adjustTypeForMultisetConstructor(RelDataType evenType, RelDataType oddType, SqlCallBinding sqlCallBinding) {
    SqlCall call = sqlCallBinding.getCall();
    List<RelDataType> operandTypes = sqlCallBinding.collectOperandTypes();
    List<SqlNode> operands = call.getOperandList();
    RelDataType elementType;
    for (int i = 0; i < operands.size(); i++) {
        if (i % 2 == 0) {
            elementType = evenType;
        } else {
            elementType = oddType;
        }
        if (operandTypes.get(i).equalsSansFieldNames(elementType)) {
            continue;
        }
        call.setOperand(i, castTo(operands.get(i), elementType));
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 48 with SqlCall

use of org.apache.calcite.sql.SqlCall in project druid by druid-io.

the class DruidSqlParserUtils method convertSqlNodeToGranularity.

/**
 * This method is used to extract the granularity from a SqlNode representing following function calls:
 * 1. FLOOR(__time TO TimeUnit)
 * 2. TIME_FLOOR(__time, 'PT1H')
 *
 * Validation on the sqlNode is contingent to following conditions:
 * 1. sqlNode is an instance of SqlCall
 * 2. Operator is either one of TIME_FLOOR or FLOOR
 * 3. Number of operands in the call are 2
 * 4. First operand is a SimpleIdentifier representing __time
 * 5. If operator is TIME_FLOOR, the second argument is a literal, and can be converted to the Granularity class
 * 6. If operator is FLOOR, the second argument is a TimeUnit, and can be mapped using {@link TimeUnits}
 *
 * Since it is to be used primarily while parsing the SqlNode, it is wrapped in {@code convertSqlNodeToGranularityThrowingParseExceptions}
 *
 * @param sqlNode SqlNode representing a call to a function
 * @return Granularity as intended by the function call
 * @throws ParseException SqlNode cannot be converted a granularity
 */
public static Granularity convertSqlNodeToGranularity(SqlNode sqlNode) throws ParseException {
    final String genericParseFailedMessageFormatString = "Encountered %s after PARTITIONED BY. " + "Expected HOUR, DAY, MONTH, YEAR, ALL TIME, FLOOR function or %s function";
    if (!(sqlNode instanceof SqlCall)) {
        throw new ParseException(StringUtils.format(genericParseFailedMessageFormatString, sqlNode.toString(), TimeFloorOperatorConversion.SQL_FUNCTION_NAME));
    }
    SqlCall sqlCall = (SqlCall) sqlNode;
    String operatorName = sqlCall.getOperator().getName();
    Preconditions.checkArgument("FLOOR".equalsIgnoreCase(operatorName) || TimeFloorOperatorConversion.SQL_FUNCTION_NAME.equalsIgnoreCase(operatorName), StringUtils.format("PARTITIONED BY clause only supports FLOOR(__time TO <unit> and %s(__time, period) functions", TimeFloorOperatorConversion.SQL_FUNCTION_NAME));
    List<SqlNode> operandList = sqlCall.getOperandList();
    Preconditions.checkArgument(operandList.size() == 2, StringUtils.format("%s in PARTITIONED BY clause must have two arguments", operatorName));
    // Check if the first argument passed in the floor function is __time
    SqlNode timeOperandSqlNode = operandList.get(0);
    Preconditions.checkArgument(timeOperandSqlNode.getKind().equals(SqlKind.IDENTIFIER), StringUtils.format("First argument to %s in PARTITIONED BY clause can only be __time", operatorName));
    SqlIdentifier timeOperandSqlIdentifier = (SqlIdentifier) timeOperandSqlNode;
    Preconditions.checkArgument(timeOperandSqlIdentifier.getSimple().equals(ColumnHolder.TIME_COLUMN_NAME), StringUtils.format("First argument to %s in PARTITIONED BY clause can only be __time", operatorName));
    // If the floor function is of form TIME_FLOOR(__time, 'PT1H')
    if (operatorName.equalsIgnoreCase(TimeFloorOperatorConversion.SQL_FUNCTION_NAME)) {
        SqlNode granularitySqlNode = operandList.get(1);
        Preconditions.checkArgument(granularitySqlNode.getKind().equals(SqlKind.LITERAL), "Second argument to TIME_FLOOR in PARTITIONED BY clause must be a period like 'PT1H'");
        String granularityString = SqlLiteral.unchain(granularitySqlNode).toValue();
        Period period;
        try {
            period = new Period(granularityString);
        } catch (IllegalArgumentException e) {
            throw new ParseException(StringUtils.format("%s is an invalid period string", granularitySqlNode.toString()));
        }
        return new PeriodGranularity(period, null, null);
    } else if ("FLOOR".equalsIgnoreCase(operatorName)) {
        // If the floor function is of form FLOOR(__time TO DAY)
        SqlNode granularitySqlNode = operandList.get(1);
        // In future versions of Calcite, this can be checked via
        // granularitySqlNode.getKind().equals(SqlKind.INTERVAL_QUALIFIER)
        Preconditions.checkArgument(granularitySqlNode instanceof SqlIntervalQualifier, "Second argument to the FLOOR function in PARTITIONED BY clause is not a valid granularity. " + "Please refer to the documentation of FLOOR function");
        SqlIntervalQualifier granularityIntervalQualifier = (SqlIntervalQualifier) granularitySqlNode;
        Period period = TimeUnits.toPeriod(granularityIntervalQualifier.timeUnitRange);
        Preconditions.checkNotNull(period, StringUtils.format("%s is not a valid granularity for ingestion", granularityIntervalQualifier.timeUnitRange.toString()));
        return new PeriodGranularity(period, null, null);
    }
    // Shouldn't reach here
    throw new ParseException(StringUtils.format(genericParseFailedMessageFormatString, sqlNode.toString(), TimeFloorOperatorConversion.SQL_FUNCTION_NAME));
}
Also used : SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) SqlCall(org.apache.calcite.sql.SqlCall) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) Period(org.joda.time.Period) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 49 with SqlCall

use of org.apache.calcite.sql.SqlCall in project hazelcast by hazelcast.

the class HazelcastCallBinding method newValidationSignatureError.

@Override
public CalciteException newValidationSignatureError() {
    SqlOperator operator = getOperator();
    SqlValidator validator = getValidator();
    SqlCall call = getCall();
    String operandTypes = getOperandTypes(validator, call, getScope());
    Resources.ExInst<SqlValidatorException> error;
    String operatorName = '\'' + operator.getName() + '\'';
    switch(operator.getSyntax()) {
        case FUNCTION:
        case FUNCTION_STAR:
        case FUNCTION_ID:
            error = RESOURCES.invalidFunctionOperands(operatorName, operandTypes);
            break;
        default:
            error = RESOURCES.invalidOperatorOperands(operatorName, operandTypes);
    }
    return validator.newValidationError(call, error);
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) Resources(org.apache.calcite.runtime.Resources) SqlValidatorException(org.apache.calcite.sql.validate.SqlValidatorException)

Example 50 with SqlCall

use of org.apache.calcite.sql.SqlCall in project hazelcast by hazelcast.

the class AbstractOperandChecker method check.

@Override
public boolean check(HazelcastCallBinding callBinding, boolean throwOnFailure, int operandIndex) {
    HazelcastSqlValidator validator = callBinding.getValidator();
    SqlNode operand = callBinding.getCall().operand(operandIndex);
    if (operand.getKind() == SqlKind.DYNAMIC_PARAM) {
        validateDynamicParam((SqlDynamicParam) operand, validator);
        return true;
    }
    if (operand.getKind() == SqlKind.ARGUMENT_ASSIGNMENT) {
        SqlNode value = ((SqlCall) operand).operand(0);
        if (value.getKind() == SqlKind.DYNAMIC_PARAM) {
            validateDynamicParam((SqlDynamicParam) value, validator);
            return true;
        }
    }
    RelDataType operandType = validator.deriveType(callBinding.getScope(), operand);
    assert operandType.getSqlTypeName() != SqlTypeName.NULL : "Operand type is not resolved";
    // Handle type match
    if (matchesTargetType(operandType)) {
        return true;
    }
    // Handle coercion if possible
    if (coerce(validator, callBinding, operand, operandType, operandIndex)) {
        return true;
    }
    // Failed
    if (throwOnFailure) {
        throw callBinding.newValidationSignatureError();
    } else {
        return false;
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlCall (org.apache.calcite.sql.SqlCall)108 SqlNode (org.apache.calcite.sql.SqlNode)81 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)32 SqlNodeList (org.apache.calcite.sql.SqlNodeList)32 RelDataType (org.apache.calcite.rel.type.RelDataType)30 SqlOperator (org.apache.calcite.sql.SqlOperator)26 BitString (org.apache.calcite.util.BitString)19 ArrayList (java.util.ArrayList)18 SqlSelect (org.apache.calcite.sql.SqlSelect)17 RexNode (org.apache.calcite.rex.RexNode)13 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)12 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)11 SqlLiteral (org.apache.calcite.sql.SqlLiteral)11 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 SqlKind (org.apache.calcite.sql.SqlKind)9 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)9 List (java.util.List)8 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)8 Pair (org.apache.calcite.util.Pair)8 RelNode (org.apache.calcite.rel.RelNode)7