informix
Extending Informix Dynamic Server 2000
Extending an Operator Class

Creating an Operator Class

For most indexing, the operators in the default operator class of a secondary access method provide adequate support. However, when you want to order the data in a different sequence than the default operator class provides, you can define a new operator class for the secondary access method.

The CREATE OPCLASS statement creates an operator class. It provides the following information about the operator class to the database server:

The database server stores this information in the sysopclasses system catalog table. You must have the Resource privilege for the database or be the DBA to create an operator class.

The database server provides the default operator class, btree_ops, for the generic B-tree access method. The following CREATE OPCLASS statement creates a new operator class for the generic B-tree access method:

For more information, see Generic B-Tree Index.

You might want to create a new operator class for:

Creating a New B-Tree Operator Class

To traverse the index structure, the generic B-tree index uses the sequence that the relational operators define. By default, a B-tree uses the lexicographical sequence of data because the default operator class, btree_ops, contains the relational-operator functions. (For more information on this sequence, see Changing the Sort Order.) For a generic B-tree to use a different sequence for its index values, you can create a new operator class for the btree secondary access method. You can then specify the new operator class when you define an index on that data type.

When you create a new operator class for the generic B-tree index, you provide an additional sequence for organizing data in a B-tree. When you create the B-tree index, you can specify the sequence that you want a column (or user-defined function) in the index to have.

To create a new operator class for a generic B-tree index

  1. Write external (C or Java) functions for five new B-tree strategy functions that accept the appropriate data type in their parameter list.
  2. The B-tree secondary access method expects five strategy functions; therefore, any new operator class must define exactly five. The parameter data types can be built in or user defined. However, each function must return a Boolean value. For more information on strategy functions, see B-Tree Strategy Functions.

  3. Register the new strategy functions in the database with the CREATE FUNCTION statement.
  4. You must register the set of strategy functions for each data type on which you are supporting the operator class.

  5. Write external functions for the new B-tree support function that accepts the appropriate data type in its parameter list.
  6. The B-tree secondary access method expects one support function; therefore, any new operator class must define only one. The parameter data types can be built-in or user-defined data types. However, the return type must be integer. For more information on support functions, see B-Tree Support Function.

  7. Register the new support function in the database with the CREATE FUNCTION statement.
  8. You must register a support function for each data type on which you are supporting the operator class.

  9. Create the new operator class for the B-tree secondary access method, btree.
  10. When you create an operator class, specify the following in the CREATE OPCLASS statement:

You registered these functions in step 2. You must list the functions in the order that the B-tree secondary access method expects: the first function is the replacement for lessthan(), the second for lessthanorequal(), and so on.

You registered this function in step 4. It is the replacement for the compare() function.

For more information on how to use the CREATE OPCLASS statement, refer to the Informix Guide to SQL: Syntax.

These steps create the new operator class of the generic B-tree index. You can also extend the default operator class to provide support for new data types. For more information, see Extensions of the btree_ops Operator Class.

To use the new operator class, specify the name of the operator class after the column or function name in the CREATE INDEX statement.

Creating an Absolute-Value Operator Class

As an example, suppose you want to define a new ordering for integers. The lexicographical sequence of the default B-tree operator class orders integers numerically: -4 < -3 < -2 < -1 < 0 < 1 < 2 < 3. Instead, you might want the numbers -4, 2, -1, -3 to appear in order of absolute value.

To obtain the absolute-value order, you must define external functions that treat negative integers as positive integers. The following steps create a new operator class called abs_btree_ops with strategy and support functions that provide the absolute-value order:

  1. Write and register external functions for the new strategy functions: abs_lessthan(), abs_lessthanorequal(), abs_equal(), abs_greaterthan(), and abs_greaterthanorequal().
  2. For more information, refer to Chapter 4, Developing a User-Defined Routine.

  3. Register the five new strategy functions with the CREATE FUNCTION statement.
  4. The following CREATE FUNCTION statements register the five strategy functions that handle the INTEGER data type:

  5. Write the C function for the new support function: abs_compare().
  6. Compile this function and store it in the absbtree.so shared-object file.

  7. Register the new support function with the CREATE FUNCTION statement.
  8. The following CREATE FUNCTION statement registers the support function that handles the INTEGER data type:

  9. Create the new abs_btree_ops operator class for the B-tree secondary access method.

You can now create a B-tree index on an INTEGER column and associate the new operator class with this column.

The c_num1_ix index uses the new operator class, abs_btree_ops, for the cust_num column. An end user can now use the absolute value functions in SQL statements, as in the following example:

In addition, because the abs_lt() function is part of an operator class, the query optimizer can use the c_num1_ix index when it looks for all cust_tab rows with cust_num values between -7 and 7. A cust_num value of -8 does not satisfy this query.

The default operator class is still available for indexes. The following CREATE INDEX statement defines a second index on the cust_num column:

The c_num2_ix index uses the default operator class, btree_ops, for the cust_num column. The following query uses the operator function for the default less than (<) operator:

The query optimizer can use the c_num2_ix index when it looks for all cust_tab rows with cust_num values less than 7. A cust_num value of -8 does satisfy this query.

Defining an Operator Class for Other Secondary Access Methods

You can also define operator classes for user-defined secondary access methods. A user-defined secondary access method is one that a database developer has defined to implement a particular type of index. These access methods might have been defined in the database by a DataBlade module.

Tip: You can examine the sysams system catalog table to determine which secondary access methods your database defines. For information on the columns of the sysams system catalog table, see the "Informix Guide to SQL: Reference."

You perform the same steps to define an operator class on a user-defined secondary access method as you use to define an operator class on the generic B-tree index. (See Creating a New B-Tree Operator Class.) The only difference is that to create the index, you must specify the name of the user-defined secondary access method in the USING clause of the CREATE INDEX statement.


Extending Informix Dynamic Server 2000, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved