pingcap / tidb

TiDB is an open-source, cloud-native, distributed, MySQL-Compatible database for elastic scale and real-time analytics. Try AI-powered Chat2Query free at : https://tidbcloud.com/free-trial
https://pingcap.com
Apache License 2.0
36.17k stars 5.72k forks source link

interface conversion: expression.Expression is *expression.ScalarFunction, not *expression.Column with union #40589

Closed hawkingrei closed 6 days ago

hawkingrei commented 1 year ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

DROP DATABASE IF EXISTS database8;
CREATE DATABASE database8;
USE database8;
CREATE TABLE t0(c0 FLOAT UNSIGNED ZEROFILL , c1 BOOL DEFAULT false UNIQUE , c2 CHAR NOT NULL DEFAULT '0' );
CREATE ALGORITHM=TEMPTABLE VIEW v0(c0, c1, c2) AS SELECT t0.c2, DEFAULT(t0.c0), NULL FROM t0 WHERE t0.c1 GROUP BY CAST(t0.c1 AS DATE) HAVING '-402534603' ORDER BY ((NULL)^(t0.c1)), DEFAULT(t0.c2), ((true) IS NOT NULL) ASC;

2. What did you expect to see? (Required)

succeed

3. What did you see instead (Required)

 SELECT v0.c0 FROM  v0 NATURAL JOIN t0  WHERE ((2076932507)LIKE(EXPORT_SET(0.45272822680659974, '-', ((IS_IPV6((~ (-1565129563))))LIKE(v0.c2))))) UNION ALL SELECT v0.c0 FROM  v0 NATURAL JOIN t0  WHERE (NOT (((2076932507)LIKE(EXPORT_SET(0.45272822680659974, '-', ((IS_IPV6((~ (-1565129563))))LIKE(v0.c2))))))) UNION ALL SELECT v0.c0 FROM  v0 NATURAL JOIN t0  WHERE ((((2076932507)LIKE(EXPORT_SET(0.45272822680659974, '-', ((IS_IPV6((~ (-1565129563))))LIKE(v0.c2)))))) IS NULL);

--> java.sql.SQLException: interface conversion: expression.Expression is *expression.ScalarFunction, not *expression.Column

4. What is your TiDB version? (Required)

YangKeao commented 1 year ago

A clearer reproduce:

CREATE TABLE t0(
  c0 BOOL DEFAULT false UNIQUE,
  c1 CHAR NOT NULL DEFAULT '0'
);

CREATE VIEW v0(c0, c1) AS
SELECT
  t0.c1,
  NULL
FROM
  t0
WHERE
  t0.c0;

SELECT
  v0.c0
FROM
  v0 NATURAL
  JOIN t0
WHERE
  (
    false LIKE(v0.c1)
  );

TiDB assigns the expressions to projection and column in (*LogicalJoin).updateEQCond(). In this function, it was an from_binary(t0.c0) = t0.c1. However, the return type of the from_binary is TypeNull. After building the projection, another cast is wrapped around them to make it possible to compare, and cause this bug.

qw4990 commented 6 days ago

Can't reproduce this issue, it might have been fixed by some other PR:

mysql> SELECT
    ->   v0.c0
    -> FROM
    ->   v0 NATURAL
    ->   JOIN t0
    -> WHERE
    ->   (
    ->     false LIKE(v0.c1)
    ->   );
Empty set (0.01 sec)