From b9ff4b4c8a827694262383c9cfaab26a0f268aa8 Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Fri, 1 Mar 2024 17:13:43 -0800 Subject: [PATCH] Dont crash on nil in LOGICAL_OR and LOGICAL_AND --- internal/function_aggregate.go | 6 ++++++ query_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/internal/function_aggregate.go b/internal/function_aggregate.go index 984f5cd..f69b029 100644 --- a/internal/function_aggregate.go +++ b/internal/function_aggregate.go @@ -350,6 +350,9 @@ type LOGICAL_AND struct { } func (f *LOGICAL_AND) Step(cond Value, opt *AggregatorOption) error { + if cond == nil { + return nil + } b, err := cond.ToBool() if err != nil { return err @@ -369,6 +372,9 @@ type LOGICAL_OR struct { } func (f *LOGICAL_OR) Step(cond Value, opt *AggregatorOption) error { + if cond == nil { + return nil + } b, err := cond.ToBool() if err != nil { return err diff --git a/query_test.go b/query_test.go index 515595e..ada20e5 100644 --- a/query_test.go +++ b/query_test.go @@ -783,11 +783,23 @@ SELECT ARRAY_CONCAT_AGG(x) AS array_concat_agg FROM ( query: `SELECT LOGICAL_AND(x) AS logical_and FROM UNNEST([true, false, true]) AS x`, expectedRows: [][]interface{}{{false}}, }, + { + name: "logical_and null", + query: ` WITH toks AS (SELECT FALSE AS x UNION ALL SELECT FALSE UNION ALL SELECT NULL) +SELECT LOGICAL_AND(x) AS logical_or FROM toks`, + expectedRows: [][]interface{}{{false}}, + }, { name: "logical_or", query: `SELECT LOGICAL_OR(x) AS logical_or FROM UNNEST([true, false, true]) AS x`, expectedRows: [][]interface{}{{true}}, }, + { + name: "logical_or null", + query: ` WITH toks AS (SELECT FALSE AS x UNION ALL SELECT FALSE UNION ALL SELECT NULL) +SELECT LOGICAL_OR(x) AS logical_or FROM toks`, + expectedRows: [][]interface{}{{false}}, + }, { name: "max from int group", query: `SELECT MAX(x) AS max FROM UNNEST([8, 37, 4, 55]) AS x`,