@@ -703,3 +703,137 @@ logical_plan
703703physical_plan
70470401)ProjectionExec: expr=[column1@0 as CASE WHEN CASE WHEN t.a IS NOT NULL THEN t.a ELSE Int64(1) END IS NOT NULL THEN t.a ELSE Int64(1) END]
70570502)--DataSourceExec: partitions=1, partition_sizes=[1]
706+
707+ #####
708+ # CASE with literal characters (to test lookup table CASE optimization)
709+ #####
710+ statement ok
711+ create table source (letter varchar) as values ('a'), ('b'), (NULL), ('c'), ('a'), ('c'), ('d');
712+
713+ # Table with different string types
714+ statement ok
715+ create table letters as
716+ select
717+ arrow_cast(letter, 'Utf8') as letter_utf8,
718+ arrow_cast(letter, 'LargeUtf8') as letter_large_utf8,
719+ arrow_cast(letter, 'Utf8View') as letter_utf8_view,
720+ arrow_cast(letter, 'Dictionary(Int32, Utf8)') as letter_string_dict,
721+ from source;
722+
723+
724+ query TIIIII
725+ select
726+ letter_utf8 as letter
727+ ,CASE letter_utf8 WHEN 'b' THEN 1 WHEN 'a' THEN 2 WHEN 'd' THEN 3 ELSE 0 END as utf8
728+ ,CASE letter_large_utf8 WHEN 'b' THEN 1 WHEN 'a' THEN 2 WHEN 'd' THEN 3 ELSE 0 END as large_utf8
729+ ,CASE letter_utf8_view WHEN 'b' THEN 1 WHEN 'a' THEN 2 WHEN 'd' THEN 3 ELSE 0 END as utf8_view
730+ ,CASE letter_string_dict WHEN 'b' THEN 1 WHEN 'a' THEN 2 WHEN 'd' THEN 3 ELSE 0 END as string_dict
731+ ,CASE letter_utf8 WHEN 'b' THEN 1 WHEN NULL THEN 2 WHEN 'd' THEN 3 ELSE 0 END as utf8_with_null
732+ FROM letters;
733+ ----
734+ a 2 2 2 2 0
735+ b 1 1 1 1 1
736+ NULL 0 0 0 0 0
737+ c 0 0 0 0 0
738+ a 2 2 2 2 0
739+ c 0 0 0 0 0
740+ d 3 3 3 3 3
741+
742+ statement ok
743+ create table letters_binary as
744+ select
745+ arrow_cast(letter, 'Binary') as letter_binary,
746+ arrow_cast(letter, 'LargeBinary') as letter_large_binary,
747+ arrow_cast(letter, 'BinaryView') as letter_binary_view,
748+ arrow_cast(letter, 'Dictionary(Int32, Binary)') as letter_binary_dict,
749+ arrow_cast(arrow_cast(letter, 'Binary'), 'FixedSizeBinary(1)') as letter_fsb
750+ from source;
751+
752+ query ?IIIII
753+ select
754+ letter_binary as letter
755+ ,CASE letter_binary WHEN X'62' THEN 1 WHEN X'61' THEN 2 WHEN X'64' THEN 3 ELSE 0 END as binary
756+ ,CASE letter_large_binary WHEN X'62' THEN 1 WHEN X'61' THEN 2 WHEN X'64' THEN 3 ELSE 0 END as large_binary
757+ ,CASE letter_binary_view WHEN X'62' THEN 1 WHEN X'61' THEN 2 WHEN X'64' THEN 3 ELSE 0 END as binary_view
758+ ,CASE letter_binary_dict WHEN X'62' THEN 1 WHEN X'61' THEN 2 WHEN X'64' THEN 3 ELSE 0 END as binary_dict
759+ ,CASE letter_fsb WHEN X'62' THEN 1 WHEN X'61' THEN 2 WHEN X'64' THEN 3 ELSE 0 END as fsb
760+ FROM letters_binary;
761+ ----
762+ 61 2 2 2 2 2
763+ 62 1 1 1 1 1
764+ NULL 0 0 0 0 0
765+ 63 0 0 0 0 0
766+ 61 2 2 2 2 2
767+ 63 0 0 0 0 0
768+ 64 3 3 3 3 3
769+
770+ statement ok
771+ drop table source;
772+
773+
774+ statement ok
775+ drop table letters;
776+
777+ statement ok
778+ drop table letters_binary;
779+
780+ # Tests for CASE with boolean expressions
781+ statement ok
782+ create table booleans (b boolean) as values (true), (false), (null), (true), (null), (false);
783+
784+ query BIII
785+ select
786+ b as boolean_value
787+ ,CASE b WHEN true THEN 1 WHEN false THEN 2 ELSE 0 END as boolean_case
788+ ,CASE b WHEN false THEN 1 WHEN true THEN 2 ELSE 0 END as boolean_case_rev
789+ ,CASE b WHEN true THEN 1 WHEN NULL THEN 2 WHEN false THEN 3 ELSE 0 END as boolean_with_nulls
790+ FROM booleans;
791+ ----
792+ true 1 2 1
793+ false 2 1 3
794+ NULL 0 0 0
795+ true 1 2 1
796+ NULL 0 0 0
797+ false 2 1 3
798+
799+ statement ok
800+ drop table booleans;
801+
802+ # Tests for CASE with floating point literals
803+ statement ok
804+ create table float_source (f float) as values (1.0), (2.0), (null), (3.5), (2.0), (null);
805+
806+ statement ok
807+ create table floats as
808+ select
809+ arrow_cast(f, 'Float16') as f16,
810+ arrow_cast(f, 'Float32') as f32,
811+ arrow_cast(f, 'Float64') as f64,
812+ arrow_cast(f, 'Dictionary(Int32, Float32)') as f32_dict,
813+ from float_source;
814+
815+ query RTTTT
816+ select
817+ f32 as float_value
818+ ,CASE f16 WHEN 1.0 THEN 'one' WHEN 3.5 THEN 'three_point_five' WHEN 2.0 THEN 'two' ELSE 'N/A' END as f16_case
819+ ,CASE f32 WHEN 1.0 THEN 'one' WHEN 3.5 THEN 'three_point_five' WHEN 2.0 THEN 'two' ELSE 'N/A' END as f32_case
820+ ,CASE f64 WHEN 1.0 THEN 'one' WHEN 3.5 THEN 'three_point_five' WHEN 2.0 THEN 'two' ELSE 'N/A' END as f64_case
821+ ,CASE f32_dict WHEN 1.0 THEN 'one' WHEN 3.5 THEN 'three_point_five' WHEN 2.0 THEN 'two' ELSE 'N/A' END as f32_dict_case
822+ FROM floats;
823+ ----
824+ 1 one one one one
825+ 2 two two two two
826+ NULL N/A N/A N/A N/A
827+ 3.5 three_point_five three_point_five three_point_five three_point_five
828+ 2 two two two two
829+ NULL N/A N/A N/A N/A
830+
831+ statement ok
832+ drop table float_source;
833+
834+ statement ok
835+ drop table floats;
836+
837+ #####
838+ # End of lookup table CASE tests
839+ #####
0 commit comments