public static String enhanceXPath(final String prefix, final int index, final String xpath) { if (log.isDebugEnabled()) { log.debug("prefix: " + prefix); log.debug("index: " + index); log.debug("xpath: " + xpath); } final ANTLRStringStream stringStream = new ANTLRStringStream(xpath); final XPathEnhancerLexer xl = new XPathEnhancerLexer(stringStream); final TokenRewriteStream trs = new TokenRewriteStream(xl); final XPathEnhancerParser xp = new XPathEnhancerParser(trs, index, prefix); try { xp.main(); } catch (RecognitionException e) { throw new IllegalArgumentException(xpath + " is no valid XPath expression", e); } final String enhanced = trs.toString(); return enhanced; }
public ASTNode parseExpression(String command) throws ParseException { LOG.debug("Parsing expression: {}", command); HiveLexerX lexer = new HiveLexerX(new ANTLRNoCaseStringStream(command)); TokenRewriteStream tokens = new TokenRewriteStream(lexer); HiveParser parser = new HiveParser(tokens); parser.setTreeAdaptor(adaptor); HiveParser_IdentifiersParser.expression_return r = null; try { r = parser.expression(); } catch (RecognitionException e) { throw new ParseException(parser.errors); } if (lexer.getErrors().size() == 0 && parser.errors.size() == 0) { LOG.debug("Parse Completed"); } else if (lexer.getErrors().size() != 0) { throw new ParseException(lexer.getErrors()); } else { throw new ParseException(parser.errors); } return (ASTNode) r.getTree(); }
public ASTNode parseHint(String command) throws ParseException { LOG.debug("Parsing hint: {}", command); HiveLexerX lexer = new HiveLexerX(new ANTLRNoCaseStringStream(command)); TokenRewriteStream tokens = new TokenRewriteStream(lexer); HintParser parser = new HintParser(tokens); parser.setTreeAdaptor(adaptor); HintParser.hint_return r = null; try { r = parser.hint(); } catch (RecognitionException e) { throw new ParseException(parser.errors); } if (lexer.getErrors().size() == 0 && parser.errors.size() == 0) { LOG.debug("Parse Completed"); } else if (lexer.getErrors().size() != 0) { throw new ParseException(lexer.getErrors()); } else { throw new ParseException(parser.errors); } return (ASTNode) r.getTree(); }
public ASTNode parseSelect(String command, Context ctx) throws ParseException { LOG.debug("Parsing command: {}", command); HiveLexerX lexer = new HiveLexerX(new ANTLRNoCaseStringStream(command)); TokenRewriteStream tokens = new TokenRewriteStream(lexer); if (ctx != null) { ctx.setTokenRewriteStream(tokens); } HiveParser parser = new HiveParser(tokens); parser.setTreeAdaptor(adaptor); HiveParser_SelectClauseParser.selectClause_return r = null; try { r = parser.selectClause(); } catch (RecognitionException e) { throw new ParseException(parser.errors); } if (lexer.getErrors().size() == 0 && parser.errors.size() == 0) { LOG.debug("Parse Completed"); } else if (lexer.getErrors().size() != 0) { throw new ParseException(lexer.getErrors()); } else { throw new ParseException(parser.errors); } return r.getTree(); } public ASTNode parseExpression(String command) throws ParseException {
public ASTNode parseTriggerActionExpression(String command) throws ParseException { HiveLexerX lexer = new HiveLexerX(new ANTLRNoCaseStringStream(command)); TokenRewriteStream tokens = new TokenRewriteStream(lexer); HiveParser parser = new HiveParser(tokens); parser.setTreeAdaptor(adaptor); HiveParser_ResourcePlanParser.triggerActionExpressionStandalone_return r = null; try { r = parser.gResourcePlanParser.triggerActionExpressionStandalone(); } catch (RecognitionException e) { throw new ParseException(parser.errors); } if (lexer.getErrors().size() != 0) { throw new ParseException(lexer.getErrors()); } else if (parser.errors.size() != 0) { throw new ParseException(parser.errors); } return r.getTree(); } }
public ASTNode parseTriggerExpression(String command) throws ParseException { HiveLexerX lexer = new HiveLexerX(new ANTLRNoCaseStringStream(command)); TokenRewriteStream tokens = new TokenRewriteStream(lexer); HiveParser parser = new HiveParser(tokens); parser.setTreeAdaptor(adaptor); HiveParser_ResourcePlanParser.triggerExpressionStandalone_return r = null; try { r = parser.gResourcePlanParser.triggerExpressionStandalone(); } catch (RecognitionException e) { throw new ParseException(parser.errors); } if (lexer.getErrors().size() != 0) { throw new ParseException(lexer.getErrors()); } else if (parser.errors.size() != 0) { throw new ParseException(parser.errors); } return r.getTree(); }
/** Test basic || expression */ @Test public void selectAll() throws RecognitionException { String queryString = "select * where a = 1 or b > 2"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; Collection<SelectFieldMapping> identifiers = query.getSelectFieldMappings(); assertEquals( 0, identifiers.size() ); }
@Test public void selectField() throws RecognitionException { String queryString = "select c where a = 1 or b > 2"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; Collection<SelectFieldMapping> identifiers = query.getSelectFieldMappings(); final SelectFieldMapping fieldMapping = identifiers.iterator().next(); assertEquals( "c", fieldMapping.getSourceFieldName() ); assertEquals( "c", fieldMapping.getTargetFieldName() ); }
@Test public void selectRename() throws RecognitionException { String queryString = "select {source:target} where a = 1 or b > 2"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; Collection<SelectFieldMapping> identifiers = query.getSelectFieldMappings(); final SelectFieldMapping fieldMapping = identifiers.iterator().next(); assertEquals( "source", fieldMapping.getSourceFieldName() ); assertEquals( "target", fieldMapping.getTargetFieldName() ); }
@Test public void uuidParse() throws RecognitionException { String queryString = "select * where title = c6ee8a1c-3ef4-11e2-8861-02e81adcf3d0"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; Equal rootNode = ( Equal ) query.getRootOperand(); assertEquals( "title", rootNode.getProperty().getValue() ); assertEquals( UUID.fromString( "c6ee8a1c-3ef4-11e2-8861-02e81adcf3d0" ), ( ( UUIDLiteral ) rootNode.getLiteral() ).getValue() ); }
/** Simple test that constructs and AST from the ANTLR generated files */ @Test public void equality() throws RecognitionException { String queryString = "select * where a = 5"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; Operand root = query.getRootOperand(); Equal equal = ( Equal ) root; assertEquals( "a", equal.getProperty().getValue() ); assertEquals( 5, ( ( LongLiteral ) equal.getLiteral() ).getValue().intValue() ); }
/** Test basic not expression */ @Test public void complexExpression() throws RecognitionException { String queryString = "select * where not a = 1"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; Operand root = query.getRootOperand(); NotOperand not = ( NotOperand ) root; Equal equal = ( Equal ) not.getOperation(); assertEquals( "a", equal.getProperty().getValue() ); assertEquals( 1, ( ( LongLiteral ) equal.getLiteral() ).getValue().intValue() ); }
/** Test basic not expression */ @Test public void notExpression() throws RecognitionException { String queryString = "select * where not a = 1"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; Operand root = query.getRootOperand(); NotOperand not = ( NotOperand ) root; Equal equal = ( Equal ) not.getOperation(); assertEquals( "a", equal.getProperty().getValue() ); assertEquals( 1, ( ( LongLiteral ) equal.getLiteral() ).getValue().intValue() ); }
@Test public void selectDistance() throws RecognitionException { String queryString = "select * where a contains 'foo'"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; ContainsOperand operand = ( ContainsOperand ) query.getRootOperand(); assertEquals( "a", operand.getProperty().getValue() ); assertEquals( "foo", operand.getString().getValue() ); }
@Test public void stringLower() throws Exception { String queryString = "select * where title = 'Hot'"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; Equal rootNode = ( Equal ) query.getRootOperand(); assertEquals( "title", rootNode.getProperty().getValue() ); assertEquals( "hot", ( ( StringLiteral ) rootNode.getLiteral() ).getValue() ); }
/** Test basic && expression */ @Test public void andExpression() throws RecognitionException { String queryString = "select * where a = 1 and b > 2"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; Operand root = query.getRootOperand(); AndOperand and = ( AndOperand ) root; Equal equal = ( Equal ) and.getLeft(); assertEquals( "a", equal.getProperty().getValue() ); assertEquals( 1, ( ( LongLiteral ) equal.getLiteral() ).getValue().intValue() ); GreaterThan greater = ( GreaterThan ) and.getRight(); assertEquals( "b", greater.getProperty().getValue() ); assertEquals( 2, ( ( LongLiteral ) greater.getLiteral() ).getValue().intValue() ); }
/** Test basic || expression */ @Test public void orExpression() throws RecognitionException { String queryString = "select * where a = 1 or b > 2"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; Operand root = query.getRootOperand(); OrOperand and = ( OrOperand ) root; Equal equal = ( Equal ) and.getLeft(); assertEquals( "a", equal.getProperty().getValue() ); assertEquals( 1, ( ( LongLiteral ) equal.getLiteral() ).getValue().intValue() ); GreaterThan greater = ( GreaterThan ) and.getRight(); assertEquals( "b", greater.getProperty().getValue() ); assertEquals( 2, ( ( LongLiteral ) greater.getLiteral() ).getValue().intValue() ); }
@Test public void selectGeoWithInt() throws RecognitionException { String queryString = "select * where a within 1 of -40.343666, 175.630917"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; WithinOperand operand = ( WithinOperand ) query.getRootOperand(); assertEquals( "a", operand.getProperty().getValue() ); assertEquals( 1f, operand.getDistance().getFloatValue(), 0 ); assertEquals( -40.343666f, operand.getLatitude().getFloatValue(), 0 ); assertEquals( 175.630917f, operand.getLongitude().getFloatValue(), 0 ); }
@Test public void selectGeo() throws RecognitionException { String queryString = "select * where a within .1 of -40.343666, 175.630917"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; WithinOperand operand = ( WithinOperand ) query.getRootOperand(); assertEquals( "a", operand.getProperty().getValue() ); assertEquals( .1f, operand.getDistance().getFloatValue(), 0 ); assertEquals( -40.343666f, operand.getLatitude().getFloatValue(), 0 ); assertEquals( 175.630917f, operand.getLongitude().getFloatValue(), 0 ); }
@Test public void selectGeoWithAnd() throws RecognitionException { String queryString = "select * where location within 20000 of 37,-75 " + "and created > 1407776999925 and created < 1407777000266"; ANTLRStringStream in = new ANTLRStringStream( queryString ); CpQueryFilterLexer lexer = new CpQueryFilterLexer( in ); TokenRewriteStream tokens = new TokenRewriteStream( lexer ); CpQueryFilterParser parser = new CpQueryFilterParser( tokens ); ParsedQuery query = parser.ql().parsedQuery; AndOperand andOp1 = ( AndOperand ) query.getRootOperand(); AndOperand andOp2 = ( AndOperand ) andOp1.getLeft(); WithinOperand withinOperand = ( WithinOperand ) andOp2.getLeft(); assertEquals( "location", withinOperand.getProperty().getValue() ); assertEquals( 20000, withinOperand.getDistance().getFloatValue(), 0 ); assertEquals( 37f, withinOperand.getLatitude().getFloatValue(), 0 ); assertEquals( -75f, withinOperand.getLongitude().getFloatValue(), 0 ); }