use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class ElasticsearchSort method implement.
@Override
public void implement(Implementor implementor) {
implementor.visitChild(0, getInput());
if (!collation.getFieldCollations().isEmpty()) {
final List<String> keys = new ArrayList<>();
if (input instanceof Project) {
final List<RexNode> projects = ((Project) input).getProjects();
for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
RexNode project = projects.get(fieldCollation.getFieldIndex());
String name = project.accept(MapProjectionFieldVisitor.INSTANCE);
keys.add(ElasticsearchRules.quote(name) + ": " + direction(fieldCollation));
}
} else {
final List<RelDataTypeField> fields = getRowType().getFieldList();
for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
final String name = fields.get(fieldCollation.getFieldIndex()).getName();
keys.add(ElasticsearchRules.quote(name) + ": " + direction(fieldCollation));
}
}
implementor.add("\"sort\": [ " + Util.toString(keys, "{", "}, {", "}") + "]");
}
if (offset != null) {
implementor.add("\"from\": " + ((RexLiteral) offset).getValue());
}
if (fetch != null) {
implementor.add("\"size\": " + ((RexLiteral) fetch).getValue());
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RelMdCollation method values.
/**
* Helper method to determine a
* {@link org.apache.calcite.rel.core.Values}'s collation.
*
* <p>We actually under-report the collations. A Values with 0 or 1 rows - an
* edge case, but legitimate and very common - is ordered by every permutation
* of every subset of the columns.
*
* <p>So, our algorithm aims to:<ul>
* <li>produce at most N collations (where N is the number of columns);
* <li>make each collation as long as possible;
* <li>do not repeat combinations already emitted -
* if we've emitted {@code (a, b)} do not later emit {@code (b, a)};
* <li>probe the actual values and make sure that each collation is
* consistent with the data
* </ul>
*
* <p>So, for an empty Values with 4 columns, we would emit
* {@code (a, b, c, d), (b, c, d), (c, d), (d)}.
*/
public static List<RelCollation> values(RelMetadataQuery mq, RelDataType rowType, ImmutableList<ImmutableList<RexLiteral>> tuples) {
// for future use
Util.discard(mq);
final List<RelCollation> list = Lists.newArrayList();
final int n = rowType.getFieldCount();
final List<Pair<RelFieldCollation, Ordering<List<RexLiteral>>>> pairs = Lists.newArrayList();
outer: for (int i = 0; i < n; i++) {
pairs.clear();
for (int j = i; j < n; j++) {
final RelFieldCollation fieldCollation = new RelFieldCollation(j);
Ordering<List<RexLiteral>> comparator = comparator(fieldCollation);
Ordering<List<RexLiteral>> ordering;
if (pairs.isEmpty()) {
ordering = comparator;
} else {
ordering = Util.last(pairs).right.compound(comparator);
}
pairs.add(Pair.of(fieldCollation, ordering));
if (!ordering.isOrdered(tuples)) {
if (j == i) {
continue outer;
}
pairs.remove(pairs.size() - 1);
}
}
if (!pairs.isEmpty()) {
list.add(RelCollations.of(Pair.left(pairs)));
}
}
return list;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RelJsonReader method readRel.
private void readRel(final Map<String, Object> jsonRel) {
String id = (String) jsonRel.get("id");
String type = (String) jsonRel.get("relOp");
Constructor constructor = relJson.getConstructor(type);
RelInput input = new RelInput() {
public RelOptCluster getCluster() {
return cluster;
}
public RelTraitSet getTraitSet() {
return cluster.traitSetOf(Convention.NONE);
}
public RelOptTable getTable(String table) {
final List<String> list = getStringList(table);
return relOptSchema.getTableForMember(list);
}
public RelNode getInput() {
final List<RelNode> inputs = getInputs();
assert inputs.size() == 1;
return inputs.get(0);
}
public List<RelNode> getInputs() {
final List<String> jsonInputs = getStringList("inputs");
if (jsonInputs == null) {
return ImmutableList.of(lastRel);
}
final List<RelNode> inputs = new ArrayList<>();
for (String jsonInput : jsonInputs) {
inputs.add(lookupInput(jsonInput));
}
return inputs;
}
public RexNode getExpression(String tag) {
return relJson.toRex(this, jsonRel.get(tag));
}
public ImmutableBitSet getBitSet(String tag) {
return ImmutableBitSet.of(getIntegerList(tag));
}
public List<ImmutableBitSet> getBitSetList(String tag) {
List<List<Integer>> list = getIntegerListList(tag);
if (list == null) {
return null;
}
final ImmutableList.Builder<ImmutableBitSet> builder = ImmutableList.builder();
for (List<Integer> integers : list) {
builder.add(ImmutableBitSet.of(integers));
}
return builder.build();
}
public List<String> getStringList(String tag) {
// noinspection unchecked
return (List<String>) jsonRel.get(tag);
}
public List<Integer> getIntegerList(String tag) {
// noinspection unchecked
return (List<Integer>) jsonRel.get(tag);
}
public List<List<Integer>> getIntegerListList(String tag) {
// noinspection unchecked
return (List<List<Integer>>) jsonRel.get(tag);
}
public List<AggregateCall> getAggregateCalls(String tag) {
@SuppressWarnings("unchecked") final List<Map<String, Object>> jsonAggs = (List) jsonRel.get(tag);
final List<AggregateCall> inputs = new ArrayList<>();
for (Map<String, Object> jsonAggCall : jsonAggs) {
inputs.add(toAggCall(jsonAggCall));
}
return inputs;
}
public Object get(String tag) {
return jsonRel.get(tag);
}
public String getString(String tag) {
return (String) jsonRel.get(tag);
}
public float getFloat(String tag) {
return ((Number) jsonRel.get(tag)).floatValue();
}
public boolean getBoolean(String tag, boolean default_) {
final Boolean b = (Boolean) jsonRel.get(tag);
return b != null ? b : default_;
}
public <E extends Enum<E>> E getEnum(String tag, Class<E> enumClass) {
return Util.enumVal(enumClass, getString(tag).toUpperCase(Locale.ROOT));
}
public List<RexNode> getExpressionList(String tag) {
@SuppressWarnings("unchecked") final List<Object> jsonNodes = (List) jsonRel.get(tag);
final List<RexNode> nodes = new ArrayList<>();
for (Object jsonNode : jsonNodes) {
nodes.add(relJson.toRex(this, jsonNode));
}
return nodes;
}
public RelDataType getRowType(String tag) {
final Object o = jsonRel.get(tag);
return relJson.toType(cluster.getTypeFactory(), o);
}
public RelDataType getRowType(String expressionsTag, String fieldsTag) {
final List<RexNode> expressionList = getExpressionList(expressionsTag);
@SuppressWarnings("unchecked") final List<String> names = (List<String>) get(fieldsTag);
return cluster.getTypeFactory().createStructType(new AbstractList<Map.Entry<String, RelDataType>>() {
@Override
public Map.Entry<String, RelDataType> get(int index) {
return Pair.of(names.get(index), expressionList.get(index).getType());
}
@Override
public int size() {
return names.size();
}
});
}
public RelCollation getCollation() {
// noinspection unchecked
return relJson.toCollation((List) get("collation"));
}
public RelDistribution getDistribution() {
return relJson.toDistribution(get("distribution"));
}
public ImmutableList<ImmutableList<RexLiteral>> getTuples(String tag) {
// noinspection unchecked
final List<List> jsonTuples = (List) get(tag);
final ImmutableList.Builder<ImmutableList<RexLiteral>> builder = ImmutableList.builder();
for (List jsonTuple : jsonTuples) {
builder.add(getTuple(jsonTuple));
}
return builder.build();
}
public ImmutableList<RexLiteral> getTuple(List jsonTuple) {
final ImmutableList.Builder<RexLiteral> builder = ImmutableList.builder();
for (Object jsonValue : jsonTuple) {
builder.add((RexLiteral) relJson.toRex(this, jsonValue));
}
return builder.build();
}
};
try {
final RelNode rel = (RelNode) constructor.newInstance(input);
relMap.put(id, rel);
lastRel = rel;
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
final Throwable e2 = e.getCause();
if (e2 instanceof RuntimeException) {
throw (RuntimeException) e2;
}
throw new RuntimeException(e2);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RelMdSize method averageColumnSizes.
public List<Double> averageColumnSizes(Values rel, RelMetadataQuery mq) {
final List<RelDataTypeField> fields = rel.getRowType().getFieldList();
final ImmutableList.Builder<Double> list = ImmutableList.builder();
for (int i = 0; i < fields.size(); i++) {
RelDataTypeField field = fields.get(i);
double d;
if (rel.getTuples().isEmpty()) {
d = averageTypeValueSize(field.getType());
} else {
d = 0;
for (ImmutableList<RexLiteral> literals : rel.getTuples()) {
d += typeValueSize(field.getType(), literals.get(i).getValueAs(Comparable.class));
}
d /= rel.getTuples().size();
}
list.add(d);
}
return list.build();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class Values method assertRowType.
/**
* Returns true if all tuples match rowType; otherwise, assert on
* mismatch.
*/
private boolean assertRowType() {
for (List<RexLiteral> tuple : tuples) {
assert tuple.size() == rowType.getFieldCount();
for (Pair<RexLiteral, RelDataTypeField> pair : Pair.zip(tuple, rowType.getFieldList())) {
RexLiteral literal = pair.left;
RelDataType fieldType = pair.right.getType();
// been dealt with.
if (!RexLiteral.isNullLiteral(literal)) {
assert SqlTypeUtil.canAssignFrom(fieldType, literal.getType()) : "to " + fieldType + " from " + literal;
}
}
}
return true;
}
Aggregations