Search in sources :

Example 1 with SqlCharStringLiteral

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);
    }
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) NlsString(org.apache.calcite.util.NlsString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) UserException(org.apache.drill.common.exceptions.UserException) ValidationException(org.apache.calcite.tools.ValidationException) ForemanSetupException(org.apache.drill.exec.work.foreman.ForemanSetupException) RelConversionException(org.apache.calcite.tools.RelConversionException) SqlSelect(org.apache.calcite.sql.SqlSelect) DrillSqlDescribeTable(org.apache.drill.exec.planner.sql.parser.DrillSqlDescribeTable) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) NlsString(org.apache.calcite.util.NlsString) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with SqlCharStringLiteral

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);
        }
    }
}
Also used : SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral)

Example 3 with SqlCharStringLiteral

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);
    }
}
Also used : Function(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function) ScalarFn(org.apache.beam.sdk.extensions.sql.udf.ScalarFn) CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) LazyAggregateCombineFn(org.apache.beam.sdk.extensions.sql.impl.LazyAggregateCombineFn) JavaUdfLoader(org.apache.beam.sdk.extensions.sql.impl.JavaUdfLoader) SqlCharStringLiteral(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlCharStringLiteral) Method(java.lang.reflect.Method)

Example 4 with SqlCharStringLiteral

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);
        }
    }
}
Also used : RemoteFunctionRegistry(org.apache.drill.exec.expr.fn.registry.RemoteFunctionRegistry) Jar(org.apache.drill.exec.proto.UserBitShared.Jar) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral) UserException(org.apache.drill.common.exceptions.UserException) IOException(java.io.IOException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) ForemanSetupException(org.apache.drill.exec.work.foreman.ForemanSetupException) VersionMismatchException(org.apache.drill.exec.exception.VersionMismatchException) SqlDropFunction(org.apache.drill.exec.planner.sql.parser.SqlDropFunction)

Example 5 with SqlCharStringLiteral

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");
    }
}
Also used : ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) AggregateFunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) AsExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Expression(com.hortonworks.streamline.streams.layout.component.rule.expression.Expression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral) SqlNumericLiteral(org.apache.calcite.sql.SqlNumericLiteral) AsExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlCharStringLiteral (org.apache.calcite.sql.SqlCharStringLiteral)10 SqlNode (org.apache.calcite.sql.SqlNode)5 SqlNodeList (org.apache.calcite.sql.SqlNodeList)5 NlsString (org.apache.calcite.util.NlsString)4 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)3 SqlSelect (org.apache.calcite.sql.SqlSelect)3 UserException (org.apache.drill.common.exceptions.UserException)3 ForemanSetupException (org.apache.drill.exec.work.foreman.ForemanSetupException)3 IOException (java.io.IOException)2 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)2 VersionMismatchException (org.apache.drill.exec.exception.VersionMismatchException)2 RemoteFunctionRegistry (org.apache.drill.exec.expr.fn.registry.RemoteFunctionRegistry)2 SqlDropFunction (org.apache.drill.exec.planner.sql.parser.SqlDropFunction)2 Jar (org.apache.drill.exec.proto.UserBitShared.Jar)2 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)2 AggregateFunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression)1 ArrayFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression)1 AsExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression)1 BinaryExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression)1