Menu

Some workaround for COUNT ( DISTINCT col )

zheng han
2010-09-10
2012-12-07
1 2 > >> (Page 1 of 2)
  • zheng han

    zheng han - 2010-09-10

    hi
    the most lastest release (0.6.3) have a problem parsing COUNT(DISTINCT col) function
    I check into the JSqlParser.jj file, and I have a *hack* to make the parser parse that func correctly

    in Function()

    "(" [["ALL" | "DISTINCT" ({distinct = true;})] (expressionList=SimpleExpressionList() | "*" { retval.setAllColumns(true); })] ")"
        ["}"]
        {
            if(distinct) {
              Object exp = expressionList.getExpressions().remove(0);
              if(!(exp instanceof Column)){
                throw new ParseException("DISTINCT can only be used in an aggregate function,eg. sum,count");
              }
              Column column = (Column)exp;
              DistinctExpression de = new DistinctExpression(column.toString());
              expressionList.getExpressions().add(de);          
            }
    

    here's the DistinctExpression.java

    public class DistinctExpression implements Expression {
        private String name;
        public DistinctExpression(String name) {
            super();
            this.name = name;
        }
        @Override
        public void accept(ExpressionVisitor expressionVisitor) {
            expressionVisitor.visit(this);
        }
        @Override
        public String toString() {
            return "DISTINCT " + name;
        }
    }
    

    it's not an elegant solutions(sorry, I'm new to javacc), but it works for count(distinct col), and passes all your testcases.
    I hope you can give a better solution.

    br

    hanzheng

     
  • Leonardo Francalanci

    should be fixed in 0.6.4, with a different approach (Function now has isDistinct())

     
  • zheng han

    zheng han - 2010-11-19

    thanks for replying.
    good job!

     
  • Nobody/Anonymous

    comment3,

     
  • Nobody/Anonymous

    comment2,

     
  • Nobody/Anonymous

    comment2,

     
  • Nobody/Anonymous

    comment6,

     
  • Nobody/Anonymous

    comment5,

     
  • Nobody/Anonymous

    comment5,

     
  • Nobody/Anonymous

    comment2,

     
  • Nobody/Anonymous

    comment5,

     
  • Nobody/Anonymous

    comment1,

     
  • Nobody/Anonymous

    comment2,

     
  • Nobody/Anonymous

    comment4,

     
  • Nobody/Anonymous

    comment2,

     
  • Nobody/Anonymous

    comment1,

     
  • Nobody/Anonymous

    comment4,

     
  • Nobody/Anonymous

    comment1,

     
  • Nobody/Anonymous

    comment1,

     
  • Nobody/Anonymous

    comment3,

     
  • Nobody/Anonymous

    comment6,

     
  • Nobody/Anonymous

    comment5,

     
  • Nobody/Anonymous

    comment6,

     
  • Nobody/Anonymous

    comment1,

     
1 2 > >> (Page 1 of 2)

Log in to post a comment.