The Forum for Discussion about The Third Manifesto and Related Matters

Please or Register to create posts and topics.

ChatGPT implementation of the relational model.

Page 1 of 3Next

For fun, I thought I'd try to get ChatGPT to implement the relational model, using the same fundamental technologies as Rel.

I asked it this:

Create an implementation of Date and Darwen's relational algebra, written in Java, using the Berkeley DB for persistence.

It gave me this:

Date and Darwen's relational algebra is an extension of Codd's relational algebra that includes additional operations such as natural join, division, and aggregation. To implement this algebra in Java using the Berkeley DB for persistence, we can build on the implementation of Codd's relational algebra and add the new operations.

Here are the additional operations of Date and Darwen's relational algebra:

- Natural join: combines two tables based on common columns.
- Division: computes the quotient of two tables.
- Aggregation: computes summary statistics on a table.

We can implement these operations in Java using the following code:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.DatabaseNotFoundException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;

public class RelationalAlgebra {
    private Environment env;
    private Database table;
    
    public RelationalAlgebra(String dbName) {
        // Create environment configuration
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setAllowCreate(true);
        
        // Open environment and database
        env = new Environment(".", envConfig);
        table = env.openDatabase(null, dbName, null, null);
    }
    
    public void close() throws DatabaseException {
        // Close database and environment
        if (table != null) {
            table.close();
        }
        if (env != null) {
            env.close();
        }
    }
    
    public List<String[]> select(String[] columns, String condition) throws DatabaseException {
        // Initialize result set
        List<String[]> result = new ArrayList<String[]>();
        
        // Open cursor and iterate over records
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        table.openCursor(null, null).forEach(entry -> {
            // Deserialize row
            String[] row = deserialize(entry.getValue());
            
            // Check condition
            if (evaluate(condition, row)) {
                // Project columns
                String[] projectedRow = project(columns, row);
                
                // Add to result set
                result.add(projectedRow);
            }
        });
        
        return result;
    }
    
    public List<String[]> project(String[] columns) throws DatabaseException {
        // Initialize result set
        List<String[]> result = new ArrayList<String[]>();
        
        // Open cursor and iterate over records
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        table.openCursor(null, null).forEach(entry -> {
            // Deserialize row
            String[] row = deserialize(entry.getValue());
            
            // Project columns
            String[] projectedRow = project(columns, row);
            
            // Add to result set
            result.add(projectedRow);
        });
        
        return result;
    }
    
    public List<String[]> union(RelationalAlgebra other) throws DatabaseException {
        // Initialize result set
        List<String[]> result = new ArrayList<String[]>();
        
        // Open cursor and iterate over records
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        table.openCursor(null, null).forEach(entry -> {
            // Deserialize row
            String[] row = deserialize(entry.getValue());
            
            // Add to result set
            result.add(row);
        });
        other.table.openCursor(null, null).forEach(entry -> {
            // Deserialize row
            String[] row = deserialize(entry.getValue());
            
            // Add to result set if not already present

The abrupt ending is where it stopped. I don't know if that's a limitation of the free version, or what, so I thought I'd ask:

You stopped in the middle. Please give the code for the 'union' operator.

It responded:

Something went wrong. If this issue persists please contact us through our help center at help.openai.com.

Ah.

Identifying the flaws, influences, limitations and features of what it did manage to do is left as an exercise for the reader.

 

I'm the forum administrator and lead developer of Rel. Email me at dave@armchair.mb.ca with the Subject 'TTM Forum'. Download Rel from https://reldb.org

Heh heh. (For some definition of "fun".)

[ChatGPT] an extension of Codd's relational algebra that includes additional operations such as natural join, division, and aggregation.

Hmm it wouldn't have occurred to me to describe Tutorial D or even a D-in-general like that. Do D&D call a D a "relational algebra" anywhere? I think the way you put your question might have set the poor thing off on the wrong track. TTM -- the Manifesto itself -- does reference operators of (Codd's) RA, without claiming a D is an algebra as such.

Appendix A does present what it calls an 'Algebra':

[Appx A] The algebra A differs from Codd's original algebra [20-22] in four principal respects: ...

Actually (now that I come to think about it) those four respects are very far from the 'principal' difference. Which is that A (and D) adopt the named perspective from Codd 1970 -- which is not clearly spelled out in that paper, nor anywhere else in precise form from Codd.

[ChatGPT] we can build on the implementation of Codd's relational algebra and add ...

So what does the poor thing mean by "the implementation"? In particular -- as per the discussion at ref [22] in DTATRM -- is there a RENAME operator? And if so where is it getting its definition/implementation from?

Quote from AntC on April 9, 2023, 6:14 am

... I think the way you put your question might have set the poor thing off on the wrong track.

To quote @erwin from a couple of days ago "the problem statement is the problem."

If you'd put your question on any of the RA-oriented forums, I suspect:

a) You wouldn't have got any attempts at an answer nor pointers as to how to make a start.

b) You would have got a load of meta comments [**] suggesting you were using terms of art incorrectly.

The code you did get (before ChatGPT threw a hissy-fit) looks like it's copied from somewhere. Anybody like to guess from where? And does that use named or positional perspective?

And does it really implement every operation by deserializeing each row to a String then return its answers as List<String[]>? Does Java not have some data structuring to represent a Set of Sets of Name-Value pairs? (Or of Sets of Values conforming to some Name-Type schema?)

[**] "meta comments": what the (by now voluminous) commentariat on ChatGPT point out is that the poor thing seems to lack any ability to go meta, or have self-awareness to realise when it's getting teased or being asked to do impossible things. Perhaps that's by design: on the interwebs a sense of humour is a dangerous thing/one person's sense of irony will get 'taken literally'/as an insult by somebody else.

I don't think my programming/systems analysis skills are in any danger of being undercut by no heap of iron.

I'm not sure there is such a thing as "Date and Darwen's algebra", apart perhaps from A, which is clearly not what Dave Voorhis meant and not what ChatGPT thought he meant.

Codd himself introduced the term natural join, which we reduced to just join (in Tutorial D).  He also defined an operator he called divide, but this was extended by Stephen Todd and further extended by us, until we dropped it altogether in TD.

We could said to have added aggregation, I suppose, though we merely copied BS12 which had in turn got it from PRTV.

We don't use the term table.

Hugh

Coauthor of The Third Manifesto and related books.
Quote from Hugh on April 9, 2023, 10:58 am

I'm not sure there is such a thing as "Date and Darwen's algebra", apart perhaps from A, which is clearly not what Dave Voorhis meant and not what ChatGPT thought he meant.

Actually A is what I was thinking of, after asking a series of prior questions that generated missed-the-point answers. I wondered if being more specific would generate better quality answers, which it kind of, almost, not entirely, did not at all.

For example, in a prior question, I asked:

Create an implementation of the relational model, written in Java.

I got this superficial and generally useless result, which is fairly typical:

The relational model is a theoretical framework for organizing and manipulating data in a database. While it is not necessary to implement the entire model in a programming language, we can create a basic implementation of the model's key concepts in Java.

First, we need to define the basic components of the relational model: tables, attributes, and relationships.

A table is a collection of related data organized into rows and columns. In Java, we can represent a table as a class with fields representing its attributes and a collection of instances representing its rows.

An attribute is a characteristic of a table, such as a column name or data type. In Java, we can represent an attribute as a field in a table class.

A relationship is a connection between two tables based on a common attribute. In Java, we can represent a relationship as a field in a table class that references another table class.

Here's an example implementation of the relational model in Java:

public class Employee {
    private int id;
    private String name;
    private String department;
    private double salary;
    private Address address; // Relationship to Address table
    
    // Constructor
    public Employee(int id, String name, String department, double salary, Address address) {
        this.id = id;
        this.name = name;
        this.department = department;
        this.salary = salary;
        this.address = address;
    }
    
    // Getters and setters
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getDepartment() {
        return department;
    }
    
    public void setDepartment(String department) {
        this.department = department;
    }
    
    public double getSalary() {
        return salary;
    }
    
    public void setSalary(double salary) {
        this.salary = salary;
    }
    
    public Address getAddress() {
        return address;
    }
    
    public void setAddress(Address address) {
        this.address = address;
    }
}

public class Address {
    private String street;
    private String city;
    private String state;
    private String zip;
    
    // Constructor
    public Address(String street, String city, String state, String zip) {
        this.street = street;
        this.city = city;
        this.state = state;
        this.zip = zip;
    }
    
    // Getters and setters
    public String getStreet() {
        return street;
    }
    
    public void setStreet(String street) {
        this.street = street;
    }
    
    public String getCity() {
        return city;
    }
    
    public void setCity(String city) {
        this.city = city;
    }
    
    public String getState() {
        return state;
    }
    
    public void setState(String state) {
        this.state = state;
    }
    
    public String getZip() {
        return zip;
    }
    
    public void setZip(String zip) {
        this.zip = zip;
    }
}

In this example, we have two tables: `Employee` and `Address`. The `Employee` table has attributes for `id`, `name`, `department`, `salary`, and a relationship to the `Address` table represented by the `address` field. The `Address` table has attributes for `street`, `city`, `state`, and `zip`.

This is a very basic implementation of the relational model in Java, but it demonstrates how we can use classes to represent tables, attributes, and relationships. Depending on the requirements of the application, we can

Yes, it stopped abruptly in the middle of that sentence.

I find its results almost universally awful; typically superficial or useless or wrong, and often all three. But I regularly see claims that it's wonderful, that it's producing production-quality code and being used to do what would have previously taken considerable effort, etc.

I find such claims baffling, particularly as it has no knowledge of the proprietary schemas, libraries, pre-written classes & functions, business policies, etc., that invariably factor heavily in real enterprise development. Unless its fans are all doing university-assignment style isolated work...?

But that aside, it consistently produces rubbish that needs more time and effort to rewrite than to write. I think the best I got out of it in prior experiments was (a) a fairly complex regex, though no idea if it was correct; and (b) an adequate SQL schema definition for a standard scholastic "bookstore" example.

Everything else, worthless.

Maybe I'm not a good "ChatGPT question engineer."

I'm the forum administrator and lead developer of Rel. Email me at dave@armchair.mb.ca with the Subject 'TTM Forum'. Download Rel from https://reldb.org
Quote from AntC on April 9, 2023, 7:20 am
Quote from AntC on April 9, 2023, 6:14 am

... I think the way you put your question might have set the poor thing off on the wrong track.

To quote @erwin from a couple of days ago "the problem statement is the problem."

If you'd put your question on any of the RA-oriented forums, I suspect:

a) You wouldn't have got any attempts at an answer nor pointers as to how to make a start.

b) You would have got a load of meta comments [**] suggesting you were using terms of art incorrectly.

The code you did get (before ChatGPT threw a hissy-fit) looks like it's copied from somewhere.

Indeed. It's all, in a sense, copied (and translated) from somewhere. That's how it works.

Anybody like to guess from where? And does that use named or positional perspective?

And does it really implement every operation by deserializeing each row to a String then return its answers as List<String[]>? Does Java not have some data structuring to represent a Set of Sets of Name-Value pairs? (Or of Sets of Values conforming to some Name-Type schema?)

Yes, there are numerous better ways of doing that, so many that my brain actually stalls trying to come up with a specific better one to show (so I won't.) But the ChatGPT approach is probably the worst of all options.

[**] "meta comments": what the (by now voluminous) commentariat on ChatGPT point out is that the poor thing seems to lack any ability to go meta, or have self-awareness to realise when it's getting teased or being asked to do impossible things. Perhaps that's by design: on the interwebs a sense of humour is a dangerous thing/one person's sense of irony will get 'taken literally'/as an insult by somebody else.

I don't think my programming/systems analysis skills are in any danger of being undercut by no heap of iron.

There's little mention of it in my work (software engineering consultancy) circles. Much of the buzz seems to come from people who don't do "real" software engineering but need an occasional snippet of code -- for which it might be just the thing -- or those with peripheral involvement and a lot of optimism.

In reality, it's no threat to those of us who program for a living but I hope that it will eventually turn into programming power tools. I'd love to have a tool do coding grunt-work for me, but its current iteration seems to require all the descriptive grunt-work from me only to deliver worthless results in return.

I'm the forum administrator and lead developer of Rel. Email me at dave@armchair.mb.ca with the Subject 'TTM Forum'. Download Rel from https://reldb.org
Quote from AntC on April 9, 2023, 7:20 am

Does Java not have some data structuring to represent a Set of Sets of Name-Value pairs? (Or of Sets of Values conforming to some Name-Type schema?)

Yes.

The TupleBuffer class in SIRA_PRISE holds a Map<NameIdentifier, ValueBuffer>.  NameIdentifier being the Java equivalent for some value of type Name, ValueBuffer being the class for encoded forms of values of any type what so ever.

And the RelationBuffer class has a Set<TupleBuffer>.

Those are oriented toward the physical, there's also classes Tuple and Relation, the latter could include a Set<Tuple> (I no longer know by heart).

Was your question rather rhetoric, by the way ?

Quote from Erwin on April 10, 2023, 10:51 pm

Was your question rather rhetoric, by the way ?

Not entirely, I don't know Java at all.

But yes, mostly: since both you and Dave have implemented a relational engine in Java, I'd expect it can support something better than representing everything as String. We haven't yet figured whether ChatGPT's attempt used explicit Attribute Names in the List<String[]>s or was going positionally -- in which case it has probably failed in the D&D compliance part.

Anyone trying to understand what ChatGPT is doing might want to read this: https://writings.stephenwolfram.com/2023/02/what-is-chatgpt-doing-and-why-does-it-work/.

It's a miracle it ever writes meaningful code, and is probably hopelessly skewed towards nonsense if the training set for the topic is small.

Andl - A New Database Language - andl.org
Quote from dandl on April 13, 2023, 12:49 am

... might want to read this: ....

 

Hmm. I might if it wasn't so flipping long and digressive. And with sneaky puffs for Wolfram. And full of hype (" it’s amazing how human-like the results are" -- most of the claims to that effect I've seen are only wrt really short examples, longer stuff just doesn't seem human-like; "provides perhaps the best impetus we’ve had in two thousand years to understand better just what the fundamental character and principles might be of that central feature of the human condition ...").

I note most of Wolfram is a carefully curated knowledge base, it doesn't merely scrape the web.

And I'm not convinced a game of "produce a “reasonable continuation” " is going to generate sensible Java/any programming language: although there's plenty of { ; }, you can't just strew them anywhere. I guess the code Dave shows was copied holus bolus from somewhere. And given it's trying to promote itself as some sort of programmers' aid, it 'knows' program code exactly isn't "human-like" language.

 

Page 1 of 3Next