diff --git a/AQuery2 b/AQuery2 new file mode 100644 index 0000000..4478769 --- /dev/null +++ b/AQuery2 @@ -0,0 +1,2616 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + GitHub - sunyinqi0508/AQuery2: An in-memory column-store time-series database that uses query compilation + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Skip to content + + + + + + + + + + + + +
+ +
+ + + + + + + +
+ + + + + +
+ + + + + + + + + + + +
+
+
+ + + + + + + + + + + + +
+ +
+ +
+
+ + + + / + + AQuery2 + + + Public +
+ +
+ + + +
+ +
+
+

+ An in-memory column-store time-series database that uses query compilation +

+ +

License

+ + + + +
+
+ +
+ +
+
+ +
+ + + + +
+ + + + + +
+ + + + + + +

sunyinqi0508/AQuery2

+
+ + +
+ + +
+ +
+ + + + + +
+
+ +
+ +
+
+ + + master + + + + +
+
+
+ Switch branches/tags + +
+ + + +
+ +
+ +
+ + +
+ +
+ + + + + + + + + + + + + + + +
+ + +
+
+
+
+ +
+ +
+ + + + +
+ + + + + + + + +
+ Code +
+ +
+
+ +
+ + +
+
+ + + + + + +
+
+

Latest commit

+
+ +
+
 
+
+

Git stats

+ +
+
+
+

Files

+ + + + + Permalink + +
+ + + Failed to load latest commit information. + + + +
+
+
+
Type
+
Name
+
Latest commit message
+
Commit time
+
+ +
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ data +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ docs +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ engine +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ lib +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ monetdb +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ msvs-py +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ sdk +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ server +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ tests +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ LICENSE +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ Makefile +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ README.md +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ build.py +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ csv.h +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ dbconn.py +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ mmw.cpp +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ +
+ prompt.py +
+ +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+
+ +
+ + + +
+
 
+
+ +
+
 
+
+ +
+
+ +
+ +
+ + +
+ + + + +
+ + + +
+

AQuery++ Database

+

Introduction

+

AQuery++ Database is a cross-platform, In-Memory Column-Store Database that incorporates compiled query execution.

+

Requirements

+
    +
  1. +

    Recent version of Linux, Windows or MacOS, with recent C++ compiler that has C++17 (1z) support. (however c++20 is recommended if available for heterogeneous lookup on unordered containers)

    +
      +
    • GCC: 9.0 or above (g++ 7.x, 8.x fail to handle fold-expressions due to a compiler bug)
    • +
    • Clang: 5.0 or above (Recommended)
    • +
    • MSVC: 2019 or later (2022 or above is recommended)
    • +
    +
  2. +
  3. +

    Monetdb for Hybrid Engine

    +
      +
    • On windows, the required libraries and headers are already included in the repo.
    • +
    • On Linux, see Monetdb Easy Setup for instructions.
    • +
    • On MacOS, Monetdb can be easily installed in homebrew brew install monetdb.
    • +
    +
  4. +
  5. +

    Python 3.6 or above and install required packages in requirements.txt by python3 -m pip install -r requirements.txt

    +
  6. +
+

Installation

+

AQuery is tested on mainstream operating systems such as Windows, macOS and Linux

+

Windows

+

There're multiple options to run AQuery on Windows. You can use the native toolchain from Microsoft Visual Studio or gcc from Cygwin/MinGW or run it under Windows Subsystem for Linux.

+
    +
  • +

    For WSL, Docker or Linux virtual machines, see Linux, Docker sections below

    +
  • +
  • +

    For Visual Studio:

    +
      +
    1. Install python3.6 or above from official website or Microsoft Store.
    2. +
    3. Install Microsoft Visual Studio 2022 or later with Desktop development with C++ selected.
    4. +
    5. Clone AQuery repo from Github
    6. +
    7. Install python requirements with pip python3 -m pip install -r requirements.txt
    8. +
    9. Change the build driver from aquery_config.py to "MSBuild"
    10. +
    11. The libraries and headers for Monetdb are already included in msc-plugins, however you can also choose to download them from Monetdb Easy Setup and put them in the same place.
    12. +
    +
  • +
  • +

    For MinGW:

    +
      +
    1. Install gcc and python3 using the package manager. (For Msys2, pacman -S gcc python3)
    2. +
    3. Clone AQuery repo from Github
    4. +
    5. Install python requirements
    6. +
    7. The prebuilt binaries are included in ./lib directory. However, you could also rebuild them from source.
    8. +
    +
  • +
  • +

    Note that it might be possible to use python from python.org or Microsoft store with gcc from MinGW. However, it might not work because of ABI breakage. So the better way is to use gcc with MinGW python from pacman or use clang/MSVC instead.

    +
  • +
+

macOS

+
    +
  • Install a package manager such as homebrew
  • +
  • Install python3 and monetdb using homebrew brew install python3 monetdb
  • +
  • Install C++ compiler come with Xcode commandline tool by xcode-select --install or from homebrew
  • +
  • If you have multiple C++ compilers on the system. Specify C++ compiler by setting the CXX environment variable. e.g. export CXX=clang
  • +
  • Install python packages from requirements.txt
  • +
+

for arm64 macOS users

+
    +
  • In theory, AQuery++ could work on both native arm64 and x86_64 through Rosetta. But for maximum performance, running native is preferred.
  • +
  • However, they can't be mixed up, i.e. make sure every component, python binary, C++ compiler, monetdb library and system commandline utilities such as uname should have the same architecture.
  • +
  • Because I can't get access to an arm-based mac to fully test this setup, there might still be issues. Please open an issue if you encounter any problems.
  • +
+

Linux

+
    +
  • Install monetdb, see Monetdb Easy Setup for instructions.
  • +
  • Install python3, C++ compiler and git. (For Ubuntu, run apt update && apt install -y python3 python3-pip clang-14 libmonetdbe-dev git )
  • +
  • Install required python packages by python3 -m pip install -r requirements.txt
  • +
  • If you have multiple C++ compilers on the system. Specify C++ compiler by setting the CXX environment variable. e.g. export CXX=clang++-14
  • +
+

Docker:

+
    +
  • Alternatively, you can also use docker to run AQuery.
  • +
  • Type make docker to build the docker image from scratch.
  • +
  • For Arm-based Mac users, you would need to build and run the x86_64 docker image because MonetDB doesn't offer official binaries for arm64 Linux.
  • +
+

Usage

+

python3 prompt.py will launch the interactive command prompt. The server binary will be automatically rebuilt and started.

+

Commands:

+
    +
  • +

    <sql statement>: parse AQuery statement

    +
  • +
  • +

    f <filename>: parse all AQuery statements in file

    +
  • +
  • +

    dbg start debugging session

    +
  • +
  • +

    print: printout parsed AQuery statements

    +
  • +
  • +

    xexec: execute last parsed statement(s) with Hybrid Execution Engine. Hybrid Execution Engine decouples the query into two parts. The standard SQL (MonetDB dialect) part is executed by an Embedded version of Monetdb and everything else is executed by a post-process module which is generated by AQuery++ Compiler in C++ and then compiled and executed.

    +
  • +
  • +

    save <OPTIONAL: filename>: save current code snippet. will use random filename if not specified.

    +
  • +
  • +

    exit: quit the prompt

    +
  • +
  • +

    r: run the last generated code snippet

    +
  • +
+

Example:

+

f moving_avg.a
+xexec

+

See ./tests/ for more examples.

+

Architecture

+

Architecture

+

AQuery Compiler

+
    +
  • The query is first processed by the AQuery Compiler which is composed of a frontend that parses the query into AST and a backend that generates target code that delivers the query.
  • +
  • Front end of AQuery++ Compiler is built on top of mo-sql-parsing with modifications to handle AQuery dialect and extension.
  • +
  • Backend of AQuery++ Compiler generates target code dependent on the Execution Engine. It can either be the C++ code for AQuery Execution Engine or sql and C++ post-processor for Hybrid Engine or k9 for the k9 Engine.
  • +
+

Execution Engines

+
    +
  • AQuery++ supports different execution engines thanks to the decoupled compiler structure.
  • +
  • AQuery Execution Engine: executes queries by compiling the query plan to C++ code. Doesn't support joins and udf functions.
  • +
  • Hybrid Execution Engine: decouples the query into two parts. The sql-compliant part is executed by an Embedded version of Monetdb and everything else is executed by a post-process module which is generated by AQuery++ Compiler in C++ and then compiled and executed.
  • +
  • K9 Execution Engine: (discontinued).
  • +
+

Roadmap

+
    +
  • SQL Parser -> AQuery Parser (Front End)
  • +
  • AQuery-C++ Compiler (Back End) +
      +
    • Schema and Data Model
    • +
    • Data acquisition/output from/to csv file
    • +
    +
  • +
  • Execution Engine +
      +
    • Projections and single-group Aggregations
    • +
    • Group by Aggregations
    • +
    • Filters
    • +
    • Order by
    • +
    • Assumption
    • +
    • Flatten
    • +
    • UDFs (Hybrid Engine only)
    • +
    • User Module
    • +
    • Triggers
    • +
    • Join (Hybrid Engine only)
    • +
    • Subqueries
    • +
    +
  • +
  • Query Optimization +
      +
    • Selection/Order by push-down
    • +
    • Join Optimization (Only in Hybrid Engine)
    • +
    +
  • +
+

Known Issues:

+
    +
  • Interval based triggers
  • +
  • Hot reloading server binary
  • +
  • Bug fixes: type deduction misaligned in Hybrid Engine
  • +
  • Investigation: Using postproc only for q1 in Hybrid Engine (make is_special always on)
  • +
  • C++ Meta-Programming: Eliminate template recursions as much as possible.
  • +
  • Functionality: Basic helper functions in aquery
  • +
  • Improvement: More DDLs, e.g. drop table, update table, etc.
  • +
  • Bug: Join-Aware Column management
  • +
  • Bug: Order By after Group By
  • +
+
+
+
+ +
+ + +
+
+ +
+
+
+

About

+ +

+ An in-memory column-store time-series database that uses query compilation +

+ +

Topics

+ + +

Resources

+ + +

License

+ + + + + + + +

Stars

+ + +

Watchers

+ + +

Forks

+ + +
+
+ + + + + + + + + + + +
+
+

Languages

+
+ + + + + +
+ + +
+
+
+
+ +
+ +
+ + +
+ +
+ + +
+
+ +
+ + + + + + + + + + + + + + + + + + + + + diff --git a/MonetDB-release-epel.noarch.rpm b/MonetDB-release-epel.noarch.rpm new file mode 100644 index 0000000..3edafcf Binary files /dev/null and b/MonetDB-release-epel.noarch.rpm differ diff --git a/README.md b/README.md index f1e0cb1..df2e038 100644 --- a/README.md +++ b/README.md @@ -28,21 +28,23 @@ There're multiple options to run AQuery on Windows. You can use the native toolc 2. Install Microsoft Visual Studio 2022 or later with **Desktop development with C++** selected. 3. Clone AQuery repo from [Github](https://github.com/sunyinqi0508/AQuery2) 4. Install python requirements with pip `python3 -m pip install -r requirements.txt` - 5. Change the build driver from aquery_config.py to "MSBuild" + 5. Change the build_driver variable in aquery_config.py to "MSBuild" 6. The libraries and headers for Monetdb are already included in msc-plugins, however you can also choose to download them from [Monetdb Easy Setup](https://www.monetdb.org/easy-setup/) and put them in the same place. - For Winlibs (Recommended): - Download latest winlibs toolchain from the [official website](https://winlibs.com/) - Since winlibs is linked with native windows runtime libraries (UCRT or MSVCRT), it offers better interoperatibility with other libraries built with MSVC such as python and monetdb. - Other steps can be either the same as Visual Studio or Cygwin/Mingw (below) without ABI break. + - Copy or link `mingw64/libexec/gcc///liblto-plugin.dll` to `mingw64/lib/bfd-plugins/` For Link time optimization support on gcc-ar and gcc-ranlib - For CygWin/MinGW: - 1. Install gcc and python3 using its **builtin package manager** instead of the one from python.org or windows store. (For Msys2, `pacman -S gcc python3`) + 1. Install gcc and python3 using its **builtin package manager** instead of the one from python.org or windows store. (For Msys2, `pacman -S gcc python3`). Otherwise, ABI breakage may happen. 2. Clone AQuery repo from Github 3. Install python requirements 4. The prebuilt binaries are included in ./lib directory. However, you could also rebuild them from [source](https://github.com/MonetDB/MonetDB). ### macOS +- If you're using an arm-based mac (e.g. M1, M2 processors). Please go to the Application folder and right-click on the Terminal app, select 'Get Info' and ensure that the 'Open using Rosetta' option is unchecked. See the section below for more notes for arm-based macs. - Install a package manager such as [homebrew](https://brew.sh) - Install python3 and monetdb using homebrew `brew install python3 monetdb` - Install C++ compiler come with Xcode commandline tool by `xcode-select --install` or from homebrew @@ -50,8 +52,10 @@ There're multiple options to run AQuery on Windows. You can use the native toolc - Install python packages from **requirements.txt** **for arm64 macOS users** -- In theory, AQuery++ could work on both native arm64 and x86_64 through Rosetta. But for maximum performance, running native is preferred. -- However, they can't be mixed up, i.e. make sure every component, `python` binary, `C++ compiler`, `monetdb` library and system commandline utilities such as `uname` should have the same architecture. +- In theory, AQuery++ can work on both native arm64 and x86_64 through Rosetta. But for maximum performance, running native is preferred. +- However, they can't be mixed up, i.e. make sure every component, `python` , `C++ compiler`, `monetdb` library and system commandline utilities such as `uname` should have the same architecture. +- Use the script `./arch-check.sh` to check if relevant binaries all have the same architecture. +- In the case where binaries have different architectures, install the software with desired architecture and make an alias or link to ensure the newly installed binary is referred to. - Because I can't get access to an arm-based mac to fully test this setup, there might still be issues. Please open an issue if you encounter any problems. ### Linux @@ -59,19 +63,23 @@ There're multiple options to run AQuery on Windows. You can use the native toolc - Install python3, C++ compiler and git. (For Ubuntu, run `apt update && apt install -y python3 python3-pip clang-14 libmonetdbe-dev git `) - Install required python packages by `python3 -m pip install -r requirements.txt` - If you have multiple C++ compilers on the system. Specify C++ compiler by setting the **CXX** environment variable. e.g. `export CXX=clang++-14` +- Note for anaconda users: the system libraries included in anaconda might differ from the ones your compiler is using. In this case, you might get errors similar to: + >ImportError: libstdc++.so.6: version `GLIBCXX_3.4.26' not found + + In this case, upgrade anaconda or your compiler or use the python from your OS or package manager instead. Or (**NOT recommended**) copy/link the library from your system (e.g. /usr/lib/x86_64-linux-gnu/libstdc++.so.6) to anaconda's library directory (e.g. ~/Anaconda3/lib/). + ### Docker: - Alternatively, you can also use docker to run AQuery. - Type `make docker` to build the docker image from scratch. - - For Arm-based Mac users, you would need to build and run the x86_64 docker image because MonetDB doesn't offer official binaries for arm64 Linux. + - For Arm-based Mac users, you would have to build and run the x86_64 docker image because MonetDB doesn't offer official binaries for arm64 Linux. ## Usage `python3 prompt.py` will launch the interactive command prompt. The server binary will be automatically rebuilt and started. #### Commands: - ``: parse AQuery statement - `f `: parse all AQuery statements in file +- `exec`: execute last parsed statement(s) with Hybrid Execution Engine. Hybrid Execution Engine decouples the query into two parts. The standard SQL (MonetDB dialect) part is executed by an Embedded version of Monetdb and everything else is executed by a post-process module which is generated by AQuery++ Compiler in C++ and then compiled and executed. - `dbg` start debugging session - `print`: printout parsed AQuery statements - -- `xexec`: execute last parsed statement(s) with Hybrid Execution Engine. Hybrid Execution Engine decouples the query into two parts. The standard SQL (MonetDB dialect) part is executed by an Embedded version of Monetdb and everything else is executed by a post-process module which is generated by AQuery++ Compiler in C++ and then compiled and executed. - `save `: save current code snippet. will use random filename if not specified. - `exit`: quit the prompt - `r`: run the last generated code snippet diff --git a/aquery_config.py b/aquery_config.py index 699ad70..c2ede14 100644 --- a/aquery_config.py +++ b/aquery_config.py @@ -2,7 +2,7 @@ ## GLOBAL CONFIGURATION FLAGS -version_string = '0.4.6a' +version_string = '0.4.7a' add_path_to_ldpath = True rebuild_backend = False run_backend = True diff --git a/arch-check.sh b/arch-check.sh new file mode 100644 index 0000000..a472028 --- /dev/null +++ b/arch-check.sh @@ -0,0 +1,21 @@ +ARCH=`uname -m` +ARCH2=`arch` +echo Current architechure: $ARCH $ARCH2 +echo Current shell: $SHELL +PASSED=1 +for i in python3 c++ make ranlib libtool $SHELL +do + FILEPATH=`which $i` + FILEINFO=`file $FILEPATH` + if [[ $FILEINFO =~ $ARCH ]]; then + echo $i@$FILEPATH: passed + else + echo "\033[1;31mERROR\033[0m: Architecture of $i is not $ARCH: $FILEINFO" + PASSED=0 + fi +done + +if [[ PASSED -eq 1 ]]; then + echo "\033[1;32mBinary archtechure check passed\033[0m" +fi + diff --git a/build.py b/build.py index edf09be..172b30c 100644 --- a/build.py +++ b/build.py @@ -94,8 +94,8 @@ class build_manager: ret = True for c in self.build_cmd: if c: - try: - ret = subprocess.call(c, stdout = stdout, stderr = stderr) and ret + try: # only last success matters + ret = not subprocess.call(c, stdout = stdout, stderr = stderr) # and ret except (FileNotFoundError): ret = False pass @@ -205,21 +205,23 @@ class build_manager: self.cache_status.sources or self.cache_status.env ): - self.driver.pch() - self.driver.libaquery_a() - self.driver.server() + success &= self.driver.pch() + success &= self.driver.libaquery_a() + success &= self.driver.server() else: if self.cache_status.libaquery_a: - success = self.driver.libaquery_a() and success + success &= self.driver.libaquery_a() if self.cache_status.pch_hpp_gch: - success = self.driver.pch() and success + success &= self.driver.pch() if self.cache_status.server: - success = self.driver.server() and success + success &= self.driver.server() if success: current.calc(self.cxx, libaquery_a) with open('.cached', 'wb') as cache_sig: cache_sig.write(pickle.dumps(current)) else: + if aquery_config.os_platform == 'mac': + os.system('./arch-check.sh') try: os.remove('./.cached') except: diff --git a/engine/utils.py b/engine/utils.py index 995d354..065f8c8 100644 --- a/engine/utils.py +++ b/engine/utils.py @@ -140,4 +140,7 @@ nullstream = open(os.devnull, 'w') def clamp(val, minval, maxval): - return min(max(val, minval), maxval) \ No newline at end of file + return min(max(val, minval), maxval) + +def escape_qoutes(string : str): + return re.sub(r'^\'', r'\'',re.sub(r'([^\\])\'', r'\1\'', string)) diff --git a/prompt.py b/prompt.py index 91fbbe2..3598c62 100644 --- a/prompt.py +++ b/prompt.py @@ -43,7 +43,7 @@ dbg: print: printout parsed sql statements exec: - execute last parsed statement(s) with AQuery Execution Engine + execute last parsed statement(s) with AQuery Execution Engine (disabled) xexec: execute last parsed statement(s) with Hybrid Execution Engine r: @@ -336,7 +336,7 @@ def prompt(running = lambda:True, next = lambda:input('> '), state = None): time.sleep(.00001) og_q : str = next() q = og_q.lower().strip() - if q == 'exec': # generate build and run (AQuery Engine) + if False and q == 'exec': # generate build and run (AQuery Engine) state.cfg.backend_type = Backend_Type.BACKEND_AQuery.value cxt = engine.exec(state.stmts, cxt, keep) if state.buildmgr.build_dll() == 0: @@ -352,7 +352,7 @@ def prompt(running = lambda:True, next = lambda:input('> '), state = None): else: print(prompt_help) continue - elif q.startswith('xexec'): # generate build and run (MonetDB Engine) + elif q.startswith('xexec') or q.startswith('exec'): # generate build and run (MonetDB Engine) state.cfg.backend_type = Backend_Type.BACKEND_MonetDB.value cxt = xengine.exec(state.stmts, cxt, keep) diff --git a/reconstruct/ast.py b/reconstruct/ast.py index 52dc8ef..44fb969 100644 --- a/reconstruct/ast.py +++ b/reconstruct/ast.py @@ -617,6 +617,7 @@ class join(ast_node): self.join_conditions = [] # self.tmp_name = 'join_' + base62uuid(4) # self.datasource = TableInfo(self.tmp_name, [], self.context) + def append(self, tbls, __alias = ''): alias = lambda t : t + ' ' + __alias if len(__alias) else t if type(tbls) is join: @@ -661,8 +662,11 @@ class join(ast_node): self.have_sep = True j = join(self, node[keys[0]]) tablename = f' {keys[0]} {j}' - if len(keys) > 1 and keys[1].lower() == 'on': - tablename += f' on {expr(self, node[keys[1]])}' + if len(keys) > 1 : + if keys[1].lower() == 'on': + tablename += f' ON {expr(self, node[keys[1]])}' + elif keys[1].lower() == 'using': + tablename += f' USING {expr(self, node[keys[1]])}' self.joins.append((tablename, self.have_sep)) self.tables += j.tables self.tables_dir = {**self.tables_dir, **j.tables_dir} @@ -731,8 +735,9 @@ class join(ast_node): class filter(ast_node): name = 'where' def produce(self, node): - self.add(expr(self, node).sql) - + filter_expr = expr(self, node) + self.add(filter_expr.sql) + self.datasource.join_conditions += filter_expr.join_conditions class create_table(ast_node): name = 'create_table' diff --git a/reconstruct/expr.py b/reconstruct/expr.py index e5980d4..0faf9a5 100644 --- a/reconstruct/expr.py +++ b/reconstruct/expr.py @@ -12,6 +12,11 @@ from engine.types import * class expr(ast_node): name='expr' + valid_joincond = { + 0 : ('and', 'eq', 'not'), + 1 : ('or', 'neq', 'not'), + 2 : ('', '', '') + } @property def udf_decltypecall(self): return self._udf_decltypecall if self._udf_decltypecall else self.sql @@ -46,6 +51,7 @@ class expr(ast_node): self.node = node self.supress_undefined = supress_undefined if(type(parent) is expr): + self.next_valid = parent.next_valid self.inside_agg = parent.inside_agg self.is_udfexpr = parent.is_udfexpr self.is_agg_func = parent.is_agg_func @@ -53,6 +59,8 @@ class expr(ast_node): self.c_code = parent.c_code self.builtin_vars = parent.builtin_vars else: + self.join_conditions = [] + self.next_valid = 0 self.is_agg_func = False self.is_udfexpr = type(parent) is udf self.root : expr = self @@ -92,9 +100,18 @@ class expr(ast_node): else: if len(node) > 1: print(f'Parser Error: {node} has more than 1 dict entry.') - + + is_joincond = False for key, val in node.items(): key = key.lower() + if key not in self.valid_joincond[self.next_valid]: + self.next_valid = 2 + else: + if key == self.valid_joincond[self.next_valid][2]: + self.next_valid = not self.next_valid + elif key == self.valid_joincond[self.next_valid][1]: + self.next_valid = 2 + is_joincond = True if key in self.operators: if key in builtin_func: if self.is_agg_func: @@ -200,6 +217,9 @@ class expr(ast_node): else: print(f'Undefined expr: {key}{val}') + if (is_joincond and len(self.children) == 2 + and all([c.is_ColExpr for c in self.children])) : + self.root.join_conditions.append((c.raw_col for c in self.children)) if type(node) is str: if self.is_udfexpr: @@ -342,9 +362,11 @@ class expr(ast_node): exec(f'loc["{b}"] = lambda : "{b}"') x = self.c_code if c_code is None else c_code + from engine.utils import escape_qoutes if decltypestr: - return eval('f\'' + self.udf_decltypecall + '\'') - return eval('f\'' + self.sql + '\'') + return eval('f\'' + escape_qoutes(self.udf_decltypecall) + '\'') + self.sql.replace("'", "\\'") + return eval('f\'' + escape_qoutes(self.sql) + '\'') if self.is_recursive_call_inudf or (self.need_decltypestr and self.is_udfexpr) or gettype: return call else: diff --git a/test.aquery b/test.aquery index 624e03b..7756ff4 100644 --- a/test.aquery +++ b/test.aquery @@ -1,5 +1,8 @@ #!aquery +select "hello world" +xexec + echo Testing Insert, Filters and Nested Aggregation f stock.a xexec