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

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

Open
gmarbury opened this issue Jul 23, 2020 · 0 comments

Comments

@gmarbury
Copy link

gmarbury commented Jul 23, 2020

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++;
                        }
                    }
                               
                }

            }

        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant