Search in sources :

Example 1 with SqlSelect

use of org.apache.calcite.sql.SqlSelect in project incubator-gobblin by apache.

the class JdbcExtractor method hasJoinOperation.

/**
 * Check if the SELECT query has join operation
 */
public static boolean hasJoinOperation(String selectQuery) {
    if (selectQuery == null || selectQuery.length() == 0) {
        return false;
    }
    SqlParser sqlParser = SqlParser.create(selectQuery);
    try {
        SqlNode all = sqlParser.parseQuery();
        SqlSelect query;
        if (all instanceof SqlSelect) {
            query = (SqlSelect) all;
        } else if (all instanceof SqlOrderBy) {
            query = (SqlSelect) ((SqlOrderBy) all).query;
        } else {
            throw new UnsupportedOperationException("The select query is type of " + all.getClass() + " which is not supported here");
        }
        return query.getFrom().getKind() == SqlKind.JOIN;
    } catch (SqlParseException e) {
        return false;
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SqlParser(org.apache.calcite.sql.parser.SqlParser) SqlOrderBy(org.apache.calcite.sql.SqlOrderBy) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with SqlSelect

use of org.apache.calcite.sql.SqlSelect in project calcite by apache.

the class JdbcTable method generateSql.

SqlString generateSql() {
    final SqlNodeList selectList = new SqlNodeList(Collections.singletonList(SqlIdentifier.star(SqlParserPos.ZERO)), SqlParserPos.ZERO);
    SqlSelect node = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY, selectList, tableName(), null, null, null, null, null, null, null);
    final SqlPrettyWriter writer = new SqlPrettyWriter(jdbcSchema.dialect);
    node.unparse(writer, 0, 0);
    return writer.toSqlString();
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlNodeList(org.apache.calcite.sql.SqlNodeList)

Example 3 with SqlSelect

use of org.apache.calcite.sql.SqlSelect in project calcite by apache.

the class SqlMultisetQueryConstructor method deriveType.

public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
    SqlSelect subSelect = call.operand(0);
    subSelect.validateExpr(validator, scope);
    SqlValidatorNamespace ns = validator.getNamespace(subSelect);
    assert null != ns.getRowType();
    return SqlTypeUtil.createMultisetType(validator.getTypeFactory(), ns.getRowType(), false);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlValidatorNamespace(org.apache.calcite.sql.validate.SqlValidatorNamespace)

Example 4 with SqlSelect

use of org.apache.calcite.sql.SqlSelect in project calcite by apache.

the class SqlAbstractGroupFunction method validateCall.

@Override
public void validateCall(SqlCall call, SqlValidator validator, SqlValidatorScope scope, SqlValidatorScope operandScope) {
    super.validateCall(call, validator, scope, operandScope);
    final SelectScope selectScope = SqlValidatorUtil.getEnclosingSelectScope(scope);
    assert selectScope != null;
    final SqlSelect select = selectScope.getNode();
    if (!validator.isAggregate(select)) {
        throw validator.newValidationError(call, Static.RESOURCE.groupingInAggregate(getName()));
    }
    final AggregatingSelectScope aggregatingSelectScope = SqlValidatorUtil.getEnclosingAggregateSelectScope(scope);
    if (aggregatingSelectScope == null) {
        // We're probably in the GROUP BY clause
        throw validator.newValidationError(call, Static.RESOURCE.groupingInWrongClause(getName()));
    }
    for (SqlNode operand : call.getOperandList()) {
        if (scope instanceof OrderByScope) {
            operand = validator.expandOrderExpr(select, operand);
        } else {
            operand = validator.expand(operand, scope);
        }
        if (!aggregatingSelectScope.resolved.get().isGroupingExpr(operand)) {
            throw validator.newValidationError(operand, Static.RESOURCE.groupingArgument(getName()));
        }
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SelectScope(org.apache.calcite.sql.validate.SelectScope) AggregatingSelectScope(org.apache.calcite.sql.validate.AggregatingSelectScope) OrderByScope(org.apache.calcite.sql.validate.OrderByScope) AggregatingSelectScope(org.apache.calcite.sql.validate.AggregatingSelectScope) SqlNode(org.apache.calcite.sql.SqlNode)

Example 5 with SqlSelect

use of org.apache.calcite.sql.SqlSelect in project calcite by apache.

the class RelToSqlConverter method visit.

/**
 * @see #dispatch
 */
public Result visit(Values e) {
    final List<Clause> clauses = ImmutableList.of(Clause.SELECT);
    final Map<String, RelDataType> pairs = ImmutableMap.of();
    final Context context = aliasContext(pairs, false);
    SqlNode query;
    final boolean rename = stack.size() <= 1 || !(Iterables.get(stack, 1).r instanceof TableModify);
    final List<String> fieldNames = e.getRowType().getFieldNames();
    if (!dialect.supportsAliasedValues() && rename) {
        // Oracle does not support "AS t (c1, c2)". So instead of
        // (VALUES (v0, v1), (v2, v3)) AS t (c0, c1)
        // we generate
        // SELECT v0 AS c0, v1 AS c1 FROM DUAL
        // UNION ALL
        // SELECT v2 AS c0, v3 AS c1 FROM DUAL
        List<SqlSelect> list = new ArrayList<>();
        for (List<RexLiteral> tuple : e.getTuples()) {
            final List<SqlNode> values2 = new ArrayList<>();
            final SqlNodeList exprList = exprList(context, tuple);
            for (Pair<SqlNode, String> value : Pair.zip(exprList, fieldNames)) {
                values2.add(SqlStdOperatorTable.AS.createCall(POS, value.left, new SqlIdentifier(value.right, POS)));
            }
            list.add(new SqlSelect(POS, null, new SqlNodeList(values2, POS), new SqlIdentifier("DUAL", POS), null, null, null, null, null, null, null));
        }
        if (list.size() == 1) {
            query = list.get(0);
        } else {
            query = SqlStdOperatorTable.UNION_ALL.createCall(new SqlNodeList(list, POS));
        }
    } else {
        // Generate ANSI syntax
        // (VALUES (v0, v1), (v2, v3))
        // or, if rename is required
        // (VALUES (v0, v1), (v2, v3)) AS t (c0, c1)
        final SqlNodeList selects = new SqlNodeList(POS);
        for (List<RexLiteral> tuple : e.getTuples()) {
            selects.add(ANONYMOUS_ROW.createCall(exprList(context, tuple)));
        }
        query = SqlStdOperatorTable.VALUES.createCall(selects);
        if (rename) {
            final List<SqlNode> list = new ArrayList<>();
            list.add(query);
            list.add(new SqlIdentifier("t", POS));
            for (String fieldName : fieldNames) {
                list.add(new SqlIdentifier(fieldName, POS));
            }
            query = SqlStdOperatorTable.AS.createCall(POS, list);
        }
    }
    return result(query, clauses, e, null);
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlSelect(org.apache.calcite.sql.SqlSelect) SqlNodeList(org.apache.calcite.sql.SqlNodeList) TableModify(org.apache.calcite.rel.core.TableModify) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlSelect (org.apache.calcite.sql.SqlSelect)62 SqlNode (org.apache.calcite.sql.SqlNode)45 SqlNodeList (org.apache.calcite.sql.SqlNodeList)29 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)20 SqlCall (org.apache.calcite.sql.SqlCall)17 RelDataType (org.apache.calcite.rel.type.RelDataType)15 BitString (org.apache.calcite.util.BitString)12 ArrayList (java.util.ArrayList)9 SqlUpdate (org.apache.calcite.sql.SqlUpdate)8 SqlInsert (org.apache.calcite.sql.SqlInsert)7 SchemaPlus (org.apache.calcite.schema.SchemaPlus)6 SqlMerge (org.apache.calcite.sql.SqlMerge)6 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)5 SqlDelete (org.apache.calcite.sql.SqlDelete)5 SqlJoin (org.apache.calcite.sql.SqlJoin)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 SqlOrderBy (org.apache.calcite.sql.SqlOrderBy)4 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)4 RelOptTable (org.apache.calcite.plan.RelOptTable)3 RelNode (org.apache.calcite.rel.RelNode)3