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

FIX: Grouping with lookup was failing #2046

Merged
merged 5 commits into from Oct 16, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -999,12 +999,6 @@ internal void SelectParameterDeclarationsSetSql(StringBuilder result, Hashtable
var rowLimit = selectParameters.RowLimit;
var rowOffset = selectParameters.RowOffset;
bool rowOffsetSpecified = rowOffset.HasValue && rowOffset != 0;
bool hasLookupField = selectParameters.Entity.EntityDefinition
.EntityColumns
.Cast<ISchemaItem>()
.OfType<LookupField>()
.Any(field =>
selectParameters.ColumnsInfo.ColumnNames.Contains(field.Name));

if (!(entity.EntityDefinition is TableMappingItem))
{
Expand Down Expand Up @@ -1259,7 +1253,7 @@ internal void SelectParameterDeclarationsSetSql(StringBuilder result, Hashtable
finalString += $" OFFSET {rowOffset} ROWS FETCH NEXT {rowLimit} ROWS ONLY;";
}

if (hasLookupField && customGrouping != null)
if (customGrouping != null && GroupingUsesLookup(customGrouping, entity))
{
var columnNames = selectParameters.ColumnsInfo.ColumnNames;
if (selectParameters.AggregatedColumns.Count > 0)
Expand All @@ -1279,6 +1273,27 @@ internal void SelectParameterDeclarationsSetSql(StringBuilder result, Hashtable
return finalString;
}

private static bool GroupingUsesLookup(Grouping customGrouping, DataStructureEntity entity)
{
var allLookupColumnNames = entity
.ChildrenRecursive.OfType<DataStructureEntity>()
.Concat(new[] { entity })
.SelectMany(entity =>
{
var dataStructureColumnNames = entity.ChildItems.ToGeneric()
.OfType<DataStructureColumn>()
.Where(x => x.UseLookupValue)
.Select(x => x.Name);
var entityColumnNames = entity.EntityDefinition.EntityColumns
.OfType<LookupField>()
.Select(lookupField => lookupField.Name);
return dataStructureColumnNames.Concat(entityColumnNames);
});

return customGrouping != null &&
allLookupColumnNames.Contains(customGrouping.GroupBy);
}

private void PostProcessCustomCommandParserWhereClause(
Hashtable replaceParameterTexts,
Hashtable selectParameterReferences,
Expand Down Expand Up @@ -1814,6 +1829,8 @@ internal bool ShouldUpdateColumn(DataStructureColumn column, DataStructureEntity
forceDatabaseCalculation: forceDatabaseCalculation);
}

private record GroupByData(DataStructureColumn Column, string Expression);

internal bool RenderSelectColumns(SelectParameters selectParameters,
StringBuilder sqlExpression,
StringBuilder orderByBuilder, StringBuilder groupByBuilder,
Expand All @@ -1833,7 +1850,7 @@ internal bool ShouldUpdateColumn(DataStructureColumn column, DataStructureEntity
var dynamicParameters = selectParameters.Parameters;
var customFilters = selectParameters.CustomFilters;

DataStructureColumn groupByColumn = null;
GroupByData groupByData = null;
int i = 0;
List<string> group = new List<string>();
SortedList<int, SortOrder> order = new SortedList<int, SortOrder>();
Expand Down Expand Up @@ -1863,10 +1880,6 @@ internal bool ShouldUpdateColumn(DataStructureColumn column, DataStructureEntity
GetSortedColumns(entity, columnsInfo?.ColumnNames, aggregatedColumns);
foreach (DataStructureColumn column in dataStructureColumns)
{
if (customGrouping != null && column.Name == customGrouping.GroupBy)
{
groupByColumn = column;
}
LookupOrderingInfo customOrderingInfo =
LookupOrderingInfo.TryCreate(customOrderings.Orderings, column.Name );
string groupByExpression = "";
Expand All @@ -1877,6 +1890,10 @@ internal bool ShouldUpdateColumn(DataStructureColumn column, DataStructureEntity
columnsInfo ?? ColumnsInfo.Empty, column,
customOrderingInfo, filterCommandParser, orderByCommandParser,
selectParameters.RowOffset);
if (customGrouping != null && column.Name == customGrouping.GroupBy)
{
groupByData = new GroupByData(column, groupByExpression);
}
string expression;
if (columnRenderData != null)
{
Expand Down Expand Up @@ -1978,20 +1995,26 @@ internal bool ShouldUpdateColumn(DataStructureColumn column, DataStructureEntity
new Key(customGrouping.LookupId)) as DataServiceDataLookup;

var resultExpression =
RenderLookupColumnExpression(ds, entity, groupByColumn,
RenderLookupColumnExpression(ds, entity, groupByData.Column,
replaceParameterTexts, dynamicParameters, selectParameterReferences, lookup);
sqlExpression.Append(" , ");
sqlExpression.Append(resultExpression);
sqlExpression.Append($" AS {ColumnData.GroupByCaptionColumn} ");
}

groupByNeeded = true;
if (!group.Any(groupByExpression =>
groupByExpression.Contains(customGrouping.GroupBy) ||
groupByExpression == orderByExpression))
else
{
group.Add(customGrouping.GroupBy);
if (!group.Any(groupByExpression =>
groupByExpression.Contains(customGrouping.GroupBy) ||
groupByExpression == orderByExpression))
{
if (groupByData.Column.Name == customGrouping.GroupBy &&
!group.Contains(groupByData.Expression))
{
group.Add(groupByData.Expression);
}
}
}
groupByNeeded = true;
}
if (order.Count > 0)
{
Expand Down Expand Up @@ -2326,9 +2349,11 @@ private string ColumnDataToSql(ColumnRenderData columnRenderData)
resultExpression = RenderLookupColumnExpression(ds, entity, column,
replaceParameterTexts, dynamicParameters,
selectParameterReferences);
// if we would group by lookuped column, we use original column in group-by clause
groupExpression = RenderExpression(column.Field as AbstractSchemaItem,
column.Entity == null ? entity : column.Entity,
var field = column.Field is LookupField lookupField
? lookupField.Field
: column.Field;
groupExpression = RenderExpression(field,
column.Entity ?? entity,
replaceParameterTexts,
dynamicParameters, selectParameterReferences);
}
Expand Down Expand Up @@ -4190,4 +4215,4 @@ internal enum geoLatLonSql
{
Lat,
Lon
}
}
Expand Up @@ -4,6 +4,7 @@
xmlns:ads="http://schemas.origam.com/Origam.Schema.EntityModel.AbstractDataStructure/6.0.0"
xmlns:asi="http://schemas.origam.com/Origam.Schema.AbstractSchemaItem/6.0.0"
xmlns:ds="http://schemas.origam.com/Origam.Schema.EntityModel.DataStructure/6.0.0"
xmlns:dsc="http://schemas.origam.com/Origam.Schema.EntityModel.DataStructureColumn/6.0.1"
xmlns:dse="http://schemas.origam.com/Origam.Schema.EntityModel.DataStructureEntity/6.0.0"
xmlns:dsfs="http://schemas.origam.com/Origam.Schema.EntityModel.DataStructureFilterSet/6.0.0"
xmlns:dsfsf="http://schemas.origam.com/Origam.Schema.EntityModel.DataStructureFilterSetFilter/6.0.0"
Expand All @@ -29,6 +30,19 @@
asi:name="AllDataTypes"
dse:relationType="Normal"
dse:useUpsert="false">
<dsc:DataStructureColumn
asi:abstract="false"
dsc:aggregation="None"
dsc:field="Widgets/DataEntity/Widgets/AllDataTypes.origam#AllDataTypes/refTagInputSourceId/7142586a-1798-4f99-af0a-9166319d11e0"
dsc:hideInOutput="false"
x:id="064a4ffa-4576-4e9d-b52f-7e161cb5e84c"
dsc:lookup="Widgets/DataLookup/Widgets/TagInputSource_Label_GetId.origam#TagInputSource_Label_GetId/991a8bec-5169-4506-9457-7f91c27bb5fd"
asi:name="TagInputSourceLabel_DataStructure"
dsc:upsertType="Replace"
dsc:useCopiedValue="false"
dsc:useLookupValue="true"
dsc:writeOnly="false"
dsc:xmlMappingType="Default" />
<dse:DataStructureEntity
asi:abstract="false"
dse:allFields="true"
Expand Down
Expand Up @@ -1214,6 +1214,139 @@
apvi:propertyId="562004f3-48f7-460d-b8f6-e8c911235a36"
pvi1:value="Boolean 1" />
</csi:ControlSetItem>
<csi:ControlSetItem
asi:abstract="false"
x:id="f76b2df9-5639-48a0-82df-96df2222ffed"
csi:isAlternative="false"
csi:level="100"
asi:name="AsTextBox7"
csi:requestSaveAfterChange="false"
csi:widget="Root/Control/_Basic Controls/AsTextBox.origam#AsTextBox/7d1ec946-017e-4a00-9d92-d4d6b9fabffc">
<pbi:PropertyBindingInfo
asi:abstract="false"
pbi:designDataSetPath="AllDataTypes.TagInputSourceLabel_DataStructure"
x:id="253cb214-3aed-4d6a-8d3c-39bb18d06246"
asi:name="Value"
apvi:propertyId="e4c1751b-7b33-42d3-b945-11971cdf7c56"
pbi:value="TagInputSourceLabel_DataStructure" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="0e517c87-7110-45d2-8fd5-556eb1db506e"
asi:name="AllowTab"
apvi:propertyId="1cfe8847-15cd-4bc6-80a5-481107a5b61f"
pvi1:value="false" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="0faffdeb-4933-423f-9ace-ae7d9498492c"
asi:name="Height"
apvi:propertyId="c84ec964-046e-4a30-8941-a4eb234a2303"
pvi1:value="20" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="1da2da3c-e26c-42d6-a41d-e57bd830999e"
asi:name="StyleId"
apvi:propertyId="02a5fdbd-f29d-408e-ac4a-0ef7e7be7a40"
pvi1:value="00000000-0000-0000-0000-000000000000" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="34a2df12-d7cf-418e-ac52-6db6be31c75a"
asi:name="IsPassword"
apvi:propertyId="d943815e-c6df-4236-99e8-566aaf4acc20"
pvi1:value="false" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="39c6b401-dbb9-48d9-bcab-0e416ceb173f"
asi:name="CaptionLength"
apvi:propertyId="0a68bbf8-dc94-415f-baba-768e947f19e8"
pvi1:value="100" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="39d5e06a-c740-4ef5-a60d-4a0a80ab31a4"
asi:name="Caption"
apvi:propertyId="340fed3d-87c0-4c4b-a474-d28ebe2a7a14"
pvi1:value="TagInputSourceLabel_DataStructure" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="3e38f32f-c065-4881-9fbd-3cd37d73df34"
asi:name="Value"
apvi:propertyId="e4c1751b-7b33-42d3-b945-11971cdf7c56" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="43a4095e-f449-437b-879a-bf6e3764be47"
asi:name="HideOnForm"
apvi:propertyId="5ec12c37-3a98-4d95-ac90-9a3045969728"
pvi1:value="false" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="627bf403-3c37-409e-988b-d3fe606dee0d"
asi:name="TabIndex"
apvi:propertyId="47cd5ccd-1e49-40d0-bc82-ac6c98fee39f"
pvi1:value="51" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="6c737810-cdf2-4ef2-aee3-4e8623519fa3"
asi:name="IsRichText"
apvi:propertyId="6a6e791e-0854-4779-9150-e70725b5d279"
pvi1:value="false" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="79e6e377-8d14-4ca6-b327-b8cbf09b8313"
asi:name="CustomNumericFormat"
apvi:propertyId="1713bfc4-d991-4c6d-a26e-93bd8ef8046b" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="81a6ef4f-7695-42e1-a022-5b96d6b731c0"
asi:name="Width"
apvi:propertyId="f40ac6ea-026c-4e8d-89e7-a13db97bd37c"
pvi1:value="400" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="94eff13d-7277-4418-82cc-2728d36813c3"
asi:name="GridColumnCaption"
apvi:propertyId="2feb3117-3bcd-4779-baa8-ef742e1eb108" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="b8233b11-5a3a-4442-b375-63f39164ce93"
asi:name="ReadOnly"
apvi:propertyId="2240ad59-99a5-4429-89e6-94a3d9fd62b8"
pvi1:value="true" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="d98b5f79-2a57-47d6-9c5d-cb7ea8a45a9e"
asi:name="Left"
apvi:propertyId="729b0402-6d81-47bc-936a-c26328136607"
pvi1:value="108" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="df65a4c0-1151-45c7-9b07-0f2d22df975a"
asi:name="GridColumnWidth"
apvi:propertyId="16162dee-452f-4290-b8d5-753b89647f48"
pvi1:value="0" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="e01b04be-9233-474e-af61-dab24d457fc5"
asi:name="Top"
apvi:propertyId="bf2e55d0-5859-4b26-9e82-f187a61f53f7"
pvi1:value="304" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="f119e9a9-70c8-4ba3-816f-a02881ec4d8e"
asi:name="Multiline"
apvi:propertyId="c2abd09e-17b2-4a60-9a62-c28078dc632a"
pvi1:value="false" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="f3c5d6f1-e5cd-48c7-acc4-81382f0a9480"
asi:name="CaptionPosition"
apvi:propertyId="2b6c0455-a35e-4eef-9eaa-740f66732087"
pvi1:value="0" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="f4ecd878-4b1d-4eee-a1a5-388f92df8b34"
asi:name="Dock"
apvi:propertyId="24326c8e-8fa1-40f5-9e52-ba02e755127c"
pvi1:value="0" />
</csi:ControlSetItem>
<csi:ControlSetItem
asi:abstract="false"
x:id="ff303553-9c3e-407f-b63c-a981c9597aee"
Expand Down Expand Up @@ -1378,7 +1511,7 @@
x:id="22b1ac50-976e-4780-8ff3-9ab40f57e4d9"
asi:name="Height"
apvi:propertyId="2e965f41-7fea-480b-a44a-095637d0af7d"
pvi1:value="300" />
pvi1:value="354" />
<pvi1:PropertyValueItem
asi:abstract="false"
x:id="28bd09b5-7e8c-4d80-967b-739c9c666418"
Expand Down