/// <summary> /// Create a composite schema of both the partitioned columns and the underlying loader columns. /// </summary> /// <param name="ectx">The exception context.</param> /// <param name="cols">The partitioned columns.</param> /// <param name="subLoader">The sub loader.</param> /// <returns>The resulting schema.</returns> private Schema CreateSchema(IExceptionContext ectx, Column[] cols, IDataLoader subLoader) { Contracts.AssertValue(cols); Contracts.AssertValue(subLoader); var builder = new SchemaBuilder(); builder.AddColumns(cols.Select(c => new Schema.DetachedColumn(c.Name, PrimitiveType.FromKind(c.Type.Value), null))); var colSchema = builder.GetSchema(); var subSchema = subLoader.Schema; if (subSchema.Count == 0) { return(colSchema); } else { var schemas = new Schema[] { subSchema, colSchema }; return(Schema.Create(new CompositeSchema(schemas))); } }
public static InternalSchemaDefinition Create(Type userType, SchemaDefinition userSchemaDefinition) { Contracts.AssertValue(userType); Contracts.AssertValueOrNull(userSchemaDefinition); if (userSchemaDefinition == null) { userSchemaDefinition = SchemaDefinition.Create(userType); } Column[] dstCols = new Column[userSchemaDefinition.Count]; for (int i = 0; i < userSchemaDefinition.Count; ++i) { var col = userSchemaDefinition[i]; if (col.MemberName == null) { throw Contracts.ExceptParam(nameof(userSchemaDefinition), "Null field name detected in schema definition"); } bool isVector; DataKind kind; MemberInfo memberInfo = null; if (!col.IsComputed) { memberInfo = userType.GetField(col.MemberName); if (memberInfo == null) { memberInfo = userType.GetProperty(col.MemberName); } if (memberInfo == null) { throw Contracts.ExceptParam(nameof(userSchemaDefinition), "No field or property with name '{0}' found in type '{1}'", col.MemberName, userType.FullName); } //Clause to handle the field that may be used to expose the cursor channel. //This field does not need a column. if ((memberInfo is FieldInfo && (memberInfo as FieldInfo).FieldType == typeof(IChannel)) || (memberInfo is PropertyInfo && (memberInfo as PropertyInfo).PropertyType == typeof(IChannel))) { continue; } GetVectorAndKind(memberInfo, out isVector, out kind); } else { var parameterType = col.ReturnType; if (parameterType == null) { throw Contracts.ExceptParam(nameof(userSchemaDefinition), "No return parameter found in computed column."); } GetVectorAndKind(parameterType, "returnType", out isVector, out kind); } // Infer the column name. var colName = string.IsNullOrEmpty(col.ColumnName) ? col.MemberName : col.ColumnName; // REVIEW: Because order is defined, we allow duplicate column names, since producing an IDataView // with duplicate column names is completely legal. Possible objection is that we should make it less // convenient to produce "hidden" columns, since this may not be of practical use to users. ColumnType colType; if (col.ColumnType == null) { // Infer a type as best we can. PrimitiveType itemType = PrimitiveType.FromKind(kind); colType = isVector ? new VectorType(itemType) : (ColumnType)itemType; } else { // Make sure that the types are compatible with the declared type, including // whether it is a vector type. if (isVector != col.ColumnType.IsVector) { throw Contracts.ExceptParam(nameof(userSchemaDefinition), "Column '{0}' is supposed to be {1}, but type of associated field '{2}' is {3}", colName, col.ColumnType.IsVector ? "vector" : "scalar", col.MemberName, isVector ? "vector" : "scalar"); } if (kind != col.ColumnType.ItemType.RawKind) { throw Contracts.ExceptParam(nameof(userSchemaDefinition), "Column '{0}' is supposed to have item kind {1}, but associated field has kind {2}", colName, col.ColumnType.ItemType.RawKind, kind); } colType = col.ColumnType; } dstCols[i] = col.IsComputed ? new Column(colName, colType, col.Generator, col.Metadata) : new Column(colName, colType, memberInfo, col.Metadata); } return(new InternalSchemaDefinition(dstCols)); }