Search in sources :

Example 46 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project flink by apache.

the class HiveParserUtils method getSqlOperator.

public static SqlOperator getSqlOperator(String funcName, SqlOperatorTable opTable, SqlFunctionCategory category) {
    funcName = funcName.toLowerCase();
    String[] names = funcName.split("\\.");
    SqlIdentifier identifier = new SqlIdentifier(Arrays.asList(names), SqlParserPos.ZERO);
    List<SqlOperator> operators = new ArrayList<>();
    try {
        opTable.lookupOperatorOverloads(identifier, category, SqlSyntax.FUNCTION, operators, SqlNameMatchers.withCaseSensitive(false));
    } catch (Exception e) {
        LOG.warn("Error trying to resolve function " + funcName, e);
    }
    if (operators.isEmpty()) {
        return null;
    } else {
        return operators.get(0);
    }
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) NlsString(org.apache.calcite.util.NlsString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException) FlinkHiveException(org.apache.flink.connectors.hive.FlinkHiveException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException)

Example 47 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project flink by apache.

the class RichSqlHiveInsert method getPartKeyToSpec.

private static Map<SqlIdentifier, SqlProperty> getPartKeyToSpec(SqlNodeList staticSpec) {
    Map<SqlIdentifier, SqlProperty> res = new HashMap<>();
    if (staticSpec != null) {
        for (SqlNode node : staticSpec) {
            SqlProperty spec = (SqlProperty) node;
            res.put(spec.getKey(), spec);
        }
    }
    return res;
}
Also used : SqlProperty(org.apache.flink.sql.parser.SqlProperty) HashMap(java.util.HashMap) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 48 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project flink by apache.

the class SqlValidatorImpl method findAllValidFunctionNames.

private static void findAllValidFunctionNames(List<String> names, SqlValidator validator, Collection<SqlMoniker> result, SqlParserPos pos) {
    // a function name can only be 1 part
    if (names.size() > 1) {
        return;
    }
    for (SqlOperator op : validator.getOperatorTable().getOperatorList()) {
        SqlIdentifier curOpId = new SqlIdentifier(op.getName(), pos);
        final SqlCall call = validator.makeNullaryCall(curOpId);
        if (call != null) {
            result.add(new SqlMonikerImpl(op.getName(), SqlMonikerType.FUNCTION));
        } else {
            if ((op.getSyntax() == SqlSyntax.FUNCTION) || (op.getSyntax() == SqlSyntax.PREFIX)) {
                if (op.getOperandTypeChecker() != null) {
                    String sig = op.getAllowedSignatures();
                    sig = sig.replace("'", "");
                    result.add(new SqlMonikerImpl(sig, SqlMonikerType.FUNCTION));
                    continue;
                }
                result.add(new SqlMonikerImpl(op.getName(), SqlMonikerType.FUNCTION));
            }
        }
    }
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 49 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project flink by apache.

the class SqlValidatorImpl method expandSelectItem.

/**
 * If <code>selectItem</code> is "*" or "TABLE.*", expands it and returns true; otherwise writes
 * the unexpanded item.
 *
 * @param selectItem Select-list item
 * @param select Containing select clause
 * @param selectItems List that expanded items are written to
 * @param aliases Set of aliases
 * @param fields List of field names and types, in alias order
 * @param includeSystemVars If true include system vars in lists
 * @return Whether the node was expanded
 */
private boolean expandSelectItem(final SqlNode selectItem, SqlSelect select, RelDataType targetType, List<SqlNode> selectItems, Set<String> aliases, List<Map.Entry<String, RelDataType>> fields, final boolean includeSystemVars) {
    final SelectScope scope = (SelectScope) getWhereScope(select);
    if (expandStar(selectItems, aliases, fields, includeSystemVars, scope, selectItem)) {
        return true;
    }
    // Expand the select item: fully-qualify columns, and convert
    // parentheses-free functions such as LOCALTIME into explicit function
    // calls.
    SqlNode expanded = expandSelectExpr(selectItem, scope, select);
    final String alias = deriveAlias(selectItem, aliases.size());
    // If expansion has altered the natural alias, supply an explicit 'AS'.
    final SqlValidatorScope selectScope = getSelectScope(select);
    if (expanded != selectItem) {
        String newAlias = deriveAlias(expanded, aliases.size());
        if (!newAlias.equals(alias)) {
            expanded = SqlStdOperatorTable.AS.createCall(selectItem.getParserPosition(), expanded, new SqlIdentifier(alias, SqlParserPos.ZERO));
            deriveTypeImpl(selectScope, expanded);
        }
    }
    selectItems.add(expanded);
    aliases.add(alias);
    if (expanded != null) {
        inferUnknownTypes(targetType, scope, expanded);
    }
    final RelDataType type = deriveType(selectScope, expanded);
    setValidatedNodeType(expanded, type);
    fields.add(Pair.of(alias, type));
    return false;
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 50 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project flink by apache.

the class SqlValidatorImpl method checkRollUp.

private void checkRollUp(SqlNode grandParent, SqlNode parent, SqlNode current, SqlValidatorScope scope, String optionalClause) {
    current = stripAs(current);
    if (current instanceof SqlCall && !(current instanceof SqlSelect)) {
        // Validate OVER separately
        checkRollUpInWindow(getWindowInOver(current), scope);
        current = stripOver(current);
        List<SqlNode> children = ((SqlCall) stripAs(stripDot(current))).getOperandList();
        for (SqlNode child : children) {
            checkRollUp(parent, current, child, scope, optionalClause);
        }
    } else if (current instanceof SqlIdentifier) {
        SqlIdentifier id = (SqlIdentifier) current;
        if (!id.isStar() && isRolledUpColumn(id, scope)) {
            if (!isAggregation(parent.getKind()) || !isRolledUpColumnAllowedInAgg(id, scope, (SqlCall) parent, grandParent)) {
                String context = optionalClause != null ? optionalClause : parent.getKind().toString();
                throw newValidationError(id, RESOURCE.rolledUpNotAllowed(deriveAlias(id, 0), context));
            }
        }
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlCall(org.apache.calcite.sql.SqlCall) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)131 SqlNode (org.apache.calcite.sql.SqlNode)84 RelDataType (org.apache.calcite.rel.type.RelDataType)46 SqlNodeList (org.apache.calcite.sql.SqlNodeList)43 ArrayList (java.util.ArrayList)41 SqlCall (org.apache.calcite.sql.SqlCall)32 BitString (org.apache.calcite.util.BitString)28 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)21 SqlSelect (org.apache.calcite.sql.SqlSelect)21 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)12 SchemaPlus (org.apache.calcite.schema.SchemaPlus)11 SqlOperator (org.apache.calcite.sql.SqlOperator)11 NlsString (org.apache.calcite.util.NlsString)11 List (java.util.List)9 Map (java.util.Map)9 RelOptTable (org.apache.calcite.plan.RelOptTable)9 RexNode (org.apache.calcite.rex.RexNode)9 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)9 ImmutableList (com.google.common.collect.ImmutableList)8 RelNode (org.apache.calcite.rel.RelNode)7