paul-rogers commented on a change in pull request #1955: DRILL-7491: Incorrect count() returned for complex types in parquet URL: https://github.com/apache/drill/pull/1955#discussion_r366676515
########## File path: exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/AbstractGroupScanWithMetadata.java ########## @@ -167,29 +167,43 @@ public boolean isMatchAllMetadata() { */ @Override public long getColumnValueCount(SchemaPath column) { - long tableRowCount, colNulls; - Long nulls; ColumnStatistics<?> columnStats = getTableMetadata().getColumnStatistics(column); - ColumnStatistics<?> nonInterestingColStats = null; - if (columnStats == null) { - nonInterestingColStats = getNonInterestingColumnsMetadata().getColumnStatistics(column); - } + ColumnStatistics<?> nonInterestingColStats = (columnStats == null) + ? getNonInterestingColumnsMetadata().getColumnStatistics(column) : null; + long tableRowCount; if (columnStats != null) { tableRowCount = TableStatisticsKind.ROW_COUNT.getValue(getTableMetadata()); } else if (nonInterestingColStats != null) { tableRowCount = TableStatisticsKind.ROW_COUNT.getValue(getNonInterestingColumnsMetadata()); + columnStats = nonInterestingColStats; + } else if (existsNestedStatsForColumn(column, getTableMetadata()) + || existsNestedStatsForColumn(column, getNonInterestingColumnsMetadata())) { + return Statistic.NO_COLUMN_STATS; } else { return 0; // returns 0 if the column doesn't exist in the table. } - columnStats = columnStats != null ? columnStats : nonInterestingColStats; - nulls = ColumnStatisticsKind.NULLS_COUNT.getFrom(columnStats); - colNulls = nulls != null ? nulls : Statistic.NO_COLUMN_STATS; + Long nulls = ColumnStatisticsKind.NULLS_COUNT.getFrom(columnStats); + if (nulls == null || Statistic.NO_COLUMN_STATS == nulls || Statistic.NO_COLUMN_STATS == tableRowCount) { + return Statistic.NO_COLUMN_STATS; + } else { + return tableRowCount - nulls; + } + } - return Statistic.NO_COLUMN_STATS == tableRowCount - || Statistic.NO_COLUMN_STATS == colNulls - ? Statistic.NO_COLUMN_STATS : tableRowCount - colNulls; + /** + * For complex columns, stats may be present only for nested fields. For example, a column path is `a`, + * but stats present for `a`.`b`. So before making a decision that column is absent, the case needs + * to be tested. + * + * @param column column path + * @param metadata metadata with column statistics + * @return whether stats exists for nested fields + */ + private boolean existsNestedStatsForColumn(SchemaPath column, Metadata metadata) { Review comment: I wonder about the premise of this function. The Java doc suggests we have the path `a.b`. We want to know if `a` is an uninteresting column. Are we suggesting that we have metadata that says that `a.b` is uninteresting, but we have no metadata for `a` itself? Why would this be? Do we store columns in a non-structured way? That is, do we have a list of columns like 'a', 'b', 'c.d', 'e.f.g', 'c.h', and so on? Rather than `(a, b, c(d, g), e((g)))`? Further we seem to be assuming that we will gather stats for, say, `a.c`, but not `a.b`. The only place I can see that such complexity would make sense is to estimate cardinality for Flatten or Lateral Join. In such a case, might it make more sense to treat this recursively as a nested table? That is 'a` is a table and `a.b` and `a.c` are just `b` and `c` columns in the nested table. Given this, we *must* have a metadata entry for `a` (the map) in order to have metadata for any of its nested columns. So, again, I wonder if we've defined our metadata semantics as clearly as we might. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services