Changeset: d91ec813061e for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/d91ec813061e
Added Files:
        sql/test/BugTracker-2026/Tests/8102-broad-range-orderidx.test
Modified Files:
        gdk/gdk_select.c
Branch: Dec2025
Log Message:

#8102 skip use of ordered index if cost outweights linear scan


diffs (121 lines):

diff --git a/gdk/gdk_select.c b/gdk/gdk_select.c
--- a/gdk/gdk_select.c
+++ b/gdk/gdk_select.c
@@ -1359,6 +1359,7 @@ BATselect(BAT *b, BAT *s, const void *tl
        oid vwl = 0, vwh = 0;
        lng vwo = 0;
        Heap *oidxh = NULL;
+       BAT *view = NULL;
        const char *algo;
        enum range_comp_t range;
        const bool notnull = BATgetprop(b, GDK_NOT_NULL) != NULL;
@@ -1716,7 +1717,6 @@ BATselect(BAT *b, BAT *s, const void *tl
            !bi.sorted &&
            !bi.revsorted &&
            ci.tpe == cand_dense) {
-               BAT *view = NULL;
                (void) BATcheckorderidx(b);
                MT_lock_set(&b->batIdxLock);
                if ((oidxh = b->torderidx) != NULL)
@@ -1733,15 +1733,10 @@ BATselect(BAT *b, BAT *s, const void *tl
                        MT_lock_unset(&pb->batIdxLock);
                }
                if (oidxh) {
-                       /* Is query selective enough to use the ordered
-                        * index?  Finding the boundaries is 2*log(n)
-                        * where n is the size of the bat, sorting is
-                        * N*log(N) where N is the number of results.
-                        * If the sum is less than n (cost of scan),
-                        * it's cheaper.  However, to find out how large
-                        * N is, we'd have to do the two boundary
-                        * searches.  If we do that, we might as well do
-                        * it all. */
+                       /* Find the boundaries in the ordered index.  Once
+                        * we know how many values are in the range we
+                        * decide whether using the index is actually
+                        * cheaper than a scan (see below). */
                        if (view) {
                                bat_iterator_end(&bi);
                                bi = bat_iterator(b);
@@ -1847,6 +1842,26 @@ BATselect(BAT *b, BAT *s, const void *tl
                                        high = ORDERfndfirst(b, oidxh, th);
                        }
                }
+               /* Now that we know the size of the range in the ordered
+                * index, check whether using the index is actually
+                * cheaper than a scan.  Building the result from the
+                * index requires copying the matching OIDs and sorting
+                * them, which costs about N*log(N) for N results, whereas
+                * a scan costs about the number of candidates.  For broad
+                * ranges the scan is much cheaper (and avoids sorting a
+                * large result). */
+               if (oidxh != NULL && high > low &&
+                   (double) (high - low) * (log2((double) (high - low)) + 1.0) 
>= (double) ci.ncand) {
+                       HEAPdecref(oidxh, false);
+                       oidxh = NULL;
+                       if (view) {
+                               b = view;
+                               bat_iterator_end(&bi);
+                               bi = bat_iterator(b);
+                               poidx = false;
+                       }
+                       goto use_scan;
+               }
                if (anti) {
                        assert(oidxh == NULL);
                        if (bi.sorted) {
@@ -1932,6 +1947,7 @@ BATselect(BAT *b, BAT *s, const void *tl
                return bn;
        }
 
+  use_scan:
        assert(oidxh == NULL);
        /* upper limit for result size */
        maximum = ci.ncand;
diff --git a/sql/test/BugTracker-2026/Tests/8102-broad-range-orderidx.test 
b/sql/test/BugTracker-2026/Tests/8102-broad-range-orderidx.test
new file mode 100644
--- /dev/null
+++ b/sql/test/BugTracker-2026/Tests/8102-broad-range-orderidx.test
@@ -0,0 +1,42 @@
+statement ok
+CREATE TABLE t8102 (v BIGINT)
+
+statement ok
+INSERT INTO t8102 SELECT value + 1000 FROM sys.generate_series(0, 1000)
+
+statement ok
+INSERT INTO t8102 SELECT value FROM sys.generate_series(0, 1000)
+
+statement ok
+CREATE ORDERED INDEX t8102_v_oidx ON t8102(v)
+
+# A broad range over a column that has an ordered index must return the
+# same result as the equivalent scan without the ordered index.
+query I
+SELECT COUNT(*) FROM t8102 WHERE v < 1000
+----
+1000
+
+query I
+SELECT COUNT(*) FROM t8102 WHERE NOT (v >= 1000)
+----
+1000
+
+query I
+SELECT COUNT(*) FROM t8102 WHERE v >= 1000
+----
+1000
+
+# Narrow ranges should still be handled correctly as well.
+query I
+SELECT COUNT(*) FROM t8102 WHERE v < 10
+----
+10
+
+query I
+SELECT COUNT(*) FROM t8102 WHERE v >= 1990
+----
+10
+
+statement ok
+DROP TABLE t8102
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to