CQL unknown procedure {}/1

Hello,
I’m trying to use CQL library to query SQL database, but getting error like this:

?- use_module(library(cql/cql)).
true.

?- {[], se_lt_x :: [a-A, b-B, c-C]}.
ERROR: Unknown procedure: {}/1 (DWIM could not correct goal)

I’m getting the same error even if i set proper connection and schema using directives:

:- use_module(library(cql/cql_autoschema)).
:-register_database_connection_details(+Schema, +ConnectionInfo).
:-build_schema(+Schema).

Tried with SWI-Prolog 8.2.1 on Ubuntu.

What is the proper way to set CQL for simple querying? Thanks.

1 Like

Since I have a potential interest in using CQL in the future I am looking at your question, but note that I have never used CQL before.

In looking at the code on GitHub noticed a demo file. Have you run this?

I am trying to figure out how to create the database with PostgreSQL to use for the demo. If you have figured this out and can post here that will save me some effort.


Personal Notes

To create database for use with CQL demo. Based on instructions in cql_demo.pl

Using PostgreSQL on Ubuntu 20.04 on WSL 2
Using psql

  1. Start psql.
root@galaxy:/mnt/c/Users/Groot# sudo -u postgres psql

Note: psql commands are case sensitive so CREATE is not a valid command.
2. Create empty database.

postgres=# create database cql_demo;
  1. Verify database created.
postgres=# \l
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges
-----------+----------+----------+---------+---------+-----------------------
 cql_demo  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
(4 rows)
  1. Install extension.
postgres=# create extension "uuid-ossp";
CREATE EXTENSION
  1. Verify extension installed.
postgres=# \dx
                            List of installed extensions
   Name    | Version |   Schema   |                   Description
-----------+---------+------------+-------------------------------------------------
 plpgsql   | 1.0     | pg_catalog | PL/pgSQL procedural language
 uuid-ossp | 1.1     | public     | generate universally unique identifiers (UUIDs)
(2 rows)
  1. Change to the cql_demo databse.
postgres=# \c cql_demo
You are now connected to database "cql_demo" as user "postgres".
  1. Verify the cql_demo database has no tables.
cql_demo=# \dt
Did not find any relations.
  1. Add the table cql_table_1.
cql_demo=# create table cql_table_1(cql_table_1_pk SERIAL PRIMARY KEY, varchar_column varchar(30));
CREATE TABLE
  1. Verify table cql_table_1 created.
cql_demo=# \dt
            List of relations
 Schema |    Name     | Type  |  Owner
--------+-------------+-------+----------
 public | cql_table_1 | table | postgres
(1 row)
  1. Add the table cql_table_2 and verify creation.
cql_demo=# create table cql_table_2(cql_table_2_pk SERIAL PRIMARY KEY, varchar_column varchar(30), decimal_column DECIMAL(30,10));
CREATE TABLE
cql_demo=# \dt
            List of relations
 Schema |    Name     | Type  |  Owner
--------+-------------+-------+----------
 public | cql_table_1 | table | postgres
 public | cql_table_2 | table | postgres
(2 rows)
  1. Create new user so that postgres does not own the database.
postgres=# create user eric with password 'password';
CREATE ROLE
  1. Verify user created.
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 eric      |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
  1. Make user eric owner of database cql_demo and verify.
postgres=# alter database cql_demo owner to eric;
ALTER DATABASE
postgres=# \l
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges
-----------+----------+----------+---------+---------+-----------------------
 cql_demo  | eric     | UTF8     | C.UTF-8 | C.UTF-8 |
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
(4 rows)
  1. Create entry in odbc.ini
    See: SWI-Prolog connecting to PostgreSQL via ODBC
root@galaxy:~# sudo nano /etc/odbc.ini

FIle: /etc/odbc.ini

[SWI-Prolog CQL]
Description = SWI-Prolog CQL example
Driver      = PostgreSQL Unicode
Servername  = localhost
Database    = cql_demo
UserName    = eric
Password    = password
Port        = 5432
  1. Verify connection possible using ODBC.
root@WINDOWS-6F874NS:~# iusql -v "SWI-Prolog CQL"
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> quit
2 Likes

Thank you Eric!

I didn’t noticed the demo file. Based on this I have the minimal working example for local PostgreSQL now.
I’ve switched from ident to md5 authentication, created swipl user and database and table person.

:-use_module(library(cql/cql)).                                                  
  1 :-use_module(library(cql/cql_autoschema)).                                       
  2                                                                                  
  3 :-cql_option(default_schema(public)).                                               
  4                                             
  5                                                                                  
  6 :-initialization(register_database_connection_details(public, driver_string('Driver={PostgreSQL Unicode};Server=localhost;Database=swipl;Uid=swipl;Pwd=swipl;')), now).
  7 :-build_schema(public).                                                             
  8                                                                                  
  9 select_test :-                                                                   
 10   {[], person :: [name-Name]},                                                   
 11   writeln(Name). 

It works OK.

?- select_test.
jack
true .

But I’m getting error with remote MS SQL server

ERROR: Prolog initialisation failed:
ERROR: Unknown attribute in CQL attribute list: dbo:person:name

I will post the minimal example for MS SQL as well when/if I’ll figure it out.

1 Like

I can’t edit my previous post yet, so posting it here.

The default schema in the example for PostgreSQL should be public. The dbo schema is default in MS SQL.
The :-cql_option(max_db_connections(10)). is not necessary for the minimal example.

The MS SQL connection doesn’t work yet, because there are no definitions of schema_fact/3 for MS SQL in cql_autoschema.pl.
The solution is pretty simple. The queries could be exactly the same for both PostgreSQL and MS SQL except for different default schema.

However, even if we define the schema in three places in cql_option/1, register_database_connection_details/2 and build_schema/1, the library is not actually using it properly. It is still using most of the time the default schema only. Part of the problem is directly in schema_fact/3, where it is using public schema.

I’ll try to dig in a little bit more and maybe try to create some pull request later.

1 Like

I bumped up your trust level so it should work now.

1 Like