001package gudusoft.gsqlparser.ir.semantic;
002
003/**
004 * One endpoint of a {@link LineageEdge}. Either references an output column
005 * of a {@link StatementGraph} (by index + name) or a column of a base
006 * table (by qualified name + column name). The discriminator is
007 * {@link #getKind()}.
008 */
009public final class LineageRef {
010
011    public enum Kind {
012        STATEMENT_OUTPUT,
013        TABLE_COLUMN
014    }
015
016    private final Kind kind;
017    private final int statementIndex;
018    private final String outputName;
019    private final String qualifiedName;
020    private final String columnName;
021
022    private LineageRef(Kind kind, int statementIndex, String outputName,
023                       String qualifiedName, String columnName) {
024        this.kind = kind;
025        this.statementIndex = statementIndex;
026        this.outputName = outputName;
027        this.qualifiedName = qualifiedName;
028        this.columnName = columnName;
029    }
030
031    public static LineageRef statementOutput(int statementIndex, String outputName) {
032        if (statementIndex < 0) {
033            throw new IllegalArgumentException("statementIndex must be >= 0");
034        }
035        if (outputName == null || outputName.isEmpty()) {
036            throw new IllegalArgumentException("outputName must be non-empty");
037        }
038        return new LineageRef(Kind.STATEMENT_OUTPUT, statementIndex, outputName, null, null);
039    }
040
041    public static LineageRef tableColumn(String qualifiedName, String columnName) {
042        if (qualifiedName == null || qualifiedName.isEmpty()) {
043            throw new IllegalArgumentException("qualifiedName must be non-empty");
044        }
045        if (columnName == null || columnName.isEmpty()) {
046            throw new IllegalArgumentException("columnName must be non-empty");
047        }
048        return new LineageRef(Kind.TABLE_COLUMN, -1, null, qualifiedName, columnName);
049    }
050
051    public Kind getKind() {
052        return kind;
053    }
054
055    public int getStatementIndex() {
056        return statementIndex;
057    }
058
059    public String getOutputName() {
060        return outputName;
061    }
062
063    public String getQualifiedName() {
064        return qualifiedName;
065    }
066
067    public String getColumnName() {
068        return columnName;
069    }
070}