org.apache.calcite.schema.TableMacro Java Examples

The following examples show how to use org.apache.calcite.schema.TableMacro. 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 File: CachingCalciteSchema.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected void addImplicitTablesBasedOnNullaryFunctionsToBuilder(
    ImmutableSortedMap.Builder<String, Table> builder) {
  ImmutableSortedMap<String, Table> explicitTables = builder.build();

  final long now = System.currentTimeMillis();
  final NameSet set = implicitFunctionCache.get(now);
  for (String s : set.iterable()) {
    // explicit table wins.
    if (explicitTables.containsKey(s)) {
      continue;
    }
    for (Function function : schema.getFunctions(s)) {
      if (function instanceof TableMacro
          && function.getParameters().isEmpty()) {
        final Table table = ((TableMacro) function).apply(ImmutableList.of());
        builder.put(s, table);
      }
    }
  }
}
 
Example #2
Source File: CachingCalciteSchema.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected TableEntry getImplicitTableBasedOnNullaryFunction(String tableName,
    boolean caseSensitive) {
  final long now = System.currentTimeMillis();
  final NameSet set = implicitFunctionCache.get(now);
  for (String s : set.range(tableName, caseSensitive)) {
    for (Function function : schema.getFunctions(s)) {
      if (function instanceof TableMacro
          && function.getParameters().isEmpty()) {
        final Table table =
            ((TableMacro) function).apply(ImmutableList.of());
        return tableEntry(tableName, table);
      }
    }
  }
  return null;
}
 
Example #3
Source File: SimpleCalciteSchema.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected void addImplicitTablesBasedOnNullaryFunctionsToBuilder(
    ImmutableSortedMap.Builder<String, Table> builder) {
  ImmutableSortedMap<String, Table> explicitTables = builder.build();

  for (String s : schema.getFunctionNames()) {
    // explicit table wins.
    if (explicitTables.containsKey(s)) {
      continue;
    }
    for (Function function : schema.getFunctions(s)) {
      if (function instanceof TableMacro
          && function.getParameters().isEmpty()) {
        final Table table = ((TableMacro) function).apply(ImmutableList.of());
        builder.put(s, table);
      }
    }
  }
}
 
Example #4
Source File: ReflectiveSchema.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Multimap<String, Function> createFunctionMap() {
  final ImmutableMultimap.Builder<String, Function> builder =
      ImmutableMultimap.builder();
  for (Method method : clazz.getMethods()) {
    final String methodName = method.getName();
    if (method.getDeclaringClass() == Object.class
        || methodName.equals("toString")) {
      continue;
    }
    if (TranslatableTable.class.isAssignableFrom(method.getReturnType())) {
      final TableMacro tableMacro =
          new MethodTableMacro(this, method);
      builder.put(methodName, tableMacro);
    }
  }
  return builder.build();
}
 
Example #5
Source File: SqlUserDefinedTableMacro.java    From Bats with Apache License 2.0 5 votes vote down vote up
public SqlUserDefinedTableMacro(SqlIdentifier opName,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker, List<RelDataType> paramTypes,
    TableMacro tableMacro) {
  super(Util.last(opName.names), opName, SqlKind.OTHER_FUNCTION,
      returnTypeInference, operandTypeInference, operandTypeChecker,
      Objects.requireNonNull(paramTypes),
      SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION);
  this.tableMacro = tableMacro;
}
 
Example #6
Source File: TableMacroImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a {@code TableMacro} from a class, looking for an "eval"
 * method. Returns null if there is no such method. */
public static TableMacro create(Class<?> clazz) {
  final Method method = findMethod(clazz, "eval");
  if (method == null) {
    return null;
  }
  return create(method);
}
 
Example #7
Source File: TableMacroImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a {@code TableMacro} from a method. */
public static TableMacro create(final Method method) {
  Class<?> clazz = method.getDeclaringClass();
  if (!Modifier.isStatic(method.getModifiers())) {
    if (!classHasPublicZeroArgsConstructor(clazz)) {
      throw RESOURCE.requireDefaultConstructor(clazz.getName()).ex();
    }
  }
  final Class<?> returnType = method.getReturnType();
  if (!TranslatableTable.class.isAssignableFrom(returnType)) {
    return null;
  }
  return new TableMacroImpl(method);
}
 
Example #8
Source File: DremioCatalogReader.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Rest of class is utility functions taken directly from CalciteCatalogReader. This is because that class consider these utilities to be private concerns.
 */
private SqlOperator toOp(SqlIdentifier name, final Function function) {
  List<RelDataType> argTypes = new ArrayList<>();
  List<SqlTypeFamily> typeFamilies = new ArrayList<>();
  for (FunctionParameter o : function.getParameters()) {
    final RelDataType type = o.getType(typeFactory);
    argTypes.add(type);
    typeFamilies.add(
        Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
  }
  final Predicate<Integer> optional =
      new Predicate<Integer>() {
        @Override
        public boolean apply(Integer input) {
          return function.getParameters().get(input).isOptional();
        }
      };
  final FamilyOperandTypeChecker typeChecker =
      OperandTypes.family(typeFamilies, optional);
  final List<RelDataType> paramTypes = toSql(argTypes);
  if (function instanceof ScalarFunction) {
    return new SqlUserDefinedFunction(name, infer((ScalarFunction) function),
        InferTypes.explicit(argTypes), typeChecker, paramTypes, function);
  } else if (function instanceof AggregateFunction) {
    return new SqlUserDefinedAggFunction(name,
        infer((AggregateFunction) function), InferTypes.explicit(argTypes),
        typeChecker, (AggregateFunction) function, false, false, typeFactory);
  } else if (function instanceof TableMacro) {
    return new SqlUserDefinedTableMacro(name, ReturnTypes.CURSOR,
        InferTypes.explicit(argTypes), typeChecker, paramTypes,
        (TableMacro) function);
  } else if (function instanceof TableFunction) {
    return new SqlUserDefinedTableFunction(name, ReturnTypes.CURSOR,
        InferTypes.explicit(argTypes), typeChecker, paramTypes,
        (TableFunction) function);
  } else {
    throw new AssertionError("unknown function type " + function);
  }
}
 
Example #9
Source File: SimpleCalciteSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected TableEntry getImplicitTableBasedOnNullaryFunction(String tableName,
    boolean caseSensitive) {
  Collection<Function> functions = schema.getFunctions(tableName);
  if (functions != null) {
    for (Function function : functions) {
      if (function instanceof TableMacro
          && function.getParameters().isEmpty()) {
        final Table table = ((TableMacro) function).apply(ImmutableList.of());
        return tableEntry(tableName, table);
      }
    }
  }
  return null;
}
 
Example #10
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void lookupOperatorOverloads(final SqlIdentifier opName,
    SqlFunctionCategory category,
    SqlSyntax syntax,
    List<SqlOperator> operatorList,
    SqlNameMatcher nameMatcher) {
  if (syntax != SqlSyntax.FUNCTION) {
    return;
  }

  final Predicate<Function> predicate;
  if (category == null) {
    predicate = function -> true;
  } else if (category.isTableFunction()) {
    predicate = function ->
        function instanceof TableMacro
            || function instanceof TableFunction;
  } else {
    predicate = function ->
        !(function instanceof TableMacro
            || function instanceof TableFunction);
  }
  getFunctionsFrom(opName.names)
      .stream()
      .filter(predicate)
      .map(function -> toOp(opName, function))
      .forEachOrdered(operatorList::add);
}
 
Example #11
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Converts a function to a {@link org.apache.calcite.sql.SqlOperator}.
 *
 * <p>The {@code typeFactory} argument is technical debt; see [CALCITE-2082]
 * Remove RelDataTypeFactory argument from SqlUserDefinedAggFunction
 * constructor. */
private static SqlOperator toOp(RelDataTypeFactory typeFactory,
    SqlIdentifier name, final Function function) {
  List<RelDataType> argTypes = new ArrayList<>();
  List<SqlTypeFamily> typeFamilies = new ArrayList<>();
  for (FunctionParameter o : function.getParameters()) {
    final RelDataType type = o.getType(typeFactory);
    argTypes.add(type);
    typeFamilies.add(
        Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
  }
  final FamilyOperandTypeChecker typeChecker =
      OperandTypes.family(typeFamilies, i ->
          function.getParameters().get(i).isOptional());
  final List<RelDataType> paramTypes = toSql(typeFactory, argTypes);
  if (function instanceof ScalarFunction) {
    return new SqlUserDefinedFunction(name, infer((ScalarFunction) function),
        InferTypes.explicit(argTypes), typeChecker, paramTypes, function);
  } else if (function instanceof AggregateFunction) {
    return new SqlUserDefinedAggFunction(name,
        infer((AggregateFunction) function), InferTypes.explicit(argTypes),
        typeChecker, (AggregateFunction) function, false, false,
        Optionality.FORBIDDEN, typeFactory);
  } else if (function instanceof TableMacro) {
    return new SqlUserDefinedTableMacro(name, ReturnTypes.CURSOR,
        InferTypes.explicit(argTypes), typeChecker, paramTypes,
        (TableMacro) function);
  } else if (function instanceof TableFunction) {
    return new SqlUserDefinedTableFunction(name, ReturnTypes.CURSOR,
        InferTypes.explicit(argTypes), typeChecker, paramTypes,
        (TableFunction) function);
  } else {
    throw new AssertionError("unknown function type " + function);
  }
}
 
Example #12
Source File: SqlUserDefinedTableMacro.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SqlUserDefinedTableMacro(SqlIdentifier opName,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker, List<RelDataType> paramTypes,
    TableMacro tableMacro) {
  super(Util.last(opName.names), opName, SqlKind.OTHER_FUNCTION,
      returnTypeInference, operandTypeInference, operandTypeChecker,
      Objects.requireNonNull(paramTypes),
      SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION);
  this.tableMacro = tableMacro;
}
 
Example #13
Source File: TableMacroImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a {@code TableMacro} from a class, looking for an "eval"
 * method. Returns null if there is no such method. */
public static TableMacro create(Class<?> clazz) {
  final Method method = findMethod(clazz, "eval");
  if (method == null) {
    return null;
  }
  return create(method);
}
 
Example #14
Source File: TableMacroImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a {@code TableMacro} from a method. */
public static TableMacro create(final Method method) {
  Class clazz = method.getDeclaringClass();
  if (!Modifier.isStatic(method.getModifiers())) {
    if (!classHasPublicZeroArgsConstructor(clazz)) {
      throw RESOURCE.requireDefaultConstructor(clazz.getName()).ex();
    }
  }
  final Class<?> returnType = method.getReturnType();
  if (!TranslatableTable.class.isAssignableFrom(returnType)) {
    return null;
  }
  return new TableMacroImpl(method);
}
 
Example #15
Source File: MockCatalogReaderDynamic.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public MockCatalogReader init() {
  // Register "DYNAMIC" schema.
  MockSchema schema = new MockSchema("SALES");
  registerSchema(schema);

  MockDynamicTable nationTable =
      new MockDynamicTable(schema.getCatalogName(),
          schema.getName(), "NATION");
  registerTable(nationTable);

  Supplier<MockDynamicTable> customerTableSupplier = () ->
      new MockDynamicTable(schema.getCatalogName(), schema.getName(), "CUSTOMER");

  MockDynamicTable customerTable = customerTableSupplier.get();
  registerTable(customerTable);

  // CREATE TABLE "REGION" - static table with known schema.
  final RelDataType intType =
      typeFactory.createSqlType(SqlTypeName.INTEGER);
  final RelDataType varcharType =
      typeFactory.createSqlType(SqlTypeName.VARCHAR);

  MockTable regionTable =
      MockTable.create(this, schema, "REGION", false, 100);
  regionTable.addColumn("R_REGIONKEY", intType);
  regionTable.addColumn("R_NAME", varcharType);
  regionTable.addColumn("R_COMMENT", varcharType);
  registerTable(regionTable);

  List<String> custModifiableViewNames = Arrays.asList(
      schema.getCatalogName(), schema.getName(), "CUSTOMER_MODIFIABLEVIEW");
  TableMacro custModifiableViewMacro = MockModifiableViewRelOptTable.viewMacro(rootSchema,
      "select n_name from SALES.CUSTOMER", custModifiableViewNames.subList(0, 2),
      Collections.singletonList(custModifiableViewNames.get(2)), true);
  TranslatableTable empModifiableView = custModifiableViewMacro.apply(Collections.emptyList());
  MockTable mockCustViewTable = MockRelViewTable.create(
      (ViewTable) empModifiableView, this,
      custModifiableViewNames.get(0), custModifiableViewNames.get(1),
      custModifiableViewNames.get(2), false, 20, null);
  registerTable(mockCustViewTable);

  // re-registers customer table to clear its row type after view registration
  reregisterTable(customerTableSupplier.get());

  return this;
}