Skip to content

Commit

Permalink
toString for windowing elements corrected
Browse files Browse the repository at this point in the history
lax equality test implemented
included oracle test sqls
included @ and # for identifiers
  • Loading branch information
wumpz committed Mar 5, 2014
1 parent 2f5d134 commit 884dcaf
Show file tree
Hide file tree
Showing 281 changed files with 2,775 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void setRange(WindowRange range) {
@Override
public String toString() {
StringBuilder buffer = new StringBuilder(type.toString());

if (offset != null) {
buffer.append(offset.toString());
} else if (range != null) {
Expand All @@ -69,5 +69,4 @@ public String toString() {

return buffer.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void setType(Type type) {
public String toString() {
StringBuilder buffer = new StringBuilder();
if (expression != null) {
buffer.append(expression);
buffer.append(' ').append(expression);
if (type != null) {
buffer.append(' ');
buffer.append(type);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jsqlparser/expression/WindowRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public void setStart(WindowOffset start) {
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append(" BETWEEN ");
buffer.append(" BETWEEN");
buffer.append(start);
buffer.append(" AND ");
buffer.append(" AND");
buffer.append(end);
return buffer.toString();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ TOKEN:
{
< S_IDENTIFIER: ( <LETTER> | <ADDITIONAL_LETTERS> )+ ( <DIGIT> | <LETTER> | <ADDITIONAL_LETTERS> | <SPECIAL_CHARS>)* >
| < #LETTER: ["a"-"z", "A"-"Z", "_"] >
| < #SPECIAL_CHARS: "$" | "_">
| < #SPECIAL_CHARS: "$" | "_" | "#" | "@">
| < S_CHAR_LITERAL: "'" (~["'"])* "'" ("'" (~["'"])* "'")*>
| < S_QUOTED_IDENTIFIER: "\"" (~["\n","\r","\""])* "\"" | ("`" (~["\n","\r","`"])* "`") | ("[" (~["\n","\r","]"])* "]") >

Expand Down
80 changes: 57 additions & 23 deletions src/test/java/net/sf/jsqlparser/test/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,67 @@
import java.io.*;

import static junit.framework.Assert.assertEquals;
import org.junit.Test;

/**
*
* @author toben
*/
public class TestUtils {
public static void assertSqlCanBeParsedAndDeparsed(String statement) throws JSQLParserException {
Statement parsed = CCJSqlParserUtil.parse(new StringReader(statement));
assertStatementCanBeDeparsedAs(parsed, statement);
}

public static void assertStatementCanBeDeparsedAs(Statement parsed, String statement) {
assertEquals(statement, parsed.toString());

StatementDeParser deParser = new StatementDeParser(new StringBuilder());
parsed.accept(deParser);
assertEquals(statement, deParser.getBuffer().toString());
}

public static void assertExpressionCanBeDeparsedAs(final Expression parsed, String expression) {
ExpressionDeParser expressionDeParser = new ExpressionDeParser();
StringBuilder stringBuilder = new StringBuilder();
expressionDeParser.setBuffer(stringBuilder);
SelectDeParser selectDeParser = new SelectDeParser(expressionDeParser, stringBuilder);
expressionDeParser.setSelectVisitor(selectDeParser);
parsed.accept(expressionDeParser);

assertEquals(expression, stringBuilder.toString());
}

public static void assertSqlCanBeParsedAndDeparsed(String statement) throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(statement, false);
}

/**
* Tries to parse and deparse the given statement.
*
* @param statement
* @param laxDeparsingCheck removes all linefeeds from the original and
* removes all double spaces. The check is caseinsensitive.
* @throws JSQLParserException
*/
public static void assertSqlCanBeParsedAndDeparsed(String statement, boolean laxDeparsingCheck) throws JSQLParserException {
Statement parsed = CCJSqlParserUtil.parse(new StringReader(statement));
assertStatementCanBeDeparsedAs(parsed, statement, laxDeparsingCheck);
}

public static void assertStatementCanBeDeparsedAs(Statement parsed, String statement) {
assertStatementCanBeDeparsedAs(parsed, statement, false);
}

public static void assertStatementCanBeDeparsedAs(Statement parsed, String statement, boolean laxDeparsingCheck) {
assertEquals(buildSqlString(statement, laxDeparsingCheck),
buildSqlString(parsed.toString(), laxDeparsingCheck));

StatementDeParser deParser = new StatementDeParser(new StringBuilder());
parsed.accept(deParser);
assertEquals(buildSqlString(statement, laxDeparsingCheck),
buildSqlString(deParser.getBuffer().toString(), laxDeparsingCheck));
}

public static String buildSqlString(String sql, boolean laxDeparsingCheck) {
if (laxDeparsingCheck) {
return sql.replaceAll("\\s", " ").replaceAll("\\s+", " ").toLowerCase().trim();
} else {
return sql;
}
}

@Test
public void testBuildSqlString() {
assertEquals("select * from test", buildSqlString(" SELECT * FROM \r\n \t TEST \n", true));
assertEquals("select * from test", buildSqlString("select * from test", false));
}

public static void assertExpressionCanBeDeparsedAs(final Expression parsed, String expression) {
ExpressionDeParser expressionDeParser = new ExpressionDeParser();
StringBuilder stringBuilder = new StringBuilder();
expressionDeParser.setBuffer(stringBuilder);
SelectDeParser selectDeParser = new SelectDeParser(expressionDeParser, stringBuilder);
expressionDeParser.setSelectVisitor(selectDeParser);
parsed.accept(expressionDeParser);

assertEquals(expression, stringBuilder.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2014 JSQLParser.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package net.sf.jsqlparser.test.select;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jsqlparser.JSQLParserException;
import static net.sf.jsqlparser.test.TestUtils.*;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

/**
* Tries to parse and deparse all statments in net.sf.jsqlparser.test.oracle-tests.
* @author toben
*/
public class SpecialOracleTests {

private static final File SQLS_DIR = new File("target/test-classes/net/sf/jsqlparser/test/oracle-tests");
private static final Logger LOG = Logger.getLogger(SpecialOracleTests.class.getName());

@Test
public void testAllSqls() throws IOException {
File[] sqlTestFiles = SQLS_DIR.listFiles();

for (File file : sqlTestFiles) {
LOG.log(Level.INFO, "testing {0}", file.getName());
String sql = FileUtils.readFileToString(file);
try {
assertSqlCanBeParsedAndDeparsed(sql, true);

LOG.info(" -> SUCCESS");
} catch (JSQLParserException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
with
codes2codelocales as
(
select t6.cdl_name as cod_name, t7.cdl_name as cod_cod_name, t14.cod_oid
from servicedesk.itsm_codes t14
left outer join servicedesk.itsm_codes_locale t6 on (t6.cdl_cod_oid=t14.cod_oid)
left outer join servicedesk.itsm_codes_locale t7 on (t7.cdl_cod_oid=t14.cod_cod_oid)
)
, incident as
(
select t1.*,
c2cl1.cod_name as "closure code", c2cl1.cod_cod_name as "closure code parent",
c2cl2.cod_name as "reason caused code", c2cl2.cod_cod_name as "reason caused code parent",
t11.cdl_name "severity", t13.cdl_name "business impact", t16.cdl_name "priority",
t2.rct_name "status", t12.rct_name "category", t99.rct_name "folder"
from servicedesk.itsm_incidents t1
join servicedesk.itsm_codes_locale t11 on (t1.inc_sev_oid=t11.cdl_cod_oid)
join servicedesk.itsm_codes_locale t13 on (t1.inc_imp_oid=t13.cdl_cod_oid)
join servicedesk.itsm_codes_locale t16 on (t1.inc_pri_oid=t16.cdl_cod_oid)
join servicedeskrepo.rep_codes_text t2 on (t1.inc_sta_oid=t2.rct_rcd_oid)
join servicedeskrepo.rep_codes_text t12 on (t1.inc_cat_oid=t12.rct_rcd_oid)
join servicedeskrepo.rep_codes_text t99 on (t1.inc_poo_oid=t99.rct_rcd_oid)
left outer join codes2codelocales c2cl1 on (t1.inc_clo_oid=c2cl1.cod_oid)
left outer join codes2codelocales c2cl2 on (t1.inc_cla_oid=c2cl2.cod_oid)
where t1."reg_created" between sysdate-1 and sysdate
)
, workgrouphistory as
(
select i.inc_id
, max(t101.hin_subject) keep (dense_rank first order by (t101.reg_created)) as "first"
, max(t101.hin_subject) keep (dense_rank last order by (t101.reg_created)) as "last"
from
servicedesk.itsm_historylines_incident t101
join incident i on (t101.hin_inc_oid = i.inc_oid)
-- from servicedesk.itsm_incidents i (t101.hin_inc_oid = i.inc_oid)
where t101.hin_subject like 'to workgroup from%'
-- and i."reg_created" between sysdate-1 and sysdate
group by i.inc_id
)
select
incident.inc_id "id"
,incident."status"
,incident.inc_description "description"
,t4.wog_searchcode "workgroup"
,t5.per_searchcode "person"
,incident.inc_solution "solution"
,incident."closure code"
,incident."closure code parent"
,incident."reason caused code"
,incident."reason caused code parent"
,t10.cit_searchcode "ci"
,incident."severity"
,incident."category"
,incident."business impact"
,incident."priority"
,to_char(incident."reg_created", 'dd-mm-yy hh24:mi:ss') "registered"
,to_char(incident."inc_deadline", 'dd-mm-yy hh24:mi:ss') "deadline"
,to_char(incident."inc_actualfinish", 'dd-mm-yy hh24:mi:ss') "finish"
,t3.icf_incshorttext3 "message group"
,t3.icf_incshorttext4 "application"
,t3.icf_incshorttext2 "msg id"
,incident."folder"
,workgrouphistory."first" "first wg"
,workgrouphistory."last" "last wg"
,t102.hin_subject "frirst pri"
from incident
join servicedesk.itsm_inc_custom_fields t3 on (incident.inc_oid=t3.icf_inc_oid)
join servicedesk.itsm_workgroups t4 on (incident.inc_assign_workgroup=t4.wog_oid)
join workgrouphistory on (incident.inc_id = workgrouphistory.inc_id)
left outer join servicedesk.itsm_persons t5 on (incident.inc_assign_person_to=t5.per_oid)
left outer join servicedesk.itsm_configuration_items t10 on (incident.inc_cit_oid=t10.cit_oid)
left outer join servicedesk.itsm_historylines_incident t102 on (incident.inc_oid = t102.hin_inc_oid and t102.hin_subject like 'priority set to%')
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
select time_id, product
, last_value(quantity ignore nulls) over (partition by product order by time_id) quantity
, last_value(quantity respect nulls) over (partition by product order by time_id) quantity
from ( select times.time_id, product, quantity
from inventory partition by (product)
right outer join times on (times.time_id = inventory.time_id)
where times.time_id between to_date('01/04/01', 'dd/mm/yy')
and to_date('06/04/01', 'dd/mm/yy'))
order by 2,1


Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
select times.time_id, product, quantity from inventory
partition by (product)
right outer join times on (times.time_id = inventory.time_id)
where times.time_id between to_date('01/04/01', 'dd/mm/yy')
and to_date('06/04/01', 'dd/mm/yy')
order by 2,1


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
select deptno
, ename
, hiredate
, listagg(ename, ',') within group (order by hiredate) over (partition by deptno) as employees
from emp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
select metric_id ,bsln_guid ,timegroup ,obs_value as obs_value
, cume_dist () over (partition by metric_id, bsln_guid, timegroup order by obs_value ) as cume_dist
, count(1) over (partition by metric_id, bsln_guid, timegroup ) as n
, row_number () over (partition by metric_id, bsln_guid, timegroup order by obs_value) as rrank
, percentile_disc(:b7 ) within group (order by obs_value asc) over (partition by metric_id, bsln_guid, timegroup) as mid_tail_value
, max(obs_value) over (partition by metric_id, bsln_guid, timegroup ) as max_val
, min(obs_value) over (partition by metric_id, bsln_guid, timegroup ) as min_val
, avg(obs_value) over (partition by metric_id, bsln_guid, timegroup ) as avg_val
, stddev(obs_value) over (partition by metric_id, bsln_guid, timegroup ) as sdev_val
, percentile_cont(0.25) within group (order by obs_value asc) over (partition by metric_id, bsln_guid, timegroup) as pctile_25
, percentile_cont(0.5) within group (order by obs_value asc) over (partition by metric_id, bsln_guid, timegroup) as pctile_50
, percentile_cont(0.75) within group (order by obs_value asc) over (partition by metric_id, bsln_guid, timegroup) as pctile_75
, percentile_cont(0.90) within group (order by obs_value asc) over (partition by metric_id, bsln_guid, timegroup) as pctile_90
, percentile_cont(0.95) within group (order by obs_value asc) over (partition by metric_id, bsln_guid, timegroup) as pctile_95
, percentile_cont(0.99) within group (order by obs_value asc) over (partition by metric_id, bsln_guid, timegroup) as pctile_99
from timegrouped_rawdata d

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select trim(both ' ' from ' a ') from dual where trim(:a) is not null
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
with
clus_tab as (
select id,
a.attribute_name aname,
a.conditional_operator op,
nvl(a.attribute_str_value,
round(decode(a.attribute_name, n.col,
a.attribute_num_value * n.scale + n.shift,
a.attribute_num_value),4)) val,
a.attribute_support support,
a.attribute_confidence confidence
from table(dbms_data_mining.get_model_details_km('km_sh_clus_sample')) t,
table(t.rule.antecedent) a,
km_sh_sample_norm n
where a.attribute_name = n.col (+) and a.attribute_confidence > 0.55
),
clust as (
select id,
cast(collect(cattr(aname, op, to_char(val), support, confidence)) as cattrs) cl_attrs
from clus_tab
group by id
),
custclus as (
select t.cust_id, s.cluster_id, s.probability
from (select
cust_id
, cluster_set(km_sh_clus_sample, null, 0.2 using *) pset
from km_sh_sample_apply_prepared
where cust_id = 101362) t,
table(t.pset) s
)
select a.probability prob, a.cluster_id cl_id,
b.attr, b.op, b.val, b.supp, b.conf
from custclus a,
(select t.id, c.*
from clust t,
table(t.cl_attrs) c) b
where a.cluster_id = b.id
order by prob desc, cl_id asc, conf desc, attr asc, val asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select manager_id, last_name, hire_date,
count(*) over (partition by manager_id order by hire_date
range numtodsinterval(100, 'day') preceding) as t_count
from employees
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
select
listagg(column_value, ',') within group (order by column_value)
from
table(
cast(
multiset(
select 'one' from dual
union all
select 'two' from dual
) as t_str
)
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SELECT STALENESS,
OSIZE, OBJ#,
TYPE#,
ROW_NUMBER() OVER (PARTITION BY BO# ORDER BY STALENESS, OSIZE, OBJ#),
CASE WHEN ROW_NUMBER() OVER (PARTITION BY BO# ORDER BY STALENESS, OSIZE, OBJ#) = 1 THEN 64 ELSE 0 END +
CASE WHEN ROW_NUMBER() OVER (PARTITION BY (SELECT TCP0.BO# FROM TABCOMPART$ TCP0 WHERE TCP0.OBJ#=ST0.BO#) ORDER BY STALENESS, OSIZE, OBJ#) = 1 THEN 32
ELSE 0 END AFLAGS,
0 STATUS,
:B5 SID,
:B4 SERIAL#, PART#, BO#, LOC_STALE_PCT
FROM
A
Loading

0 comments on commit 884dcaf

Please sign in to comment.