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;
}
}
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);
}
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);
}
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;
}
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);
}
Aggregations