Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit cast from Boolean to UInt8 #3291

Closed
Tracked by #3428
sundy-li opened this issue Dec 8, 2021 · 1 comment
Closed
Tracked by #3428

Implicit cast from Boolean to UInt8 #3291

sundy-li opened this issue Dec 8, 2021 · 1 comment
Labels
A-query Area: databend query

Comments

@sundy-li
Copy link
Member

sundy-li commented Dec 8, 2021

Summary: make some functions work with boolean datatypes.

So we can transform avg(boolean) ---> avg(uint8)


Now databend boolean return true/false , like this:

[local] [sql]> select 1>0, 0>1;
[ok] ✅ Query precheck passed!
+---------+---------+
| (1 > 0) | (0 > 1) |
+---------+---------+
| true    | false   |
+---------+---------+
[ok] ✅ read rows: 1, read bytes: 1 B, rows/sec: 500 (rows/sec), bytes/sec: 500 (B/sec), time: 0.002 sec

if we can make boolean return 0/ 1 like this:

[local] [sql]> select 1>0, 0>1;
[ok] ✅ Query precheck passed!
+---------+---------+
| (1 > 0) | (0 > 1) |
+---------+---------+
| 1       | 0        |
+---------+---------+
[ok] ✅ read rows: 1, read bytes: 1 B, rows/sec: 500 (rows/sec), bytes/sec: 500 (B/sec), time: 0.002 sec

if support boolean return 0 or 1 , can optimize sql :
The percentage of delays by carrier for 2007

SELECT Carrier, c, c2, c*100/c2 as c3
FROM
(
    SELECT
        IATA_CODE_Reporting_Airline AS Carrier,
        count(*) AS c
    FROM ontime
    WHERE DepDelay>10
        AND Year=2007
    GROUP BY Carrier
) q
JOIN
(
    SELECT
        IATA_CODE_Reporting_Airline AS Carrier,
        count(*) AS c2
    FROM ontime
    WHERE Year=2007
    GROUP BY Carrier
) qq USING Carrier
ORDER BY c3 DESC;

Better version of the same query:

SELECT IATA_CODE_Reporting_Airline AS Carrier, avg(DepDelay>10)*100 AS c3
FROM ontime
WHERE Year=2007
GROUP BY Carrier
ORDER BY c3 DESC

Originally posted by @wubx in #3267

@sundy-li sundy-li added the A-query Area: databend query label Dec 8, 2021
@PsiACE PsiACE added the v0.7 label Dec 13, 2021
@BohuTANG
Copy link
Member

Fixed in #4237

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-query Area: databend query
Projects
None yet
Development

No branches or pull requests

3 participants