Skip to content

StatisticType.Maximum on dates fields returns incorrectly in GeoDatabaseFeatureTable.QueryStatisticsAsync #810

@gmarbury

Description

@gmarbury

StatisticRecord.Statistics.Value always looks like a value like this 1/1/1970 12:40:57 AM on 1/1/1970
Ironically, StatisticType.Sum seems to work. Below is an example function that illustrates the problem with StatisticType.Maximum. Just pass it a Geodatabase

        public async Task TestDateMax(Geodatabase gdb)
        {
       
            foreach (var t in gdb.GeodatabaseFeatureTables)
            {
                if (t.LoadStatus != LoadStatus.Loaded)
                {
                    await t.LoadAsync();
                }

                var dateFields = new List<Field>();
                //Get all the date fields
                foreach (var f in t.Fields)
                {
                    if (f.FieldType == FieldType.Date)
                    {
                        dateFields.Add(f);
                    }
                }

                //If no date fields or records skip this table
                if (dateFields.Count == 0 || t.NumberOfFeatures ==0 )
                {
                    continue;
                }

                System.Diagnostics.Debug.WriteLine($"Testing {t.TableName} with {t.NumberOfFeatures} features");
                var fieldToMax_Loop = new Dictionary<Field, DateTimeOffset?>();
              
                //Manually (slowly) get the max for each date field
                var qp = new QueryParameters();
                var features = await t.QueryFeaturesAsync(qp);

                foreach( ArcGISFeature feature in features)
                {
                    if (feature.LoadStatus != LoadStatus.Loaded)
                    {
                        await feature.LoadAsync();
                    }
                    foreach ( var f in dateFields)
                    {
                        var value = (DateTimeOffset?) feature.GetAttributeValue(f) ;

                        if( !fieldToMax_Loop.TryGetValue(f, out DateTimeOffset? currentValue))
                        {
                            fieldToMax_Loop[f] = value;
                        }
                        else
                        {
                            if( value > currentValue)
                            {
                                fieldToMax_Loop[f] = value;
                            }
                        }
                    }
                }


                //Now lets's try to use StatisticType.Maximum and log the differences.. HINT they all break
                var fieldToMax_StatisticTypeMaximum = new Dictionary<Field, DateTimeOffset?>();

                int fieldsWrongCount = 0;
                int fieldsOkCount = 0;
                foreach (var f in dateFields)
                {
                    var statList = new List<StatisticDefinition>();
                    string statFieldName = "MaxOf" + f.Name;
                    var sd = new StatisticDefinition(f.Name, StatisticType.Maximum, statFieldName);

                    statList.Add(sd);
                    var statParams = new StatisticsQueryParameters(statList);                  
                    var tableStats = await t.QueryStatisticsAsync(statParams).ConfigureAwait();

                    var statRecord = tableStats.First();
                    var statValue = (DateTimeOffset?)statRecord.Statistics[statFieldName];

                    fieldToMax_StatisticTypeMaximum[f] = statValue;
                    fieldToMax_Loop.TryGetValue(f, out DateTimeOffset? loopValue);

                    if (loopValue != null)
                    {
                        if (loopValue != statValue)
                        {  
                            //SHOULD NEVER GET TO THIS LINE OF CODE---BUT WE DO
                            System.Diagnostics.Debug.WriteLine($"{t.TableName}.{f.Name} LoopMax={loopValue},  statMax={statValue}");
                            fieldsWrongCount++;
                        }
                        else
                        {
                            fieldsOkCount++;
                        }
                    }
                               
                }

            }

        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions