Last active
January 22, 2024 12:03
-
-
Save parj/e9d46c437513e30e8977348dee782b60 to your computer and use it in GitHub Desktop.
Example of calcite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.parj.sql; | |
import org.apache.calcite.config.Lex; | |
import org.apache.calcite.sql.*; | |
import org.apache.calcite.sql.fun.SqlStdOperatorTable; | |
import org.apache.calcite.sql.parser.SqlParseException; | |
import org.apache.calcite.sql.parser.SqlParser; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class App { | |
public static void main(String[] args) { | |
parseAndPrint("SELECT column1, column2 FROM table1 WHERE column1 = 42"); | |
parseAndPrint("SELECT column1 FROM table1 WHERE column2 IN (SELECT column2 FROM table2)"); | |
parseAndPrint("SELECT column1 FROM table1 UNION SELECT column1 FROM table2"); | |
parseAndPrint("SELECT t1.column1, t2.column2 FROM table1 t1 JOIN (SELECT column1 FROM table2) t2 ON t1.id = t2.id"); | |
} | |
private static void parseAndPrint(String sqlQuery) { | |
System.out.println("Parsing SQL Query: " + sqlQuery); | |
SqlParser.Config parserConfig = SqlParser.config().withLex(Lex.JAVA); | |
SqlParser sqlParser = SqlParser.create(sqlQuery, parserConfig); | |
try { | |
SqlNode sqlNode = sqlParser.parseQuery(); | |
handleSqlNode(sqlNode); | |
System.out.println("-------------"); | |
} catch (SqlParseException e) { | |
System.out.println("Error parsing query: " + e.getMessage()); | |
System.out.println("-------------"); | |
} | |
} | |
private static void handleSqlNode(SqlNode sqlNode) { | |
SqlNodeHandler handler = HANDLERS.get(sqlNode.getClass()); | |
if (handler != null) { | |
handler.handle(sqlNode); | |
} else { | |
System.out.println("Unsupported query type: " + sqlNode); | |
} | |
} | |
interface SqlNodeHandler { | |
void handle(SqlNode node); | |
} | |
static class SqlSelectHandler implements SqlNodeHandler { | |
@Override | |
public void handle(SqlNode node) { | |
SqlSelect sqlSelect = (SqlSelect) node; | |
System.out.println("Columns: " + sqlSelect.getSelectList()); | |
handleSqlNode(sqlSelect.getFrom()); | |
} | |
} | |
static class SqlBasicCallHandler implements SqlNodeHandler { | |
@Override | |
public void handle(SqlNode node) { | |
SqlBasicCall sqlBasicCall = (SqlBasicCall) node; | |
if (sqlBasicCall.getOperator() == SqlStdOperatorTable.AS) { | |
System.out.println("Alias: " + sqlBasicCall.operand(1)); | |
handleSqlNode(sqlBasicCall.operand(0)); | |
} else if (sqlBasicCall.getOperator() == SqlStdOperatorTable.UNION) { | |
System.out.println("UNION operation detected"); | |
for (SqlNode operand : sqlBasicCall.getOperandList()) { | |
handleSqlNode(operand); | |
} | |
} else { | |
System.out.println("Unsupported operation: " + sqlBasicCall.getOperator()); | |
} | |
} | |
} | |
static class SqlIdentifierHandler implements SqlNodeHandler { | |
@Override | |
public void handle(SqlNode node) { | |
SqlIdentifier sqlIdentifier = (SqlIdentifier) node; | |
List<String> names = sqlIdentifier.names; | |
if (names.size() > 1) { | |
System.out.println("Table: " + names.get(0)); | |
System.out.println("Alias: " + names.get(names.size() - 1)); | |
} else { | |
System.out.println("Table: " + names.get(0)); | |
} | |
} | |
} | |
static class SqlJoinHandler implements SqlNodeHandler { | |
@Override | |
public void handle(SqlNode node) { | |
SqlJoin sqlJoin = (SqlJoin) node; | |
System.out.println("Join detected"); | |
handleSqlNode(sqlJoin.getLeft()); | |
handleSqlNode(sqlJoin.getRight()); | |
} | |
} | |
private static final Map<Class<? extends SqlNode>, SqlNodeHandler> HANDLERS = new HashMap<>(); | |
static { | |
HANDLERS.put(SqlSelect.class, new SqlSelectHandler()); | |
HANDLERS.put(SqlBasicCall.class, new SqlBasicCallHandler()); | |
HANDLERS.put(SqlIdentifier.class, new SqlIdentifierHandler()); | |
HANDLERS.put(SqlJoin.class, new SqlJoinHandler()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment