Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 334aff9

Browse files
committedNov 14, 2023
now with sql
1 parent abe7a07 commit 334aff9

File tree

9 files changed

+114
-43
lines changed

9 files changed

+114
-43
lines changed
 

‎CMakeLists.txt

+36
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ endif()
2020
option(LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." ON)
2121
option(LLVM_BUILD_TOOLS "Build the LLVM tools. If OFF, just generate build targets." ON)
2222

23+
option(ENABLE_SQL "Build SQL dialect" OFF)
24+
2325
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
2426
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
2527

@@ -104,6 +106,40 @@ set(LLVM_LIT_ARGS "-sv" CACHE STRING "lit default options")
104106
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules")
105107
include(sanitizers)
106108

109+
# if (ENABLE_SQL)
110+
include(FetchContent)
111+
include(ExternalProject)
112+
113+
FetchContent_Declare(sqlparser_ext
114+
GIT_REPOSITORY https://github.com/wsmoses/sql-parser
115+
GIT_TAG c2471248cef8cd33081e698e8ac65d691283dbd4
116+
)
117+
118+
FetchContent_GetProperties(sqlparser_ext)
119+
120+
FetchContent_MakeAvailable(sqlparser_ext)
121+
122+
ExternalProject_Add(sqlparser
123+
SOURCE_DIR ${sqlparser_ext_SOURCE_DIR}
124+
INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/sql/install
125+
CONFIGURE_COMMAND ""
126+
BUILD_COMMAND ${CMAKE_COMMAND} -E env
127+
CXX=${CMAKE_CXX_COMPILER}
128+
make static=yes -C ${sqlparser_ext_SOURCE_DIR}
129+
BUILD_IN_SOURCE TRUE
130+
INSTALL_COMMAND ""
131+
BUILD_BYPRODUCTS ${sqlparser_ext_SOURCE_DIR}/libsqlparser.a
132+
)
133+
134+
135+
add_library(sqlparse_lib INTERFACE)
136+
137+
target_include_directories(sqlparse_lib INTERFACE "${sqlparser_ext_SOURCE_DIR}/src")
138+
target_link_libraries(sqlparse_lib INTERFACE ${sqlparser_ext_SOURCE_DIR}/libsqlparser.a)
139+
add_dependencies(sqlparse_lib sqlparser)
140+
141+
# endif()
142+
107143
add_subdirectory(include)
108144
add_subdirectory(lib)
109145
add_subdirectory(tools)

‎include/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
add_subdirectory(polygeist)
2-
add_subdirectory(sql)
2+
if (ENABLE_SQL)
3+
add_subdirectory(sql)
4+
endif()

‎include/sql/Parser.h

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99

10-
11-
1210
#ifndef SQLPARSER_H
1311
#define SQLPARSER_H
1412

‎include/sql/SQLOps.td

+23
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ def WhereOp: SQL_Op<"where", [Pure]> {
4949
let hasCanonicalizer = 0;
5050
}
5151

52+
def BoolArithOp: SQL_Op<"bool_arith", [Pure]> {
53+
let summary = "bool_arith op";
54+
55+
let arguments = (ins SQLBoolType:$left, SQLBoolType:$right, StrAttr:$op);
56+
let results = (outs SQLBoolType:$result);
57+
58+
let hasFolder = 0;
59+
let hasCanonicalizer = 0;
60+
}
61+
5262
def CalcBoolOp: SQL_Op<"calc_bool", [Pure]> {
5363
let summary = "calc_bool op";
5464

@@ -59,6 +69,19 @@ def CalcBoolOp: SQL_Op<"calc_bool", [Pure]> {
5969
let hasCanonicalizer = 0;
6070
}
6171

72+
73+
74+
75+
def ArithOp: SQL_Op<"arith", [Pure]> {
76+
let summary = "arith op";
77+
78+
let arguments = (ins SQLExprType:$left, SQLExprType:$right, StrAttr:$op);
79+
let results = (outs SQLExprType:$result);
80+
81+
let hasFolder = 0;
82+
let hasCanonicalizer = 0;
83+
}
84+
6285
def AndOp: SQL_Op<"and", [Pure]> {
6386
let summary = "and op";
6487

‎lib/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
add_subdirectory(polygeist)
2-
add_subdirectory(sql)
2+
if (ENABLE_SQL)
3+
add_subdirectory(sql)
4+
endif()

‎lib/sql/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Types.cpp
33
Dialect.cpp
44
Ops.cpp
55
Parser.cpp
6+
NewParser.cpp
67

78

89
ADDITIONAL_HEADER_DIRS
@@ -13,7 +14,7 @@ MLIRSQLOpsIncGen
1314
# MLIRSQLTypesIncGen
1415

1516
LINK_LIBS PUBLIC
16-
MLIRIR
17+
MLIRIR sqlparse_lib
1718
)
1819

1920
add_subdirectory(Passes)

‎test_with_pragma.c

+38-33
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,57 @@
1+
#include <libpq-fe.h>
12
#include <stdio.h>
23
#include <stdlib.h>
3-
#include <libpq-fe.h>
44

55
// PGresult *PQexec(PGconn*, const char* command);
66
// PQgetvalue
7-
// %7 = call @PQexec(%2, %6) : (memref<?x1xi8>, memref<?xi8>) -> memref<?x1xi8>
8-
#pragma lower_to(num_rows_fn, "sql.num_results")
9-
int num_rows_fn(size_t);// char*
7+
// %7 = call @PQexec(%2, %6) : (memref<?x1xi8>, memref<?xi8>) ->
8+
// memref<?x1xi8>
9+
// #pragma lower_to(num_rows_fn, "sql.num_results")
10+
// int num_rows_fn(size_t);// char*
1011

11-
#pragma lower_to(get_value_fn_int, "sql.get_value")
12-
int get_value_fn_int(size_t, int, int);
12+
// #pragma lower_to(get_value_fn_int, "sql.get_value")
13+
// int get_value_fn_int(size_t, int, int);
1314

14-
#pragma lower_to(get_value_fn_double, "sql.get_value")
15-
double get_value_fn_double(size_t, int, int);
15+
// #pragma lower_to(get_value_fn_double, "sql.get_value")
16+
// double get_value_fn_double(size_t, int, int);
1617

18+
// #pragma lower_to(execute, "sql.execute")
19+
// PGresult* execute(size_t, char*);
1720

1821
void do_exit(PGconn *conn) {
19-
PQfinish(conn);
20-
exit(1);
22+
PQfinish(conn);
23+
exit(1);
2124
}
2225

2326
int main() {
24-
25-
PGconn *conn = PQconnectdb("user=janbodnar dbname=testdb");
26-
27-
if (PQstatus(conn) == CONNECTION_BAD) {
28-
29-
fprintf(stderr, "Connection to database failed: %s\n",
30-
PQerrorMessage(conn));
31-
do_exit(conn);
32-
}
3327

34-
PGresult *res = PQexec(conn, "SELECT VERSION()");
35-
36-
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
28+
PGconn *conn = PQconnectdb("user=carl dbname=testdb");
3729

38-
printf("No data retrieved\n");
39-
PQclear(res);
40-
do_exit(conn);
41-
}
42-
43-
printf("%s\n", PQgetvalue(res, 0, 0));
44-
printf("%d\n", get_value_fn_int((size_t)res, 0, 0));
45-
printf("%d\n", num_rows_fn((size_t)res));
46-
// res, 0, 0));
30+
if (PQstatus(conn) == CONNECTION_BAD) {
4731

32+
fprintf(stderr, "Connection to database failed: %s\n",
33+
PQerrorMessage(conn));
34+
do_exit(conn);
35+
}
36+
37+
// PGresult *res = PQexec(conn, "SELECT 17");
38+
PGresult *res = PQexec(conn, "SELECT a FROM table1");
39+
PGresult *res1 = PQexec(conn, "SELECT * FROM table1 WHERE b > 10 OR c < 10 AND a <= 20");
40+
PGresult *res2 = PQexec(conn, "SELECT * FROM table1 WHERE b > 10 AND c < 10");
41+
PGresult *res3 = PQexec(conn, "SELECT b, c FROM table1 WHERE a <= 10 LIMIT 10");
42+
// PGresult *res3 = PQexec(conn, "SELECT b, c FROM table1 LIMIT ALL");
43+
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
44+
45+
printf("No data retrieved\n");
4846
PQclear(res);
49-
PQfinish(conn);
47+
do_exit(conn);
48+
}
49+
50+
PQclear(res);
51+
PQclear(res1);
52+
PQclear(res2);
53+
PQclear(res3);
54+
PQfinish(conn);
5055

51-
return 0;
56+
return 0;
5257
}

‎tools/cgeist/CMakeLists.txt

+5-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ target_compile_definitions(cgeist PUBLIC -DLLVM_OBJ_ROOT="${LLVM_BINARY_DIR}")
5151
target_link_libraries(cgeist PRIVATE
5252
MLIRSCFTransforms
5353
MLIRPolygeist
54-
MLIRSQL
55-
5654
MLIRSupport
5755
MLIRIR
5856
MLIRAnalysis
@@ -69,7 +67,6 @@ target_link_libraries(cgeist PRIVATE
6967
MLIRMathToLLVM
7068
MLIRTargetLLVMIRImport
7169
MLIRPolygeistTransforms
72-
MLIRSQLTransforms
7370
MLIRLLVMToLLVMIRTranslation
7471
MLIRSCFToOpenMP
7572
MLIROpenMPToLLVM
@@ -86,6 +83,11 @@ target_link_libraries(cgeist PRIVATE
8683
clangLex
8784
clangSerialization
8885
)
86+
87+
if (ENABLE_SQL)
88+
target_link_libraries(cgeist PRIVATE MLIRSQLTransforms MLIRSQL)
89+
endif()
90+
8991
add_dependencies(cgeist MLIRPolygeistOpsIncGen MLIRPolygeistPassIncGen)
9092
add_dependencies(cgeist MLIRSQLOpsIncGen MLIRSQLPassIncGen)
9193
add_subdirectory(Test)

‎tools/polygeist-opt/CMakeLists.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ set(LIBS
66
MLIROptLib
77
MLIRPolygeist
88
MLIRPolygeistTransforms
9-
MLIRSQLTransforms
10-
MLIRSQL
119
)
10+
if (ENABLE_SQL)
11+
set(LIBS ${LIBS} MLIRSQLTransforms MLIRSQL)
12+
endif()
13+
1214
add_llvm_executable(polygeist-opt polygeist-opt.cpp)
1315

1416
install(TARGETS polygeist-opt

0 commit comments

Comments
 (0)
Please sign in to comment.