use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.Pair in project beam by apache.
the class SqlCreateFunction method execute.
@Override
public void execute(CalcitePrepare.Context context) {
final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, functionName);
SchemaPlus schema = pair.left.plus();
String lastName = pair.right;
if (!schema.getFunctions(lastName).isEmpty()) {
throw SqlUtil.newContextException(functionName.getParserPosition(), RESOURCE.internal(String.format("Function %s is already defined.", lastName)));
}
JavaUdfLoader udfLoader = new JavaUdfLoader();
// TODO(BEAM-12355) Support qualified function names.
List<String> functionPath = ImmutableList.of(lastName);
if (!(jarPath instanceof SqlCharStringLiteral)) {
throw SqlUtil.newContextException(jarPath.getParserPosition(), RESOURCE.internal("Jar path is not instanceof SqlCharStringLiteral."));
}
String unquotedJarPath = ((SqlCharStringLiteral) jarPath).getNlsString().getValue();
if (isAggregate) {
// Try loading the aggregate function just to make sure it exists. LazyAggregateCombineFn will
// need to fetch it again at runtime.
udfLoader.loadAggregateFunction(functionPath, unquotedJarPath);
LazyAggregateCombineFn<?, ?, ?> combineFn = new LazyAggregateCombineFn<>(functionPath, unquotedJarPath);
schema.add(lastName, combineFn.getUdafImpl());
} else {
ScalarFn scalarFn = udfLoader.loadScalarFunction(functionPath, unquotedJarPath);
Method method = ScalarFnReflector.getApplyMethod(scalarFn);
Function function = ScalarFunctionImpl.create(method, unquotedJarPath);
schema.add(lastName, function);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.Pair in project beam by apache.
the class SqlSetOptionBeam method execute.
@Override
public void execute(CalcitePrepare.Context context) {
final SqlIdentifier name = getName();
final SqlNode value = getValue();
final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
if (!(pair.left.schema instanceof BeamCalciteSchema)) {
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.internal("Schema is not instanceof BeamCalciteSchema"));
}
BeamCalciteSchema schema = (BeamCalciteSchema) pair.left.schema;
if (value != null) {
schema.setPipelineOption(pair.right, SqlDdlNodes.getString(value));
} else if ("ALL".equals(pair.right)) {
schema.removeAllPipelineOptions();
} else {
schema.removePipelineOption(pair.right);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.Pair in project beam by apache.
the class SqlCreateExternalTable method execute.
@Override
public void execute(CalcitePrepare.Context context) {
final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
if (pair.left.plus().getTable(pair.right) != null) {
// Table exists.
if (!ifNotExists) {
// They did not specify IF NOT EXISTS, so give error.
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.tableExists(pair.right));
}
return;
}
// Table does not exist. Create it.
if (!(pair.left.schema instanceof BeamCalciteSchema)) {
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.internal("Schema is not instanceof BeamCalciteSchema"));
}
BeamCalciteSchema schema = (BeamCalciteSchema) pair.left.schema;
schema.getTableProvider().createTable(toTable());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.Pair in project beam by apache.
the class SqlDdlNodes method schema.
/**
* Returns the schema in which to create an object.
*/
static Pair<CalciteSchema, String> schema(CalcitePrepare.Context context, boolean mutable, SqlIdentifier id) {
final List<String> path;
if (id.isSimple()) {
path = context.getDefaultSchemaPath();
} else {
path = Util.skipLast(id.names);
}
CalciteSchema schema = mutable ? context.getMutableRootSchema() : context.getRootSchema();
for (String p : path) {
schema = schema.getSubSchema(p, true);
if (schema == null) {
throw new AssertionError(String.format("Got null sub-schema for path '%s' in %s", p, path));
}
}
return Pair.of(schema, name(id));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.Pair in project beam by apache.
the class BigQueryFilter method isSupported.
/**
* Check whether a {@code RexNode} is supported. As of right now BigQuery supports: 1. Complex
* predicates (both conjunction and disjunction). 2. Comparison between a column and a literal.
*
* <p>TODO: Check if comparison between two columns is supported. Also over a boolean field.
*
* @param node A node to check for predicate push-down support.
* @return A pair containing a boolean whether an expression is supported and the number of input
* references used by the expression.
*/
private Pair<Boolean, Integer> isSupported(RexNode node) {
int numberOfInputRefs = 0;
boolean isSupported = true;
if (node instanceof RexCall) {
RexCall compositeNode = (RexCall) node;
// CAST, TRIM? and REVERSE? should be supported as well.
if (!node.getKind().belongsTo(SUPPORTED_OPS)) {
isSupported = false;
} else {
for (RexNode operand : compositeNode.getOperands()) {
// All operands must be supported for a parent node to be supported.
Pair<Boolean, Integer> childSupported = isSupported(operand);
// (OR).
if (!node.getKind().belongsTo(ImmutableSet.of(AND, OR))) {
numberOfInputRefs += childSupported.getRight();
}
// Predicate functions, where more than one field is involved are unsupported.
isSupported = numberOfInputRefs < 2 && childSupported.getLeft();
}
}
} else if (node instanceof RexInputRef) {
numberOfInputRefs = 1;
} else if (node instanceof RexLiteral) {
// RexLiterals are expected, but no action is needed.
} else {
throw new UnsupportedOperationException("Encountered an unexpected node type: " + node.getClass().getSimpleName());
}
return Pair.of(isSupported, numberOfInputRefs);
}
Aggregations