use of org.apache.calcite.util.Pair in project calcite by apache.
the class IdentifierNamespace method validateImpl.
public RelDataType validateImpl(RelDataType targetRowType) {
resolvedNamespace = Preconditions.checkNotNull(resolveImpl(id));
if (resolvedNamespace instanceof TableNamespace) {
SqlValidatorTable table = resolvedNamespace.getTable();
if (validator.shouldExpandIdentifiers()) {
// TODO: expand qualifiers for column references also
List<String> qualifiedNames = table.getQualifiedName();
if (qualifiedNames != null) {
// Assign positions to the components of the fully-qualified
// identifier, as best we can. We assume that qualification
// adds names to the front, e.g. FOO.BAR becomes BAZ.FOO.BAR.
List<SqlParserPos> poses = new ArrayList<>(Collections.nCopies(qualifiedNames.size(), id.getParserPosition()));
int offset = qualifiedNames.size() - id.names.size();
// reader.
if (offset >= 0) {
for (int i = 0; i < id.names.size(); i++) {
poses.set(i + offset, id.getComponentParserPosition(i));
}
}
id.setNames(qualifiedNames, poses);
}
}
}
RelDataType rowType = resolvedNamespace.getRowType();
if (extendList != null) {
if (!(resolvedNamespace instanceof TableNamespace)) {
throw new RuntimeException("cannot convert");
}
resolvedNamespace = ((TableNamespace) resolvedNamespace).extend(extendList);
rowType = resolvedNamespace.getRowType();
}
// Build a list of monotonic expressions.
final ImmutableList.Builder<Pair<SqlNode, SqlMonotonicity>> builder = ImmutableList.builder();
List<RelDataTypeField> fields = rowType.getFieldList();
for (RelDataTypeField field : fields) {
final String fieldName = field.getName();
final SqlMonotonicity monotonicity = resolvedNamespace.getMonotonicity(fieldName);
if (monotonicity != SqlMonotonicity.NOT_MONOTONIC) {
builder.add(Pair.of((SqlNode) new SqlIdentifier(fieldName, SqlParserPos.ZERO), monotonicity));
}
}
monotonicExprs = builder.build();
// Validation successful.
return rowType;
}
use of org.apache.calcite.util.Pair in project calcite by apache.
the class DelegatingScope method resolveInNamespace.
/**
* If a record type allows implicit references to fields, recursively looks
* into the fields. Otherwise returns immediately.
*/
void resolveInNamespace(SqlValidatorNamespace ns, boolean nullable, List<String> names, SqlNameMatcher nameMatcher, Path path, Resolved resolved) {
if (names.isEmpty()) {
resolved.found(ns, nullable, this, path, null);
return;
}
final RelDataType rowType = ns.getRowType();
if (rowType.isStruct()) {
SqlValidatorTable validatorTable = ns.getTable();
if (validatorTable instanceof Prepare.PreparingTable) {
Table t = ((Prepare.PreparingTable) validatorTable).unwrap(Table.class);
if (t instanceof CustomColumnResolvingTable) {
final List<Pair<RelDataTypeField, List<String>>> entries = ((CustomColumnResolvingTable) t).resolveColumn(rowType, validator.getTypeFactory(), names);
for (Pair<RelDataTypeField, List<String>> entry : entries) {
final RelDataTypeField field = entry.getKey();
final List<String> remainder = entry.getValue();
final SqlValidatorNamespace ns2 = new FieldNamespace(validator, field.getType());
final Step path2 = path.plus(rowType, field.getIndex(), field.getName(), StructKind.FULLY_QUALIFIED);
resolveInNamespace(ns2, nullable, remainder, nameMatcher, path2, resolved);
}
return;
}
}
final String name = names.get(0);
final RelDataTypeField field0 = nameMatcher.field(rowType, name);
if (field0 != null) {
final SqlValidatorNamespace ns2 = ns.lookupChild(field0.getName());
final Step path2 = path.plus(rowType, field0.getIndex(), field0.getName(), StructKind.FULLY_QUALIFIED);
resolveInNamespace(ns2, nullable, names.subList(1, names.size()), nameMatcher, path2, resolved);
} else {
for (RelDataTypeField field : rowType.getFieldList()) {
switch(field.getType().getStructKind()) {
case PEEK_FIELDS:
case PEEK_FIELDS_DEFAULT:
case PEEK_FIELDS_NO_EXPAND:
final Step path2 = path.plus(rowType, field.getIndex(), field.getName(), field.getType().getStructKind());
final SqlValidatorNamespace ns2 = ns.lookupChild(field.getName());
resolveInNamespace(ns2, nullable, names, nameMatcher, path2, resolved);
}
}
}
}
}
use of org.apache.calcite.util.Pair in project drill by axbaretto.
the class NumberingRelWriter method done.
public RelWriter done(RelNode node) {
int i = 0;
if (values.size() > 0 && values.get(0).left.equals("subset")) {
++i;
}
for (RelNode input : node.getInputs()) {
assert values.get(i).right == input;
++i;
}
for (RexNode expr : node.getChildExps()) {
assert values.get(i).right == expr;
++i;
}
final List<Pair<String, Object>> valuesCopy = ImmutableList.copyOf(values);
values.clear();
explain_(node, valuesCopy);
pw.flush();
return this;
}
use of org.apache.calcite.util.Pair in project drill by apache.
the class NumberingRelWriter method done.
@SuppressWarnings("deprecation")
public RelWriter done(RelNode node) {
int i = 0;
if (values.size() > 0 && values.get(0).left.equals("subset")) {
++i;
}
for (RelNode input : node.getInputs()) {
assert values.get(i).right == input;
++i;
}
for (RexNode expr : node.getChildExps()) {
assert values.get(i).right == expr;
++i;
}
final List<Pair<String, Object>> valuesCopy = ImmutableList.copyOf(values);
values.clear();
explain_(node, valuesCopy);
pw.flush();
return this;
}
use of org.apache.calcite.util.Pair in project drill by apache.
the class CodeGenMemberInjector method injectMembers.
/**
* Generated code for a class may have several class members, they
* are initialized by invoking this method when the instance created.
*
* @param cg the class generator
* @param instance the class instance created by the compiler
* @param context the fragment context
*/
public static void injectMembers(ClassGenerator<?> cg, Object instance, FragmentContext context) {
Map<Integer, Object> cachedInstances = new HashMap<>();
for (Map.Entry<Pair<Integer, JVar>, Function<DrillBuf, ? extends ValueHolder>> setter : cg.getConstantVars().entrySet()) {
try {
JVar var = setter.getKey().getValue();
Integer depth = setter.getKey().getKey();
Object varInstance = getFieldInstance(instance, depth, cachedInstances);
Field field = varInstance.getClass().getDeclaredField(var.name());
field.setAccessible(true);
field.set(varInstance, setter.getValue().apply(context.getManagedBuffer()));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}
Aggregations