org.apache.calcite.schema.Function Java Examples

The following examples show how to use org.apache.calcite.schema.Function. 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: 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 #2
Source File: CatalogImpl.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private Collection<Function> getFunctionsInternal(
  NamespaceKey path
) {

  switch (getType(path, true)) {
    case SOURCE:
      FileSystemPlugin plugin = asFSn(path);
      if(plugin == null) {
        return ImmutableList.of();
      }
      return plugin.getFunctions(path.getPathComponents(), options.getSchemaConfig());

    case HOME:

      try {
        return getHomeFilesPlugin().getFunctions(path.getPathComponents(), options.getSchemaConfig());
      } catch (ExecutionSetupException e) {
        throw new RuntimeException(e);
      }
    case SPACE:
    default:
      return Collections.emptyList();
  }
}
 
Example #3
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates an operator table that contains functions in the given class.
 *
 * @see ModelHandler#addFunctions */
public static SqlOperatorTable operatorTable(String className) {
  // Dummy schema to collect the functions
  final CalciteSchema schema =
      CalciteSchema.createRootSchema(false, false);
  ModelHandler.addFunctions(schema.plus(), null, ImmutableList.of(),
      className, "*", true);

  // The following is technical debt; see [CALCITE-2082] Remove
  // RelDataTypeFactory argument from SqlUserDefinedAggFunction constructor
  final SqlTypeFactoryImpl typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);

  final ListSqlOperatorTable table = new ListSqlOperatorTable();
  for (String name : schema.getFunctionNames()) {
    for (Function function : schema.getFunctions(name, true)) {
      final SqlIdentifier id = new SqlIdentifier(name, SqlParserPos.ZERO);
      table.add(
          toOp(typeFactory, id, function));
    }
  }
  return table;
}
 
Example #4
Source File: StreamlineSqlImpl.java    From streamline with Apache License 2.0 6 votes vote down vote up
private void handleCreateFunction(SqlCreateFunction sqlCreateFunction) throws ClassNotFoundException {
  if(sqlCreateFunction.jarName() != null) {
    throw new UnsupportedOperationException("UDF 'USING JAR' not implemented");
  }
  Method method;
  Function function;
  if ((method=findMethod(sqlCreateFunction.className(), "evaluate")) != null) {
    function = ScalarFunctionImpl.create(method);
  } else if (findMethod(sqlCreateFunction.className(), "add") != null) {
    function = AggregateFunctionImpl.create(Class.forName(sqlCreateFunction.className()));
  } else {
    throw new RuntimeException("Invalid scalar or aggregate function");
  }
  schema.add(sqlCreateFunction.functionName().toUpperCase(), function);
  hasUdf = true;
}
 
Example #5
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 #6
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Executes a {@code CREATE VIEW} command. */
public void execute(SqlCreateView create,
    CalcitePrepare.Context context) {
  final Pair<CalciteSchema, String> pair =
      schema(context, true, create.name);
  final SchemaPlus schemaPlus = pair.left.plus();
  for (Function function : schemaPlus.getFunctions(pair.right)) {
    if (function.getParameters().isEmpty()) {
      if (!create.getReplace()) {
        throw SqlUtil.newContextException(create.name.getParserPosition(),
            RESOURCE.viewExists(pair.right));
      }
      pair.left.removeFunction(pair.right);
    }
  }
  final SqlNode q = renameColumns(create.columnList, create.query);
  final String sql = q.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
  final ViewTableMacro viewTableMacro =
      ViewTable.viewMacro(schemaPlus, sql, pair.left.path(null),
          context.getObjectPath(), false);
  final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
  Util.discard(x);
  schemaPlus.add(pair.right, viewTableMacro);
}
 
Example #7
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 #8
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 #9
Source File: DremioCatalogReader.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void lookupOperatorOverloads(final SqlIdentifier paramSqlIdentifier, SqlFunctionCategory paramSqlFunctionCategory, SqlSyntax paramSqlSyntax, List<SqlOperator> paramList) {
  if(paramSqlFunctionCategory != SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION) {
    return;
  }

  paramList.addAll(FluentIterable.from(catalog.getFunctions(new NamespaceKey(paramSqlIdentifier.names))).transform(new com.google.common.base.Function<org.apache.calcite.schema.Function, SqlOperator>(){

    @Override
    public SqlOperator apply(Function input) {
      return toOp(paramSqlIdentifier, input);
    }}).toList());
}
 
Example #10
Source File: JSqlSchema.java    From kafka-eagle with Apache License 2.0 5 votes vote down vote up
@Override
protected Multimap<String, Function> getFunctionMultimap() {
	ImmutableMultimap<String, ScalarFunction> funcs = ScalarFunctionImpl.createAll(JSONFunction.class);
	Multimap<String, Function> functions = HashMultimap.create();
	for (String key : funcs.keySet()) {
		for (ScalarFunction func : funcs.get(key)) {
			functions.put(key, func);
		}
	}
	return functions;
}
 
Example #11
Source File: WorkspaceSchemaFactory.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public List<Function> getFunctions(String name) {
  List<TableSignature> sigs = optionExtractor.getTableSignatures(name);
  return sigs.stream()
      .map(input -> new WithOptionsTableMacro(input, WorkspaceSchema.this))
      .collect(Collectors.toList());
}
 
Example #12
Source File: SqlUserDefinedTableMacro.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Converts arguments from {@link org.apache.calcite.sql.SqlNode} to
 * java object format.
 *
 * @param typeFactory type factory used to convert the arguments
 * @param operandList input arguments
 * @param function target function to get parameter types from
 * @param opName name of the operator to use in error message
 * @param failOnNonLiteral true when conversion should fail on non-literal
 * @return converted list of arguments
 */
public static List<Object> convertArguments(RelDataTypeFactory typeFactory,
    List<SqlNode> operandList, Function function,
    SqlIdentifier opName,
    boolean failOnNonLiteral) {
  List<Object> arguments = new ArrayList<>(operandList.size());
  // Construct a list of arguments, if they are all constants.
  for (Pair<FunctionParameter, SqlNode> pair
      : Pair.zip(function.getParameters(), operandList)) {
    try {
      final Object o = getValue(pair.right);
      final Object o2 = coerce(o, pair.left.getType(typeFactory));
      arguments.add(o2);
    } catch (NonLiteralException e) {
      if (failOnNonLiteral) {
        throw new IllegalArgumentException("All arguments of call to macro "
            + opName + " should be literal. Actual argument #"
            + pair.left.getOrdinal() + " (" + pair.left.getName()
            + ") is not literal: " + pair.right);
      }
      final RelDataType type = pair.left.getType(typeFactory);
      final Object value;
      if (type.isNullable()) {
        value = null;
      } else {
        value = 0L;
      }
      arguments.add(value);
    }
  }
  return arguments;
}
 
Example #13
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 #14
Source File: SqlUserDefinedFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a {@link SqlUserDefinedFunction}. */
public SqlUserDefinedFunction(SqlIdentifier opName,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker,
    List<RelDataType> paramTypes,
    Function function) {
  this(opName, returnTypeInference, operandTypeInference, operandTypeChecker,
      paramTypes, function, SqlFunctionCategory.USER_DEFINED_FUNCTION);
}
 
Example #15
Source File: SqlUserDefinedFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Constructor used internally and by derived classes. */
protected SqlUserDefinedFunction(SqlIdentifier opName,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker,
    List<RelDataType> paramTypes,
    Function function,
    SqlFunctionCategory category) {
  super(Util.last(opName.names), opName, SqlKind.OTHER_FUNCTION,
      returnTypeInference, operandTypeInference, operandTypeChecker,
      paramTypes, category);
  this.function = function;
}
 
Example #16
Source File: SqlUserDefinedTableMacro.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Converts arguments from {@link org.apache.calcite.sql.SqlNode} to
 * java object format.
 *
 * @param typeFactory type factory used to convert the arguments
 * @param operandList input arguments
 * @param function target function to get parameter types from
 * @param opName name of the operator to use in error message
 * @param failOnNonLiteral true when conversion should fail on non-literal
 * @return converted list of arguments
 */
public static List<Object> convertArguments(RelDataTypeFactory typeFactory,
    List<SqlNode> operandList, Function function,
    SqlIdentifier opName,
    boolean failOnNonLiteral) {
  List<Object> arguments = new ArrayList<>(operandList.size());
  // Construct a list of arguments, if they are all constants.
  for (Pair<FunctionParameter, SqlNode> pair
      : Pair.zip(function.getParameters(), operandList)) {
    try {
      final Object o = getValue(pair.right);
      final Object o2 = coerce(o, pair.left.getType(typeFactory));
      arguments.add(o2);
    } catch (NonLiteralException e) {
      if (failOnNonLiteral) {
        throw new IllegalArgumentException("All arguments of call to macro "
            + opName + " should be literal. Actual argument #"
            + pair.left.getOrdinal() + " (" + pair.left.getName()
            + ") is not literal: " + pair.right);
      }
      final RelDataType type = pair.left.getType(typeFactory);
      final Object value;
      if (type.isNullable()) {
        value = null;
      } else {
        value = 0L;
      }
      arguments.add(value);
    }
  }
  return arguments;
}
 
Example #17
Source File: CatalogImpl.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void createDataset(
    NamespaceKey key,
    com.google.common.base.Function<DatasetConfig, DatasetConfig> datasetMutator
) {
  final ManagedStoragePlugin plugin = pluginRetriever.getPlugin(key.getRoot(), true);
  if (plugin == null) {
    throw UserException.validationError()
        .message("Unknown source %s", key.getRoot())
        .buildSilently();
  }

  datasets.createDataset(key, plugin, datasetMutator);
}
 
Example #18
Source File: SqlUserDefinedFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Constructor used internally and by derived classes. */
protected SqlUserDefinedFunction(SqlIdentifier opName,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker,
    List<RelDataType> paramTypes,
    Function function,
    SqlFunctionCategory category) {
  super(Util.last(opName.names), opName, SqlKind.OTHER_FUNCTION,
      returnTypeInference, operandTypeInference, operandTypeChecker,
      paramTypes, category);
  this.function = function;
}
 
Example #19
Source File: SqlUserDefinedFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a {@link SqlUserDefinedFunction}. */
public SqlUserDefinedFunction(SqlIdentifier opName,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker,
    List<RelDataType> paramTypes,
    Function function) {
  this(opName, returnTypeInference, operandTypeInference, operandTypeChecker,
      paramTypes, function, SqlFunctionCategory.USER_DEFINED_FUNCTION);
}
 
Example #20
Source File: DremioCatalogReader.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private List<RelDataType> toSql(List<RelDataType> types) {
  return Lists.transform(types,
      new com.google.common.base.Function<RelDataType, RelDataType>() {
        @Override
        public RelDataType apply(RelDataType type) {
          return toSql(type);
        }
      });
}
 
Example #21
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 #22
Source File: FormatPluginOptionExtractor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public List<Function> getFunctions(final List<String> tableSchemaPath, final FileSystemPlugin plugin, final SchemaConfig schemaConfig) {
  List<TableSignature> sigs = getTableSignatures(tableSchemaPath.get(tableSchemaPath.size() - 1));
  return FluentIterable.from(sigs).transform(new com.google.common.base.Function<TableSignature, Function>() {
    @Override
    public Function apply(TableSignature input) {
      return new WithOptionsTableMacro(tableSchemaPath, input, plugin, schemaConfig);
    }
  }).toList();
}
 
Example #23
Source File: CalciteSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns a collection of all functions, explicit and implicit, with a given
 * name. Never null. */
public final Collection<Function> getFunctions(String name, boolean caseSensitive) {
  final ImmutableList.Builder<Function> builder = ImmutableList.builder();
  // Add explicit functions.
  for (FunctionEntry functionEntry
      : Pair.right(functionMap.range(name, caseSensitive))) {
    builder.add(functionEntry.getFunction());
  }
  // Add implicit functions.
  addImplicitFunctionsToBuilder(builder, name, caseSensitive);
  return builder.build();
}
 
Example #24
Source File: CalciteSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
private FunctionEntry add(String name, Function function) {
  final FunctionEntryImpl entry =
      new FunctionEntryImpl(this, name, function);
  functionMap.put(name, entry);
  functionNames.add(name);
  if (function.getParameters().isEmpty()) {
    nullaryFunctionMap.put(name, entry);
  }
  return entry;
}
 
Example #25
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 #26
Source File: SimpleCalciteSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected void addImplicitFunctionsToBuilder(
    ImmutableList.Builder<Function> builder,
    String name, boolean caseSensitive) {
  Collection<Function> functions = schema.getFunctions(name);
  if (functions != null) {
    builder.addAll(functions);
  }
}
 
Example #27
Source File: PigUserDefinedFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
private PigUserDefinedFunction(SqlIdentifier opName,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker,
    List<RelDataType> paramTypes,
    Function function,
    FuncSpec funcSpec) {
  super(opName, returnTypeInference, operandTypeInference, operandTypeChecker, paramTypes,
      function,
      SqlFunctionCategory.USER_DEFINED_CONSTRUCTOR);
  this.funcSpec = funcSpec;
}
 
Example #28
Source File: PigUserDefinedFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
public PigUserDefinedFunction(String name,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeChecker operandTypeChecker,
    List<RelDataType> paramTypes,
    Function function,
    FuncSpec funcSpec) {
  this(new SqlIdentifier(ImmutableList.of(name), SqlParserPos.ZERO),
      returnTypeInference,
      null,
      operandTypeChecker,
      paramTypes,
      function,
      funcSpec);
}
 
Example #29
Source File: PigUserDefinedFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
public PigUserDefinedFunction(String name,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeChecker operandTypeChecker,
    List<RelDataType> paramTypes,
    Function function) {
  this(name, returnTypeInference, operandTypeChecker, paramTypes, function, null);
}
 
Example #30
Source File: CachingCalciteSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected void addImplicitFunctionsToBuilder(
    ImmutableList.Builder<Function> builder,
    String name, boolean caseSensitive) {
  // Add implicit functions, case-insensitive.
  final long now = System.currentTimeMillis();
  final NameSet set = implicitFunctionCache.get(now);
  for (String name2 : set.range(name, caseSensitive)) {
    final Collection<Function> functions = schema.getFunctions(name2);
    if (functions != null) {
      builder.addAll(functions);
    }
  }
}