<commit_before>package no.ssb.vtl.script.visitors.join;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import me.yanaga.guava.stream.MoreCollectors;
import no.ssb.vtl.model.Component;
import no.ssb.vtl.model.Dataset;
import no.ssb.vtl.parser.VTLBaseVisitor;
import no.ssb.vtl.parser.VTLParser;
import no.ssb.vtl.script.operations.join.AbstractJoinOperation;
import no.ssb.vtl.script.operations.join.CrossJoinOperation;
import no.ssb.vtl.script.operations.join.InnerJoinOperation;
import no.ssb.vtl.script.operations.join.OuterJoinOperation;
import no.ssb.vtl.script.visitors.ReferenceVisitor;

import javax.script.Bindings;
import javax.script.ScriptContext;
import java.util.List;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Visitor that handle the join definition
 */
public class JoinDefinitionVisitor extends VTLBaseVisitor<AbstractJoinOperation> {

    private final ReferenceVisitor referenceVisitor;

    public JoinDefinitionVisitor(ScriptContext context) {
        checkNotNull(context);
        Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
        referenceVisitor = new ReferenceVisitor(bindings);
    }

    private Dataset getDataset(VTLParser.DatasetRefContext ref) {
        Object referencedObject = referenceVisitor.visit(ref);
        return (Dataset) referencedObject; //TODO: Is this always safe? Hadrien: Yes, DatasetRefContext and ComponentRefContext will return the correct type
    }

    private ImmutableMap<String, Dataset> extractDatasets(List<VTLParser.DatasetRefContext> ctx) {
        return ctx.stream()
                    .collect(MoreCollectors.toImmutableMap(
                            // TODO: Need to support alias here. The spec forgot it.
                            datasetRefContext -> datasetRefContext.variableRef().identifier().getText(),
                            datasetRefContext -> (Dataset) referenceVisitor.visit(datasetRefContext)
                    ));
    }

    private ImmutableSet<Component> extractIdentifierComponents(List<VTLParser.ComponentRefContext> ctx) {
        return ctx.stream()
                    .map(componentRefContext -> (Component) referenceVisitor.visit(componentRefContext))
                    .collect(MoreCollectors.toImmutableSet());
    }

    @Override
    public AbstractJoinOperation visitJoinDefinition(VTLParser.JoinDefinitionContext ctx) {

        Integer joinType = Optional.ofNullable(ctx.type.getType()).orElse(VTLParser.INNER);
        ImmutableMap<String, Dataset> datasets = extractDatasets(ctx.datasetRef());
        ImmutableSet<Component> identifiers = extractIdentifierComponents(ctx.componentRef());

        switch (ctx.type.getType()) {
            case VTLParser.INNER:
                return new InnerJoinOperation(datasets, identifiers);
            case VTLParser.OUTER:
                return new OuterJoinOperation(datasets, identifiers);
            case VTLParser.CROSS:
                return new CrossJoinOperation(datasets, identifiers);

        }
        return super.visitJoinDefinition(ctx);
    }
}
<commit_msg>Fix default join type bug<commit_after>package no.ssb.vtl.script.visitors.join;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import me.yanaga.guava.stream.MoreCollectors;
import no.ssb.vtl.model.Component;
import no.ssb.vtl.model.Dataset;
import no.ssb.vtl.parser.VTLBaseVisitor;
import no.ssb.vtl.parser.VTLParser;
import no.ssb.vtl.script.operations.join.AbstractJoinOperation;
import no.ssb.vtl.script.operations.join.CrossJoinOperation;
import no.ssb.vtl.script.operations.join.InnerJoinOperation;
import no.ssb.vtl.script.operations.join.OuterJoinOperation;
import no.ssb.vtl.script.visitors.ReferenceVisitor;
import org.antlr.v4.runtime.Token;

import javax.script.Bindings;
import javax.script.ScriptContext;
import java.util.List;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Visitor that handle the join definition
 */
public class JoinDefinitionVisitor extends VTLBaseVisitor<AbstractJoinOperation> {

    private final ReferenceVisitor referenceVisitor;

    public JoinDefinitionVisitor(ScriptContext context) {
        checkNotNull(context);
        Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
        referenceVisitor = new ReferenceVisitor(bindings);
    }

    private Dataset getDataset(VTLParser.DatasetRefContext ref) {
        Object referencedObject = referenceVisitor.visit(ref);
        return (Dataset) referencedObject; //TODO: Is this always safe? Hadrien: Yes, DatasetRefContext and ComponentRefContext will return the correct type
    }

    private ImmutableMap<String, Dataset> extractDatasets(List<VTLParser.DatasetRefContext> ctx) {
        return ctx.stream()
                    .collect(MoreCollectors.toImmutableMap(
                            // TODO: Need to support alias here. The spec forgot it.
                            datasetRefContext -> datasetRefContext.variableRef().identifier().getText(),
                            datasetRefContext -> (Dataset) referenceVisitor.visit(datasetRefContext)
                    ));
    }

    private ImmutableSet<Component> extractIdentifierComponents(List<VTLParser.ComponentRefContext> ctx) {
        return ctx.stream()
                    .map(componentRefContext -> (Component) referenceVisitor.visit(componentRefContext))
                    .collect(MoreCollectors.toImmutableSet());
    }

    @Override
    public AbstractJoinOperation visitJoinDefinition(VTLParser.JoinDefinitionContext ctx) {

        ImmutableMap<String, Dataset> datasets = extractDatasets(ctx.datasetRef());
        ImmutableSet<Component> identifiers = extractIdentifierComponents(ctx.componentRef());

        Integer joinType = Optional.ofNullable(ctx.type).map(Token::getType).orElse(VTLParser.INNER);
        switch (joinType) {
            case VTLParser.INNER:
                return new InnerJoinOperation(datasets, identifiers);
            case VTLParser.OUTER:
                return new OuterJoinOperation(datasets, identifiers);
            case VTLParser.CROSS:
                return new CrossJoinOperation(datasets, identifiers);

        }
        return super.visitJoinDefinition(ctx);
    }
}

{'fraction_non_alphanumeric': 0.08956862745098039, 'fraction_numerical': 0.00015686274509803922, 'mean_word_length': 3.4124567474048444, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5901208', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1680, 'n_words': 320}