use of io.crate.sql.tree.QualifiedName in project crate by crate.
the class SymbolPrinterTest method prepare.
@Before
public void prepare() throws Exception {
DocTableInfo tableInfo = TestingTableInfo.builder(new TableIdent(DocSchemaInfo.NAME, TABLE_NAME), null).add("foo", DataTypes.STRING).add("bar", DataTypes.LONG).add("CraZy", DataTypes.IP).add("select", DataTypes.BYTE).add("idx", DataTypes.INTEGER).add("s_arr", new ArrayType(DataTypes.STRING)).build();
Map<QualifiedName, AnalyzedRelation> sources = ImmutableMap.<QualifiedName, AnalyzedRelation>builder().put(QualifiedName.of(TABLE_NAME), new TableRelation(tableInfo)).build();
sqlExpressions = new SqlExpressions(sources);
printer = new SymbolPrinter(sqlExpressions.functions());
}
use of io.crate.sql.tree.QualifiedName in project crate by crate.
the class AbstractScalarFunctionsTest method prepareFunctions.
@Before
public void prepareFunctions() throws Exception {
DocTableInfo tableInfo = TestingTableInfo.builder(new TableIdent(DocSchemaInfo.NAME, "users"), null).add("id", DataTypes.INTEGER).add("name", DataTypes.STRING).add("tags", new ArrayType(DataTypes.STRING)).add("age", DataTypes.INTEGER).add("a", DataTypes.INTEGER).add("x", DataTypes.LONG).add("shape", DataTypes.GEO_SHAPE).add("timestamp", DataTypes.TIMESTAMP).add("timezone", DataTypes.STRING).add("interval", DataTypes.STRING).add("time_format", DataTypes.STRING).add("long_array", new ArrayType(DataTypes.LONG)).add("int_array", new ArrayType(DataTypes.INTEGER)).add("long_set", new SetType(DataTypes.LONG)).add("regex_pattern", DataTypes.STRING).add("geoshape", DataTypes.GEO_SHAPE).add("geopoint", DataTypes.GEO_POINT).add("geostring", DataTypes.STRING).add("is_awesome", DataTypes.BOOLEAN).add("double_val", DataTypes.DOUBLE).add("float_val", DataTypes.DOUBLE).add("short_val", DataTypes.SHORT).add("obj", DataTypes.OBJECT, ImmutableList.of()).build();
DocTableRelation tableRelation = new DocTableRelation(tableInfo);
tableSources = ImmutableMap.of(new QualifiedName("users"), tableRelation);
sqlExpressions = new SqlExpressions(tableSources);
functions = sqlExpressions.getInstance(Functions.class);
}
use of io.crate.sql.tree.QualifiedName in project crate by crate.
the class ManyTableConsumerTest method testOptimizeJoinNoPresort.
@Test
public void testOptimizeJoinNoPresort() throws Exception {
JoinPair pair1 = new JoinPair(T3.T1, T3.T2, JoinType.CROSS);
JoinPair pair2 = new JoinPair(T3.T2, T3.T3, JoinType.CROSS);
@SuppressWarnings("unchecked") Collection<QualifiedName> qualifiedNames = ManyTableConsumer.orderByJoinConditions(Arrays.asList(T3.T1, T3.T2, T3.T3), ImmutableSet.<Set<QualifiedName>>of(), ImmutableList.of(pair1, pair2), ImmutableList.<QualifiedName>of());
assertThat(qualifiedNames, Matchers.contains(T3.T1, T3.T2, T3.T3));
}
use of io.crate.sql.tree.QualifiedName in project crate by crate.
the class ManyTableConsumer method twoTableJoin.
static TwoTableJoin twoTableJoin(MultiSourceSelect mss) {
assert mss.sources().size() == 2 : "number of mss.sources() must be 2";
Iterator<QualifiedName> it = getOrderedRelationNames(mss, ImmutableSet.of()).iterator();
QualifiedName left = it.next();
QualifiedName right = it.next();
JoinPair joinPair = JoinPairs.ofRelationsWithMergedConditions(left, right, mss.joinPairs(), true);
RelationSource leftSource = mss.sources().get(left);
RelationSource rightSource = mss.sources().get(right);
JoinPairs.removeOrderByOnOuterRelation(left, right, leftSource.querySpec(), rightSource.querySpec(), joinPair);
Optional<OrderBy> remainingOrderByToApply = Optional.empty();
if (mss.remainingOrderBy().isPresent() && mss.remainingOrderBy().get().validForRelations(Sets.newHashSet(left, right))) {
remainingOrderByToApply = Optional.of(mss.remainingOrderBy().get().orderBy());
}
return new TwoTableJoin(mss.querySpec(), leftSource, rightSource, remainingOrderByToApply, joinPair);
}
use of io.crate.sql.tree.QualifiedName in project crate by crate.
the class ManyTableConsumer method buildTwoTableJoinTree.
/**
* build a TwoTableJoin tree.
* E.g. given a MSS with 3 tables:
* <code>
* select t1.a, t2.b, t3.c from t1, t2, t3
* </code>
* <p>
* a TwoTableJoin tree is built:
* <p>
* </code>
* join(
* join(t1, t2),
* t3
* )
* </code>
* <p>
* Where:
* <code>
* join(t1, t2)
* has:
* QS: [ RC(t1, 0), RC(t2, 0) ]
* t1: select a from t1
* t2: select b from t2
* </code>
* <p>
* and
* <code>
* join(join(t1, t2), t3)
* has:
* QS: [ RC(join(t1, t2), 0), RC(join(t1, t2), 1), RC(t3, 0) ]
* join(t1, t2) -
* t3: select c from t3
* <p>
* </code>
*/
static TwoTableJoin buildTwoTableJoinTree(MultiSourceSelect mss) {
Map<Set<QualifiedName>, Symbol> splitQuery = ImmutableMap.of();
if (mss.querySpec().where().hasQuery()) {
splitQuery = QuerySplitter.split(mss.querySpec().where().query());
mss.querySpec().where(WhereClause.MATCH_ALL);
}
Collection<QualifiedName> orderedRelationNames = getOrderedRelationNames(mss, splitQuery.keySet());
Iterator<QualifiedName> it = orderedRelationNames.iterator();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("relations={} orderedRelations={}", mss.sources().keySet(), orderedRelationNames);
}
QualifiedName leftName = it.next();
QuerySpec rootQuerySpec = mss.querySpec();
RelationSource leftSource = mss.sources().get(leftName);
AnalyzedRelation leftRelation = leftSource.relation();
QuerySpec leftQuerySpec = leftSource.querySpec();
Optional<RemainingOrderBy> remainingOrderBy = mss.remainingOrderBy();
List<JoinPair> joinPairs = mss.joinPairs();
List<TwoTableJoin> twoTableJoinList = new ArrayList<>(orderedRelationNames.size());
QualifiedName rightName;
RelationSource rightSource;
while (it.hasNext()) {
rightName = it.next();
rightSource = mss.sources().get(rightName);
// process where clause
Set<QualifiedName> names = Sets.newHashSet(leftName, rightName);
Predicate<Symbol> predicate = new SubSetOfQualifiedNamesPredicate(names);
QuerySpec newQuerySpec = rootQuerySpec.subset(predicate, it.hasNext());
if (splitQuery.containsKey(names)) {
Symbol symbol = splitQuery.remove(names);
newQuerySpec.where(new WhereClause(symbol));
}
Optional<OrderBy> remainingOrderByToApply = Optional.empty();
if (remainingOrderBy.isPresent() && remainingOrderBy.get().validForRelations(names)) {
remainingOrderByToApply = Optional.of(remainingOrderBy.get().orderBy());
remainingOrderBy = Optional.empty();
}
// get explicit join definition
JoinPair joinPair = JoinPairs.ofRelationsWithMergedConditions(leftName, rightName, joinPairs, true);
JoinPairs.removeOrderByOnOuterRelation(leftName, rightName, leftQuerySpec, rightSource.querySpec(), joinPair);
// NestedLoop will add NULL rows - so order by needs to be applied after the NestedLoop
TwoTableJoin join = new TwoTableJoin(newQuerySpec, new RelationSource(leftName, leftRelation, leftQuerySpec), rightSource, remainingOrderByToApply, joinPair);
assert leftQuerySpec != null : "leftQuerySpec must not be null";
final RelationColumnReWriteCtx reWriteCtx = new RelationColumnReWriteCtx(join);
Function<? super Symbol, Symbol> replaceFunction = new Function<Symbol, Symbol>() {
@Nullable
@Override
public Symbol apply(@Nullable Symbol input) {
if (input == null) {
return null;
}
return RelationColumnReWriter.INSTANCE.process(input, reWriteCtx);
}
};
/**
* Rewrite where, join and order by clauses and create a new query spec, where all RelationColumn symbols
* with a QualifiedName of {@link RelationColumnReWriteCtx#left} or {@link RelationColumnReWriteCtx#right}
* are replaced with a RelationColumn with QualifiedName of {@link RelationColumnReWriteCtx#newName}
*/
splitQuery = rewriteSplitQueryNames(splitQuery, leftName, rightName, join.name(), replaceFunction);
JoinPairs.rewriteNames(leftName, rightName, join.name(), replaceFunction, joinPairs);
rewriteOrderByNames(remainingOrderBy, leftName, rightName, join.name(), replaceFunction);
rootQuerySpec = rootQuerySpec.copyAndReplace(replaceFunction);
leftQuerySpec = newQuerySpec.copyAndReplace(replaceFunction);
leftRelation = join;
leftName = join.name();
twoTableJoinList.add(join);
}
TwoTableJoin join = (TwoTableJoin) leftRelation;
if (!splitQuery.isEmpty()) {
join.querySpec().where(new WhereClause(AndOperator.join(splitQuery.values())));
}
// Find the last join pair that contains a filtering
int index = 0;
for (int i = twoTableJoinList.size() - 1; i >= 0; i--) {
index = i;
WhereClause where = twoTableJoinList.get(i).querySpec().where();
if (where.hasQuery() && !(where.query() instanceof Literal)) {
break;
}
}
// Remove limit from all join pairs before the last filtered one
for (int i = 0; i < index; i++) {
twoTableJoinList.get(i).querySpec().limit(Optional.empty());
}
return join;
}
Aggregations