You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Statement.getFeatures() throws NullPointerException on plain SELECT DISTINCT #2573
Statement.getFeatures() throws a NullPointerException for a plain SELECT DISTINCT, so a read-only guard that calls it has to fail closed on every DISTINCT query.
Version:com.manticore-projects.jsqlformatter:jsqlparser:5.4.351-SNAPSHOT (build 5.4.351-20260910.100601-1), master at b2115ac8.
Reproduce
CCJSqlParserUtil.parse("SELECT DISTINCT id FROM foo").getFeatures();
java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because the return value of "net.sf.jsqlparser.statement.select.Distinct.getOnSelectItems()" is null
at net.sf.jsqlparser.statement.select.SelectVisitorAdapter.visit(SelectVisitorAdapter.java:149)
at net.sf.jsqlparser.statement.StatementFeatureVisitor$FeatureSelectVisitor.visit(StatementFeatureVisitor.java:801)
at net.sf.jsqlparser.statement.StatementFeatureVisitor$FeatureSelectVisitor.visit(StatementFeatureVisitor.java:769)
SELECT DISTINCT ON (id) id FROM foo and SELECT ALL id FROM foo classify fine; only a Distinct without an ON list is affected.
Cause
SelectVisitorAdapter.visit(PlainSelect) (SelectVisitorAdapter.java:149) iterates plainSelect.getDistinct().getOnSelectItems() without a null check; Distinct.getOnSelectItems() is null unless DISTINCT ON (..) was written. SelectDeParser (:531) guards the same list with != null.
Suggestion
Guard the loop in SelectVisitorAdapter (if (onSelectItems != null)), and add SELECT DISTINCT a FROM t to StatementFeatureVisitorTest / AdapterRegressionTest. Setting an empty list from the outside is not a workaround: the deparser then prints DISTINCT ON ().
Activity