9
9
*
10
10
*
11
11
* IDENTIFICATION
12
- * $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.120 2002/07/13 19:20:34 tgl Exp $
12
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.121 2002/09/02 06:22:18 momjian Exp $
13
13
*
14
14
*-------------------------------------------------------------------------
15
15
*/
@@ -97,7 +97,7 @@ static bool function_index_operand(Expr *funcOpnd, RelOptInfo *rel,
97
97
static bool match_special_index_operator (Expr * clause , Oid opclass ,
98
98
bool indexkey_on_left );
99
99
static List * prefix_quals (Var * leftop , Oid expr_op ,
100
- char * prefix , Pattern_Prefix_Status pstatus );
100
+ Const * prefix , Pattern_Prefix_Status pstatus );
101
101
static List * network_prefix_quals (Var * leftop , Oid expr_op , Datum rightop );
102
102
static Oid find_operator (const char * opname , Oid datatype );
103
103
static Datum string_to_datum (const char * str , Oid datatype );
@@ -1675,10 +1675,9 @@ match_special_index_operator(Expr *clause, Oid opclass,
1675
1675
Var * leftop ,
1676
1676
* rightop ;
1677
1677
Oid expr_op ;
1678
- Datum constvalue ;
1679
- char * patt ;
1680
- char * prefix ;
1681
- char * rest ;
1678
+ Const * patt = NULL ;
1679
+ Const * prefix = NULL ;
1680
+ Const * rest = NULL ;
1682
1681
1683
1682
/*
1684
1683
* Currently, all known special operators require the indexkey on the
@@ -1697,76 +1696,53 @@ match_special_index_operator(Expr *clause, Oid opclass,
1697
1696
if (!IsA (rightop , Const ) ||
1698
1697
((Const * ) rightop )-> constisnull )
1699
1698
return false;
1700
- constvalue = (( Const * ) rightop ) -> constvalue ;
1699
+ patt = (Const * ) rightop ;
1701
1700
1702
1701
switch (expr_op )
1703
1702
{
1704
1703
case OID_TEXT_LIKE_OP :
1705
1704
case OID_BPCHAR_LIKE_OP :
1706
1705
case OID_VARCHAR_LIKE_OP :
1707
1706
case OID_NAME_LIKE_OP :
1707
+ /* the right-hand const is type text for all of these */
1708
1708
if (locale_is_like_safe ())
1709
- {
1710
- /* the right-hand const is type text for all of these */
1711
- patt = DatumGetCString (DirectFunctionCall1 (textout ,
1712
- constvalue ));
1713
1709
isIndexable = pattern_fixed_prefix (patt , Pattern_Type_Like ,
1714
1710
& prefix , & rest ) != Pattern_Prefix_None ;
1715
- if (prefix )
1716
- pfree (prefix );
1717
- pfree (patt );
1718
- }
1711
+ break ;
1712
+
1713
+ case OID_BYTEA_LIKE_OP :
1714
+ isIndexable = pattern_fixed_prefix (patt , Pattern_Type_Like ,
1715
+ & prefix , & rest ) != Pattern_Prefix_None ;
1719
1716
break ;
1720
1717
1721
1718
case OID_TEXT_ICLIKE_OP :
1722
1719
case OID_BPCHAR_ICLIKE_OP :
1723
1720
case OID_VARCHAR_ICLIKE_OP :
1724
1721
case OID_NAME_ICLIKE_OP :
1722
+ /* the right-hand const is type text for all of these */
1725
1723
if (locale_is_like_safe ())
1726
- {
1727
- /* the right-hand const is type text for all of these */
1728
- patt = DatumGetCString (DirectFunctionCall1 (textout ,
1729
- constvalue ));
1730
1724
isIndexable = pattern_fixed_prefix (patt , Pattern_Type_Like_IC ,
1731
1725
& prefix , & rest ) != Pattern_Prefix_None ;
1732
- if (prefix )
1733
- pfree (prefix );
1734
- pfree (patt );
1735
- }
1736
1726
break ;
1737
1727
1738
1728
case OID_TEXT_REGEXEQ_OP :
1739
1729
case OID_BPCHAR_REGEXEQ_OP :
1740
1730
case OID_VARCHAR_REGEXEQ_OP :
1741
1731
case OID_NAME_REGEXEQ_OP :
1732
+ /* the right-hand const is type text for all of these */
1742
1733
if (locale_is_like_safe ())
1743
- {
1744
- /* the right-hand const is type text for all of these */
1745
- patt = DatumGetCString (DirectFunctionCall1 (textout ,
1746
- constvalue ));
1747
1734
isIndexable = pattern_fixed_prefix (patt , Pattern_Type_Regex ,
1748
1735
& prefix , & rest ) != Pattern_Prefix_None ;
1749
- if (prefix )
1750
- pfree (prefix );
1751
- pfree (patt );
1752
- }
1753
1736
break ;
1754
1737
1755
1738
case OID_TEXT_ICREGEXEQ_OP :
1756
1739
case OID_BPCHAR_ICREGEXEQ_OP :
1757
1740
case OID_VARCHAR_ICREGEXEQ_OP :
1758
1741
case OID_NAME_ICREGEXEQ_OP :
1742
+ /* the right-hand const is type text for all of these */
1759
1743
if (locale_is_like_safe ())
1760
- {
1761
- /* the right-hand const is type text for all of these */
1762
- patt = DatumGetCString (DirectFunctionCall1 (textout ,
1763
- constvalue ));
1764
1744
isIndexable = pattern_fixed_prefix (patt , Pattern_Type_Regex_IC ,
1765
1745
& prefix , & rest ) != Pattern_Prefix_None ;
1766
- if (prefix )
1767
- pfree (prefix );
1768
- pfree (patt );
1769
- }
1770
1746
break ;
1771
1747
1772
1748
case OID_INET_SUB_OP :
@@ -1777,6 +1753,12 @@ match_special_index_operator(Expr *clause, Oid opclass,
1777
1753
break ;
1778
1754
}
1779
1755
1756
+ if (prefix )
1757
+ {
1758
+ pfree (DatumGetPointer (prefix -> constvalue ));
1759
+ pfree (prefix );
1760
+ }
1761
+
1780
1762
/* done if the expression doesn't look indexable */
1781
1763
if (!isIndexable )
1782
1764
return false;
@@ -1798,6 +1780,12 @@ match_special_index_operator(Expr *clause, Oid opclass,
1798
1780
isIndexable = false;
1799
1781
break ;
1800
1782
1783
+ case OID_BYTEA_LIKE_OP :
1784
+ if (!op_in_opclass (find_operator (">=" , BYTEAOID ), opclass ) ||
1785
+ !op_in_opclass (find_operator ("<" , BYTEAOID ), opclass ))
1786
+ isIndexable = false;
1787
+ break ;
1788
+
1801
1789
case OID_BPCHAR_LIKE_OP :
1802
1790
case OID_BPCHAR_ICLIKE_OP :
1803
1791
case OID_BPCHAR_REGEXEQ_OP :
@@ -1867,10 +1855,9 @@ expand_indexqual_conditions(List *indexquals)
1867
1855
Var * leftop = get_leftop (clause );
1868
1856
Var * rightop = get_rightop (clause );
1869
1857
Oid expr_op = ((Oper * ) clause -> oper )-> opno ;
1870
- Datum constvalue ;
1871
- char * patt ;
1872
- char * prefix ;
1873
- char * rest ;
1858
+ Const * patt = (Const * ) rightop ;
1859
+ Const * prefix = NULL ;
1860
+ Const * rest = NULL ;
1874
1861
Pattern_Prefix_Status pstatus ;
1875
1862
1876
1863
switch (expr_op )
@@ -1885,82 +1872,57 @@ expand_indexqual_conditions(List *indexquals)
1885
1872
case OID_BPCHAR_LIKE_OP :
1886
1873
case OID_VARCHAR_LIKE_OP :
1887
1874
case OID_NAME_LIKE_OP :
1888
- /* the right-hand const is type text for all of these */
1889
- constvalue = ((Const * ) rightop )-> constvalue ;
1890
- patt = DatumGetCString (DirectFunctionCall1 (textout ,
1891
- constvalue ));
1875
+ case OID_BYTEA_LIKE_OP :
1892
1876
pstatus = pattern_fixed_prefix (patt , Pattern_Type_Like ,
1893
1877
& prefix , & rest );
1894
1878
resultquals = nconc (resultquals ,
1895
1879
prefix_quals (leftop , expr_op ,
1896
1880
prefix , pstatus ));
1897
- if (prefix )
1898
- pfree (prefix );
1899
- pfree (patt );
1900
1881
break ;
1901
1882
1902
1883
case OID_TEXT_ICLIKE_OP :
1903
1884
case OID_BPCHAR_ICLIKE_OP :
1904
1885
case OID_VARCHAR_ICLIKE_OP :
1905
1886
case OID_NAME_ICLIKE_OP :
1906
1887
/* the right-hand const is type text for all of these */
1907
- constvalue = ((Const * ) rightop )-> constvalue ;
1908
- patt = DatumGetCString (DirectFunctionCall1 (textout ,
1909
- constvalue ));
1910
1888
pstatus = pattern_fixed_prefix (patt , Pattern_Type_Like_IC ,
1911
1889
& prefix , & rest );
1912
1890
resultquals = nconc (resultquals ,
1913
1891
prefix_quals (leftop , expr_op ,
1914
1892
prefix , pstatus ));
1915
- if (prefix )
1916
- pfree (prefix );
1917
- pfree (patt );
1918
1893
break ;
1919
1894
1920
1895
case OID_TEXT_REGEXEQ_OP :
1921
1896
case OID_BPCHAR_REGEXEQ_OP :
1922
1897
case OID_VARCHAR_REGEXEQ_OP :
1923
1898
case OID_NAME_REGEXEQ_OP :
1924
1899
/* the right-hand const is type text for all of these */
1925
- constvalue = ((Const * ) rightop )-> constvalue ;
1926
- patt = DatumGetCString (DirectFunctionCall1 (textout ,
1927
- constvalue ));
1928
1900
pstatus = pattern_fixed_prefix (patt , Pattern_Type_Regex ,
1929
1901
& prefix , & rest );
1930
1902
resultquals = nconc (resultquals ,
1931
1903
prefix_quals (leftop , expr_op ,
1932
1904
prefix , pstatus ));
1933
- if (prefix )
1934
- pfree (prefix );
1935
- pfree (patt );
1936
1905
break ;
1937
1906
1938
1907
case OID_TEXT_ICREGEXEQ_OP :
1939
1908
case OID_BPCHAR_ICREGEXEQ_OP :
1940
1909
case OID_VARCHAR_ICREGEXEQ_OP :
1941
1910
case OID_NAME_ICREGEXEQ_OP :
1942
1911
/* the right-hand const is type text for all of these */
1943
- constvalue = ((Const * ) rightop )-> constvalue ;
1944
- patt = DatumGetCString (DirectFunctionCall1 (textout ,
1945
- constvalue ));
1946
1912
pstatus = pattern_fixed_prefix (patt , Pattern_Type_Regex_IC ,
1947
1913
& prefix , & rest );
1948
1914
resultquals = nconc (resultquals ,
1949
1915
prefix_quals (leftop , expr_op ,
1950
1916
prefix , pstatus ));
1951
- if (prefix )
1952
- pfree (prefix );
1953
- pfree (patt );
1954
1917
break ;
1955
1918
1956
1919
case OID_INET_SUB_OP :
1957
1920
case OID_INET_SUBEQ_OP :
1958
1921
case OID_CIDR_SUB_OP :
1959
1922
case OID_CIDR_SUBEQ_OP :
1960
- constvalue = ((Const * ) rightop )-> constvalue ;
1961
1923
resultquals = nconc (resultquals ,
1962
1924
network_prefix_quals (leftop , expr_op ,
1963
- constvalue ));
1925
+ patt -> constvalue ));
1964
1926
break ;
1965
1927
1966
1928
default :
@@ -1980,15 +1942,16 @@ expand_indexqual_conditions(List *indexquals)
1980
1942
*/
1981
1943
static List *
1982
1944
prefix_quals (Var * leftop , Oid expr_op ,
1983
- char * prefix , Pattern_Prefix_Status pstatus )
1945
+ Const * prefix_const , Pattern_Prefix_Status pstatus )
1984
1946
{
1985
1947
List * result ;
1986
1948
Oid datatype ;
1987
1949
Oid oproid ;
1950
+ char * prefix ;
1988
1951
Const * con ;
1989
1952
Oper * op ;
1990
1953
Expr * expr ;
1991
- char * greaterstr ;
1954
+ Const * greaterstr = NULL ;
1992
1955
1993
1956
Assert (pstatus != Pattern_Prefix_None );
1994
1957
@@ -2001,6 +1964,10 @@ prefix_quals(Var *leftop, Oid expr_op,
2001
1964
datatype = TEXTOID ;
2002
1965
break ;
2003
1966
1967
+ case OID_BYTEA_LIKE_OP :
1968
+ datatype = BYTEAOID ;
1969
+ break ;
1970
+
2004
1971
case OID_BPCHAR_LIKE_OP :
2005
1972
case OID_BPCHAR_ICLIKE_OP :
2006
1973
case OID_BPCHAR_REGEXEQ_OP :
@@ -2027,6 +1994,11 @@ prefix_quals(Var *leftop, Oid expr_op,
2027
1994
return NIL ;
2028
1995
}
2029
1996
1997
+ if (prefix_const -> consttype != BYTEAOID )
1998
+ prefix = DatumGetCString (DirectFunctionCall1 (textout , prefix_const -> constvalue ));
1999
+ else
2000
+ prefix = DatumGetCString (DirectFunctionCall1 (byteaout , prefix_const -> constvalue ));
2001
+
2030
2002
/*
2031
2003
* If we found an exact-match pattern, generate an "=" indexqual.
2032
2004
*/
@@ -2060,17 +2032,15 @@ prefix_quals(Var *leftop, Oid expr_op,
2060
2032
* "x < greaterstr".
2061
2033
*-------
2062
2034
*/
2063
- greaterstr = make_greater_string (prefix , datatype );
2035
+ greaterstr = make_greater_string (con );
2064
2036
if (greaterstr )
2065
2037
{
2066
2038
oproid = find_operator ("<" , datatype );
2067
2039
if (oproid == InvalidOid )
2068
2040
elog (ERROR , "prefix_quals: no < operator for type %u" , datatype );
2069
- con = string_to_const (greaterstr , datatype );
2070
2041
op = makeOper (oproid , InvalidOid , BOOLOID , false);
2071
- expr = make_opclause (op , leftop , (Var * ) con );
2042
+ expr = make_opclause (op , leftop , (Var * ) greaterstr );
2072
2043
result = lappend (result , expr );
2073
- pfree (greaterstr );
2074
2044
}
2075
2045
2076
2046
return result ;
@@ -2186,6 +2156,8 @@ string_to_datum(const char *str, Oid datatype)
2186
2156
*/
2187
2157
if (datatype == NAMEOID )
2188
2158
return DirectFunctionCall1 (namein , CStringGetDatum (str ));
2159
+ else if (datatype == BYTEAOID )
2160
+ return DirectFunctionCall1 (byteain , CStringGetDatum (str ));
2189
2161
else
2190
2162
return DirectFunctionCall1 (textin , CStringGetDatum (str ));
2191
2163
}
0 commit comments