/// <summary>
        /// Removes invalid selections e.g. hidden selections still being sent from the client.
        /// The client keep them since they can be visible when the filters changes.
        /// This is only applicable for discrete facets (range facet selection are always visible)
        /// </summary>
        /// <param name="facetsConfig"></param>
        /// <returns></returns>
        public FacetsConfig2 Update(FacetsConfig2 facetsConfig)
        {
            foreach (string facetCode in facetsConfig.GetFacetCodes())
            {
                var config = facetsConfig.GetConfig(facetCode);

                if (config.Facet.FacetTypeId != EFacetType.Discrete || config.Picks.Count == 0)
                {
                    continue;
                }

                if (!config.HasPicks())
                {
                    continue;
                }

                config.Picks = QueryProxy.QueryRows(
                    PicksCompiler.Compile(
                        QuerySetupBuilder.Build(facetsConfig, config.Facet, null, null),
                        config.GetIntegerPickValues()
                        ),
                    x => new FacetConfigPick(EPickType.discrete, x.GetString(0), x.GetString(1))
                    );
            }
            return(facetsConfig);
        }
 protected override string Compile(Facet facet, FacetsConfig2 facetsConfig, string intervalQuery)
 => RangeCountSqlCompiler.Compile(
     QuerySetupBuilder.Build(
         facetsConfig,
         facet,
         CountTables,
         facetsConfig.GetFacetCodes()
         ),
     facet,
     intervalQuery,
     Config.CountColumn
     );
        protected override string Compile(Facet facet, FacetsConfig2 facetsConfig, string payload)
        {
            // FIXME: Fix confusing args, when does facet differ from targetFacet?
            var aggregateFacet = Facets.Get(facet.AggregateFacetId);
            var targetFacet    = Facets.GetByCode(facetsConfig.TargetCode);
            var tableNames     = GetTables(facetsConfig, targetFacet, aggregateFacet);
            var facetCodes     = facetsConfig.GetFacetCodes();

            facetCodes.InsertAt(targetFacet.FacetCode, aggregateFacet.FacetCode);

            var querySetup = QuerySetupBuilder.Build(facetsConfig, aggregateFacet, tableNames, facetCodes);
            var sql        = CountSqlCompiler.Compile(querySetup, targetFacet, aggregateFacet, facet.AggregateType ?? "count");

            return(sql);
        }