use of org.apache.calcite.sql.SqlNodeList in project calcite by apache.
the class SqlValidatorUtil method analyzeGroupExpr.
/**
* Analyzes a component of a tuple in a GROUPING SETS clause.
*/
private static ImmutableBitSet analyzeGroupExpr(SqlValidatorScope scope, GroupAnalyzer groupAnalyzer, SqlNode groupExpr) {
final SqlNode expandedGroupExpr = scope.getValidator().expand(groupExpr, scope);
switch(expandedGroupExpr.getKind()) {
case ROW:
return ImmutableBitSet.union(analyzeGroupTuple(scope, groupAnalyzer, ((SqlCall) expandedGroupExpr).getOperandList()));
case OTHER:
if (expandedGroupExpr instanceof SqlNodeList && ((SqlNodeList) expandedGroupExpr).size() == 0) {
return ImmutableBitSet.of();
}
}
final int ref = lookupGroupExpr(groupAnalyzer, groupExpr);
if (expandedGroupExpr instanceof SqlIdentifier) {
// SQL 2003 does not allow expressions of column references
SqlIdentifier expr = (SqlIdentifier) expandedGroupExpr;
// column references should be fully qualified.
assert expr.names.size() == 2;
String originalRelName = expr.names.get(0);
String originalFieldName = expr.names.get(1);
final SqlNameMatcher nameMatcher = scope.getValidator().getCatalogReader().nameMatcher();
final SqlValidatorScope.ResolvedImpl resolved = new SqlValidatorScope.ResolvedImpl();
scope.resolve(ImmutableList.of(originalRelName), nameMatcher, false, resolved);
assert resolved.count() == 1;
final SqlValidatorScope.Resolve resolve = resolved.only();
final RelDataType rowType = resolve.rowType();
final int childNamespaceIndex = resolve.path.steps().get(0).i;
int namespaceOffset = 0;
if (childNamespaceIndex > 0) {
// If not the first child, need to figure out the width of
// output types from all the preceding namespaces
final SqlValidatorScope ancestorScope = resolve.scope;
assert ancestorScope instanceof ListScope;
List<SqlValidatorNamespace> children = ((ListScope) ancestorScope).getChildren();
for (int j = 0; j < childNamespaceIndex; j++) {
namespaceOffset += children.get(j).getRowType().getFieldCount();
}
}
RelDataTypeField field = nameMatcher.field(rowType, originalFieldName);
int origPos = namespaceOffset + field.getIndex();
groupAnalyzer.groupExprProjection.put(origPos, ref);
}
return ImmutableBitSet.of(ref);
}
use of org.apache.calcite.sql.SqlNodeList in project calcite by apache.
the class SelectScope method lookupWindow.
public SqlWindow lookupWindow(String name) {
final SqlNodeList windowList = select.getWindowList();
for (int i = 0; i < windowList.size(); i++) {
SqlWindow window = (SqlWindow) windowList.get(i);
final SqlIdentifier declId = window.getDeclName();
assert declId.isSimple();
if (declId.names.get(0).equals(name)) {
return window;
}
}
// if not in the select scope, then check window scope
if (windowParent != null) {
return windowParent.lookupWindow(name);
} else {
return null;
}
}
use of org.apache.calcite.sql.SqlNodeList 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.SqlNodeList in project calcite by apache.
the class AggregatingSelectScope method resolve.
// ~ Methods ----------------------------------------------------------------
private Resolved resolve() {
final ImmutableList.Builder<ImmutableList<ImmutableBitSet>> builder = ImmutableList.builder();
List<SqlNode> extraExprs = ImmutableList.of();
Map<Integer, Integer> groupExprProjection = ImmutableMap.of();
if (select.getGroup() != null) {
final SqlNodeList groupList = select.getGroup();
final SqlValidatorUtil.GroupAnalyzer groupAnalyzer = new SqlValidatorUtil.GroupAnalyzer(temporaryGroupExprList);
for (SqlNode groupExpr : groupList) {
SqlValidatorUtil.analyzeGroupItem(this, groupAnalyzer, builder, groupExpr);
}
extraExprs = groupAnalyzer.extraExprs;
groupExprProjection = groupAnalyzer.groupExprProjection;
}
final Set<ImmutableBitSet> flatGroupSets = Sets.newTreeSet(ImmutableBitSet.COMPARATOR);
for (List<ImmutableBitSet> groupSet : Linq4j.product(builder.build())) {
flatGroupSets.add(ImmutableBitSet.union(groupSet));
}
// For GROUP BY (), we need a singleton grouping set.
if (flatGroupSets.isEmpty()) {
flatGroupSets.add(ImmutableBitSet.of());
}
return new Resolved(extraExprs, temporaryGroupExprList, flatGroupSets, groupExprProjection);
}
use of org.apache.calcite.sql.SqlNodeList in project calcite by apache.
the class AliasNamespace method validateImpl.
// ~ Methods ----------------------------------------------------------------
protected RelDataType validateImpl(RelDataType targetRowType) {
final List<String> nameList = new ArrayList<String>();
final List<SqlNode> operands = call.getOperandList();
final SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
final RelDataType rowType = childNs.getRowTypeSansSystemColumns();
final List<SqlNode> columnNames = Util.skip(operands, 2);
for (final SqlNode operand : columnNames) {
String name = ((SqlIdentifier) operand).getSimple();
if (nameList.contains(name)) {
throw validator.newValidationError(operand, RESOURCE.aliasListDuplicate(name));
}
nameList.add(name);
}
if (nameList.size() != rowType.getFieldCount()) {
// Position error over all column names
final SqlNode node = operands.size() == 3 ? operands.get(2) : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
throw validator.newValidationError(node, RESOURCE.aliasListDegree(rowType.getFieldCount(), getString(rowType), nameList.size()));
}
final List<RelDataType> typeList = new ArrayList<RelDataType>();
for (RelDataTypeField field : rowType.getFieldList()) {
typeList.add(field.getType());
}
return validator.getTypeFactory().createStructType(typeList, nameList);
}
Aggregations