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) throws SQLException {
Map<String, Object> map = Maps.newLinkedHashMap();
AvaticaStatement statement = lookupStatement(handle);
final List<TypedValue> parameterValues = TROJAN.getParameterValues(statement);
if (MetaImpl.checkParameterValueHasNull(parameterValues)) {
throw new SQLException("exception while executing query: unbound parameter");
}
for (Ord<TypedValue> o : Ord.zip(parameterValues)) {
map.put("?" + o.i, o.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);
final DataContext dataContext = createDataContext(map, signature.rootSchema);
return signature.enumerable(dataContext);
}
use of org.apache.calcite.DataContext in project plc4x by apache.
the class Plc4xBaseTable method scan.
/**
* if tableCutoff is positive, then the row gets limited to that.
*/
public Enumerable<Object[]> scan(DataContext root) {
return new AbstractEnumerable<Object[]>() {
@Override
public Enumerator<Object[]> enumerator() {
return new Enumerator<Object[]>() {
private final AtomicLong counter = new AtomicLong(0);
@Override
public Object[] current() {
List<Object> objects = new ArrayList<>(Arrays.asList(new Timestamp(current.timestamp.toEpochMilli()), current.source));
List<Object> objects2 = names.stream().map(name -> current.values.get(name)).collect(Collectors.toList());
objects.addAll(objects2);
return objects.toArray();
}
@Override
public boolean moveNext() {
try {
current = queue.take();
// If stream, simply return
if (tableCutoff <= 0L) {
return true;
}
// If table, return if below cutoff
return counter.getAndIncrement() < tableCutoff;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return false;
}
@Override
public void reset() {
counter.set(0);
}
@Override
public void close() {
// Unimplemented
}
};
}
};
}
use of org.apache.calcite.DataContext in project calcite by apache.
the class TableScanNode method createProjectableFilterable.
private static TableScanNode createProjectableFilterable(Compiler compiler, TableScan rel, ImmutableList<RexNode> filters, @Nullable ImmutableIntList projects, ProjectableFilterableTable pfTable) {
final DataContext root = compiler.getDataContext();
final ImmutableIntList originalProjects = projects;
for (; ; ) {
final List<RexNode> mutableFilters = Lists.newArrayList(filters);
final int[] projectInts;
if (projects == null || projects.equals(TableScan.identity(rel.getTable()))) {
projectInts = null;
} else {
projectInts = projects.toIntArray();
}
for (RexNode filter : mutableFilters) {
if (!filters.contains(filter)) {
throw RESOURCE.filterableTableInventedFilter(filter.toString()).ex();
}
}
final ImmutableBitSet usedFields = RelOptUtil.InputFinder.bits(mutableFilters, null);
if (projects != null) {
int changeCount = 0;
for (int usedField : usedFields) {
if (!projects.contains(usedField)) {
// A field that is not projected is used in a filter that the
// table rejected. We won't be able to apply the filter later.
// Try again without any projects.
projects = ImmutableIntList.copyOf(Iterables.concat(projects, ImmutableList.of(usedField)));
++changeCount;
}
}
if (changeCount > 0) {
continue;
}
}
final Enumerable<@Nullable Object[]> enumerable1 = pfTable.scan(root, mutableFilters, projectInts);
final Enumerable<Row> rowEnumerable = Enumerables.toRow(enumerable1);
final ImmutableIntList rejectedProjects;
if (originalProjects == null || originalProjects.equals(projects)) {
rejectedProjects = null;
} else {
// We projected extra columns because they were needed in filters. Now
// project the leading columns.
rejectedProjects = ImmutableIntList.identity(originalProjects.size());
}
return createEnumerable(compiler, rel, rowEnumerable, projects, mutableFilters, rejectedProjects);
}
}
use of org.apache.calcite.DataContext in project calcite by apache.
the class CalciteMetaImpl method createResultSet.
@Override
protected MetaResultSet createResultSet(Map<String, Object> internalParameters, List<ColumnMetaData> columns, CursorFactory cursorFactory, final Frame firstFrame) {
try {
final CalciteConnectionImpl connection = getConnection();
final AvaticaStatement statement = connection.createStatement();
final CalcitePrepare.CalciteSignature<Object> signature = new CalcitePrepare.CalciteSignature<Object>("", ImmutableList.of(), internalParameters, null, columns, cursorFactory, null, ImmutableList.of(), -1, null, Meta.StatementType.SELECT) {
@Override
public Enumerable<Object> enumerable(DataContext dataContext) {
return Linq4j.asEnumerable(firstFrame.rows);
}
};
return MetaResultSet.create(connection.id, statement.getId(), true, signature, firstFrame);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
use of org.apache.calcite.DataContext in project calcite by apache.
the class RexProgramBuilderBase method setUp.
@BeforeEach
public void setUp() {
typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
rexBuilder = new RexBuilder(typeFactory);
final DataContext dataContext = DataContexts.of(ImmutableMap.of(DataContext.Variable.TIME_ZONE.camelName, TimeZone.getTimeZone("America/Los_Angeles"), DataContext.Variable.CURRENT_TIMESTAMP.camelName, 1311120000000L));
executor = new RexExecutorImpl(dataContext);
simplify = new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, executor).withParanoid(true);
trueLiteral = rexBuilder.makeLiteral(true);
falseLiteral = rexBuilder.makeLiteral(false);
nonNullableInt = typeFactory.createSqlType(SqlTypeName.INTEGER);
nullableInt = typeFactory.createTypeWithNullability(nonNullableInt, true);
nullInt = rexBuilder.makeNullLiteral(nullableInt);
nonNullableSmallInt = typeFactory.createSqlType(SqlTypeName.SMALLINT);
nullableSmallInt = typeFactory.createTypeWithNullability(nonNullableSmallInt, true);
nullSmallInt = rexBuilder.makeNullLiteral(nullableSmallInt);
nonNullableBool = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
nullableBool = typeFactory.createTypeWithNullability(nonNullableBool, true);
nullBool = rexBuilder.makeNullLiteral(nullableBool);
nonNullableVarchar = typeFactory.createSqlType(SqlTypeName.VARCHAR);
nullableVarchar = typeFactory.createTypeWithNullability(nonNullableVarchar, true);
nullVarchar = rexBuilder.makeNullLiteral(nullableVarchar);
nonNullableDecimal = typeFactory.createSqlType(SqlTypeName.DECIMAL);
nullableDecimal = typeFactory.createTypeWithNullability(nonNullableDecimal, true);
nullDecimal = rexBuilder.makeNullLiteral(nullableDecimal);
nonNullableVarbinary = typeFactory.createSqlType(SqlTypeName.VARBINARY);
nullableVarbinary = typeFactory.createTypeWithNullability(nonNullableVarbinary, true);
nullVarbinary = rexBuilder.makeNullLiteral(nullableVarbinary);
}
Aggregations