Skip to content

Make CompositeSchema not an ISchema #2001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ private Schema CreateSchema(IExceptionContext ectx, Column[] cols, IDataLoader s
colSchema
};

return Schema.Create(new CompositeSchema(schemas));
return new ZipBinding(schemas).OutputSchema;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML.Internal.Utilities;

namespace Microsoft.ML.Data
Expand All @@ -13,16 +11,16 @@ namespace Microsoft.ML.Data
/// A convenience class for concatenating several schemas together.
/// This would be necessary when combining IDataViews through any type of combining operation, for example, zip.
/// </summary>
internal sealed class CompositeSchema : ISchema
internal sealed class ZipBinding
{
private readonly Schema[] _sources;

public Schema AsSchema { get; }
public Schema OutputSchema { get; }

// Zero followed by cumulative column counts. Zero being used for the empty case.
private readonly int[] _cumulativeColCounts;

public CompositeSchema(Schema[] sources)
public ZipBinding(Schema[] sources)
{
Contracts.AssertNonEmpty(sources);
_sources = sources;
Expand All @@ -34,7 +32,11 @@ public CompositeSchema(Schema[] sources)
var schema = sources[i];
_cumulativeColCounts[i + 1] = _cumulativeColCounts[i] + schema.Count;
}
AsSchema = Schema.Create(this);

var schemaBuilder = new SchemaBuilder();
foreach (var sourceSchema in sources)
schemaBuilder.AddColumns(sourceSchema);
OutputSchema = schemaBuilder.GetSchema();
}

public int ColumnCount => _cumulativeColCounts[_cumulativeColCounts.Length - 1];
Expand Down Expand Up @@ -74,50 +76,5 @@ public void GetColumnSource(int col, out int srcIndex, out int srcCol)
srcCol = col - _cumulativeColCounts[srcIndex];
Contracts.Assert(0 <= srcCol && srcCol < _sources[srcIndex].Count);
}

public bool TryGetColumnIndex(string name, out int col)
{
for (int i = _sources.Length; --i >= 0;)
{
if (_sources[i].TryGetColumnIndex(name, out col))
{
col += _cumulativeColCounts[i];
return true;
}
}

col = -1;
return false;
}

public string GetColumnName(int col)
{
GetColumnSource(col, out int dv, out int srcCol);
return _sources[dv][srcCol].Name;
}

public ColumnType GetColumnType(int col)
{
GetColumnSource(col, out int dv, out int srcCol);
return _sources[dv][srcCol].Type;
}

public IEnumerable<KeyValuePair<string, ColumnType>> GetMetadataTypes(int col)
{
GetColumnSource(col, out int dv, out int srcCol);
return _sources[dv][srcCol].Metadata.Schema.Select(c => new KeyValuePair<string, ColumnType>(c.Name, c.Type));
}

public ColumnType GetMetadataTypeOrNull(string kind, int col)
{
GetColumnSource(col, out int dv, out int srcCol);
return _sources[dv][srcCol].Metadata.Schema.GetColumnOrNull(kind)?.Type;
}

public void GetMetadata<TValue>(string kind, int col, ref TValue value)
{
GetColumnSource(col, out int dv, out int srcCol);
_sources[dv][srcCol].Metadata.GetValue(kind, ref value);
}
}
}
20 changes: 10 additions & 10 deletions src/Microsoft.ML.Data/DataView/ZipDataView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public sealed class ZipDataView : IDataView

private readonly IHost _host;
private readonly IDataView[] _sources;
private readonly CompositeSchema _compositeSchema;
private readonly ZipBinding _zipBinding;

public static IDataView Create(IHostEnvironment env, IEnumerable<IDataView> sources)
{
Expand All @@ -47,12 +47,12 @@ private ZipDataView(IHost host, IDataView[] sources)

_host.Assert(Utils.Size(sources) > 1);
_sources = sources;
_compositeSchema = new CompositeSchema(_sources.Select(x => x.Schema).ToArray());
_zipBinding = new ZipBinding(_sources.Select(x => x.Schema).ToArray());
}

public bool CanShuffle { get { return false; } }

public Schema Schema => _compositeSchema.AsSchema;
public Schema Schema => _zipBinding.OutputSchema;

public long? GetRowCount()
{
Expand All @@ -75,7 +75,7 @@ public RowCursor GetRowCursor(Func<int, bool> predicate, Random rand = null)
_host.CheckValue(predicate, nameof(predicate));
_host.CheckValueOrNull(rand);

var srcPredicates = _compositeSchema.GetInputPredicates(predicate);
var srcPredicates = _zipBinding.GetInputPredicates(predicate);

// REVIEW: if we know the row counts, we could only open cursor if it has needed columns, and have the
// outer cursor handle the early stopping. If we don't know row counts, we need to open all the cursors because
Expand Down Expand Up @@ -106,7 +106,7 @@ public RowCursor[] GetRowCursorSet(Func<int, bool> predicate, int n, Random rand
private sealed class Cursor : RootCursorBase
{
private readonly RowCursor[] _cursors;
private readonly CompositeSchema _compositeSchema;
private readonly ZipBinding _zipBinding;
private readonly bool[] _isColumnActive;
private bool _disposed;

Expand All @@ -119,8 +119,8 @@ public Cursor(ZipDataView parent, RowCursor[] srcCursors, Func<int, bool> predic
Ch.AssertValue(predicate);

_cursors = srcCursors;
_compositeSchema = parent._compositeSchema;
_isColumnActive = Utils.BuildArray(_compositeSchema.ColumnCount, predicate);
_zipBinding = parent._zipBinding;
_isColumnActive = Utils.BuildArray(_zipBinding.ColumnCount, predicate);
}

protected override void Dispose(bool disposing)
Expand Down Expand Up @@ -172,19 +172,19 @@ protected override bool MoveManyCore(long count)
return true;
}

public override Schema Schema => _compositeSchema.AsSchema;
public override Schema Schema => _zipBinding.OutputSchema;

public override bool IsColumnActive(int col)
{
_compositeSchema.CheckColumnInRange(col);
_zipBinding.CheckColumnInRange(col);
return _isColumnActive[col];
}

public override ValueGetter<TValue> GetGetter<TValue>(int col)
{
int dv;
int srcCol;
_compositeSchema.GetColumnSource(col, out dv, out srcCol);
_zipBinding.GetColumnSource(col, out dv, out srcCol);
return _cursors[dv].GetGetter<TValue>(srcCol);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public RowMapper(IHostEnvironment env, BindableMapper parent, RoleMappedSchema s
}

_outputGenericSchema = _genericRowMapper.OutputSchema;
OutputSchema = new CompositeSchema(new Schema[] { _outputGenericSchema, _outputSchema, }).AsSchema;
OutputSchema = new ZipBinding(new Schema[] { _outputGenericSchema, _outputSchema, }).OutputSchema;
}

/// <summary>
Expand Down