use of io.crate.expression.symbol.ScopedSymbol in project crate by crate.
the class AnalyzedView method getField.
@Override
public Symbol getField(ColumnIdent column, Operation operation, boolean errorOnUnknownObjectKey) throws AmbiguousColumnException, ColumnUnknownException, UnsupportedOperationException {
Symbol field = relation.getField(column, operation, errorOnUnknownObjectKey);
if (field == null || field instanceof VoidReference) {
return field;
}
ScopedSymbol scopedSymbol = new ScopedSymbol(name, column, field.valueType());
int i = outputSymbols.indexOf(scopedSymbol);
if (i >= 0) {
return outputSymbols.get(i);
}
return scopedSymbol;
}
use of io.crate.expression.symbol.ScopedSymbol in project crate by crate.
the class Stats method estimateSizeForColumns.
public long estimateSizeForColumns(List<Symbol> toCollect) {
long sum = 0L;
for (int i = 0; i < toCollect.size(); i++) {
Symbol symbol = toCollect.get(i);
ColumnStats<?> columnStats = null;
if (symbol instanceof Reference) {
columnStats = statsByColumn.get(((Reference) symbol).column());
} else if (symbol instanceof ScopedSymbol) {
columnStats = statsByColumn.get(((ScopedSymbol) symbol).column());
}
if (columnStats == null) {
if (symbol.valueType() instanceof FixedWidthType) {
sum += ((FixedWidthType) symbol.valueType()).fixedSize();
} else {
sum += RamUsageEstimator.UNKNOWN_DEFAULT_RAM_BYTES_USED;
}
} else {
sum += columnStats.averageSizeInBytes();
}
}
return sum;
}
use of io.crate.expression.symbol.ScopedSymbol in project crate by crate.
the class ExpressionAnalyzerTest method testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation.
@Test
public void testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation() throws Exception {
TableInfo t1 = executor.resolveTableInfo("t1");
TableRelation relation = new TableRelation(t1);
RelationName a1 = new RelationName(null, "a1");
RelationName a2 = new RelationName(null, "a2");
Map<RelationName, AnalyzedRelation> sources = Map.of(a1, new AliasedAnalyzedRelation(relation, a1), a2, new AliasedAnalyzedRelation(relation, a2));
SessionContext sessionContext = SessionContext.systemSessionContext();
CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, expressions.nodeCtx, paramTypeHints, new FullQualifiedNameFieldProvider(sources, ParentRelations.NO_PARENTS, sessionContext.searchPath().currentSchema()), null);
Function andFunction = (Function) expressionAnalyzer.convert(SqlParser.createExpression("not a1.x = 1 and not a2.x = 1"), context);
ScopedSymbol t1Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(0)).arguments().get(0)).arguments().get(0));
ScopedSymbol t2Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(1)).arguments().get(0)).arguments().get(0));
assertThat(t1Id.relation(), is(not(t2Id.relation())));
}
use of io.crate.expression.symbol.ScopedSymbol in project crate by crate.
the class ExcludedFieldProviderTest method testResolveFieldsAndValues.
@Test
public void testResolveFieldsAndValues() {
QualifiedName normalField1 = QualifiedName.of("field1");
QualifiedName normalField2 = QualifiedName.of("normal", "field2");
QualifiedName excludedName = QualifiedName.of("excluded", "field3");
FieldProvider<?> fieldProvider = (qualifiedName, path, operation, errorOnUnknownObjectKey) -> new ScopedSymbol(new RelationName("doc", "dummy"), new ColumnIdent(qualifiedName.toString()), DataTypes.INTEGER);
ValuesResolver valuesResolver = argumentColumn -> {
assertThat(argumentColumn, isField("field3"));
return Literal.of(42);
};
ExcludedFieldProvider excludedFieldProvider = new ExcludedFieldProvider(fieldProvider, valuesResolver);
assertThat(excludedFieldProvider.resolveField(normalField1, null, Operation.READ, DEFAULT_ERROR_ON_UNKNOWN_OBJECT_KEY), isField(normalField1.toString()));
assertThat(excludedFieldProvider.resolveField(normalField2, null, Operation.READ, DEFAULT_ERROR_ON_UNKNOWN_OBJECT_KEY), isField(normalField2.toString()));
assertThat(excludedFieldProvider.resolveField(excludedName, null, Operation.READ, DEFAULT_ERROR_ON_UNKNOWN_OBJECT_KEY), isLiteral(42));
}
use of io.crate.expression.symbol.ScopedSymbol in project crate by crate.
the class GroupHashAggregate method approximateDistinctValues.
static long approximateDistinctValues(long numSourceRows, TableStats tableStats, List<Symbol> groupKeys) {
long distinctValues = 1;
int numKeysWithStats = 0;
for (Symbol groupKey : groupKeys) {
Stats stats = null;
ColumnStats columnStats = null;
if (groupKey instanceof Reference) {
Reference ref = (Reference) groupKey;
stats = tableStats.getStats(ref.ident().tableIdent());
columnStats = stats.statsByColumn().get(ref.column());
numKeysWithStats++;
} else if (groupKey instanceof ScopedSymbol) {
ScopedSymbol scopedSymbol = (ScopedSymbol) groupKey;
stats = tableStats.getStats(scopedSymbol.relation());
columnStats = stats.statsByColumn().get(scopedSymbol.column());
numKeysWithStats++;
}
if (columnStats == null) {
// Assume worst case: Every value is unique
distinctValues *= numSourceRows;
} else {
// `approxDistinct` is the number of distinct values in relation to `stats.numDocs()ยด, not in
// relation to `numSourceRows`, which is based on the estimates of a source operator.
// That is why we calculate the cardinality ratio and calculate the new distinct
// values based on `numSourceRows` to account for changes in the number of rows in source operators
//
// e.g. SELECT x, count(*) FROM tbl GROUP BY x
// and SELECT x, count(*) FROM tbl WHERE pk = 1 GROUP BY x
//
// have a different number of groups
double cardinalityRatio = columnStats.approxDistinct() / stats.numDocs();
distinctValues *= (long) (numSourceRows * cardinalityRatio);
}
}
if (numKeysWithStats == groupKeys.size()) {
return Math.min(distinctValues, numSourceRows);
} else {
return numSourceRows;
}
}
Aggregations