Skip to content
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

Add a warning to the CountRows function if a data source that caches its count is passed to it #2640

Merged
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 @@ -36,6 +36,8 @@ public ExternalCdpDataSource(DName name, string datasetName, ServiceCapabilities

public TabularDataQueryOptions QueryOptions => new TabularDataQueryOptions(this);

public bool HasCachedCountRows => false;

public string Name => EntityName.Value;

public bool IsSelectable => ServiceCapabilities.IsSelectable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ internal interface IExternalTabularDataSource : IExternalDataSource, IDisplayMap
{
TabularDataQueryOptions QueryOptions { get; }

/// <summary>
/// Some data sources (like Dataverse) may return a cached value for
/// the number of rows (calls to CountRows) instead of always retrieving
/// the latest count.
/// </summary>
bool HasCachedCountRows { get; }
anderson-joyle marked this conversation as resolved.
Show resolved Hide resolved

IReadOnlyList<string> GetKeyColumns();

IEnumerable<string> GetKeyColumns(IExpandInfo expandInfo);
Expand All @@ -23,4 +30,4 @@ internal interface IExternalTabularDataSource : IExternalDataSource, IDisplayMap

bool CanIncludeExpand(IExpandInfo parentExpandInfo, IExpandInfo expandToAdd);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,8 @@ internal static class TexlStrings
public static ErrorResourceKey WarnDeferredType = new ErrorResourceKey("WarnDeferredType");
public static ErrorResourceKey ErrColRenamedTwice_Name = new ErrorResourceKey("ErrColRenamedTwice_Name");

public static ErrorResourceKey WrnCountRowsMayReturnCachedValue = new ErrorResourceKey("WrnCountRowsMayReturnCachedValue");

public static StringGetter InfoMessage = (b) => StringResources.Get("InfoMessage", b);
public static StringGetter InfoNode_Node = (b) => StringResources.Get("InfoNode_Node", b);
public static StringGetter InfoTok_Tok = (b) => StringResources.Get("InfoTok_Tok", b);
Expand Down
27 changes: 27 additions & 0 deletions src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/CountRows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Microsoft.PowerFx.Core.App.ErrorContainers;
using Microsoft.PowerFx.Core.Binding;
using Microsoft.PowerFx.Core.Binding.BindInfo;
using Microsoft.PowerFx.Core.Entities;
using Microsoft.PowerFx.Core.Errors;
using Microsoft.PowerFx.Core.Functions;
Expand Down Expand Up @@ -68,6 +69,32 @@ public override bool IsServerDelegatable(CallNode callNode, TexlBinding binding)
return TryGetValidDataSourceForDelegation(callNode, binding, out var dataSource, out var preferredFunctionDelegationCapability);
}

public override void CheckSemantics(TexlBinding binding, TexlNode[] args, DType[] argTypes, IErrorContainer errors)
{
base.CheckSemantics(binding, args, argTypes, errors);
if (args[0] is not FirstNameNode node)
{
// No additional check
return;
}

var info = binding.GetInfo(args[0] as FirstNameNode);
if (info.Kind != BindKind.Data)
{
// No additional check
return;
}

if (argTypes[0].AssociatedDataSources?.Count == 1)
{
var associatedDataSource = argTypes[0].AssociatedDataSources.Single();
if (associatedDataSource.HasCachedCountRows)
{
errors.EnsureError(DocumentErrorSeverity.Warning, node, TexlStrings.WrnCountRowsMayReturnCachedValue);
}
}
}

// See if CountDistinct delegation is available. If true, we can make use of it on primary key as a workaround for CountRows delegation
internal bool TryGetValidDataSourceForDelegation(CallNode callNode, TexlBinding binding, out IExternalDataSource dataSource, out DelegationCapability preferredFunctionDelegationCapability)
{
Expand Down
4 changes: 4 additions & 0 deletions src/strings/PowerFxResources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4332,6 +4332,10 @@
<value>Can't delegate {0}: contains a behavior function '{1}'.</value>
<comment>Warning message.</comment>
</data>
<data name="WrnCountRowsMayReturnCachedValue" xml:space="preserve">
<value>CountRows may return a cached value. Use CountIf(DataSource, true) to get the latest count.</value>
<comment>{Locked=CountRows}. Warning message when an expression with the CountRows function is used with a data source that caches its size.</comment>
</data>
<data name="AboutIsMatch" xml:space="preserve">
<value>Determines if the supplied text has a match of the supplied text format.</value>
<comment>Description of 'IsMatch' function.</comment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace Microsoft.PowerFx.Core.Tests.AssociatedDataSourcesTests
{
public class AccountsEntity : IExternalEntity, IExternalDataSource
{
private readonly bool _hasCachedCountRows;

public AccountsEntity(bool hasCachedCountRows = false)
{
this._hasCachedCountRows = hasCachedCountRows;
}

public DName EntityName => new DName("Accounts");

public string Name => "Accounts";
Expand All @@ -33,7 +40,7 @@ public class AccountsEntity : IExternalEntity, IExternalDataSource

public bool IsClearable => true;

DType IExternalEntity.Type => AccountsTypeHelper.GetDType();
DType IExternalEntity.Type => AccountsTypeHelper.GetDType(this._hasCachedCountRows);

IExternalDataEntityMetadataProvider IExternalDataSource.DataEntityMetadataProvider => throw new NotImplementedException();

Expand All @@ -54,14 +61,15 @@ internal static class AccountsTypeHelper
"name`Account Name`:s, numberofemployees:n, primarytwitterid:s, stockexchange:s, telephone1:s, telephone2:s, telephone3:s, tickersymbol:s, versionnumber:n, " +
"websiteurl:h, nonsearchablestringcol`Non-searchable string column`:s, nonsortablestringcolumn`Non-sortable string column`:s]";

public static DType GetDType()
public static DType GetDType(bool hasCachedCountRows = false)
{
DType accountsType = TestUtils.DT2(SimplifiedAccountsSchema);
var dataSource = new TestDataSource(
"Accounts",
accountsType,
keyColumns: new[] { "accountid" },
selectableColumns: new[] { "name", "address1_city", "accountid", "address1_country", "address1_line1" });
selectableColumns: new[] { "name", "address1_city", "accountid", "address1_country", "address1_line1" },
hasCachedCountRows: hasCachedCountRows);
var displayNameMapping = dataSource.DisplayNameMapping;
displayNameMapping.Add("name", "Account Name");
displayNameMapping.Add("address1_city", "Address 1: City");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Text;
using Microsoft.PowerFx.Core.Entities.QueryOptions;
using Microsoft.PowerFx.Core.Errors;
using Microsoft.PowerFx.Core.Tests.Helpers;
using Microsoft.PowerFx.Core.Texl;
using Microsoft.PowerFx.Types;
Expand Down Expand Up @@ -100,5 +101,38 @@ private void TestDelegableExpressions(Features features, string expression, bool
// validate we can generate the display expression
string displayExpr = engine.GetDisplayExpression(expression, symbolTable);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void TestCountRowsWarningForCachedData(bool isCachedData)
{
var symbolTable = new DelegatableSymbolTable();
symbolTable.AddEntity(new AccountsEntity(isCachedData));
var config = new PowerFxConfig(Features.PowerFxV1)
{
SymbolTable = symbolTable
};

var engine = new Engine(config);
var result = engine.Check("CountRows(Accounts)");
Assert.True(result.IsSuccess);

if (!isCachedData)
{
Assert.Empty(result.Errors);
}
else
{
Assert.Single(result.Errors);
var error = result.Errors.Single();
Assert.Equal(ErrorSeverity.Warning, error.Severity);
}

// Only shows warning if data source is passed directly to CountRows
result = engine.Check("CountRows(Filter(Accounts, IsBlank('Address 1: City')))");
Assert.True(result.IsSuccess);
Assert.Empty(result.Errors);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ internal class TestDataSource : IExternalDataSource, IExternalTabularDataSource
private readonly string[] _keyColumns;
private readonly HashSet<string> _selectableColumns;
private readonly TabularDataQueryOptions _tabularDataQueryOptions;
private readonly bool _hasCachedCountRows;

internal TestDataSource(string name, DType schema, string[] keyColumns = null, IEnumerable<string> selectableColumns = null)
internal TestDataSource(string name, DType schema, string[] keyColumns = null, IEnumerable<string> selectableColumns = null, bool hasCachedCountRows = false)
{
ExternalDataEntityMetadataProvider = new ExternalDataEntityMetadataProvider();
Type = DType.AttachDataSourceInfo(schema, this);
Expand All @@ -185,10 +186,13 @@ internal TestDataSource(string name, DType schema, string[] keyColumns = null, I
_keyColumns = keyColumns ?? Array.Empty<string>();
_selectableColumns = new HashSet<string>(selectableColumns ?? Enumerable.Empty<string>());
_tabularDataQueryOptions = new TabularDataQueryOptions(this);
_hasCachedCountRows = hasCachedCountRows;
}

public string Name { get; }

public bool HasCachedCountRows => this._hasCachedCountRows;

public virtual bool IsSelectable => true;

public virtual bool IsDelegatable => throw new NotImplementedException();
Expand Down