@@ -1876,6 +1876,159 @@ private void checkExponentialCnf(int n) {
18761876 checkSimplifyUnchanged (rexBuilder .makeCall (SqlStdOperatorTable .SOME_GT , operand1 , operand2 ));
18771877 }
18781878
1879+ /** Unit test for
1880+ * <a href="https://issues.apache.org/jira/browse/CALCITE-5733">[CALCITE-5733]
1881+ * Simplify 'a = ARRAY[1,2] AND a = ARRAY[2,3]' to 'false'</a>. */
1882+ @ Test void testSimplifyArrayEquality () {
1883+ final RelDataType arrayType = tArray (tInt ());
1884+ final RexNode aRef = input (arrayType , 0 );
1885+ final RexNode array12 =
1886+ rexBuilder .makeCall (arrayType , SqlStdOperatorTable .ARRAY_VALUE_CONSTRUCTOR ,
1887+ ImmutableList .of (literal (1 ), literal (2 )));
1888+ final RexNode array21 =
1889+ rexBuilder .makeCall (arrayType , SqlStdOperatorTable .ARRAY_VALUE_CONSTRUCTOR ,
1890+ ImmutableList .of (literal (2 ), literal (1 )));
1891+ final RexNode array23 =
1892+ rexBuilder .makeCall (arrayType , SqlStdOperatorTable .ARRAY_VALUE_CONSTRUCTOR ,
1893+ ImmutableList .of (literal (2 ), literal (3 )));
1894+ final RexNode array2Null =
1895+ rexBuilder .makeCall (arrayType , SqlStdOperatorTable .ARRAY_VALUE_CONSTRUCTOR ,
1896+ ImmutableList .of (literal (2 ), nullInt ));
1897+ final RexNode arrayDoubleNull =
1898+ rexBuilder .makeCall (arrayType , SqlStdOperatorTable .ARRAY_VALUE_CONSTRUCTOR ,
1899+ ImmutableList .of (nullInt , nullInt ));
1900+
1901+ // a = ARRAY[1,2] AND a = ARRAY[2,3]
1902+ final RexNode condition = and (eq (aRef , array12 ), eq (aRef , array23 ));
1903+ checkSimplifyFilter (condition , "false" );
1904+
1905+ // a = ARRAY[1,2] AND a = ARRAY[2,null]
1906+ final RexNode condition2 = and (eq (aRef , array12 ), eq (aRef , array2Null ));
1907+ checkSimplifyFilter (condition2 , "false" );
1908+
1909+ // a = ARRAY[1,2] AND a = ARRAY[2,null]
1910+ final RexNode condition3 = and (eq (aRef , array12 ), eq (aRef , arrayDoubleNull ));
1911+ checkSimplifyFilter (condition3 , "false" );
1912+
1913+ // a = ARRAY[2,null] AND a = ARRAY[2,null]
1914+ final RexNode condition4 = and (eq (aRef , arrayDoubleNull ), eq (aRef , arrayDoubleNull ));
1915+ checkSimplifyFilter (condition4 , "=($0, ARRAY(null:INTEGER, null:INTEGER))" );
1916+
1917+ // a = ARRAY[1,2] AND a = ARRAY[2,1]
1918+ final RexNode condition5 = and (eq (aRef , array12 ), eq (aRef , array21 ));
1919+ checkSimplifyFilter (condition5 , "false" );
1920+
1921+ // Nested type for Array
1922+ final RelDataType nestedArrayType = tArray (arrayType );
1923+ final RexNode nestedRef = input (nestedArrayType , 1 );
1924+
1925+ final RexNode nestedArray1212 =
1926+ rexBuilder .makeCall (nestedArrayType , SqlStdOperatorTable .ARRAY_VALUE_CONSTRUCTOR ,
1927+ ImmutableList .of (array12 , array12 ));
1928+ final RexNode nestedArray1221 =
1929+ rexBuilder .makeCall (nestedArrayType , SqlStdOperatorTable .ARRAY_VALUE_CONSTRUCTOR ,
1930+ ImmutableList .of (array12 , array21 ));
1931+ final RexNode nestedArray232Null =
1932+ rexBuilder .makeCall (nestedArrayType , SqlStdOperatorTable .ARRAY_VALUE_CONSTRUCTOR ,
1933+ ImmutableList .of (array23 , array2Null ));
1934+ final RexNode nestedArrayNulls =
1935+ rexBuilder .makeCall (nestedArrayType , SqlStdOperatorTable .ARRAY_VALUE_CONSTRUCTOR ,
1936+ ImmutableList .of (arrayDoubleNull , arrayDoubleNull ));
1937+
1938+ // a = ARRAY[ARRAY[1,2], ARRAY[1,2]] and a = ARRAY[ARRAY[1,2], ARRAY[2,1]]
1939+ final RexNode nestedCondition =
1940+ and (eq (nestedRef , nestedArray1212 ), eq (nestedRef , nestedArray1221 ));
1941+ checkSimplifyFilter (nestedCondition , "false" );
1942+
1943+ // a = ARRAY[ARRAY[1,2], ARRAY[1,2]] and a = ARRAY[ARRAY[2,3], ARRAY[2,null]]
1944+ final RexNode nestedCondition2 =
1945+ and (eq (nestedRef , nestedArray1212 ), eq (nestedRef , nestedArray232Null ));
1946+ checkSimplifyFilter (nestedCondition2 , "false" );
1947+
1948+ // a = ARRAY[ARRAY[1,2], ARRAY[1,2]] and a = ARRAY[ARRAY[null,null], ARRAY[null,null]]
1949+ final RexNode nestedCondition3 =
1950+ and (eq (nestedRef , nestedArray1212 ), eq (nestedRef , nestedArrayNulls ));
1951+ checkSimplifyFilter (nestedCondition3 , "false" );
1952+ }
1953+
1954+ /** Unit test for
1955+ * <a href="https://issues.apache.org/jira/browse/CALCITE-5733">[CALCITE-5733]
1956+ * Simplify 'a = ARRAY[1,2] AND a = ARRAY[2,3]' to 'false'</a>. */
1957+ @ Test void testSimplifyMultisetEquality () {
1958+ final RelDataType multisetType = typeFactory .createMultisetType (tInt (), -1 );
1959+ final RexNode aRef = input (multisetType , 0 );
1960+ final RexNode multiset12 =
1961+ rexBuilder .makeCall (multisetType , SqlStdOperatorTable .MULTISET_VALUE ,
1962+ ImmutableList .of (literal (1 ), literal (2 )));
1963+ final RexNode multiset21 =
1964+ rexBuilder .makeCall (multisetType , SqlStdOperatorTable .MULTISET_VALUE ,
1965+ ImmutableList .of (literal (2 ), literal (1 )));
1966+ final RexNode multiset23 =
1967+ rexBuilder .makeCall (multisetType , SqlStdOperatorTable .MULTISET_VALUE ,
1968+ ImmutableList .of (literal (2 ), literal (3 )));
1969+ final RexNode multiset2Null =
1970+ rexBuilder .makeCall (multisetType , SqlStdOperatorTable .MULTISET_VALUE ,
1971+ ImmutableList .of (literal (2 ), nullInt ));
1972+ final RexNode multisetDoubleNull =
1973+ rexBuilder .makeCall (multisetType , SqlStdOperatorTable .MULTISET_VALUE ,
1974+ ImmutableList .of (nullInt , nullInt ));
1975+
1976+ // a = MULTISET[1,2] AND a = MULTISET[2,3]
1977+ final RexNode condition = and (eq (aRef , multiset12 ), eq (aRef , multiset23 ));
1978+ checkSimplifyFilter (condition , "false" );
1979+
1980+ // a = MULTISET[1,2] AND a = MULTISET[2,null]
1981+ final RexNode condition2 = and (eq (aRef , multiset12 ), eq (aRef , multiset2Null ));
1982+ checkSimplifyFilter (condition2 , "false" );
1983+
1984+ // a = MULTISET[1,2] AND a = MULTISET[2,null]
1985+ final RexNode condition3 = and (eq (aRef , multiset12 ), eq (aRef , multisetDoubleNull ));
1986+ checkSimplifyFilter (condition3 , "false" );
1987+
1988+ // a = MULTISET[2,null] AND a = MULTISET[2,null]
1989+ final RexNode condition4 = and (eq (aRef , multisetDoubleNull ), eq (aRef , multisetDoubleNull ));
1990+ checkSimplifyFilter (condition4 , "=($0, MULTISET(null:INTEGER, null:INTEGER))" );
1991+
1992+ // a = MULTISET[1,2] AND a = MULTISET[2,1]
1993+ final RexNode condition5 = and (eq (aRef , multiset12 ), eq (aRef , multiset21 ));
1994+ checkSimplifyFilter (condition5 , "AND(=($0, MULTISET(1, 2)), =($0, MULTISET(2, 1)))" );
1995+
1996+ // Nested type for Multiset
1997+ final RelDataType nestedMultisetType = typeFactory .createMultisetType (multisetType , -1 );
1998+ final RexNode nestedRef = input (nestedMultisetType , 1 );
1999+
2000+ final RexNode nestedMultiset1212 =
2001+ rexBuilder .makeCall (nestedMultisetType , SqlStdOperatorTable .MULTISET_VALUE ,
2002+ ImmutableList .of (multiset12 , multiset12 ));
2003+ final RexNode nestedMultiset1221 =
2004+ rexBuilder .makeCall (nestedMultisetType , SqlStdOperatorTable .MULTISET_VALUE ,
2005+ ImmutableList .of (multiset12 , multiset21 ));
2006+ final RexNode nestedMultiset232Null =
2007+ rexBuilder .makeCall (nestedMultisetType , SqlStdOperatorTable .MULTISET_VALUE ,
2008+ ImmutableList .of (multiset23 , multiset2Null ));
2009+ final RexNode nestedMultisetNulls =
2010+ rexBuilder .makeCall (nestedMultisetType , SqlStdOperatorTable .MULTISET_VALUE ,
2011+ ImmutableList .of (multisetDoubleNull , multisetDoubleNull ));
2012+
2013+ // a = MULTISET[MULTISET[1,2], MULTISET[1,2]] and a = MULTISET[MULTISET[1,2], MULTISET[2,1]]
2014+ final RexNode nestedCondition =
2015+ and (eq (nestedRef , nestedMultiset1212 ), eq (nestedRef , nestedMultiset1221 ));
2016+ checkSimplifyFilter (nestedCondition ,
2017+ "AND(=($1, MULTISET(MULTISET(1, 2), MULTISET(1, 2))),"
2018+ + " =($1, MULTISET(MULTISET(1, 2), MULTISET(2, 1))))" );
2019+
2020+ // a = MULTISET[MULTISET[1,2], MULTISET[1,2]] and a = MULTISET[MULTISET[2,3], MULTISET[2,null]]
2021+ final RexNode nestedCondition2 =
2022+ and (eq (nestedRef , nestedMultiset1212 ), eq (nestedRef , nestedMultiset232Null ));
2023+ checkSimplifyFilter (nestedCondition2 , "false" );
2024+
2025+ // a = MULTISET[MULTISET[1,2], MULTISET[1,2]]
2026+ // and a = MULTISET[MULTISET[null,null], MULTISET[null,null]]
2027+ final RexNode nestedCondition3 =
2028+ and (eq (nestedRef , nestedMultiset1212 ), eq (nestedRef , nestedMultisetNulls ));
2029+ checkSimplifyFilter (nestedCondition3 , "false" );
2030+ }
2031+
18792032 @ Test void testSimplifyRange () {
18802033 final RexNode aRef = input (tInt (), 0 );
18812034 // ((0 < a and a <= 10) or a >= 15) and a <> 6 and a <> 12
0 commit comments