Search in sources :

Example 11 with SqlLiteral

use of org.apache.calcite.sql.SqlLiteral in project drill by apache.

the class SetOptionHandler method getPlan.

/**
 * Handles {@link DrillSqlSetOption} query
 */
@Override
public final PhysicalPlan getPlan(SqlNode sqlNode) throws ForemanSetupException {
    // sqlNode could contain DrillSqlResetOption or DrillSqlSetOption, depends on parsed statement
    SqlSetOption statement = unwrap(sqlNode, SqlSetOption.class);
    OptionScope optionScope = getScope(statement, context.getOptions());
    OptionManager optionManager = context.getOptions().getOptionManager(optionScope);
    String optionName = statement.getName().toString();
    SqlNode optionValue = statement.getValue();
    if (optionValue == null) {
        // OptionManager.getOptionDefinition() call ensures that the specified option name is valid
        OptionDefinition optionDefinition = optionManager.getOptionDefinition(optionName);
        String value = String.valueOf(optionManager.getOption(optionName).getValue());
        // obtains option name from OptionDefinition to use the name as defined in the option, rather than what the user provided
        return DirectPlan.createDirectPlan(context, new SetOptionViewResult(optionDefinition.getValidator().getOptionName(), value));
    } else {
        if (optionScope == OptionValue.OptionScope.SYSTEM) {
            checkAdminPrivileges(context.getOptions());
        }
        if (!(optionValue instanceof SqlLiteral)) {
            throw UserException.validationError().message("Drill does not support assigning non-literal values in SET statements.").build(logger);
        }
        optionManager.setLocalOption(optionName, sqlLiteralToObject((SqlLiteral) optionValue));
        return DirectPlan.createDirectPlan(context, true, String.format("%s updated.", optionName));
    }
}
Also used : OptionScope(org.apache.drill.exec.server.options.OptionValue.OptionScope) DrillSqlSetOption(org.apache.drill.exec.planner.sql.parser.DrillSqlSetOption) SqlSetOption(org.apache.calcite.sql.SqlSetOption) NlsString(org.apache.calcite.util.NlsString) OptionDefinition(org.apache.drill.exec.server.options.OptionDefinition) SqlLiteral(org.apache.calcite.sql.SqlLiteral) OptionManager(org.apache.drill.exec.server.options.OptionManager) SqlNode(org.apache.calcite.sql.SqlNode)

Example 12 with SqlLiteral

use of org.apache.calcite.sql.SqlLiteral in project drill by apache.

the class DropAliasHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ForemanSetupException, IOException {
    checkAliasesEnabled();
    SqlDropAlias node = unwrap(sqlNode, SqlDropAlias.class);
    String alias = SchemaPath.getCompoundPath(node.getAlias().names.toArray(new String[0])).toExpr();
    String aliasTarget = ((SqlLiteral) node.getAliasKind()).toValue();
    AliasRegistry aliasRegistry = getAliasRegistry(aliasTarget);
    Aliases aliases = getAliases(node, aliasRegistry);
    boolean checkIfExists = ((SqlLiteral) node.getIfExists()).booleanValue();
    boolean removed = aliases.remove(alias);
    if (!removed && !checkIfExists) {
        throw UserException.validationError().message("No alias found with given name [%s]", alias).build(logger);
    }
    String message = removed ? String.format("%s alias '%s' dropped successfully", StringUtils.capitalize(aliasTarget.toLowerCase(Locale.ROOT)), alias) : String.format("No %s alias found with given name [%s]", aliasTarget.toLowerCase(Locale.ROOT), alias);
    return DirectPlan.createDirectPlan(context, removed, message);
}
Also used : SqlDropAlias(org.apache.drill.exec.planner.sql.parser.SqlDropAlias) AliasRegistry(org.apache.drill.exec.alias.AliasRegistry) Aliases(org.apache.drill.exec.alias.Aliases) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Example 13 with SqlLiteral

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

the class SqlCastFunction method inferReturnType.

// ~ Methods ----------------------------------------------------------------
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    assert opBinding.getOperandCount() == 2;
    RelDataType ret = opBinding.getOperandType(1);
    RelDataType firstType = opBinding.getOperandType(0);
    ret = opBinding.getTypeFactory().createTypeWithNullability(ret, firstType.isNullable());
    if (opBinding instanceof SqlCallBinding) {
        SqlCallBinding callBinding = (SqlCallBinding) opBinding;
        SqlNode operand0 = callBinding.operand(0);
        // to them using the type they are casted to.
        if (((operand0 instanceof SqlLiteral) && (((SqlLiteral) operand0).getValue() == null)) || (operand0 instanceof SqlDynamicParam)) {
            final SqlValidatorImpl validator = (SqlValidatorImpl) callBinding.getValidator();
            validator.setValidatedNodeType(operand0, ret);
        }
    }
    return ret;
}
Also used : SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 14 with SqlLiteral

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

the class FlinkCalciteSqlValidator method validateJoin.

@Override
protected void validateJoin(SqlJoin join, SqlValidatorScope scope) {
    // temporarily forbid the common predicates until the problem is fixed (see FLINK-7865).
    if (join.getJoinType() == JoinType.LEFT && SqlUtil.stripAs(join.getRight()).getKind() == SqlKind.COLLECTION_TABLE) {
        SqlNode right = SqlUtil.stripAs(join.getRight());
        if (right instanceof SqlBasicCall) {
            SqlBasicCall call = (SqlBasicCall) right;
            SqlNode operand0 = call.operand(0);
            if (operand0 instanceof SqlBasicCall && ((SqlBasicCall) operand0).getOperator() instanceof SqlWindowTableFunction) {
                return;
            }
        }
        final SqlNode condition = join.getCondition();
        if (condition != null && (!SqlUtil.isLiteral(condition) || ((SqlLiteral) condition).getValueAs(Boolean.class) != Boolean.TRUE)) {
            throw new ValidationException(String.format("Left outer joins with a table function do not accept a predicate such as %s. " + "Only literal TRUE is accepted.", condition));
        }
    }
    super.validateJoin(join, scope);
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlWindowTableFunction(org.apache.calcite.sql.SqlWindowTableFunction) SqlNode(org.apache.calcite.sql.SqlNode)

Example 15 with SqlLiteral

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

the class LiteralUtils method literal.

public static Literal literal(SqlNode node) {
    if (node.getKind() != SqlKind.LITERAL) {
        // Not a literal
        return null;
    }
    SqlLiteral literal = (SqlLiteral) node;
    SqlTypeName typeName = literal.getTypeName();
    Object value = CHAR_TYPES.contains(typeName) ? literal.toValue() : literal.getValue();
    return literal0(typeName, value);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) 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