@Override protected Void visitValues(Values node, Integer indent) { builder.append(" VALUES "); boolean first = true; for (Expression row : node.getRows()) { builder.append("\n") .append(indentString(indent)) .append(first ? " " : ", "); builder.append(formatExpression(row, parameters, indent)); first = false; } builder.append('\n'); return null; }
/** * Parses the list with values to insert and returns them as Objects */ @Override public List<Object> visitValues(Values values, QueryState state){ List<Object> result = new ArrayList<Object>(); for(Expression rowExpression : values.getRows()){ if(rowExpression instanceof Row) { Row row = (Row)rowExpression; for(Expression rowValue : row.getItems()){ if(!(rowValue instanceof Literal)) { state.addException("Unable to parse non-literal value : "+rowValue); return result; } result.add(getObject((Literal)rowValue)); } }else if (rowExpression instanceof Literal){ result.add(getObject((Literal)rowExpression)); }else { state.addException("Unknown VALUES type "+rowExpression.getClass()+" encountered"); return null; } } return result; }
@Override protected Void visitValues(Values node, Integer indent) { builder.append(" VALUES "); boolean first = true; for (Expression row : node.getRows()) { builder.append("\n") .append(indentString(indent)) .append(first ? " " : ", "); builder.append(formatExpression(row)); first = false; } builder.append('\n'); return null; }
/** * Parses the Insert statement and returns the values as one list (even if multiple value sets were found). Columns * to be inserted are added to the provided heading * @param insert * @param updateState * @param heading * @return * @throws SQLException */ public List<Object> parse(Insert insert, QueryState state) throws SQLException{ if(!insert.getColumns().isPresent()) throw new SQLException("Unable to insert data without column references"); if(insert.getQuery().getQueryBody() == null) throw new SQLException("Unable to insert data without any values"); if(!(insert.getQuery().getQueryBody() instanceof Values)) throw new SQLException("Unable to insert data from a query, use ... VALUES (...)"); List<String> fields = insert.getColumns().get(); List<Object> values = insert.getQuery().getQueryBody().accept(this, state); if(state.hasException()) throw state.getException(); for(String field : fields) state.getHeading().add(new Column(field)); return values; }
/** * Parses and executes the provided insert statement and returns 1 if execution was successful * @param sql * @param insert * @param index * @return the number of executed inserts * @throws SQLException */ public int execute(String sql, Insert insert, String index) throws SQLException{ if(insert.getQuery().getQueryBody() instanceof Values){ // parse one or multiple value sets (... VALUES (1,2,'a'), (2,4,'b'), ...) return this.insertFromValues(sql, insert, index, Utils.getIntProp(props, Utils.PROP_FETCH_SIZE, 2500)); }else if(insert.getQuery().getQueryBody() instanceof QuerySpecification){ // insert data based on a SELECT statement return this.insertFromSelect(sql, insert, index, Utils.getIntProp(props, Utils.PROP_FETCH_SIZE, 2500)); }else throw new SQLException("Unknown set of values to insert ("+insert.getQuery().getQueryBody()+")"); }
@Override protected RelationType visitShowCatalogs(ShowCatalogs node, AnalysisContext context) { List<Expression> rows = metadata.getCatalogNames().keySet().stream() .map(name -> row(new StringLiteral(name))) .collect(toList()); Query query = simpleQuery( selectList(new AllColumns()), aliased(new Values(rows), "catalogs", ImmutableList.of("Catalog"))); return process(query, context); }
@Override protected RelationType visitShowFunctions(ShowFunctions node, AnalysisContext context) { ImmutableList.Builder<Expression> rows = ImmutableList.builder(); for (SqlFunction function : metadata.listFunctions()) { if (function.getSignature().getKind() == APPROXIMATE_AGGREGATE) { continue; } rows.add(row( new StringLiteral(function.getSignature().getName()), new StringLiteral(function.getSignature().getReturnType().toString()), new StringLiteral(Joiner.on(", ").join(function.getSignature().getArgumentTypes())), new StringLiteral(getFunctionType(function)), function.isDeterministic() ? TRUE_LITERAL : FALSE_LITERAL, new StringLiteral(nullToEmpty(function.getDescription())))); } Map<String, String> columns = ImmutableMap.<String, String>builder() .put("function_name", "Function") .put("return_type", "Return Type") .put("argument_types", "Argument Types") .put("function_type", "Function Type") .put("deterministic", "Deterministic") .put("description", "Description") .build(); Query query = simpleQuery( selectAll(columns.entrySet().stream() .map(entry -> aliasedName(entry.getKey(), entry.getValue())) .collect(toImmutableList())), aliased(new Values(rows.build()), "functions", ImmutableList.copyOf(columns.keySet())), ordering( ascending("function_name"), ascending("return_type"), ascending("argument_types"), ascending("function_type"))); return process(query, context); }
@Override protected RelationType visitShowSession(ShowSession node, AnalysisContext context) { ImmutableList.Builder<Expression> rows = ImmutableList.builder(); List<SessionPropertyValue> sessionProperties = metadata.getSessionPropertyManager().getAllSessionProperties(session); for (SessionPropertyValue sessionProperty : sessionProperties) { if (sessionProperty.isHidden()) { continue; } String value = sessionProperty.getValue(); String defaultValue = sessionProperty.getDefaultValue(); rows.add(row( new StringLiteral(sessionProperty.getFullyQualifiedName()), new StringLiteral(nullToEmpty(value)), new StringLiteral(nullToEmpty(defaultValue)), new StringLiteral(sessionProperty.getType()), new StringLiteral(sessionProperty.getDescription()), TRUE_LITERAL)); } // add bogus row so we can support empty sessions StringLiteral empty = new StringLiteral(""); rows.add(row(empty, empty, empty, empty, empty, FALSE_LITERAL)); Query query = simpleQuery( selectList( aliasedName("name", "Name"), aliasedName("value", "Value"), aliasedName("default", "Default"), aliasedName("type", "Type"), aliasedName("description", "Description")), aliased( new Values(rows.build()), "session", ImmutableList.of("name", "value", "default", "type", "description", "include")), nameReference("include")); return process(query, context); }
@Override public Node visitInlineTable(SqlBaseParser.InlineTableContext context) { return new Values(getLocation(context), visit(context.expression(), Expression.class)); }
public static Values values(Row... row) { return new Values(ImmutableList.copyOf(row)); }