ibis-project / ibis

the portable Python dataframe library
https://ibis-project.org
Apache License 2.0
5.33k stars 599 forks source link

fix(sql): remove constants in `order_by` calls during select merging #10475

Open cpcloud opened 1 week ago

cpcloud commented 1 week ago

Remove literals in ORDER BY when sorting tables. Fixes #10428.

cpcloud commented 1 week ago

It looks like DuckDB's planner erases constants in ORDER BY:

❯ duckdb <<< "SET order_by_non_integer_literal=true; CREATE TABLE t AS SELECT i FROM RANGE(1, 4) AS _ (i); EXPLAIN SELECT i, 'foo' as x FROM t ORDER BY i DESC, 'foo'"

┌─────────────────────────────┐
│┌───────────────────────────┐│
││       Physical Plan       ││
│└───────────────────────────┘│
└─────────────────────────────┘
┌───────────────────────────┐
│         PROJECTION        │
│    ────────────────────   │
│__internal_decompress_integ│
│     ral_bigint(#0, 1)     │
│__internal_decompress_strin│
│           g(#1)           │
│                           │
│          ~3 Rows          │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│          ORDER_BY         │
│    ────────────────────   │
│          t.i DESC         │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│         PROJECTION        │
│    ────────────────────   │
│__internal_compress_integra│
│     l_utinyint(#0, 1)     │
│__internal_compress_string_│
│        uinteger(#1)       │
│                           │
│          ~3 Rows          │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│         PROJECTION        │
│    ────────────────────   │
│             i             │
│             x             │
│                           │
│          ~3 Rows          │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│         SEQ_SCAN          │
│    ────────────────────   │
│             t             │
│                           │
│       Projections: i      │
│                           │
│          ~3 Rows          │
└───────────────────────────┘