Subject: rules

From: Stan Yates <[email-masked]>
Date: Thu, 30 Jul 92 10:14:43 -0700

Can you explain the following situation?  The following two relations are
created and populated:

Query sent to backend is "create records(recno=int4,record=text)"

Query sent to backend is "create title(recno=int4,tag=char16,value=text)"

The values in the title relation are as follows and the following
retrieves work ok:

Query sent to backend is "retrieve(title.all)"
-------------------------------------------
| recno       | tag         | value       |
-------------------------------------------
| 3034199     | 245         | 00$aBiography of Imam Taqi.|
-------------------------------------------
| 3034203     | 245         | 10$aAttorney General Yahya Bakhtiar's opening
address in the Supreme Court of Pakistan in the reference by the Islamic
Republic of Pakistan on dissolution of National Awami Party, Rawalpindi, June
19, 20, and 23, 1975.|
-------------------------------------------
| 3034203     | 740         | 01$aAttorney General Yahya Bakhtiar's opening
address in the Supreme Court of Pakistan ...|
-------------------------------------------
| 3034225     | 245         | 10$aCharagh-e-hidayat :$bwith salaat illustrated
/$c[Mirza Bahadur Ali].|
-------------------------------------------
| 3034232     | 245         | 10$aTranslation of the Holy Qur-an from the
original Arabic text with critical essays, life of Muhammad, complete summary of
contents /$cby Hafiz Ghulam Sarwar.|
-------------------------------------------
| 3034245     | 245         | 00$aSind through the centuries :$ban introduction
to Sind, a progressive province of Pakistan.|
-------------------------------------------
| 3034250     | 245         | 00$aMedicinal plants /$cManzoor Ahmad A. Baloch
... [et al].|
-------------------------------------------
| 3034254     | 245         | 00$aShiaism explained :$blight, knowledge, truth.|
-------------------------------------------
| 3034317     | 245         | 10$aImpact of technological changes on per unit
cost and returns in agriculture in Sind Province of Pakistan :$bfinal report,
1974 /$cM. Toaha Qureshi.|
-------------------------------------------
| 3034319     | 245         | 10$aTowards understanding Islam /$cby Sayyid Abul
A0la Maududi ; translated and edited by Khurshid Ahmad.|
-------------------------------------------
| 3034363     | 245         | 10$aKonda phonology,$cby Richard and Edith
Hugoniot.|
-------------------------------------------

Query sent to backend is "retrieve(records.all)where records.recno=3034199"
-----------------------------
| recno       | record      |
-----------------------------
| 3034199     | 00786nam  2200265 i
4500010013000000080041000130050017000540100017000710400018000880200011001060500
01900117082001500136099001400151049000900165245002800174250001200202260004900214
30000250026350000170028850000470030552001020035260000350045465000220048991000090
0511ocm03034199 770511s1975    pk            00000beng  19880908201008.0  a
75930551   aDLCcDLCdCRL  cRs5.000 aBP193.19b.B56  a297/.61aB
a75-930551  aCRLL00aBiography of Imam Taqi.  a1st ed.0 aKarachi
:bPeermahomed Ebrahim Trust,c1975.  aiv, 124 p. ;c19 cm.  aCover title.
aAt head of title: Light, knowledge, truth.8 aStudy of the life of Muhammad
Taqi ibn 0Ali Raza, 810?-835? ninth imam of the Shia sect of
Islam.00aMurhammad al-Jawead,d811-835. 0aImamsxBiography.  arcp1|
-----------------------------

Query sent to backend is "retrieve(title.all)where title.value~"Biography""
-------------------------------------------
| recno       | tag         | value       |
-------------------------------------------
| 3034199     | 245         | 00$aBiography of Imam Taqi.|
-------------------------------------------

Then the following rule is defined:

Query sent to backend is "define rule title2rec is on retrieve to title.recno do
instead retrieve (records.record) where records.recno=current.recno"

Now some strange results of queries:

Query sent to backend is "retrieve(title.all)where title.value~"Biography""
-------------------------------------------
| recno       | tag         | value       |
-------------------------------------------
| 1333144     | 245         | 00$aBiography of Imam Taqi.|
-------------------------------------------

i.e., the rule obviously doesn't work properly--and the recno appears to
be changed.  Now I can't retrieve a title record with either the correct
or the "new" recno:

Query sent to backend is "retrieve(title.all)where title.recno=3034199"
-------------------------------------------
| recno       | tag         | value       |
-------------------------------------------

Go
* retrieve  (title.all)where title.recno=1333144\g

Query sent to backend is "retrieve(title.all)where title.recno=1333144"
-------------------------------------------
| recno       | tag         | value       |
-------------------------------------------

Go

Indeed, the recno for all values in the relation seem to be affected by
this rule:

Query sent to backend is "retrieve(title.all)"
-------------------------------------------
| recno       | tag         | value       |
-------------------------------------------
| 1323688     | 245         | 00$aBiography of Imam Taqi.|
-------------------------------------------
| 1323504     | 245         | 10$aAttorney General Yahya Bakhtiar's opening
address in the Supreme Court of Pakistan in the reference by the Islamic
Republic of Pakistan on dissolution of National Awami Party, Rawalpindi, June
19, 20, and 23, 1975.|
-------------------------------------------
| 1276448     | 740         | 01$aAttorney General Yahya Bakhtiar's opening
address in the Supreme Court of Pakistan ...|
-------------------------------------------
| 1324112     | 245         | 10$aCharagh-e-hidayat :$bwith salaat illustrated
/$c[Mirza Bahadur Ali].|
-------------------------------------------
| 1276128     | 245         | 10$aTranslation of the Holy Qur-an from the
original Arabic text with critical essays, life of Muhammad, complete summary of
contents /$cby Hafiz Ghulam Sarwar.|
-------------------------------------------
| 1326064     | 245         | 00$aSind through the centuries :$ban introduction
to Sind, a progressive province of Pakistan.|
-------------------------------------------
| 1323912     | 245         | 00$aMedicinal plants /$cManzoor Ahmad A. Baloch
... [et al].|
-------------------------------------------
| 1323608     | 245         | 00$aShiaism explained :$blight, knowledge, truth.|
-------------------------------------------
| 1323480     | 245         | 10$aImpact of technological changes on per unit
cost and returns in agriculture in Sind Province of Pakistan :$bfinal report,
1974 /$cM. Toaha Qureshi.|
-------------------------------------------
| 1323672     | 245         | 10$aTowards understanding Islam /$cby Sayyid Abul
A0la Maududi ; translated and edited by Khurshid Ahmad.|
-------------------------------------------
| 1276240     | 245         | 10$aKonda phonology,$cby Richard and Edith
Hugoniot.|
-------------------------------------------

Now when I remove the rule, the title relation is back to normal.

Query sent to backend is "remove rule title2rec"
REMOVE
Go
* retrieve(title.all)\g

Query sent to backend is "retrieve(title.all)"
-------------------------------------------
| recno       | tag         | value       |
-------------------------------------------
| 3034199     | 245         | 00$aBiography of Imam Taqi.|
-------------------------------------------
| 3034203     | 245         | 10$aAttorney General Yahya Bakhtiar's opening
address in the Supreme Court of Pakistan in the reference by the Islamic
Republic of Pakistan on dissolution of National Awami Party, Rawalpindi, June
19, 20, and 23, 1975.|
-------------------------------------------
| 3034203     | 740         | 01$aAttorney General Yahya Bakhtiar's opening
address in the Supreme Court of Pakistan ...|
-------------------------------------------
| 3034225     | 245         | 10$aCharagh-e-hidayat :$bwith salaat illustrated
/$c[Mirza Bahadur Ali].|
-------------------------------------------
| 3034232     | 245         | 10$aTranslation of the Holy Qur-an from the
original Arabic text with critical essays, life of Muhammad, complete summary of
contents /$cby Hafiz Ghulam Sarwar.|
-------------------------------------------
| 3034245     | 245         | 00$aSind through the centuries :$ban introduction
to Sind, a progressive province of Pakistan.|
-------------------------------------------
| 3034250     | 245         | 00$aMedicinal plants /$cManzoor Ahmad A. Baloch
... [et al].|
-------------------------------------------
| 3034254     | 245         | 00$aShiaism explained :$blight, knowledge, truth.|
-------------------------------------------
| 3034317     | 245         | 10$aImpact of technological changes on per unit
cost and returns in agriculture in Sind Province of Pakistan :$bfinal report,
1974 /$cM. Toaha Qureshi.|
-------------------------------------------
| 3034319     | 245         | 10$aTowards understanding Islam /$cby Sayyid Abul
A0la Maududi ; translated and edited by Khurshid Ahmad.|
-------------------------------------------
| 3034363     | 245         | 10$aKonda phonology,$cby Richard and Edith
Hugoniot.|
-------------------------------------------

What I want to do is be able to do a search on the value in the title relation
(eventually the format of this value will be refined and it will be indexed)
and then retrieve the corresponding complete record.  Obviously this could
be done using a join, but I am experimenting with rules to see if that is
more efficient.  In any case, I would like to get the rule to work.  Have
I defined it incorrectly?

Thanks for any assistance.

---

From: Spyros Potamianos <[email-masked]>
Date: Thu, 30 Jul 92 11:07:05 PDT

> [.......... stuff deleted .............]
>

The problem with this rule is that 'title.recno' is an 'int4' and
'records.record' is 'text'. So, when the rule is activated at query
execution time, it tries to replace an integer with a text.
Unfortunatelly type checking in the rule manager leaves a lot to be
desired, so the system tries to use the text value as an integer,
and of course strange things started happening!!!

However, it is possible to use a "rewrite" rule:

	define rewrite rule title2rec is
	on retrieve to title.recno
	do instead retrieve (records.record)
	where records.recno=current.recno"

The advantage is that the rule processing happens before an execution plan
is created, and the system knows what types of data to expect.
However under the current implementation of the query rewrite rule system
you will not get any tuples of 'title' that have no corresponding entry
in 'records' (i.e. when the join 'records.recno=current.recno' fails)

> Thanks for any assistance.

I would suggest to either use a good old-fashioned join, or define an extra
dummy attribute on 'title':

    create title(recno=int4,tag=char16,value=text, rec=text)

and define a rule:

	define rule title2rec is
	on retrieve to title.rec
	do instead retrieve (records.record)
	where records.recno=current.recno"

(thus you are replacing the value of a 'text' attribute with the value of
another 'text' attribute, and you won't have problems with type mismatches)

Hope that helps!
Spyros Potamianos
[email-masked]