use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class SqlValidatorUtil method convertGroupSet.
/**
* Analyzes a GROUPING SETS item in a GROUP BY clause.
*/
private static void convertGroupSet(SqlValidatorScope scope, GroupAnalyzer groupAnalyzer, ImmutableList.Builder<ImmutableBitSet> builder, SqlNode groupExpr) {
switch(groupExpr.getKind()) {
case GROUPING_SETS:
final SqlCall call = (SqlCall) groupExpr;
for (SqlNode node : call.getOperandList()) {
convertGroupSet(scope, groupAnalyzer, builder, node);
}
return;
case ROW:
final List<ImmutableBitSet> bitSets = analyzeGroupTuple(scope, groupAnalyzer, ((SqlCall) groupExpr).getOperandList());
builder.add(ImmutableBitSet.union(bitSets));
return;
case ROLLUP:
case CUBE:
{
// GROUPING SETS ( (a), ROLLUP(c,b), CUBE(d,e) )
// is EQUIVALENT to
// GROUPING SETS ( (a), (c,b), (b) ,(), (d,e), (d), (e) ).
// Expand all ROLLUP/CUBE nodes
List<ImmutableBitSet> operandBitSet = analyzeGroupTuple(scope, groupAnalyzer, ((SqlCall) groupExpr).getOperandList());
switch(groupExpr.getKind()) {
case ROLLUP:
builder.addAll(rollup(operandBitSet));
return;
default:
builder.addAll(cube(operandBitSet));
return;
}
}
default:
builder.add(analyzeGroupExpr(scope, groupAnalyzer, groupExpr));
return;
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode 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.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class TableNamespace method checkExtendedColumnTypes.
/**
* Ensures that extended columns that have the same name as a base column also
* have the same data-type.
*/
private void checkExtendedColumnTypes(SqlNodeList extendList) {
final List<RelDataTypeField> extendedFields = SqlValidatorUtil.getExtendedColumns(validator.getTypeFactory(), table, extendList);
final List<RelDataTypeField> baseFields = getBaseRowType().getFieldList();
final Map<String, Integer> nameToIndex = SqlValidatorUtil.mapNameToIndex(baseFields);
for (final RelDataTypeField extendedField : extendedFields) {
final String extFieldName = extendedField.getName();
if (nameToIndex.containsKey(extFieldName)) {
final Integer baseIndex = nameToIndex.get(extFieldName);
final RelDataType baseType = baseFields.get(baseIndex).getType();
final RelDataType extType = extendedField.getType();
if (!extType.equals(baseType)) {
// Get the extended column node that failed validation.
final Predicate<SqlNode> nameMatches = new PredicateImpl<SqlNode>() {
@Override
public boolean test(SqlNode sqlNode) {
if (sqlNode instanceof SqlIdentifier) {
final SqlIdentifier identifier = (SqlIdentifier) sqlNode;
return Util.last(identifier.names).equals(extendedField.getName());
}
return false;
}
};
final SqlNode extColNode = Iterables.find(extendList.getList(), nameMatches);
throw validator.getValidationErrorFunction().apply(extColNode, RESOURCE.typeNotAssignable(baseFields.get(baseIndex).getName(), baseType.getFullTypeString(), extendedField.getName(), extType.getFullTypeString()));
}
}
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class SelectNamespace method getMonotonicity.
public SqlMonotonicity getMonotonicity(String columnName) {
final RelDataType rowType = this.getRowTypeSansSystemColumns();
final int field = SqlTypeUtil.findField(rowType, columnName);
final SqlNode selectItem = validator.getRawSelectScope(select).getExpandedSelectList().get(field);
return validator.getSelectScope(select).getMonotonicity(selectItem);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class SqlCreateMaterializedView method unparse.
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("CREATE");
writer.keyword("MATERIALIZED VIEW");
if (ifNotExists) {
writer.keyword("IF NOT EXISTS");
}
name.unparse(writer, leftPrec, rightPrec);
if (columnList != null) {
SqlWriter.Frame frame = writer.startList("(", ")");
for (SqlNode c : columnList) {
writer.sep(",");
c.unparse(writer, 0, 0);
}
writer.endList(frame);
}
writer.keyword("AS");
writer.newlineAndIndent();
query.unparse(writer, 0, 0);
}
Aggregations