Special strings don't get replaced in subqueries

Created on 17 November 2023, 8 months ago
Updated 18 November 2023, 8 months ago

Problem/Motivation

In the process of attempting to solve 🌱 Idea for archive pages (> operator) Active I turned an entire views query into a subquery, which returned this error:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '***CURRENT_TIME***+0) ORDER BY "node_field_data"."changed" DESC)'

So it appears that special strings are not replaced within subqueries.

Steps to reproduce

I'll work on this, I want to make sure I at least report this before I forget.

Proposed resolution

Had a conversation with @chx on Slack who answered:

in my very humble opinion the only way to solve this is to push the placeholder replacement from Views up to DBTNG because you need to run it after compilation

...

the short answer is, you need a new hook at the very end of Drupal\Core\Database\Query\Select::__toString

Remaining tasks

Test
Discuss approach
Step 3
Profit

User interface changes

None

API changes

I don't think so?

Data model changes

None

Release notes snippet

πŸ› Bug report
Status

Active

Version

10.1 ✨

Component
ViewsΒ  β†’

Last updated 1 minute ago

Created by

πŸ‡¦πŸ‡ΊAustralia darvanen Sydney, Australia

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

  • Issue created by @darvanen
  • πŸ‡¦πŸ‡ΊAustralia darvanen Sydney, Australia

    Inside this fork I've made an attempt at a test, but I ran into a different error:

    SQLSTATE[42S22]: Column not found: 1054 Unknown column 'node_field_data_nid' in 'order clause':
    
    SELECT "node_field_data"."nid" AS "nid"
    FROM "test53838627node_field_data" "node_field_data"
    WHERE "nid" NOT IN (SELECT "node_field_data"."nid" AS "nid"
                        FROM "test53838627node_field_data" "node_field_data"
                                 LEFT JOIN "test53838627node__field_date" "node__field_date"
                                           ON node_field_data.nid = node__field_date.entity_id AND
                                              (node__field_date.deleted = :views_join_condition_0 AND
                                               node__field_date.langcode = node_field_data.langcode)
                        WHERE (DATE_FORMAT(node__field_date.field_date_value, '%Y-%m-%d') >=
                               DATE_FORMAT('2023-11-18T12:14:47', '%Y-%m-%d'))
                        ORDER BY "node_field_data_nid" ASC);
    
     Array
    (
        [:views_join_condition_0] => 0
    )
    

    When I print the view query before making a subquery of it, the field alias is correct:

    SELECT "node_field_data"."nid" AS "node_field_data_nid", "node_field_data"."nid" AS "nid"
    FROM {node_field_data} "node_field_data"
    LEFT JOIN {node__field_date} "node__field_date"
    ON node_field_data.nid = node__field_date.entity_id AND (node__field_date.deleted = :views_join_condition_0 AND node__field_date.langcode = node_field_data.langcode)
    WHERE (DATE_FORMAT(node__field_date.field_date_value, '%Y-%m-%d') >= DATE_FORMAT('2023-11-18T13:11:20', '%Y-%m-%d'))
    ORDER BY "node_field_data_nid" ASC
    

    So there's some funny business happening with subqueries and field aliases, and now I'm quite lost :)

Production build 0.69.0 2024