Java Code Examples for org.apache.calcite.DataContext

The following examples show how to use org.apache.calcite.DataContext. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: calcite   Source File: JdbcCatalogSchema.java    License: Apache License 2.0 6 votes vote down vote up
public static JdbcCatalogSchema create(
    SchemaPlus parentSchema,
    String name,
    DataSource dataSource,
    SqlDialectFactory dialectFactory,
    String catalog) {
  final Expression expression =
      parentSchema != null
          ? Schemas.subSchemaExpression(parentSchema, name,
              JdbcCatalogSchema.class)
          : Expressions.call(DataContext.ROOT,
              BuiltInMethod.DATA_CONTEXT_GET_ROOT_SCHEMA.method);
  final SqlDialect dialect =
      JdbcSchema.createDialect(dialectFactory, dataSource);
  final JdbcConvention convention =
      JdbcConvention.of(dialect, expression, name);
  return new JdbcCatalogSchema(dataSource, dialect, convention, catalog);
}
 
Example 2
Source Project: Bats   Source File: RexExecutable.java    License: Apache License 2.0 6 votes vote down vote up
private static Function1<DataContext, Object[]> compile(String code,
    Object reason) {
  try {
    final ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.setClassName(GENERATED_CLASS_NAME);
    cbe.setExtendedClass(Utilities.class);
    cbe.setImplementedInterfaces(new Class[] {Function1.class, Serializable.class});
    cbe.setParentClassLoader(RexExecutable.class.getClassLoader());
    cbe.cook(new Scanner(null, new StringReader(code)));
    Class c = cbe.getClazz();
    //noinspection unchecked
    final Constructor<Function1<DataContext, Object[]>> constructor =
        c.getConstructor();
    return constructor.newInstance();
  } catch (CompileException | IOException | InstantiationException
      | IllegalAccessException | InvocationTargetException
      | NoSuchMethodException e) {
    throw new RuntimeException("While compiling " + reason, e);
  }
}
 
Example 3
Source Project: calcite   Source File: RexExecutorTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Ensures that for a given context operator,
 * the correct value is retrieved from the {@link DataContext}.
 *
 * @param operator The Operator to check
 * @param variable The DataContext variable this operator should be bound to
 * @param value The expected value to retrieve.
 */
private void testContextLiteral(
    final SqlOperator operator,
    final DataContext.Variable variable,
    final Object value) {
  Frameworks.withPrepare((cluster, relOptSchema, rootSchema, statement) -> {
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    final RexExecutorImpl executor =
        new RexExecutorImpl(
            new SingleValueDataContext(variable.camelName, value));
    try {
      checkConstant(value, builder -> {
        final List<RexNode> output = new ArrayList<>();
        executor.reduce(rexBuilder,
            ImmutableList.of(rexBuilder.makeCall(operator)), output);
        return output.get(0);
      });
    } catch (Exception e) {
      throw TestUtil.rethrow(e);
    }
    return null;
  });
}
 
Example 4
Source Project: calcite   Source File: RexExecutable.java    License: Apache License 2.0 6 votes vote down vote up
private static Function1<DataContext, Object[]> compile(String code,
    Object reason) {
  try {
    final ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.setClassName(GENERATED_CLASS_NAME);
    cbe.setExtendedClass(Utilities.class);
    cbe.setImplementedInterfaces(new Class[] {Function1.class, Serializable.class});
    cbe.setParentClassLoader(RexExecutable.class.getClassLoader());
    cbe.cook(new Scanner(null, new StringReader(code)));
    Class c = cbe.getClazz();
    //noinspection unchecked
    final Constructor<Function1<DataContext, Object[]>> constructor =
        c.getConstructor();
    return constructor.newInstance();
  } catch (CompileException | IOException | InstantiationException
      | IllegalAccessException | InvocationTargetException
      | NoSuchMethodException e) {
    throw new RuntimeException("While compiling " + reason, e);
  }
}
 
Example 5
Source Project: calcite   Source File: RexImplicationChecker.java    License: Apache License 2.0 6 votes vote down vote up
private boolean isSatisfiable(RexNode second, DataContext dataValues) {
  if (dataValues == null) {
    return false;
  }

  ImmutableList<RexNode> constExps = ImmutableList.of(second);
  final RexExecutable exec = RexExecutorImpl.getExecutable(builder, constExps, rowType);

  Object[] result;
  exec.setDataContext(dataValues);
  try {
    result = exec.execute();
  } catch (Exception e) {
    // TODO: CheckSupport should not allow this exception to be thrown
    // Need to monitor it and handle all the cases raising them.
    LOGGER.warn("Exception thrown while checking if => {}: {}", second, e.getMessage());
    return false;
  }
  return result != null
      && result.length == 1
      && result[0] instanceof Boolean
      && (Boolean) result[0];
}
 
Example 6
Source Project: calcite   Source File: TableFunctionImpl.java    License: Apache License 2.0 6 votes vote down vote up
private static CallImplementor createImplementor(final Method method) {
  return RexImpTable.createImplementor(
      new ReflectiveCallNotNullImplementor(method) {
        public Expression implement(RexToLixTranslator translator,
            RexCall call, List<Expression> translatedOperands) {
          Expression expr = super.implement(translator, call,
              translatedOperands);
          final Class<?> returnType = method.getReturnType();
          if (QueryableTable.class.isAssignableFrom(returnType)) {
            Expression queryable = Expressions.call(
                Expressions.convert_(expr, QueryableTable.class),
                BuiltInMethod.QUERYABLE_TABLE_AS_QUERYABLE.method,
                Expressions.call(DataContext.ROOT,
                    BuiltInMethod.DATA_CONTEXT_GET_QUERY_PROVIDER.method),
                Expressions.constant(null, SchemaPlus.class),
                Expressions.constant(call.getOperator().getName(), String.class));
            expr = Expressions.call(queryable,
                BuiltInMethod.QUERYABLE_AS_ENUMERABLE.method);
          } else {
            expr = Expressions.call(expr,
                BuiltInMethod.SCANNABLE_TABLE_SCAN.method, DataContext.ROOT);
          }
          return expr;
        }
      }, NullPolicy.NONE, false);
}
 
Example 7
Source Project: calcite   Source File: VisitorDataContext.java    License: Apache License 2.0 6 votes vote down vote up
public static DataContext of(RelDataType rowType,
    List<Pair<RexInputRef, RexNode>> usageList) {
  final int size = rowType.getFieldList().size();
  final Object[] values = new Object[size];
  for (Pair<RexInputRef, RexNode> elem : usageList) {
    Pair<Integer, ?> value = getValue(elem.getKey(), elem.getValue());
    if (value == null) {
      LOGGER.warn("{} is not handled for {} for checking implication",
          elem.getKey(), elem.getValue());
      return null;
    }
    int index = value.getKey();
    values[index] = value.getValue();
  }
  return new VisitorDataContext(values);
}
 
Example 8
Source Project: quark   Source File: QuarkMetaImpl.java    License: Apache License 2.0 6 votes vote down vote up
protected MetaResultSet createResultSet(
    Map<String, Object> internalParameters, List<ColumnMetaData> columns,
    CursorFactory cursorFactory, final Frame firstFrame) {
  try {
    final QuarkConnectionImpl connection = getConnection();
    final AvaticaStatement statement = connection.createStatement();
    final CalcitePrepare.CalciteSignature<Object> signature =
        new CalcitePrepare.CalciteSignature<Object>("",
            ImmutableList.<AvaticaParameter>of(), internalParameters, null,
            columns, cursorFactory, null, ImmutableList.<RelCollation>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);
  }
}
 
Example 9
Source Project: kylin   Source File: OLAPContext.java    License: Apache License 2.0 6 votes vote down vote up
private void bindVariable(TupleFilter filter, DataContext dataContext) {
    if (filter == null) {
        return;
    }

    for (TupleFilter childFilter : filter.getChildren()) {
        bindVariable(childFilter, dataContext);
    }

    if (filter instanceof CompareTupleFilter && dataContext != null) {
        CompareTupleFilter compFilter = (CompareTupleFilter) filter;
        for (Map.Entry<String, Object> entry : compFilter.getVariables().entrySet()) {
            String variable = entry.getKey();
            Object value = dataContext.get(variable);
            if (value != null) {
                String str = value.toString();
                str = transferDateTimeColumnToMillis(compFilter, str);
                compFilter.clearPreviousVariableValues(variable);
                compFilter.bindVariable(variable, str);
            }

        }
    }
}
 
Example 10
Source Project: Bats   Source File: SqlFunctions.java    License: Apache License 2.0 5 votes vote down vote up
/** SQL {@code CURRENT_DATE} function. */
@NonDeterministic
public static int currentDate(DataContext root) {
  final long timestamp = currentTimestamp(root);
  int date = (int) (timestamp / DateTimeUtils.MILLIS_PER_DAY);
  final int time = (int) (timestamp % DateTimeUtils.MILLIS_PER_DAY);
  if (time < 0) {
    --date;
  }
  return date;
}
 
Example 11
Source Project: Bats   Source File: Schemas.java    License: Apache License 2.0 5 votes vote down vote up
/** Returns an {@link org.apache.calcite.util.Enumerable} over object
 * arrays, given a fully-qualified table name which leads to a
 * {@link ScannableTable}. */
public static Table table(DataContext root, String... names) {
  SchemaPlus schema = root.getRootSchema();
  final List<String> nameList = Arrays.asList(names);
  for (Iterator<? extends String> iterator = nameList.iterator();;) {
    String name = iterator.next();
    if (iterator.hasNext()) {
      schema = schema.getSubSchema(name);
    } else {
      return schema.getTable(name);
    }
  }
}
 
Example 12
Source Project: Bats   Source File: VisitorDataContext.java    License: Apache License 2.0 5 votes vote down vote up
public static DataContext of(RelDataType rowType, RexNode rex) {
  final int size = rowType.getFieldList().size();
  final Object[] values = new Object[size];
  final List<RexNode> operands = ((RexCall) rex).getOperands();
  final RexNode firstOperand = operands.get(0);
  final RexNode secondOperand = operands.get(1);
  final Pair<Integer, ?> value = getValue(firstOperand, secondOperand);
  if (value != null) {
    int index = value.getKey();
    values[index] = value.getValue();
    return new VisitorDataContext(values);
  } else {
    return null;
  }
}
 
Example 13
Source Project: kareldb   Source File: FilterableTable.java    License: Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root, List<RexNode> filters) {
    CollectionWrapper rows = (CollectionWrapper) getModifiableCollection();
    for (RexNode filter : filters) {
        rows = scanFilterForKeyFields(root, filter, rows);
    }
    final Collection coll = rows;
    final AtomicBoolean cancelFlag = DataContext.Variable.CANCEL_FLAG.get(root);
    return new AbstractEnumerable<Object[]>() {
        @SuppressWarnings("unchecked")
        public Enumerator<Object[]> enumerator() {
            return new TableEnumerator<>(Iterators.<Object, Object[]>transform(
                coll.iterator(), Table::toArray), cancelFlag, true);
        }
    };
}
 
Example 14
Source Project: calcite   Source File: RedisTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override public Enumerable<Object[]> scan(DataContext root) {
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return new RedisEnumerator(redisConfig, schema, tableName);
    }
  };
}
 
Example 15
Source Project: calcite   Source File: RexExecutorImpl.java    License: Apache License 2.0 5 votes vote down vote up
private static String compile(RexBuilder rexBuilder, List<RexNode> constExps,
    RexToLixTranslator.InputGetter getter, RelDataType rowType) {
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(rowType, rexBuilder);
  for (RexNode node : constExps) {
    programBuilder.addProject(
        node, "c" + programBuilder.getProjectList().size());
  }
  final JavaTypeFactoryImpl javaTypeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
  final BlockBuilder blockBuilder = new BlockBuilder();
  final ParameterExpression root0_ =
      Expressions.parameter(Object.class, "root0");
  final ParameterExpression root_ = DataContext.ROOT;
  blockBuilder.add(
      Expressions.declare(
          Modifier.FINAL, root_,
          Expressions.convert_(root0_, DataContext.class)));
  final SqlConformance conformance = SqlConformanceEnum.DEFAULT;
  final RexProgram program = programBuilder.getProgram();
  final List<Expression> expressions =
      RexToLixTranslator.translateProjects(program, javaTypeFactory,
          conformance, blockBuilder, null, root_, getter, null);
  blockBuilder.add(
      Expressions.return_(null,
          Expressions.newArrayInit(Object[].class, expressions)));
  final MethodDeclaration methodDecl =
      Expressions.methodDecl(Modifier.PUBLIC, Object[].class,
          BuiltInMethod.FUNCTION1_APPLY.method.getName(),
          ImmutableList.of(root0_), blockBuilder.toBlock());
  String code = Expressions.toString(methodDecl);
  if (CalciteSystemProperty.DEBUG.value()) {
    Util.debugCode(System.out, code);
  }
  return code;
}
 
Example 16
Source Project: kylin-on-parquet-v2   Source File: OLAPQuery.java    License: Apache License 2.0 5 votes vote down vote up
public OLAPQuery(DataContext optiqContext, EnumeratorTypeEnum type, int ctxId) {
    this.optiqContext = optiqContext;
    this.type = type;
    this.contextId = ctxId;

    QueryContextFacade.current().addContext(ctxId, type.toString(),
            type == EnumeratorTypeEnum.OLAP);
}
 
Example 17
Source Project: kylin-on-parquet-v2   Source File: SparkExec.java    License: Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object[]> collectToEnumerable(DataContext dataContext) {
    if (BackdoorToggles.getPrepareOnly()) {
        return Linq4j.emptyEnumerable();
    }

    OLAPRel olapRel = (OLAPRel) QueryContextFacade.current().getOlapRel();
    RelDataType rowType = (RelDataType) QueryContextFacade.current().getResultType();
    try {
        Enumerable<Object[]> computer = QueryEngineFactory.compute(dataContext, olapRel, rowType);
        return computer;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 18
Source Project: kylin-on-parquet-v2   Source File: SparkExec.java    License: Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object> collectToScalarEnumerable(DataContext dataContext) {
    if (BackdoorToggles.getPrepareOnly()) {
        return Linq4j.emptyEnumerable();
    }

    OLAPRel olapRel = (OLAPRel) QueryContextFacade.current().getOlapRel();
    RelDataType rowType = (RelDataType) QueryContextFacade.current().getResultType();
    try {
        Enumerable<Object> objects = QueryEngineFactory.computeSCALA(dataContext, olapRel, rowType);
        return objects;

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 19
Source Project: kylin-on-parquet-v2   Source File: SparkExec.java    License: Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object[]> asyncResult(DataContext dataContext) {
    if (BackdoorToggles.getPrepareOnly()) {
        return Linq4j.emptyEnumerable();
    }
    OLAPRel olapRel = (OLAPRel) QueryContextFacade.current().getOlapRel();
    RelDataType rowType = (RelDataType) QueryContextFacade.current().getResultType();
    return QueryEngineFactory.computeAsync(dataContext, olapRel, rowType);
}
 
Example 20
Source Project: calcite   Source File: GeodeSimpleScannableTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override public Enumerable<Object[]> scan(DataContext root) {
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return new GeodeSimpleEnumerator<Object[]>(clientCache, regionName) {
        @Override public Object[] convert(Object obj) {
          Object values = convertToRowValues(relDataType.getFieldList(), obj);
          if (values instanceof Object[]) {
            return (Object[]) values;
          }
          return new Object[]{values};
        }
      };
    }
  };
}
 
Example 21
Source Project: kylin-on-parquet-v2   Source File: QueryEngineFactory.java    License: Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object> computeSCALA(DataContext dataContext, RelNode relNode, RelDataType resultType)
        throws IllegalAccessException, ClassNotFoundException, InstantiationException {
    try {
        String property = System.getProperty("kylin-query-engine", "org.apache.kylin.query.runtime.SparkEngine");
        QueryEngine o = (QueryEngine) Class.forName(property).newInstance();
        return o.computeSCALA(dataContext, relNode, resultType);
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw e;
    }
}
 
Example 22
Source Project: kylin-on-parquet-v2   Source File: QueryEngineFactory.java    License: Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object[]> compute(DataContext dataContext, RelNode relNode, RelDataType resultType)
        throws IllegalAccessException, ClassNotFoundException, InstantiationException {
    try {
        String property = System.getProperty("kylin-query-engine", "org.apache.kylin.query.runtime.SparkEngine");
        QueryEngine o = (QueryEngine) Class.forName(property).newInstance();
        return o.compute(dataContext, relNode, resultType);
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw e;
    }
}
 
Example 23
Source Project: calcite   Source File: RexExecutorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testTimestampFromContext() throws Exception {
  // CURRENT_TIMESTAMP actually rounds the value to nearest second
  // and that's why we do currentTimeInMillis / 1000 * 1000
  long val = System.currentTimeMillis() / 1000 * 1000;
  testContextLiteral(SqlStdOperatorTable.CURRENT_TIMESTAMP,
      DataContext.Variable.CURRENT_TIMESTAMP, val);
}
 
Example 24
Source Project: calcite   Source File: ScannableTableTest.java    License: Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root, List<RexNode> filters,
    final int[] projects) {
  final Pair<Integer, Object> filter = getFilter(cooperative, filters);
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return beatles(buf, filter, projects);
    }
  };
}
 
Example 25
Source Project: calcite   Source File: ScannableTableTest.java    License: Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return tens();
    }
  };
}
 
Example 26
Source Project: calcite   Source File: CalciteConnectionImpl.java    License: Apache License 2.0 5 votes vote down vote up
public DataContext createDataContext(Map<String, Object> parameterValues,
    CalciteSchema rootSchema) {
  if (config().spark()) {
    return new SlimDataContext();
  }
  return new DataContextImpl(this, parameterValues, rootSchema);
}
 
Example 27
Source Project: calcite   Source File: CsvFilterableTable.java    License: Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root, List<RexNode> filters) {
  final List<CsvFieldType> fieldTypes = getFieldTypes(root.getTypeFactory());
  final String[] filterValues = new String[fieldTypes.size()];
  filters.removeIf(filter -> addFilter(filter, filterValues));
  final int[] fields = CsvEnumerator.identityList(fieldTypes.size());
  final AtomicBoolean cancelFlag = DataContext.Variable.CANCEL_FLAG.get(root);
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return new CsvEnumerator<>(source, cancelFlag, false, filterValues,
          new CsvEnumerator.ArrayRowConverter(fieldTypes, fields));
    }
  };
}
 
Example 28
Source Project: Mycat2   Source File: CalciteUtls.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static AtomicBoolean getCancelFlag(DataContext root) {
    AtomicBoolean tempFlag = DataContext.Variable.CANCEL_FLAG.get(root);
    if (tempFlag == null) {
        return new AtomicBoolean(false);
    } else {
        return tempFlag;
    }
}
 
Example 29
Source Project: calcite   Source File: JsonScannableTable.java    License: Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return new JsonEnumerator(getDataList(root.getTypeFactory()));
    }
  };
}
 
Example 30
Source Project: calcite   Source File: ReflectiveSchema.java    License: Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
  if (elementType == Object[].class) {
    //noinspection unchecked
    return enumerable;
  } else {
    //noinspection unchecked
    return enumerable.select(new FieldSelector((Class) elementType));
  }
}