All Projects → JiamingMai → clickhouse-ast-parser

JiamingMai / clickhouse-ast-parser

Licence: Apache-2.0 license
AST parser and visitor for ClickHouse SQL

Programming Languages

java
68154 projects - #9 most used programming language
ANTLR
299 projects

Projects that are alternatives of or similar to clickhouse-ast-parser

macro-visit
A macro-based generic visitor generator
Stars: ✭ 23 (-61.67%)
Mutual labels:  ast, visitor
pascal-interpreter
A simple interpreter for a large subset of Pascal language written for educational purposes
Stars: ✭ 21 (-65%)
Mutual labels:  ast, visitor
chtable
Grafana's table plugin for ClickHouse
Stars: ✭ 26 (-56.67%)
Mutual labels:  clickhouse
toast
Plugin-driven CLI utility for code generation using Go source as IDL
Stars: ✭ 52 (-13.33%)
Mutual labels:  ast
asmdot
[Unstable] Fast, zero-copy and lightweight (Arm | Mips | x86) assembler in (C | C++ | C# | Go | Haskell | Javascript | Nim | OCaml | Python | Rust).
Stars: ✭ 23 (-61.67%)
Mutual labels:  ast
uptrace
Open source APM: OpenTelemetry traces, metrics, and logs
Stars: ✭ 1,187 (+1878.33%)
Mutual labels:  clickhouse
trickster
Open Source HTTP Reverse Proxy Cache and Time Series Dashboard Accelerator
Stars: ✭ 1,753 (+2821.67%)
Mutual labels:  clickhouse
scope-analyzer
simple scope analysis for javascript ASTs
Stars: ✭ 20 (-66.67%)
Mutual labels:  ast
ts-transform-react-jsx-source
TypeScript AST Transformer that adds source file and line number to JSX elements
Stars: ✭ 12 (-80%)
Mutual labels:  ast
pyCompiler
Python Compiler
Stars: ✭ 13 (-78.33%)
Mutual labels:  visitor
predeclared
Find definitions and declarations in Go source code that shadow predeclared identifiers
Stars: ✭ 26 (-56.67%)
Mutual labels:  ast
c-compiler
A compiler that accepts any valid program written in C. It is made using Lex and Yacc. Returns a symbol table, parse tree, annotated syntax tree and intermediate code.
Stars: ✭ 37 (-38.33%)
Mutual labels:  ast
ClickHouseTools
Инструменты обслуживания и разработки для Yandex ClickHouse, а также другие интересности
Stars: ✭ 16 (-73.33%)
Mutual labels:  clickhouse
code summarization public
source code for 'Improving automatic source code summarization via deep reinforcement learning'
Stars: ✭ 71 (+18.33%)
Mutual labels:  ast
openreplay
📺 OpenReplay is developer-friendly, open-source session replay.
Stars: ✭ 6,131 (+10118.33%)
Mutual labels:  clickhouse
markright
A customizable markdown parser in Elixir: pure pattern matching.
Stars: ✭ 14 (-76.67%)
Mutual labels:  ast
klara
Automatic test case generation for python and static analysis library
Stars: ✭ 250 (+316.67%)
Mutual labels:  ast
freAST
Fast, simple Free Monads using ScalaMeta macro annotations. Port of Freasy-Monad.
Stars: ✭ 14 (-76.67%)
Mutual labels:  ast
ClickHouseMigrator
Help to migrate data to ClickHouse, create database and table auto.
Stars: ✭ 58 (-3.33%)
Mutual labels:  clickhouse
babel-plugin-detective
Babel plugin that scans the AST for require calls and import statements
Stars: ✭ 26 (-56.67%)
Mutual labels:  ast

ClickHouse AST Parser & Visitor

Introduction

ClickHouse AST Parser, it is much more than a parser. It is a convenient toolbox that provides services related to ClickHouse AST. With ClickHouse AST Parser, you are able to easily convert ClickHouse SQL statement to AST (Abstract Syntax Tree), and further utilize the parsing results. You can operate on key objects such as CST, CST parser, CST visitor, AST, AST parser and AST visitor throughout the parsing process.

String sql = "SELECT t1.id, count(1) as total_count FROM my_db1.table1 t1 LEFT JOIN my_db2.table2 t2 ON t1.id = t2.id GROUP BY t1.id";
AstParser astParser = new AstParser();
INode ast = (INode) astParser.parse(sql);

Use cases

Find out related tables

Functions such as hotspot analysis and caching rely on this basic parsing capability.

String sql = "SELECT t1.id, count(1) as total_count FROM my_db1.table1 t1 LEFT JOIN my_db2.table2 t2 ON t1.id = t2.id GROUP BY t1.id";
AstParser astParser = new AstParser();
INode ast = (INode) astParser.parse(sql);
ReferredTablesDetector referredTablesDetector = new ReferredTablesDetector();
// tables should be ["my_db1.table1", "my_db2.table2"] in this case
List<String> tables = referredTablesDetector.searchTables(ast);

Find out related partitions

Related partitions can also be found. But this time we need to implement MetadataService to tell it how to get the metadata since ClickHouse SQL Parser needs to know the partition column name of a specified table.

// we need to implement MetadataService first
MetadataService metadataService = new MetadataService() {
            @Override
            public String getPartitionColName(String tableFullName) {
                // TODO: implement this method
                return null;
            }

            @Override
            public List<String> getTables() {
                // TODO: implement this method
                return null;
            }
        };
String todayDate = "2022-01-01"; // for parsing UDF like today() and yesterday() in the SQL
String targetIP = "127.0.0.1"; // the node to get metadata
ReferredPartitionsDetector referredPartitionsDetector = new ReferredPartitionsDetector(todayDate, targetIp, metadataService);
List<String> partitionRangeList = referredPartitionsDetector.searchTablePartitions(ast);

Extract arguments of Distributed engine

Although Distributed engine is well designed to organize data, sometimes we do need to extract the detail information such as related cluster and local table name. However, it is not convenient to extract them with regular expression because it is easy to make mistake, especially when there are complex comments in a CREATE SQL. ClickHouse SQL Parser solve this problem.

String sql = "CREATE TABLE my_db.my_tbl (date Date, name String) Engine = Distributed('my_cluster', 'my_db', 'my_tbl_local', rand())";
DistributedTableInfoDetector distributedTableInfoDetector = new DistributedTableInfoDetector();
// clusterName is "my_cluster"
String clusterName = distributedTableInfoDetector.searchCluster(sql);
// tableFullName is "my_db.my_tbl_local"
String tableFullName = distributedTableInfoDetector.searchLocalTableFullName(sql);

Rewrite SQL adding "global" keywords

This allows us to optimize a slow SQL by rewriting it. Adding "global" keyword to the SQL is such a case. Develop more rewriters based on this ClickHouse SQL Parser will bring more benefits.

String sql = "SELECT t1.id, count(1) as total_count FROM my_db1.table1 t1 LEFT JOIN my_db2.table2 t2 ON t1.id = t2.id GROUP BY t1.id";
AstParser astParser = new AstParser(false);
SelectUnionQuery ast = (SelectUnionQuery) astParser.parse(sql);
GlobalJoinAstRewriter globalJoinAstRewriter = new GlobalJoinAstRewriter();
String rewrittenSql =  globalJoinAstRewriter.visit((INode) ast);
// the rewritten SQL should be:
// SELECT t1.id, count(1) as total_count FROM my_db1.table1 t1 GLOBAL LEFT JOIN my_db2.table2 t2 ON t1.id = t2.id GROUP BY t1.id
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].