From 74e2b41b2a07aa7f9c50571876dcb7d912694afc Mon Sep 17 00:00:00 2001
From: anderson-joyle <joyle@outlook.com>
Date: Thu, 7 Mar 2024 11:26:30 -0600
Subject: [PATCH] Mark IExternalDataSource as mutable if applicable

---
 .../Microsoft.PowerFx.Core/Binding/Binder.cs          | 11 ++---------
 .../Entities/External/IExternalDataSource.cs          |  2 ++
 .../AssociatedDataSourcesTests/TestDVEntity.cs        |  2 ++
 .../Helpers/TestTabularDataSource.cs                  |  2 ++
 4 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/src/libraries/Microsoft.PowerFx.Core/Binding/Binder.cs b/src/libraries/Microsoft.PowerFx.Core/Binding/Binder.cs
index d989c66859..b874c7e294 100644
--- a/src/libraries/Microsoft.PowerFx.Core/Binding/Binder.cs
+++ b/src/libraries/Microsoft.PowerFx.Core/Binding/Binder.cs
@@ -2873,16 +2873,9 @@ public override void Visit(FirstNameNode node)
                         isConstantNamedFormula = formula.IsConstant;
                     }
                 }
-                else if (lookupInfo.Kind == BindKind.Data)
+                else if (lookupInfo.Data is IExternalDataSource ds)
                 {
-                    if (lookupInfo.Data is IExternalCdsDataSource or IExternalTabularDataSource)
-                    {
-                        _txb.SetMutable(node, true);
-                    }
-                }
-                else if (lookupInfo.Kind == BindKind.ScopeCollection)
-                {
-                    _txb.SetMutable(node, true);
+                    _txb.SetMutable(node, ds.IsWritable);
                 }
 
                 Contracts.Assert(lookupInfo.Kind != BindKind.LambdaField);
diff --git a/src/libraries/Microsoft.PowerFx.Core/Entities/External/IExternalDataSource.cs b/src/libraries/Microsoft.PowerFx.Core/Entities/External/IExternalDataSource.cs
index ed7791bb8a..fac6d91cac 100644
--- a/src/libraries/Microsoft.PowerFx.Core/Entities/External/IExternalDataSource.cs
+++ b/src/libraries/Microsoft.PowerFx.Core/Entities/External/IExternalDataSource.cs
@@ -18,6 +18,8 @@ internal interface IExternalDataSource : IExternalEntity, IExternalPageableSymbo
         bool IsRefreshable { get; }
 
         bool RequiresAsync { get; }
+        
+        bool IsWritable { get; }
 
         IExternalDataEntityMetadataProvider DataEntityMetadataProvider { get; }
 
diff --git a/src/tests/Microsoft.PowerFx.Core.Tests/AssociatedDataSourcesTests/TestDVEntity.cs b/src/tests/Microsoft.PowerFx.Core.Tests/AssociatedDataSourcesTests/TestDVEntity.cs
index 256d5b0eac..55e69e1694 100644
--- a/src/tests/Microsoft.PowerFx.Core.Tests/AssociatedDataSourcesTests/TestDVEntity.cs
+++ b/src/tests/Microsoft.PowerFx.Core.Tests/AssociatedDataSourcesTests/TestDVEntity.cs
@@ -29,6 +29,8 @@ public class AccountsEntity : IExternalEntity, IExternalDataSource
 
         public bool IsPageable => true;
 
+        public bool IsWritable => true;
+
         DType IExternalEntity.Type => AccountsTypeHelper.GetDType();
 
         IExternalDataEntityMetadataProvider IExternalDataSource.DataEntityMetadataProvider => throw new NotImplementedException();
diff --git a/src/tests/Microsoft.PowerFx.Core.Tests/Helpers/TestTabularDataSource.cs b/src/tests/Microsoft.PowerFx.Core.Tests/Helpers/TestTabularDataSource.cs
index c3a78e0a19..37e39381f2 100644
--- a/src/tests/Microsoft.PowerFx.Core.Tests/Helpers/TestTabularDataSource.cs
+++ b/src/tests/Microsoft.PowerFx.Core.Tests/Helpers/TestTabularDataSource.cs
@@ -218,6 +218,8 @@ internal TestDataSource(string name, DType schema, string[] keyColumns = null, I
 
         IDelegationMetadata IExternalDataSource.DelegationMetadata => DelegationMetadata;
 
+        public bool IsWritable => throw new NotImplementedException();
+
         public bool CanIncludeExpand(IExpandInfo expandToAdd)
         {
             throw new NotImplementedException();