Search in sources :

Example 11 with SqlSelect

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

the class SqlValidatorImpl method getFieldOrigin.

private List<String> getFieldOrigin(SqlNode sqlQuery, int i) {
    if (sqlQuery instanceof SqlSelect) {
        SqlSelect sqlSelect = (SqlSelect) sqlQuery;
        final SelectScope scope = getRawSelectScope(sqlSelect);
        final List<SqlNode> selectList = scope.getExpandedSelectList();
        final SqlNode selectItem = stripAs(selectList.get(i));
        if (selectItem instanceof SqlIdentifier) {
            final SqlQualified qualified = scope.fullyQualify((SqlIdentifier) selectItem);
            SqlValidatorNamespace namespace = qualified.namespace;
            final SqlValidatorTable table = namespace.getTable();
            if (table == null) {
                return null;
            }
            final List<String> origin = new ArrayList<>(table.getQualifiedName());
            for (String name : qualified.suffix()) {
                namespace = namespace.lookupChild(name);
                if (namespace == null) {
                    return null;
                }
                origin.add(name);
            }
            return origin;
        }
        return null;
    } else if (sqlQuery instanceof SqlOrderBy) {
        return getFieldOrigin(((SqlOrderBy) sqlQuery).query, i);
    } else {
        return null;
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) ArrayList(java.util.ArrayList) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlOrderBy(org.apache.calcite.sql.SqlOrderBy) SqlNode(org.apache.calcite.sql.SqlNode)

Example 12 with SqlSelect

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

the class SqlToRelConverter method convertToSingleValueSubq.

/**
 * Converts the RelNode tree for a select statement to a select that
 * produces a single value.
 *
 * @param query the query
 * @param plan   the original RelNode tree corresponding to the statement
 * @return the converted RelNode tree
 */
public RelNode convertToSingleValueSubq(SqlNode query, RelNode plan) {
    // Check whether query is guaranteed to produce a single value.
    if (query instanceof SqlSelect) {
        SqlSelect select = (SqlSelect) query;
        SqlNodeList selectList = select.getSelectList();
        SqlNodeList groupList = select.getGroup();
        if ((selectList.size() == 1) && ((groupList == null) || (groupList.size() == 0))) {
            SqlNode selectExpr = selectList.get(0);
            if (selectExpr instanceof SqlCall) {
                SqlCall selectExprCall = (SqlCall) selectExpr;
                if (Util.isSingleValue(selectExprCall)) {
                    return plan;
                }
            }
            // it is ensured to produce a single value
            if (select.getFetch() != null && select.getFetch() instanceof SqlNumericLiteral) {
                SqlNumericLiteral limitNum = (SqlNumericLiteral) select.getFetch();
                if (((BigDecimal) limitNum.getValue()).intValue() < 2) {
                    return plan;
                }
            }
        }
    } else if (query instanceof SqlCall) {
        // If the query is (values ...),
        // it is necessary to look into the operands to determine
        // whether SingleValueAgg is necessary
        SqlCall exprCall = (SqlCall) query;
        if (exprCall.getOperator() instanceof SqlValuesOperator && Util.isSingleValue(exprCall)) {
            return plan;
        }
    }
    // If not, project SingleValueAgg
    return RelOptUtil.createSingleValueAggRel(cluster, plan);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlValuesOperator(org.apache.calcite.sql.SqlValuesOperator) SqlCall(org.apache.calcite.sql.SqlCall) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNumericLiteral(org.apache.calcite.sql.SqlNumericLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 13 with SqlSelect

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

the class SqlDdlNodes method renameColumns.

/**
 * Wraps a query to rename its columns. Used by CREATE VIEW and CREATE
 * MATERIALIZED VIEW.
 */
static SqlNode renameColumns(SqlNodeList columnList, SqlNode query) {
    if (columnList == null) {
        return query;
    }
    final SqlParserPos p = query.getParserPosition();
    final SqlNodeList selectList = new SqlNodeList(ImmutableList.<SqlNode>of(SqlIdentifier.star(p)), p);
    final SqlCall from = SqlStdOperatorTable.AS.createCall(p, ImmutableList.<SqlNode>builder().add(query).add(new SqlIdentifier("_", p)).addAll(columnList).build());
    return new SqlSelect(p, null, selectList, from, null, null, null, null, null, null, null);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlCall(org.apache.calcite.sql.SqlCall) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 14 with SqlSelect

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

the class SetopOperandTypeChecker method checkOperandTypes.

public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
    assert callBinding.getOperandCount() == 2 : "setops are binary (for now)";
    final RelDataType[] argTypes = new RelDataType[callBinding.getOperandCount()];
    int colCount = -1;
    final SqlValidator validator = callBinding.getValidator();
    for (int i = 0; i < argTypes.length; i++) {
        final RelDataType argType = argTypes[i] = callBinding.getOperandType(i);
        if (!argType.isStruct()) {
            if (throwOnFailure) {
                throw new AssertionError("setop arg must be a struct");
            } else {
                return false;
            }
        }
        // Each operand must have the same number of columns.
        final List<RelDataTypeField> fields = argType.getFieldList();
        if (i == 0) {
            colCount = fields.size();
            continue;
        }
        if (fields.size() != colCount) {
            if (throwOnFailure) {
                SqlNode node = callBinding.operand(i);
                if (node instanceof SqlSelect) {
                    node = ((SqlSelect) node).getSelectList();
                }
                throw validator.newValidationError(node, RESOURCE.columnCountMismatchInSetop(callBinding.getOperator().getName()));
            } else {
                return false;
            }
        }
    }
    // column j.
    for (int i = 0; i < colCount; i++) {
        final int i2 = i;
        final RelDataType type = callBinding.getTypeFactory().leastRestrictive(new AbstractList<RelDataType>() {

            public RelDataType get(int index) {
                return argTypes[index].getFieldList().get(i2).getType();
            }

            public int size() {
                return argTypes.length;
            }
        });
        if (type == null) {
            if (throwOnFailure) {
                SqlNode field = SqlUtil.getSelectListItem(callBinding.operand(0), i);
                throw validator.newValidationError(field, // 1-based
                RESOURCE.columnTypeMismatchInSetop(// 1-based
                i + 1, callBinding.getOperator().getName()));
            } else {
                return false;
            }
        }
    }
    return true;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlSelect(org.apache.calcite.sql.SqlSelect) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 15 with SqlSelect

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

the class ShowSchemasHandler method rewrite.

/**
 * Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.SCHEMATA ...
 */
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
    SqlShowSchemas node = unwrap(sqlNode, SqlShowSchemas.class);
    List<SqlNode> selectList = ImmutableList.of((SqlNode) new SqlIdentifier(SCHS_COL_SCHEMA_NAME, SqlParserPos.ZERO));
    SqlNode fromClause = new SqlIdentifier(ImmutableList.of(IS_SCHEMA_NAME, TAB_SCHEMATA), null, SqlParserPos.ZERO, null);
    SqlNode where = null;
    final SqlNode likePattern = node.getLikePattern();
    if (likePattern != null) {
        where = DrillParserUtil.createCondition(new SqlIdentifier(SCHS_COL_SCHEMA_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.LIKE, likePattern);
    } else if (node.getWhereClause() != null) {
        where = node.getWhereClause();
    }
    return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO), fromClause, where, null, null, null, null, null, null);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlShowSchemas(org.apache.drill.exec.planner.sql.parser.SqlShowSchemas) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) 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