@Test public void testConverter() throws IOException { COMPILER.compileToBrokerRequest("Select * from T where a IN (1,2,2,3,4)"); COMPILER.compileToBrokerRequest("SELECT MIN(div(DaysSinceEpoch,2)) FROM mytable"); COMPILER.compileToBrokerRequest( "SELECT SUM(DepDelayMinutes), SUM(ArrDel15), SUM(DepDelay), SUM(DepDel15) FROM myStarTable WHERE Carrier IN ('UA', 'WN', 'FL', 'F9') AND Carrier NOT IN ('EV', 'AS', 'FL') AND DayofMonth > 5 AND DayofMonth <= 17 AND Diverted > 0 AND OriginCityName > 'Detroit, MI' GROUP BY CRSDepTime"); COMPILER.compileToBrokerRequest("Select * from T where a > 1 and a < 10"); COMPILER.compileToBrokerRequest("Select * from T where a between 1 and 10"); BrokerRequest brokerRequest = COMPILER.compileToBrokerRequest("select * from vegetables where name != 'Brussels sprouts'"); Assert.assertEquals(brokerRequest.getPinotQuery().getFilterExpression().getFunctionCall().getOperator(), FilterKind.NOT_EQUALS.name()); final BufferedReader br = new BufferedReader( new InputStreamReader(Pql2CompilerTest.class.getClassLoader().getResourceAsStream("pql_queries.list"))); String pql; int seqId = 0; while ((pql = br.readLine()) != null) { try { LOGGER.info("Trying to compile PQL Id - {}, PQL: {}", seqId++, pql); COMPILER.compileToBrokerRequest(pql); } catch (Exception e) { LOGGER.error("Failed to compile pql {} to BrokerRequest.", pql, e); throw e; } } }
@Override public BrokerRequest compileToBrokerRequest(String expression) throws Pql2CompilationException { try { CharStream charStream = new ANTLRInputStream(expression); PQL2Lexer lexer = new PQL2Lexer(charStream); lexer.setTokenFactory(new CommonTokenFactory(true)); lexer.removeErrorListeners(); lexer.addErrorListener(ERROR_LISTENER); TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer); PQL2Parser parser = new PQL2Parser(tokenStream); parser.setErrorHandler(new BailErrorStrategy()); parser.removeErrorListeners(); parser.addErrorListener(ERROR_LISTENER); ParseTree parseTree = parser.root(); ParseTreeWalker walker = new ParseTreeWalker(); Pql2AstListener listener = new Pql2AstListener(expression); walker.walk(listener, parseTree); AstNode rootNode = listener.getRootNode(); BrokerRequest brokerRequest = new BrokerRequest(); rootNode.updateBrokerRequest(brokerRequest); if (ENABLE_PINOT_QUERY) { try { PinotQuery pinotQuery = new PinotQuery(); rootNode.updatePinotQuery(pinotQuery); if (VALIDATE_CONVERTER) { PinotQuery2BrokerRequestConverter converter = new PinotQuery2BrokerRequestConverter(); BrokerRequest tempBrokerRequest = converter.convert(pinotQuery); boolean result = BrokerRequestComparisonUtils.validate(brokerRequest, tempBrokerRequest); if (!result) { LOGGER.error("Pinot query to broker request conversion failed. PQL:{}", expression); if (FAIL_ON_CONVERSION_ERROR) { throw new Pql2CompilationException( "Pinot query to broker request conversion failed. PQL:" + expression); } } } brokerRequest.setPinotQuery(pinotQuery); } catch (Exception e) { LOGGER.error("Non fatal: Failed to populate pinot query and broker request. PQL:{}", expression, e); if (FAIL_ON_CONVERSION_ERROR) { throw e; } } } return brokerRequest; } catch (Pql2CompilationException e) { throw e; } catch (Exception e) { throw new Pql2CompilationException(ExceptionUtils.getStackTrace(e)); } }
Pql2Compiler implements AbstractCompiler { @Override public BrokerRequest compileToBrokerRequest(String expression) throws Pql2CompilationException { try { CharStream charStream = new ANTLRInputStream(expression); PQL2Lexer lexer = new PQL2Lexer(charStream); lexer.setTokenFactory(new CommonTokenFactory(true)); lexer.removeErrorListeners(); lexer.addErrorListener(ERROR_LISTENER); TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer); PQL2Parser parser = new PQL2Parser(tokenStream); parser.setErrorHandler(new BailErrorStrategy()); parser.removeErrorListeners(); parser.addErrorListener(ERROR_LISTENER); ParseTree parseTree = parser.root(); ParseTreeWalker walker = new ParseTreeWalker(); Pql2AstListener listener = new Pql2AstListener(expression); walker.walk(listener, parseTree); AstNode rootNode = listener.getRootNode(); BrokerRequest brokerRequest = new BrokerRequest(); rootNode.updateBrokerRequest(brokerRequest); if (ENABLE_PINOT_QUERY) { try { PinotQuery pinotQuery = new PinotQuery(); rootNode.updatePinotQuery(pinotQuery); if (VALIDATE_CONVERTER) { PinotQuery2BrokerRequestConverter converter = new PinotQuery2BrokerRequestConverter(); BrokerRequest tempBrokerRequest = converter.convert(pinotQuery); boolean result = BrokerRequestComparisonUtils.validate(brokerRequest, tempBrokerRequest); if (!result) { LOGGER.error("Pinot query to broker request conversion failed. PQL:{}", expression); if (FAIL_ON_CONVERSION_ERROR) { throw new Pql2CompilationException( "Pinot query to broker request conversion failed. PQL:" + expression); } } } brokerRequest.setPinotQuery(pinotQuery); } catch (Exception e) { LOGGER.error("Non fatal: Failed to populate pinot query and broker request. PQL:{}", expression, e); if (FAIL_ON_CONVERSION_ERROR) { throw e; } } } return brokerRequest; } catch (Pql2CompilationException e) { throw e; } catch (Exception e) { throw new Pql2CompilationException(ExceptionUtils.getStackTrace(e)); } } }
Pql2Compiler implements AbstractCompiler { @Override public BrokerRequest compileToBrokerRequest(String expression) throws Pql2CompilationException { try { CharStream charStream = new ANTLRInputStream(expression); PQL2Lexer lexer = new PQL2Lexer(charStream); lexer.setTokenFactory(new CommonTokenFactory(true)); lexer.removeErrorListeners(); lexer.addErrorListener(ERROR_LISTENER); TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer); PQL2Parser parser = new PQL2Parser(tokenStream); parser.setErrorHandler(new BailErrorStrategy()); parser.removeErrorListeners(); parser.addErrorListener(ERROR_LISTENER); ParseTree parseTree = parser.root(); ParseTreeWalker walker = new ParseTreeWalker(); Pql2AstListener listener = new Pql2AstListener(expression); walker.walk(listener, parseTree); AstNode rootNode = listener.getRootNode(); BrokerRequest brokerRequest = new BrokerRequest(); rootNode.updateBrokerRequest(brokerRequest); if (ENABLE_PINOT_QUERY) { try { PinotQuery pinotQuery = new PinotQuery(); rootNode.updatePinotQuery(pinotQuery); if (VALIDATE_CONVERTER) { PinotQuery2BrokerRequestConverter converter = new PinotQuery2BrokerRequestConverter(); BrokerRequest tempBrokerRequest = converter.convert(pinotQuery); boolean result = BrokerRequestComparisonUtils.validate(brokerRequest, tempBrokerRequest); if (!result) { LOGGER.error("Pinot query to broker request conversion failed. PQL:{}", expression); if (FAIL_ON_CONVERSION_ERROR) { throw new Pql2CompilationException( "Pinot query to broker request conversion failed. PQL:" + expression); } } } brokerRequest.setPinotQuery(pinotQuery); } catch (Exception e) { LOGGER.error("Non fatal: Failed to populate pinot query and broker request. PQL:{}", expression, e); if (FAIL_ON_CONVERSION_ERROR) { throw e; } } } return brokerRequest; } catch (Pql2CompilationException e) { throw e; } catch (Exception e) { throw new Pql2CompilationException(ExceptionUtils.getStackTrace(e)); } }  }
Pql2Compiler implements AbstractCompiler { @Override public BrokerRequest compileToBrokerRequest(String expression) throws Pql2CompilationException { try { CharStream charStream = new ANTLRInputStream(expression); PQL2Lexer lexer = new PQL2Lexer(charStream); lexer.setTokenFactory(new CommonTokenFactory(true)); lexer.removeErrorListeners(); lexer.addErrorListener(ERROR_LISTENER); TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer); PQL2Parser parser = new PQL2Parser(tokenStream); parser.setErrorHandler(new BailErrorStrategy()); parser.removeErrorListeners(); parser.addErrorListener(ERROR_LISTENER); ParseTree parseTree = parser.root(); ParseTreeWalker walker = new ParseTreeWalker(); Pql2AstListener listener = new Pql2AstListener(expression); walker.walk(listener, parseTree); AstNode rootNode = listener.getRootNode(); BrokerRequest brokerRequest = new BrokerRequest(); rootNode.updateBrokerRequest(brokerRequest); if (ENABLE_PINOT_QUERY) { try { PinotQuery pinotQuery = new PinotQuery(); rootNode.updatePinotQuery(pinotQuery); if (VALIDATE_CONVERTER) { PinotQuery2BrokerRequestConverter converter = new PinotQuery2BrokerRequestConverter(); BrokerRequest tempBrokerRequest = converter.convert(pinotQuery); boolean result = BrokerRequestComparisonUtils.validate(brokerRequest, tempBrokerRequest); if (!result) { LOGGER.error("Pinot query to broker request conversion failed. PQL:{}", expression); if (FAIL_ON_CONVERSION_ERROR) { throw new Pql2CompilationException( "Pinot query to broker request conversion failed. PQL:" + expression); } } } brokerRequest.setPinotQuery(pinotQuery); } catch (Exception e) { LOGGER.error("Non fatal: Failed to populate pinot query and broker request. PQL:{}", expression, e); if (FAIL_ON_CONVERSION_ERROR) { throw e; } } } return brokerRequest; } catch (Pql2CompilationException e) { throw e; } catch (Exception e) { throw new Pql2CompilationException(ExceptionUtils.getStackTrace(e)); } }  @Override BrokerRequest compileToBrokerRequest(String expression); AstNode parseToAstNode(String expression); TransformExpressionTree compileToExpressionTree(String expression); static AstNode buildAst(String expression); }
Pql2Compiler implements AbstractCompiler { @Override public BrokerRequest compileToBrokerRequest(String expression) throws Pql2CompilationException { try { CharStream charStream = new ANTLRInputStream(expression); PQL2Lexer lexer = new PQL2Lexer(charStream); lexer.setTokenFactory(new CommonTokenFactory(true)); lexer.removeErrorListeners(); lexer.addErrorListener(ERROR_LISTENER); TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer); PQL2Parser parser = new PQL2Parser(tokenStream); parser.setErrorHandler(new BailErrorStrategy()); parser.removeErrorListeners(); parser.addErrorListener(ERROR_LISTENER); ParseTree parseTree = parser.root(); ParseTreeWalker walker = new ParseTreeWalker(); Pql2AstListener listener = new Pql2AstListener(expression); walker.walk(listener, parseTree); AstNode rootNode = listener.getRootNode(); BrokerRequest brokerRequest = new BrokerRequest(); rootNode.updateBrokerRequest(brokerRequest); if (ENABLE_PINOT_QUERY) { try { PinotQuery pinotQuery = new PinotQuery(); rootNode.updatePinotQuery(pinotQuery); if (VALIDATE_CONVERTER) { PinotQuery2BrokerRequestConverter converter = new PinotQuery2BrokerRequestConverter(); BrokerRequest tempBrokerRequest = converter.convert(pinotQuery); boolean result = BrokerRequestComparisonUtils.validate(brokerRequest, tempBrokerRequest); if (!result) { LOGGER.error("Pinot query to broker request conversion failed. PQL:{}", expression); if (FAIL_ON_CONVERSION_ERROR) { throw new Pql2CompilationException( "Pinot query to broker request conversion failed. PQL:" + expression); } } } brokerRequest.setPinotQuery(pinotQuery); } catch (Exception e) { LOGGER.error("Non fatal: Failed to populate pinot query and broker request. PQL:{}", expression, e); if (FAIL_ON_CONVERSION_ERROR) { throw e; } } } return brokerRequest; } catch (Pql2CompilationException e) { throw e; } catch (Exception e) { throw new Pql2CompilationException(ExceptionUtils.getStackTrace(e)); } }  @Override BrokerRequest compileToBrokerRequest(String expression); AstNode parseToAstNode(String expression); TransformExpressionTree compileToExpressionTree(String expression); static AstNode buildAst(String expression); static boolean ENABLE_PINOT_QUERY; static boolean VALIDATE_CONVERTER; static boolean FAIL_ON_CONVERSION_ERROR; }