use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlCharStringLiteral in project drill by apache.
the class DescribeTableHandler method rewrite.
/**
* Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.COLUMNS ...
*/
@Override
public SqlNode rewrite(SqlNode sqlNode) throws ForemanSetupException {
DrillSqlDescribeTable node = unwrap(sqlNode, DrillSqlDescribeTable.class);
try {
List<SqlNode> selectList = Arrays.asList(new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_DATA_TYPE, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_IS_NULLABLE, SqlParserPos.ZERO));
SqlNode fromClause = new SqlIdentifier(Arrays.asList(IS_SCHEMA_NAME, InfoSchemaTableType.COLUMNS.name()), SqlParserPos.ZERO);
SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
List<String> schemaPathGivenInCmd = Util.skipLast(node.getTable().names);
SchemaPlus schema = SchemaUtilites.findSchema(defaultSchema, schemaPathGivenInCmd);
if (schema == null) {
SchemaUtilites.throwSchemaNotFoundException(defaultSchema, SchemaUtilites.getSchemaPath(schemaPathGivenInCmd));
}
if (SchemaUtilites.isRootSchema(schema)) {
throw UserException.validationError().message("No schema selected.").build(logger);
}
// find resolved schema path
AbstractSchema drillSchema = SchemaUtilites.unwrapAsDrillSchemaInstance(schema);
String schemaPath = drillSchema.getFullSchemaName();
String tableName = Util.last(node.getTable().names);
if (schema.getTable(tableName) == null) {
throw UserException.validationError().message("Unknown table [%s] in schema [%s]", tableName, schemaPath).build(logger);
}
SqlNode schemaCondition = null;
if (!SchemaUtilites.isRootSchema(schema)) {
schemaCondition = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(schemaPath, Util.getDefaultCharset().name(), SqlParserPos.ZERO));
}
SqlNode tableNameColumn = new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO);
// if table names are case insensitive, wrap column values and condition in lower function
if (!drillSchema.areTableNamesCaseSensitive()) {
tableNameColumn = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, tableNameColumn);
tableName = tableName.toLowerCase();
}
SqlNode where = DrillParserUtil.createCondition(tableNameColumn, SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(tableName, Util.getDefaultCharset().name(), SqlParserPos.ZERO));
where = DrillParserUtil.createCondition(schemaCondition, SqlStdOperatorTable.AND, where);
SqlNode columnFilter = null;
if (node.getColumn() != null) {
columnFilter = DrillParserUtil.createCondition(SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO)), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(node.getColumn().toString().toLowerCase(), Util.getDefaultCharset().name(), SqlParserPos.ZERO));
} else if (node.getColumnQualifier() != null) {
SqlNode columnQualifier = node.getColumnQualifier();
SqlNode column = new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO);
if (columnQualifier instanceof SqlCharStringLiteral) {
NlsString conditionString = ((SqlCharStringLiteral) columnQualifier).getNlsString();
columnQualifier = SqlCharStringLiteral.createCharString(conditionString.getValue().toLowerCase(), conditionString.getCharsetName(), columnQualifier.getParserPosition());
column = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, column);
}
columnFilter = DrillParserUtil.createCondition(column, SqlStdOperatorTable.LIKE, columnQualifier);
}
where = DrillParserUtil.createCondition(where, SqlStdOperatorTable.AND, columnFilter);
return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO), fromClause, where, null, null, null, null, null, null);
} catch (Exception ex) {
throw UserException.planError(ex).message("Error while rewriting DESCRIBE query: %d", ex.getMessage()).build(logger);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlCharStringLiteral in project flink by apache.
the class SqlAddHivePartitions method unparse.
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("ALTER TABLE");
tableIdentifier.unparse(writer, leftPrec, rightPrec);
writer.newlineAndIndent();
writer.keyword("ADD");
if (ifNotExists()) {
writer.keyword("IF NOT EXISTS");
}
int opLeftPrec = getOperator().getLeftPrec();
int opRightPrec = getOperator().getRightPrec();
for (int i = 0; i < getPartSpecs().size(); i++) {
writer.newlineAndIndent();
SqlNodeList partSpec = getPartSpecs().get(i);
writer.keyword("PARTITION");
partSpec.unparse(writer, opLeftPrec, opRightPrec);
SqlCharStringLiteral location = partLocations.get(i);
if (location != null) {
writer.keyword("LOCATION");
location.unparse(writer, opLeftPrec, opRightPrec);
}
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlCharStringLiteral 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.sql.SqlCharStringLiteral in project drill by axbaretto.
the class DropFunctionHandler method getPlan.
/**
* Unregisters UDFs dynamically. Process consists of several steps:
* <ol>
* <li>Registering jar in jar registry to ensure that several jars with the same name is not being unregistered.</li>
* <li>Starts remote unregistration process, gets list of all jars and excludes jar to be deleted.</li>
* <li>Signals drill bits to start local unregistration process.</li>
* <li>Removes source and binary jars from registry area.</li>
* </ol>
*
* UDFs unregistration is allowed only if dynamic UDFs support is enabled.
* Only jars registered dynamically can be unregistered,
* built-in functions loaded at start up are not allowed to be unregistered.
*
* Limitation: before jar unregistration make sure no one is using functions from this jar.
* There is no guarantee that running queries will finish successfully or give correct result.
*
* @return - Single row indicating list of unregistered UDFs, raise exception otherwise
*/
@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ForemanSetupException, IOException {
if (!context.getOption(ExecConstants.DYNAMIC_UDF_SUPPORT_ENABLED).bool_val) {
throw UserException.validationError().message("Dynamic UDFs support is disabled.").build(logger);
}
SqlDropFunction node = unwrap(sqlNode, SqlDropFunction.class);
String jarName = ((SqlCharStringLiteral) node.getJar()).toValue();
RemoteFunctionRegistry remoteFunctionRegistry = context.getRemoteFunctionRegistry();
boolean inProgress = false;
try {
final String action = remoteFunctionRegistry.addToJars(jarName, RemoteFunctionRegistry.Action.UNREGISTRATION);
if (!(inProgress = action == null)) {
return DirectPlan.createDirectPlan(context, false, String.format("Jar with %s name is used. Action: %s", jarName, action));
}
Jar deletedJar = unregister(jarName, remoteFunctionRegistry);
if (deletedJar == null) {
return DirectPlan.createDirectPlan(context, false, String.format("Jar %s is not registered in remote registry", jarName));
}
remoteFunctionRegistry.submitForUnregistration(jarName);
removeJarFromArea(jarName, remoteFunctionRegistry.getFs(), remoteFunctionRegistry.getRegistryArea());
removeJarFromArea(JarUtil.getSourceName(jarName), remoteFunctionRegistry.getFs(), remoteFunctionRegistry.getRegistryArea());
return DirectPlan.createDirectPlan(context, true, String.format("The following UDFs in jar %s have been unregistered:\n%s", jarName, deletedJar.getFunctionSignatureList()));
} catch (Exception e) {
logger.error("Error during UDF unregistration", e);
return DirectPlan.createDirectPlan(context, false, e.getMessage());
} finally {
if (inProgress) {
remoteFunctionRegistry.finishUnregistration(jarName);
remoteFunctionRegistry.removeFromJars(jarName);
}
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlCharStringLiteral in project streamline by hortonworks.
the class ExpressionGenerator method visitSqlSpecialOperator.
private Expression visitSqlSpecialOperator(SqlSpecialOperator specialOperator, List<SqlNode> operands) {
if (specialOperator.getName().equalsIgnoreCase("ITEM")) {
Expression left = operands.get(0).accept(this);
SqlNode right = operands.get(1);
if (right instanceof SqlNumericLiteral) {
SqlNumericLiteral index = (SqlNumericLiteral) right;
if (!index.isInteger()) {
throw new IllegalArgumentException("Invalid array index " + index);
}
return new ArrayFieldExpression(left, Integer.parseInt(index.toValue()));
} else if (right instanceof SqlCharStringLiteral) {
String key = ((SqlCharStringLiteral) right).toValue();
return new MapFieldExpression(left, key);
} else {
throw new IllegalArgumentException("Item right operand '" + right + "' must be numeric or character type");
}
} else if (specialOperator.getName().equalsIgnoreCase("AS")) {
Expression left = operands.get(0).accept(this);
String alias = operands.get(1).toString();
return new AsExpression(left, alias);
} else {
throw new UnsupportedOperationException("Operator " + specialOperator + " not implemented");
}
}
Aggregations