/* Parses and executes the AGGREGATE procedure. */ int cmd_aggregate (struct lexer *lexer, struct dataset *ds) { struct dictionary *dict = dataset_dict (ds); struct agr_proc agr; struct file_handle *out_file = NULL; struct casereader *input = NULL, *group; struct casegrouper *grouper; struct casewriter *output = NULL; bool copy_documents = false; bool presorted = false; bool saw_direction; bool ok; memset(&agr, 0 , sizeof (agr)); agr.missing = ITEMWISE; agr.src_dict = dict; subcase_init_empty (&agr.sort); /* OUTFILE subcommand must be first. */ lex_match (lexer, T_SLASH); if (!lex_force_match_id (lexer, "OUTFILE")) goto error; lex_match (lexer, T_EQUALS); if (!lex_match (lexer, T_ASTERISK)) { out_file = fh_parse (lexer, FH_REF_FILE, dataset_session (ds)); if (out_file == NULL) goto error; } if (out_file == NULL && lex_match_id (lexer, "MODE")) { lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "ADDVARIABLES")) { agr.add_variables = true; /* presorted is assumed in ADDVARIABLES mode */ presorted = true; } else if (lex_match_id (lexer, "REPLACE")) { agr.add_variables = false; } else goto error; } if ( agr.add_variables ) agr.dict = dict_clone (dict); else agr.dict = dict_create (dict_get_encoding (dict)); dict_set_label (agr.dict, dict_get_label (dict)); dict_set_documents (agr.dict, dict_get_documents (dict)); /* Read most of the subcommands. */ for (;;) { lex_match (lexer, T_SLASH); if (lex_match_id (lexer, "MISSING")) { lex_match (lexer, T_EQUALS); if (!lex_match_id (lexer, "COLUMNWISE")) { lex_error_expecting (lexer, "COLUMNWISE", NULL); goto error; } agr.missing = COLUMNWISE; } else if (lex_match_id (lexer, "DOCUMENT")) copy_documents = true; else if (lex_match_id (lexer, "PRESORTED")) presorted = true; else if (lex_force_match_id (lexer, "BREAK")) { int i; lex_match (lexer, T_EQUALS); if (!parse_sort_criteria (lexer, dict, &agr.sort, &agr.break_vars, &saw_direction)) goto error; agr.break_var_cnt = subcase_get_n_fields (&agr.sort); if (! agr.add_variables) for (i = 0; i < agr.break_var_cnt; i++) dict_clone_var_assert (agr.dict, agr.break_vars[i]); /* BREAK must follow the options. */ break; } else goto error; } if (presorted && saw_direction) msg (SW, _("When PRESORTED is specified, specifying sorting directions " "with (A) or (D) has no effect. Output data will be sorted " "the same way as the input data.")); /* Read in the aggregate functions. */ lex_match (lexer, T_SLASH); if (!parse_aggregate_functions (lexer, dict, &agr)) goto error; /* Delete documents. */ if (!copy_documents) dict_clear_documents (agr.dict); /* Cancel SPLIT FILE. */ dict_set_split_vars (agr.dict, NULL, 0); /* Initialize. */ agr.case_cnt = 0; if (out_file == NULL) { /* The active dataset will be replaced by the aggregated data, so TEMPORARY is moot. */ proc_cancel_temporary_transformations (ds); proc_discard_output (ds); output = autopaging_writer_create (dict_get_proto (agr.dict)); } else { output = any_writer_open (out_file, agr.dict); if (output == NULL) goto error; } input = proc_open (ds); if (!subcase_is_empty (&agr.sort) && !presorted) { input = sort_execute (input, &agr.sort); subcase_clear (&agr.sort); } for (grouper = casegrouper_create_vars (input, agr.break_vars, agr.break_var_cnt); casegrouper_get_next_group (grouper, &group); casereader_destroy (group)) { struct casereader *placeholder = NULL; struct ccase *c = casereader_peek (group, 0); if (c == NULL) { casereader_destroy (group); continue; } initialize_aggregate_info (&agr); if ( agr.add_variables ) placeholder = casereader_clone (group); { struct ccase *cg; for (; (cg = casereader_read (group)) != NULL; case_unref (cg)) accumulate_aggregate_info (&agr, cg); } if (agr.add_variables) { struct ccase *cg; for (; (cg = casereader_read (placeholder)) != NULL; case_unref (cg)) dump_aggregate_info (&agr, output, cg); casereader_destroy (placeholder); } else { dump_aggregate_info (&agr, output, c); } case_unref (c); } if (!casegrouper_destroy (grouper)) goto error; if (!proc_commit (ds)) { input = NULL; goto error; } input = NULL; if (out_file == NULL) { struct casereader *next_input = casewriter_make_reader (output); if (next_input == NULL) goto error; dataset_set_dict (ds, agr.dict); dataset_set_source (ds, next_input); agr.dict = NULL; } else { ok = casewriter_destroy (output); output = NULL; if (!ok) goto error; } agr_destroy (&agr); fh_unref (out_file); return CMD_SUCCESS; error: if (input != NULL) proc_commit (ds); casewriter_destroy (output); agr_destroy (&agr); fh_unref (out_file); return CMD_CASCADING_FAILURE; }
/* Parses SAVE or XSAVE or EXPORT or XEXPORT command. WRITER_TYPE identifies the type of file to write, and COMMAND_TYPE identifies the type of command. On success, returns a writer. For procedures only, sets *RETAIN_UNSELECTED to true if cases that would otherwise be excluded by FILTER or USE should be included. On failure, returns a null pointer. */ static struct casewriter * parse_write_command (struct lexer *lexer, struct dataset *ds, enum writer_type writer_type, enum command_type command_type, bool *retain_unselected) { /* Common data. */ struct file_handle *handle; /* Output file. */ struct dictionary *dict; /* Dictionary for output file. */ struct casewriter *writer; /* Writer. */ struct case_map *map; /* Map from input data to data for writer. */ /* Common options. */ bool print_map; /* Print map? TODO. */ bool print_short_names; /* Print long-to-short name map. TODO. */ struct sfm_write_options sysfile_opts; struct pfm_write_options porfile_opts; assert (writer_type == SYSFILE_WRITER || writer_type == PORFILE_WRITER); assert (command_type == XFORM_CMD || command_type == PROC_CMD); assert ((retain_unselected != NULL) == (command_type == PROC_CMD)); if (command_type == PROC_CMD) *retain_unselected = true; handle = NULL; dict = dict_clone (dataset_dict (ds)); writer = NULL; map = NULL; print_map = false; print_short_names = false; sysfile_opts = sfm_writer_default_options (); porfile_opts = pfm_writer_default_options (); case_map_prepare_dict (dict); dict_delete_scratch_vars (dict); lex_match (lexer, T_SLASH); for (;;) { if (lex_match_id (lexer, "OUTFILE")) { if (handle != NULL) { lex_sbc_only_once ("OUTFILE"); goto error; } lex_match (lexer, T_EQUALS); handle = fh_parse (lexer, FH_REF_FILE, NULL); if (handle == NULL) goto error; } else if (lex_match_id (lexer, "NAMES")) print_short_names = true; else if (lex_match_id (lexer, "PERMISSIONS")) { bool cw; lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "READONLY")) cw = false; else if (lex_match_id (lexer, "WRITEABLE")) cw = true; else { lex_error_expecting (lexer, "READONLY", "WRITEABLE", NULL_SENTINEL); goto error; } sysfile_opts.create_writeable = porfile_opts.create_writeable = cw; } else if (command_type == PROC_CMD && lex_match_id (lexer, "UNSELECTED")) { lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "RETAIN")) *retain_unselected = true; else if (lex_match_id (lexer, "DELETE")) *retain_unselected = false; else { lex_error_expecting (lexer, "RETAIN", "DELETE", NULL_SENTINEL); goto error; } } else if (writer_type == SYSFILE_WRITER && lex_match_id (lexer, "COMPRESSED")) sysfile_opts.compress = true; else if (writer_type == SYSFILE_WRITER && lex_match_id (lexer, "UNCOMPRESSED")) sysfile_opts.compress = false; else if (writer_type == SYSFILE_WRITER && lex_match_id (lexer, "VERSION")) { lex_match (lexer, T_EQUALS); if (!lex_force_int (lexer)) goto error; sysfile_opts.version = lex_integer (lexer); lex_get (lexer); } else if (writer_type == PORFILE_WRITER && lex_match_id (lexer, "TYPE")) { lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "COMMUNICATIONS")) porfile_opts.type = PFM_COMM; else if (lex_match_id (lexer, "TAPE")) porfile_opts.type = PFM_TAPE; else { lex_error_expecting (lexer, "COMM", "TAPE", NULL_SENTINEL); goto error; } } else if (writer_type == PORFILE_WRITER && lex_match_id (lexer, "DIGITS")) { lex_match (lexer, T_EQUALS); if (!lex_force_int (lexer)) goto error; porfile_opts.digits = lex_integer (lexer); lex_get (lexer); } else if (!parse_dict_trim (lexer, dict)) goto error; if (!lex_match (lexer, T_SLASH)) break; } if (lex_end_of_command (lexer) != CMD_SUCCESS) goto error; if (handle == NULL) { lex_sbc_missing ("OUTFILE"); goto error; } dict_delete_scratch_vars (dict); dict_compact_values (dict); if (fh_get_referent (handle) == FH_REF_FILE) { switch (writer_type) { case SYSFILE_WRITER: writer = sfm_open_writer (handle, dict, sysfile_opts); break; case PORFILE_WRITER: writer = pfm_open_writer (handle, dict, porfile_opts); break; } } else writer = any_writer_open (handle, dict); if (writer == NULL) goto error; map = case_map_from_dict (dict); if (map != NULL) writer = case_map_create_output_translator (map, writer); dict_destroy (dict); fh_unref (handle); return writer; error: fh_unref (handle); casewriter_destroy (writer); dict_destroy (dict); case_map_destroy (map); return NULL; }