use of org.apache.calcite.DataContext in project calcite by apache.
the class RexImplicationChecker method implies2.
/**
* Returns whether the predicate {@code first} (not a conjunction)
* implies {@code second}.
*/
private boolean implies2(RexNode first, RexNode second) {
if (second.isAlwaysFalse()) {
// f cannot imply s
return false;
}
// E.g. "x is null" implies "x is null".
if (first.equals(second)) {
return true;
}
// Several things imply "IS NOT NULL"
switch(second.getKind()) {
case IS_NOT_NULL:
// Suppose we know that first is strong in second; that is,
// the if second is null, then first will be null.
// Then, first being not null implies that second is not null.
//
// For example, first is "x > y", second is "x".
// If we know that "x > y" is not null, we know that "x" is not null.
final RexNode operand = ((RexCall) second).getOperands().get(0);
final Strong strong = new Strong() {
@Override
public boolean isNull(RexNode node) {
return node.equals(operand) || super.isNull(node);
}
};
if (strong.isNull(first)) {
return true;
}
break;
default:
break;
}
final InputUsageFinder firstUsageFinder = new InputUsageFinder();
final InputUsageFinder secondUsageFinder = new InputUsageFinder();
RexUtil.apply(firstUsageFinder, ImmutableList.of(), first);
RexUtil.apply(secondUsageFinder, ImmutableList.of(), second);
// Check Support
if (!checkSupport(firstUsageFinder, secondUsageFinder)) {
LOGGER.warn("Support for checking {} => {} is not there", first, second);
return false;
}
ImmutableList.Builder<Set<Pair<RexInputRef, @Nullable RexNode>>> usagesBuilder = ImmutableList.builder();
for (Map.Entry<RexInputRef, InputRefUsage<SqlOperator, @Nullable RexNode>> entry : firstUsageFinder.usageMap.entrySet()) {
ImmutableSet.Builder<Pair<RexInputRef, @Nullable RexNode>> usageBuilder = ImmutableSet.builder();
if (entry.getValue().usageList.size() > 0) {
for (final Pair<SqlOperator, @Nullable RexNode> pair : entry.getValue().usageList) {
usageBuilder.add(Pair.of(entry.getKey(), pair.getValue()));
}
usagesBuilder.add(usageBuilder.build());
}
}
final Set<List<Pair<RexInputRef, @Nullable RexNode>>> usages = Sets.cartesianProduct(usagesBuilder.build());
for (List<Pair<RexInputRef, @Nullable RexNode>> usageList : usages) {
// Get the literals from first conjunction and executes second conjunction
// using them.
//
// E.g., for
// x > 30 ⇒ x > 10,
// we will replace x by 30 in second expression and execute it i.e.,
// 30 > 10
//
// If it's true then we infer implication.
final DataContext dataValues = VisitorDataContext.of(rowType, usageList);
if (!isSatisfiable(second, dataValues)) {
return false;
}
}
return true;
}
use of org.apache.calcite.DataContext in project calcite by apache.
the class PsTableFunction method eval.
public static ScannableTable eval(boolean b) {
return new ScannableTable() {
@Override
public Enumerable<@Nullable Object[]> scan(DataContext root) {
JavaTypeFactory typeFactory = root.getTypeFactory();
final RelDataType rowType = getRowType(typeFactory);
final List<String> fieldNames = ImmutableList.copyOf(rowType.getFieldNames());
final String[] args;
final String osName = System.getProperty("os.name");
final String osVersion = System.getProperty("os.version");
Util.discard(osVersion);
switch(osName) {
case // tested on version 10.12.5
"Mac OS X":
args = new String[] { "ps", "ax", "-o", "ppid=,pid=,pgid=,tpgid=,stat=," + "user=,pcpu=,pmem=,vsz=,rss=,tty=,start=,time=,uid=,ruid=," + "sess=,comm=" };
break;
default:
args = new String[] { "ps", "--no-headers", "axo", "ppid,pid,pgrp," + "tpgid,stat,user,pcpu,pmem,vsz,rss,tty,start_time,time,euid," + "ruid,sess,comm" };
}
return Processes.processLines(args).select(new Function1<String, Object[]>() {
@Override
public Object[] apply(String line) {
final String[] fields = line.trim().split(" +");
final Object[] values = new Object[fieldNames.size()];
for (int i = 0; i < values.length; i++) {
try {
values[i] = field(fieldNames.get(i), fields[i]);
} catch (RuntimeException e) {
throw new RuntimeException("while parsing value [" + fields[i] + "] of field [" + fieldNames.get(i) + "] in line [" + line + "]");
}
}
return values;
}
private Object field(String field, String value) {
switch(field) {
case "pid":
case "ppid":
// linux only; macOS equivalent is "pgid"
case "pgrp":
// see "pgrp"
case "pgid":
case "tpgid":
return Integer.valueOf(value);
case "pcpu":
case "pmem":
return (int) (Float.valueOf(value) * 10f);
case "time":
final Matcher m1 = MINUTE_SECOND_MILLIS_PATTERN.matcher(value);
if (m1.matches()) {
final long h = Long.parseLong(m1.group(1));
final long m = Long.parseLong(m1.group(2));
final long s = Long.parseLong(m1.group(3));
return h * 3600000L + m * 60000L + s * 1000L;
}
final Matcher m2 = HOUR_MINUTE_SECOND_PATTERN.matcher(value);
if (m2.matches()) {
final long m = Long.parseLong(m2.group(1));
final long s = Long.parseLong(m2.group(2));
String g3 = m2.group(3);
while (g3.length() < 3) {
g3 = g3 + "0";
}
final long millis = Long.parseLong(g3);
return m * 60000L + s * 1000L + millis;
}
return 0L;
// linux only; macOS version is "lstart"
case "start_time":
// see "start_time"
case "lstart":
// linux only; macOS equivalent is "uid"
case "euid":
// see "euid"
case "uid":
default:
return value;
}
}
});
}
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
return typeFactory.builder().add("pid", SqlTypeName.INTEGER).add("ppid", SqlTypeName.INTEGER).add("pgrp", SqlTypeName.INTEGER).add("tpgid", SqlTypeName.INTEGER).add("stat", SqlTypeName.VARCHAR).add("user", SqlTypeName.VARCHAR).add("pcpu", SqlTypeName.DECIMAL, 3, 1).add("pmem", SqlTypeName.DECIMAL, 3, 1).add("vsz", SqlTypeName.INTEGER).add("rss", SqlTypeName.INTEGER).add("tty", SqlTypeName.VARCHAR).add("start_time", SqlTypeName.VARCHAR).add("time", TimeUnit.HOUR, -1, TimeUnit.SECOND, 0).add("euid", SqlTypeName.VARCHAR).add("ruid", SqlTypeName.VARCHAR).add("sess", SqlTypeName.VARCHAR).add("command", SqlTypeName.VARCHAR).build();
}
@Override
public Statistic getStatistic() {
return Statistics.of(1000d, ImmutableList.of(ImmutableBitSet.of(1)));
}
@Override
public Schema.TableType getJdbcTableType() {
return Schema.TableType.TABLE;
}
@Override
public boolean isRolledUp(String column) {
return false;
}
@Override
public boolean rolledUpColumnValidInsideAgg(String column, SqlCall call, @Nullable SqlNode parent, @Nullable CalciteConnectionConfig config) {
return true;
}
};
}
use of org.apache.calcite.DataContext in project calcite by apache.
the class CalciteConnectionImpl method enumerable.
public <T> Enumerable<T> enumerable(Meta.StatementHandle handle, CalcitePrepare.CalciteSignature<T> signature, @Nullable List<TypedValue> parameterValues0) throws SQLException {
Map<String, Object> map = new LinkedHashMap<>();
AvaticaStatement statement = lookupStatement(handle);
final List<TypedValue> parameterValues;
if (parameterValues0 == null || parameterValues0.isEmpty()) {
parameterValues = TROJAN.getParameterValues(statement);
} else {
parameterValues = parameterValues0;
}
if (MetaImpl.checkParameterValueHasNull(parameterValues)) {
throw new SQLException("exception while executing query: unbound parameter");
}
Ord.forEach(parameterValues, (e, i) -> map.put("?" + i, e.toLocal()));
map.putAll(signature.internalParameters);
final AtomicBoolean cancelFlag;
try {
cancelFlag = getCancelFlag(handle);
} catch (NoSuchStatementException e) {
throw new RuntimeException(e);
}
map.put(DataContext.Variable.CANCEL_FLAG.camelName, cancelFlag);
int queryTimeout = statement.getQueryTimeout();
// Avoid overflow
if (queryTimeout > 0 && queryTimeout < Integer.MAX_VALUE / 1000) {
map.put(DataContext.Variable.TIMEOUT.camelName, queryTimeout * 1000L);
}
final DataContext dataContext = createDataContext(map, signature.rootSchema);
return signature.enumerable(dataContext);
}
use of org.apache.calcite.DataContext in project calcite by apache.
the class TableScanNode method createFilterable.
private static TableScanNode createFilterable(Compiler compiler, TableScan rel, ImmutableList<RexNode> filters, @Nullable ImmutableIntList projects, FilterableTable filterableTable) {
final DataContext root = compiler.getDataContext();
final List<RexNode> mutableFilters = Lists.newArrayList(filters);
final Enumerable<@Nullable Object[]> enumerable = filterableTable.scan(root, mutableFilters);
for (RexNode filter : mutableFilters) {
if (!filters.contains(filter)) {
throw RESOURCE.filterableTableInventedFilter(filter.toString()).ex();
}
}
final Enumerable<Row> rowEnumerable = Enumerables.toRow(enumerable);
return createEnumerable(compiler, rel, rowEnumerable, null, mutableFilters, projects);
}
use of org.apache.calcite.DataContext in project calcite by apache.
the class TableScanNode method createQueryable.
private static TableScanNode createQueryable(Compiler compiler, TableScan rel, ImmutableList<RexNode> filters, @Nullable ImmutableIntList projects, QueryableTable queryableTable) {
final DataContext root = compiler.getDataContext();
final RelOptTable relOptTable = rel.getTable();
final Type elementType = queryableTable.getElementType();
SchemaPlus schema = root.getRootSchema();
for (String name : Util.skipLast(relOptTable.getQualifiedName())) {
requireNonNull(schema, () -> "schema is null while resolving " + name + " for table" + relOptTable.getQualifiedName());
schema = schema.getSubSchema(name);
}
final Enumerable<Row> rowEnumerable;
if (elementType instanceof Class) {
// noinspection unchecked
final Queryable<Object> queryable = Schemas.queryable(root, (Class) elementType, relOptTable.getQualifiedName());
ImmutableList.Builder<Field> fieldBuilder = ImmutableList.builder();
Class type = (Class) elementType;
for (Field field : type.getFields()) {
if (Modifier.isPublic(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) {
fieldBuilder.add(field);
}
}
final List<Field> fields = fieldBuilder.build();
rowEnumerable = queryable.select(o -> {
@Nullable final Object[] values = new Object[fields.size()];
for (int i = 0; i < fields.size(); i++) {
Field field = fields.get(i);
try {
values[i] = field.get(o);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return new Row(values);
});
} else {
rowEnumerable = Schemas.queryable(root, Row.class, relOptTable.getQualifiedName());
}
return createEnumerable(compiler, rel, rowEnumerable, null, filters, projects);
}
Aggregations