use of org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class SqlValidatorUtil method addFields.
private static void addFields(List<RelDataTypeField> fieldList, List<RelDataType> typeList, List<String> nameList, Set<String> uniqueNames) {
for (RelDataTypeField field : fieldList) {
String name = field.getName();
// Ensure that name is unique from all previous field names
if (uniqueNames.contains(name)) {
String nameBase = name;
for (int j = 0; ; j++) {
name = nameBase + j;
if (!uniqueNames.contains(name)) {
break;
}
}
}
nameList.add(name);
uniqueNames.add(name);
typeList.add(field.getType());
}
}
use of org.apache.calcite.rel.type.RelDataTypeField 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.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class SqlCreateTable method execute.
public void execute(CalcitePrepare.Context context) {
final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
final JavaTypeFactory typeFactory = new JavaTypeFactoryImpl();
final RelDataType queryRowType;
if (query != null) {
// A bit of a hack: pretend it's a view, to get its row type
final String sql = query.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
final ViewTableMacro viewTableMacro = ViewTable.viewMacro(pair.left.plus(), sql, pair.left.path(null), context.getObjectPath(), false);
final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
queryRowType = x.getRowType(typeFactory);
if (columnList != null && queryRowType.getFieldCount() != columnList.size()) {
throw SqlUtil.newContextException(columnList.getParserPosition(), RESOURCE.columnCountMismatch());
}
} else {
queryRowType = null;
}
final List<SqlNode> columnList;
if (this.columnList != null) {
columnList = this.columnList.getList();
} else {
if (queryRowType == null) {
// a list of column names and types, "CREATE TABLE t (INT c)".
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.createTableRequiresColumnList());
}
columnList = new ArrayList<>();
for (String name : queryRowType.getFieldNames()) {
columnList.add(new SqlIdentifier(name, SqlParserPos.ZERO));
}
}
final ImmutableList.Builder<ColumnDef> b = ImmutableList.builder();
final RelDataTypeFactory.Builder builder = typeFactory.builder();
final RelDataTypeFactory.Builder storedBuilder = typeFactory.builder();
for (Ord<SqlNode> c : Ord.zip(columnList)) {
if (c.e instanceof SqlColumnDeclaration) {
final SqlColumnDeclaration d = (SqlColumnDeclaration) c.e;
final RelDataType type = d.dataType.deriveType(typeFactory, true);
builder.add(d.name.getSimple(), type);
if (d.strategy != ColumnStrategy.VIRTUAL) {
storedBuilder.add(d.name.getSimple(), type);
}
b.add(ColumnDef.of(d.expression, type, d.strategy));
} else if (c.e instanceof SqlIdentifier) {
final SqlIdentifier id = (SqlIdentifier) c.e;
if (queryRowType == null) {
throw SqlUtil.newContextException(id.getParserPosition(), RESOURCE.createTableRequiresColumnTypes(id.getSimple()));
}
final RelDataTypeField f = queryRowType.getFieldList().get(c.i);
final ColumnStrategy strategy = f.getType().isNullable() ? ColumnStrategy.NULLABLE : ColumnStrategy.NOT_NULLABLE;
b.add(ColumnDef.of(c.e, f.getType(), strategy));
builder.add(id.getSimple(), f.getType());
storedBuilder.add(id.getSimple(), f.getType());
} else {
throw new AssertionError(c.e.getClass());
}
}
final RelDataType rowType = builder.build();
final RelDataType storedRowType = storedBuilder.build();
final List<ColumnDef> columns = b.build();
final InitializerExpressionFactory ief = new NullInitializerExpressionFactory() {
@Override
public ColumnStrategy generationStrategy(RelOptTable table, int iColumn) {
return columns.get(iColumn).strategy;
}
@Override
public RexNode newColumnDefaultValue(RelOptTable table, int iColumn, InitializerContext context) {
final ColumnDef c = columns.get(iColumn);
if (c.expr != null) {
return context.convertExpression(c.expr);
}
return super.newColumnDefaultValue(table, iColumn, context);
}
};
if (pair.left.plus().getTable(pair.right) != null) {
// Table exists.
if (!ifNotExists) {
// They did not specify IF NOT EXISTS, so give error.
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.tableExists(pair.right));
}
return;
}
// Table does not exist. Create it.
pair.left.add(pair.right, new MutableArrayTable(pair.right, RelDataTypeImpl.proto(storedRowType), RelDataTypeImpl.proto(rowType), ief));
if (query != null) {
SqlDdlNodes.populate(name, query, context);
}
}
use of org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class Handler method handle.
/**
* Creates relational expressions for a given AST node.
*/
public Handler handle(Ast.Node node) {
final RelNode input;
final List<RexNode> rexNodes;
switch(node.op) {
case LOAD:
final Ast.LoadStmt load = (Ast.LoadStmt) node;
builder.scan((String) load.name.value);
register(load.target.value);
return this;
case VALUES:
final Ast.ValuesStmt values = (Ast.ValuesStmt) node;
final RelDataType rowType = toType(values.schema);
builder.values(tuples(values, rowType), rowType);
register(values.target.value);
return this;
case FOREACH:
final Ast.ForeachStmt foreach = (Ast.ForeachStmt) node;
builder.clear();
input = map.get(foreach.source.value);
builder.push(input);
rexNodes = new ArrayList<>();
for (Ast.Node exp : foreach.expList) {
rexNodes.add(toRex(exp));
}
builder.project(rexNodes);
register(foreach.target.value);
return this;
case FOREACH_NESTED:
final Ast.ForeachNestedStmt foreachNested = (Ast.ForeachNestedStmt) node;
builder.clear();
input = map.get(foreachNested.source.value);
builder.push(input);
System.out.println(input.getRowType());
for (RelDataTypeField field : input.getRowType().getFieldList()) {
switch(field.getType().getSqlTypeName()) {
case ARRAY:
System.out.println(field);
}
}
for (Ast.Stmt stmt : foreachNested.nestedStmtList) {
handle(stmt);
}
rexNodes = new ArrayList<>();
for (Ast.Node exp : foreachNested.expList) {
rexNodes.add(toRex(exp));
}
builder.project(rexNodes);
register(foreachNested.target.value);
return this;
case FILTER:
final Ast.FilterStmt filter = (Ast.FilterStmt) node;
builder.clear();
input = map.get(filter.source.value);
builder.push(input);
final RexNode rexNode = toRex(filter.condition);
builder.filter(rexNode);
register(filter.target.value);
return this;
case DISTINCT:
final Ast.DistinctStmt distinct = (Ast.DistinctStmt) node;
builder.clear();
input = map.get(distinct.source.value);
builder.push(input);
builder.distinct(null, -1);
register(distinct.target.value);
return this;
case ORDER:
final Ast.OrderStmt order = (Ast.OrderStmt) node;
builder.clear();
input = map.get(order.source.value);
builder.push(input);
final List<RexNode> nodes = new ArrayList<>();
for (Pair<Ast.Identifier, Ast.Direction> field : order.fields) {
toSortRex(nodes, field);
}
builder.sort(nodes);
register(order.target.value);
return this;
case LIMIT:
final Ast.LimitStmt limit = (Ast.LimitStmt) node;
builder.clear();
input = map.get(limit.source.value);
final int count = ((Number) limit.count.value).intValue();
builder.push(input);
builder.limit(0, count);
register(limit.target.value);
return this;
case GROUP:
final Ast.GroupStmt group = (Ast.GroupStmt) node;
builder.clear();
input = map.get(group.source.value);
builder.push(input).as(group.source.value);
final List<RelBuilder.GroupKey> groupKeys = new ArrayList<>();
final List<RexNode> keys = new ArrayList<>();
if (group.keys != null) {
for (Ast.Node key : group.keys) {
keys.add(toRex(key));
}
}
groupKeys.add(builder.groupKey(keys));
builder.group(PigRelBuilder.GroupOption.COLLECTED, null, -1, groupKeys);
register(group.target.value);
return this;
case PROGRAM:
final Ast.Program program = (Ast.Program) node;
for (Ast.Stmt stmt : program.stmtList) {
handle(stmt);
}
return this;
case DUMP:
final Ast.DumpStmt dump = (Ast.DumpStmt) node;
final RelNode relNode = map.get(dump.relation.value);
dump(relNode);
// nothing to do; contains no algebra
return this;
default:
throw new AssertionError("unknown operation " + node.op);
}
}
use of org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class MongoSort method implement.
public void implement(Implementor implementor) {
implementor.visitChild(0, getInput());
if (!collation.getFieldCollations().isEmpty()) {
final List<String> keys = new ArrayList<String>();
final List<RelDataTypeField> fields = getRowType().getFieldList();
for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
final String name = fields.get(fieldCollation.getFieldIndex()).getName();
keys.add(name + ": " + direction(fieldCollation));
if (false) {
// TODO: NULLS FIRST and NULLS LAST
switch(fieldCollation.nullDirection) {
case FIRST:
break;
case LAST:
break;
}
}
}
implementor.add(null, "{$sort: " + Util.toString(keys, "{", ", ", "}") + "}");
}
if (offset != null) {
implementor.add(null, "{$skip: " + ((RexLiteral) offset).getValue() + "}");
}
if (fetch != null) {
implementor.add(null, "{$limit: " + ((RexLiteral) fetch).getValue() + "}");
}
}
Aggregations