@gingkoo/pandora-metabase 1.0.158-alpha.12 → 1.0.158-alpha.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -91,7 +91,7 @@ var Metabase = function Metabase(props) {
91
91
  _props$sqlDialect = props.sqlDialect,
92
92
  sqlDialect = _props$sqlDialect === void 0 ? 'mysql' : _props$sqlDialect,
93
93
  _props$sqlValidationE = props.sqlValidationEnabled,
94
- sqlValidationEnabled = _props$sqlValidationE === void 0 ? false : _props$sqlValidationE,
94
+ sqlValidationEnabled = _props$sqlValidationE === void 0 ? true : _props$sqlValidationE,
95
95
  _props$sqlValidationQ = props.sqlValidationQueryKind,
96
96
  sqlValidationQueryKind = _props$sqlValidationQ === void 0 ? 'query' : _props$sqlValidationQ;
97
97
  var store = (0, _useProvider.useStore)();
@@ -191,12 +191,19 @@ var Metabase = function Metabase(props) {
191
191
  intercept = false;
192
192
  _metaList = (0, _utils.normalizeLegacySummarizeForSave)((0, _utils.splitByUnion)(store.metaList));
193
193
  _metaList[0].patchVersion = '1.1.0';
194
- store.metaList.map(function (v) {
195
- var _v$list;
196
- var firstTableName = (_v$list = v.list) === null || _v$list === void 0 || (_v$list = _v$list[0]) === null || _v$list === void 0 || (_v$list = _v$list.table) === null || _v$list === void 0 ? void 0 : _v$list.name;
197
- if (v.type !== 'union' && !firstTableName) {
194
+ store.metaList.forEach(function (v) {
195
+ var _dataMeta$table, _dataMeta$subquery;
196
+ if (v.type === 'union') return;
197
+ var dataMeta = (v.list || []).find(function (item) {
198
+ if ((item === null || item === void 0 ? void 0 : item.type) !== 'data') return false;
199
+ return !item.hidden;
200
+ });
201
+ if (!dataMeta) return;
202
+ var hasTable = Boolean((_dataMeta$table = dataMeta.table) === null || _dataMeta$table === void 0 ? void 0 : _dataMeta$table.name);
203
+ var hasSubquery = Boolean(dataMeta.isSubquery && ((_dataMeta$subquery = dataMeta.subquery) === null || _dataMeta$subquery === void 0 ? void 0 : _dataMeta$subquery.length));
204
+ if (!hasTable && !hasSubquery) {
198
205
  intercept = true;
199
- return _pandora.Toast.warning((0, _locale.__)('data.pleaseSelectDataTable'));
206
+ _pandora.Toast.warning((0, _locale.__)('data.pleaseSelectDataTable'));
200
207
  }
201
208
  });
202
209
  if (!(saveLoading || intercept)) {
@@ -326,7 +333,7 @@ var Metabase = function Metabase(props) {
326
333
  className: 'Sqb-fun-btns',
327
334
  children: copy()
328
335
  }), store.metaList.map(function (v, index) {
329
- var _v$list2;
336
+ var _v$list;
330
337
  if (v.type === 'union' && v.union) {
331
338
  return (0, _jsxRuntime.jsx)("div", {
332
339
  className: (0, _classnames["default"])("Sqb-list"),
@@ -343,7 +350,7 @@ var Metabase = function Metabase(props) {
343
350
  className: (0, _classnames["default"])("Sqb-list"),
344
351
  onMouseDownCapture: stopReadonlyInteraction,
345
352
  onClickCapture: stopReadonlyInteraction,
346
- children: v === null || v === void 0 || (_v$list2 = v.list) === null || _v$list2 === void 0 ? void 0 : _v$list2.map(function (meta) {
353
+ children: v === null || v === void 0 || (_v$list = v.list) === null || _v$list === void 0 ? void 0 : _v$list.map(function (meta) {
347
354
  var metaType = meta.type,
348
355
  metaKey = meta.metaKey,
349
356
  hidden = meta.hidden;
@@ -2,7 +2,7 @@ import type { MetaListType, ToolbarType, FormulaTemplatesItem, ChangeType, Formu
2
2
  import { UnionEnum } from './store/enum';
3
3
  import { DataType } from './components/dialog/select-column-multiple';
4
4
  export type SqlDialect = 'mysql' | 'oracle' | 'sqlserver';
5
- export type SqlValidationQueryKind = 'query' | 'exists' | 'scalar';
5
+ export type SqlValidationQueryKind = 'query' | 'exists' | 'scalar' | 'fragment';
6
6
  export interface MetaListProps {
7
7
  name: string;
8
8
  type: 'group' | 'union';
@@ -47,6 +47,30 @@ var BlockTypeEnum = {
47
47
  subquery: 'subquery'
48
48
  };
49
49
  var SummarizeAlias = 'source';
50
+ var hasSqlText = function hasSqlText(value) {
51
+ return value != null && String(value).trim() !== '';
52
+ };
53
+ var qualifyIdentifier = function qualifyIdentifier(qualifier, name) {
54
+ if (hasSqlText(qualifier) && hasSqlText(name)) return "".concat(qualifier, ".").concat(name);
55
+ if (hasSqlText(name)) return String(name);
56
+ return hasSqlText(qualifier) ? String(qualifier) : '';
57
+ };
58
+ var formatFieldSql = function formatFieldSql() {
59
+ var item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
60
+ var fieldName = item.fieldName;
61
+ if (item.tableAlias === SummarizeAlias && hasSqlText(item.fieldAlias)) {
62
+ fieldName = item.fieldAlias;
63
+ }
64
+ if (!hasSqlText(fieldName)) fieldName = item.fieldAlias;
65
+ return qualifyIdentifier(item.tableAlias, fieldName);
66
+ };
67
+ var formatAliasedTable = function formatAliasedTable(tableExpr, alias) {
68
+ var asKeyword = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'AS ';
69
+ var expr = hasSqlText(tableExpr) ? String(tableExpr).trim() : '';
70
+ var tableAlias = hasSqlText(alias) ? String(alias).trim() : '';
71
+ if (expr && tableAlias) return "".concat(expr, " ").concat(asKeyword).concat(tableAlias);
72
+ return expr || (tableAlias ? "".concat(asKeyword).concat(tableAlias) : '');
73
+ };
50
74
  var SQL_GROUP_TYPE = {
51
75
  NUMBER: 'number',
52
76
  STRING: 'string',
@@ -120,12 +144,11 @@ var _handleBaseType2Sql9 = exports.handleBaseType2Sql = function handleBaseType2
120
144
  sql_items.push(item.val);
121
145
  }
122
146
  if (type === BlockTypeEnum.field) {
123
- var fileName = item.fieldName;
124
- if (item.tableAlias === SummarizeAlias && item.fieldAlias) {
125
- fileName = item.fieldAlias;
147
+ var identifier = formatFieldSql(item);
148
+ if (identifier) {
149
+ sql += " ".concat(identifier);
150
+ sql_items.push(identifier);
126
151
  }
127
- sql += " ".concat(item.tableAlias, ".").concat(fileName);
128
- sql_items.push("".concat(item.tableAlias, ".").concat(fileName));
129
152
  }
130
153
  if (type === BlockTypeEnum.inputNumber) {
131
154
  sql += " ".concat(item.val);
@@ -140,9 +163,12 @@ var _handleBaseType2Sql9 = exports.handleBaseType2Sql = function handleBaseType2
140
163
  sql_items.push("".concat(item.val));
141
164
  }
142
165
  if (type === BlockTypeEnum.constant) {
143
- sql += " ".concat(item.val);
144
- sql_items.push("".concat(item.val));
145
- constants.push(item.val);
166
+ var constantVal = hasSqlText(item.val) ? item.val : item.id;
167
+ if (hasSqlText(constantVal)) {
168
+ sql += " ".concat(constantVal);
169
+ sql_items.push("".concat(constantVal));
170
+ constants.push(constantVal);
171
+ }
146
172
  }
147
173
  if (type === BlockTypeEnum.inputStringList) {
148
174
  if (Array.isArray(item.val) && item.val.length) {
@@ -718,7 +744,7 @@ var _handleNesting = function handleNesting(data) {
718
744
  if (summarize.by.length) {
719
745
  obj.groupBy.groupColumns = summarize.by.map(function (v) {
720
746
  var _v$atoms;
721
- var fieldSql = (_v$atoms = v.atoms) !== null && _v$atoms !== void 0 && _v$atoms.length ? _handleBaseType2Sql9(v.atoms, db).sql.trim() || v.sql : v.sql || "".concat(v.alias, ".").concat(v.realName || v.name);
747
+ var fieldSql = (_v$atoms = v.atoms) !== null && _v$atoms !== void 0 && _v$atoms.length ? _handleBaseType2Sql9(v.atoms, db).sql.trim() || v.sql : v.sql || qualifyIdentifier(v.alias, v.realName || v.name);
722
748
  queryColumns.push({
723
749
  field_sql: "".concat(fieldSql, " as ").concat(v.fieldAlias),
724
750
  read_name: '',
@@ -860,7 +886,8 @@ var _sqlStruct2text3 = function sqlStruct2text(data) {
860
886
  if (hasInnerSql) {
861
887
  if (innerSqlStruct.queryColumns && innerSqlStruct.queryColumns.length) {
862
888
  sql += innerSqlStruct.queryColumns.map(function (v) {
863
- return "".concat(alias, ".").concat(v.name, " as ").concat(v.name);
889
+ var identifier = qualifyIdentifier(alias, v.name);
890
+ return hasSqlText(v.name) ? "".concat(identifier, " as ").concat(v.name) : identifier;
864
891
  }).join(', ') + ' ';
865
892
  flag = true;
866
893
  }
@@ -897,21 +924,18 @@ var _sqlStruct2text3 = function sqlStruct2text(data) {
897
924
  sql += 'FROM ';
898
925
  var as = db === procEnum.oracle ? '' : 'AS ';
899
926
  if (hasInnerSql) {
900
- sql += "(".concat(_sqlStruct2text3(innerSqlStruct, indent + 1, db).sql, ") ").concat(as).concat(alias, " ");
927
+ sql += "".concat(formatAliasedTable("(".concat(_sqlStruct2text3(innerSqlStruct, indent + 1, db).sql, ")"), alias, as), " ");
901
928
  } else {
902
929
  if (tableSubQuery) {
903
- sql += "(".concat(handleSqlUnion(tableSubQuery, db).sql, ") ").concat(as).concat(alias, " ");
930
+ sql += "".concat(formatAliasedTable("(".concat(handleSqlUnion(tableSubQuery, db).sql, ")"), alias, as), " ");
904
931
  } else {
905
- sql += "".concat(tableName, " ").concat(as).concat(alias, " ");
932
+ sql += "".concat(formatAliasedTable(tableName, alias, as), " ");
906
933
  }
907
934
  }
908
935
  if (joinTables) {
909
936
  joinTables.forEach(function (it) {
910
- if (it.subQuery) {
911
- sql += "".concat(it.joinType, " (").concat(handleSqlUnion(it.subQuery, db).sql, ") ").concat(as).concat(it.alias, " ").concat(it.joinCondition, " ");
912
- } else {
913
- sql += "".concat(it.joinType, " ").concat(it.tableName, " ").concat(as).concat(it.alias, " ").concat(it.joinCondition, " ");
914
- }
937
+ var joinTable = it.subQuery ? "(".concat(handleSqlUnion(it.subQuery, db).sql, ")") : it.tableName;
938
+ sql += "".concat(it.joinType, " ").concat(formatAliasedTable(joinTable, it.alias, as), " ").concat(it.joinCondition, " ");
915
939
  });
916
940
  }
917
941
  if (filters) {
@@ -1190,11 +1214,25 @@ var validateSqlSyntax = exports.validateSqlSyntax = function validateSqlSyntax(s
1190
1214
  }
1191
1215
  if (parenthesesDepth !== 0) addIssue('parentheses');
1192
1216
  if (caseStack.length) addIssue('case');
1217
+ if (tokens.some(function (token) {
1218
+ return token.value === 'UNDEFINED';
1219
+ })) addIssue('invalid-identifier');
1220
+ var invalidExpressionStart = new Set(['AND', 'OR', '=', '!=', '<>', '<', '>', '<=', '>=', ',', '.']);
1221
+ var invalidExpressionEnd = new Set(['AND', 'OR', 'NOT', '=', '!=', '<>', '<', '>', '<=', '>=', '+', '-', '*', '/', '%', ',', '.']);
1222
+ if (!completeQuery) {
1223
+ var segment = tokens.filter(function (token) {
1224
+ return token.value !== ';';
1225
+ });
1226
+ if (segment.length) {
1227
+ if (invalidExpressionStart.has(segment[0].value)) addIssue('dangling-operator');
1228
+ if (invalidExpressionEnd.has(segment[segment.length - 1].value)) {
1229
+ addIssue('dangling-operator');
1230
+ }
1231
+ }
1232
+ }
1193
1233
  if (completeQuery) {
1194
1234
  var queryBoundaryWords = new Set(['UNION']);
1195
1235
  var clauseBoundaryWords = new Set(['JOIN', 'WHERE', 'GROUP', 'HAVING', 'ORDER', 'LIMIT', 'UNION']);
1196
- var invalidExpressionStart = new Set(['AND', 'OR', '=', '!=', '<>', '<', '>', '<=', '>=', ',', '.']);
1197
- var invalidExpressionEnd = new Set(['AND', 'OR', 'NOT', '=', '!=', '<>', '<', '>', '<=', '>=', '+', '-', '*', '/', '%', ',', '.']);
1198
1236
  var findBoundary = function findBoundary(start, depth, words) {
1199
1237
  for (var _cursor = start; _cursor < tokens.length; _cursor += 1) {
1200
1238
  if (tokenDepths[_cursor] < depth) return _cursor;
@@ -1227,9 +1265,6 @@ var validateSqlSyntax = exports.validateSqlSyntax = function validateSqlSyntax(s
1227
1265
  if (!tokens.some(function (token) {
1228
1266
  return token.value === 'SELECT';
1229
1267
  })) addIssue('empty-query');
1230
- if (tokens.some(function (token) {
1231
- return token.value === 'UNDEFINED';
1232
- })) addIssue('invalid-identifier');
1233
1268
  tokens.forEach(function (current, index) {
1234
1269
  var token = current.value;
1235
1270
  var depth = tokenDepths[index];
@@ -1386,11 +1421,39 @@ var createIssueCollector = function createIssueCollector() {
1386
1421
  }
1387
1422
  };
1388
1423
  };
1424
+ var _collectMetaItems = function collectMetaItems(metas) {
1425
+ var acc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
1426
+ if (!Array.isArray(metas)) return acc;
1427
+ metas.forEach(function (item) {
1428
+ if (!item) return;
1429
+ if (item.type === 'group') {
1430
+ _collectMetaItems(item.list, acc);
1431
+ return;
1432
+ }
1433
+ acc.push(item);
1434
+ if (item.type === MetabaseTypeEnum.union || item.type === 'union') {
1435
+ _collectMetaItems(item.subquery, acc);
1436
+ _collectMetaItems(item.list, acc);
1437
+ }
1438
+ });
1439
+ return acc;
1440
+ };
1441
+ var hasCompleteQuerySource = function hasCompleteQuerySource(metas) {
1442
+ return _collectMetaItems(metas).some(function (item) {
1443
+ return item.type === MetabaseTypeEnum.data && !item.hidden;
1444
+ });
1445
+ };
1446
+ var getQuerySourceOptions = function getQuerySourceOptions(metas) {
1447
+ return {
1448
+ skipQuerySource: !hasCompleteQuerySource(metas)
1449
+ };
1450
+ };
1389
1451
  var validateNestedMetasForSyntax = function validateNestedMetasForSyntax(metas, ctx, db, path) {
1390
1452
  var noFields = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
1391
1453
  if (!Array.isArray(metas) || !metas.length) return;
1392
1454
  try {
1393
1455
  var sqlResult = handleSqlUnion(metas, db, noFields);
1456
+ if (noFields && !hasCompleteQuerySource(metas)) return;
1394
1457
  ctx.merge(validateSqlSyntax(sqlResult.sql), path);
1395
1458
  if (!noFields) {
1396
1459
  var fieldCounts = (sqlResult.union_fields || []).map(function (fields) {
@@ -1409,6 +1472,7 @@ var validateNestedMetasForSyntax = function validateNestedMetasForSyntax(metas,
1409
1472
  };
1410
1473
  var _walkMetasForSyntax = function walkMetasForSyntax(metas, ctx, db) {
1411
1474
  var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1475
+ var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
1412
1476
  if (!Array.isArray(metas)) return;
1413
1477
  metas.forEach(function (item, index) {
1414
1478
  if (!item) return;
@@ -1416,9 +1480,10 @@ var _walkMetasForSyntax = function walkMetasForSyntax(metas, ctx, db) {
1416
1480
  _walkMetasForSyntax(item.list, ctx, db, [].concat((0, _toConsumableArray2["default"])(path), [{
1417
1481
  type: 'group',
1418
1482
  index: index
1419
- }]));
1483
+ }]), options);
1420
1484
  return;
1421
1485
  }
1486
+ if (item.hidden) return;
1422
1487
  var itemPath = [].concat((0, _toConsumableArray2["default"])(path), [{
1423
1488
  type: item.type,
1424
1489
  metaKey: item.metaKey,
@@ -1429,23 +1494,26 @@ var _walkMetasForSyntax = function walkMetasForSyntax(metas, ctx, db) {
1429
1494
  ctx.addIssue('union', 'error', itemPath);
1430
1495
  }
1431
1496
  validateNestedMetasForSyntax(item.subquery, ctx, db, itemPath);
1432
- _walkMetasForSyntax(item.subquery, ctx, db, itemPath);
1433
- _walkMetasForSyntax(item.list, ctx, db, itemPath);
1497
+ _walkMetasForSyntax(item.subquery, ctx, db, itemPath, getQuerySourceOptions(item.subquery));
1498
+ _walkMetasForSyntax(item.list, ctx, db, itemPath, getQuerySourceOptions(item.list));
1434
1499
  return;
1435
1500
  }
1436
1501
  if (item.type === MetabaseTypeEnum.data) {
1437
- var _item$table;
1438
- if (!((_item$table = item.table) !== null && _item$table !== void 0 && _item$table.name)) ctx.addIssue('from', 'error', itemPath);
1439
1502
  var subqueryPath = [].concat((0, _toConsumableArray2["default"])(itemPath), [{
1440
1503
  type: 'subquery'
1441
1504
  }]);
1442
- if (item.isSubquery) {
1443
- if (!Array.isArray(item.subquery) || !item.subquery.length) {
1505
+ if (!options.skipQuerySource) {
1506
+ var _item$table;
1507
+ if (!item.isSubquery && !((_item$table = item.table) !== null && _item$table !== void 0 && _item$table.name)) ctx.addIssue('from', 'error', itemPath);
1508
+ if (item.isSubquery && (!Array.isArray(item.subquery) || !item.subquery.length)) {
1444
1509
  ctx.addIssue('from', 'error', subqueryPath);
1445
- } else {
1510
+ }
1511
+ }
1512
+ if (item.isSubquery) {
1513
+ if (Array.isArray(item.subquery) && item.subquery.length) {
1446
1514
  validateNestedMetasForSyntax(item.subquery, ctx, db, subqueryPath);
1447
1515
  }
1448
- _walkMetasForSyntax(item.subquery, ctx, db, subqueryPath);
1516
+ _walkMetasForSyntax(item.subquery, ctx, db, subqueryPath, getQuerySourceOptions(item.subquery));
1449
1517
  }
1450
1518
  }
1451
1519
  if (item.type === MetabaseTypeEnum.joinData) {
@@ -1463,7 +1531,7 @@ var _walkMetasForSyntax = function walkMetasForSyntax(metas, ctx, db) {
1463
1531
  } else {
1464
1532
  validateNestedMetasForSyntax(item.subquery, ctx, db, _subqueryPath);
1465
1533
  }
1466
- _walkMetasForSyntax(item.subquery, ctx, db, _subqueryPath);
1534
+ _walkMetasForSyntax(item.subquery, ctx, db, _subqueryPath, getQuerySourceOptions(item.subquery));
1467
1535
  }
1468
1536
  }
1469
1537
  if (item.type === MetabaseTypeEnum.customColumn) {
@@ -1503,7 +1571,7 @@ var _walkAtomsDeepForSyntax = function walkAtomsDeepForSyntax(list, ctx, db) {
1503
1571
  return value == null || value === '' || Array.isArray(value) && value.length === 0;
1504
1572
  };
1505
1573
  var emptyInputAllowed = [BlockTypeEnum.inputString, BlockTypeEnum.inputNumber, BlockTypeEnum.inputStringList, BlockTypeEnum.inputNumberList].includes(item.type);
1506
- if (item.type === BlockTypeEnum.unknown || item.type === BlockTypeEnum.field && !item.fieldName || item.type === BlockTypeEnum.operator && !item.val || item.type === BlockTypeEnum.constant && isEmptyValue(item.id || item.val) || item.type === BlockTypeEnum.select && isEmptyValue(item.val) || item.type === BlockTypeEnum.FORMULA && !item.name || !emptyInputAllowed && !Object.values(BlockTypeEnum).includes(item.type) && 'val' in item && isEmptyValue(item.val)) {
1574
+ if (item.type === BlockTypeEnum.unknown || item.type === BlockTypeEnum.field && !item.fieldName && !item.fieldAlias || item.type === BlockTypeEnum.operator && !item.val || item.type === BlockTypeEnum.constant && isEmptyValue(item.id || item.val) || item.type === BlockTypeEnum.select && isEmptyValue(item.val) || item.type === BlockTypeEnum.FORMULA && !item.name || !emptyInputAllowed && !Object.values(BlockTypeEnum).includes(item.type) && 'val' in item && isEmptyValue(item.val)) {
1507
1575
  ctx.addIssue('missing-selection', 'error', path);
1508
1576
  }
1509
1577
  if (item.type === BlockTypeEnum.caseWhen) {
@@ -1581,7 +1649,7 @@ var _walkAtomsDeepForSyntax = function walkAtomsDeepForSyntax(list, ctx, db) {
1581
1649
  } else {
1582
1650
  validateNestedMetasForSyntax(item.notExists, ctx, db, nestedPath, true);
1583
1651
  }
1584
- _walkMetasForSyntax(item.notExists, ctx, db, nestedPath);
1652
+ _walkMetasForSyntax(item.notExists, ctx, db, nestedPath, getQuerySourceOptions(item.notExists));
1585
1653
  }
1586
1654
  if (item.type === BlockTypeEnum.subquery) {
1587
1655
  var _nestedPath = [].concat((0, _toConsumableArray2["default"])(path), [{
@@ -1594,7 +1662,7 @@ var _walkAtomsDeepForSyntax = function walkAtomsDeepForSyntax(list, ctx, db) {
1594
1662
  } else {
1595
1663
  validateNestedMetasForSyntax(item.subQuery, ctx, db, _nestedPath);
1596
1664
  }
1597
- _walkMetasForSyntax(item.subQuery, ctx, db, _nestedPath);
1665
+ _walkMetasForSyntax(item.subQuery, ctx, db, _nestedPath, getQuerySourceOptions(item.subQuery));
1598
1666
  }
1599
1667
  });
1600
1668
  };
@@ -1633,35 +1701,40 @@ var validateMetabaseConfig = exports.validateMetabaseConfig = function validateM
1633
1701
  var ctx = createIssueCollector();
1634
1702
  var sql = '';
1635
1703
  var queryKind = options.queryKind || 'query';
1704
+ var isFragmentQuery = queryKind === 'fragment' || !hasCompleteQuerySource(metas);
1636
1705
  try {
1637
1706
  var sqlResult = transformSql(metas, db, queryKind === 'exists');
1638
1707
  sql = sqlResult.sql;
1639
- var syntaxResult = validateSqlSyntax(sql);
1640
- if (queryKind === 'scalar') {
1641
- syntaxResult.issues = syntaxResult.issues.filter(function (issue) {
1642
- return issue.code !== 'select-list';
1643
- });
1644
- }
1645
- ctx.merge(syntaxResult);
1646
- if (queryKind !== 'exists') {
1647
- var fieldCounts = (sqlResult.union_fields || []).map(function (fields) {
1648
- return (fields === null || fields === void 0 ? void 0 : fields.length) || 0;
1649
- });
1650
- if (queryKind === 'scalar' && fieldCounts.some(function (count) {
1651
- return count !== 1;
1652
- })) {
1653
- ctx.addIssue('scalar-select-list', 'error');
1654
- } else if (!fieldCounts[0]) {
1655
- ctx.addIssue('select-list', 'error');
1708
+ if (!isFragmentQuery) {
1709
+ var syntaxResult = validateSqlSyntax(sql);
1710
+ if (queryKind === 'scalar') {
1711
+ syntaxResult.issues = syntaxResult.issues.filter(function (issue) {
1712
+ return issue.code !== 'select-list';
1713
+ });
1714
+ }
1715
+ ctx.merge(syntaxResult);
1716
+ if (queryKind !== 'exists') {
1717
+ var fieldCounts = (sqlResult.union_fields || []).map(function (fields) {
1718
+ return (fields === null || fields === void 0 ? void 0 : fields.length) || 0;
1719
+ });
1720
+ if (queryKind === 'scalar' && fieldCounts.some(function (count) {
1721
+ return count !== 1;
1722
+ })) {
1723
+ ctx.addIssue('scalar-select-list', 'error');
1724
+ } else if (!fieldCounts[0]) {
1725
+ ctx.addIssue('select-list', 'error');
1726
+ }
1727
+ if (fieldCounts.some(function (count) {
1728
+ return count !== fieldCounts[0];
1729
+ })) ctx.addIssue('union', 'error');
1656
1730
  }
1657
- if (fieldCounts.some(function (count) {
1658
- return count !== fieldCounts[0];
1659
- })) ctx.addIssue('union', 'error');
1660
1731
  }
1661
1732
  } catch (e) {
1662
1733
  ctx.addIssue('generation', 'error');
1663
1734
  }
1664
- _walkMetasForSyntax(metas, ctx, db);
1735
+ _walkMetasForSyntax(metas, ctx, db, [], {
1736
+ skipQuerySource: isFragmentQuery
1737
+ });
1665
1738
  return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, ctx.result()), {}, {
1666
1739
  sql: sql
1667
1740
  });
@@ -82,7 +82,7 @@ var Metabase = function Metabase(props) {
82
82
  _props$sqlDialect = props.sqlDialect,
83
83
  sqlDialect = _props$sqlDialect === void 0 ? 'mysql' : _props$sqlDialect,
84
84
  _props$sqlValidationE = props.sqlValidationEnabled,
85
- sqlValidationEnabled = _props$sqlValidationE === void 0 ? false : _props$sqlValidationE,
85
+ sqlValidationEnabled = _props$sqlValidationE === void 0 ? true : _props$sqlValidationE,
86
86
  _props$sqlValidationQ = props.sqlValidationQueryKind,
87
87
  sqlValidationQueryKind = _props$sqlValidationQ === void 0 ? 'query' : _props$sqlValidationQ;
88
88
  var store = useStore();
@@ -182,12 +182,19 @@ var Metabase = function Metabase(props) {
182
182
  intercept = false;
183
183
  _metaList = normalizeLegacySummarizeForSave(splitByUnion(store.metaList));
184
184
  _metaList[0].patchVersion = '1.1.0';
185
- store.metaList.map(function (v) {
186
- var _v$list;
187
- var firstTableName = (_v$list = v.list) === null || _v$list === void 0 || (_v$list = _v$list[0]) === null || _v$list === void 0 || (_v$list = _v$list.table) === null || _v$list === void 0 ? void 0 : _v$list.name;
188
- if (v.type !== 'union' && !firstTableName) {
185
+ store.metaList.forEach(function (v) {
186
+ var _dataMeta$table, _dataMeta$subquery;
187
+ if (v.type === 'union') return;
188
+ var dataMeta = (v.list || []).find(function (item) {
189
+ if ((item === null || item === void 0 ? void 0 : item.type) !== 'data') return false;
190
+ return !item.hidden;
191
+ });
192
+ if (!dataMeta) return;
193
+ var hasTable = Boolean((_dataMeta$table = dataMeta.table) === null || _dataMeta$table === void 0 ? void 0 : _dataMeta$table.name);
194
+ var hasSubquery = Boolean(dataMeta.isSubquery && ((_dataMeta$subquery = dataMeta.subquery) === null || _dataMeta$subquery === void 0 ? void 0 : _dataMeta$subquery.length));
195
+ if (!hasTable && !hasSubquery) {
189
196
  intercept = true;
190
- return Toast.warning(__('data.pleaseSelectDataTable'));
197
+ Toast.warning(__('data.pleaseSelectDataTable'));
191
198
  }
192
199
  });
193
200
  if (!(saveLoading || intercept)) {
@@ -317,7 +324,7 @@ var Metabase = function Metabase(props) {
317
324
  className: 'Sqb-fun-btns',
318
325
  children: copy()
319
326
  }), store.metaList.map(function (v, index) {
320
- var _v$list2;
327
+ var _v$list;
321
328
  if (v.type === 'union' && v.union) {
322
329
  return _jsx("div", {
323
330
  className: cx("Sqb-list"),
@@ -334,7 +341,7 @@ var Metabase = function Metabase(props) {
334
341
  className: cx("Sqb-list"),
335
342
  onMouseDownCapture: stopReadonlyInteraction,
336
343
  onClickCapture: stopReadonlyInteraction,
337
- children: v === null || v === void 0 || (_v$list2 = v.list) === null || _v$list2 === void 0 ? void 0 : _v$list2.map(function (meta) {
344
+ children: v === null || v === void 0 || (_v$list = v.list) === null || _v$list === void 0 ? void 0 : _v$list.map(function (meta) {
338
345
  var metaType = meta.type,
339
346
  metaKey = meta.metaKey,
340
347
  hidden = meta.hidden;
package/lib/es/types.d.ts CHANGED
@@ -2,7 +2,7 @@ import type { MetaListType, ToolbarType, FormulaTemplatesItem, ChangeType, Formu
2
2
  import { UnionEnum } from './store/enum';
3
3
  import { DataType } from './components/dialog/select-column-multiple';
4
4
  export type SqlDialect = 'mysql' | 'oracle' | 'sqlserver';
5
- export type SqlValidationQueryKind = 'query' | 'exists' | 'scalar';
5
+ export type SqlValidationQueryKind = 'query' | 'exists' | 'scalar' | 'fragment';
6
6
  export interface MetaListProps {
7
7
  name: string;
8
8
  type: 'group' | 'union';
@@ -40,6 +40,30 @@ var BlockTypeEnum = {
40
40
  subquery: 'subquery'
41
41
  };
42
42
  var SummarizeAlias = 'source';
43
+ var hasSqlText = function hasSqlText(value) {
44
+ return value != null && String(value).trim() !== '';
45
+ };
46
+ var qualifyIdentifier = function qualifyIdentifier(qualifier, name) {
47
+ if (hasSqlText(qualifier) && hasSqlText(name)) return "".concat(qualifier, ".").concat(name);
48
+ if (hasSqlText(name)) return String(name);
49
+ return hasSqlText(qualifier) ? String(qualifier) : '';
50
+ };
51
+ var formatFieldSql = function formatFieldSql() {
52
+ var item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
53
+ var fieldName = item.fieldName;
54
+ if (item.tableAlias === SummarizeAlias && hasSqlText(item.fieldAlias)) {
55
+ fieldName = item.fieldAlias;
56
+ }
57
+ if (!hasSqlText(fieldName)) fieldName = item.fieldAlias;
58
+ return qualifyIdentifier(item.tableAlias, fieldName);
59
+ };
60
+ var formatAliasedTable = function formatAliasedTable(tableExpr, alias) {
61
+ var asKeyword = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'AS ';
62
+ var expr = hasSqlText(tableExpr) ? String(tableExpr).trim() : '';
63
+ var tableAlias = hasSqlText(alias) ? String(alias).trim() : '';
64
+ if (expr && tableAlias) return "".concat(expr, " ").concat(asKeyword).concat(tableAlias);
65
+ return expr || (tableAlias ? "".concat(asKeyword).concat(tableAlias) : '');
66
+ };
43
67
  var SQL_GROUP_TYPE = {
44
68
  NUMBER: 'number',
45
69
  STRING: 'string',
@@ -113,12 +137,11 @@ var _handleBaseType2Sql9 = function handleBaseType2Sql(list) {
113
137
  sql_items.push(item.val);
114
138
  }
115
139
  if (type === BlockTypeEnum.field) {
116
- var fileName = item.fieldName;
117
- if (item.tableAlias === SummarizeAlias && item.fieldAlias) {
118
- fileName = item.fieldAlias;
140
+ var identifier = formatFieldSql(item);
141
+ if (identifier) {
142
+ sql += " ".concat(identifier);
143
+ sql_items.push(identifier);
119
144
  }
120
- sql += " ".concat(item.tableAlias, ".").concat(fileName);
121
- sql_items.push("".concat(item.tableAlias, ".").concat(fileName));
122
145
  }
123
146
  if (type === BlockTypeEnum.inputNumber) {
124
147
  sql += " ".concat(item.val);
@@ -133,9 +156,12 @@ var _handleBaseType2Sql9 = function handleBaseType2Sql(list) {
133
156
  sql_items.push("".concat(item.val));
134
157
  }
135
158
  if (type === BlockTypeEnum.constant) {
136
- sql += " ".concat(item.val);
137
- sql_items.push("".concat(item.val));
138
- constants.push(item.val);
159
+ var constantVal = hasSqlText(item.val) ? item.val : item.id;
160
+ if (hasSqlText(constantVal)) {
161
+ sql += " ".concat(constantVal);
162
+ sql_items.push("".concat(constantVal));
163
+ constants.push(constantVal);
164
+ }
139
165
  }
140
166
  if (type === BlockTypeEnum.inputStringList) {
141
167
  if (Array.isArray(item.val) && item.val.length) {
@@ -712,7 +738,7 @@ var _handleNesting = function handleNesting(data) {
712
738
  if (summarize.by.length) {
713
739
  obj.groupBy.groupColumns = summarize.by.map(function (v) {
714
740
  var _v$atoms;
715
- var fieldSql = (_v$atoms = v.atoms) !== null && _v$atoms !== void 0 && _v$atoms.length ? _handleBaseType2Sql9(v.atoms, db).sql.trim() || v.sql : v.sql || "".concat(v.alias, ".").concat(v.realName || v.name);
741
+ var fieldSql = (_v$atoms = v.atoms) !== null && _v$atoms !== void 0 && _v$atoms.length ? _handleBaseType2Sql9(v.atoms, db).sql.trim() || v.sql : v.sql || qualifyIdentifier(v.alias, v.realName || v.name);
716
742
  queryColumns.push({
717
743
  field_sql: "".concat(fieldSql, " as ").concat(v.fieldAlias),
718
744
  read_name: '',
@@ -854,7 +880,8 @@ var _sqlStruct2text3 = function sqlStruct2text(data) {
854
880
  if (hasInnerSql) {
855
881
  if (innerSqlStruct.queryColumns && innerSqlStruct.queryColumns.length) {
856
882
  sql += innerSqlStruct.queryColumns.map(function (v) {
857
- return "".concat(alias, ".").concat(v.name, " as ").concat(v.name);
883
+ var identifier = qualifyIdentifier(alias, v.name);
884
+ return hasSqlText(v.name) ? "".concat(identifier, " as ").concat(v.name) : identifier;
858
885
  }).join(', ') + ' ';
859
886
  flag = true;
860
887
  }
@@ -891,21 +918,18 @@ var _sqlStruct2text3 = function sqlStruct2text(data) {
891
918
  sql += 'FROM ';
892
919
  var as = db === procEnum.oracle ? '' : 'AS ';
893
920
  if (hasInnerSql) {
894
- sql += "(".concat(_sqlStruct2text3(innerSqlStruct, indent + 1, db).sql, ") ").concat(as).concat(alias, " ");
921
+ sql += "".concat(formatAliasedTable("(".concat(_sqlStruct2text3(innerSqlStruct, indent + 1, db).sql, ")"), alias, as), " ");
895
922
  } else {
896
923
  if (tableSubQuery) {
897
- sql += "(".concat(handleSqlUnion(tableSubQuery, db).sql, ") ").concat(as).concat(alias, " ");
924
+ sql += "".concat(formatAliasedTable("(".concat(handleSqlUnion(tableSubQuery, db).sql, ")"), alias, as), " ");
898
925
  } else {
899
- sql += "".concat(tableName, " ").concat(as).concat(alias, " ");
926
+ sql += "".concat(formatAliasedTable(tableName, alias, as), " ");
900
927
  }
901
928
  }
902
929
  if (joinTables) {
903
930
  joinTables.forEach(function (it) {
904
- if (it.subQuery) {
905
- sql += "".concat(it.joinType, " (").concat(handleSqlUnion(it.subQuery, db).sql, ") ").concat(as).concat(it.alias, " ").concat(it.joinCondition, " ");
906
- } else {
907
- sql += "".concat(it.joinType, " ").concat(it.tableName, " ").concat(as).concat(it.alias, " ").concat(it.joinCondition, " ");
908
- }
931
+ var joinTable = it.subQuery ? "(".concat(handleSqlUnion(it.subQuery, db).sql, ")") : it.tableName;
932
+ sql += "".concat(it.joinType, " ").concat(formatAliasedTable(joinTable, it.alias, as), " ").concat(it.joinCondition, " ");
909
933
  });
910
934
  }
911
935
  if (filters) {
@@ -1184,11 +1208,25 @@ export var validateSqlSyntax = function validateSqlSyntax(sql) {
1184
1208
  }
1185
1209
  if (parenthesesDepth !== 0) addIssue('parentheses');
1186
1210
  if (caseStack.length) addIssue('case');
1211
+ if (tokens.some(function (token) {
1212
+ return token.value === 'UNDEFINED';
1213
+ })) addIssue('invalid-identifier');
1214
+ var invalidExpressionStart = new Set(['AND', 'OR', '=', '!=', '<>', '<', '>', '<=', '>=', ',', '.']);
1215
+ var invalidExpressionEnd = new Set(['AND', 'OR', 'NOT', '=', '!=', '<>', '<', '>', '<=', '>=', '+', '-', '*', '/', '%', ',', '.']);
1216
+ if (!completeQuery) {
1217
+ var segment = tokens.filter(function (token) {
1218
+ return token.value !== ';';
1219
+ });
1220
+ if (segment.length) {
1221
+ if (invalidExpressionStart.has(segment[0].value)) addIssue('dangling-operator');
1222
+ if (invalidExpressionEnd.has(segment[segment.length - 1].value)) {
1223
+ addIssue('dangling-operator');
1224
+ }
1225
+ }
1226
+ }
1187
1227
  if (completeQuery) {
1188
1228
  var queryBoundaryWords = new Set(['UNION']);
1189
1229
  var clauseBoundaryWords = new Set(['JOIN', 'WHERE', 'GROUP', 'HAVING', 'ORDER', 'LIMIT', 'UNION']);
1190
- var invalidExpressionStart = new Set(['AND', 'OR', '=', '!=', '<>', '<', '>', '<=', '>=', ',', '.']);
1191
- var invalidExpressionEnd = new Set(['AND', 'OR', 'NOT', '=', '!=', '<>', '<', '>', '<=', '>=', '+', '-', '*', '/', '%', ',', '.']);
1192
1230
  var findBoundary = function findBoundary(start, depth, words) {
1193
1231
  for (var _cursor = start; _cursor < tokens.length; _cursor += 1) {
1194
1232
  if (tokenDepths[_cursor] < depth) return _cursor;
@@ -1221,9 +1259,6 @@ export var validateSqlSyntax = function validateSqlSyntax(sql) {
1221
1259
  if (!tokens.some(function (token) {
1222
1260
  return token.value === 'SELECT';
1223
1261
  })) addIssue('empty-query');
1224
- if (tokens.some(function (token) {
1225
- return token.value === 'UNDEFINED';
1226
- })) addIssue('invalid-identifier');
1227
1262
  tokens.forEach(function (current, index) {
1228
1263
  var token = current.value;
1229
1264
  var depth = tokenDepths[index];
@@ -1380,11 +1415,39 @@ var createIssueCollector = function createIssueCollector() {
1380
1415
  }
1381
1416
  };
1382
1417
  };
1418
+ var _collectMetaItems = function collectMetaItems(metas) {
1419
+ var acc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
1420
+ if (!Array.isArray(metas)) return acc;
1421
+ metas.forEach(function (item) {
1422
+ if (!item) return;
1423
+ if (item.type === 'group') {
1424
+ _collectMetaItems(item.list, acc);
1425
+ return;
1426
+ }
1427
+ acc.push(item);
1428
+ if (item.type === MetabaseTypeEnum.union || item.type === 'union') {
1429
+ _collectMetaItems(item.subquery, acc);
1430
+ _collectMetaItems(item.list, acc);
1431
+ }
1432
+ });
1433
+ return acc;
1434
+ };
1435
+ var hasCompleteQuerySource = function hasCompleteQuerySource(metas) {
1436
+ return _collectMetaItems(metas).some(function (item) {
1437
+ return item.type === MetabaseTypeEnum.data && !item.hidden;
1438
+ });
1439
+ };
1440
+ var getQuerySourceOptions = function getQuerySourceOptions(metas) {
1441
+ return {
1442
+ skipQuerySource: !hasCompleteQuerySource(metas)
1443
+ };
1444
+ };
1383
1445
  var validateNestedMetasForSyntax = function validateNestedMetasForSyntax(metas, ctx, db, path) {
1384
1446
  var noFields = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
1385
1447
  if (!Array.isArray(metas) || !metas.length) return;
1386
1448
  try {
1387
1449
  var sqlResult = handleSqlUnion(metas, db, noFields);
1450
+ if (noFields && !hasCompleteQuerySource(metas)) return;
1388
1451
  ctx.merge(validateSqlSyntax(sqlResult.sql), path);
1389
1452
  if (!noFields) {
1390
1453
  var fieldCounts = (sqlResult.union_fields || []).map(function (fields) {
@@ -1403,6 +1466,7 @@ var validateNestedMetasForSyntax = function validateNestedMetasForSyntax(metas,
1403
1466
  };
1404
1467
  var _walkMetasForSyntax = function walkMetasForSyntax(metas, ctx, db) {
1405
1468
  var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1469
+ var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
1406
1470
  if (!Array.isArray(metas)) return;
1407
1471
  metas.forEach(function (item, index) {
1408
1472
  if (!item) return;
@@ -1410,9 +1474,10 @@ var _walkMetasForSyntax = function walkMetasForSyntax(metas, ctx, db) {
1410
1474
  _walkMetasForSyntax(item.list, ctx, db, [].concat(_toConsumableArray(path), [{
1411
1475
  type: 'group',
1412
1476
  index: index
1413
- }]));
1477
+ }]), options);
1414
1478
  return;
1415
1479
  }
1480
+ if (item.hidden) return;
1416
1481
  var itemPath = [].concat(_toConsumableArray(path), [{
1417
1482
  type: item.type,
1418
1483
  metaKey: item.metaKey,
@@ -1423,23 +1488,26 @@ var _walkMetasForSyntax = function walkMetasForSyntax(metas, ctx, db) {
1423
1488
  ctx.addIssue('union', 'error', itemPath);
1424
1489
  }
1425
1490
  validateNestedMetasForSyntax(item.subquery, ctx, db, itemPath);
1426
- _walkMetasForSyntax(item.subquery, ctx, db, itemPath);
1427
- _walkMetasForSyntax(item.list, ctx, db, itemPath);
1491
+ _walkMetasForSyntax(item.subquery, ctx, db, itemPath, getQuerySourceOptions(item.subquery));
1492
+ _walkMetasForSyntax(item.list, ctx, db, itemPath, getQuerySourceOptions(item.list));
1428
1493
  return;
1429
1494
  }
1430
1495
  if (item.type === MetabaseTypeEnum.data) {
1431
- var _item$table;
1432
- if (!((_item$table = item.table) !== null && _item$table !== void 0 && _item$table.name)) ctx.addIssue('from', 'error', itemPath);
1433
1496
  var subqueryPath = [].concat(_toConsumableArray(itemPath), [{
1434
1497
  type: 'subquery'
1435
1498
  }]);
1436
- if (item.isSubquery) {
1437
- if (!Array.isArray(item.subquery) || !item.subquery.length) {
1499
+ if (!options.skipQuerySource) {
1500
+ var _item$table;
1501
+ if (!item.isSubquery && !((_item$table = item.table) !== null && _item$table !== void 0 && _item$table.name)) ctx.addIssue('from', 'error', itemPath);
1502
+ if (item.isSubquery && (!Array.isArray(item.subquery) || !item.subquery.length)) {
1438
1503
  ctx.addIssue('from', 'error', subqueryPath);
1439
- } else {
1504
+ }
1505
+ }
1506
+ if (item.isSubquery) {
1507
+ if (Array.isArray(item.subquery) && item.subquery.length) {
1440
1508
  validateNestedMetasForSyntax(item.subquery, ctx, db, subqueryPath);
1441
1509
  }
1442
- _walkMetasForSyntax(item.subquery, ctx, db, subqueryPath);
1510
+ _walkMetasForSyntax(item.subquery, ctx, db, subqueryPath, getQuerySourceOptions(item.subquery));
1443
1511
  }
1444
1512
  }
1445
1513
  if (item.type === MetabaseTypeEnum.joinData) {
@@ -1457,7 +1525,7 @@ var _walkMetasForSyntax = function walkMetasForSyntax(metas, ctx, db) {
1457
1525
  } else {
1458
1526
  validateNestedMetasForSyntax(item.subquery, ctx, db, _subqueryPath);
1459
1527
  }
1460
- _walkMetasForSyntax(item.subquery, ctx, db, _subqueryPath);
1528
+ _walkMetasForSyntax(item.subquery, ctx, db, _subqueryPath, getQuerySourceOptions(item.subquery));
1461
1529
  }
1462
1530
  }
1463
1531
  if (item.type === MetabaseTypeEnum.customColumn) {
@@ -1497,7 +1565,7 @@ var _walkAtomsDeepForSyntax = function walkAtomsDeepForSyntax(list, ctx, db) {
1497
1565
  return value == null || value === '' || Array.isArray(value) && value.length === 0;
1498
1566
  };
1499
1567
  var emptyInputAllowed = [BlockTypeEnum.inputString, BlockTypeEnum.inputNumber, BlockTypeEnum.inputStringList, BlockTypeEnum.inputNumberList].includes(item.type);
1500
- if (item.type === BlockTypeEnum.unknown || item.type === BlockTypeEnum.field && !item.fieldName || item.type === BlockTypeEnum.operator && !item.val || item.type === BlockTypeEnum.constant && isEmptyValue(item.id || item.val) || item.type === BlockTypeEnum.select && isEmptyValue(item.val) || item.type === BlockTypeEnum.FORMULA && !item.name || !emptyInputAllowed && !Object.values(BlockTypeEnum).includes(item.type) && 'val' in item && isEmptyValue(item.val)) {
1568
+ if (item.type === BlockTypeEnum.unknown || item.type === BlockTypeEnum.field && !item.fieldName && !item.fieldAlias || item.type === BlockTypeEnum.operator && !item.val || item.type === BlockTypeEnum.constant && isEmptyValue(item.id || item.val) || item.type === BlockTypeEnum.select && isEmptyValue(item.val) || item.type === BlockTypeEnum.FORMULA && !item.name || !emptyInputAllowed && !Object.values(BlockTypeEnum).includes(item.type) && 'val' in item && isEmptyValue(item.val)) {
1501
1569
  ctx.addIssue('missing-selection', 'error', path);
1502
1570
  }
1503
1571
  if (item.type === BlockTypeEnum.caseWhen) {
@@ -1575,7 +1643,7 @@ var _walkAtomsDeepForSyntax = function walkAtomsDeepForSyntax(list, ctx, db) {
1575
1643
  } else {
1576
1644
  validateNestedMetasForSyntax(item.notExists, ctx, db, nestedPath, true);
1577
1645
  }
1578
- _walkMetasForSyntax(item.notExists, ctx, db, nestedPath);
1646
+ _walkMetasForSyntax(item.notExists, ctx, db, nestedPath, getQuerySourceOptions(item.notExists));
1579
1647
  }
1580
1648
  if (item.type === BlockTypeEnum.subquery) {
1581
1649
  var _nestedPath = [].concat(_toConsumableArray(path), [{
@@ -1588,7 +1656,7 @@ var _walkAtomsDeepForSyntax = function walkAtomsDeepForSyntax(list, ctx, db) {
1588
1656
  } else {
1589
1657
  validateNestedMetasForSyntax(item.subQuery, ctx, db, _nestedPath);
1590
1658
  }
1591
- _walkMetasForSyntax(item.subQuery, ctx, db, _nestedPath);
1659
+ _walkMetasForSyntax(item.subQuery, ctx, db, _nestedPath, getQuerySourceOptions(item.subQuery));
1592
1660
  }
1593
1661
  });
1594
1662
  };
@@ -1627,35 +1695,40 @@ export var validateMetabaseConfig = function validateMetabaseConfig(metas) {
1627
1695
  var ctx = createIssueCollector();
1628
1696
  var sql = '';
1629
1697
  var queryKind = options.queryKind || 'query';
1698
+ var isFragmentQuery = queryKind === 'fragment' || !hasCompleteQuerySource(metas);
1630
1699
  try {
1631
1700
  var sqlResult = transformSql(metas, db, queryKind === 'exists');
1632
1701
  sql = sqlResult.sql;
1633
- var syntaxResult = validateSqlSyntax(sql);
1634
- if (queryKind === 'scalar') {
1635
- syntaxResult.issues = syntaxResult.issues.filter(function (issue) {
1636
- return issue.code !== 'select-list';
1637
- });
1638
- }
1639
- ctx.merge(syntaxResult);
1640
- if (queryKind !== 'exists') {
1641
- var fieldCounts = (sqlResult.union_fields || []).map(function (fields) {
1642
- return (fields === null || fields === void 0 ? void 0 : fields.length) || 0;
1643
- });
1644
- if (queryKind === 'scalar' && fieldCounts.some(function (count) {
1645
- return count !== 1;
1646
- })) {
1647
- ctx.addIssue('scalar-select-list', 'error');
1648
- } else if (!fieldCounts[0]) {
1649
- ctx.addIssue('select-list', 'error');
1702
+ if (!isFragmentQuery) {
1703
+ var syntaxResult = validateSqlSyntax(sql);
1704
+ if (queryKind === 'scalar') {
1705
+ syntaxResult.issues = syntaxResult.issues.filter(function (issue) {
1706
+ return issue.code !== 'select-list';
1707
+ });
1708
+ }
1709
+ ctx.merge(syntaxResult);
1710
+ if (queryKind !== 'exists') {
1711
+ var fieldCounts = (sqlResult.union_fields || []).map(function (fields) {
1712
+ return (fields === null || fields === void 0 ? void 0 : fields.length) || 0;
1713
+ });
1714
+ if (queryKind === 'scalar' && fieldCounts.some(function (count) {
1715
+ return count !== 1;
1716
+ })) {
1717
+ ctx.addIssue('scalar-select-list', 'error');
1718
+ } else if (!fieldCounts[0]) {
1719
+ ctx.addIssue('select-list', 'error');
1720
+ }
1721
+ if (fieldCounts.some(function (count) {
1722
+ return count !== fieldCounts[0];
1723
+ })) ctx.addIssue('union', 'error');
1650
1724
  }
1651
- if (fieldCounts.some(function (count) {
1652
- return count !== fieldCounts[0];
1653
- })) ctx.addIssue('union', 'error');
1654
1725
  }
1655
1726
  } catch (e) {
1656
1727
  ctx.addIssue('generation', 'error');
1657
1728
  }
1658
- _walkMetasForSyntax(metas, ctx, db);
1729
+ _walkMetasForSyntax(metas, ctx, db, [], {
1730
+ skipQuerySource: isFragmentQuery
1731
+ });
1659
1732
  return _objectSpread(_objectSpread({}, ctx.result()), {}, {
1660
1733
  sql: sql
1661
1734
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gingkoo/pandora-metabase",
3
- "version": "1.0.158-alpha.12",
3
+ "version": "1.0.158-alpha.13",
4
4
  "description": "",
5
5
  "main": "lib/es/index.js",
6
6
  "module": "lib/es/index.js",