Search in sources :

Example 1 with ScopedSymbol

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;
}
Also used : VoidReference(io.crate.expression.symbol.VoidReference) Symbol(io.crate.expression.symbol.Symbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol)

Example 2 with 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;
}
Also used : Symbol(io.crate.expression.symbol.Symbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Reference(io.crate.metadata.Reference) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) FixedWidthType(io.crate.types.FixedWidthType)

Example 3 with ScopedSymbol

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())));
}
Also used : CoalesceFunction(io.crate.expression.scalar.conditional.CoalesceFunction) SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) ArraySliceFunction(io.crate.expression.scalar.ArraySliceFunction) ImplicitCastFunction(io.crate.expression.scalar.cast.ImplicitCastFunction) Function(io.crate.expression.symbol.Function) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) RelationName(io.crate.metadata.RelationName) SessionContext(io.crate.action.sql.SessionContext) TableInfo(io.crate.metadata.table.TableInfo) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) TableRelation(io.crate.analyze.relations.TableRelation) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 4 with ScopedSymbol

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));
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) Literal(io.crate.expression.symbol.Literal) SymbolMatchers.isField(io.crate.testing.SymbolMatchers.isField) RelationName(io.crate.metadata.RelationName) DataTypes(io.crate.types.DataTypes) ColumnIdent(io.crate.metadata.ColumnIdent) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) ValuesResolver(io.crate.analyze.ValuesResolver) Test(org.junit.Test) Operation(io.crate.metadata.table.Operation) QualifiedName(io.crate.sql.tree.QualifiedName) SymbolMatchers.isLiteral(io.crate.testing.SymbolMatchers.isLiteral) ValuesResolver(io.crate.analyze.ValuesResolver) ColumnIdent(io.crate.metadata.ColumnIdent) QualifiedName(io.crate.sql.tree.QualifiedName) RelationName(io.crate.metadata.RelationName) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Test(org.junit.Test)

Example 5 with ScopedSymbol

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;
    }
}
Also used : ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Symbol(io.crate.expression.symbol.Symbol) ColumnStats(io.crate.statistics.ColumnStats) Reference(io.crate.metadata.Reference) ColumnStats(io.crate.statistics.ColumnStats) TableStats(io.crate.statistics.TableStats) Stats(io.crate.statistics.Stats) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol)

Aggregations

ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)8 Symbol (io.crate.expression.symbol.Symbol)6 Reference (io.crate.metadata.Reference)3 RelationName (io.crate.metadata.RelationName)3 VoidReference (io.crate.expression.symbol.VoidReference)2 ColumnIdent (io.crate.metadata.ColumnIdent)2 TableStats (io.crate.statistics.TableStats)2 IdentityHashMap (java.util.IdentityHashMap)2 Test (org.junit.Test)2 SessionContext (io.crate.action.sql.SessionContext)1 OrderBy (io.crate.analyze.OrderBy)1 ValuesResolver (io.crate.analyze.ValuesResolver)1 AliasedAnalyzedRelation (io.crate.analyze.relations.AliasedAnalyzedRelation)1 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)1 FullQualifiedNameFieldProvider (io.crate.analyze.relations.FullQualifiedNameFieldProvider)1 TableRelation (io.crate.analyze.relations.TableRelation)1 ArraySliceFunction (io.crate.expression.scalar.ArraySliceFunction)1 ImplicitCastFunction (io.crate.expression.scalar.cast.ImplicitCastFunction)1 CoalesceFunction (io.crate.expression.scalar.conditional.CoalesceFunction)1 FieldsVisitor (io.crate.expression.symbol.FieldsVisitor)1