You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by vkorukanti <gi...@git.apache.org> on 2016/03/11 19:40:01 UTC

[GitHub] drill pull request: DRILL-1328: Support table statistics

GitHub user vkorukanti opened a pull request:

    https://github.com/apache/drill/pull/425

    DRILL-1328: Support table statistics

    Patch attached to the JIRA is seems to be useful for generating table stats and using them for query planning. I rebased the patch to latest master, fixed few issues and added few tests.
    
    It still needs work to make it a full fledged feature, but I think the current state of the patch is good enough to commit and make improvements/fixes later.
    
    @jinfengni and @amansinha100 : Could you please review the patch?

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/vkorukanti/drill DRILL-1328-r1

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/drill/pull/425.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #425
    
----
commit 079d109ee40d1be0ce6f5cfed5a091da41a477dc
Author: Cliff Buchanan <cb...@maprtech.com>
Date:   2014-08-21T21:59:53Z

    DRILL-1328: Support table statistics
    
    PRE: Add "append" concept to directory write.
    
    * This is so stats can be stored in [table].stats.drill and be appended to be writing a new file into the directory.
    
    FUNCS: Statistics functions as UDFs:
    Currently using FieldReader to ensure consistent output type so that Unpivot doesn't get confused. All stats columns should be Nullable, so that stats functions can return NULL when N/A.
    * custom versions of "count" that always return BigInt
    * HyperLogLog based NDV that returns BigInt that works only on VarChars
    * HyperLogLog with binary output that only works on VarChars
    
    OPS: Updated protobufs for new ops
    
    OPS: Implemented StatisticsAggregate
    
    OPS: Implemented StatisticsUnpivot
    
    ANALYZE: AnalyzeTable functionality
    * JavaCC syntax more-or-less copied from LucidDB.
    * (Basic) AnalyzePrule: DrillAnalyzeRel -> UnpivotPrel and StatsAggPrel
    
    ANALYZE: Add getMetadataTable() to AbstractSchema
    
    USAGE: Change field access in QueryWrapper
    
    USAGE: Add getDrillTable() to DrillScanRelBase and ScanPrel
    * since ScanPrel does not inherit from DrillScanRelBase, this requires adding a DrillTable to the constructor
    * This is done so that a custom ReflectiveRelMetadataProvider can access the DrillTable associated with Logical/Physical scans.
    
    USAGE: Attach DrillTableMetadata to DrillTable.
    * DrillTableMetadata represents the data scanned from a corresponding ".stats.drill" table
    * In order to avoid doing query execution right after the ".stats.drill" table is found, metadata is not actually collected until the MaterializationVisitor is used.
    ** Currently, the metadata source must be a string (so that a SQL query can be created). Doing this with a table is probably more complicated.
    ** Query is set up to extract only the most recent statistics results for each column.
    
    USAGE: Configure DrillJoinRelBase to use NDV metadata when available.
    
    USAGE: attach metadata to table
    
    USAGE: implement optiq provider

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63800120
  
    --- Diff: exec/java-exec/src/main/codegen/data/Parser.tdd ---
    @@ -36,7 +36,34 @@
         "USE",
         "FILES",
         "REFRESH",
    -    "METADATA"
    +    "METADATA",
    +    "ANALYZE",
    +    "COMPUTE",
    +    "ESTIMATE",
    +    "STATISTICS",
    +    "COLUMNS",
    +    "SAMPLE"
    +    "PERCENT"
    +  ]
    +
    +  # List of keywords from "keywords" section that are not reserved by SQL:2003 standard.
    +  # Example: "DATABASES", "TABLES" are keywords but are not reserved by SQL:2003 standard.
    +  # First keyword that starts the statement should be a reserved keyword, otherwise the current parser
    +  # ends up considering it as a expression and fails (TODO: need to follow up with Calcite to find if it is a bug
    +  # or by design.
    +  nonReservedKeywords: [
    +    "DATABASES",
    +    "REPLACE",
    +    "SCHEMAS",
    +    "TABLES",
    +    "FILES",
    --- End diff --
    
    Also [DRILL-3875](https://issues.apache.org/jira/browse/DRILL-3875).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by vkorukanti <gi...@git.apache.org>.
Github user vkorukanti commented on the pull request:

    https://github.com/apache/drill/pull/425#issuecomment-201911246
  
    @amansinha100 Thanks for reviewing, I haven't tested when the table has parquet metadata cache and stats. I will test it before posting the updated patch.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63799624
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/dotdrill/DotDrillType.java ---
    @@ -21,9 +21,9 @@
     import org.apache.hadoop.fs.Path;
     
     public enum DotDrillType {
    -  VIEW
    +  VIEW,
       // ,FORMAT
    -  // ,STATS
    +  STATS
    --- End diff --
    
    Change the order to avoid changing ordinal in the future.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by vkorukanti <gi...@git.apache.org>.
Github user vkorukanti commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63806827
  
    --- Diff: exec/jdbc-all/pom.xml ---
    @@ -441,7 +441,7 @@
                       This is likely due to you adding new dependencies to a java-exec and not updating the excludes in this module. This is important as it minimizes the size of the dependency of Drill application users.
                       
                       </message>
    -                  <maxsize>20000000</maxsize>
    +                  <maxsize>37000000</maxsize>
    --- End diff --
    
    jdbc-size all issue is fixed in updated commit.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by jaltekruse <gi...@git.apache.org>.
Github user jaltekruse commented on the pull request:

    https://github.com/apache/drill/pull/425#issuecomment-220190035
  
    As a matter of good convention can we add a JIRA number to the TODOs? I don't think it is necessary to create a JIRA for each one, but even a follow up JIRA that says "address shortcomings of statistics" and a JIRA number we can search the code for would be useful to make sure we don't lose track of them.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63799668
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillJoinRelBase.java ---
    @@ -94,9 +97,39 @@ public RelOptCost computeSelfCost(RelOptPlanner planner) {
       public double getRows() {
         if (this.condition.isAlwaysTrue()) {
           return joinRowFactor * this.getLeft().getRows() * this.getRight().getRows();
    -    } else {
    -      return joinRowFactor * Math.max(this.getLeft().getRows(), this.getRight().getRows());
         }
    +
    +    int[] joinFields = new int[2];
    +
    +    LogicalJoin jr = new LogicalJoin(
    +        this.getCluster(),
    +        this.getTraitSet(),
    +        this.getLeft(),
    +        this.getRight(),
    +        this.getCondition(),
    +        this.getJoinType(),
    +        this.getVariablesStopped(),
    +        false /* TODO - verify if this the correct value */,
    --- End diff --
    
    Does this need to be addressed?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by vkorukanti <gi...@git.apache.org>.
Github user vkorukanti commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63805497
  
    --- Diff: exec/jdbc-all/pom.xml ---
    @@ -441,7 +441,7 @@
                       This is likely due to you adding new dependencies to a java-exec and not updating the excludes in this module. This is important as it minimizes the size of the dependency of Drill application users.
                       
                       </message>
    -                  <maxsize>20000000</maxsize>
    +                  <maxsize>37000000</maxsize>
    --- End diff --
    
    This shouldn't increase in size. I need to exclude some dependencies.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by vkorukanti <gi...@git.apache.org>.
Github user vkorukanti commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63745018
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StatisticsAggrFunctions.java ---
    @@ -0,0 +1,295 @@
    +/*******************************************************************************
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + ******************************************************************************/
    +
    +/*
    + * This class is automatically generated from AggrTypeFunctions2.tdd using FreeMarker.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.expr.DrillAggFunc;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableBigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableVarBinaryHolder;
    +import org.apache.drill.exec.expr.holders.ObjectHolder;
    +import org.apache.drill.exec.vector.complex.reader.FieldReader;
    +
    +import javax.inject.Inject;
    +
    +@SuppressWarnings("unused")
    +public class StatisticsAggrFunctions {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(StatisticsAggrFunctions.class);
    +
    +  @FunctionTemplate(name = "statcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class StatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      count.value++;
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "nonnullstatcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class NonNullStatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (in.isSet()) {
    +        count.value++;
    +      }
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "hll", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class HllFieldReader implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    ObjectHolder work;
    +    @Output
    +    NullableVarBinaryHolder out;
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Override
    +    public void setup() {
    +      work = new ObjectHolder();
    +      work.obj = new com.clearspring.analytics.stream.cardinality.HyperLogLog(10);
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (work.obj != null) {
    +        com.clearspring.analytics.stream.cardinality.HyperLogLog hll =
    +            (com.clearspring.analytics.stream.cardinality.HyperLogLog) work.obj;
    +        int mode = in.getType().getMode().getNumber();
    +        int type = in.getType().getMinorType().getNumber();
    +
    +        switch (mode) {
    +          case org.apache.drill.common.types.TypeProtos.DataMode.OPTIONAL_VALUE:
    +            if (!in.isSet()) {
    +              hll.offer(null);
    +              break;
    +            }
    +            // fall through //
    +          case org.apache.drill.common.types.TypeProtos.DataMode.REQUIRED_VALUE:
    +            switch (type) {
    +              case org.apache.drill.common.types.TypeProtos.MinorType.VARCHAR_VALUE:
    +                hll.offer(in.readText().toString());
    +                break;
    +              default:
    +                work.obj = null;
    +            }
    +            break;
    +          default:
    +            work.obj = null;
    +        }
    +      }
    +    }
    +
    +    @Override
    +    public void output() {
    +      if (work.obj != null) {
    +        com.clearspring.analytics.stream.cardinality.HyperLogLog hll =
    +            (com.clearspring.analytics.stream.cardinality.HyperLogLog) work.obj;
    +
    +        try {
    +          byte[] ba = hll.getBytes();
    +          out.buffer = buffer.reallocIfNeeded(ba.length);
    +          out.start = 0;
    +          out.end = ba.length;
    +          out.buffer.setBytes(0, ba);
    +          out.isSet = 1;
    +        } catch (java.io.IOException e) {
    +          throw new org.apache.drill.common.exceptions.DrillRuntimeException("Failed to get HyperLogLog output", e);
    --- End diff --
    
    Currently we have no mechanism to handle exceptions in UDFs. We generally tried to avoid `try...catch` blocks in UDFs, but in this case we need a `try...catch` block because `HyperLogLog.getBytes()` throws a checked exception. Once [DRILL-3764] (https://issues.apache.org/jira/browse/DRILL-3764) is fixed, we can change this accordingly.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63799730
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/QueryWrapper.java ---
    @@ -112,14 +112,14 @@ public String toString() {
       }
     
     
    -  private static class Listener implements UserResultsListener {
    +  public static class Listener implements UserResultsListener {
    --- End diff --
    
    This class should not be public. Changes in this should not affect stats table.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by amansinha100 <gi...@git.apache.org>.
Github user amansinha100 commented on the pull request:

    https://github.com/apache/drill/pull/425#issuecomment-201908762
  
    If you could clarify what we want to do with the ObjectHolder,  I am ok with getting this patch merged in and address remaining issues incrementally.  The benefits of having the framework for computing NDV and nullability statistics is substantial.  


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by amansinha100 <gi...@git.apache.org>.
Github user amansinha100 commented on the pull request:

    https://github.com/apache/drill/pull/425#issuecomment-201908491
  
    @vkorukanti have you had a chance to test this with metadata caching ?  Since the row count stats is stored in both the metadata cache and the .drill.stats file, I am not sure where it is getting retrieved from.  


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on the pull request:

    https://github.com/apache/drill/pull/425#issuecomment-220189028
  
    Sounds good. I agree that some of my questions can be ignored :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63799874
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/FormatPlugin.java ---
    @@ -48,7 +48,7 @@
     
       public FormatMatcher getMatcher();
     
    -  public AbstractWriter getWriter(PhysicalOperator child, String location, List<String> partitionColumns) throws IOException;
    +  public AbstractWriter getWriter(PhysicalOperator child, String location, boolean append, List<String> partitionColumns) throws IOException;
    --- End diff --
    
    Could pass a map of options, when "append" is an option?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by vkorukanti <gi...@git.apache.org>.
Github user vkorukanti commented on the pull request:

    https://github.com/apache/drill/pull/425#issuecomment-220186692
  
    Just want to clarify. There are TODOs and somethings may not work in certain cases. Consider this patch as alpha feature. Needs more work to make it a full fledged feature.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63805232
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/physical/config/UnpivotMaps.java ---
    @@ -0,0 +1,59 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.drill.exec.physical.config;
    +
    +import java.util.List;
    +
    +import org.apache.drill.exec.physical.base.AbstractSingle;
    +import org.apache.drill.exec.physical.base.PhysicalOperator;
    +import org.apache.drill.exec.physical.base.PhysicalVisitor;
    +import org.apache.drill.exec.proto.UserBitShared.CoreOperatorType;
    +
    +import com.fasterxml.jackson.annotation.JsonCreator;
    +import com.fasterxml.jackson.annotation.JsonProperty;
    +import com.fasterxml.jackson.annotation.JsonTypeName;
    +
    +@JsonTypeName("unpivot-maps")
    +public class UnpivotMaps extends AbstractSingle {
    --- End diff --
    
    Can this be exposed as a function too, might be useful function in general?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by amansinha100 <gi...@git.apache.org>.
Github user amansinha100 commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r57015590
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StatisticsAggrFunctions.java ---
    @@ -0,0 +1,295 @@
    +/*******************************************************************************
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + ******************************************************************************/
    +
    +/*
    + * This class is automatically generated from AggrTypeFunctions2.tdd using FreeMarker.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.expr.DrillAggFunc;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableBigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableVarBinaryHolder;
    +import org.apache.drill.exec.expr.holders.ObjectHolder;
    +import org.apache.drill.exec.vector.complex.reader.FieldReader;
    +
    +import javax.inject.Inject;
    +
    +@SuppressWarnings("unused")
    +public class StatisticsAggrFunctions {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(StatisticsAggrFunctions.class);
    +
    +  @FunctionTemplate(name = "statcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class StatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      count.value++;
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "nonnullstatcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class NonNullStatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (in.isSet()) {
    +        count.value++;
    +      }
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "hll", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class HllFieldReader implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    ObjectHolder work;
    +    @Output
    +    NullableVarBinaryHolder out;
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Override
    +    public void setup() {
    +      work = new ObjectHolder();
    +      work.obj = new com.clearspring.analytics.stream.cardinality.HyperLogLog(10);
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (work.obj != null) {
    +        com.clearspring.analytics.stream.cardinality.HyperLogLog hll =
    --- End diff --
    
    Is this HLL package (com.clearspring.*) acceptable from a licensing perspective ? 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by amansinha100 <gi...@git.apache.org>.
Github user amansinha100 commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r57517061
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StatisticsAggrFunctions.java ---
    @@ -0,0 +1,295 @@
    +/*******************************************************************************
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + ******************************************************************************/
    +
    +/*
    + * This class is automatically generated from AggrTypeFunctions2.tdd using FreeMarker.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.expr.DrillAggFunc;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableBigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableVarBinaryHolder;
    +import org.apache.drill.exec.expr.holders.ObjectHolder;
    +import org.apache.drill.exec.vector.complex.reader.FieldReader;
    +
    +import javax.inject.Inject;
    +
    +@SuppressWarnings("unused")
    +public class StatisticsAggrFunctions {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(StatisticsAggrFunctions.class);
    +
    +  @FunctionTemplate(name = "statcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class StatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      count.value++;
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "nonnullstatcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class NonNullStatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (in.isSet()) {
    +        count.value++;
    +      }
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "hll", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class HllFieldReader implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    ObjectHolder work;
    +    @Output
    +    NullableVarBinaryHolder out;
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Override
    +    public void setup() {
    +      work = new ObjectHolder();
    +      work.obj = new com.clearspring.analytics.stream.cardinality.HyperLogLog(10);
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (work.obj != null) {
    +        com.clearspring.analytics.stream.cardinality.HyperLogLog hll =
    --- End diff --
    
    I see...I suppose if @jacques-n has checked the licensing part I am good with it.  


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by vkorukanti <gi...@git.apache.org>.
Github user vkorukanti commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r57026410
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StatisticsAggrFunctions.java ---
    @@ -0,0 +1,295 @@
    +/*******************************************************************************
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + ******************************************************************************/
    +
    +/*
    + * This class is automatically generated from AggrTypeFunctions2.tdd using FreeMarker.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.expr.DrillAggFunc;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableBigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableVarBinaryHolder;
    +import org.apache.drill.exec.expr.holders.ObjectHolder;
    +import org.apache.drill.exec.vector.complex.reader.FieldReader;
    +
    +import javax.inject.Inject;
    +
    +@SuppressWarnings("unused")
    +public class StatisticsAggrFunctions {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(StatisticsAggrFunctions.class);
    +
    +  @FunctionTemplate(name = "statcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class StatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      count.value++;
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "nonnullstatcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class NonNullStatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (in.isSet()) {
    +        count.value++;
    +      }
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "hll", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class HllFieldReader implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    ObjectHolder work;
    +    @Output
    +    NullableVarBinaryHolder out;
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Override
    +    public void setup() {
    +      work = new ObjectHolder();
    +      work.obj = new com.clearspring.analytics.stream.cardinality.HyperLogLog(10);
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (work.obj != null) {
    +        com.clearspring.analytics.stream.cardinality.HyperLogLog hll =
    --- End diff --
    
    From [1], it is released under Apache 2.0 license [1] . 
    
    [1] https://github.com/addthis/stream-lib
    [2] https://github.com/addthis/stream-lib/blob/master/LICENSE.txt


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63805225
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StatisticsAggrFunctions.java ---
    @@ -0,0 +1,285 @@
    +/*******************************************************************************
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + ******************************************************************************/
    +
    +/*
    + * This class is automatically generated from AggrTypeFunctions2.tdd using FreeMarker.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.expr.DrillAggFunc;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableBigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableVarBinaryHolder;
    +import org.apache.drill.exec.expr.holders.ObjectHolder;
    +import org.apache.drill.exec.vector.complex.reader.FieldReader;
    +
    +import javax.inject.Inject;
    +
    +@SuppressWarnings("unused")
    +public class StatisticsAggrFunctions {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(StatisticsAggrFunctions.class);
    +
    +  @FunctionTemplate(name = "statcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class StatCount implements DrillAggFunc {
    +    @Param FieldReader in;
    +    @Workspace BigIntHolder count;
    +    @Output NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      count.value++;
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "nonnullstatcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class NonNullStatCount implements DrillAggFunc {
    +    @Param FieldReader in;
    +    @Workspace BigIntHolder count;
    +    @Output NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (in.isSet()) {
    +        count.value++;
    +      }
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "hll", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class HllFieldReader implements DrillAggFunc {
    +    @Param FieldReader in;
    +    @Workspace ObjectHolder work;
    +    @Output NullableVarBinaryHolder out;
    +    @Inject DrillBuf buffer;
    +
    +    @Override
    +    public void setup() {
    +      work = new ObjectHolder();
    +      work.obj = new com.clearspring.analytics.stream.cardinality.HyperLogLog(10);
    --- End diff --
    
    What's magical about 10? Should this be configurable?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by vkorukanti <gi...@git.apache.org>.
Github user vkorukanti commented on the pull request:

    https://github.com/apache/drill/pull/425#issuecomment-220101853
  
    Tested with parquet metadata cache. Currently if you do a count(*) on parquet table having metadata cache, we get that count from cache. For rowcount in planning: if a stats table exists we get the count from stats table, otherwise we get it from groupscan which in parquet case gets it from the metadata cache if exists.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63799694
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/AnalyzePrule.java ---
    @@ -0,0 +1,64 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.drill.exec.planner.physical;
    +
    +import com.google.common.collect.ImmutableList;
    +import com.google.common.collect.Lists;
    +import org.apache.calcite.plan.RelOptRule;
    +import org.apache.calcite.plan.RelOptRuleCall;
    +import org.apache.calcite.plan.RelTraitSet;
    +import org.apache.calcite.rel.RelNode;
    +import org.apache.calcite.rel.SingleRel;
    +import org.apache.drill.exec.planner.common.DrillStatsTable;
    +import org.apache.drill.exec.planner.logical.DrillAnalyzeRel;
    +import org.apache.drill.exec.planner.logical.DrillRel;
    +import org.apache.drill.exec.planner.logical.RelOptHelper;
    +
    +import java.util.List;
    +
    +public class AnalyzePrule extends Prule {
    +  public static final RelOptRule INSTANCE = new AnalyzePrule();
    +
    +  private static final List<String> FUNCTIONS = ImmutableList.of(
    +      "statcount", // total number of entries in the table
    --- End diff --
    
    These constants are hardcoded in multiple places, might be hard to track.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by amansinha100 <gi...@git.apache.org>.
Github user amansinha100 commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r57517122
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StatisticsAggrFunctions.java ---
    @@ -0,0 +1,295 @@
    +/*******************************************************************************
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + ******************************************************************************/
    +
    +/*
    + * This class is automatically generated from AggrTypeFunctions2.tdd using FreeMarker.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.expr.DrillAggFunc;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableBigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableVarBinaryHolder;
    +import org.apache.drill.exec.expr.holders.ObjectHolder;
    +import org.apache.drill.exec.vector.complex.reader.FieldReader;
    +
    +import javax.inject.Inject;
    +
    +@SuppressWarnings("unused")
    +public class StatisticsAggrFunctions {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(StatisticsAggrFunctions.class);
    +
    +  @FunctionTemplate(name = "statcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class StatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      count.value++;
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "nonnullstatcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class NonNullStatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (in.isSet()) {
    +        count.value++;
    +      }
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "hll", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class HllFieldReader implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    ObjectHolder work;
    +    @Output
    +    NullableVarBinaryHolder out;
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Override
    +    public void setup() {
    +      work = new ObjectHolder();
    +      work.obj = new com.clearspring.analytics.stream.cardinality.HyperLogLog(10);
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (work.obj != null) {
    +        com.clearspring.analytics.stream.cardinality.HyperLogLog hll =
    +            (com.clearspring.analytics.stream.cardinality.HyperLogLog) work.obj;
    +        int mode = in.getType().getMode().getNumber();
    +        int type = in.getType().getMinorType().getNumber();
    +
    +        switch (mode) {
    +          case org.apache.drill.common.types.TypeProtos.DataMode.OPTIONAL_VALUE:
    +            if (!in.isSet()) {
    +              hll.offer(null);
    +              break;
    +            }
    +            // fall through //
    +          case org.apache.drill.common.types.TypeProtos.DataMode.REQUIRED_VALUE:
    +            switch (type) {
    +              case org.apache.drill.common.types.TypeProtos.MinorType.VARCHAR_VALUE:
    +                hll.offer(in.readText().toString());
    +                break;
    +              default:
    +                work.obj = null;
    +            }
    +            break;
    +          default:
    +            work.obj = null;
    +        }
    +      }
    +    }
    +
    +    @Override
    +    public void output() {
    +      if (work.obj != null) {
    +        com.clearspring.analytics.stream.cardinality.HyperLogLog hll =
    +            (com.clearspring.analytics.stream.cardinality.HyperLogLog) work.obj;
    +
    +        try {
    +          byte[] ba = hll.getBytes();
    +          out.buffer = buffer.reallocIfNeeded(ba.length);
    +          out.start = 0;
    +          out.end = ba.length;
    +          out.buffer.setBytes(0, ba);
    +          out.isSet = 1;
    +        } catch (java.io.IOException e) {
    +          throw new org.apache.drill.common.exceptions.DrillRuntimeException("Failed to get HyperLogLog output", e);
    --- End diff --
    
    Throwing exception in the generated code is generally discouraged...or are we ok with RuntimeException ? 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63805262
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/FormatPlugin.java ---
    @@ -48,7 +48,7 @@
     
       public FormatMatcher getMatcher();
     
    -  public AbstractWriter getWriter(PhysicalOperator child, String location, List<String> partitionColumns) throws IOException;
    +  public AbstractWriter getWriter(PhysicalOperator child, String location, boolean append, List<String> partitionColumns) throws IOException;
    --- End diff --
    
    Note that this is API breaking change for an alpha feature.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63805239
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStatsTable.java ---
    @@ -0,0 +1,161 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.drill.exec.planner.common;
    +
    +import java.util.Map;
    +
    +import com.google.common.base.Preconditions;
    +import com.google.common.collect.Maps;
    +import org.apache.calcite.rel.RelNode;
    +import org.apache.calcite.rel.RelVisitor;
    +import org.apache.calcite.rel.core.TableScan;
    +import org.apache.drill.exec.client.DrillClient;
    +import org.apache.drill.exec.ops.QueryContext;
    +import org.apache.drill.exec.planner.logical.DrillTable;
    +import org.apache.drill.exec.proto.UserBitShared;
    +import org.apache.drill.exec.server.DrillbitContext;
    +import org.apache.drill.exec.server.rest.QueryWrapper.Listener;
    +
    +/**
    + * Wraps the stats table info including schema and tableName. Also materializes stats from storage and keeps them in
    + * memory.
    + */
    +public class DrillStatsTable {
    +  private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillStatsTable.class);
    +
    +  /**
    +   * List of columns in stats table.
    +   */
    +  public static final String COL_COLUMN = "column";
    +  public static final String COL_COMPUTED = "computed";
    +  public static final String COL_STATCOUNT = "statcount";
    +  public static final String COL_NDV = "ndv";
    +
    +  private final String schemaName;
    +  private final String tableName;
    +
    +  private final Map<String, Long> ndv = Maps.newHashMap();
    +  private double rowCount = -1;
    +
    +  private boolean materialized = false;
    +
    +  public DrillStatsTable(String schemaName, String tableName) {
    +    this.schemaName = schemaName;
    +    this.tableName = tableName;
    +  }
    +
    +  /**
    +   * Get number of distinct values of given column. If stats are not present for the given column, a null is returned.
    +   *
    +   * Note: returned data may not be accurate. Accuracy depends on whether the table data has changed after the
    +   * stats are computed.
    +   *
    +   * @param col
    +   * @return
    +   */
    +  public Double getNdv(String col) {
    +    Preconditions.checkState(materialized, "Stats are not yet materialized.");
    +
    +    final String upperCol = col.toUpperCase();
    +    final Long ndvCol = ndv.get(upperCol);
    +    if (ndvCol != null) {
    +      return Math.min(ndvCol, rowCount);
    +    }
    +
    +    return null;
    +  }
    +
    +  /**
    +   * Get row count of the table. Returns null if stats are not present.
    +   *
    +   * Note: returned data may not be accurate. Accuracy depends on whether the table data has changed after the
    +   * stats are computed.
    +   *
    +   * @return
    +   */
    +  public Double getRowCount() {
    +    Preconditions.checkState(materialized, "Stats are not yet materialized.");
    +    return rowCount > 0 ? rowCount : null;
    +  }
    +
    +  /**
    +   * Read the stats from storage and keep them in memory.
    +   * @param context
    +   * @throws Exception
    +   */
    +  public void materialize(final QueryContext context) throws Exception {
    +    if (materialized) {
    +      return;
    +    }
    +
    +    final String fullTableName = "`" + schemaName + "`.`" + tableName + "`";
    +    final String sql = "SELECT a.* FROM " + fullTableName + " AS a INNER JOIN " +
    +        "(SELECT `" + COL_COLUMN + "`, max(`" + COL_COMPUTED +"`) AS `" + COL_COMPUTED + "` " +
    +        "FROM " + fullTableName + " GROUP BY `" + COL_COLUMN + "`) AS b " +
    +        "ON a.`" + COL_COLUMN + "` = b.`" + COL_COLUMN +"` and a.`" + COL_COMPUTED + "` = b.`" + COL_COMPUTED + "`";
    +
    +    final DrillbitContext dc = context.getDrillbitContext();
    +    try(final DrillClient client = new DrillClient(dc.getConfig(), dc.getClusterCoordinator(), dc.getAllocator())) {
    --- End diff --
    
    Note that this is creating two executor services.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by amansinha100 <gi...@git.apache.org>.
Github user amansinha100 commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r57517236
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStatsTable.java ---
    @@ -0,0 +1,161 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.drill.exec.planner.common;
    +
    +import java.util.Map;
    +
    +import com.google.common.base.Preconditions;
    +import com.google.common.collect.Maps;
    +import org.apache.calcite.rel.RelNode;
    +import org.apache.calcite.rel.RelVisitor;
    +import org.apache.calcite.rel.core.TableScan;
    +import org.apache.drill.exec.client.DrillClient;
    +import org.apache.drill.exec.ops.QueryContext;
    +import org.apache.drill.exec.planner.logical.DrillTable;
    +import org.apache.drill.exec.proto.UserBitShared;
    +import org.apache.drill.exec.server.DrillbitContext;
    +import org.apache.drill.exec.server.rest.QueryWrapper.Listener;
    +
    +/**
    + * Wraps the stats table info including schema and tableName. Also materializes stats from storage and keeps them in
    + * memory.
    + */
    +public class DrillStatsTable {
    +  private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillStatsTable.class);
    +
    +  /**
    +   * List of columns in stats table.
    +   */
    +  public static final String COL_COLUMN = "column";
    +  public static final String COL_COMPUTED = "computed";
    +  public static final String COL_STATCOUNT = "statcount";
    +  public static final String COL_NDV = "ndv";
    +
    +  private final String schemaName;
    +  private final String tableName;
    +
    +  private final Map<String, Long> ndv = Maps.newHashMap();
    +  private double rowCount = -1;
    +
    +  private boolean materialized = false;
    +
    +  public DrillStatsTable(String schemaName, String tableName) {
    --- End diff --
    
    When updating the drill stats file on the filesystem, similar to metadata cache we should add a check: recompute stats only if the directory modification time is later than the last stats timestamp.  No need to recompute if stats is not stale. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by vkorukanti <gi...@git.apache.org>.
Github user vkorukanti commented on the pull request:

    https://github.com/apache/drill/pull/425#issuecomment-220124783
  
    Updated patch. Also added a brief design and usage doc to JIRA. Waiting for CALCITE-1241.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63805316
  
    --- Diff: exec/jdbc-all/pom.xml ---
    @@ -441,7 +441,7 @@
                       This is likely due to you adding new dependencies to a java-exec and not updating the excludes in this module. This is important as it minimizes the size of the dependency of Drill application users.
                       
                       </message>
    -                  <maxsize>20000000</maxsize>
    +                  <maxsize>37000000</maxsize>
    --- End diff --
    
    Why is the jdbc-all jar affected?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63799606
  
    --- Diff: exec/java-exec/src/main/codegen/data/Parser.tdd ---
    @@ -36,7 +36,34 @@
         "USE",
         "FILES",
         "REFRESH",
    -    "METADATA"
    +    "METADATA",
    +    "ANALYZE",
    +    "COMPUTE",
    +    "ESTIMATE",
    +    "STATISTICS",
    +    "COLUMNS",
    +    "SAMPLE"
    +    "PERCENT"
    +  ]
    +
    +  # List of keywords from "keywords" section that are not reserved by SQL:2003 standard.
    +  # Example: "DATABASES", "TABLES" are keywords but are not reserved by SQL:2003 standard.
    +  # First keyword that starts the statement should be a reserved keyword, otherwise the current parser
    +  # ends up considering it as a expression and fails (TODO: need to follow up with Calcite to find if it is a bug
    +  # or by design.
    +  nonReservedKeywords: [
    +    "DATABASES",
    +    "REPLACE",
    +    "SCHEMAS",
    +    "TABLES",
    +    "FILES",
    --- End diff --
    
    Something like this came up before where a list of non reserved keyword might result in some ambiguous queries. See [DRILL-2116](https://issues.apache.org/jira/browse/DRILL-2116).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by jacques-n <gi...@git.apache.org>.
Github user jacques-n commented on the pull request:

    https://github.com/apache/drill/pull/425#issuecomment-201910127
  
    ObjectHolder should be used cautiously. That is why it is marked as deprecated. I'm pretty sure it was deprecated when we first added it. In order to hold complex values that can change size in aggregation, we have to hold them in the Java heap because vectors are mutable. This is why we have ObjectHolder, basically allows us variable width mutable values in aggregation. It makes sense to use it here as well.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by amansinha100 <gi...@git.apache.org>.
Github user amansinha100 commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r57517080
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StatisticsAggrFunctions.java ---
    @@ -0,0 +1,295 @@
    +/*******************************************************************************
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + ******************************************************************************/
    +
    +/*
    + * This class is automatically generated from AggrTypeFunctions2.tdd using FreeMarker.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.expr.DrillAggFunc;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableBigIntHolder;
    +import org.apache.drill.exec.expr.holders.NullableVarBinaryHolder;
    +import org.apache.drill.exec.expr.holders.ObjectHolder;
    +import org.apache.drill.exec.vector.complex.reader.FieldReader;
    +
    +import javax.inject.Inject;
    +
    +@SuppressWarnings("unused")
    +public class StatisticsAggrFunctions {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(StatisticsAggrFunctions.class);
    +
    +  @FunctionTemplate(name = "statcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class StatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      count.value++;
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "nonnullstatcount", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class NonNullStatCount implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    BigIntHolder count;
    +    @Output
    +    NullableBigIntHolder out;
    +
    +    @Override
    +    public void setup() {
    +      count = new BigIntHolder();
    +    }
    +
    +    @Override
    +    public void add() {
    +      if (in.isSet()) {
    +        count.value++;
    +      }
    +    }
    +
    +    @Override
    +    public void output() {
    +      out.isSet = 1;
    +      out.value = count.value;
    +    }
    +
    +    @Override
    +    public void reset() {
    +      count.value = 0;
    +    }
    +  }
    +
    +  @FunctionTemplate(name = "hll", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE)
    +  public static class HllFieldReader implements DrillAggFunc {
    +    @Param
    +    FieldReader in;
    +    @Workspace
    +    ObjectHolder work;
    --- End diff --
    
    ObjectHolder has been deprecated in Drill master ... do we want to continue using it ? 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63799657
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/unpivot/UnpivotMapsRecordBatch.java ---
    @@ -0,0 +1,241 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.drill.exec.physical.impl.unpivot;
    +
    +import java.util.List;
    +import java.util.Map;
    +
    +import org.apache.drill.common.types.TypeProtos.MajorType;
    +import org.apache.drill.common.types.TypeProtos.MinorType;
    +import org.apache.drill.exec.exception.OutOfMemoryException;
    +import org.apache.drill.exec.exception.SchemaChangeException;
    +import org.apache.drill.exec.expr.TypeHelper;
    +import org.apache.drill.exec.ops.FragmentContext;
    +import org.apache.drill.exec.physical.config.UnpivotMaps;
    +import org.apache.drill.exec.record.AbstractSingleRecordBatch;
    +import org.apache.drill.exec.record.MaterializedField;
    +import org.apache.drill.exec.record.RecordBatch;
    +import org.apache.drill.exec.record.TransferPair;
    +import org.apache.drill.exec.record.VectorContainer;
    +import org.apache.drill.exec.record.VectorWrapper;
    +import org.apache.drill.exec.vector.ValueVector;
    +import org.apache.drill.exec.vector.complex.MapVector;
    +
    +import com.google.common.collect.Lists;
    +import com.google.common.collect.Maps;
    +
    +/**
    + * TODO: This needs cleanup, especially in state transitions.
    --- End diff --
    
    Does this need to be addressed?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] drill pull request: DRILL-1328: Support table statistics

Posted by sudheeshkatkam <gi...@git.apache.org>.
Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/425#discussion_r63799682
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStatsTable.java ---
    @@ -0,0 +1,161 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.drill.exec.planner.common;
    +
    +import java.util.Map;
    +
    +import com.google.common.base.Preconditions;
    +import com.google.common.collect.Maps;
    +import org.apache.calcite.rel.RelNode;
    +import org.apache.calcite.rel.RelVisitor;
    +import org.apache.calcite.rel.core.TableScan;
    +import org.apache.drill.exec.client.DrillClient;
    +import org.apache.drill.exec.ops.QueryContext;
    +import org.apache.drill.exec.planner.logical.DrillTable;
    +import org.apache.drill.exec.proto.UserBitShared;
    +import org.apache.drill.exec.server.DrillbitContext;
    +import org.apache.drill.exec.server.rest.QueryWrapper.Listener;
    +
    +/**
    + * Wraps the stats table info including schema and tableName. Also materializes stats from storage and keeps them in
    + * memory.
    + */
    +public class DrillStatsTable {
    +  private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillStatsTable.class);
    +
    +  /**
    +   * List of columns in stats table.
    +   */
    +  public static final String COL_COLUMN = "column";
    +  public static final String COL_COMPUTED = "computed";
    +  public static final String COL_STATCOUNT = "statcount";
    +  public static final String COL_NDV = "ndv";
    +
    +  private final String schemaName;
    +  private final String tableName;
    +
    +  private final Map<String, Long> ndv = Maps.newHashMap();
    +  private double rowCount = -1;
    +
    +  private boolean materialized = false;
    +
    +  public DrillStatsTable(String schemaName, String tableName) {
    +    this.schemaName = schemaName;
    +    this.tableName = tableName;
    +  }
    +
    +  /**
    +   * Get number of distinct values of given column. If stats are not present for the given column, a null is returned.
    +   *
    +   * Note: returned data may not be accurate. Accuracy depends on whether the table data has changed after the
    +   * stats are computed.
    +   *
    +   * @param col
    +   * @return
    +   */
    +  public Double getNdv(String col) {
    +    Preconditions.checkState(materialized, "Stats are not yet materialized.");
    +
    +    final String upperCol = col.toUpperCase();
    +    final Long ndvCol = ndv.get(upperCol);
    +    if (ndvCol != null) {
    +      return Math.min(ndvCol, rowCount);
    +    }
    +
    +    return null;
    +  }
    +
    +  /**
    +   * Get row count of the table. Returns null if stats are not present.
    +   *
    +   * Note: returned data may not be accurate. Accuracy depends on whether the table data has changed after the
    +   * stats are computed.
    +   *
    +   * @return
    +   */
    +  public Double getRowCount() {
    +    Preconditions.checkState(materialized, "Stats are not yet materialized.");
    +    return rowCount > 0 ? rowCount : null;
    +  }
    +
    +  /**
    +   * Read the stats from storage and keep them in memory.
    +   * @param context
    +   * @throws Exception
    +   */
    +  public void materialize(final QueryContext context) throws Exception {
    +    if (materialized) {
    +      return;
    +    }
    +
    +    final String fullTableName = "`" + schemaName + "`.`" + tableName + "`";
    +    final String sql = "SELECT a.* FROM " + fullTableName + " AS a INNER JOIN " +
    +        "(SELECT `" + COL_COLUMN + "`, max(`" + COL_COMPUTED +"`) AS `" + COL_COMPUTED + "` " +
    +        "FROM " + fullTableName + " GROUP BY `" + COL_COLUMN + "`) AS b " +
    +        "ON a.`" + COL_COLUMN + "` = b.`" + COL_COLUMN +"` and a.`" + COL_COMPUTED + "` = b.`" + COL_COMPUTED + "`";
    +
    +    final DrillbitContext dc = context.getDrillbitContext();
    +    try(final DrillClient client = new DrillClient(dc.getConfig(), dc.getClusterCoordinator(), dc.getAllocator())) {
    --- End diff --
    
    Few questions here:
    (1) How does this work when authentication is enabled?
    (2) Why does this use a listener from the rest package?
    (3) Are there alternative way to run this query?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---