Skip to content

Commit

Permalink
Move HttpClient to CdpTableValue and update ADS hack (#2489)
Browse files Browse the repository at this point in the history
HttpClient is now passed via InitAsync function and no more required in
RuntimeConfig
Removed tabular IR rewriter and related logic
Updated ADS hack with a new approach - ADS will be update in another PR
Tested live with no side effect
  • Loading branch information
LucGenetier committed Jun 19, 2024
1 parent 6bcbe7d commit dceb84b
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 393 deletions.
68 changes: 0 additions & 68 deletions src/libraries/Microsoft.PowerFx.Connectors/CdpIRVisitor.cs

This file was deleted.

38 changes: 0 additions & 38 deletions src/libraries/Microsoft.PowerFx.Connectors/EngineExtensions.cs

This file was deleted.

This file was deleted.

37 changes: 28 additions & 9 deletions src/libraries/Microsoft.PowerFx.Connectors/Public/CdpTableValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerFx.Core.Entities;
using Microsoft.PowerFx.Core.Entities;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Types;

Expand All @@ -21,33 +22,51 @@ public class CdpTableValue : TableValue, IRefreshable, IDelegatableTableValue

protected internal readonly ConnectorType _connectorType;

private IReadOnlyCollection<DValue<RecordValue>> _cachedRows;

internal readonly HttpClient HttpClient;

public RecordType TabularRecordType => _tabularService?.TabularRecordType;

public CdpTableValue(CdpService tabularService, ConnectorType connectorType)
: base(IRContext.NotInSource(new CdpTableType(tabularService.TableType)))
{
_tabularService = tabularService;
_connectorType = connectorType;
_connectorType = connectorType;

HttpClient = tabularService.HttpClient;
}

internal CdpTableValue(IRContext irContext)
: base(irContext)
{
_cachedRows = null;
}

public override IEnumerable<DValue<RecordValue>> Rows => throw new InvalidOperationException("No service context. Make sure to call engine.EnableTabularConnectors().");

public virtual void Refresh()
{
}
public override IEnumerable<DValue<RecordValue>> Rows => GetRowsAsync(null, null, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();

public async Task<IReadOnlyCollection<DValue<RecordValue>>> GetRowsAsync(IServiceProvider services, DelegationParameters parameters, CancellationToken cancel)
{
var op = parameters.ToOdataParameters();
if (parameters == null && _cachedRows != null)
{
return _cachedRows;
}

var op = parameters?.ToOdataParameters();
var rows = await _tabularService.GetItemsAsync(services, op, cancel).ConfigureAwait(false);

if (parameters == null)
{
_cachedRows = rows;
}

return rows;
}
}

public void Refresh()
{
_cachedRows = null;
}
}

internal static class ODataParametersExtensions
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerFx.Types;
Expand All @@ -23,6 +24,8 @@ public abstract class CdpService : CdpServiceBase

public abstract ConnectorType ConnectorType { get; }

public abstract HttpClient HttpClient { get; }

public virtual CdpTableValue GetTableValue()
{
return IsInitialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public sealed class CdpTable : CdpService

public string DatasetName { get; private set; }

public override HttpClient HttpClient => _httpClient;

public override bool IsDelegable => TableCapabilities?.IsDelegable ?? false;

public override ConnectorType ConnectorType => TabularTableDescriptor.ConnectorType;
Expand All @@ -34,6 +36,8 @@ public sealed class CdpTable : CdpService

private string _uriPrefix;

private HttpClient _httpClient;

public CdpTable(string dataset, string table)
{
DatasetName = dataset ?? throw new ArgumentNullException(nameof(dataset));
Expand All @@ -57,6 +61,8 @@ public async Task InitAsync(HttpClient httpClient, string uriPrefix, Cancellatio
throw new InvalidOperationException("TabularService already initialized");
}

_httpClient = httpClient;

// $$$ This is a hack to generate ADS
bool adsHack = false;
if (uriPrefix.StartsWith("*", StringComparison.Ordinal))
Expand Down Expand Up @@ -99,16 +105,15 @@ protected override async Task<IReadOnlyCollection<DValue<RecordValue>>> GetItems
{
cancellationToken.ThrowIfCancellationRequested();
ConnectorLogger executionLogger = serviceProvider?.GetService<ConnectorLogger>();
HttpClient httpClient = serviceProvider?.GetService<HttpClient>() ?? throw new InvalidOperationException("HttpClient is required on IServiceProvider");


string queryParams = (odataParameters != null) ? "&" + odataParameters.ToQueryString() : string.Empty;

Uri uri = new Uri(
(_uriPrefix ?? string.Empty) +
(_uriPrefix.Contains("/sql/") ? "/v2" : string.Empty) +
$"/datasets/{(DatasetMetadata.IsDoubleEncoding ? DoubleEncode(DatasetName) : DatasetName)}/tables/{HttpUtility.UrlEncode(TableName)}/items?api-version=2015-09-01" + queryParams, UriKind.Relative);

string text = await GetObject(httpClient, $"List items ({nameof(GetItemsInternalAsync)})", uri.ToString(), cancellationToken, executionLogger).ConfigureAwait(false);
string text = await GetObject(_httpClient, $"List items ({nameof(GetItemsInternalAsync)})", uri.ToString(), cancellationToken, executionLogger).ConfigureAwait(false);
return !string.IsNullOrWhiteSpace(text) ? GetResult(text) : Array.Empty<DValue<RecordValue>>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerFx.Types;
Expand Down Expand Up @@ -46,23 +46,15 @@ public async Task FileTabularTest()
PowerFxConfig config = new PowerFxConfig(Features.PowerFxV1);
RecalcEngine engine = new RecalcEngine(config);

#pragma warning disable CS0618 // Type or member is obsolete
engine.EnableTabularConnectors();
#pragma warning restore CS0618 // Type or member is obsolete

SymbolValues symbolValues = new SymbolValues().Add("File", fileTable);

// Expression with tabular connector
string expr = @"Last(FirstN(File, 2)).line";

CheckResult check = engine.Check(expr, options: new ParserOptions() { AllowsSideEffects = true }, symbolTable: symbolValues.SymbolTable);
Assert.True(check.IsSuccess);

// Confirm that InjectServiceProviderFunction has properly been added
string ir = new Regex("RuntimeValues_[0-9]+").Replace(check.PrintIR(), "RuntimeValues_XXX");
Assert.Equal("FieldAccess(Last:![line:s](FirstN:*[line:s](InjectServiceProviderFunction:*[line:s](ResolvedObject('File:RuntimeValues_XXX')), Float:n(2:w))), line)", ir);

// Use tabular connector. Internally we'll call ConnectorTableValueWithServiceProvider.GetRowsInternal to get the data

// Use tabular connector. Internally we'll call CdpTableValue.GetRowsInternal to get the data
FormulaValue result = await check.GetEvaluator().EvalAsync(CancellationToken.None, symbolValues);
StringValue str = Assert.IsType<StringValue>(result);
Assert.Equal("b", str.Value);
Expand All @@ -85,6 +77,9 @@ public FileTabularService(string fileName)

public override ConnectorType ConnectorType => null;

// No need for files
public override HttpClient HttpClient => null;

// Initialization can be synchronous
public void Init()
{
Expand Down
Loading

0 comments on commit dceb84b

Please sign in to comment.