public static String from(Query query) {
        if(query instanceof SqmInterpretationsKey.InterpretationsKeySource &&
           query instanceof QueryImplementor &&
           query instanceof QuerySqmImpl) {
            QueryInterpretationCache.Key cacheKey = SqmInterpretationsKey.createInterpretationsKey((SqmInterpretationsKey.InterpretationsKeySource) query);
            QuerySqmImpl querySqm = (QuerySqmImpl) query;
            Supplier buildSelectQueryPlan = () -> ReflectionUtils.invokeMethod(querySqm, "buildSelectQueryPlan");
            SelectQueryPlan plan = cacheKey != null ? ((QueryImplementor) query).getSession().getFactory().getQueryEngine()
                .getInterpretationCache()
                .resolveSelectQueryPlan(cacheKey, buildSelectQueryPlan) :
                (SelectQueryPlan) buildSelectQueryPlan.get();
            if(plan instanceof ConcreteSqmSelectQueryPlan) {
                ConcreteSqmSelectQueryPlan selectQueryPlan = (ConcreteSqmSelectQueryPlan) plan;
                Object cacheableSqmInterpretation = ReflectionUtils.getFieldValueOrNull(selectQueryPlan, "cacheableSqmInterpretation");
                if(cacheableSqmInterpretation == null) {
                    DomainQueryExecutionContext domainQueryExecutionContext = DomainQueryExecutionContext.class.cast(querySqm);
                    cacheableSqmInterpretation = ReflectionUtils.invokeStaticMethod(
                        ReflectionUtils.getMethod(
                            ConcreteSqmSelectQueryPlan.class,
                            "buildCacheableSqmInterpretation",
                            SqmSelectStatement.class,
                            DomainParameterXref.class,
                            DomainQueryExecutionContext.class
                        ),
                        ReflectionUtils.getFieldValueOrNull(selectQueryPlan, "sqm"),
                        ReflectionUtils.getFieldValueOrNull(selectQueryPlan, "domainParameterXref"),
                        domainQueryExecutionContext
                    );
                }
                if (cacheableSqmInterpretation != null) {
                    JdbcOperationQuerySelect jdbcSelect = ReflectionUtils.getFieldValueOrNull(cacheableSqmInterpretation, "jdbcSelect");
                    if (jdbcSelect != null) {
                        return jdbcSelect.getSqlString();
                    }
                }
            }
        }
        return ReflectionUtils.invokeMethod(query, "getQueryString");
    }
@Test
    public void testJPQL() {
        doInJPA(entityManager -> {
            Query jpql = entityManager
            .createQuery(
                "select " +
                "   YEAR(p.createdOn) as year, " +
                "   count(p) as postCount " +
                "from " +
                "   Post p " +
                "group by " +
                "   YEAR(p.createdOn)", Tuple.class);

            String sql = SQLExtractor.from(jpql);

            assertNotNull(sql);

            LOGGER.info(
                "The JPQL query: [\n{}\n]\ngenerates the following SQL query: [\n{}\n]",
                jpql.unwrap(org.hibernate.query.Query.class).getQueryString(),
                sql
            );
        });
    }