@Test public void testStringLiteral() { assertCompilationFails("SELECT 'foo' FROM table"); BrokerRequest brokerRequest = COMPILER.compileToBrokerRequest("SELECT SUM('foo'), MAX(\"bar\") FROM table GROUP BY 'foo', \"bar\""); List<AggregationInfo> aggregationInfos = brokerRequest.getAggregationsInfo(); Assert.assertEquals(aggregationInfos.size(), 2); Assert.assertEquals(aggregationInfos.get(0).getExpressions().get(0), "foo"); Assert.assertEquals(aggregationInfos.get(1).getExpressions().get(0), "bar"); List<String> expressions = brokerRequest.getGroupBy().getExpressions(); Assert.assertEquals(expressions.size(), 2); Assert.assertEquals(expressions.get(0), "foo"); Assert.assertEquals(expressions.get(1), "bar"); List<Expression> selectFunctionList = brokerRequest.getPinotQuery().getSelectList(); Assert.assertEquals(selectFunctionList.size(), 2); Assert.assertEquals(selectFunctionList.get(0).getFunctionCall().getOperands().get(0).getLiteral().getStringValue(), "foo"); Assert.assertEquals(selectFunctionList.get(1).getFunctionCall().getOperands().get(0).getLiteral().getStringValue(), "bar"); List<Expression> groupbyList = brokerRequest.getPinotQuery().getGroupByList(); Assert.assertEquals(groupbyList.size(), 2); Assert.assertEquals(groupbyList.get(0).getLiteral().getStringValue(), "foo"); Assert.assertEquals(groupbyList.get(1).getLiteral().getStringValue(), "bar"); brokerRequest = COMPILER.compileToBrokerRequest("SELECT SUM(ADD(foo, 'bar')) FROM table GROUP BY SUB(\"foo\", bar)"); aggregationInfos = brokerRequest.getAggregationsInfo(); Assert.assertEquals(aggregationInfos.size(), 1); Assert.assertEquals(aggregationInfos.get(0).getExpressions().get(0), "add(foo,'bar')"); expressions = brokerRequest.getGroupBy().getExpressions(); Assert.assertEquals(expressions.size(), 1); Assert.assertEquals(expressions.get(0), "sub('foo',bar)"); selectFunctionList = brokerRequest.getPinotQuery().getSelectList(); Assert.assertEquals(selectFunctionList.size(), 1); Assert.assertEquals(selectFunctionList.get(0).getFunctionCall().getOperator(), "SUM"); Assert.assertEquals(selectFunctionList.get(0).getFunctionCall().getOperands().size(), 1); Assert .assertEquals(selectFunctionList.get(0).getFunctionCall().getOperands().get(0).getFunctionCall().getOperator(), "ADD"); Assert.assertEquals( selectFunctionList.get(0).getFunctionCall().getOperands().get(0).getFunctionCall().getOperands().size(), 2); Assert.assertEquals( selectFunctionList.get(0).getFunctionCall().getOperands().get(0).getFunctionCall().getOperands().get(0) .getIdentifier().getName(), "foo"); Assert.assertEquals( selectFunctionList.get(0).getFunctionCall().getOperands().get(0).getFunctionCall().getOperands().get(1) .getLiteral().getStringValue(), "bar"); groupbyList = brokerRequest.getPinotQuery().getGroupByList(); Assert.assertEquals(groupbyList.size(), 1); Assert.assertEquals(groupbyList.get(0).getFunctionCall().getOperator(), "SUB"); Assert.assertEquals(groupbyList.get(0).getFunctionCall().getOperands().size(), 2); Assert.assertEquals(groupbyList.get(0).getFunctionCall().getOperands().get(0).getLiteral().getStringValue(), "foo"); Assert.assertEquals(groupbyList.get(0).getFunctionCall().getOperands().get(1).getIdentifier().getName(), "bar"); }
@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; }