Intelligent compilation of procedural functions for query processing systems5987455Abstract A method, apparatus, and article of manufacture for an intelligent compiler. A query is executed in a computer to retrieve data from a relational database stored on a data storage device. The query contains a procedural function. The query is compiled to generate an internal representation of the procedural function. Then, the execution of the procedural function is optimized using the generated internal representation. Claims What is claimed is: Description BACKGROUND OF THE INVENTION
______________________________________
CREATE FUNCTION fooS (arg.sub.1, INT, arg.sub.2 VARCHAR (20))
RETURNS INT
LANGUAGE SQL
READS SQL DATA
RETURN arg.sub.1 + (SELECT c.sub.1 FROM T.sub.1 WHERE arg.sub.2 =
c.sub.2);
______________________________________
The function fooS has two arguments, arg.sub.1 which is an integer, and arg.sub.2, which is a variable length character string having up to twenty (20) characters. The function fooS returns an integer, it is written in SQL, and it reads SQL data. The function fooS returns an integer generated from the formula: arg.sub.1 +(SELECT c.sub.1 FROM T.sub.1 WHERE arg.sub.2 =C.sub.2).
______________________________________
CREATE FUNCTION fooT (arg.sub.1 VARCHAR (20))
RETURNS TABLE (c.sub.1 FLOAT, c.sub.2 INT)
LANGUAGE SQL
READS SQL DATA
RETURN SELECT b.sub.1 * LENGTH(arg1), b.sub.2
FROM T.sub.2 WHERE arg.sub.1 = b.sub.3 ;
______________________________________
The function fooT has an argument arg.sub.1, which is a variable length character string having up to twenty (20) characters. The function fooT returns a table having two columns, c.sub.1, which contains floating-point numbers, and c.sub.2, which contains integers. The function fooT is written in SQL and reads SQL data. The function fooT returns data based on the query: SELECT b.sub.1 * LENGTH(arg1), b.sub.2 FROM T.sub.2 WHERE arg.sub.1 =b.sub.3. The SQL-bodied functions can be used in a query, such as the following:
______________________________________
SELECT c.sub.1 FROM TABLE (fooT('hello') AS T
WHERE fooS(c.sub.2, 'world') > c.sub.1
______________________________________
Conventional compilers ignore the references to functions fooT and fooS. Then, these conventional compilers parse the query, without parsing the SQL-bodied functions. As the conventional systems parse the query, they are able to optimize the execution of the query. By ignoring the SQL-bodied functions during parsing, the conventional systems fail to optimize execution of the SQL-bodied functions within the context of the query. On the other hand, the intelligent compiler component 100 performs parsing of the SQL-bodied function while parsing a query. Therefore, the intelligent compiler component 100 provides the query global semantics component 104, the query rewrite and transform component 106, and the plan optimization component 108 with a query graph model that reflects the parsing of the SQL-bodied function. Then, the plan optimization component 108 is able to generate an optimal execution plan that executes the query containing the SQL-bodied function in an optimal manner. The following function is another sample SQL-bodied function written in pseudocode:
______________________________________
CREATE FUNCTION Foo (arg.sub.1 t.sub.1,,arg.sub.n t.sub.n)
RETURNS t.sub.n+1 or TABLE
LANGUAGE SQL
RETURN body (arg.sub.1,,arg.sub.n);
______________________________________
The function Foo has as its arguments types, t.sub.1 and t.sub.n, and the function Foo returns a type for a scalar value (i.e., t.sub.n+1) or a type for a table. The results of the function Foo are determined during execution using body (arg.sub.1, ,arg.sub.n). The function Foo is written in SQL, which indicates it is to be compiled by a SQL compiler. If the function returns a scalar value, the function may be used in a query such as the following:
______________________________________
SELECT Foo (Employee.a.sub.1,,Employee.a.sub.n)
FROM EMPLOYEE
______________________________________
The SELECT statement returns a type for a scalar value. However, one skilled in the art would recognize that if the SQL-bodied function returned a different type, such as a table type, the function could be used in a query to return that type. The intelligent compiler component 100 translates the above SELECT statement into the following compiled SQL form:
______________________________________
SELECT T.c.sub.1 FROM EMPLOYEE
SELECT body(arg.sub.1,,arg.sub.n)
FROM (SELECT Employee.a.sub.1 AS arg.sub.1,,Employee.a.sub.n
AS arg.sub.n FROM VALUES (1));
______________________________________
The compiled SQL is an internal representation of the query. The query may be represented using other internal representations, such as a query graph model. Although the example above shows the compiled SQL form of a query containing a SQL-bodied function that returns a scalar type, one skilled in the art will recognize that the compiled SQL form may be used for queries containing SQL-bodied functions that return other types, for example, table types. FIG. 3 illustrates a query graph model generated by the intelligent compiler component 100 that corresponds to the compiled SQL statement for the query containing a SQL-bodied function. The use of query graph models reduces the complexity of query compilation and optimization procedures. Query graph models also facilitate rewriting of queries in an efficient manner. A query graph model represents subquery operations, such as SELECT, in "boxes." The interconnections ("arcs" or "edges") between the boxes define the relationships between different items of data from different subquery operations. Query graph models are well known in the art and are described in more detail in: Pirahesh et al., "Extensible/Rule Based Query Rewrite Optimization in Starburst," Proceedings of ACM SIGMOD '92 International Conference on Management of Data, San Diego, Calif., U.S.A., 1992, and which is incorporated by reference herein. In the following discussions of query graph models, different "boxes" will be labeled to identify their functionality, but each of these boxes is a SELECT box. For example, the following labels may be placed on SELECT boxes: Sequence, TopSeq, BodySeq, labelSequ, VarTable, TopVarTbl, ArgTable, Loop, Iterator, Return, Case, or Foo. The query graph model supports arbitrary table operations where the inputs are tables and the outputs are tables. When a query is represented by a query graph model, each operation of the query may be graphically represented by a box having a head and body. For each box, the head of the box describes the output data stream or table produced by the operation, and the body specifies the operation required to compute the output table from records flowing into the body. The head of the box specifies each output column, and includes column names, types, and output ordering information. The body of the box may include "quantifiers," comprising table references or derived tables. Quantifiers have several types, including type "E" (Existential) quantifiers that correspond to the subquery associated with SQL's predicates EXISTS, IN ANY, and SOME. As illustrated in FIG. 3, the Foo box 300 processes the function Foo using as its inputs data from the Arguments box 302 and the Select box 306. The Arguments a.sub.1 and a.sub.n are supplied as inputs to the arguments box 302. The Values box 304 is a placeholder that provides a single data item (e.g., a row of a table). In FIG. 3, the Values box 304 provides a single value to the Arguments box 302. The Arguments box 302 in turn supplies the Arguments a.sub.1 and a.sub.n and the value to the Foo box 300. The Select box 306 receives arguments a.sub.1 and a.sub.n, which are passed as arg.sub.1 and arg.sub.n, from the Foo box 300. When arguments are passed in this manner, they are said to be correlated. The body of the Select box 306 performs a SELECT operation and returns its result to the Foo box 300. Upon receiving data from the Arguments box 302 and the Select box 306, the Foo box 300 returns the table t.sub.n+1. FIG. 4 is a flow diagram illustrating the steps performed by the intelligent compiler component 100 upon receiving a query containing a SQL-bodied function. In Block 400, the parser 102 receives a query containing a SQL-bodied function. In block 402, the intelligent compiler component 100, in conjunction with the parser 102, parses the query to generate an internal representation of the query. In particular, the intelligent compiler component 100 contains the functionality to parse the SQL-bodied function. In Block 404, the intelligent compiler component 100 forwards the generated internal representation to other components for further processing to generate an optimal execution plan. Intelligent Compilation of SQL Scripting Language The present invention compiles a scripting language (e.g., SQL scripting language statements) within the parser 102 using the intelligent compiler component 100. Conventional systems that implement the SQL scripting language do not perform parsing of the SQL scripting language statements. Instead, conventional systems perform the extra steps of processing SQL scripting language statements separately from the parser. Unlike conventional systems, the present invention is able to perform the parsing of the SQL scripting language statements in one pass within the parser. The intelligent compiler component 100, in conjunction with the parser 102, parses the query containing SQL scripting language statements. The intelligent compiler component 100 modifies the query graph model 114 that a traditional parser 102 generates. The intelligent compiler component provides the "glue" that modifies the query graph model to reflect the parsing of SQL scripting language statements. The modified query graph model 114 is then ready to be used by the query global semantics component 104, the query rewrite and transform component 106, the plan optimization component 108, and the threaded codegen component 110 to generate an optimal execution plan, without the need for modifying any of these components. The intelligent compiler component 100 enables optimal execution of SQL scripting language statements within the context of queries. The intelligent compiler component 100 addresses simple sequencing of statements, local variables, sequencing with early exit and return, iteration, table return, case statements, shallow and deep correlation, and splitting of common subexpressions. Each of these elements will be discussed below in further detail. In SQL, a sequence can be represented by the following format:
______________________________________
BEGIN ATOMIC
SQL-stmt.sub.1 ;
. . .
SQL-stmt.sub.n ;
END
______________________________________
A sequence is an atomic set of statements. The statements may be any SQL statements, such as SELECT. The intelligent compiler component 100 translates the above sequence into the following compiled SQL form:
______________________________________
SELECT 1 FROM VALUES (1)
WHERE 1 > CASE WHEN EXISTS (SQL-stmt.sub.l WHERE 1 = 0)
THEN 1
. . .
WHEN EXISTS (SQL-stmt.sub.n WHERE 1 = 0)
THEN 1
ELSE 1 END;
______________________________________
The compiled SQL statement incorporates a CASE expression that evaluates the WHERE predicate (1=0) for each record produced by each statement. Since this predicate evaluates to FALSE for every record, the values of an entire statement are exhausted, finally resulting in FALSE for its containing EXISTS clause, causing the processing of the CASE statement to drop to the next WHEN predicate to process the next statement. That is, the CASE statement is generated by the intelligent compiler component 100 so that all statements are processed. The intelligent compiler component 100 translates the sequence into compiled SQL statements that can be understood by the query rewrite and transform component 106, the plan optimization component 108, and the threaded codegen component 110, thereby enabling the generation of an optimal execution plan for the query containing SQL scripting language statements. FIG. 5 illustrates a query graph model generated by the intelligent compiler component 100 that corresponds to the compiled SQL statements for the sequence. The Sequence box 500 receives as input data from the Values box 502 and receives each statement 504, 506. The Sequence box 500 processes each statement and does not return anything (i.e., the return data is empty). In SQL, local variables can be represented by the following format:
______________________________________
DECLARE var.sub.1
. . .
var.sub.n type.sub.x ;
DECLARE var.sub.m,
. . .
var.sub.p type.sub.y ;
______________________________________
The intelligent compiler component 100 translates the above statements into the following compiled SQL form:
______________________________________
SELECT CAST(NULL AS type.sub.1) AS var.sub.1,
. . .
CAST(NULL AS type.sub.p) AS var.sub.p
FROM VALUES (1);
______________________________________
The CAST function ensures that a buffer length of the appropriate length is determined for each variable, depending on its type. In SQL, the use of local variables with a sequence can be represented by the following format:
______________________________________
label: BEGIN ATOMIC
DECLARE var.sub.n type.sub.x ;
DECLARE var.sub.p type.sub.y ;
SQL-stmt.sub.1 ;
. . .
SQL-stmt.sub.n ;
END label
______________________________________
The intelligent compiler component 100 translates the above statements into the following compiled SQL form:
______________________________________
SELECT 1 FROM VarTable
WHERE 1 > CASE WHEN EXISTS (SQL-stmt.sub.l WHERE 1 = 0)
THEN 1
. . .
WHEN EXISTS (SQL-stmt.sub.n WHERE 1 = 0)
THEN 1
ELSE 1 END;
______________________________________
The VarTable is defined by the SELECT statement from the compiled SQL form of the SQL format for defining local variables, discussed above. The compiled SQL statements indicate the amount of memory created for variables through the use of the VarTable (i.e., variable table). FIG. 6 illustrates a query graph model generated by the intelligent compiler component 100 that corresponds to the compiled SQL statements for defining and using local variables. The VarTable box 602 receives input data from the Values box 604 and allocates buffers for variables The Sequence box 600 processes each statement 606, 608 that it receives as input. In SQL, the setting local variables can be represented by the following format:
______________________________________
SET var.sub.1 = expr.sub.1,
. . .
var.sub.n = expr.sub.n ;
______________________________________
The intelligent compiler component 100 translates the above statement into the following compiled SQL form:
______________________________________
SELECT assignment (expr.sub.1, var.sub.1),
. . .
assignment (expr.sub.n, var.sub.n)
FROM VALUES (1)
______________________________________
The assignment statement overwrites the buffer location allocated via the VarTable for the variable (e.g., var.sub.1) by the expression (e.g., expr.sub.1). This sets the variable to the expression. For example, the variable x might be set to 3. Then, an assignment statement would read assignment(3, x), and the value 3 would be written to the buffer location for the variable x. FIG. 7 illustrates a query graph model generated by the intelligent compiler component 100 that corresponds to the compiled SQL statement for the setting of local variables. The Sequence box 700 processes each statement 704, 706, and 708. In particular, the Sequence box 700 processes the assignment statement 706. The variables, whose buffers are allocated via the VarTable 702, are accessed in the Select assignment box 706 through correlation. The Select assignment box processes the SELECT assignment statement and passes the results to the Sequence box 700. In SQL, the return statement can be represented by the following format: RETURN expr; To affect the return, two hidden variables, "return" and "state" are added to the VarTable and initialized to NULL. This is equivalent to having the following implicit definitions:
______________________________________
DECLARE return rType;
state INT;
SET state = NULL,
return = NULL;
______________________________________
When the return statement is encountered, the intelligent compiler component 100 compiles the simple assignment into the following SQL form:
______________________________________
SET state = 0
return = expr;
______________________________________
The intelligent compiler component 100 translates the return statement to a simple assignment statement. Two variables, state and return, are used to return a value. The state variable represents a return code which, in this case, is set to zero. When the state variable has the appropriate return code, the return variable is used to return an expression. FIG. 8 illustrates a query graph model generated by the intelligent compiler component 100 that corresponds to the compiled SQL statements for a return in a sequence. The buffers for the state and return variables are allocated via the TopvarTable box 802. The Select box 804 is added to perform the initialization of the variables and to access the state and return variables via correlation to the TopSeq box 800. The return statement is implemented by the assignment in the subgraph 806, which also accesses the state and return variables via correlation to the TopSeq box 800. In some cases an early exit is required while executing a sequence and when the computer system enters a particular state. In order to handle early exits, the intelligent compiler component 100 generates the following compiled SQL for sequencing:
______________________________________
SELECT 1 FROM VarTable
WHERE 1 > CASE WHEN EXISTS (SQL-stmt.sub.l WHERE 1 = 0)
THEN 1
WHEN state < level.sub.m THEN 1
. . .
WHEN state < level.sub.m THEN 1
WHEN EXISTS (SQL-stmt.sub.n WHERE 1 = 0)
THEN 1 ELSE 1 END;
______________________________________
Typically, a CASE expression used to implement sequencing does not allow for an early exit. However, the intelligent compiler component 100 generates a CASE expression that does allow an early exit. The CASE expression uses a state variable, which represents a return code, and a level.sub.m constant, which identifies a nesting level. The state variable is compared to the level.sub.m constant. When the comparison fails, an early exit is performed. The use of the level.sub.m constant enables identification of the level at which to exit, that is, the level.sub.m constant allows multiple nesting exit control. FIG. 9 illustrates a query graph model generated by the intelligent compiler component 100 that corresponds to the compiled SQL statements for an early exit in a sequence. Buffer space for the variables are allocated in the TopvarTbl box 904. The Sequence box 900 has one statement that it executes, which is from the labelsequ box 902. When there is an early exit, the state variable is set to zero. In SQL, iteration can be represented by the following format:
______________________________________
BEGIN ATOMIC
DECLARE i, sum INT;
SET i = 0, sum = 0;
count:
LOOP
SET i = 1 + 1;
IF i = 5 THEN CONTINUE count
ELSEIF i = 8 THEN LEAVE count END IF;
SET sum = sum + i;
END LOOP count;
END
______________________________________
The CONTINUE predicate exits the body of the loop early and starts it again for the next iteration. The LEAVE predicate breaks out of the loop. Additionally, WHILE or UNTIL predicates can be added to the iteration. The intelligent compiler component 100 translates the above statements into the following compiled SQL form:
______________________________________
(LEAVE label)
SET state = level.sub.m -2;
(CONTINUE label)
SET state = level.sub.m -1;
(Loop)
SELECT 1
FROM VALUES(1)
WHERE EXISTS (body-sequence);
(Body-Sequence)
SELECT 1 FROM Iterator, VarTable
WHERE level.sub.m -1 > CASE
WHEN EXISTS(stmt.sub.l WHERE 1 = 0)
THEN state
WHEN state < level.sub.m THEN state
. . .
ELSE state END
______________________________________
The label for the LOOP is at level.sub.m-1. Hence, when the state is at level.sub.m-1, the intelligent compiler component 100 continues to the next iteration, and when the state is at level.sub.m-2, the intelligent compiler component 100 breaks out of the loop. The iterator can generate an infinite number of integers or it can be a subquery that generates values identifying multiple iterations. The intelligent compiler component 100 generates an optimization so that once an existential is true, an early exit is performed. FIG. 10 illustrates a query graph model generated by the intelligent compiler component 100 that corresponds to the compiled SQL statements for iteration. The BodySeq box 1004 executes for each row generated by the Iterator box 1006, which can either generate an infinite number of integers or could return only those values generated by a user specified SQL query. The VarTable box 1008 allocates buffer space for the hidden return and state variables as well as any other variables explicitly declared by a function. Using the optimization mentioned above, the Loop box 1002 processes the BodySeq box 1004 either until it consumes the records generated by the iterator or until it returns one TRUE value, which occurs only when an early exit or return statement occurs. The assignment in the subgraph 1010 accesses the state variable via correlation and performs an assignment. In SQL, return of a table can be represented by the following format:
______________________________________
CREATE FUNCTION Foo(x INT) RETURNS TABLE (c.sub.1 INT)
LANGUAGE SQL READS SQL DATA
BEGIN ATOMIC
DECLARE var INT;
SET var = 2 * x;
RETURN SELECT c.sub.1 FROM T.sub.1 WHERE var * x < c.sub.1 ;
END
______________________________________
The above function returns a scalar c.sub.1. However, sometimes it is preferable to return a table. The intelligent compiler component 100 is able to compile the function so that a table is returned. FIG. 11 illustrates a query graph model generated by the intelligent compiler component 100 that corresponds to the compiled SQL statements for a table return. The Foo box 1100 is a SELECT box that represents the results of computing the block of statements in the function Foo (i.e., the statements between the BEGIN and END). The SELECT box 1104 implements the block of statements in the function Foo. Buffers for the return and state variables are allocated by the ArgTable box 1102, and buffers for the local variables for the block of statements in the function Foo are allocated by the VarTable box 1106. The SELECT box 1100 that implements the function Foo returns the results of computing the function Foo by Select box 1104. In SQL, a CASE statement can be represented by the following format:
______________________________________
CASE expr.sub.0
WHEN expr.sub.1 THEN stmt.sub.1
. . .
WHEN expr.sub.n THEN stmt.sub.n
ELSE expr.sub.n+1
END CASE
______________________________________
The intelligent compiler component 100 provides support for a CASE statement with the use of CASE expressions that are already supported by the SQL compiler. The statement evaluates expr.sub.0 and executes the stmt.sub.j that corresponds to the first expr.sub.i that equals the result of evaluating expr.sub.0. The intelligent compiler component 100 translates the above statements into the following compiled SQL form that employs the CASE expression supported by the SQL compiler:
______________________________________
SELECT 1 FROM VALUES(1)
WHERE 1 = CASE expr.sub.0
WHEN expr.sub.1
THEN (CASE WHEN EXISTS(stmt.sub.l)
THEN 1 ELSE 1)
. . .
WHEN expr.sub.n
THEN (CASE WHEN EXISTS(stmt.sub.n)
THEN 1 ELSE 1)
ELSE (CASE WHEN EXISTS(stm.sub.n+1)
THEN 1 ELSE 1)
END
______________________________________
FIG. 12 illustrates a query graph model generated by the intelligent compiler component 100 that corresponds to the compiled SQL statements for a CASE statement. The Case box 1200 receives input data from the Values box 1202 and receives statements 1204, 1206, which are processed in the Case box 1200. The following is an example that demonstrates the need for shallow correlation:
______________________________________
CREATE FUNCTION foo( ) RETURNS INT
LANGUAGE SQL EXTERNAL ACTION READS SQL DATA
BEGIN ATOMIC
FOR this AS SELECT * FROM T.sub.1 DO
SELECT sideeff( ) FROM T.sub.2 ;
END FOR;
RETURN 0;
END
______________________________________
The function sideeff has sideeffects and must be reevaluated for each iteration of the loop (i.e., for every row from table T.sub.1). A conventional optimizer does not recognize that the SELECT statement requires multiple execution. However, when the intelligent compiler component 100 recognizes that function Foo has sideeffects, it adds a predicate that forces correlation to a variable whose value changes for each iteration of the loop. The intelligent compiler component 100 translates the above example into the following compiled SQL form:
______________________________________
CREATE FUNCTION foo( ) RETURNS INT
LANGUAGE SQL EXTERNAL ACTION READS SQL DATA
BEGIN ATOMIC
FOR this AS SELECT *, CONSEC( ) AS c
FROM T.sub.1 DO SELECT sideeff( ) FROM T.sub.2
WHERE this.c <> 0;
END FOR;
RETURN 0;
END
______________________________________
The consec() function produces many arbitrary nonzero results that guarantee that each statement will be evaluated. This is especially useful for the UPDATE, DELETE, and INSERT statements, for statements with sideeffects and for statements that use local variables. FIG. 13 illustrates a query graph model generated by the intelligent compiler component 100 that corresponds to the compiled SQL statements for shallow correlation. The BodySeq box 1302 executes for each row generated by the Iterator box 1304 using the results of the Sideeffect and No Sideeffect boxes 1306, 1308, 1310 for use in determining sideeffects for a particular sequence. The Loop box 1300 processes the BodySeq box 1302 until it consumes the records generated by the Iterator box 1304 or until an early exit or return statement occurs. The following is an example that demonstrates a need for deep correlation:
______________________________________
CREATE FUNCTION foo( ) RETURNS INT
LANGUAGE SQL MODIFIES SQL DATA
BEGIN ATOMIC
FOR this AS SELECT * FROM T.sub.1 DO
UPDATE T.sub.2 SET c.sub.2 = c.sub.2 + 1;
INSERT INTO T.sub.3 SELECT * FROM T.sub.2 ;
END FOR;
RETURN 0;
END
______________________________________
This example results in the column T.sub.2.c.sub.2 for each row of table T.sub.2 being incremented for the total number of rows in table T.sub.1. Table T.sub.3 contains all of the intermediate results of this increment. A conventional optimizer does not recognize that reloading table T.sub.2 may be required for each iteration through the loop in order to pickup the new values in table T.sub.2 to insert into table T.sub.3. Actually, the reloading is also required during each iteration so that the UPDATE gets the most recent values of table T.sub.2 when determining its update values. An erroneous execution of this function would cause the UPDATE to increment column T.sub.2.c.sub.2 by some number less than the total number of rows in table T.sub.1. Most likely, the error case would increment exactly once. Therefore, the intelligent compiler component 100 forces this reloading by adding a predicate to the read of table T.sub.2 that accesses a column that cannot be temped. The intelligent compiler component 100 translates the example into the following compiled SQL form:
______________________________________
CREATE FUNCTION foo( ) RETURNS INT
LANGUAGE SQL MODIFIES SQL DATA
BEGIN ATOMIC
FOR this AS SELECT * FROM T.sub.1,
CONSEC( ) AS c DO
UPDATE T.sub.2 SET c.sub.2 = c.sub.2 + 1
WHERE this.c <> T.sub.2.TID;
INSERT INTO T.sub.3 SELECT * FROM T.sub.2 WHERE
this.c <> T.sub.2.TID + 1;
END FOR;
RETURN 0;
END
______________________________________
FIG. 14 illustrates a query graph model generated by the intelligent compiler component 100 that corresponds to the compiled SQL statements for deep correlation. The BodySeq box 1402 sequences the execution of UPDATE AND INSERT statements from the UPDATE box 1406 and the INSERT box 1408 for each iteration through the loop. The Iterator box 1404 in this case is a SELECT box over table T.sub.1 with the CONSEC function as an extra column. Reloading of base table T.sub.2 for SELECT boxes 1410 and 1412 is guaranteed by deep correlation of their tuple-id (TID) columns with the CONSEC function, which is added as a column of the SELECT box 1404. The Loop box 1400 processes the BodySeq box 1402 until it consumes the records generated by the Iterator box 1404 or until an early exit or return statement occurs. Deep and shallow correlation also apply when the function itself is referenced by a loop and included in the context of other SQL statements. The intelligent compiler component 10 recognizes interactions between the compiled in function and the containing query, hence allowing reuse of results where it is semantically safe to do so. The following is an example that demonstrates a need for the splitting of common subexpressions:
______________________________________
CREATE VIEW V.sub.2 AS SELECT * FROM T.sub.2
WHERE c.sub.1 IN (1, 3, 5, 7)
CREATE FUNCTION foo() RETURNS INT
LANGUAGE SQL MODIFIES SQL DATA
BEGIN ATOMIC
INSERT INTO T.sub.3 SELECT * FROM V.sub.2 ;
DELETE FROM T.sub.2 WHERE c.sub.1 > 3;
INSERT INTO T.sub.4 SELECT * FROM V.sub.2 ;
END
______________________________________
In some cases, the intelligent compiler component 100 constructs a query graph that allows the sharing of the results of a view within the query as an optimization. This sharing creates a common subexpression in the query graph. However, it is not always semantically correct to share the results of a view. The query global semantics component 104 determines when this is the case and indicates that the view computation should not be shared. We say that the common subexpression is "split" in these cases. The determination of such splitting (and deep and shallow correlation) is typically performed during the resolution of read/write conflicts, which is more fully described in U.S. Pat. No. 5,546,576 for "Query Optimizer System That Detects and Prevents Mutating Table Violations of Database Integrity in a Query Before Execution Plan Generation," issued to Cochrane et al. on Aug. 13, 1996, and which is incorporated by reference herein. FIG. 15 illustrates a query graph model generated by the intelligent compiler component 100 corresponding to the compiled SQL statements for the above example prior to the splitting of a common subexpression. The ArgTable box 1502 does not allocate space for any buffers since there are no arguments. The body of the function is a Sequence box 1504 for sequencing the three statements in the function. The first Statement box 1506 is an insert into table T.sub.3, the second Statement box 1508 is a delete from table T.sub.2, and the third statement box 1510 is an insert into table T.sub.4. SELECT boxes 1512 and 1516 both SELECT from view V.sub.2, represented by SELECT box 1518 to insert into their respective tables. However, the intervening DELETE box 1508 deletes from table T.sub.2, which is the underlying base table of view V.sub.2. The SELECT box 1514 identifies he rows from table T.sub.2 that should be deleted by the DELETE box 1508. In this example, if the results of view V.sub.2 are computed by the SELECT box 1518 and shared by both SELECT box 1512 and SELECT box 1516, the INSERT box 1510 will not reflect the modifications of the DELETE box 1508. Hence the common subexpression 1518 must be split. FIG. 16 illustrates the query graph model generated by the intelligent compiler component 100 corresponding to the compiled SQL statements for the above example after the splitting of the common subexpression 1518, shown in FIG. 15. Both SELECT box 1618 and SELECT box 1622 compute the results of view V.sub.2. However, since the views are not common, the INSERT box 1610 into table T.sub.4 reflects the modification of the DELETE box 1608. All other boxes are identical in functionality to the boxes of the query graph model illustrated in FIG. 15. FIG. 17 is a flow diagram illustrating the steps performed by the intelligent compiler component 100 upon receiving a query containing SQL scripting language statements. In Block 1700, the intelligent compiler component 100 receives a query containing SQL scripting language statements. In block 1702, the intelligent compiler component 100, in conjunction with the parser 102, parses the query to generate an internal representation of the query. In particular, the intelligent compiler component 100 parses the SQL scripting language statements. In Block 1704, the intelligent compiler component 100 modifies the query graph model to reflect the resolution of sideeffects and read-write conflicts that require shallow correlation, deep correlation, and/or view splitting. In Block 1706, the intelligent compiler component 100 continues processing of the internal representation to generate an optimal execution plan. Conclusion This concludes the description of the preferred embodiment of the invention. The following describes some alternative embodiments for accomplishing the present invention. For example, any type of computer, such as a mainframe, minicomputer, or personal computer, or computer configuration, such as a timesharing mainframe, local area network, or standalone personal computer, could be used with the present invention. In summary, the present invention discloses a method, apparatus, and article of manufacture for an intelligent compiler. The present invention provides optimized execution of procedural functions. The present invention also compiles procedural functions to generate an internal representation that is used in optimizing the execution of the query. The foregoing description of the preferred embodiment of the invention has been presented for the purposes of illustration and description. It is not intended to be exhaustive or to limit the invention to the precise form disclosed. Many modifications and variations are possible in light of the above teaching. It is intended that the scope of the invention be limited not by this detailed description, but rather by the claims appended hereto.
|
Same subclass Same class Consider this |
||||||||||
