Bug 95174 - SQL parser doesn't support catalog&schema in function name
Summary: SQL parser doesn't support catalog&schema in function name
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Base (show other bugs)
Version:
(earliest affected)
5.0.3.1 rc
Hardware: Other All
: medium normal
Assignee: Devansh Varshney
URL:
Whiteboard:
Keywords: difficultyInteresting, easyHack, skillCpp, skillSql
Depends on:
Blocks:
 
Reported: 2015-10-19 10:35 UTC by Lionel Elie Mamane
Modified: 2024-04-12 07:08 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments
Error banner (32.45 KB, image/png)
2024-03-25 10:31 UTC, Devansh Varshney
Details
Tables with Dummy data (71.97 KB, image/png)
2024-03-25 10:32 UTC, Devansh Varshney
Details
Error with psql (40.37 KB, image/png)
2024-03-25 14:02 UTC, Devansh Varshney
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Lionel Elie Mamane 2015-10-19 10:35:09 UTC
for example

 SELECT schema_name.function_name(a, b) FROM C

while

 SELECT function_name(a, b) FROM C

works
Comment 1 QA Administrators 2016-11-08 11:10:14 UTC Comment hidden (obsolete)
Comment 2 Lionel Elie Mamane 2016-11-08 11:34:41 UTC
In file connectivity/source/parse/sqlbison.y, rule

function_name:
		string_function
	|	date_function
	|	numeric_function
	|	SQL_TOKEN_NAME

add two new cases, something like

	|	SQL_TOKEN_NAME '.' SQL_TOKEN_NAME 
			{$$ = SQL_NEW_RULE;
			$$->append($1);
			$$->append(newNode(".", SQLNodeType::Punctuation));
			$$->append($3);
			}
	|	SQL_TOKEN_NAME '.' SQL_TOKEN_NAME '.' SQL_TOKEN_NAME
			{$$ = SQL_NEW_RULE;
			$$->append($1);
			$$->append(newNode(".", SQLNodeType::Punctuation));
			$$->append($3);
			$$->append(newNode(".", SQLNodeType::Punctuation));
			$$->append($5);}

Then "git grep -E '(function_name|set_fct_spec)" and examine the code that matches this. If it examines one of these nodes, it expects a single token at the function_name place. It must be adapted to expect a token _or_ a node.
Comment 3 Devansh Varshney 2024-03-25 04:57:26 UTC
Since past 2 weeks I am seeing this while building my local build via make for any changes I do -

[CXX] workdir/YaccTarget/connectivity/source/parse/sqlbison.cxx
[CXX] workdir/LexTarget/connectivity/source/parse/sqlflex.cxx
[CXX] connectivity/source/parse/sqliterator.cxx
[CXX] connectivity/source/parse/sqlnode.cxx
/home/devansh/libreoffice/workdir/YaccTarget/connectivity/source/parse/sqlbison.cxx: In static member function ‘static rtl::OString connectivity::OSQLParser::TokenIDToStr(sal_uInt32, const connectivity::IParseContext*)’:
/home/devansh/libreoffice/workdir/YaccTarget/connectivity/source/parse/sqlbison.cxx:1142:6: warning: comparison of unsigned expression in ‘>= 0’ is always true [-Wtype-limits]
 1142 |   (0 <= (YYX) && (YYX) <= YYMAXUTOK                     \
      |    ~~^~~~~~~~
/home/devansh/libreoffice/workdir/YaccTarget/connectivity/source/parse/sqlbison.cxx:10930:32: note: in expansion of macro ‘YYTRANSLATE’
10930 |                 aStr = yytname[YYTRANSLATE(nTokenID)];
      |                                ^~~~~~~~~~~


After looking for the files in the above message I found this bug.(not related to this)



Found these resources -

https://www.oreilly.com/library/view/flex-bison/9780596805418/ch04.html [Parsing SQL]

https://www.oreilly.com/library/view/flex-bison/9780596805418/

https://dev.blog.documentfoundation.org/2023/09/14/catalog-and-schema-support-for-sql-functions-difficulty-interesting-easyhack/


I am currently working on this :)
Comment 4 Devansh Varshney 2024-03-25 10:31:19 UTC
Created attachment 193286 [details]
Error banner

CREATE FUNCTION PUBLIC.GET_NAME(P_ID INT) RETURNS VARCHAR(50)
BEGIN ATOMIC
  DECLARE V_NAME VARCHAR(50);
  SET V_NAME = (SELECT NAME FROM PUBLIC.TABLE1 WHERE ID = P_ID);
  RETURN V_NAME;
END;

I am getting the error while creating the functions in HSQLDB.
Comment 5 Devansh Varshney 2024-03-25 10:32:54 UTC
Created attachment 193287 [details]
Tables with Dummy data


I have already added dummy data for two tables (See the SS) 


I am getting the Error while creating these functions for the HSQLDB

CREATE FUNCTION PUBLIC.GET_NAME(P_ID INT) RETURNS VARCHAR(50)
BEGIN ATOMIC
  DECLARE V_NAME VARCHAR(50);
  SET V_NAME = (SELECT NAME FROM PUBLIC.TABLE1 WHERE ID = P_ID);
  RETURN V_NAME;
END;

CREATE FUNCTION PUBLIC.GET_CITY(P_ID INT) RETURNS VARCHAR(50)
BEGIN ATOMIC
  DECLARE V_CITY VARCHAR(50);
  SET V_CITY = (SELECT CITY FROM PUBLIC.TABLE2 WHERE ID = P_ID);
  RETURN V_CITY;
END;
Comment 6 Devansh Varshney 2024-03-25 14:02:18 UTC
Created attachment 193292 [details]
Error with psql


From my terminal -

postgres=# \c testdb
You are now connected to database "testdb" as user "postgres".
testdb=# \df schema2.get_city
                          List of functions
 Schema  |   Name   | Result data type  | Argument data types | Type 
---------+----------+-------------------+---------------------+------
 schema2 | get_city | character varying | p_id integer        | func
(1 row)

testdb=# \dn
  List of schemas
  Name   |  Owner   
---------+----------
 public  | postgres
 schema1 | postgres
 schema2 | postgres
(3 rows)

testdb=# SELECT schema1.get_name(1);
 get_name 
----------
 Ayushman
(1 row)

testdb=# SELECT nspname FROM pg_namespace WHERE nspname IN ('schema1', 'schema2');
 nspname 
---------
 schema1
 schema2
(2 rows)


But, unable to get same results via the LO Base having this error.
Comment 7 Devansh Varshney 2024-04-12 07:08:47 UTC
Here is the PR related to this Bug -

https://gerrit.libreoffice.org/c/core/+/165279

Need some feedback and if someone can also review it.